unit U_YCFinishedClothInspection;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, MovePanel, cxTextEdit,
  cxContainer, cxCurrencyEdit, BtnEdit, RM_e_main, RM_e_htm, RM_e_Graphic,
  RM_e_Jpeg, RM_e_txt, RM_e_csv, RM_E_llPDF, Math, Clipbrd, cxPC, cxButtonEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmYCFinishedClothInspection = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_Main: TDataSource;
    TBExport: TToolButton;
    OrderNo: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Label10: TLabel;
    JYID: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label4: TLabel;
    JYType: TComboBox;
    v1Column13: TcxGridDBColumn;
    Label5: TLabel;
    conno: TEdit;
    v1Column17: TcxGridDBColumn;
    Label12: TLabel;
    factoryName: TEdit;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Filler: TComboBox;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    MovePanel2: TMovePanel;
    P_Color: TEdit;
    v1Column32: TcxGridDBColumn;
    v1Column34: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    IsJYTime: TCheckBox;
    v1Column16: TcxGridDBColumn;
    Label1: TLabel;
    RKOrdID: TEdit;
    N3: TMenuItem;
    X1: TMenuItem;
    Panel3: TPanel;
    Panel4: TPanel;
    cxTabControl1: TcxTabControl;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    Order_Main: TClientDataSet;
    TBRafresh: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    x2: TMenuItem;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Label2: TLabel;
    P_CODE: TEdit;
    Tv1Column8: TcxGridDBColumn;
    Label3: TLabel;
    JTH: TEdit;
    Label6: TLabel;
    person: TEdit;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TBManageClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBZFClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure JYIDKeyPress(Sender: TObject; var Key: Char);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);

    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure MJStr2Change(Sender: TObject);
    procedure JYTypeChange(Sender: TObject);
    procedure KHConNOKeyPress(Sender: TObject; var Key: Char);
    procedure RKOrdIDKeyPress(Sender: TObject; var Key: Char);
    procedure N3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure X1Click(Sender: TObject);
    procedure B1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);

    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure x2Click(Sender: TObject);
    procedure v1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton2Click(Sender: TObject);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure InitForm();
    function Savedata(): Boolean;
    procedure SetStatus();
    procedure PrintCKMDG(MRKOrdID: string);
    { Private declarations }
  public
    FFInt: Integer;
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_Fun, U_MJEdit, U_ZDYHelp, U_SysLogHelp, U_SCPerson;

{$R *.dfm}
procedure TfrmYCFinishedClothInspection.SetStatus();
begin
  ToolButton1.Visible := False;
  ToolButton2.Visible := False;
  case cxTabControl1.TabIndex of
    0:
      begin
        ToolButton1.Visible := true;
      end;
    1:
      begin
        ToolButton2.Visible := true;
      end;
    2:
      begin

      end;

  end;

end;

procedure TfrmYCFinishedClothInspection.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmYCFinishedClothInspection.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmYCFinishedClothInspection.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + '2', Tv1, '检验管理');
  Close;
end;

procedure TfrmYCFinishedClothInspection.PrintCKMDG(MRKOrdID: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  ExportFtErpFile('成品仓库码单.rmf', ADOQueryTemp);
  with Order_Main do
  begin

    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      sql.Add(' exec P_CPCK_Print_RKMD ');
      SQL.Add(' @RKOrdID=''' + Trim(MRKOrdID) + '''');
      Open;
    end;
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(ADOQueryPrint.fieldbyname('RKNO').AsString);
      fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
      if FileExists(fImagePath) then
        DeleteFile(fImagePath);
      Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
    except
      application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\成品仓库码单.rmf';
    if FileExists(fPrintFile) then
    begin
      RMVariables['QRBARCODE'] := fImagePath;
      RM2.LoadFromFile(fPrintFile);

      RM2.ShowReport;
    end
    else
    begin
      Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
      Exit;
    end;

  end;

end;

procedure TfrmYCFinishedClothInspection.InitGrid();
var
  fwhere, Pwhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;

  if IsJYTime.Checked then
  begin
    fwhere := ' where MSCE>=2 AND   Filltime>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    fwhere := fwhere + ' and Filltime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  end
  else
    fwhere := ' where MSCE>=2 AND  1=1 ';

  Pwhere := SGetFilters(Panel1, 1, 2);
  if (Pwhere <> '') and (person.text <> '') then
  begin
    Pwhere := Pwhere + ' and ';
  end;
  if person.text <> '' then
  begin
    Pwhere := Pwhere + ' ( person1 like ''%' + trim(person.text) + '%''';
    Pwhere := Pwhere + ' or person2 like ''%' + trim(person.text) + '%''';
    Pwhere := Pwhere + ' or person3 like ''%' + trim(person.text) + '%''';
    Pwhere := Pwhere + ' or person4 like ''%' + trim(person.text) + '%''';
    Pwhere := Pwhere + ' or person5 like ''%' + trim(person.text) + '%'')';
  end;
//  ShowMessage(Pwhere);
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' and ' + Pwhere;

  case cxTabControl1.TabIndex of
    0:
      begin
        fwhere := fwhere + ' and YCFLAG=''0'' ';
      end;
    1:
      begin
        fwhere := fwhere + ' and YCFLAG=''1'' ';
      end;
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_Page_Cloth_Inspect ');
      sql.Add('@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;
end;

procedure TfrmYCFinishedClothInspection.InitForm();
var
  fsj: string;
begin
  ReadCxGrid(self.Caption + '2', Tv1, '检验管理');
  fsj := 'select distinct(Filler) name from JY_Cloth   ';
  SInitComBoxBySql(ADOQueryCmd, Filler, False, fsj);
//  Filler.ItemIndex := Filler.Items.IndexOf(trim(DName));
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 30;
  InitGrid();
end;

procedure TfrmYCFinishedClothInspection.TBFindClick(Sender: TObject);
var
  fsj, fwhere: string;
begin

end;

procedure TfrmYCFinishedClothInspection.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '检验报告');
end;

procedure TfrmYCFinishedClothInspection.TBRafreshClick(Sender: TObject);
begin
  RKOrdID.SetFocus;

  CurrentPage := 1;
  InitGrid();
//  TV1.OptionsData.Editing := False;
//  TV1.OptionsSelection.CellSelect := False;
end;

procedure TfrmYCFinishedClothInspection.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 500;
//  TV1.OptionsData.Editing := False;
//  TV1.OptionsSelection.CellSelect := False;
  InitForm();
  SetStatus();
end;

procedure TfrmYCFinishedClothInspection.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmYCFinishedClothInspection.TBManageClick(Sender: TObject);
var
  key: Char;
begin
  key := #13;
  if Order_Main.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_Cloth_CR where CRFlag in(''入库'',''出库'') and JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    if ADOQueryTemp.fieldbyname('CRFlag').Value = '入库' then
    begin
      Order_Main.EnableControls;
      Application.MessageBox('已产生入库数据不能编辑!', '提示', 0);
      Exit;
    end
    else
    begin
      Order_Main.EnableControls;
      Application.MessageBox('已产生出库数据不能编辑!', '提示', 0);
      Exit;
    end;
  end;
  try
    frmMJEdit := TfrmMJEdit.Create(Application);
    with frmMJEdit do
    begin
      JYID.Text := Trim(Self.Order_Main.fieldbyname('JYID').AsString);
      JYIDKeyPress(JYID, key);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmMJEdit.Free;
  end;
end;

procedure TfrmYCFinishedClothInspection.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main, True);
end;

procedure TfrmYCFinishedClothInspection.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main, False);
end;

procedure TfrmYCFinishedClothInspection.TBZFClick(Sender: TObject);
begin
  if Order_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要作废数据吗？作废后数据不能恢复。', '提示', 32 + 4) <> IDYES then
    Exit;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_Cloth_CR where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + ''' and CRFlag in(''入库'',''出库'')');
          Open;
        end;
        if ADOQueryTemp.IsEmpty = False then
        begin
          if ADOQueryTemp.fieldbyname('CRFlag').Value = '入库' then
          begin
            Order_Main.EnableControls;
            Application.MessageBox('已产生入库数据不能作废!', '提示', 0);
            Exit;
          end
          else
          begin
            Order_Main.EnableControls;
            Application.MessageBox('已产生出库数据不能作废!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          ADOQueryCmd.Connection.BeginTrans;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete JY_Cloth where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
            sql.Add('delete CK_Cloth_CR where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
            sql.Add('delete CK_Cloth_KC where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
            sql.Add('delete JY_Cloth_CD where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');

//            更新开卡
//            sql.Add('exec P_UPdate_LCKJY @SPID=''' + trim(Order_Main.fieldbyname('SPID').AsString) + '''');
            sql.Add('update PD_Plan_AnPai set jyflag=(case when isnull((select count(*) from JY_Cloth where apid=''' + trim(Order_Main.fieldbyname('apid').AsString) + '''),0)>0 then 1 else 0 end)');
            sql.Add('where apid =''' + trim(Order_Main.fieldbyname('apid').AsString) + ''' ');

            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' ' + quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(',' + quotedstr(trim(self.Caption)));
            sql.Add(',' + quotedstr(trim('作废成品检验数据')));
            sql.Add(',' + quotedstr('卷条码：' + trim(Order_Main.fieldbyname('JYID').AsString) + ' 卷号：' + trim(Order_Main.fieldbyname('JYXH').AsString) + ' 卡号：' + trim(Order_Main.fieldbyname('apid').AsString)));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
//            showmessage(sql.text);
            ExecSQL;
          end;

          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('exec P_JY_ChaiPi_Del ');
            sql.Add(' @OrgJYID=' + quotedstr(trim(Order_Main.FieldByName('OrgJYID').AsString)));
            sql.Add(',@JYID=' + quotedstr(trim(Order_Main.FieldByName('JYID').AsString)));
//            showmessage(sql.text);
            open;
          end;
          if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
          begin
            Order_Main.EnableControls;
            application.MessageBox(PChar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)), '提示信息', 0);
            ADOQueryCmd.Connection.RollbackTrans;
          end;
          ADOQueryCmd.Connection.CommitTrans;
        end;
        Order_Main.Delete;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;

end;

procedure TfrmYCFinishedClothInspection.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean = True then
      begin

        ADOQueryCmd.Connection.BeginTrans;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update JY_Cloth set YCFLAG=''1'' where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');

          ExecSQL;
        end;

        ADOQueryCmd.Connection.CommitTrans;

        Order_Main.Delete;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;
end;

function TfrmYCFinishedClothInspection.Savedata(): Boolean;
var
  maxno, LLID: string;
  HXPS: Integer;
  HXQty, HXMQty: Double;
begin
  HXPS := 0;
  HXQty := 0;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1 * from Contract_Cloth_LL where OrdSubId=''' + Trim(Order_Main.fieldbyname('SubId').AsString) + '''');
    sql.Add(' and FirstName=''' + Trim(Order_Main.fieldbyname('AOrddefstr4').AsString) + '''');
    Open;
  end;
  LLID := Trim(ADOQueryTemp.fieldbyname('LLID').AsString);
  if Trim(LLID) = '' then
  begin
    Result := False;
    Application.MessageBox('没有相对应的加工厂!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd, maxno, 'HX', 'Contract_Cloth_BefChkHX', 2, 1) = False then
    begin
      Result := False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取回修最大号失败!', '提示', 0);
      Exit;
    end;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          HXPS := HXPS + 1;
          HXQty := HXQty + Order_Main.FieldByName('MJLen').Value;
        end;
        Next;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('');
      Open;
    end;
  except
  end;
end;

procedure TfrmYCFinishedClothInspection.v1Column15PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with Order_Main do
  begin
    Edit;
    FieldByName('PanDing').Value := Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate WFB_MJJY Set PanDing=''' + Trim(mvalue) + '''');
    SQL.Add(' where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmYCFinishedClothInspection.JYIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(JYID.Text) < 4 then
      Exit;
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmYCFinishedClothInspection.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmYCFinishedClothInspection.BTNPClick(Sender: TObject);
begin
  if CurrentPage < Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmYCFinishedClothInspection.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmYCFinishedClothInspection.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmYCFinishedClothInspection.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmYCFinishedClothInspection.MJStr2Change(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmYCFinishedClothInspection.JYTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmYCFinishedClothInspection.KHConNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmYCFinishedClothInspection.RKOrdIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmYCFinishedClothInspection.N3Click(Sender: TObject);
var
  SSubId, SGangNo: string;
begin

  with Order_Main do
  begin
    SSubId := trim(FieldByName('SubId').AsString);
    Order_Main.DisableControls;
    first;
    while not eof do
    begin
      if SSubId = trim(FieldByName('SubId').AsString) then
      begin
        edit;
        FieldByName('SSel').value := true;
        Post;
      end
      else
      begin
        edit;
        FieldByName('SSel').value := false;
        Post;
      end;
      Next;
    end;
    Order_Main.Locate('SubId', VarArrayOf([SSubId]), []);
    Order_Main.EnableControls;
  end;
end;

procedure TfrmYCFinishedClothInspection.ToolButton4Click(Sender: TObject);
begin
  if Trim(Order_Main.fieldbyname('RKNO').AsString) = '' then
    exit;
  PrintCKMDG(Trim(Order_Main.fieldbyname('RKNO').AsString));
end;

procedure TfrmYCFinishedClothInspection.X1Click(Sender: TObject);
var
  str, SSubId: string;
  ZChiShu, DQChiShu: integer;
  ISSel: boolean;
begin
  ISSel := true;
  SSubId := trim(Order_Main.FieldByName('SubId').AsString);
  str := '';
  if InputQuery('选择', '请输入数字', str) then
  begin
    ZChiShu := strtointdef(str, 0);
    if ZChiShu <= 0 then
      exit;

    with Order_Main do
    begin

      Order_Main.DisableControls;
      first;
      DQChiShu := 0;
      while not eof do
      begin

        if SSubId = trim(FieldByName('SubId').AsString) then
        begin
          edit;
          FieldByName('SSel').value := ISSel;
          DQChiShu := DQChiShu + 1;
          Post;
        end
        else
        begin
          edit;
          FieldByName('SSel').value := false;
          Post;
        end;
        if DQChiShu = ZChiShu then
          ISSel := false;
        Next;
      end;
      Order_Main.Locate('SubId', VarArrayOf([SSubId]), []);
      Order_Main.EnableControls;
    end;
  end;

end;

procedure TfrmYCFinishedClothInspection.B1Click(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmYCFinishedClothInspection.cxTabControl1Change(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
  SetStatus();
end;

procedure TfrmYCFinishedClothInspection.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  mvalue, FFieldName, FValue: string;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    frmSCPerson := TfrmSCPerson.Create(Self);
    with frmSCPerson do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        mvalue := frmSCPerson.FSDPerson;

      end;
    end
  finally
    frmSCPerson.free;

  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JY_Cloth ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where JYID=' + quotedstr(Order_Main.fieldbyname('JYID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();

  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end

end;

procedure TfrmYCFinishedClothInspection.x2Click(Sender: TObject);
begin
  TV1.OptionsData.Editing := True;
  TV1.OptionsSelection.CellSelect := True;
end;

procedure TfrmYCFinishedClothInspection.v1Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName, FValue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if mvalue = '' then
  begin
    mvalue := '0';
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JY_Cloth ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where JYID=' + quotedstr(Order_Main.fieldbyname('JYID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();

  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end

end;

procedure TfrmYCFinishedClothInspection.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if (AViewInfo.GridRecord.Values[v1Column12.Index] = '次品') then
  begin
    ACanvas.Brush.Color := $008080FF
  end;

end;

procedure TfrmYCFinishedClothInspection.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean = True then
      begin

        ADOQueryCmd.Connection.BeginTrans;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update JY_Cloth set YCFLAG=''0'' where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');

          ExecSQL;
        end;

        ADOQueryCmd.Connection.CommitTrans;

        Order_Main.Delete;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;
end;

end.

