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;

type
  TfrmCPDBao = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    Panel1: TPanel;
    PHYG: TBtnEditA;
    defstr2: TComboBox;
    Label16: TLabel;
    Label15: TLabel;
    packNo: TcxCurrencyEdit;
    Label14: TLabel;
    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;
    Edit2: TEdit;
    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;
    cxButton1: TcxButton;
    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;
    cxButton2: 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_Fbz: TCheckBox;
    Fbq: TBtnEditA;
    ComboBox2: TComboBox;
    check_bz: TCheckBox;
    BaoBq: TBtnEditA;
    cxButton5: TcxButton;
    cxButton6: 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;
    MJXH: TEdit;
    Label11: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);

    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 cxButton1Click(Sender: TObject);
    procedure baoNoExit(Sender: TObject);
    procedure BtnEditA1BtnClick(Sender: TObject);
    procedure cxButton3Click(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure cxButton4Click(Sender: TObject);
    procedure FbqBtnClick(Sender: TObject);
    procedure BaoBqBtnClick(Sender: TObject);
    procedure cxButton5Click(Sender: TObject);
    procedure cxButton6Click(Sender: TObject);
    procedure cxButton7Click(Sender: TObject);
  private
    Rolls:integer;
    procedure InitGrid();
    procedure InitMJID();
    function  SaveRK():Boolean;
    Procedure QueryRk();
    Procedure QueryTM();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCPDBao: TfrmCPDBao;
  newh:hwnd;

implementation
uses
  U_DataLink,U_Fun, U_ZDYHelp, U_OrderSelRK;
{$R *.dfm}
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('and  A.mjstr2=''未入库'' and isnull(A.BaoID,'''')='''' and C.orderNo='+quotedstr(trim(BtnEditA1.Text)));
    open;
  end;
  SCreateCDS20(ADOQueryTM,CDSTM);
  SInitCDSData20(ADOQueryTM,CDSTM);

  SCreateCDS20(ADOQueryTM,CDSMJID);
//  SInitCDSData20(ADOQueryMain,CDSMJID);
  SCreateCDS20(ADOQueryTM,CDSSEL);
//  SInitCDSData20(ADOQueryMain,CDSSEL);

  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',3,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取包条码最大号失败！','提示',0);
      Exit;
    end;
    BaoID.Text:=trim(FBaoid);
  end;

  SMNO.SetFocus;
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;

function TfrmCPDBao.SaveRK():Boolean;
var
  maxno:String;
  CRID:Integer;
  MaxCkNo,MaxCkSubNo:String;
  MhcTime:TdateTime;
begin
  Result:=false;
  ADOQueryCmd.Connection.BeginTrans;

  try
    {with ADOQueryTmp  do
    begin
      Close;
      sql.Clear;
      SQL.add('Update CK_BanCP_CRID Set CRID=CRID+1');
      sql.Add('select * from CK_BanCP_CRID');
      Open;
    end;
    CRID:=ADOQueryTmp.fieldbyname('CRID').AsInteger;
    if GetLSNo(ADOQueryTmp,MaxCkNo,'JR','CK_BanCP_CR',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取成品仓库最大号失败！','提示',0);
      Exit;
    end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_CR where 1<>1');
            Open;
          end;
          mhcTime:=SGetServerDateTime(ADOQueryTmp);

          with ADOQueryCmd do
          begin
            Append;
            FieldByName('MainId').Value:=Trim(ADOQueryMain.fieldbyname('MainId').AsString);
            FieldByName('SubId').Value:=Trim(ADOQueryMain.fieldbyname('SubId').AsString);
            FieldByName('C_Code').Value:=Trim(ADOQueryMain.fieldbyname('PRTCode').AsString);
            FieldByName('C_CodeName').Value:=Trim(ADOQueryMain.fieldbyname('PRTCodeName').AsString);
            //FieldByName('C_SPec').Value:=Trim(ADOQueryMain.fieldbyname('C_SPec').AsString);
            FieldByName('C_Color').Value:=Trim(ADOQueryMain.fieldbyname('PRTColor').AsString);
            FieldByName('APID').Value:=Trim(ADOQueryMain.fieldbyname('APID').AsString);
            FieldByName('MJID').Value:=Trim(ADOQueryMain.fieldbyname('MJId').AsString);
            FieldByName('mjxh').Value:=Trim(ADOQueryMain.fieldbyname('mjxh').AsString);
            FieldByName('BaoNo').Value:=Trim(BaoNo.Text);
            FieldByName('BaoID').Value:=Trim(BaoID.Text);

            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',mhcTime);
            FieldByName('CRFlag').Value:='入库';
            FieldByName('CRType').Value:='检验入库';
            FieldByName('CRID').Value:=CRID;
            FieldByName('KGQty').Value:=0;
            FieldByName('Qty').Value:=ADOQueryMain.fieldbyname('MJLen').Value;
            FieldByName('RollNum').Value:=1;
            FieldByName('QtyUnit').Value:=ADOQueryMain.fieldbyname('MJTypeOther').Value;
            FieldByName('CPType').Value:=ADOQueryMain.fieldbyname('MJType').Value;
            //FieldByName('Note').Value:=ADOQueryMain.fieldbyname('Note').Value;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTmp);
            //fieldbyname('kcKw').Value:=GetKw(ADOQueryTmp,ADOQueryMain.fieldbyname('PRTCodeName').AsString,ADOQueryMain.fieldbyname('PRTColor').AsString);
            Post;
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_KC where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('CRID').Value:=CRID;
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',mhcTime);
            FieldByName('CRType').Value:='检验入库';
            FieldByName('CPType').Value:=ADOQueryMain.fieldbyname('MJType').Value;
            FieldByName('C_Code').Value:=Trim(ADOQueryMain.fieldbyname('PRTCode').AsString);
            FieldByName('C_CodeName').Value:=Trim(ADOQueryMain.fieldbyname('PRTCodeName').AsString);
            //FieldByName('C_SPec').Value:=Trim(ADOQueryMain.fieldbyname('C_SPec').AsString);
            FieldByName('C_Color').Value:=Trim(ADOQueryMain.fieldbyname('PRTColor').AsString);
            FieldByName('BaoNo').Value:=Trim(BaoNo.Text);
            FieldByName('BaoID').Value:=Trim(BaoID.Text);
            FieldByName('MJID').Value:=Trim(ADOQueryMain.fieldbyname('MJID').AsString);
            FieldByName('KCKGQty').Value:=0;
            FieldByName('KCQty').Value:=ADOQueryMain.fieldbyname('MJlen').Value;
            FieldByName('RollNum').Value:=1;
            FieldByName('KCQtyUnit').Value:=ADOQueryMain.fieldbyname('MJTypeOther').Value;
            FieldByName('mjxh').Value:=Trim(ADOQueryMain.fieldbyname('mjxh').AsString);
            //FieldByName('KCNote').Value:=ADOQueryMain.fieldbyname('Note').Value;
            //fieldbyname('kcKw').Value:=GetKw(ADOQueryTmp,ADOQueryMain.fieldbyname('PRTCodeName').AsString,ADOQueryMain.fieldbyname('PRTColor').AsString);
            Post;
          end; }
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set BaoNo='''+trim(BaoNo.Text)+''',BaoID='''+trim(Baoid.text)+''' where MJID='''+Trim(ADOQueryMain.fieldbyname('MJID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  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,CDSMJID);
  SInitCDSData20(ADOQueryMain,CDSMJID);
  SCreateCDS20(ADOQueryMain,CDSSEL);
  SInitCDSData20(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);
    CDSMJID.delete;
  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.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmCPDBao.FormCreate(Sender: TObject);
begin
  Panel2.Align:=alClient;
  //readCxGrid(self.Caption+tv1.Name,Tv1,'成品仓库');
  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(BaoNo.Text)='' then
    begin
      application.MessageBox('包号不能为空','提示');
      exit;
    end;
    if trim(BaoID.Text)='' then
    begin
      application.MessageBox('包条码不能为空','提示');
      exit;
    end;
    IF CDSTM.IsEmpty then exit;
    if trim(smno.Text)='' then exit;
    IF CDSMJID.Locate('MJID',trim(smno.Text),[]) then
    begin
      MovePanel1.Visible:=true;
      Edit2.Text:=SmNO.Text;
      Label2.Caption:='此卷号重复扫描！';
      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
          MovePanel1.Visible:=true;
          Edit2.Text:=SmNO.Text;
          Label2.Caption:='此卷号错误！';
        end
        else
        begin
          If not CDSTM.Locate('orderNoM',trim(fieldbyname('OrderNoM').AsString),[]) then
          begin
            MovePanel1.Visible:=true;
            Edit2.Text:=SmNO.Text;
            Label2.Caption:='此卷不属于当前订单！';
          end
          else
          begin
            IF Fieldbyname('MJstr2').AsString<>'未入库' then
            begin
              MovePanel1.Visible:=true;
              Edit2.Text:=SmNO.Text;
              Label2.Caption:=fieldbyname('Mjstr2').AsString;
            end
            else
            begin
              MovePanel1.Visible:=true;
              Edit2.Text:=SmNO.Text;
              Label2.Caption:='此卷已打包！';
            end;
          end;
        end;
      end;
      smno.Text:='';
      smno.SetFocus;
      exit;
    end
    else
    begin
      initMJID();
    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;
 // InitGrid();
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
  MovePanel1.Visible:=False;
  SmNO.SetFocus;
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',3,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:string;
    i:Integer;
begin
  if trim(BaoID.Text)='' then
  begin
    application.MessageBox('包条码不能未空','提示');
    exit;
  end;
  IF CDSMJID.IsEmpty then exit;
  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)+'''');
          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;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('数据修改失败！','提示信息',0);
    exit;
  end;


{  if trim(BaoID.Text)='' then
  begin
    application.MessageBox('包条码不能未空','提示');
    exit;
  end;   }
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.Baoid,A.BaoNo,A.MJTypeOther as QtyUnit,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.Mprtspec,D.OrdPerson1,D.MPRTKuanNO,D.LBName,D.NLBName,');
    sql.Add('PRTColor=DBO.F_Get_Order_SubStr(BaoID,''BNColor''),SOrddefstr1=DBO.F_Get_Order_SubStr(BaoID,''BNSOrddefstr1''),');
    sql.Add('SOrddefstr4=DBO.F_Get_Order_SubStr(BaoID,''BNSOrddefstr4''),PRtHX=DBO.F_Get_Order_SubStr(BaoID,''BNPRtHX''), ');
    sql.Add('Mjstr4=DBO.F_Get_Order_SubStr(BaoID,''BNGangNo''), ');
    sql.Add('BNMJLENLIST=DBO.F_Get_Order_SubStr(BaoID,''BNMJLENLIST''), ');
    sql.Add('PRTkuanNo=DBO.F_Get_Order_SubStr(BaoID,''BNPrtkuanno''),');
    sql.Add('khConNo=(select top 1 khConNo from JYOrderCon_Main X where X.ConNO=D.conNO), ');
    sql.Add('MprtCodeNameEng=(select top 1 Note from KH_Zdy X where X.zdyName=D.MprtCodeName), ');
    SQL.ADD('count(A.MJID) as JSl,sum(A.MJMaoZ) MJMAOZ,sum(MJQty3) as MJQty3,sum(MJQty4) as MJQty4,SUM(A.MJLen)as MJLen');
    sql.Add('from WFB_MJJY A');
    sql.Add(' inner join JYOrder_Sub C on C.SubID=A.SubID');
    sql.Add(' inner join JYOrder_Main D on D.MainID=A.MainID');
    SQL.Add('where A.BaoID='''+Trim(BaoID.Text)+'''');
    SQL.ADD('group by A.Baoid,A.BaoNo,A.MJTypeOther,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.Mprtspec,D.OrdPerson1,D.MPRTKuanNO,D.LBName,D.NLBName');
    Open;
  end;

  if ADOQueryPrint.RecordCount>1 then
  begin
    Application.MessageBox('打包错误，请重新打包!','提示',0);
    Exit;
  end;
  if ADOQueryPrint.RecordCount<1 then
  begin
    Application.MessageBox('此包号还未被保存，请保存数据，在打印条码！','提示',0);
    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',3,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
    if Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)<>'' then
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)+'.rmf' 
    else
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\包标签.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.ShowReport;
      RM2.printReport;
     end;
  end;

  Rolls:=0;
  SmNO.SetFocus;
{
  IF check_Fbz.Checked then
  begin
       fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(Fbq.Text)+'.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(ComboBox2.Text),1);
   //RM2.ShowReport;
         RM2.printReport;
       end;
   end;
   }
end;

procedure TfrmCPDBao.cxButton1Click(Sender: TObject);
begin
  TBClose.Click;
end;

procedure TfrmCPDBao.baoNoExit(Sender: TObject);
var
  FBaoID:string;
begin
 IF baoNo.Text<>'' then
 begin
    if GetLSNo(ADOQueryTmp,FBaoID,'BI','WFB_MJJY',3,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);
begin
    frmOrderSelRK:=TfrmOrderSelRK.Create(Application);
    with frmOrderSelRK do
    begin
      if ShowModal=1 then
      begin
        CDS_OrderSel.DisableControls;
        with CDS_OrderSel do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').Value=True then
            begin
              BtnEditA1.Text:=trim(CDS_OrderSel.fieldbyname('OrderNo').asstring);
            end;
            Next;
          end;
        end;
        CDS_OrderSel.EnableControls;
      end;
    end;
  QueryTm();
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.cxButton2Click(Sender: TObject);
begin
  IF CDSMJID.IsEmpty then exit;
  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)+'''');
          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;
  //  application.MessageBox('数据修改成功！','提示信息',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('数据修改失败！','提示信息',0);
  end;
end;

procedure TfrmCPDBao.cxButton4Click(Sender: TObject);
begin
  if CDSSEL.IsEmpty then Exit;
  if CDSSEL.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    CDSSEL.DisableControls;
  //  ADOQueryCmd.Connection.BeginTrans;
    with CDSSEL do
    begin
      First;
      while FieldByName('SSel').AsBoolean do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update WFB_MJJY Set baoID='''',baoNo=''''  ');
          sql.Add('where MJID='''+Trim(CDSSEL.fieldbyname('MJID').AsString)+'''');
          ExecSQL;
        end;
        delete;
      end;
    end;
  //  ADOQueryCmd.Connection.CommitTrans;
    CDSSEL.EnableControls;
    application.MessageBox('数据撤销成功！','提示信息');
    QueryTm();
    exit;
  except
  //  ADOQueryCmd.Connection.RollbackTrans;
    CDSSEL.EnableControls;
    application.MessageBox('数据撤销失败！','提示信息',0);
  end;
end;

procedure TfrmCPDBao.FbqBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='BAOFFLAG';
      flagname:='包副标签';
      if ShowModal=1 then
      begin
        Fbq.Text:=trim(ClientDataSet1.fieldbyname('zdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPDBao.BaoBqBtnClick(Sender: TObject);
type
  TMyFunc = function(App:Tapplication; FormH:hwnd; FormID:integer;
                  Language: integer; WinStyle:integer;
                  GCode: Pchar; GName: Pchar; DataBase:Pchar;Title:PChar;
                  Parameters1:PChar;Parameters2:PChar;Parameters3:PChar;Parameters4:PChar;
                  Parameters5:PChar;Parameters6:PChar;Parameters7:PChar;Parameters8:PChar;
                  Parameters9:PChar;Parameters10:PChar;DataBaseStr:PChar):hwnd;stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th:Thandle;
  LabInt,labname:String;
begin
  //if PPInt=2 then Exit;
  Ddatabase:=StringOfChar(' ', 32);
  Th := LoadLibrary('LabelSet.dll');
  if Th > 0 then
  begin
    try
      Tp := GetProcAddress(Th, 'GetDllForm');
      if Tp <> nil then
      begin
        Tf := TMyFunc(Tp);
        newh:=Tf(Application,0,2,0,0,
          PChar(DCode),
          PChar(DName),
          PChar(Ddatabase),
          PChar('标签模版设置'),
          PChar(''),
          PChar(''),
          '','','','','','','','',PChar(DConString)
          );
          if Trim(PChar(Ddatabase))<>'' then
          begin
            Ddatabase:=Trim(PChar(Ddatabase));
          //  LabInt:=Trim( LeftBStr(Ddatabase,Pos('|',Ddatabase)-1) ) ;
            BaoBq.Text:=Trim(RightBStr(Ddatabase,Length(Ddatabase)-Pos('|',Ddatabase) ) );
          end;
      end
      else
      begin
        ShowMessage('打印执行错误');
      end;
    finally
    // FreeLibrary();
    end;
  end
  else
  begin
    ShowMessage('找不到'+Trim('LabelSet.dll'));
  end;

end;

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

procedure TfrmCPDBao.cxButton6Click(Sender: TObject);
begin
  IF CDSTM.IsEmpty then exit;
  if trim(BaoNo.Text)='' then
  begin
    application.MessageBox('包号不能为空','提示');
    exit;
  end;
  if trim(BaoID.Text)='' then
  begin
    application.MessageBox('包条码不能为空','提示');
    exit;
  end;

  with CDSTM do
  begin
    DisableControls;
  //  first;
    while Locate('ssel',true,[]) do
    begin
      IF CDSMJID.Locate('MJID',trim(CDSTM.fieldbyname('MJID').AsString),[]) then
      begin
        MovePanel1.Visible:=true;
        Edit2.Text:=SmNO.Text;
        Label2.Caption:='此卷号重复扫描！';
        smno.Text:='';
        smno.SetFocus;
        EnableControls;
        exit;
      end;
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value:='打包成功';
      CDSMJID.FieldByName('MJID').Value:=trim(Fieldbyname('MJID').AsString);
//            CDSMJID.FieldByName('CRID').Value:=trim(ADOQueryMain.Fieldbyname('CRID').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;
      tv2.Controller.EditingController.ShowEdit();
      tv3.Controller.EditingController.ShowEdit();
    end;
    first;
    EnableControls;
  end;
end;

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

end.
