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;

type
  TfrmCPDBao = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    CDSMJID: TClientDataSet;
    DataSource2: TDataSource;
    SmNO: TEdit;
    ADOQueryTmp: TADOQuery;
    V2Column1: TcxGridDBColumn;
    kcKw: TBtnEditA;
    Label5: TLabel;
    MovePanel1: TMovePanel;
    Button4: TButton;
    Label2: TLabel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    Tv3Column1: TcxGridDBColumn;
    ADOQueryTm: TADOQuery;
    DSTm: TDataSource;
    baoNo: TEdit;
    BaoID: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    V2BaoNO: TcxGridDBColumn;
    V2BaoID: TcxGridDBColumn;
    Panel3: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    SpeedButton49: TSpeedButton;
    FileName: TcxButton;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    ComboBox1: TComboBox;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    Panel4: TPanel;
    ADOQueryPrint: TADOQuery;
    Label6: TLabel;
    BtnEditA1: TBtnEditA;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    CDSTM: TClientDataSet;
    Panel5: TPanel;
    cxButton3: TcxButton;
    CDSSEL: TClientDataSet;
    cxGrid1: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    DSSEL: TDataSource;
    cxButton4: TcxButton;
    V2Column5: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    V4Column1: TcxGridDBColumn;
    V4Column2: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    check_bz: TCheckBox;
    cxButton5: TcxButton;
    v3Column5: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    ComboBox3: TComboBox;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    cxButton7: TcxButton;
    v3Column8: TcxGridDBColumn;
    Label7: TLabel;
    PRTColor: TEdit;
    SOrddefstr1: TEdit;
    Label8: TLabel;
    PRTHX: TEdit;
    Label9: TLabel;
    mjstr4: TEdit;
    Label10: TLabel;
    prtkuanNo: TEdit;
    Label11: TLabel;
    ComboBox4: TComboBox;
    v3Column9: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    Label12: TLabel;
    MJlen: TEdit;
    ComboBox5: TEdit;
    Label13: TLabel;
    ZBS: TEdit;
    Label17: TLabel;
    v3Column10: TcxGridDBColumn;
    V2Column6: TcxGridDBColumn;
    V4Column3: TcxGridDBColumn;
    cxButton1: TcxButton;
    cxButton8: TcxButton;
    ADOTemp: TADOQuery;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    N3: TMenuItem;
    N4: TMenuItem;
    cxButton9: TcxButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);

    procedure FormCreate(Sender: TObject);
    procedure SmNOKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure kcKwBtnClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure baoNoKeyPress(Sender: TObject; var Key: Char);
    procedure baoNoClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure SpeedButton49Click(Sender: TObject);
    procedure FileNameClick(Sender: TObject);
    procedure baoNoExit(Sender: TObject);
    procedure BtnEditA1BtnClick(Sender: TObject);
    procedure cxButton3Click(Sender: TObject);
    procedure cxButton4Click(Sender: TObject);
    procedure cxButton5Click(Sender: TObject);
    procedure cxButton7Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure cxButton8Click(Sender: TObject);
    procedure MovePanel1DblClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure cxButton9Click(Sender: TObject);
  private
    Rolls:integer;
    MyBlock:string;
    procedure InitGrid();
    procedure InitMJID();
    Procedure QueryRk();
    Procedure QueryTM();
    procedure QueryCDSSEL();
    procedure ShowMovePanel(msg:string;dbps:Integer);
    procedure PrintBao(PBaoId:string);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCPDBao: TfrmCPDBao;
  newh:hwnd;

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

Procedure TfrmCPDBao.PrintBao(PBaoId:string);
var fPrintFile: string;
    Txt,fImagePath:string;
    Moudle: THandle;
    Makebar:TMakebar;
    Mixtext:TMixtext;
    NLBName:string;
begin
  NLBName:='包标签';
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' SELECT D.LBName,D.NLBName');
    sql.Add(' from WFB_MJJY A');
    sql.Add(' inner join JYOrder_Main D on D.MainID=A.MainID');
    SQL.Add('where BaoId='''+PBaoId+'''');
    Open;
    IF not ADOQueryPrint.IsEmpty then
    begin
      if Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)<>'' then
      begin
        NLBName:=Trim(ADOQueryPrint.fieldbyname('NLBName').AsString);
      end;
    end;
  end;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec [P_Do_PrintDaBao]'+quotedstr(PBaoId));
    Open;
  end;
  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=PBaoID;
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
    CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    exit;
  end;

  IF check_bz.Checked then
  begin
    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;
  end;
end;

Procedure TfrmCPDBao.ShowMovePanel(msg:string;dbps:Integer);
begin
  with ADOQueryTmp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec [P_Do_GetDaBaoTJ1] '+quotedstr(Trim(DCode)));
    SQL.Add(','+quotedstr('99'));
    Open;
  end;
  Label18.Caption:=msg;
  Label2.Caption:='包数：'+Trim(IntToStr(ADOQueryTmp.FieldByName('bs').AsInteger));
  Label19.Caption:='匹数：'+Trim(IntToStr(ADOQueryTmp.FieldByName('ps').AsInteger));
  Label21.Caption:='预计卷数：'+Trim(IntToStr(dbps));
  if dbps=ADOQueryTmp.FieldByName('ps').AsInteger then
  begin
    Label2.Font.Color:=clBlack;
    Label19.Font.Color:=clBlack;
  end
  else
  begin
    Label2.Font.Color:=clRed;
    Label19.Font.Color:=clRed;
//    MyBlock:='禁止';
  end;
  MovePanel1.Visible:=True;
end;

Procedure TfrmCPDBao.QueryTm();
var
  FBaoID:string;
begin
  with ADOQueryTM do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,B.*,C.MPrtCodeName,C.orderNo as OrderNoM');
    sql.Add('from WFB_MJJY A   ');
    sql.Add('inner join JYOrder_sub B on B.subID=A.subID ');
    sql.Add('inner join JYOrder_main C on C.mainID=A.MainID ');
    sql.Add(' where  A.mjstr2=''未入库'' and isnull(A.BaoID,'''')='''' and C.orderNo='+quotedstr(trim(BtnEditA1.Text)));
    SQL.Add(' and B.subid in (select wsql1 from TBwsql where dname='+quotedstr(Trim(DName))+' and maintype=''打包'')');
    open;
  end;
  SCreateCDS20(ADOQueryTM,CDSTM);
  SInitCDSData20(ADOQueryTM,CDSTM);
  cxButton7.Click;
  SCreateCDS20(ADOQueryTM,CDSMJID);
  IF not CDSTM.IsEmpty then
  begin
    with ADOQuery1 do
    begin
      close;
      sql.clear;
      sql.add('select isnull(max(cast(baoNo as int)),0)+1 as MaxBaoNo from WFB_MJJY ');
      sql.Add('where mainID='+quotedstr(trim(CDSTM.fieldbyname('MainID').AsString)));
      open;
    end;
    baoNo.text:=ADOQuery1.fieldbyname('MaxBaoNo').AsString;
    if GetLSNo(ADOQueryTmp,FBaoID,'BI','WFB_MJJY',4,1)=False then
    begin
      Application.MessageBox('取包条码最大号失败！','提示',0);
      Exit;
    end;
    BaoID.Text:=trim(FBaoid);
  end;
  SMNO.SetFocus;
end;

procedure TfrmCPDBao.QueryCDSSEL();
begin
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,B.*,C.MPrtCodeName,C.orderNo as OrderNoM');
    sql.Add('from WFB_MJJY A   ');
    sql.Add('inner join JYOrder_sub B on B.subID=A.subID ');
    sql.Add('inner join JYOrder_main C on C.mainID=A.MainID ');
    sql.Add(' where  A.mjstr2=''未入库'' and C.orderNo='+quotedstr(trim(BtnEditA1.Text)));
    SQL.Add(' and B.subid in (select wsql1 from TBwsql where dname='+quotedstr(Trim(DName))+' and maintype=''打包'')');
    sql.Add(' and SUBSTRING(isnull(A.BaoID,''''),3,6)=convert(varchar(100),getdate(),12) ');
    open;
//    ShowMessage(SQL.Text);
  end;
  SCreateCDS20(ADOTemp,CDSSEL);
  SInitCDSData20(ADOTemp,CDSSEL);
end;

Procedure TfrmCPDBao.QueryRk();
begin
  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,hC_ps=A.AordQty2,JY_ps=(select count(MainID) from WFB_MJJY X where X.APID=A.APID),');
    sql.Add('JY_Qty=(select sum(MJLen) from WFB_MJJY X where X.APID=A.APID), ');
    sql.Add('RK_RollNum=(select sum(SOrdQty1) from CK_BanCP_CR X where X.APID=A.APID and X.CRType=''检验入库''),');
    sql.Add('RK_Qty=(select sum(MJLen) from CK_BanCP_CR X where X.APID=A.APID and X.CRType=''检验入库'') ');
    sql.Add('from JYOrder_sub_anPai A   ');
    SQL.Add('WHERE exists(select MJID from WFB_MJJY X where X.APID=A.APID and X.MJID='+quotedstr(trim(smNO.Text)));
    sql.Add(')');
    open;
  end;
end;


procedure TfrmCPDBao.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.*,C.MprtCodeName,C.orderNo as OrderNoM from WFB_MJJY A ');
    sql.add('inner join JYOrder_Sub B on B.SubId=A.SubId');
    sql.add('inner join JYOrder_Main C on C.maiNID=A.mainID');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDSSEL);
end;

procedure TfrmCPDBao.InitMJID();
begin
  MovePanel1.Visible:=false;


  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.*,C.MprtCodeName,C.orderNo as OrderNoM from WFB_MJJY A ');
    sql.add('inner join JYOrder_Sub B on B.SubId=A.SubId');
    sql.add('inner join JYOrder_Main C on C.maiNID=A.mainID');
    sql.Add('where MJID='+quotedstr(trim(SmNO.Text)));
    Open;
    if not IsEmpty  then
    begin
      IF trim(Fieldbyname('MJstr2').AsString)='已入库' then
      begin
        CDSMJID.Append;
        CDSMJID.FieldByName('SDefNote').Value:='此条码已入库';
        CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
        CDSMJID.Post;
      end
      else
      begin
            CDSMJID.Append;
            CDSMJID.FieldByName('SDefNote').Value:='打包成功';
            CDSMJID.FieldByName('MJID').Value:=trim(ADOQueryMain.Fieldbyname('MJID').AsString);
//            CDSMJID.FieldByName('CRID').Value:=trim(ADOQueryMain.Fieldbyname('CRID').AsString);
            CDSMJID.FieldByName('PRTCodeName').Value:=trim(ADOQueryMain.Fieldbyname('PRTCodeName').AsString);
            CDSMJID.FieldByName('MPRTCodeName').Value:=trim(ADOQueryMain.Fieldbyname('MPRTCodeName').AsString);
            CDSMJID.FieldByName('PRTColor').Value:=trim(ADOQueryMain.Fieldbyname('PRTColor').AsString);
            CDSMJID.FieldByName('PRTHX').Value:=trim(ADOQueryMain.Fieldbyname('PRTHX').AsString);
            CDSMJID.FieldByName('MJStr4').Value:=trim(ADOQueryMain.Fieldbyname('MJStr4').AsString);
            CDSMJID.FieldByName('MJType').Value:=trim(ADOQueryMain.Fieldbyname('MJType').AsString);
            CDSMJID.FieldByName('BaoNo').Value:=trim(BaoNo.Text);
            CDSMJID.FieldByName('BaoID').Value:=trim(BaoID.Text);
            CDSMJID.FieldByName('SOrdQty1').Value:=1;
            CDSMJID.FieldByName('MJLen').Value:=ADOQueryMain.Fieldbyname('MJLen').AsFloat;
            CDSMJID.FieldByName('MJMaoZ').Value:=ADOQueryMain.Fieldbyname('MJMaoZ').AsFloat;
            CDSMJID.FieldByName('MJQty3').Value:=ADOQueryMain.Fieldbyname('MJQty3').AsFloat;
            CDSMJID.FieldByName('MJQty4').Value:=ADOQueryMain.Fieldbyname('MJQty4').AsFloat;
            CDSMJID.FieldByName('MJTypeOther').Value:=trim(ADOQueryMain.Fieldbyname('MJTypeOther').AsString);
            CDSMJID.FieldByName('mainID').Value:=trim(ADOQueryMain.Fieldbyname('mainID').AsString);
            CDSMJID.FieldByName('subID').Value:=trim(ADOQueryMain.Fieldbyname('subID').AsString);
            CDSMJID.Post;
            Rolls:=Rolls+1;
      end;
    end
    else
    begin
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value:='此条码不存在';
      CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
      CDSMJID.Post;
    end;
  end;
  IF trim(CDSMJID.FieldByName('SDefNote').AsString)<>'打包成功' then
  begin
//    MovePanel1.Visible:=true;
//    Edit2.Text:=SmNO.Text;
//    Label2.Caption:=trim(CDSMJID.FieldByName('SDefNote').AsString);
    IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\打包错误.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\打包错误.wav'),0, SND_ASYNC);
    CDSMJID.delete;
  end
  else
  begin
    IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav'),0, SND_ASYNC);
  end;  
  If CDSTM.Locate('MJID',trim(smno.Text),[]) then
  begin
   // application.MessageBox('AAAA','AAAAAAAA',0);
    CDSTM.Delete;
    tv2.Controller.EditingController.ShowEdit();
    tv3.Controller.EditingController.ShowEdit();
  end;
  IF CheckBox1.Checked then
  begin
    IF (Rolls=strtointdef(trim(ComboBox3.Text),0)) and (strtointdef(trim(ComboBox3.Text),0)>0) then
    begin
      FileName.Click;
    end;
  end;
end;

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

procedure TfrmCPDBao.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  //writeCxGrid(self.Caption+tv1.Name,Tv1,'成品仓库');
  writeCxGrid(self.Caption+Tv2.Name,Tv2,'成品仓库');
  writeCxGrid(self.Caption+Tv3.Name+'1',Tv3,'成品仓库');
  writeCxGrid(self.Caption+Tv4.Name,Tv4,'成品仓库');
  Action:=cafree;
end;

procedure TfrmCPDBao.FormCreate(Sender: TObject);
begin
  readCxGrid(self.Caption+Tv2.Name,Tv2,'成品仓库');
  readCxGrid(self.Caption+Tv3.Name+'1',Tv3,'成品仓库');
  readCxGrid(self.Caption+Tv4.Name,Tv4,'成品仓库');
  Rolls:=0;
end;

procedure TfrmCPDBao.SmNOKeyPress(Sender: TObject; var Key: Char);
begin
  IF key=#13 then
  begin
    if trim(smno.Text)='' then exit;
    if trim(smno.Text)='123456789' then
    begin
       FileName.Click;
    end
    else
    begin
      if trim(BaoNo.Text)='' then
      begin
        application.MessageBox('包号不能为空','提示');
        exit;
      end;
      if trim(BaoNo.Text)='' then
      begin
        application.MessageBox('包号不能为空','提示');
        exit;
      end;
      if trim(BaoID.Text)='' then
      begin
        application.MessageBox('包条码不能为空','提示');
        exit;
      end;
      IF CDSTM.IsEmpty then exit;
      IF CDSMJID.Locate('MJID',trim(smno.Text),[]) then
      begin
        IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\重复扫描.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\重复扫描.wav'),0, SND_ASYNC);
        smno.Text:='';
        smno.SetFocus;
        exit;
      end;
      If not CDSTM.Locate('MJID',trim(smno.Text),[]) then
      begin
        with ADOQuery1 do
        begin
          Close;
          sql.Clear;
          SQL.Add('select A.*,B.*,C.MprtCodeName,C.OrderNO as orderNoM from WFB_MJJY A ');
          sql.add('inner join JYOrder_Sub B on B.SubId=A.SubId');
          sql.add('inner join JYOrder_Main C on C.maiNID=A.mainID');
          sql.Add('where MJID='+quotedstr(trim(SmNO.Text)));
          Open;
          IF isempty then
          begin
            IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav')) then
            PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav'),0, SND_ASYNC);
          end
          else
          begin
            If not CDSTM.Locate('orderNoM',trim(fieldbyname('OrderNoM').AsString),[]) then
            begin
              IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav')) then
              PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav'),0, SND_ASYNC);
            end
            else
            begin
              IF Fieldbyname('MJstr2').AsString<>'未入库' then
              begin
                IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav')) then
                PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav'),0, SND_ASYNC);
              end
              else
              begin
                IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav')) then
               PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav'),0, SND_ASYNC);
              end;
            end;
          end;
        end;
        smno.Text:='';
        smno.SetFocus;
        exit;
      end
      else
      begin
        initMJID();
      end;
    end;
    smno.Text:='';
    smno.SetFocus;
  end;
end;

procedure TfrmCPDBao.FormShow(Sender: TObject);
begin
//  MovePanel1.Left:=(Width-MovePanel1.Width) div 2;
//  MovePanel1.top:=(Height-MovePanel1.Height-200) div 2;
  Tv3.OptionsView.Footer:=True;
end;

procedure TfrmCPDBao.TV2CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
var
  i:integer;
begin
  i:=tv2.GetColumnByFieldName('SDefNote').Index;
  if (AViewInfo.GridRecord.Values[i]<>'打包成功')  then
  ACanvas.Brush.Color:=clred;
end;

procedure TfrmCPDBao.kcKwBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CPKWFlag';
      flagname:='库位信息';
      V1Name.Caption:='库位名称';
      V1Note.Caption:='英文名称';
//      MainType:=Trim(DName);
      fnote:=FALSE;
      if ShowModal=1 then
      begin
        kcKw.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPDBao.Button4Click(Sender: TObject);
begin
  if MyBlock<>'禁止' then
  begin
    MovePanel1.Visible:=False;
    SmNO.SetFocus;
  end;
end;

procedure TfrmCPDBao.baoNoKeyPress(Sender: TObject; var Key: Char);
var FBaoID: string;
begin

end;

procedure TfrmCPDBao.baoNoClick(Sender: TObject);
var
  i:Integer;
begin
//  Panel3.Visible:=True;
 { with Panel3 do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint:=Trim(TEdit(Sender).Name);
      end;
    end;
  end;   }
end;

procedure TfrmCPDBao.SpeedButton1Click(Sender: TObject);
var
  fsj:string;
begin
  fsj:=Trim(TSpeedButton(Sender).Hint);
  if Trim(fsj)='' then Exit;
  fsj:=Trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text:=fsj+Trim(TSpeedButton(Sender).Caption);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelectAll;
end;

procedure TfrmCPDBao.SpeedButton12Click(Sender: TObject);
var
  fsj:string;
begin
  fsj:=Trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);
  if Trim(fsj)='' then Exit;
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text:=Copy(fsj,1,Length(fsj)-1);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelectAll;
end;
procedure TfrmCPDBao.SpeedButton49Click(Sender: TObject);
var
  FBaoID:string;
begin
  IF trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Hint)<>'' then
  begin
    if GetLSNo(ADOQueryTmp,FBaoID,'BI','WFB_MJJY',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取包条码最大号失败！','提示',0);
      Exit;
    end;
    BaoID.Text:=trim(FBaoid);
  end;
  Panel3.Visible:=False;
end;

procedure TfrmCPDBao.FileNameClick(Sender: TObject);
var fPrintFile: string;
    Txt,fImagePath:string;
    Moudle: THandle;
    Makebar:TMakebar;
    Mixtext:TMixtext;
    FBaoID,NLBName:string;
    i:Integer;
begin
  if trim(BaoID.Text)='' then
  begin
    application.MessageBox('包条码不能为空','提示');
    exit;
  end;
  IF CDSMJID.IsEmpty then exit;
    NLBName:='包标签';
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' SELECT D.LBName,D.NLBName');
    sql.Add(' from WFB_MJJY A');
    sql.Add(' inner join JYOrder_Main D on D.MainID=A.MainID');
    SQL.Add('where MJID='''+Trim(CDSMJID.fieldbyname('MJID').AsString)+'''');
    Open;
    IF not ADOQueryPrint.IsEmpty then
    begin
      if Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)<>'' then
      begin
        NLBName:=Trim(ADOQueryPrint.fieldbyname('NLBName').AsString);
      end;
    end;
  end;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with  CDSMJID do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update WFB_MJJY Set BaoNo='''+trim(BaoNo.Text)+''',BaoID='''+trim(Baoid.text)+''' where MJID='''+Trim(CDSMJID.fieldbyname('MJID').AsString)+'''');
          sql.Add('UPDATE CK_BanCP_CR SET BaoNo='''+trim(BaoNo.Text)+''',baoID='''+trim(Baoid.text)+''' where MJID='''+Trim(CDSMJID.fieldbyname('MJID').AsString)+'''');
          ExecSQL;
        end;
        edit;
        fieldByName('Sflag').AsString :='2';
        fieldbyname('BaoNo').Value:=trim(BaoNo.Text);
        fieldbyname('BaoID').Value:=trim(BaoID.Text);
        post;
        next;
      end;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    if Trim(ComboBox4.Text)='手动打包' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Update_Order_JD_ByOther @MainType=''打包''');
        sql.Add(',@DName='+quotedstr(Trim(DName)));
        ExecSQL;
      end;
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav'),0, SND_ASYNC);
    end;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\打包错误.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\打包错误.wav'),0, SND_ASYNC);
    application.MessageBox('数据修改失败！','提示信息',0);
    exit;
  end;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec [P_Do_PrintDaBao] '+quotedstr(Trim(BaoID.Text)));
    Open;
  end;
  if ADOQueryPrint.RecordCount>1 then
  begin
    Application.MessageBox('打印包标签失败，请重新打包!','提示',0);
    IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\打包错误.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\打包错误.wav'),0, SND_ASYNC);
    Exit;
  end;
  if ADOQueryPrint.RecordCount<1 then
  begin
    Application.MessageBox('此包号还未被保存，请保存数据，在打印条码！','提示',0);
    IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\打包错误.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\打包错误.wav'),0, SND_ASYNC);
    Exit;
  end;

  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=trim(BaoID.Text);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
    CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    exit;
  end;

  baoNo.Text:=inttostr(strtointdef(trim(baoNo.Text),0)+1);
  if GetLSNo(ADOQueryTmp,FBaoID,'BI','WFB_MJJY',4,1)=False then
  begin
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('取包条码最大号失败！','提示',0);
    Exit;
  end;
  BaoID.Text:=trim(FBaoid);
  CDSsel.Last;
  with CDSMJID do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      CDSsel.Append;
      for i:=0 to FieldCount-1 do
      begin
         CDSsel.fields[i].value:=Fields[i].Value ;
      end;
      CDSsel.Post;
      next;
    end;
    EnableControls;
  end;
  CDSMJID.EmptyDataSet;

  IF check_bz.Checked then
  begin
    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;
  end;
  Rolls:=0;
  SmNO.SetFocus;
end;

procedure TfrmCPDBao.baoNoExit(Sender: TObject);
var
  FBaoID:string;
begin
 IF baoNo.Text<>'' then
 begin
    if GetLSNo(ADOQueryTmp,FBaoID,'BI','WFB_MJJY',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取包条码最大号失败！','提示',0);
      Exit;
    end;
    BaoID.Text:=trim(FBaoid);
 end
 else
   BaoID.Text:='';
end;

procedure TfrmCPDBao.BtnEditA1BtnClick(Sender: TObject);
var
  FBaoID:string;
  FMainID:string;
begin
    frmOrderSelDB:=TfrmOrderSelDB.Create(Application);
    with frmOrderSelDB do
    begin
      if ShowModal=1 then
      begin
        CDS_OrderSel.DisableControls;
        with CDS_OrderSel do
        begin
          with ADOTemp do
          begin
            close;
            sql.Clear;
            SQL.Add('delete TBwsql where DName='+quotedstr(Trim(DName))+' and MainType='+quotedstr('打包'));
            ExecSQL;
          end;
          First;
          while not Eof do
          begin
            if FieldByName('SSel').Value=True then
            begin
              BtnEditA1.Text:=trim(CDS_OrderSel.fieldbyname('OrderNo').asstring);
              with ADOTemp do
              begin
                close;
                sql.Clear;
                sql.Add('insert into TBwsql(DName,MainType,wsql1) values('+quotedstr(Trim(DName))+','+quotedstr('打包')+','+quotedstr(CDS_OrderSel.fieldbyname('subid').asstring)+')');
                ExecSQL;
              end;
            end;
            Next;
          end;
        end;
        CDS_OrderSel.EnableControls;
      end;
      frmOrderSelDB.Free;
    end;
  SMNO.SetFocus;
  QueryTm();
  InitGrid();
end;

procedure TfrmCPDBao.cxButton3Click(Sender: TObject);
var
  i:integer;
begin
  IF CDSMJID.IsEmpty then exit;
  IF CDSMJID.fieldByName('Sflag').AsString='2' then
  begin
    application.MessageBox('已打包，不能删除！','提示信息',0);
    exit;
  end
  else
  begin
    with CDSMJID do
    begin
      CDSTM.Last;
      CDSTM.Append;
      for i:=0 to FieldCount-1 do
      begin
         CDSTM.fields[i].value:=Fields[i].Value ;
      end;
      CDSTM.Post;
    end;
    CDSMJID.Delete;
  end;
end;

procedure TfrmCPDBao.cxButton4Click(Sender: TObject);
var
  strsql:string;
begin
  if CDSSEL.IsEmpty then Exit;
  if CDSSEL.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    CDSSEL.DisableControls;
    with CDSSEL do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean then
        begin
          strsql:='卷号：'+trim(CDSSEL.FieldByName('MJID').AsString)+';包ID:'+trim(CDSSEL.FieldByName('BAOID').AsString)+
          '包号：'+trim(CDSSEL.FieldByName('baoNo').AsString);
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY Set baoID='''',baoNo=''''  ');
            sql.Add('where MJID='''+Trim(CDSSEL.fieldbyname('MJID').AsString)+'''');
            sql.Add('UPDATE CK_BanCP_CR SET BaoNo='''',baoID='''' where MJID='''+Trim(CDSSEL.fieldbyname('MJID').AsString)+'''');

            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' '+quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(','+quotedstr(trim(self.Caption)));
            sql.Add(','+quotedstr(trim('撤销打包')));
            sql.Add(','+quotedstr(trim(strsql)));
            sql.Add(','+quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;
          delete;
        end
        else
        begin
          Next;
        end;
      end;
    end;
    
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Order_JD_ByOther @MainType=''打包''');
      sql.Add(',@DName='+quotedstr(Trim(DName)));
      ExecSQL;
    end;
    
    CDSSEL.EnableControls;
    application.MessageBox('数据撤销成功！','提示信息');
    QueryTm();
    exit;
  except
    CDSSEL.EnableControls;
    application.MessageBox('数据撤销失败！','提示信息',0);
  end;
end;

procedure TfrmCPDBao.cxButton5Click(Sender: TObject);
begin
  QueryCDSSEL();
  QueryTm();
end;

procedure TfrmCPDBao.cxButton7Click(Sender: TObject);
begin
  SDofilter(ADOQueryTM,SGetFilters(Panel5,1,2));
  SCreateCDS20(ADOQueryTM,CDSTM);
  SInitCDSData20(ADOQueryTM,CDSTM);
end;

procedure TfrmCPDBao.N1Click(Sender: TObject);
begin
   if CDSTM.IsEmpty then Exit;
  CDSTM.DisableControls;
  with CDSTM do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=True;
      Post;
      Next;
    end;
  end;
  CDSTM.EnableControls;
end;

procedure TfrmCPDBao.N2Click(Sender: TObject);
begin
  if CDSTM.IsEmpty then Exit;
  CDSTM.DisableControls;
  with CDSTM do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
      Next;
    end;
  end;
  CDSTM.EnableControls;
end;

procedure TfrmCPDBao.MenuItem1Click(Sender: TObject);
begin
  if CDSSEL.IsEmpty then Exit;
  CDSSEL.DisableControls;
  with CDSSEL do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=True;
      Post;
      Next;
    end;
  end;
  CDSSEL.EnableControls;
end;

procedure TfrmCPDBao.MenuItem2Click(Sender: TObject);
begin
   if CDSSEL.IsEmpty then Exit;
  CDSSEL.DisableControls;
  with CDSSEL do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
      Next;
    end;
  end;
  CDSSEL.EnableControls;
end;

procedure TfrmCPDBao.cxButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption+Tv2.Name,Tv2,'成品仓库');
  WriteCxGrid(self.Caption+Tv3.Name+'1',Tv3,'成品仓库');
  WriteCxGrid(self.Caption+Tv4.Name,Tv4,'成品仓库');
  close;
end;

procedure TfrmCPDBao.cxButton8Click(Sender: TObject);
begin
  PrintBao(CDSSEL.fieldbyname('BaoId').AsString);
end;

procedure TfrmCPDBao.MovePanel1DblClick(Sender: TObject);
begin
  Button4.Click;
end;

procedure TfrmCPDBao.N3Click(Sender: TObject);
var
  FBaoId:string;
begin
  FBaoId:=Trim(CDSSEL.FieldByName('BaoID').AsString);
  with CDSSEL do
  begin
    DisableControls;
     while Locate('SSel;BaoID', VarArrayOf([False,FBaoId]),[]) do
     begin
       Edit;
       FieldByName('Ssel').Value:=True;
       Post;
     end;
     EnableControls;
  end;
end;

procedure TfrmCPDBao.N4Click(Sender: TObject);
var
  FBaoId:string;
begin
  FBaoId:=Trim(CDSSEL.FieldByName('BaoID').AsString);
  with CDSSEL do
  begin
    DisableControls;
     while Locate('SSel;BaoID', VarArrayOf([true,FBaoId]),[]) do
     begin
       Edit;
       FieldByName('Ssel').Value:=false;
       Post;
     end;
     EnableControls;
  end;
end;

procedure TfrmCPDBao.cxButton9Click(Sender: TObject);
var
  i,j,k,MLQty,xs,dbps:Integer;
  ts:Double;
  msg:string;
begin
 MLQty:=StrToInt(ComboBox5.Text);
  if ComboBox4.Text='手动打包' then
  begin
    with CDSTM do
    begin
      DisableControls;
      while Locate('ssel',true,[]) do
      begin
        CDSMJID.Append;
        CDSMJID.FieldByName('SDefNote').Value:='打包成功';
        CDSMJID.FieldByName('MJID').Value:=trim(Fieldbyname('MJID').AsString);
        CDSMJID.FieldByName('MJXH').Value:=trim(Fieldbyname('MJXH').AsString);
        CDSMJID.FieldByName('PRTCodeName').Value:=trim(Fieldbyname('PRTCodeName').AsString);
        CDSMJID.FieldByName('MPRTCodeName').Value:=trim(Fieldbyname('MPRTCodeName').AsString);
        CDSMJID.FieldByName('PRTColor').Value:=trim(Fieldbyname('PRTColor').AsString);
        CDSMJID.FieldByName('PRTHX').Value:=trim(Fieldbyname('PRTHX').AsString);
        CDSMJID.FieldByName('MJStr4').Value:=trim(Fieldbyname('MJStr4').AsString);
        CDSMJID.FieldByName('MJType').Value:=trim(Fieldbyname('MJType').AsString);
        CDSMJID.FieldByName('BaoNo').Value:=trim(BaoNo.Text);
        CDSMJID.FieldByName('BaoID').Value:=trim(BaoID.Text);
        CDSMJID.FieldByName('SOrdQty1').Value:=1;
        CDSMJID.FieldByName('MJLen').Value:=Fieldbyname('MJLen').AsFloat;
        CDSMJID.FieldByName('MJMaoZ').Value:=Fieldbyname('MJMaoZ').AsFloat;
        CDSMJID.FieldByName('MJQty3').Value:=Fieldbyname('MJQty3').AsFloat;
        CDSMJID.FieldByName('MJQty4').Value:=Fieldbyname('MJQty4').AsFloat;
        CDSMJID.FieldByName('MJTypeOther').Value:=trim(Fieldbyname('MJTypeOther').AsString);
        CDSMJID.FieldByName('mainID').Value:=trim(Fieldbyname('mainID').AsString);
        CDSMJID.FieldByName('subID').Value:=trim(Fieldbyname('subID').AsString);
        CDSMJID.Post;
        CDSTM.Delete;
      end;
      first;
      EnableControls;
    end;
    tv2.Controller.EditingController.ShowEdit();
    tv3.Controller.EditingController.ShowEdit();
  end
  else
  begin
    //自动打包
    if CDSTM.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择数据','提示',0);
      Exit;
    end;
    try
      CDSTM.DisableControls;
      ADOQueryCmd.Connection.BeginTrans;
      //删除历史数据
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
        ExecSQL;
      end;
      //插入打包数据
      with CDSTM do
      begin
        First;
        while not Eof do
        begin
          If Fieldbyname('Ssel').AsBoolean then
          begin
            with ADOQueryCmd do
            begin
              Close;
              SQL.Clear;
              SQL.Add('insert into TBSubID(SubId,Dname) values('+quotedstr(Trim(CDSTM.fieldbyname('MJID').AsString))+','+quotedstr(Trim(DCode))+')');
              ExecSQL;
            end;
          end;
          Next;
        end;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDSTM.EnableControls;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      CDSTM.EnableControls;
    end;

    if ComboBox4.Text='颜色打包' then
    begin
      //计算打包数量及剩余数量
      with ADOQueryTmp do
      begin
        Close;
        SQL.Clear;
        SQL.Add('exec [P_Do_GetDaBaoTJ] '+quotedstr(Trim(IntToStr(MLQty))));
        sql.Add(','+quotedstr(Trim(DCode)));
        sql.Add(','+quotedstr('1'));
        Open;
      end;
      msg:=ADOQueryTmp.FieldByName('msg').AsString;
      xs:=ADOQueryTmp.FieldByName('xs').AsInteger;
      dbps:=ADOQueryTmp.FieldByName('dbps').AsInteger;
      ts:=dbps/xs;
      //获取实际打包数
      if  Trim(ZBS.Text)<>'全部' then
      begin
        if xs>StrToInt(Trim(ZBS.Text)) then
        begin
          xs:= StrToInt(Trim(ZBS.Text));
          if  (ts*xs)=(ts*xs) then
          begin
             dbps:=Ceil(ts*xs);
          end
          else
          begin
             Application.MessageBox('已出库数据不能转单!','提示',0);
             Exit;
          end;
        end;
      end;
      
      if Application.MessageBox(PChar(msg),'确认打包',MB_YESNO)=6 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' exec [P_Do_DaBao] '+quotedstr(Trim(DCode))
          +','+quotedstr('颜色')
          +','+inttostr(xs)+','+inttostr(MLQty));
          Open;
        end;
        with ADOQueryCmd do
        begin
          while not Eof do
          begin
            PrintBao(Trim(FieldByName('BaoId').AsString));
            Sleep(200);
            Next;  
          end;  
        end;
       ShowMovePanel(msg,dbps);
      end;
    end;

    if ComboBox4.Text='花型打包' then
    begin
      //计算打包数量及剩余数量
      with ADOQueryTmp do
      begin
        Close;
        SQL.Clear;
        SQL.Add('exec [P_Do_GetDaBaoTJ] '+quotedstr(Trim(IntToStr(MLQty))));
        sql.Add(','+quotedstr(Trim(DCode)));
        sql.Add(','+quotedstr('3'));
        Open;
      end;
      msg:=ADOQueryTmp.FieldByName('msg').AsString;
      xs:=ADOQueryTmp.FieldByName('xs').AsInteger;
      dbps:=ADOQueryTmp.FieldByName('dbps').AsInteger;
      ts:=dbps/xs;
      //获取实际打包数
      if  Trim(ZBS.Text)<>'全部' then
      begin
        if xs>StrToInt(Trim(ZBS.Text)) then
        begin
          xs:= StrToInt(Trim(ZBS.Text));
          if  (ts*xs)=(ts*xs) then
          begin
             dbps:=Ceil(ts*xs);
          end
          else
          begin
             Application.MessageBox('已出库数据不能转单!','提示',0);
             Exit;
          end;
        end;
      end;
      
      if Application.MessageBox(PChar(msg),'确认打包',MB_YESNO)=6 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' exec [P_Do_DaBao] '+quotedstr(Trim(DCode))
          +','+quotedstr('花型')
          +','+inttostr(xs)+','+inttostr(MLQty));
          Open;
        end;
        with ADOQueryCmd do
        begin
          while not Eof do
          begin
            PrintBao(Trim(FieldByName('BaoId').AsString));
            Sleep(200);
            Next;
          end;
        end;
        ShowMovePanel(msg,dbps); 
      end;

    end;

    if ComboBox4.Text='款型打包' then
    begin
      with ADOQueryTmp do
      begin
        Close;
        SQL.Clear;
        SQL.Add('exec [P_Do_GetDaBaoTJ] '+quotedstr(Trim(IntToStr(MLQty))));
        sql.Add(','+quotedstr(Trim(DCode)));
        sql.Add(','+quotedstr('2'));
        Open;
      end;
      msg:=ADOQueryTmp.FieldByName('msg').AsString;
      xs:=ADOQueryTmp.FieldByName('xs').AsInteger;
      dbps:=ADOQueryTmp.FieldByName('dbps').AsInteger;
      ts:=dbps/xs;
      if  Trim(ZBS.Text)<>'全部' then
      begin
        if xs>StrToInt(Trim(ZBS.Text)) then
        begin
          xs:= StrToInt(Trim(ZBS.Text));
          if  (ts*xs)=(ts*xs) then
          begin
             dbps:=Ceil(ts*xs);
          end
          else
          begin
             Application.MessageBox('已出库数据不能转单!','提示',0);
             Exit;
          end;
        end;
      end;

      if Application.MessageBox(PChar(msg),'确认打包',MB_YESNO)=6 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' exec [P_Do_DaBao] '+quotedstr(Trim(DCode))
          +','+quotedstr('款型')
          +','+inttostr(xs)+','+inttostr(MLQty));
          Open;
        end;
        with ADOQueryCmd do
        begin
          while not Eof do
          begin
            PrintBao(Trim(FieldByName('BaoId').AsString));
            Sleep(200);
            Next;
          end;
        end;
        ShowMovePanel(msg,dbps);
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Order_JD_ByOther @MainType=''打包''');
      sql.Add(',@DName='+quotedstr(Trim(DName)));
      ExecSQL;
    end;
     QueryTm();
     QueryCDSSEL();
  end;
end;

end.
