unit U_TradePack;

interface

uses
  Windows, Messages, strutils, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, StdCtrls, cxContainer, cxTextEdit, cxCurrencyEdit, BtnEdit, ExtCtrls,
  ComCtrls, ToolWin, DBClient, ADODB, MovePanel, Buttons, Menus,
  cxLookAndFeelPainters, cxButtons, RM_Common, RM_Class, RM_GridReport,
  RM_System, RM_Dataset, cxCheckBox, MMSystem, Math, WinSpool, Printers,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels, cxNavigator,
    dxDateRanges, dxBarBuiltInMenu,
  U_BaseList;

type
  TfrmTradePack = class(TfrmBaseList)
    Panel1: TPanel;
    Label1: TLabel;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    CDS_DDB: TClientDataSet;
    DS_DDB: TDataSource;
    SmNO: TEdit;
    ADOQueryTmp: TADOQuery;
    V2Column1: TcxGridDBColumn;
    DataSource1: TDataSource;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    Tv3Column1: TcxGridDBColumn;
    DS_KDB: TDataSource;
    V2BaooNO: TcxGridDBColumn;
    V2BaooID: TcxGridDBColumn;
    RMDB_1: TRMDBDataSet;
    RM2: TRMGridReport;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    Panel4: TPanel;
    ADOQueryPrint: TADOQuery;
    Label6: TLabel;
    edtOrder: TBtnEditA;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    CDS_KDB: TClientDataSet;
    Panel5: TPanel;
    CDS_YDB: TClientDataSet;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    DS_YDB: TDataSource;
    V2Column4: TcxGridDBColumn;
    V4Column2: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    PM_KDB: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    PM_YDB: TPopupMenu;
    V2Column6: TcxGridDBColumn;
    V4Column3: TcxGridDBColumn;
    btnClose: TcxButton;
    N5: TMenuItem;
    ComboBox1: TComboBox;
    chkBBQ: TCheckBox;
    ComboBox3: TComboBox;
    PM_DDB: TPopupMenu;
    N6: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N7: TMenuItem;
    Label2: TLabel;
    CDS_DDY: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    v3Column4: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    btnOK: TcxButton;
    Label4: TLabel;
    cxButton2: TcxButton;
    edtPacketNo: TEdit;
    Label5: TLabel;
    cxButton3: TcxButton;
    Edit1: TEdit;
    N13: TMenuItem;
    CDS_PacketId: TClientDataSet;
    V1Column1: TcxGridDBColumn;
    N8: TMenuItem;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    chkhs: TCheckBox;
    btnhs: TcxButton;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    Tv3Column4: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    cxButton1: TcxButton;
    Label3: TLabel;
    Tv3Column5: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure edtOrderBtnClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure PRTColorKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure SmNOKeyPress(Sender: TObject; var Key: Char);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure edtPacketNoClick(Sender: TObject);
    procedure cxButton3Click(Sender: TObject);
    procedure N13Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure chkhsClick(Sender: TObject);
    procedure btnhsClick(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
  private
    FDDBSql, FPacketId, FPacketNo: string;
    NLBName: string;
    procedure InitGridKDB(); //可打包数据
    procedure InitGridDDB(); //待打包数据
    procedure InitGridYDB(); //已打包数据
    procedure GetPacketNo();
    procedure DaBao();
    procedure HSDB();
    procedure OKDDB();
    procedure MoveCDS(CDS_Left: TclientDataSet; CDS_Right: TclientDataSet);
    procedure PrintBao(PPacketId: string);
    procedure PlayWav(MWav: string);
        { Private declarations }
  public
    { Public declarations }
  end;

var
  frmTradePack: TfrmTradePack;
  newh: hwnd;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_TradeClothWaitPack;
{$R *.dfm}

procedure TfrmTradePack.OKDDB();
begin
  if CDS_KDB.IsEmpty then
    Exit;
  with CDS_KDB do
  begin
    CDS_KDB.DisableControls;
    while CDS_KDB.Locate('SSel', True, []) do
    begin
      MoveCDS(CDS_KDB, CDS_DDB);
      with CDS_DDB do
      begin
        Edit;
        FieldByName('PacketId').Value := FPacketId;
        FieldByName('PacketNo').Value := FPacketNo;
      end;
      CDS_KDB.Delete;
      PlayWav('正确');
    end;

    CDS_KDB.EnableControls;
  end;
end;

procedure TfrmTradePack.PlayWav(MWav: string);
begin
  if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\' + MWav + '.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\' + MWav + '.wav'), 0, SND_ASYNC);
end;

procedure TfrmTradePack.HSDB();
var
  MPacketId: string;
begin
  if CDS_YDB.IsEmpty then
    Exit;
  FDDBSql := '';
  with CDS_YDB do
  begin
    CDS_YDB.DisableControls;
    First;
    while not Eof do
    begin
      FDDBSql := FDDBSql + ' ' + 'Update Trade_Cloth_Inspect Set PacketTime=GETDATE() , PacketNo=' + QuotedStr(trim(CDS_YDB.FieldByName('PacketNo').AsString)) + ',PacketId=' + QuotedStr(trim(CDS_YDB.FieldByName('PacketId').AsString)) + ',PacketCode=' + QuotedStr(trim(DCode)) + ',PacketName=' + QuotedStr(trim(DName)) + 'where CIID=' + QuotedStr(trim(CDS_YDB.FieldByName('CIID').AsString));
      FDDBSql := FDDBSql + ' ' + 'Update Trade_Cloth_Stock Set PacketTime=GETDATE() , PacketNo=' + QuotedStr(trim(CDS_YDB.FieldByName('PacketNo').AsString)) + ',PacketId=' + QuotedStr(trim(CDS_YDB.FieldByName('PacketId').AsString)) + ',PacketCode=' + QuotedStr(trim(DCode)) + ',PacketName=' + QuotedStr(trim(DName)) + 'where CIID=' + QuotedStr(trim(CDS_YDB.FieldByName('CIID').AsString));
      Next;
    end;
    CDS_YDB.EnableControls;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(FDDBSql);
//      ShowMessage(SQL.Text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('打包失败！', '提示信息', 0);
  end;
  if chkBBQ.Checked then
  begin
    SelOKNo(CDS_YDB, True);
    if CDS_YDB.Locate('SSel', True, []) = False then
    begin
      application.MessageBox('请选择打印数据！', '提示信息', MB_ICONERROR);
      exit;
    end;
    while CDS_YDB.Locate('SSel', True, []) do
    begin
      MPacketId := Trim(CDS_YDB.FieldByName('PacketId').AsString);
      PrintBao(MPacketId);
      while CDS_YDB.Locate('SSel;PacketId', VarArrayOf([True, MPacketId]), []) do
      begin
        with CDS_YDB do
        begin
          Edit;
          CDS_YDB.FieldByName('SSel').Value := False;
          Post;
        end;
      end;
    end;

  end;
end;

procedure TfrmTradePack.DaBao();
begin
  if CDS_DDB.IsEmpty then
    Exit;
  FDDBSql := '';
  with CDS_DDB do
  begin
    CDS_DDB.DisableControls;
    First;
    while not Eof do
    begin
      FDDBSql := FDDBSql + ' ' + 'Update Trade_Cloth_Inspect Set PacketTime=GETDATE() , PacketNo=' + QuotedStr(trim(FPacketNo)) + ',PacketId=' + QuotedStr(trim(FPacketId)) + ',PacketCode=' + QuotedStr(trim(DCode)) + ',PacketName=' + QuotedStr(trim(DName)) + 'where CIID=' + QuotedStr(trim(CDS_DDB.FieldByName('CIID').AsString));
      FDDBSql := FDDBSql + ' ' + 'Update Trade_Cloth_Stock Set PacketTime=GETDATE() , PacketNo=' + QuotedStr(trim(FPacketNo)) + ',PacketId=' + QuotedStr(trim(FPacketId)) + ',PacketCode=' + QuotedStr(trim(DCode)) + ',PacketName=' + QuotedStr(trim(DName)) + 'where CIID=' + QuotedStr(trim(CDS_DDB.FieldByName('CIID').AsString));
      Next;
    end;
    CDS_DDB.EnableControls;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(FDDBSql);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
//    PlayWav('成功');
  except
    ADOQueryCmd.Connection.RollbackTrans;
//    PlayWav('扫描错误');
    application.MessageBox('打包失败！', '提示信息', 0);
  end;

end;

procedure TfrmTradePack.GetPacketNo();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.add('select max(cast(isnull(PacketNo,''0'') as int)  )+1  as MaxPacketNo from Trade_Cloth_Stock ');
    sql.Add('where  OrderNo=' + quotedstr(trim(edtOrder.Text)));
//    ShowMessage(SQL.Text);
    open;
  end;
  FPacketNo := Trim(ADOQueryTmp.fieldbyname('MaxPacketNo').AsString);
  if FPacketNo = '' then
    FPacketNo := '1';

  edtPacketNo.Text := FPacketNo;
  if GetLSNo(ADOQueryTmp, FPacketId, 'BI', 'Trade_Cloth_Inspect', 4, 1) = False then
  begin
    Application.MessageBox('取包条码最大号失败！', '提示', 0);
    Exit;
  end;

end;

procedure TfrmTradePack.MoveCDS(CDS_Left: TclientDataSet; CDS_Right: TclientDataSet);
var
  i: Integer;
begin
  with CDS_Right do
  begin
    Append;
    for i := 0 to CDS_Left.FieldCount - 1 do
    begin
      CDS_Right.FieldByName(CDS_Left.Fields[i].FieldName).Value := CDS_Left.fieldbyname(CDS_Left.Fields[i].FieldName).Value;
    end;
    Post;
  end;
end;

procedure TfrmTradePack.PrintBao(PPacketId: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin

  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec P_Trade_Insp_Prt_PackLab ' + quotedstr(PPacketId));
//    showmessage(SQL.text);
    Open;
  end;

  NLBName := ADOQueryPrint.FieldByName('LabPackage').AsString;

  ExportFtErpFile(Trim(NLBName) + '.rmf', ADOQueryTmp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(NLBName) + '.rmf';
  if not FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    Exit;
  end;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.DefaultCopies := strtointdef(trim(ComboBox1.Text), 1);
    RM2.printReport;
  end;
  if FileExists(fImagePath) then
    DeleteFile(fImagePath);

end;

procedure TfrmTradePack.InitGridYDB();
begin
  if Trim(edtOrder.Text) = '' then
    Exit;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;

    sql.Add('select * from Trade_Cloth_Inspect A where isnull(PacketId,'''')<>'''' and OrderNo=' + QUOTEDSTR(Trim(edtOrder.Text)));

    open;
  end;
  SCreateCDS(ADOQueryTmp, CDS_YDB);
  SInitCDSData(ADOQueryTmp, CDS_YDB);
end;

procedure TfrmTradePack.InitGridKDB();
begin
  if Trim(edtOrder.Text) = '' then
    Exit;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Trade_Cloth_Inspect A where isnull(PacketId,'''')='''' and OrderNo=' + QUOTEDSTR(Trim(edtOrder.Text)));
//    ShowMessage(sql.Text);
    open;
  end;
  SCreateCDS(ADOQueryTmp, CDS_KDB);
  SInitCDSData(ADOQueryTmp, CDS_KDB);
end;

procedure TfrmTradePack.InitGridDDB();
begin
  //////////////////////初始化待打包界面/////////////////////
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select AA.* from Trade_Cloth_Inspect AA where 1=2');
    open;
  end;
  SCreateCDS(ADOQueryTmp, CDS_DDB);
//////////////////////初始化待打包界面/////////////////////
end;

procedure TfrmTradePack.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradePack := nil;
end;

procedure TfrmTradePack.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  writeCxGrid('成品扫描打包Tv1', Tv1, '成品仓库');
  writeCxGrid('成品扫描打包Tv2', Tv2, '成品仓库');
  writeCxGrid('成品扫描打包Tv3', Tv3, '成品仓库');
  Action := cafree;
end;

procedure TfrmTradePack.FormCreate(Sender: TObject);
begin
  inherited;
  readCxGrid('成品扫描打包Tv1', Tv1, '成品仓库');
  readCxGrid('成品扫描打包Tv2', Tv2, '成品仓库');
  readCxGrid('成品扫描打包Tv3', Tv3, '成品仓库');

end;

procedure TfrmTradePack.edtOrderBtnClick(Sender: TObject);
var
  FPacketId: string;
  FMainID: string;
begin
  try
    frmTradeClothWaitPack := TfrmTradeClothWaitPack.Create(self);
    with frmTradeClothWaitPack do
    begin
      if ShowModal = 1 then
      begin
        edtOrder.Text := Trim(CDS_OrderSel.fieldbyname('OrderNo').AsString);
      end;
    end;
  finally
    frmTradeClothWaitPack.Free;
  end;
  SMNO.SetFocus;
  InitGridKDB();
  InitGridYDB();
  GetPacketNo();
end;

procedure TfrmTradePack.btnCloseClick(Sender: TObject);
begin

  close;
end;

procedure TfrmTradePack.PRTColorKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel5, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_KDB);
    SInitCDSData(ADOQueryMain, CDS_KDB);
  end;
end;

procedure TfrmTradePack.N1Click(Sender: TObject);
begin
//  SelOKNo(CDS_KDB, True);
  SelOKNoFiler(Tv3, True);
end;

procedure TfrmTradePack.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_KDB, False);
end;

procedure TfrmTradePack.FormShow(Sender: TObject);
begin
  InitGridDDB();

end;

procedure TfrmTradePack.N6Click(Sender: TObject);
begin

  if CDS_DDB.IsEmpty then
    Exit;
  with CDS_DDB do
  begin

    MoveCDS(CDS_DDB, CDS_KDB);
    CDS_DDB.Delete;
  end;

end;

procedure TfrmTradePack.SmNOKeyPress(Sender: TObject; var Key: Char);
var
  MPS, MM, MY, MN, MG: Double;
begin
  if Key = #13 then
  begin
    if trim(smno.Text) = '' then
      exit;

    if trim(edtOrder.Text) = '' then
    begin
      application.MessageBox('计划单号不能为空', '提示');
      exit;
    end;
//    if Trim(CDS_KDB.FieldByName('ciid').AsString) = Trim(SmNO.Text) then
//    begin
//      with CDS_KDB do
//      begin
//        Edit;
//        FieldByName('SSel').Value := True;
//        Post;
//      end;
//    end;
    if CDS_KDB.IsEmpty then
      Exit;
    with CDS_KDB do
    begin
      CDS_KDB.DisableControls;
      First;
      while not Eof do
      begin
//          MoveCDS(CDS_DDB, CDS_YDB);
        if Trim(CDS_KDB.FieldByName('ciid').AsString) = Trim(SmNO.Text) then
        begin
          with CDS_KDB do
          begin
            Edit;
            FieldByName('SSel').Value := True;
            Post;
          end;
        end;
        Next;
      end;
      CDS_KDB.EnableControls;
//        CDS_DDB.EmptyDataSet;
    end;

    with ADOQuery2 do
    begin

      close;
      sql.Clear;
      sql.Add('select * from Trade_Cloth_Inspect A where  OrderNo=' + QUOTEDSTR(Trim(edtOrder.Text)));
      SQL.Add('And isnull(PacketId,'''')<>'''' and  CIID=' + QUOTEDSTR(Trim(SMNo.Text)));
//      ShowMessage(sql.Text);
      open;

    end;
    if CDS_DDB.IsEmpty <> True then
    begin
      if CDS_DDB.Locate('CIID', Trim(SMNo.Text), []) then
      begin
        PlayWav('重复扫描');
      end;
    end
    else
    begin
      if ADOQuery2.IsEmpty <> True then
      begin
        PlayWav('重复扫描');
      end;
    end;

    OKDDB();

    with ADOQuery1 do
    begin

      close;
      sql.Clear;
      sql.Add('select * from Trade_Cloth_Inspect A where OrderNo=' + QUOTEDSTR(Trim(edtOrder.Text)));
      SQL.Add('And  CIID=' + QUOTEDSTR(Trim(SMNo.Text)));
//      ShowMessage(sql.Text);
      open;

    end;
    if ADOQuery1.IsEmpty then
    begin
      PlayWav('扫描错误');
    end;

    Sleep(200);

    if StrToInt(ComboBox3.Text) = CDS_DDB.RecordCount then
    begin
      MPS := tv2.DataController.Summary.FooterSummaryValues[7];
      MM := tv2.DataController.Summary.FooterSummaryValues[1];
      MY := tv2.DataController.Summary.FooterSummaryValues[8];
      MG := tv2.DataController.Summary.FooterSummaryValues[3];
      MN := tv2.DataController.Summary.FooterSummaryValues[4];
      Label3.Caption := '匹数：' + FloattoStr(MPS) + '    总米数：' + FloattoStr(MM) + '    总码数：' + FloattoStr(MY) + '    总毛重：' + FloattoStr(MG) + '    总净重：' + FloattoStr(MN);
      DaBao();
      with CDS_DDB do
      begin
        CDS_DDB.DisableControls;
        First;
        while not Eof do
        begin
          MoveCDS(CDS_DDB, CDS_YDB);
          Next;
        end;
        CDS_DDB.EnableControls;
        CDS_DDB.EmptyDataSet;
      end;
      if chkBBQ.Checked then
      begin
        PrintBao(FPacketId);
      end;
      GetPacketNo();
    end;

    smno.Text := '';
    smno.SetFocus;
  end;

end;

procedure TfrmTradePack.N3Click(Sender: TObject);
var
  MPacketId: string;
begin
  if CDS_YDB.Locate('SSel', True, []) = False then
  begin
    application.MessageBox('请选择打印数据！', '提示信息', MB_ICONERROR);
    exit;
  end;
  while CDS_YDB.Locate('SSel', True, []) do
  begin
    MPacketId := Trim(CDS_YDB.FieldByName('PacketId').AsString);
    PrintBao(MPacketId);
    while CDS_YDB.Locate('SSel;PacketId', VarArrayOf([True, MPacketId]), []) do
    begin
      with CDS_YDB do
      begin
        Edit;
        CDS_YDB.FieldByName('SSel').Value := False;
        Post;
      end;
    end;
  end;

end;

procedure TfrmTradePack.N4Click(Sender: TObject);
var
  NPacketId: string;
begin

  if CDS_YDB.Locate('SSel', True, []) = False then
  begin
    application.MessageBox('请选择打印数据！', '提示信息', MB_ICONERROR);
    exit;
  end;
  while CDS_YDB.Locate('SSel', True, []) do
  begin
    NPacketId := Trim(CDS_YDB.FieldByName('PacketId').AsString);
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;

      sql.Add('Update Trade_Cloth_Inspect Set PacketNo=null,PacketId=null  where isnull(PacketId,'''')<>'''' and PacketId=' + QuotedStr(NPacketId));

      sql.Add('Update Trade_Cloth_Stock Set PacketNo=null,PacketId=null  where isnull(PacketId,'''')<>'''' and  PacketId=' + QuotedStr(NPacketId));

      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      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(NPacketId));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    while CDS_YDB.Locate('SSel;PacketId', VarArrayOf([True, NPacketId]), []) do
    begin
      with CDS_YDB do
      begin
        Edit;
        CDS_YDB.FieldByName('SSel').Value := False;
        Post;
      end;
    end;
  end;
  InitGridKDB();
  InitGridYDB();
  InitGridDDB();
  GetPacketNo();
end;

procedure TfrmTradePack.N7Click(Sender: TObject);
begin
  DaBao();
  with CDS_DDB do
  begin
    CDS_DDB.DisableControls;
    First;
    while not Eof do
    begin
      MoveCDS(CDS_DDB, CDS_YDB);
      Next;
    end;
    CDS_DDB.EnableControls;
    CDS_DDB.EmptyDataSet;
  end;
  if chkBBQ.Checked then
  begin
    PrintBao(FPacketId);
  end;

  GetPacketNo();
end;

procedure TfrmTradePack.N8Click(Sender: TObject);
begin
  SelOKNo(CDS_YDB, false);

end;

procedure TfrmTradePack.N5Click(Sender: TObject);
begin
  OKDDB();
end;

procedure TfrmTradePack.btnhsClick(Sender: TObject);
var
  rtvalues: TArray<string>;
  FOrderNO, msg: string;
  FJS, FJH: Integer;
begin
  if not CDS_DDB.IsEmpty then
  begin
    Application.MessageBox('请清空待打包数据', '提示', 0);
    Exit;
  end;
  if CDS_KDB.IsEmpty then
    Exit;

  rtvalues := SelCDSKey(CDS_KDB, ['CIID']);
  FOrderNO := edtOrder.text;
  FJS := StrToInt(ComboBox3.Text);
  FJH := StrToInt(edtPacketNo.text);

  with ADOQuery1 do
  begin
    Close;
    Sql.Clear;
    Sql.Add('exec P_GetDaBao ');
    Sql.Add(' @CIIDs=' + quotedstr(rtvalues[0]));
    Sql.Add(',@OrderNO=' + quotedstr(Trim(FOrderNO)));
    Sql.Add(',@JS=' + inttostr(FJS));
    SQL.Add(',@JH=' + inttostr(FJH));
//      showmessage(SQL.text);
    Open;
  end;
  SCreateCDS(ADOQuery1, CDS_YDB);
  SInitCDSData(ADOQuery1, CDS_YDB);

  with ADOQuery2 do
  begin
    Close;
    Sql.Clear;
    Sql.Add('exec P_GetDaBaoTS ');
    Sql.Add(' @CIIDs=' + quotedstr(rtvalues[0]));
    Sql.Add(',@OrderNO=' + quotedstr(Trim(FOrderNO)));
    Sql.Add(',@JS=' + inttostr(FJS));
//      showmessage(SQL.text);
    Open;
  end;

  msg := ADOQuery2.FieldByName('msg').AsString;

  if Application.MessageBox(PChar(msg), '确认打包', 32 + 4) = IDYES then
  begin
    HSDB();
  end;
  GetPacketNo();
  InitGridKDB();
  InitGridYDB();

end;

procedure TfrmTradePack.btnOKClick(Sender: TObject);
var
  i, j, K: Integer;
  MCurBatchNO, MoldBatchNO, MCurSubID, MoldSubID: string;
begin
  if not CDS_DDB.IsEmpty then
  begin
    Application.MessageBox('请清空待打包数据', '提示', 0);
    Exit;
  end;
  if CDS_KDB.IsEmpty then
    Exit;

  MCurBatchNO := '';
  MoldBatchNO := '';
  i := 0;
  j := StrToInt(ComboBox3.Text);
  K := 0;
  with CDS_KDB do
  begin
    CDS_KDB.DisableControls;
    CDS_DDB.DisableControls;
    First;
    while not Eof do
    begin
      if CDS_KDB.FieldByName('SSel').AsBoolean then
      begin
        if K = 0 then
        begin
          MoldBatchNO := Trim(CDS_KDB.FieldByName('BatchNO').AsString);
          MoldSubID := Trim(CDS_KDB.FieldByName('SubID').AsString);
        end;
        MCurBatchNO := Trim(CDS_KDB.FieldByName('BatchNO').AsString);
        MCurSubID := Trim(CDS_KDB.FieldByName('SubID').AsString);
        if j = 1 then
        begin
          MoveCDS(CDS_KDB, CDS_DDB);
          with CDS_DDB do
          begin
            Edit;
            FieldByName('PacketId').Value := FPacketId;
            FieldByName('PacketNo').Value := FPacketNo;
            Post
          end;
          DaBao();
          CDS_DDB.EmptyDataSet;
          if chkBBQ.Checked then
          begin
            PrintBao(FPacketId);
          end;
          GetPacketNo();
          Sleep(200);
          i := 0;
        end
        else
        begin
          if (MoldBatchNO <> MCurBatchNO) or (MoldSubID <> MCurSubID) then
          begin
            DaBao();
            CDS_DDB.EmptyDataSet;
            Sleep(100);
            if chkBBQ.Checked then
            begin
              PrintBao(FPacketId);
            end;

            GetPacketNo();
            MoveCDS(CDS_KDB, CDS_DDB);
            with CDS_DDB do
            begin
              Edit;
              FieldByName('PacketId').Value := FPacketId;
              FieldByName('PacketNo').Value := FPacketNo;
              Post
            end;
            i := 1;
          end
          else
          begin
            i := i + 1;

            if i <= j then
            begin
              MoveCDS(CDS_KDB, CDS_DDB);
              with CDS_DDB do
              begin
                Edit;
                FieldByName('PacketId').Value := FPacketId;
                FieldByName('PacketNo').Value := FPacketNo;
                Post
              end;
            end;

            if i = j then
            begin
              DaBao();
              CDS_DDB.EmptyDataSet;
              if chkBBQ.Checked then
              begin
                PrintBao(FPacketId);
              end;

              GetPacketNo();
              Sleep(100);
              i := 0;
            end;
          end;
        end;

        K := K + 1;
      end;

      MoldBatchNO := MCurBatchNO;
      MoldSubID := MCurSubID;
      with CDS_KDB do
      begin
        Edit;
        FieldByName('SSel').Value := false;
        Post
      end;
      Next;
    end;
    if not CDS_DDB.IsEmpty then
    begin
      DaBao();
      CDS_DDB.EmptyDataSet;
      if chkBBQ.Checked then
      begin
        PrintBao(FPacketId);
      end;

      GetPacketNo();
      Sleep(100);
      i := 0;
    end;
    CDS_DDB.EnableControls;
    CDS_KDB.EnableControls;
  end;
  InitGridKDB();
  InitGridYDB();

end;

procedure TfrmTradePack.chkhsClick(Sender: TObject);
begin
  if chkhs.Checked then
  begin
    label2.Caption := '每色卷数';
    btnHS.Visible := True;
    cxButton1.Visible := False;
  end
  else if not chkhs.Checked then
  begin
    label2.Caption := '每包卷数';
    cxButton1.Visible := True;
    btnHS.Visible := False;
  end;

end;

procedure TfrmTradePack.cxButton1Click(Sender: TObject);
var
  rtvalues: TArray<string>;
  FOrderNO, msg: string;
  FJS, FJH: Integer;
begin
  if not CDS_DDB.IsEmpty then
  begin
    Application.MessageBox('请清空待打包数据', '提示', 0);
    Exit;
  end;
  if CDS_KDB.IsEmpty then
    Exit;

  rtvalues := SelCDSKey(CDS_KDB, ['CIID']);
  FOrderNO := edtOrder.text;
  FJS := StrToInt(ComboBox3.Text);
  FJH := StrToInt(edtPacketNo.text);

  with ADOQuery1 do
  begin
    Close;
    Sql.Clear;
    Sql.Add('exec P_GetDaBao_ZD ');
    Sql.Add(' @CIIDs=' + quotedstr(rtvalues[0]));
    Sql.Add(',@OrderNO=' + quotedstr(Trim(FOrderNO)));
    Sql.Add(',@JS=' + inttostr(FJS));
    SQL.Add(',@JH=' + inttostr(FJH));
//      showmessage(SQL.text);
    Open;
  end;
  SCreateCDS(ADOQuery1, CDS_YDB);
  SInitCDSData(ADOQuery1, CDS_YDB);

//  with ADOQuery2 do
//  begin
//    Close;
//    Sql.Clear;
//    Sql.Add('exec P_GetDaBaoTS ');
//    Sql.Add(' @CIIDs=' + quotedstr(rtvalues[0]));
//    Sql.Add(',@OrderNO=' + quotedstr(Trim(FOrderNO)));
//    Sql.Add(',@JS=' + inttostr(FJS));
////      showmessage(SQL.text);
//    Open;
//  end;
//  msg := ADOQuery2.FieldByName('msg').AsString;

  if Application.MessageBox('确定要打包吗？', '确认打包', 32 + 4) = IDYES then
  begin
    HSDB();
  end;
  GetPacketNo();
  InitGridKDB();
  InitGridYDB();
//  with CDS_YDB do
//  begin
//
//  end;
end;

procedure TfrmTradePack.cxButton2Click(Sender: TObject);
begin
  InitGridKDB();
  InitGridYDB();
  CDS_DDB.EmptyDataSet;
end;

procedure TfrmTradePack.edtPacketNoClick(Sender: TObject);
begin
  if not CDS_DDB.IsEmpty then
  begin
    Application.MessageBox('请清空待打包数据', '提示', 0);
    Exit;
  end;
end;

procedure TfrmTradePack.cxButton3Click(Sender: TObject);
begin
  edtPacketNo.Text := Edit1.Text;
  FPacketNo := Edit1.Text;
end;

procedure TfrmTradePack.N13Click(Sender: TObject);
begin
  SelOKNo(CDS_YDB, True);
end;

end.

