unit U_CPDBAO;

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,
  RM_BarCode;

type
  TfrmCPDBao = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    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;
    V2BaoNO: TcxGridDBColumn;
    V2BaoID: TcxGridDBColumn;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    Panel4: TPanel;
    ADOQueryPrint: TADOQuery;
    Label6: TLabel;
    edtFHSQ: 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;
    v3Column10: TcxGridDBColumn;
    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;
    N8: TMenuItem;
    Label2: TLabel;
    CDS_DDY: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    v3Column4: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    btnOK: TcxButton;
    BaoLab: TEdit;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    RMDBPRT: TRMDBDataSet;
    RM3: TRMGridReport;
    cbbMD: TComboBox;
    cxButton1: TcxButton;
    N9: TMenuItem;
    N10: TMenuItem;
    N11: TMenuItem;
    Label4: TLabel;
    cxButton2: TcxButton;
    edtBaoNo: TEdit;
    Label5: TLabel;
    cxButton3: TcxButton;
    Edit1: TEdit;
    N13: TMenuItem;
    CDS_BaoID: TClientDataSet;
    N14: TMenuItem;
    N15: TMenuItem;
    V1Column1: TcxGridDBColumn;
    RMBarCodeObject1: TRMBarCodeObject;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure edtFHSQBtnClick(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 cxButton1Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure N9Click(Sender: TObject);
    procedure N10Click(Sender: TObject);
    procedure N11Click(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure edtBaoNoClick(Sender: TObject);
    procedure cxButton3Click(Sender: TObject);
    procedure N13Click(Sender: TObject);
    procedure N14Click(Sender: TObject);
    procedure N15Click(Sender: TObject);
  private
    FDDBSql, FBaoID, FBaoNo: string;
    NLBName: string;
    procedure InitGridYDB();
    procedure InitGridKDB();
    procedure GetBaoNo();
    procedure DaBao();
    procedure OKDDB();
    procedure UPDB();
    procedure MoveCDS(CDS_Left: TclientDataSet; CDS_Right: TclientDataSet);
    procedure PrintBao(PBaoId: string);
        { Private declarations }
  public
    FOrderNo: string;
    { Public declarations }
  end;

var
  frmCPDBao: TfrmCPDBao;
  newh: hwnd;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ProductOrderListSel;
{$R *.dfm}

procedure TfrmCPDBao.UPDB();
begin

//  with ADOQueryCmd do       CDS_DDB
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('exec P_DB_UPDB  ' + quotedstr(trim(edtFHSQ.Text)));
//    ExecSQL;
//  end;

end;

procedure TfrmCPDBao.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('BaoID').Value := FBaoID;
        FieldByName('BaoNo').Value := FBaoNo;
      end;
      CDS_KDB.Delete;
    end;
    CDS_KDB.EnableControls;
  end;
end;

procedure TfrmCPDBao.DaBao();
var
  DBMsg: string;
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 WFB_MJJY Set BaoTime=GETDATE() , BaoNo=' + QuotedStr(trim(FBaoNo)) + ',BaoID=' + QuotedStr(trim(FBaoID)) + ' where MJID=' + QuotedStr(trim(CDS_DDB.FieldByName('MJID').AsString));
      FDDBSql := FDDBSql + ' ' + 'Update CK_BanCP_CR Set BaoTime=GETDATE() , BaoNo=' + QuotedStr(trim(FBaoNo)) + ',BaoID=' + QuotedStr(trim(FBaoID)) + '  where MJID=' + QuotedStr(trim(CDS_DDB.FieldByName('MJID').AsString));
      Next;
    end;
    CDS_DDB.EnableControls;
  end;
  if DBMsg = '打包异常' then
  begin
    application.MessageBox('打包失败！', '提示信息', 0);
  end
  else
  begin
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(FDDBSql);
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox('打包失败！', '提示信息', 0);
    end;
  end;
end;

procedure TfrmCPDBao.GetBaoNo();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.add('select max(cast(isnull(baoNo,''0'') as int)  )+1  as MaxBaoNo from CK_BanCP_CR A inner join JYOrder_Main B on A.MainId=B.MainId');
    sql.Add('where CRFlag in (''出库'',''待出库'') and B.OrderNo=' + quotedstr(trim(edtFHSQ.Text)));
//    ShowMessage(SQL.Text);
    open;
  end;
  FBaoNo := Trim(ADOQueryTmp.fieldbyname('MaxBaoNo').AsString);
  if FBaoNo = '' then
    FBaoNo := '1';

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

end;

procedure TfrmCPDBao.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 TfrmCPDBao.PrintBao(PBaoId: 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_Do_PrintDaBao ' + quotedstr(PBaoId));
    Open;
  end;
  if Trim(BaoLab.Text) <> '' then
  begin
    NLBName := Trim(BaoLab.Text);
  end
  else
  begin
    NLBName := '包标签';
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := PBaoId;
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\' + trim(PBaoId) + '.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;

  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
    RMVariables['QRBARCODE'] := fImagePath;
    RM2.LoadFromFile(fPrintFile);
    RM2.DefaultCopies := strtointdef(trim(ComboBox1.Text), 1);
    RM2.printReport;
  end;
  if FileExists(fImagePath) then
    DeleteFile(fImagePath);

end;

procedure TfrmCPDBao.InitGridYDB();
begin
  if Trim(edtFHSQ.Text) = '' then
    Exit;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY A  ');
    sql.Add('inner join JYOrder_Main B on A.MainId=B.MainId    ');
    sql.Add('inner join JYOrder_Sub C on A.SubId=C.SubId ');
    sql.Add('where A.MJStr2=''未入库'' and B.orderno=' + QUOTEDSTR(Trim(edtFHSQ.Text)));
    sql.Add('and isnull(A.BaoId,'''')<>'''' ');
    open;
  end;
  SCreateCDS20(ADOQueryTmp, CDS_YDB);
  SInitCDSData20(ADOQueryTmp, CDS_YDB);
end;

procedure TfrmCPDBao.InitGridKDB();
begin
  if Trim(edtFHSQ.Text) = '' then
    Exit;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY A  ');
    sql.Add('inner join JYOrder_Main B on A.MainId=B.MainId    ');
    sql.Add('inner join JYOrder_Sub C on A.SubId=C.SubId ');
    sql.Add('where  A.MJStr2=''未入库'' and B.orderno=' + QUOTEDSTR(Trim(edtFHSQ.Text)));
    sql.Add('and isnull(A.BaoId,'''')='''' ');
    open;
  end;
  SCreateCDS20(ADOQueryTmp, CDS_KDB);
  SInitCDSData20(ADOQueryTmp, CDS_KDB);
end;

procedure TfrmCPDBao.FormDestroy(Sender: TObject);
begin
  frmCPDBao := nil;
end;

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

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

end;

procedure TfrmCPDBao.edtFHSQBtnClick(Sender: TObject);
begin
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin

        edtFHSQ.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
        BaoLab.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('NLBName').AsString);

      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;

  SMNO.SetFocus;
  InitGridKDB();
  InitGridYDB();
  GetBaoNo();
end;

procedure TfrmCPDBao.btnCloseClick(Sender: TObject);
begin

  close;
end;

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

procedure TfrmCPDBao.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_KDB, True);
end;

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

procedure TfrmCPDBao.FormShow(Sender: TObject);
begin
//////////////////////初始化待打包界面/////////////////////
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select  * from   WFB_MJJY  AA   ');
    sql.Add('inner join JYOrder_Main BB on AA.MainId=BB.MainId ');
    sql.Add('inner join JYOrder_Sub CC on AA.SubId=CC.SubId where 1=2');
    open;
  end;
  SCreateCDS20(ADOQueryTmp, CDS_DDB);
//////////////////////初始化待打包界面/////////////////////


end;

procedure TfrmCPDBao.N6Click(Sender: TObject);
begin

  if CDS_DDB.IsEmpty then
    Exit;
  with CDS_DDB do
  begin
//    CDS_DDB.DisableControls;
//    while CDS_DDB.Locate('SSel', True, []) do
//    begin
    MoveCDS(CDS_DDB, CDS_KDB);
    CDS_DDB.Delete;
//    end;
//    CDS_DDB.EnableControls;
  end;

end;

procedure TfrmCPDBao.SmNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if trim(smno.Text) = '' then
      exit;

    if trim(edtFHSQ.Text) = '' then
    begin
      application.MessageBox('计划的不能为空', '提示');
      exit;
    end;

    if CDS_KDB.Locate('MJID', Trim(SmNO.Text), []) then
    begin
      with CDS_KDB do
      begin
        Edit;
        FieldByName('SSel').Value := True;
        Post;
      end;
    end;

    OKDDB();

    Sleep(200);
    if CDS_DDB.RecordCount = StrToInt(ComboBox3.Text) then
    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(FBaoID);
      end;
      GetBaoNo();
      UPDB();
    end;

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

end;

procedure TfrmCPDBao.N3Click(Sender: TObject);
var
  MBaoid: 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
    MBaoid := Trim(CDS_YDB.FieldByName('baoid').AsString);
    PrintBao(MBaoid);
    while CDS_YDB.Locate('SSel;Baoid', VarArrayOf([True, MBaoid]), []) do
    begin
      with CDS_YDB do
      begin
        Edit;
        CDS_YDB.FieldByName('SSel').Value := False;
        Post;
      end;
    end;
  end;

end;

procedure TfrmCPDBao.N4Click(Sender: TObject);
var
  NBaoID: string;
begin
  NBaoID := trim(CDS_YDB.FieldByName('BaoID').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;

    sql.Add('Update WFB_MJJY Set BaoNo=null,BaoID=null  where isnull(BaoID,'''')<>''''  and BaoID=' + QuotedStr(NBaoID));

    sql.Add('Update CK_BanCP_CR Set BaoNo=null,BaoID=null  where    BaoID=' + QuotedStr(NBaoID));

    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(NBaoID));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    ExecSQL;
  end;
  CDS_YDB.DisableControls;
  CDS_KDB.DisableControls;
  while CDS_YDB.Locate('BaoID', NBaoID, []) do
  begin
    MoveCDS(CDS_YDB, CDS_KDB);
    CDS_YDB.Delete
  end;
  CDS_YDB.EnableControls;
  CDS_KDB.EnableControls;

  UPDB();
  GetBaoNo();
end;

procedure TfrmCPDBao.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(FBaoID);
  end;
  UPDB();
  GetBaoNo();
end;

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

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

  MCurMJStr4 := '';
  MoldMJStr4 := '';
  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
          MoldMJStr4 := Trim(CDS_KDB.FieldByName('MJStr4').AsString);
          MoldSubID := Trim(CDS_KDB.FieldByName('SubID').AsString);
        end;
        MCurMJStr4 := Trim(CDS_KDB.FieldByName('MJStr4').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('BaoID').Value := FBaoID;
            FieldByName('BaoNo').Value := FBaoNo;
            Post
          end;
          DaBao();
          CDS_DDB.EmptyDataSet;
          if chkBBQ.Checked then
          begin
            PrintBao(FBaoID);
          end;
          GetBaoNo();
          Sleep(200);
          i := 0;
        end
        else
        begin
          if (MoldMJStr4 <> MCurMJStr4) or (MoldSubID <> MCurSubID) then
          begin
            DaBao();
            CDS_DDB.EmptyDataSet;
            Sleep(100);
            if chkBBQ.Checked then
            begin
              PrintBao(FBaoID);
            end;

            GetBaoNo();
            MoveCDS(CDS_KDB, CDS_DDB);
            with CDS_DDB do
            begin
              Edit;
              FieldByName('BaoID').Value := FBaoID;
              FieldByName('BaoNo').Value := FBaoNo;
              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('BaoID').Value := FBaoID;
                FieldByName('BaoNo').Value := FBaoNo;
                Post
              end;
            end;

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

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

        K := K + 1;
      end;

      MoldMJStr4 := MCurMJStr4;
      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(FBaoID);
      end;

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

procedure TfrmCPDBao.cxButton1Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
begin
  if Trim(edtFHSQ.Text) = '' then
    Exit;

  if trim(cbbMD.Text) = '仓库打包码单' then
  begin
    with ADOQueryTmp do
    begin

      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD1 ');
      sql.add('@CKOrdNo=' + quotedstr(Trim(edtFHSQ.Text)));
      Open;
    end;
    SCreateCDS20(ADOQueryTmp, CDS_PRT);
    SInitCDSData20(ADOQueryTmp, CDS_PRT);
  end;
  if trim(cbbMD.Text) = '单证打包码单' then
  begin
    with ADOQueryTmp do
    begin

      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD2 ');
      sql.add('@CKOrdNo=' + quotedstr(Trim(edtFHSQ.Text)));
      Open;
    end;
    SCreateCDS20(ADOQueryTmp, CDS_PRT);
    SInitCDSData20(ADOQueryTmp, CDS_PRT);

    with ADOQueryTmp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD3 ');
      sql.add('@CKOrdNo=' + quotedstr(Trim(edtFHSQ.Text)));
      Open;
    end;
    SCreateCDS20(ADOQueryTmp, CDS_HZ);
    SInitCDSData20(ADOQueryTmp, CDS_HZ);
  end;
  ExportFtErpFile(trim(cbbMD.Text) + '.rmf', ADOQueryTmp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(cbbMD.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM3.LoadFromFile(fPrintFile);

    RMVariables['ZDR'] := trim(DName);
    RM3.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCPDBao.N8Click(Sender: TObject);
var
  MRKORDID: string;
begin
  if CDS_KDB.IsEmpty then
    Exit;
  MRKORDID := Trim(CDS_KDB.FieldByName('RKORDID').AsString);
  CDS_KDB.DisableControls;
  with CDS_KDB do
  begin
    First;
    while not Eof do
    begin
      Edit;
      if Trim(FieldByName('RKORDID').AsString) = MRKORDID then
        FieldByName('SSel').Value := true
      else
        FieldByName('SSel').Value := false;
      Post;

      Next;
    end;
  end;
  CDS_KDB.EnableControls;
end;

procedure TfrmCPDBao.N9Click(Sender: TObject);
var
  MMJStr4, MSubid: string;
begin
  if CDS_KDB.IsEmpty then
    Exit;
  MMJStr4 := Trim(CDS_KDB.FieldByName('MJStr4').AsString);
  MSubid := Trim(CDS_KDB.FieldByName('Subid').AsString);
  CDS_KDB.DisableControls;
  with CDS_KDB do
  begin
    First;
    while not Eof do
    begin
      Edit;
      if (Trim(FieldByName('MJStr4').AsString) = MMJStr4) and (Trim(FieldByName('Subid').AsString) = MSubid) then
        FieldByName('SSel').Value := true
      else
        FieldByName('SSel').Value := false;
      Post;

      Next;
    end;
  end;
  CDS_KDB.EnableControls;
end;

procedure TfrmCPDBao.N10Click(Sender: TObject);
var
  MConNo: string;
begin
  if CDS_KDB.IsEmpty then
    Exit;
  MConNo := Trim(CDS_KDB.FieldByName('ConNo').AsString);
  CDS_KDB.DisableControls;
  with CDS_KDB do
  begin
    First;
    while not Eof do
    begin
      Edit;
      if Trim(FieldByName('ConNo').AsString) = MConNo then
        FieldByName('SSel').Value := true
      else
        FieldByName('SSel').Value := false;
      Post;

      Next;
    end;
  end;
  CDS_KDB.EnableControls;

end;

procedure TfrmCPDBao.N11Click(Sender: TObject);
var
  MPRTColor: string;
begin
  if CDS_KDB.IsEmpty then
    Exit;
  MPRTColor := Trim(CDS_KDB.FieldByName('PRTColor').AsString);
  CDS_KDB.DisableControls;
  with CDS_KDB do
  begin
    First;
    while not Eof do
    begin
      Edit;
      if Trim(FieldByName('PRTColor').AsString) = MPRTColor then
        FieldByName('SSel').Value := true
      else
        FieldByName('SSel').Value := false;
      Post;

      Next;
    end;
  end;
  CDS_KDB.EnableControls;
end;

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

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

procedure TfrmCPDBao.cxButton3Click(Sender: TObject);
begin
  edtBaoNo.Text := Edit1.Text;
  FBaoNo := Edit1.Text;
end;

procedure TfrmCPDBao.N13Click(Sender: TObject);
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select DISTINCT BAOID from CK_BanCP_CR X where  CRFlag in (''出库'',''待出库'') and CKOrdNo=' + QUOTEDSTR(Trim(edtFHSQ.Text)));
    ShowMessage(SQL.Text);
    open;
  end;
  SCreateCDS20(ADOQueryTmp, CDS_BaoID);
  SInitCDSData20(ADOQueryTmp, CDS_BaoID);
  with CDS_BaoID do
  begin
    First;
    while not Eof do
    begin
      PrintBao(Trim(CDS_BaoID.FieldByName('BAOID').AsString));
      Sleep(200);
      Next;
    end;
  end;
end;

procedure TfrmCPDBao.N14Click(Sender: TObject);
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select DISTINCT BAOID from CK_BanCP_CR X ');
    sql.Add('where  CRFlag in (''出库'',''待出库'') and CKOrdNo=' + QUOTEDSTR(Trim(edtFHSQ.Text)));
    sql.Add('  and mainid=' + QUOTEDSTR(Trim(CDS_YDB.FieldByName('BAOID').AsString)));
//    ShowMessage(SQL.Text);
    open;
  end;
  SCreateCDS20(ADOQueryTmp, CDS_BaoID);
  SInitCDSData20(ADOQueryTmp, CDS_BaoID);
  with CDS_BaoID do
  begin
    First;
    while not Eof do
    begin
      PrintBao(Trim(CDS_BaoID.FieldByName('BAOID').AsString));
      Sleep(200);
      Next;
    end;
  end;
end;

procedure TfrmCPDBao.N15Click(Sender: TObject);
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select DISTINCT BAOID from CK_BanCP_CR X ');
    sql.Add('where  CRFlag in (''出库'',''待出库'') and CKOrdNo=' + QUOTEDSTR(Trim(edtFHSQ.Text)));
    sql.Add('  and mainid=' + QUOTEDSTR(Trim(CDS_YDB.FieldByName('mainid').AsString)));
    sql.Add('  and ganghao=' + QUOTEDSTR(Trim(CDS_YDB.FieldByName('MJstr4').AsString)));
//    ShowMessage(SQL.Text);
    open;
  end;
  SCreateCDS20(ADOQueryTmp, CDS_BaoID);
  SInitCDSData20(ADOQueryTmp, CDS_BaoID);
  with CDS_BaoID do
  begin
    First;
    while not Eof do
    begin
      PrintBao(Trim(CDS_BaoID.FieldByName('BAOID').AsString));
      Sleep(200);
      Next;
    end;
  end;
end;

end.

