unit U_CpRkSaoMNew;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, StdCtrls, ExtCtrls, ADODB, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, MovePanel, cxCheckBox, Menus,
  ComCtrls, BtnEdit, RM_System, RM_Common, RM_Class, RM_GridReport,
  RM_Dataset;

type
  TfrmCpRkSaoMNew = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    DataSource1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel1: TPanel;
    BaoID: TEdit;
    Label1: TLabel;
    v1Column5: TcxGridDBColumn;
    Button2: TButton;
    Button3: TButton;
    CDS_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuerySub: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    v3KgQty: TcxGridDBColumn;
    Button1: TButton;
    cxGridPopupMenu4: TcxGridPopupMenu;
    MovePanel1: TMovePanel;
    Edit1: TEdit;
    Edit2: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Button4: TButton;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column2: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    Button5: TButton;
    v1Column7: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MovePanel2: TMovePanel;
    CRTime: TDateTimePicker;
    Label4: TLabel;
    Button6: TButton;
    RKOrdID: TEdit;
    RKPlace: TBtnEditA;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    BaoNo: TEdit;
    RKOrdPS: TEdit;
    Button7: TButton;
    v3Column7: TcxGridDBColumn;
    RM1: TRMGridReport;
    Button8: TButton;
    ADOQueryPrt: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    v3Column8: TcxGridDBColumn;
    ComboBox1: TComboBox;
    Edit3: TEdit;
    Label8: TLabel;
    v1MJID: TcxGridDBColumn;
    Label9: TLabel;
    gangNo: TEdit;
    Bao: TEdit;
    Label10: TLabel;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    Label12: TLabel;
    N3: TMenuItem;
    v1Column11: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BaoIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure RKPlaceBtnClick(Sender: TObject);
    procedure Tv3CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Edit3Click(Sender: TObject);
    procedure BaoKeyPress(Sender: TObject; var Key: Char);
    procedure N3Click(Sender: TObject);
  private
    { Private declarations }
    FBaoId:String;
    procedure InitGrid();
    procedure InitSubGrid();
    procedure SavedataCK();

  public
    { Public declarations }
  end;

var
  frmCpRkSaoMNew: TfrmCpRkSaoMNew;

implementation
uses
U_DataLink,U_Fun,U_RTFun,U_OrderSelRK,U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmCpRkSaoMNew.FormDestroy(Sender: TObject);
begin
  frmCpRkSaoMNew:=nil;
end;
procedure TfrmCpRkSaoMNew.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,cast(BaoNO as int) intBaoNO,OrderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId) ');
      SQL.Add(',PRTColor=(select PRTColor from JYOrder_Sub where SubId=A.SubId)');
      SQL.Add(',SOrddefstr1=(select SOrddefstr1 from JYOrder_Sub where SubId=A.SubId)');
      sql.Add(',AOrdDefstr1=(select AOrdDefstr1 from JYOrder_Sub_AnPai where ApId=A.ApId)');
      sql.Add(',MJXH=(select MJXH from WFB_MJJY where MJID=A.MJID)');
      sql.Add(',MJQty4=(select MJQty4 from WFB_MJJY where MJID=A.MJID)');
      sql.Add('from CK_BanCP_CR A');
      sql.add('where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SCreateCDS20(ADOQueryMain,CDS_MainSel);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCpRkSaoMNew.FormShow(Sender: TObject);
begin
  ReadCxGrid('订单选择',Tv2,'成品仓库');
  ReadCxGrid('成品出库',Tv1,'成品仓库');
  ReadCxGrid('成品出库Sels',Tv3,'成品仓库');
  InitSubGrid();
  InitGrid();
  CRTime.DateTime:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmCpRkSaoMNew.BaoIDKeyPress(Sender: TObject; var Key: Char);
var
  maxno:String;
  CRID:Integer;
  MaxCkNo,MaxCkSubNo:String;
begin
  if Key=#13 then
  begin
    Label12.Caption:='';
    if CDS_Sub.IsEmpty then
    begin
      BaoID.Text:='';
      Application.MessageBox('未选单不能扫描出库！','提示',0);
      Exit;
    end;
    if Trim(RKOrdID.Text)='' then
    begin
      Application.MessageBox('入库单号不能为空!','提示',0);
      Exit;
    end;
    if CDS_MainSel.Locate('MJId',Trim(BaoID.Text),[]) then
    begin
      MovePanel1.Visible:=True;
      Edit2.Text:=Trim(BaoID.Text);
      Label12.Caption:='提醒：此卷重复扫描！';
      BaoID.Text:='';
      Exit;
    end;
    if CDS_Main.Locate('MJId',Trim(BaoID.Text),[])=False then
    begin
      MovePanel1.Visible:=True;
      Edit2.Text:=Trim(BaoID.Text);
      Label12.Caption:='提醒：此卷不包含在待入库的卷数据中！';
      BaoID.Text:='';
      Exit;
    end;
   if Trim(BaoNo.Text)='' then
    begin
      if Trim(Edit3.Text)='√' then
      begin
        application.MessageBox('请输入包号！','提示信息',MB_ICONERROR);
        exit;
      end;
    end;

    CDS_Main.Locate('MJId',Trim(BaoID.Text),[]);
    try
      ADOQueryCmd.Connection.BeginTrans;
      with CDS_Main do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPDATE CK_BanCP_CR SET CRFlag=''入库'',CRType=''检验入库'',FillTime=getdate() ');
          sql.add(',CRTime='''+trim(FormatDateTime('yyyy-MM-dd',CRTime.DateTime))+'''');
          sql.Add(',RKOrdId='''+trim(RKOrdId.Text)+'''');
          sql.Add(',Filler='''+trim(DName)+'''');
          sql.Add('FROM CK_BanCP_CR A ');
          sql.Add('where A.MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          sql.Add('and A.CRFlag=''待入库'' ');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' ');
          IF trim(gangNo.Text)<>'' then
          sql.Add(',MJStr4='+Quotedstr(trim(gangNo.Text)));
          sql.Add('where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          ExecSQL;
        end;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select Mainid from CK_BanCP_CR where RKOrdId='''+Trim(RKOrdID.Text)+'''');
        SQL.Add(' group by MainId');
        Open;
      end;
      if ADOQueryCmd.RecordCount>1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('不同订单不能使用相同的入库单号!','提示',0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Update_Order_JD_ByOther @MainType=''单卷入库'' ');
        sql.Add(',@OrdMainID='+quotedstr(Trim(CDS_Main.fieldbyname('MainId').AsString)));
        sql.Add(',@OrdSubID='+quotedstr(Trim(CDS_Main.fieldbyname('SubId').AsString)));
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      with CDS_MainSel do
      begin
        Append;
        FieldByName('MainId').Value:=CDS_Main.fieldbyname('MainId').Value;
        FieldByName('SubId').Value:=Self.CDS_Main.fieldbyname('SubId').Value;
        FieldByName('OrderNo').Value:=Self.CDS_Main.fieldbyname('OrderNo').Value;
        FieldByName('MJQty4').Value:=Self.ADOQueryTemp.fieldbyname('MJQty4').Value;
        FieldByName('KGQty').Value:=Self.CDS_Main.fieldbyname('KGQty').Value;
        FieldByName('Qty').Value:=Self.CDS_Main.fieldbyname('Qty').Value;
        FieldByName('QtyUnit').Value:=Self.CDS_Main.fieldbyname('QtyUnit').Value;
        FieldByName('MJId').Value:=Self.CDS_Main.fieldbyname('MJId').Value;
        FieldByName('CRId').Value:=Self.CDS_Main.fieldbyname('CRID').Value;
        FieldByName('APID').Value:=Self.CDS_Main.fieldbyname('APID').Value;
        FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
        FieldByName('BCID').Value:=Trim(MaxCkNo);
        FieldByName('RKOrdID').Value:=Trim(RKOrdID.Text);
        FieldByName('PRTColor').Value:=CDS_Main.fieldbyname('PRTColor').Value;
        FieldByName('MJXH').Value:=CDS_Main.fieldbyname('MJXH').Value;
        IF trim(gangNo.Text)<>'' then
        FieldByName('AOrdDefstr1').Value:=trim(gangNo.Text)
        else
        FieldByName('AOrdDefstr1').Value:=CDS_Main.fieldbyname('AOrdDefstr1').Value;
        FieldByName('SOrddefstr1').Value:=CDS_Main.fieldbyname('SOrddefstr1').Value;
        FieldByName('RKOrdID').Value:=Trim(RKOrdID.Text);
        FieldByName('BaoNo').Value:=Trim(CDS_Main.fieldbyname('BaoNo').AsString);
        FieldByName('intBaoNO').Value:=Trim(CDS_Main.fieldbyname('intBaoNO').AsString);
        FieldByName('BaoID').Value:=Trim(CDS_Main.fieldbyname('BaoID').AsString);
        FieldByName('Filler').Value:=Trim(CDS_Main.fieldbyname('Filler').AsString);
        Post;
      end;
      CDS_Main.Delete;
      MovePanel1.Visible:=True;
      if CDS_MainSel.IsEmpty=False then
        Edit1.Text:=IntToStr(Tv3.DataController.Summary.FooterSummaryValues[2])
      else
        Edit1.Text:='0';
      Edit2.Text:=Trim(BaoID.Text);
      BaoID.Text:='';
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select isnull(Count(*),0) ZPS from CK_BanCP_CR where RKOrdID='''+Trim(RKOrdID.Text)+'''');
        Open;
      end;
      RKOrdPS.Text:=Trim(ADOQueryCmd.fieldbyname('ZPS').AsString)+'匹';
      Exit;
    except
      BaoID.Text:='';
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('入库异常！','提示',0);
    end;
  end;
end;
procedure TfrmCpRkSaoMNew.SavedataCK();
var
  CRID:Integer;
  MaxCkNo,MaxCkSubNo:String;
begin
  //if Trim(Cds_Main.fieldbyname('SubType').AsString)='大卷' then
  //////////////////////////////////////////////////////////////保存到半成品仓库////////////////////////////////////////////////
    with ADOQueryCmd  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:=ADOQueryCmd.fieldbyname('CRID').Value;
    if GetLSNo(ADOQueryCmd,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;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('MainId').Value:=Trim(CDS_Main.fieldbyname('MainId').AsString);
      FieldByName('SubId').Value:=Trim(CDS_Main.fieldbyname('SubId').AsString);
      FieldByName('APID').Value:=Trim(CDS_Main.fieldbyname('APID').AsString);
      FieldByName('MJID').Value:=Trim(CDS_Main.fieldbyname('MJId').AsString);
      FieldByName('BCID').Value:=Trim(MaxCkNo);
      FieldByName('CRTime').Value:=SGetServerDateTime(ADOQueryTemp);
      FieldByName('CRFlag').Value:='入库';
      FieldByName('CRType').Value:='检验入库';
      //FieldByName('JTType').Value:=Trim(XJFlag);
      FieldByName('CRID').Value:=CRID;
      FieldByName('KGQty').Value:=CDS_Main.fieldbyname('KGQty').Value;
      FieldByName('Qty').Value:=CDS_Main.fieldbyname('Qty').Value;
      FieldByName('QtyUnit').Value:=CDS_Main.fieldbyname('QtyUnit').Value;
      FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
      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('MJID').Value:=Trim(CDS_Main.fieldbyname('MJID').AsString);
      FieldByName('KCKGQty').Value:=CDS_Main.fieldbyname('KGQty').Value;
      FieldByName('KCQty').Value:=CDS_Main.fieldbyname('Qty').Value;
      FieldByName('KCQtyUnit').Value:=CDS_Main.fieldbyname('QtyUnit').Value;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
      ExecSQL;
    end;
end;
procedure TfrmCpRkSaoMNew.Button2Click(Sender: TObject);
begin
  IF (trim(RKOrdID.Text)<>'') and (CDS_MainSel.RecordCount>0) then
  begin
    if Application.MessageBox('确定要关闭界面，不打印入库单吗？','提示',32+4)<>IDYES then Exit;
  end;
  tv1.Controller.EditingController.ShowEdit();
  tv3.Controller.EditingController.ShowEdit();
  Close;
  WriteCxGrid('订单选择',Tv2,'成品仓库');
  WriteCxGrid('成品出库',Tv1,'成品仓库');
  WriteCxGrid('成品出库Sels',Tv3,'成品仓库');
end;

procedure TfrmCpRkSaoMNew.Button3Click(Sender: TObject);
begin
  try
    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
              if Self.CDS_Sub.Locate('SubId',Trim(CDS_OrderSel.fieldbyname('SubId').AsString),[])=False then
              begin
                with Self.CDS_Sub do
                begin
                  Append;
                  FieldByName('MainId').Value:=Trim(CDS_OrderSel.fieldbyname('MainId').AsString);
                  FieldByName('SubId').Value:=Trim(CDS_OrderSel.fieldbyname('SubId').AsString);
                  FieldByName('CustomerNo').Value:=Trim(CDS_OrderSel.fieldbyname('CustomerNo').AsString);
                  FieldByName('OrderNo').Value:=Trim(CDS_OrderSel.fieldbyname('OrderNo').AsString);
                  FieldByName('CustomerNoName').Value:=Trim(CDS_OrderSel.fieldbyname('CustomerNoName').AsString);
                  FieldByName('MPRTCodeName').Value:=Trim(CDS_OrderSel.fieldbyname('MPRTCodeName').AsString);
                  FieldByName('PRTOrderQty').Value:=Trim(CDS_OrderSel.fieldbyname('PRTOrderQty').AsString);
                  FieldByName('OrderUnit').Value:=Trim(CDS_OrderSel.fieldbyname('OrderUnit').AsString);
                  FieldByName('PRTColor').Value:=Trim(CDS_OrderSel.fieldbyname('PRTColor').AsString);
                  FieldByName('MPRTMF').Value:=Trim(CDS_OrderSel.fieldbyname('MPRTMF').AsString);
                  FieldByName('MPRTKZ').Value:=Trim(CDS_OrderSel.fieldbyname('MPRTKZ').AsString);
                  Post;
                end;
              end;
            end;
            Next;
          end;
        end;
        CDS_OrderSel.EnableControls;
        CDS_Sub.DisableControls;
        with CDS_Sub do
        begin
          First;
          while not Eof do
          begin
            if Trim(Self.CDS_Sub.fieldbyname('SFlag').AsString)<>'2' then
            begin
              with Self.ADOQueryTemp do
              begin
                Close;
                SQL.Clear;
                sql.Add('select orderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId)  ');
                sql.Add(',PRTColor=(select  PRTColor from JYOrder_Sub JS where JS.SubId=A.SubId) ');
                sql.Add(',SOrddefstr1=(select  SOrddefstr1 from JYOrder_Sub JS where JS.SubId=A.SubId) ');
                sql.Add(',AOrdDefStr1=A.MJStr4,A.* ,cast(A.BaoNO as int) intBaoNO ');
                sql.Add(' from WFB_MJJY A ');
                sql.Add(' where isnull(A.MJStr2,'''')=''未入库'' ');
                SQL.Add(' and A.SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
                Open;
              end;
              Self.ADOQueryTemp.DisableControls;
              CDS_Main.DisableControls;
              with Self.ADOQueryTemp do
              begin
                First;
                while not Eof do
                begin
                  with CDS_Main do
                  begin
                    Append;
                    FieldByName('MainId').Value:=Self.ADOQueryTemp.fieldbyname('MainId').Value;
                    FieldByName('SubId').Value:=Self.ADOQueryTemp.fieldbyname('SubId').Value;
                    FieldByName('APId').Value:=Self.ADOQueryTemp.fieldbyname('APId').Value;
                    FieldByName('OrderNo').Value:=Self.ADOQueryTemp.fieldbyname('OrderNo').Value;
                    FieldByName('KgQty').Value:=Self.ADOQueryTemp.fieldbyname('MJMaoZ').Value;
                    FieldByName('MJQty4').Value:=Self.ADOQueryTemp.fieldbyname('MJQty4').Value;
                    FieldByName('Qty').Value:=Self.ADOQueryTemp.fieldbyname('MJLen').Value;
                    FieldByName('QtyUnit').Value:=Self.ADOQueryTemp.fieldbyname('MJTypeOther').Value;
                    FieldByName('MJId').Value:=Self.ADOQueryTemp.fieldbyname('MJId').Value;
                    FieldByName('CPType').Value:=Self.ADOQueryTemp.fieldbyname('MJType').Value;
                    FieldByName('PRTColor').Value:=Self.ADOQueryTemp.fieldbyname('PRTColor').Value;
                    FieldByName('AOrddefstr1').Value:=Self.ADOQueryTemp.fieldbyname('AOrddefstr1').Value;
                    FieldByName('SOrddefstr1').Value:=Self.ADOQueryTemp.fieldbyname('SOrddefstr1').Value;
                    FieldByName('MJXH').Value:=Self.ADOQueryTemp.fieldbyname('MJXH').Value;
                    FieldByName('baoID').Value:=Self.ADOQueryTemp.fieldbyname('baoID').Value;
                    FieldByName('baoNo').Value:=Self.ADOQueryTemp.fieldbyname('baoNo').Value;
                    FieldByName('intBaoNO').Value:=Self.ADOQueryTemp.fieldbyname('intBaoNO').Value;
                    FieldByName('Filler').Value:=Self.ADOQueryTemp.fieldbyname('Filler').Value;
                    Post;
                  end;
                  Next;
                end;
              end;
              CDS_Main.EnableControls;
              Self.ADOQueryTemp.EnableControls;
            end;
            CDS_Sub.Edit;
            CDS_Sub.FieldByName('SFlag').Value:='2';
            CDS_Sub.Post;
            Next;
          end;
        end;
        CDS_Sub.EnableControls;
        Button6.OnClick(self);
      end;
    end;
  finally
    frmOrderSelRK.Release;
  end;
end;

procedure TfrmCpRkSaoMNew.InitSubGrid();
begin
  try
    ADOQuerySub.DisableControls;
    with ADOQuerySub do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,Cast('''' as varchar(20))');
      sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.Mainid');
      sql.Add(' where 1<>1  ');

      Open;
    end;
    SCreateCDS20(ADOQuerySub,CDS_Sub);
    SInitCDSData20(ADOQuerySub,CDS_Sub);
  finally
    ADOQuerySub.EnableControls;
  end;
end;


procedure TfrmCpRkSaoMNew.Button1Click(Sender: TObject);
begin
  if CDS_MainSel.IsEmpty then Exit;
  if CDS_MainSel.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_MainSel.DisableControls;
    CDS_Main.DisableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
      SQL.Add('and MainType='+quotedstr('撤销入库') );
      ExecSQL;
    end;
    with CDS_MainSel do
    begin
      while CDS_MainSel.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into TBValueWhere(DName,MainType,wsql1) ');
          sql.Add('values('+quotedstr(Trim(DName))+','+quotedstr('撤销入库')
          +','+quotedstr(Trim(CDS_MainSel.fieldbyname('MJID').AsString))+')');
          ExecSQL;
        end;
        with CDS_Main do
        begin
          Append;
          FieldByName('MainId').Value:=CDS_MainSel.fieldbyname('MainId').Value;
          FieldByName('SubId').Value:=Self.CDS_MainSel.fieldbyname('SubId').Value;
          FieldByName('OrderNo').Value:=Self.CDS_MainSel.fieldbyname('OrderNo').Value;
          FieldByName('MJQty4').Value:=Self.ADOQueryTemp.fieldbyname('MJQty4').Value;
          FieldByName('KgQty').Value:=Self.CDS_MainSel.fieldbyname('KgQty').Value;
          FieldByName('Qty').Value:=Self.CDS_MainSel.fieldbyname('Qty').Value;
          FieldByName('QtyUnit').Value:=Self.CDS_MainSel.fieldbyname('QtyUnit').Value;
          FieldByName('MJId').Value:=Self.CDS_MainSel.fieldbyname('MJId').Value;
          FieldByName('APID').Value:=Self.CDS_MainSel.fieldbyname('APID').Value;
          FieldByName('CPType').Value:=Self.CDS_MainSel.fieldbyname('CPType').Value;
          FieldByName('PRTColor').Value:=Self.CDS_MainSel.fieldbyname('PRTColor').Value;
          FieldByName('AOrddefstr1').Value:=Self.CDS_MainSel.fieldbyname('AOrddefstr1').Value;
          FieldByName('SOrddefstr1').Value:=CDS_MainSel.fieldbyname('SOrddefstr1').Value;
          FieldByName('MJXH').Value:=Self.CDS_MainSel.fieldbyname('MJXH').Value;
          FieldByName('baoID').Value:=Self.CDS_MainSel.fieldbyname('baoID').Value;
          FieldByName('intBaoNO').Value:=Self.CDS_MainSel.fieldbyname('intBaoNO').Value;
          FieldByName('baoNo').Value:=Self.CDS_MainSel.fieldbyname('baoNo').Value;
          FieldByName('Filler').Value:=Self.CDS_MainSel.fieldbyname('Filler').Value;
          Post;
        end;
        CDS_MainSel.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_BanCP_CR SET CK_BanCP_CR.CRFlag=''待入库'',CK_BanCP_CR.CRType='''',CK_BanCP_CR.FillTime=''''');
      sql.Add(',CK_BanCP_CR.CRTime=null,CK_BanCP_CR.RKOrdId='''' ,CK_BanCP_CR.RKPlace='''' ,CK_BanCP_CR.Filler='''' ');
      sql.Add('FROM TBValueWhere A where A.wsql1=CK_BanCP_CR.MJID and CK_BanCP_CR.CRFlag=''入库'' and A.DName='+quotedstr(Trim(DName))+' and MainType='+quotedstr('撤销入库'));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJStr2=''未入库''where not exists(select MJID from CK_BanCP_CR X where X.MJID=WFB_MJJY.MJID and X.CRFlag=''入库'' and MJStr2<>''未入库'')');
      ExecSQL;
    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;
    MovePanel2.Visible:=False;
    MovePanel1.Visible:=True;
    if CDS_MainSel.IsEmpty=False then
      Edit1.Text:=IntToStr(Tv3.DataController.Summary.FooterSummaryValues[2])
    else
    Edit1.Text:='0';
    Edit2.Text:=Trim(CDS_Main.fieldbyname('MJId').AsString);
    if Trim(RKOrdID.Text)<>'' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select isnull(Count(*),0) ZPS from CK_BanCP_CR where RKOrdID='''+Trim(RKOrdID.Text)+'''');
        Open;
      end;
      RKOrdPS.Text:=Trim(ADOQueryCmd.fieldbyname('ZPS').AsString)+'匹';
    end;
  except
    MovePanel2.Visible:=False;
    CDS_MainSel.DisableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销失败！','提示',0);
  end;
end;

procedure TfrmCpRkSaoMNew.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_MainSel.IsEmpty=False then Exit;
  if CDS_Sub.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if Trim(CDS_Main.fieldbyname('SubId').AsString)=Trim(CDS_Sub.fieldbyname('SubId').AsString) then
      begin
        CDS_Main.Delete;
      end else
        Next;
    end;
  end;
  CDS_Main.EnableControls;
  CDS_Sub.Delete;
end;

procedure TfrmCpRkSaoMNew.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible:=False;
end;

procedure TfrmCpRkSaoMNew.Button5Click(Sender: TObject);
var
  maxno:String;
  CRID:Integer;
  MaxCkNo,MaxCkSubNo:String;
begin
  if CDS_Sub.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Trim(RKOrdID.Text)='' then
  begin
    Application.MessageBox('入库单号不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行入库操作吗？','提示',32+4)<>IDYES then exit;
  if Trim(BaoNo.Text)='' then
  begin
    if Trim(Edit3.Text)='√' then
    begin
      application.MessageBox('请输入包号！','提示信息',MB_ICONERROR);
      exit;
    end;
  end;
//  MovePanel2.Visible:=True;
//  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    CDS_MainSel.DisableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
      SQL.Add('and MainType='+quotedstr('手动入库') );
      ExecSQL;
    end;
    with CDS_Main do
    begin
      while CDS_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' insert into TBValueWhere(DName,MainType,Value1,Value2,Value3,wsql1) ');
          sql.Add('values('+quotedstr(Trim(DName))
          +','+quotedstr('手动入库')
          +','+quotedstr(Trim(trim(FormatDateTime('yyyy-MM-dd',CRTime.DateTime))))
          +','+quotedstr(Trim(trim(RKOrdId.Text)))
          +','+quotedstr(Trim(trim(RKPlace.Text)))
          +','+quotedstr(Trim(Trim(CDS_Main.fieldbyname('MJID').AsString)))+')');
          ExecSQL;
        end;
          
        with CDS_MainSel do
        begin
          Append;
          FieldByName('MainId').Value:=CDS_Main.fieldbyname('MainId').Value;
          FieldByName('SubId').Value:=Self.CDS_Main.fieldbyname('SubId').Value;
          FieldByName('OrderNo').Value:=Self.CDS_Main.fieldbyname('OrderNo').Value;
//          FieldByName('MJQty4').Value:=Self.ADOQueryTemp.fieldbyname('MJQty4').Value;
          FieldByName('KGQty').Value:=Self.CDS_Main.fieldbyname('KGQty').Value;
          FieldByName('Qty').Value:=Self.CDS_Main.fieldbyname('Qty').Value;
          FieldByName('QtyUnit').Value:=Self.CDS_Main.fieldbyname('QtyUnit').Value;
          FieldByName('MJId').Value:=Self.CDS_Main.fieldbyname('MJId').Value;
          FieldByName('CRId').Value:=Self.CDS_Main.fieldbyname('CRId').Value;
          FieldByName('APID').Value:=Self.CDS_Main.fieldbyname('APID').Value;
          FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
          FieldByName('BCID').Value:=Trim(MaxCkNo);
          FieldByName('PRTColor').Value:=Self.CDS_Main.fieldbyname('PRTColor').Value;
          FieldByName('MJXH').Value:=Self.CDS_Main.fieldbyname('MJXH').Value;
          IF trim(gangNo.Text)<>'' then
          FieldByName('AOrdDefstr1').Value:=trim(gangNo.Text)
          else
          FieldByName('AOrdDefstr1').Value:=CDS_Main.fieldbyname('AOrdDefstr1').Value;
          FieldByName('SOrddefstr1').Value:=Self.CDS_Main.fieldbyname('SOrddefstr1').Value;
          FieldByName('RKOrdID').Value:=Trim(RKOrdID.Text);
          FieldByName('BaoNo').Value:=Trim(CDS_Main.fieldbyname('BaoNo').AsString);
          FieldByName('intBaoNO').Value:=Trim(CDS_Main.fieldbyname('intBaoNO').AsString);
          FieldByName('BaoID').Value:=Trim(CDS_Main.fieldbyname('BaoID').AsString);
          FieldByName('Filler').Value:=Trim(CDS_Main.fieldbyname('Filler').AsString);
          Post;
        end;
        CDS_Main.Delete;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_BanCP_CR SET CK_BanCP_CR.CRFlag=''入库'',CK_BanCP_CR.CRType=''检验入库'',CK_BanCP_CR.FillTime=getdate()');
      sql.Add(',CK_BanCP_CR.CRTime=A.Value1,CK_BanCP_CR.RKOrdId=A.Value2 ,CK_BanCP_CR.RKPlace=A.Value3 ,CK_BanCP_CR.Filler='+quotedstr(Trim(DName)));
      sql.Add('FROM TBValueWhere A where A.wsql1=CK_BanCP_CR.MJID and CK_BanCP_CR.CRFlag=''待入库'' and A.DName='+quotedstr(Trim(DName))+' and MainType='+quotedstr('手动入库'));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJStr2=''已入库''');
      IF trim(gangNo.Text)<>'' then
      sql.Add(',MJStr4='+Quotedstr(trim(gangNo.Text)));
      sql.Add('where exists (select X.MJID from CK_BanCP_CR X where X.MJID=WFB_MJJY.MJID and X.RKOrdId='''+Trim(RKOrdId.Text)+''')');
      ExecSQL;
    end;
    CDS_Main.EnableControls;
    CDS_MainSel.EnableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select Mainid from CK_BanCP_CR where RKOrdId='''+Trim(RKOrdID.Text)+'''');
      SQL.Add(' group by MainId');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('不同订单不能使用相同的入库单号!','提示',0);
      Exit;
    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;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    Exit;
  except
    BaoID.Text:='';
    CDS_Main.EnableControls;
    MovePanel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('入库异常！','提示',0);
  end;
end;

procedure TfrmCpRkSaoMNew.N1Click(Sender: TObject);
begin
  //SelOKNo(CDS_Main,True);
  if CDS_Main.IsEmpty then Exit;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if Trim(ComboBox1.Text)='' then
      begin
        Edit;
        FieldByName('SSel').Value:=True;
        Post;
      end else
      begin
        if Trim(CDS_Main.fieldbyname('CPType').AsString)=Trim(ComboBox1.Text) then
        begin
          Edit;
          FieldByName('SSel').Value:=True;
          Post;
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
end;

procedure TfrmCpRkSaoMNew.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;

procedure TfrmCpRkSaoMNew.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(CDS_MainSel,True);
end;

procedure TfrmCpRkSaoMNew.MenuItem2Click(Sender: TObject);
begin
  SelOKNo(CDS_MainSel,False);
end;

procedure TfrmCpRkSaoMNew.Button6Click(Sender: TObject);
var
  maxno:string;
begin
  if GetLSNo(ADOQueryCmd,maxno,'9','CK_BanCP_CR',4,1)=False then
  begin
    Application.MessageBox('取入库单号失败!','提示',0);
    Exit;
  end;
  RKOrdID.Text:=Trim(maxno);
end;

procedure TfrmCpRkSaoMNew.RKPlaceBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KuWei';
      flagname:='库位';
      if ShowModal=1 then
      begin
        RKPlace.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCpRkSaoMNew.Tv3CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  RKOrdID.Text:=Trim(CDS_MainSel.fieldbyname('RKOrdID').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('select isnull(Count(*),0) ZPS from CK_BanCP_CR where RKOrdID='''+Trim(RKOrdID.Text)+'''');
    Open;
  end;
  RKOrdPS.Text:=Trim(ADOQueryCmd.fieldbyname('ZPS').AsString)+'匹';
end;

procedure TfrmCpRkSaoMNew.Button7Click(Sender: TObject);
var
  fPrintFile:String;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if Trim(RKOrdID.Text)='' then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\入库单标签.rmf';
  with ADOQueryPrt do
  begin
    Close;
    sql.Clear;
    sql.Add('select A1.CRTime,A1.RKOrdID,A.MJTypeOther as QtyUnit,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.Mprtspec,D.OrdPerson1,D.MPRTKuanNO,');
    sql.Add('PRTColor=DBO.F_Get_Order_SubStr(RKOrdID,''RKColor''),SOrddefstr1=DBO.F_Get_Order_SubStr(RKOrdID,''RKSOrddefstr1''),');
    sql.Add('SOrddefstr4=DBO.F_Get_Order_SubStr(RKOrdID,''RkSOrddefstr4''),PRtHX=DBO.F_Get_Order_SubStr(RKOrdID,''RKPRtHX''), ');
    sql.Add('PRTkuanNo=DBO.F_Get_Order_SubStr(RKOrdID,''RkPrtkuanno''),');
    sql.Add('MJStr4=DBO.F_Get_Order_SubStr(RKOrdID,''RKGangNO''), ');
    sql.Add('khConNo=(select top 1 khConNo from JYOrderCon_Main X where X.ConNO=D.conNO), ');
    sql.Add('Bsl=(select count(distinct baoNo) from CK_BanCP_CR X where X.RKOrdID=A1.RKOrdID), ');
    SQL.ADD('count(A.MJID) as JSl,sum(A.MJMaoZ) MJMAOZ,sum(MJQty2) as MJQty2,sum(MJQty3) as MJQty3,sum(MJQty4) as MJQty4,SUM(A.MJLen)as MJLen');
    sql.Add('from CK_BanCP_CR A1  inner join WFB_MJJY A on A.MJID=A1.MJID ');
    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 A1.RKOrdID='''+Trim(RKOrdID.Text)+'''');
    SQL.ADD(' group by A1.CRTime,A1.RKOrdID,A.MJTypeOther,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.Mprtspec,D.OrdPerson1,D.MPRTKuanNO');
    //ShowMessage(ADOQueryPrt.SQL.Text);
    Open;
  end;
  try
      Moudle:=LoadLibrary('MakeQRBarcode.dll');
      @Makebar:=GetProcAddress(Moudle,'Make');
      @Mixtext:=GetProcAddress(Moudle,'MixText');
      Txt:=Trim(ADOQueryPrt.fieldbyname('RKOrdID').AsString);
      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 FileExists(fPrintFile) then
  begin
    //CDS_MainSel.Locate('RKOrdID',Trim(RKOrdID.Text),[]);
    //RMVariables['RKOrdID']:=Trim(CDS_MainSel.fieldbyname('RKOrdID').AsString);
    //RMVariables['OrderNo']:=Trim(CDS_MainSel.fieldbyname('OrderNo').AsString);
    //RMVariables['PS']:=Trim(RKOrdPS.Text);
    //RMVariables['QtyUnit']:=Trim(CDS_MainSel.fieldbyname('QtyUnit').AsString);
    RMVariables['QRBARCODE']:=fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\入库单标签.rmf'),'提示',0);
  end;
end;

procedure TfrmCpRkSaoMNew.Button8Click(Sender: TObject);
var
  fPrintFile:String;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if Trim(Edit3.Text)='' then Exit;
  if Trim(BaoNo.Text)='' then
  begin
    BaoNo.Text:='';
    Exit;
  end else
  begin
    BaoNo.Text:='';
  //  BaoNo.Text:=IntToStr(StrToInt(BaoNo.Text)+1);
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\包标签.rmf';
  with ADOQueryPrt do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.BaoId,A.BaoNo,A.QtyUnit,B.AOrdDefStr1,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.OrdPerson1,C.PRTColor,C.SOrddefstr1,C.SOrddefstr4,C.PRtHX,');
    sql.Add(' khConNo=(select top 1 khConNo from JYOrderCon_Main X where X.ConNO=D.conNO), ');
    sql.Add(' Count(A.BaoId) JSl,Sum(WM.MJQty4) MJQty4,Sum(WM.MJMaoZ) MJMaoZ,Sum(WM.MJLen) MJLen');
    sql.Add(' from CK_BanCP_CR A inner join WFB_MJJY WM on A.MJID=WM.MJID');
    sql.Add(' inner join JYOrder_Sub_AnPai B on A.APID=B.APID');
    sql.Add(' inner join JYOrder_Sub C on B.SubID=C.SubID');
    sql.Add(' inner join JYOrder_Main D on C.MainID=D.MainID');
    sql.Add(' where BaoId='''+Trim(FBaoId)+''' and A.CRflag=''入库'' ');
    sql.Add(' Group by A.BaoId,A.BaoNo,A.QtyUnit,B.AOrdDefStr1,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.OrdPerson1,C.PRTColor,C.SOrddefstr1,C.SOrddefstr4,C.PRtHX');
    Open;
  end;
  if ADOQueryPrt.RecordCount>1 then
  begin
    Application.MessageBox('打包错误，请撤销入库!','提示',0);
    Exit;
  end;
  try
      Moudle:=LoadLibrary('MakeQRBarcode.dll');
      @Makebar:=GetProcAddress(Moudle,'Make');
      @Mixtext:=GetProcAddress(Moudle,'MixText');
      Txt:=Trim(ADOQueryPrt.fieldbyname('BaoId').AsString);
      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 FileExists(fPrintFile) then
  begin
    //RMVariables['RKOrdID']:=Trim(CDS_MainSel.fieldbyname('RKOrdID').AsString);
    //RMVariables['OrderNo']:=Trim(CDS_MainSel.fieldbyname('OrderNo').AsString);
    //RMVariables['PS']:=Trim(RKOrdPS.Text);
    //RMVariables['QtyUnit']:=Trim(CDS_MainSel.fieldbyname('QtyUnit').AsString);
    RMVariables['QRBARCODE']:=fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
   // rm1.PrintReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\包标签.rmf'),'提示',0);
  end;
  CDS_MainSel.EmptyDataSet;
  FBaoId:='';
end;

procedure TfrmCpRkSaoMNew.Edit3Click(Sender: TObject);
begin
  if Trim(Edit3.Text)='√' then
  begin
    Edit3.Text:='';
    baoNo.Text:='';
    fbaoID:='';
  end else
  begin
    Edit3.Text:='√';
    baoNo.Text:='';
    fbaoID:='';
  end;
end;

procedure TfrmCpRkSaoMNew.BaoKeyPress(Sender: TObject; var Key: Char);
var
  maxno:String;
  CRID:Integer;
  MaxCkNo,MaxCkSubNo:String;
begin
  if Key=#13 then
  begin
    Label12.Caption:='';
    if CDS_Sub.IsEmpty then
    begin
      Bao.Text:='';
      Application.MessageBox('未选单不能扫描出库！','提示',0);
      Exit;
    end;
    if Trim(RKOrdID.Text)='' then
    begin
      Application.MessageBox('入库单号不能为空!','提示',0);
      Exit;
    end;
    IF trim(Bao.text)='' then
    begin
      Application.MessageBox('包号不能为空！','提示',0);
      Exit;
    end;
    if CDS_MainSel.Locate('baoID',Trim(Bao.Text),[]) then
    begin
      MovePanel1.Visible:=True;
      Edit2.Text:=Trim(Bao.Text);
      Label12.Caption:='提醒：此包重复扫描！';
      Bao.Text:='';
      Exit;
    end;
    if CDS_Main.Locate('baoID',Trim(Bao.Text),[])=False then
    begin
      MovePanel1.Visible:=True;
      Edit2.Text:=Trim(Bao.Text);
      Label12.Caption:='提醒：此卷不包含在待入库的卷数据中！';
      Bao.Text:='';
      Exit;
    end;

    try
      ADOQueryCmd.Connection.BeginTrans;
      with CDS_Main do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPDATE CK_BanCP_CR SET CRFlag=''入库'',CRType=''检验入库'',FillTime=getdate() ');
          sql.add(',CRTime='''+trim(FormatDateTime('yyyy-MM-dd',CRTime.DateTime))+'''');
          sql.Add(',RKOrdId='''+trim(RKOrdId.Text)+'''');
          sql.Add(',Filler='''+trim(DName)+'''');
          sql.Add('FROM CK_BanCP_CR A ');
          sql.Add('where A.BaoId='''+Trim(Bao.Text)+'''');
          sql.Add('and A.CRFlag=''待入库'' ');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update WFB_MJJY  Set MJStr2=''已入库'' ');
          IF trim(gangNo.Text)<>'' then
          sql.Add(',MJStr4='+Quotedstr(trim(gangNo.Text)));
          sql.Add('where BaoId='''+Trim(Bao.Text)+'''');
          ExecSQL;
        end;
        DisableControls;
        while CDS_Main.Locate('BaoID',Trim(Bao.Text),[]) do
        begin
          with CDS_MainSel do
          begin
            Append;
            FieldByName('MainId').Value:=CDS_Main.fieldbyname('MainId').Value;
            FieldByName('SubId').Value:=Self.CDS_Main.fieldbyname('SubId').Value;
            FieldByName('OrderNo').Value:=Self.CDS_Main.fieldbyname('OrderNo').Value;
            FieldByName('MJQty4').Value:=Self.CDS_Main.fieldbyname('MJQty4').Value;
            FieldByName('KGQty').Value:=Self.CDS_Main.fieldbyname('KGQty').Value;
            FieldByName('Qty').Value:=Self.CDS_Main.fieldbyname('Qty').Value;
            FieldByName('QtyUnit').Value:=Self.CDS_Main.fieldbyname('QtyUnit').Value;
            FieldByName('MJId').Value:=Self.CDS_Main.fieldbyname('MJId').Value;
            FieldByName('CRId').Value:=Self.CDS_Main.fieldbyname('CRID').Value;
            FieldByName('APID').Value:=Self.CDS_Main.fieldbyname('APID').Value;
            FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('RKOrdID').Value:=Trim(RKOrdID.Text);
            FieldByName('PRTColor').Value:=CDS_Main.fieldbyname('PRTColor').Value;
            FieldByName('MJXH').Value:=CDS_Main.fieldbyname('MJXH').Value;
            IF trim(gangNo.Text)<>'' then
            FieldByName('AOrdDefstr1').Value:=trim(gangNo.Text)
            else
            FieldByName('AOrdDefstr1').Value:=CDS_Main.fieldbyname('AOrdDefstr1').Value;
            FieldByName('SOrddefstr1').Value:=CDS_Main.fieldbyname('SOrddefstr1').Value;
            FieldByName('RKOrdID').Value:=Trim(RKOrdID.Text);
            FieldByName('BaoNo').Value:=Trim(CDS_Main.fieldbyname('BaoNo').AsString);
            FieldByName('intBaoNO').Value:=Trim(CDS_Main.fieldbyname('intBaoNO').AsString);
            FieldByName('BaoID').Value:=Trim(CDS_Main.fieldbyname('BaoID').AsString);
            FieldByName('Filler').Value:=Trim(CDS_Main.fieldbyname('Filler').AsString);
            Post;
          end;
          CDS_Main.Delete;
        end;
        CDS_Main.EnableControls;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select Mainid from CK_BanCP_CR where RKOrdId='''+Trim(RKOrdID.Text)+'''');
        SQL.Add(' group by MainId');
        Open;
      end;
      if ADOQueryCmd.RecordCount>1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('不同订单不能使用相同的入库单号!','提示',0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Update_Order_JD_ByOther @MainType=''单包入库''');
        sql.Add(',@BaoId='+quotedstr(Trim(Bao.Text)));
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      MovePanel1.Visible:=True;
      if CDS_MainSel.IsEmpty=False then
        Edit1.Text:=IntToStr(Tv3.DataController.Summary.FooterSummaryValues[2])
      else
        Edit1.Text:='0';
      Edit2.Text:=Trim(Bao.Text);
      Bao.Text:='';
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select isnull(Count(*),0) ZPS from CK_BanCP_CR where RKOrdID='''+Trim(RKOrdID.Text)+'''');
        Open;
      end;
      RKOrdPS.Text:=Trim(ADOQueryCmd.fieldbyname('ZPS').AsString)+'匹';
      Exit;
    except
      Bao.Text:='';
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('入库异常！','提示',0);
    end;
  end;
end;


procedure TfrmCpRkSaoMNew.N3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if Trim(Fieldbyname('baoNo').AsString)<>'' then
      begin
        Edit;
        FieldByName('SSel').Value:=True;
        Post;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
end;

end.
