unit U_TradeClothInspList;

interface

uses
  Windows, MMSystem, 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, Math, Clipbrd, cxPC, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, dxDateRanges, U_BaseList,
  dxDateTimeWheelPicker, dxSkinWXI, dxScrollbarAnnotations, cxMaskEdit,
  cxDropDownEdit, dxCore, cxDateUtils, dxLayoutcxEditAdapters, dxLayoutContainer,
  dxLayoutControl, System.Diagnostics, cxGroupBox, RM_BarCode, RM_RichEdit,
  RM_E_llPDF, dxX509Certificate, dxPDFCore, dxPDFBase, dxPDFText,
  dxPDFRecognizedObject, dxPDFForm, dxPDFFormData, dxPDFDocument, dxPrintUtils,
  dxCustomPreview, dxPDFDocumentViewer, dxPDFViewer, cxProgressBar,
  dxSkinsDefaultPainters;

type
  TfrmTradeClothInspList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    OrderNo: TcxTextEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    conNO: TcxTextEdit;
    C_Name: TcxTextEdit;
    btnEdit: TToolButton;
    TBZF: TToolButton;
    btnJBQ: TToolButton;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    MovePanel2: TMovePanel;
    C_Color: TcxTextEdit;
    BCIOID: TcxTextEdit;
    btnJYBG: TToolButton;
    CIMachNo: TcxTextEdit;
    C_Pattern: TcxTextEdit;
    C_Code: TcxTextEdit;
    C_EColor: TcxTextEdit;
    ToolButton1: TToolButton;
    PM_1: TPopupMenu;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1ssel: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1MKXS: TcxGridDBColumn;
    v1Column34: TcxGridDBColumn;
    v1Column40: TcxGridDBColumn;
    v1Column46: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    ToolButton2: TToolButton;
    BatchNO: TcxTextEdit;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    ToolButton3: TToolButton;
    grade: TcxComboBox;
    CIIOFlag: TcxComboBox;
    TCBNOR: TcxComboBox;
    Tv1AddwetPresent: TcxGridDBColumn;
    Tv1AddLenPresent: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Tout: TToolButton;
    Tunout: TToolButton;
    BegDate: TcxDateEdit;
    EndDate: TcxDateEdit;
    dxLayoutControl_queryGroup_Root: TdxLayoutGroup;
    dxLayoutControl_query: TdxLayoutControl;
    dxLayoutGroup1: TdxLayoutGroup;
    dxLayoutGroup2: TdxLayoutGroup;
    dxLayoutItem1: TdxLayoutItem;
    dxLayoutItem2: TdxLayoutItem;
    dxLayoutItem3: TdxLayoutItem;
    dxLayoutItem4: TdxLayoutItem;
    dxLayoutItem5: TdxLayoutItem;
    dxLayoutItem6: TdxLayoutItem;
    dxLayoutItem7: TdxLayoutItem;
    dxLayoutItem8: TdxLayoutItem;
    dxLayoutItem9: TdxLayoutItem;
    dxLayoutItem10: TdxLayoutItem;
    dxLayoutItem11: TdxLayoutItem;
    dxLayoutItem12: TdxLayoutItem;
    dxLayoutItem13: TdxLayoutItem;
    dxLayoutItem14: TdxLayoutItem;
    dxLayoutItem15: TdxLayoutItem;
    dxLayoutItem16: TdxLayoutItem;
    IsJYTime: TcxCheckBox;
    dxLayoutEmptySpaceItem1: TdxLayoutEmptySpaceItem;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    cxGroupBox1: TcxGroupBox;
    edit_CKMJID: TcxTextEdit;
    MovePanel1: TMovePanel;
    Tprintmd: TToolButton;
    Tv1custName: TcxGridDBColumn;
    custName: TcxTextEdit;
    dxLayoutItem17: TdxLayoutItem;
    PopupMenu2: TPopupMenu;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    Tv1lenUnit: TcxGridDBColumn;
    RMRichObject1: TRMRichObject;
    RMBarCodeObject1: TRMBarCodeObject;
    RMXLSExport1: TRMXLSExport;
    RMCSVExport1: TRMCSVExport;
    N10: TMenuItem;
    RMllPDFExport1: TRMllPDFExport;
    dxPDFViewer1: TdxPDFViewer;
    RMXLSExport2: TRMXLSExport;
    Tv1unitPieces: TcxGridDBColumn;
    Tv1operators: TcxGridDBColumn;
    Label1: TLabel;
    PopupMenu3: TPopupMenu;
    MenuItem1: TMenuItem;
    cxCheckBox1: TcxCheckBox;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    dxLayoutItem18: TdxLayoutItem;
    C_StyleNo: TcxTextEdit;
    dxLayoutItem19: TdxLayoutItem;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure btnEditClick(Sender: TObject);
    procedure TBZFClick(Sender: TObject);
    procedure btnJBQClick(Sender: TObject);
    procedure OrderNoKeyPress(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 conNOKeyPress(Sender: TObject; var Key: Char);
    procedure CIIOFlagChange(Sender: TObject);
    procedure GradeChange(Sender: TObject);
    procedure KHConNOKeyPress(Sender: TObject; var Key: Char);
    procedure RKOrdIDKeyPress(Sender: TObject; var Key: Char);
    procedure btnJYBGClick(Sender: TObject);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure cxPageControl1Change(Sender: TObject);
    procedure chkZFClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToutClick(Sender: TObject);
    procedure TunoutClick(Sender: TObject);
    procedure CIIOFlagPropertiesChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure edit_CKMJIDKeyPress(Sender: TObject; var Key: Char);
    procedure cxGroupBox1DblClick(Sender: TObject);
    procedure TprintmdClick(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure N10Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure Label1Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure v1sselPropertiesEditValueChanged(Sender: TObject);
    procedure cxCheckBox1Click(Sender: TObject);
  private
    fstopwatch: TStopwatch;
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    function Savedata(): Boolean;
    procedure SetStatus();
    procedure PrintCKMDG(MRKOrdID: string);
    procedure threadLoadform();
    function autoOut(mccids: string): boolean;
    procedure selDisp();
    { Private declarations }
  public
    FFInt: Integer;
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_TradePack, U_RTFun, U_globalVar, U_TradeInspEdit, U_ZDYHelp,
  U_LabelPrint, U_SysLogList, U_ControlData, U_FormLayOutDesign, U_MdPrint,
  U_BatchMdyData, U_iniparam;

{$R *.dfm}

procedure TfrmTradeClothInspList.SetStatus();
begin
//  btnEdit.Enabled := False;
//  TBZF.Enabled := False;
//  btnHY.Enabled := False;
//  btnJBQ.Enabled := False;
//  btnJYBG.Enabled := False;
//
//  btnEdit.Enabled := True;
//  TBZF.Enabled := True;
//  btnJBQ.Enabled := True;
//  btnJYBG.Enabled := True;

end;

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

procedure TfrmTradeClothInspList.FormCreate(Sender: TObject);
begin
 // fstopwatch := TStopwatch.StartNew;

  inherited;
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmTradeClothInspList.TBCloseClick(Sender: TObject);
begin
  inherited;
  Close;
end;

procedure TfrmTradeClothInspList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmTradeClothInspList.PrintCKMDG(MRKOrdID: string);
begin

end;

procedure TfrmTradeClothInspList.InitGrid();
var
  WSql: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  WSql := SLGetFilters(dxLayoutControl_query, 1, 2);
  if trim(WSql) <> '' then
  begin
    WSql := ' and ' + trim(WSql);
  end;

  if IsJYTime.Checked then
  begin
    WSql := WSql + ' and A.Filltime>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.Date));
    WSql := WSql + ' and A.Filltime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.Date + 1));
  end
  else
  begin
    if trim(WSql) <> '' then
      WSql := ' and 1=1' + trim(WSql)
    else
      WSql := WSql + ' and 1=2 ';
  end;
  if trim(WSql) = 'and 1=2' then
    Application.MessageBox('勾选检验日期或填入搜索条件', '提示', 0);

  try
    tv1.BeginUpdate();
    Order_Main.DisableControls;

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_Trade_Insp_Get_Page ');
      sql.Add('@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(WSql));
      Open;
    end;
    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));

    MovePanel2.Visible := false;
  finally
    Order_Main.EnableControls;
    MovePanel2.Visible := false;
    TV1.DataController.Filter.Clear;
    tv1.EndUpdate;
  end;
end;

procedure TfrmTradeClothInspList.TBRafreshClick(Sender: TObject);
begin
  OrderNo.SetFocus;
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmTradeClothInspList.FormShow(Sender: TObject);
begin
  inherited;
  edit_CKMJID.Properties.MaxLength := gScanBarcodeMaxLen;
  canshu1 := self.fParameters1;
  CurrentPage := 1;
  RecordsNumber := 500;
  InitDevCombobox(ADOQueryTemp, grade, 'CPGRADE', false);

  EndDate.Date := SGetServerDate(ADOQueryTemp);
  BegDate.Date := EndDate.Date;
//
  //ReadCxGrid(Trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
 // InitGrid();
 // SetStatus();
  threadLoadform();
  application.ProcessMessages;
  //fstopwatch.Stop;
 // showMessage(IntToStr(fstopwatch.ElapsedMilliseconds) + ' ms');
end;

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

procedure TfrmTradeClothInspList.v1sselPropertiesEditValueChanged(Sender: TObject);
begin
  inherited;
  selDisp();
end;

procedure TfrmTradeClothInspList.btnEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Trade_Cloth_IO where IOFlag=''入库'' and stkid=''' + Trim(Order_Main.fieldbyname('CIID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Order_Main.EnableControls;
    Application.MessageBox('已产生入库数据不能编辑!', '提示', 0);
    Exit;
  end;
  try
    frmTradeInspEdit := TfrmTradeInspEdit.Create(Application);
    with frmTradeInspEdit do
    begin
      CIID.Text := Trim(Self.Order_Main.fieldbyname('CIID').AsString);
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmTradeInspEdit.Free;

  end;
end;

procedure TfrmTradeClothInspList.TBZFClick(Sender: TObject);
var
  MCIIDS: string;
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

        MCIIDS := MCIIDS + Trim(Order_Main.fieldbyname('CIID').AsString) + ',';
        Order_Main.Delete;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Trade_Need_Up(UOperation,UType,UDataId) values(''检验删除'',''检验'',' + quotedstr(MCIIDS) + ')  ');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Trade_Insp_Del ');
      Sql.Add('@CIIDS=' + quotedstr(Trim(MCIIDS)));
      Sql.Add(',@DCode=' + Quotedstr(DCode));
      Sql.Add(',@DName=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CurrentPage := 1;
    InitGrid();
    application.MessageBox('删除成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTradeClothInspList.btnJBQClick(Sender: TObject);
var
  fPrintFile, FLabVolume, MStrIn: string;
  mCopys: Integer;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  MStrIn := '2';
  if not InputQuery('打印份数', '请输入', MStrIn) then
  begin
    Exit;
  end;
  mCopys := strToIntdef(trim(MStrIn), 2);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Trade_Cloth_Inspect Set PrtCount=PrtCount+1,PrtDate=getdate(),Prter=''' + Trim(DName) + '''');
          sql.Add(' where CIID=''' + Trim(Order_Main.fieldbyname('CIID').AsString) + '''');
          ExecSQL;
        end;

        with ADOQueryPrint do
        begin
          Close;
          SQL.Clear;
          sql.Add(' EXEC P_Trade_Print_djd_Lab ');
          SQL.Add(' @CIID=''' + Trim(Order_Main.fieldbyname('CIID').AsString) + '''');
          Open;
        end;
        if ADOQueryPrint.IsEmpty then
        begin
          application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
          exit;
        end;

        if Trim(ADOQueryPrint.fieldbyname('SLabVolume').AsString) <> '' then
          FLabVolume := Trim(ADOQueryPrint.fieldbyname('SLabVolume').AsString)
        else
          FLabVolume := Trim(ADOQueryPrint.fieldbyname('LabVolume').AsString);
         //默认标签
        if FLabVolume = '' then
          FLabVolume := trim(gDefaultLableFile);
        ExportFtErpFile(FLabVolume + '.rmf', ADOQueryTemp);
        fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + FLabVolume + '.rmf';

        if FileExists(fPrintFile) then
        begin
          RM1.LoadFromFile(fPrintFile);
         // if Trim(ADOQueryPrint.fieldbyname('LabVolume').AsString) <> '次品标签' then
         //   Rm1.DefaultCopies := 2
         // else
          Rm1.DefaultCopies := mCopys;
           // RM1.PrepareReport  ;

          RM1.PrintReport;
           // RM1.ExportTo(RMllPDFExport1,'C:\test.pdf');
        end
        else
        begin
          Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

function TfrmTradeClothInspList.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 TfrmTradeClothInspList.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

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

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

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

procedure TfrmTradeClothInspList.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(Trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
  if gIsCanDesign then
  begin
    saveLayOut(application, dxLayoutControl_query, ADOQueryBaseCmd, PWideChar(fDllFileName + '|' + Self.Name + '|' + dxLayoutControl_query.Name + '.ini'));
  end;
end;

procedure TfrmTradeClothInspList.ToolButton3Click(Sender: TObject);
var
  FPrintFile, MPacketId, MLabPackage: string;
begin

  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    filter := 'SSel=1 ';
    filtered := true;
  end;
  with Order_Main do
  begin
    while not Order_Main.eof do
    begin
      MPacketId := Trim(Order_Main.fieldbyname('PacketId').AsString);
      if MPacketId = '' then
      begin
        Order_Main.Next;
        Continue;
      end;
      if trim(MPacketId) <> '' then
      begin
        with ADOQueryPrint do
        begin
          Close;
          SQL.Clear;
          SQL.Add('exec P_Trade_Insp_Prt_djd_PackLab ' + quotedstr(MPacketId));
          Open;
        end;
        //
        if Trim(ADOQueryPrint.fieldbyname('SLabPackage').AsString) <> '' then
          MLabPackage := trim(ADOQueryPrint.FieldByName('SLabPackage').AsString)
        else
          MLabPackage := trim(ADOQueryPrint.FieldByName('LabPackage').AsString);

        if MLabPackage = '' then
          MLabPackage := trim(gDefaultPackLableFile);

        ExportFtErpFile(Trim(MLabPackage) + '.rmf', ADOQueryTemp);

        FPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(MLabPackage) + '.rmf';
        if not FileExists(FPrintFile) then
        begin
          Application.MessageBox(PChar('没有找' + FPrintFile), '提示', 0);
          Exit;
        end;
        if FileExists(FPrintFile) then
        begin
          RM1.LoadFromFile(FPrintFile);
          Rm1.DefaultCopies := 1;
          RM1.PrintReport;
        end
        else
        begin
          Application.MessageBox(PChar('没有找' + FPrintFile), '提示', 0);
        end;
        while Order_Main.Locate('SSel;PacketId', VarArrayOf([True, MPacketId]), []) do
        begin
          with Order_Main do
          begin
            Edit;
            Order_Main.FieldByName('SSel').Value := False;
            Post;
          end;
        end;
      end;
      Order_Main.Next;
    end;
  end;
  Order_Main.filtered := false;
  Order_Main.EnableControls;

end;

procedure TfrmTradeClothInspList.ToolButton4Click(Sender: TObject);
begin
  inherited;
  with TfrmTradePack.Create(Application.MainForm, '布匹打包') do       //
  begin
    show;
  end;
end;

procedure TfrmTradeClothInspList.ToolButton5Click(Sender: TObject);
begin
  inherited;
  frmSysLogList := TfrmSysLogList.create(self);
  with frmSysLogList do
  begin
    fModel := self.caption;
    FMainId := ''; // Trim(Self.Order_Main.FieldByName('mainId').AsString);
    showmodal;
    free;
  end;
end;

procedure TfrmTradeClothInspList.ToolButton6Click(Sender: TObject);
begin
//  with ADOQueryTemp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('SELECT * FROM [dbo].[Bs_Report_Cloud_File]');
//    sql.Add('where BLCLID=''D22E6757-43C2-45B9-AE14-921BD2293E50''');
//    open;
//  end;
//  if ADOQueryTemp.RecordCount>0 then
//  begin
//    rm1.LoadPreparedReportFromBlobField(TBlobField(ADOQueryTemp.fieldByName('GenerateFiles')));
//    rm1.ShowPreparedReport;
//    // rm1.PrintPreparedReport('',1,true,rmppAll)  ;
//  end;
end;

procedure TfrmTradeClothInspList.ToutClick(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 ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update Trade_Cloth_Inspect set CIIOFlag=''已出库'',IOTime=getdate() where CIID=''' + Trim(Order_Main.fieldbyname('CIID').AsString) + '''');
          ExecSQL;
        end;
        Order_Main.Edit;
        FieldByName('CIIOFlag').Value := '已出库';
        FieldByName('SSel').Value := false;
        Order_Main.Post;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmTradeClothInspList.TprintmdClick(Sender: TObject);
var
  WSql: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  WSql := '';
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + Trim(Order_Main.fieldbyname('CIID').AsString);
        end
        else
        begin
          WSql := Trim(Order_Main.fieldbyname('CIID').AsString);
        end;
      end;
      Next;
    end;
  end;
  Order_Main.Locate('SSel', True, []);
  Order_Main.EnableControls;
  try
    frmMdPrint := TfrmMdPrint.Create(Application);
    with frmMdPrint do
    begin
      FLMType := '入库码单';
      FFiltration1 := WSql;
      FFiltration2 := WSql;
      if ShowModal = 1 then
      begin
        if gPrintMdAutoOut then
          autoOut(WSql);
//        Self.InitGrid();
      end;
    end;
  finally
    frmMdPrint.Free;
  end;

end;

procedure TfrmTradeClothInspList.TunoutClick(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;
  try
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          //begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('update Trade_Cloth_Inspect set CIIOFlag=''已入库'',IOTime=null where CIID=''' + Trim(Order_Main.fieldbyname('CIID').AsString) + '''');
            ExecSQL;
          end;

          Edit;
          FieldByName('CIIOFlag').Value := '已入库';
          FieldByName('SSel').Value := false;
          Post;

          Next;
        end
        else
          Next;
      end;
    end;
  finally
    Order_Main.EnableControls;
    TBRafresh.Click;
  end;

end;

procedure TfrmTradeClothInspList.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 TfrmTradeClothInspList.chkZFClick(Sender: TObject);
begin
  inherited;
  CurrentPage := 1;
  SetStatus();
  InitGrid();
end;

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

procedure TfrmTradeClothInspList.cxCheckBox1Click(Sender: TObject);
begin
  if not cxCheckBox1.Checked then
    cxCheckBox1.Caption := '选择数据汇总显示'
  else
    selDisp();
end;

procedure TfrmTradeClothInspList.cxGroupBox1DblClick(Sender: TObject);
begin
  inherited;
  cxGroupBox1.Visible := false;
end;

procedure TfrmTradeClothInspList.cxPageControl1Change(Sender: TObject);
begin
  CurrentPage := 1;
  SetStatus();
  InitGrid();
end;

procedure TfrmTradeClothInspList.edit_CKMJIDKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
  if Key = #13 then
  begin
    if trim(Edit_CKMJID.Text) = '' then
      exit;

    try
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Trade_Cloth_Inspect  where ciid=' + quotedstr(Trim(Edit_CKMJID.text)));
        open;
        //set IIOFlag=''已出库''
        if recordCount <= 0 then
        begin
          if (fileexists(ExtractFilePath(Application.ExeName) + '错误条码.wav') = true) then
            PlaySound('错误条码.wav', 0, SND_FILENAME or SND_ASYNC)
          else
            showMessage('错误的条码号!');
          exit;
        end;
        if trim(fieldByName('CIIOFlag').AsString) = '已出库' then
        begin
          if (fileexists(ExtractFilePath(Application.ExeName) + '重复出库扫描.wav') = true) then
            PlaySound('重复出库扫描.wav', 0, SND_FILENAME or SND_ASYNC)
          else
            showMessage('当前条码号已出库!');
          exit;
        end;
        //
        edit;
        fieldByName('IOTime').Value := SGetServerDate(ADOQueryTemp);
        fieldByName('CIIOFlag').Value := '已出库';
        post;
      end;
      with Order_Main do
      begin
        Edit;
        FieldByName('CIIOFlag').Value := '已出库';
        Post;
      end;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.Add('select A.*,cdqty=(select sum(CIFNumber) from Trade_Cloth_Inspect_Flaw x where x.CIID =A.CIID ) ');
        sql.Add(',CIFPoints=(select sum(CIFPoints * CIFNumber) from Trade_Cloth_Inspect_Flaw x where x.CIID =A.CIID ) ');
        sql.add('from V_Trade_Cloth_Inspect A  where  A.CIIOFlag=''已出库''and  A.OrderNo=' + quotedstr(Trim(OrderNo.text)));
        Open;
      end;
      SCreateCDS(ADOQueryMain, Order_Main);
      SInitCDSData(ADOQueryMain, Order_Main);

      if (fileexists(ExtractFilePath(Application.ExeName) + '正确.wav') = true) then
        PlaySound('正确.wav', 0, SND_FILENAME or SND_ASYNC);
    except
      if (fileexists(ExtractFilePath(Application.ExeName) + '扫描错误.wav') = true) then
        PlaySound('扫描错误.wav', 0, SND_FILENAME or SND_ASYNC);
    end;
    Edit_CKMJID.Text := '';
  end;
end;

procedure TfrmTradeClothInspList.CIIOFlagChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmTradeClothInspList.CIIOFlagPropertiesChange(Sender: TObject);
begin
  inherited;

  CurrentPage := 1;
  InitGrid();

end;

procedure TfrmTradeClothInspList.GradeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

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

procedure TfrmTradeClothInspList.Label1Click(Sender: TObject);
begin
  cxGroupBox1.Visible := false;

end;

procedure TfrmTradeClothInspList.MenuItem1Click(Sender: TObject);
var
  fPrintFile, FLabVolume, MStrIn: string;
  mCopys: Integer;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  MStrIn := '2';
  if not InputQuery('打印份数', '请输入', MStrIn) then
  begin
    Exit;
  end;
  mCopys := strToIntdef(trim(MStrIn), 2);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Trade_Cloth_Inspect Set PrtCount=PrtCount+1,PrtDate=getdate(),Prter=''' + Trim(DName) + '''');
          sql.Add(' where CIID=''' + Trim(Order_Main.fieldbyname('CIID').AsString) + '''');
          ExecSQL;
        end;

        with ADOQueryPrint do
        begin
          Close;
          SQL.Clear;
          sql.Add(' EXEC P_Trade_Print_djd_Lab ');
          SQL.Add(' @CIID=''' + Trim(Order_Main.fieldbyname('CIID').AsString) + '''');
          Open;
        end;
        if ADOQueryPrint.IsEmpty then
        begin
          application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
          exit;
        end;

        if Trim(ADOQueryPrint.fieldbyname('SLabVolume').AsString) <> '' then
          FLabVolume := Trim(ADOQueryPrint.fieldbyname('SLabVolume').AsString)
        else
          FLabVolume := Trim(ADOQueryPrint.fieldbyname('LabVolume').AsString);
         //默认标签
        if FLabVolume = '' then
          FLabVolume := trim(gDefaultLableFile);
        ExportFtErpFile(FLabVolume + '.rmf', ADOQueryTemp);
        fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + FLabVolume + '.rmf';

        if FileExists(fPrintFile) then
        begin
          RM1.LoadFromFile(fPrintFile);
         // if Trim(ADOQueryPrint.fieldbyname('LabVolume').AsString) <> '次品标签' then
         //   Rm1.DefaultCopies := 2
         // else
          Rm1.DefaultCopies := mCopys;
           // RM1.PrepareReport  ;

          RM1.ShowReport;
           // RM1.ExportTo(RMllPDFExport1,'C:\test.pdf');
        end
        else
        begin
          Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;

end;

procedure TfrmTradeClothInspList.N10Click(Sender: TObject);
var
  isOk: boolean;
  mtzfs: string;
  mtzsl, mvalue: double;
  decimalY: integer;
  mfield: string;
begin
  if Order_Main.IsEmpty then
    Exit;

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据记录', '提示', 0);
    Exit;
  end;
  isOk := false;
  frmBatchMdyData := TfrmBatchMdyData.create(self);
  with frmBatchMdyData do
  begin
    fTabCaption := '独立调整重量';
    if ShowModal = 1 then
    begin
      mtzfs := Trim(tzfs1.Text);
      if Trim(tzfield.Text) = '毛重' then
      begin
        mfield := 'grossWeight';
      end
      else if Trim(tzfield.Text) = '净重' then
      begin
        mfield := 'netWeight';
      end
      else if Trim(tzfield.Text) = '皮重' then
      begin
        mfield := 'tare';
      end
      else
      begin
        free;
        exit;
      end;
      mtzsl := strTofloatdef(Trim(tzsl1.Text), 0);
      isOk := true;
    end;
    free;
  end;
  if not isOk then
    exit;

  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          if mtzfs = '增加' then
          begin
            mvalue := Order_Main.fieldbyname(mfield).AsFloat + mtzsl;
          end;
          if mtzfs = '减少' then
          begin
            mvalue := Order_Main.fieldbyname(mfield).AsFloat - mtzsl;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Trade_Cloth_Inspect Set ' + mfield + '=' + floatTostr(mvalue));
            sql.Add('where cIId=''' + Trim(Order_Main.fieldbyname('cIId').AsString) + '''');
            ExecSQL;
          end;

        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据操作成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据操作失败！', '提示信息', 0);
  end;

end;

procedure TfrmTradeClothInspList.N1Click(Sender: TObject);
begin
  inherited;
  cxGroupBox1.Visible := not cxGroupBox1.Visible;
  if cxGroupBox1.Visible then
  begin
    cxGroupBox1.Left := (self.Width - cxGroupBox1.Width) div 2;
    edit_CKMJID.SetFocus;
  end;
end;

procedure TfrmTradeClothInspList.N2Click(Sender: TObject);
begin
 // SelOKNoFiler(Tv1, True); //全选
  try
    Order_Main.DisableControls;
    Order_Main.First;
    while not Order_Main.eof do
    begin
      Order_Main.Edit;
      Order_Main.FieldByName('ssel').Value := true;
      Order_Main.Post;

      Order_Main.Next;
    end;
  finally
    Order_Main.EnableControls;
    selDisp();
  end;
end;

procedure TfrmTradeClothInspList.N3Click(Sender: TObject);
begin
  //SelOKNoFiler(Tv1, False); //全弃
  try
    Order_Main.DisableControls;
    Order_Main.First;
    while not Order_Main.eof do
    begin
      Order_Main.Edit;
      Order_Main.FieldByName('ssel').Value := false;
      Order_Main.Post;

      Order_Main.Next;
    end;
  finally
    Order_Main.EnableControls;

    selDisp();
  end;
end;

procedure TfrmTradeClothInspList.N4Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTradeClothInspList.N5Click(Sender: TObject);
var
  MStrsql, MStrIn: string;
begin

  if Order_Main.IsEmpty then
    Exit;

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据记录', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要修改数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    Order_Main.DisableControls;
    with Order_Main do
    begin
      MStrsql := '';
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean then
        begin
          if MStrsql = '' then
            MStrsql := Quotedstr(trim(Order_Main.FieldByName('CIID').AsString))
          else
            MStrsql := MStrsql + ',' + Quotedstr(trim(Order_Main.FieldByName('CIID').AsString));
        end;
        Next;
      end;
    end;
    Order_Main.EnableControls;
    MovePanel2.Visible := False;
  except
    MovePanel2.Visible := False;
    Order_Main.EnableControls;
  end;

  if InputQuery('缸号', '请输入', MStrIn) then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update Trade_Cloth_Inspect Set BatchNO=' + QuotedStr(MStrIn));
      sql.Add(' where CIID in (' + MStrsql + ')');

      sql.Add('Update Trade_Cloth_Stock Set BatchNO=' + QuotedStr(MStrIn));
      sql.Add(' where CIID in (' + MStrsql + ')');
      ExecSQL;
    end;

  end;
  InitGrid();

end;

procedure TfrmTradeClothInspList.N6Click(Sender: TObject);
var
  MStrIn: string;
  mvalue: double;
begin
  if Order_Main.IsEmpty then
    Exit;

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据记录', '提示', 0);
    Exit;
  end;

  if not InputQuery('皮重(毛重会加上皮重)', '请输入', MStrIn) then
  begin
    Exit;
  end;
  mvalue := strTofloatdef(trim(MStrIn), 0);
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Trade_Cloth_Inspect Set tare=' + floatTostr(mvalue));
            sql.Add('where cIId=''' + Trim(Order_Main.fieldbyname('cIId').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Trade_Cloth_Inspect Set GrossWeight=netWeight+tare');
            sql.Add('where cIId=''' + Trim(Order_Main.fieldbyname('cIId').AsString) + '''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据操作成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据操作失败！', '提示信息', 0);
  end;

end;

procedure TfrmTradeClothInspList.N7Click(Sender: TObject);
var
  isOk: boolean;
  mlenUnit: string;
begin
  if Order_Main.IsEmpty then
    Exit;

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据记录', '提示', 0);
    Exit;
  end;
  isOk := false;
  frmBatchMdyData := TfrmBatchMdyData.create(self);
  with frmBatchMdyData do
  begin
    fTabCaption := '修改长度单位';
    if ShowModal = 1 then
    begin
      mlenUnit := Trim(lenunit.Text);
      isOk := true;
    end;
    free;
  end;
  if not isOk then
    exit;

  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Trade_Cloth_Inspect Set lenUnit=' + quotedstr(mlenUnit));
            sql.Add('where cIId=''' + Trim(Order_Main.fieldbyname('cIId').AsString) + '''');
            ExecSQL;
          end;

        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据操作成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据操作失败！', '提示信息', 0);
  end;

end;

procedure TfrmTradeClothInspList.N8Click(Sender: TObject);
var
  isOk: boolean;
  mtzfs: string;
  mtzsl, mvalue: double;
  decimalY: integer;
begin
  if Order_Main.IsEmpty then
    Exit;

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据记录', '提示', 0);
    Exit;
  end;
  isOk := false;
  frmBatchMdyData := TfrmBatchMdyData.create(self);
  with frmBatchMdyData do
  begin
    fTabCaption := '调整米数';
    if ShowModal = 1 then
    begin
      mtzfs := Trim(tzfs.Text);
      mtzsl := strTofloatdef(Trim(tzsl.Text), 0);
      isOk := true;
    end;
    free;
  end;
  if not isOk then
    exit;

//  with ADOQueryTemp do
//  begin
//    Close;
//    SQL.Clear;
//    sql.Add(' select * from Trade_Cloth_Inspect_Rule where  RuleID=' +quotedstr(Trim(Order_Main.fieldbyname('mainId').AsString)));
//    Open;
//    if not IsEmpty then
//    begin
//       if fieldByname('decimalY').AsInteger>0 then
//          decimalY:=fieldByname('decimalY').AsInteger
//       else
//         decimalY:=1;
//    end;
//
//  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          if mtzfs = '增加' then
          begin
            mvalue := Order_Main.fieldbyname('Meter').AsFloat + mtzsl;
          end;
          if mtzfs = '减少' then
          begin
            mvalue := Order_Main.fieldbyname('Meter').AsFloat - mtzsl;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Trade_Cloth_Inspect Set Meter=' + floatTostr(mvalue));
            sql.Add('where cIId=''' + Trim(Order_Main.fieldbyname('cIId').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Trade_Cloth_Inspect Set yardage=round(meter/0.9144,ISNULL((select top 1 decimalY from Trade_Cloth_Inspect_Rule X where RuleID=Trade_Cloth_Inspect.MainId),1))');
            sql.Add('where cIId=''' + Trim(Order_Main.fieldbyname('cIId').AsString) + '''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据操作成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据操作失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmTradeClothInspList.btnJYBGClick(Sender: TObject);
var
  WSql: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  WSql := '';
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + Trim(Order_Main.fieldbyname('CIID').AsString);
        end
        else
        begin
          WSql := Trim(Order_Main.fieldbyname('CIID').AsString);
        end;
      end;
      Next;
    end;
  end;
  Order_Main.Locate('SSel', True, []);
  Order_Main.EnableControls;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeInsp';
      FFiltration1 := WSql;
      FFiltration2 := WSql;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmTradeClothInspList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if pos('已出库', trim(AViewInfo.Text)) > 0 then
  begin
    ACanvas.Canvas.Brush.Color := RGB(237, 164, 146);
    //  ACanvas.Canvas.Font.Color := clred;
  end
  else if pos('次品', trim(AViewInfo.Text)) > 0 then
  begin
    ACanvas.Canvas.Brush.Color := RGB(0, 191, 96);
  end;
 // else if pos('√ ', trim(AViewInfo.Text)) > 0 then
 // begin
   // ACanvas.Canvas.Brush.Color := $000C000;
   // if (glideType.Text<>'') and (trim(GlideName.Text)=trim(AViewInfo.Text)) then
   // begin
    //  ACanvas.Canvas.Font.Color := clRed;
   // end;
 // end

end;

procedure TfrmTradeClothInspList.Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  FBounds := AViewInfo.Bounds;
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBlack, clBlack, [bBottom, bLeft, bRight], 1);
    FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);
    InflateRect(FBounds, -3, -2); //Platform specific. May not work on Linux.
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

/////////////////////////////////////////////////
function TfrmTradeClothInspList.autoOut(mCcids: string): boolean;
begin
  try
    result := false;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update Trade_Cloth_Inspect set CIIOFlag=''已出库'',IOTime=getdate()');
      sql.Add(' where EXISTS(select X.RTValue from  [dbo].[F_Tool_SplitString](''' + mCcids + ''','','')  X where X.RTValue=CIID) ');
      execsql;
    end;
    result := true;
  except
    application.MessageBox('执行自动出库时发生错误!', '警告信息', 0)
  end;
end;

////////////////////////////////////////////////
procedure TfrmTradeClothInspList.threadLoadform();
var
  thread: TThread;
begin

  thread := TThread.CreateAnonymousThread(
    procedure
    begin
      TThread.Synchronize(nil,
        procedure
        begin
          ReadCxGrid(Trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
          InitGrid();
        end)
    end);

  thread.FreeOnTerminate := True;
  thread.Start;

end;

/////////////////////////////////////////////////////
procedure TfrmTradeClothInspList.selDisp();
var
  msumRoll: integer;
  msumMeter, msumWeight, msumNetWeight, mSumYard: double;
  mrecNo: integer;
begin
  if not cxCheckBox1.Checked then
    exit;

  mrecNo := Order_Main.RecNo;
  try
    tv1.BeginUpdate();
    Order_Main.DisableControls;

    msumRoll := 0;
    msumMeter := 0;
    mSumYard := 0;
    msumWeight := 0;
    msumNetWeight := 0;
    with Order_Main do
    begin
      Order_Main.first;
      while not eof do
      begin
        if Order_Main.FieldByName('ssel').AsBoolean then
        begin
          msumRoll := msumRoll + 1;
          msumMeter := msumMeter + fieldByName('Meter').AsFloat;
          mSumYard := mSumYard + fieldByName('Yardage').AsFloat;
          msumWeight := msumWeight + fieldByName('GrossWeight').AsFloat;
          msumNetWeight := msumNetWeight + fieldByName('NetWeight').AsFloat;
        end;
        next;
      end;
      cxCheckBox1.Caption := '已选 ' + inttoStr(msumRoll) + ' 卷 ';
      if msumNetWeight > 0 then
      begin
        cxCheckBox1.Caption := cxCheckBox1.Caption + '净重:' + floattoStr(msumNetWeight) + 'kg ';
      end;
      if msumWeight > 0 then
      begin
        cxCheckBox1.Caption := cxCheckBox1.Caption + '毛重:' + floattoStr(msumWeight) + 'kg ';
      end;
      if msumMeter > 0 then
      begin
        cxCheckBox1.Caption := cxCheckBox1.Caption + '米数:' + floattoStr(msumMeter) + ' ';
        cxCheckBox1.Caption := cxCheckBox1.Caption + '码数:' + floattoStr(mSumYard) + ' ';
      end;
    end;
  finally
    Order_Main.RecNo := mrecNo;
    Order_Main.EnableControls;
    tv1.EndUpdate;
  end;

end;

end.

