unit U_CpRkSaoMNewDB;

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;

type
  TfrmCpRkSaoMNewDB = 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;
    v3Column1: 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;
    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);
  private
    { Private declarations }
    procedure InitGrid();
    procedure InitSubGrid();
    procedure SavedataCK();
  public
    { Public declarations }
  end;

var
  frmCpRkSaoMNewDB: TfrmCpRkSaoMNewDB;

implementation
uses
U_DataLink,U_Fun,U_OrderSelRK,U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmCpRkSaoMNewDB.FormDestroy(Sender: TObject);
begin
  frmCpRkSaoMNewDB:=nil;
end;
procedure TfrmCpRkSaoMNewDB.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,OrderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId) ');
      SQL.Add(',PRTColor=(select PRTColor 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('from CK_BanCP_CR A');
      sql.add('where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,OrderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId) ');
      SQL.Add(',PRTColor=(select PRTColor 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('from CK_BanCP_CR A');
      sql.add('where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_MainSel);
    SInitCDSData20(ADOQueryMain,CDS_MainSel);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

procedure TfrmCpRkSaoMNewDB.BaoIDKeyPress(Sender: TObject; var Key: Char);
var
  maxno:String;
  CRID:Integer;
  MaxCkNo,MaxCkSubNo:String;
begin
  if Key=#13 then
  begin
    if CDS_Sub.IsEmpty then
    begin
      BaoID.Text:='';
      Application.MessageBox('未选单不能扫描出库！','提示',0);
      Exit;
    end;
    if CDS_Main.Locate('MJId',Trim(BaoID.Text),[])=False then
    begin
      BaoID.Text:='';
      //Application.MessageBox('此卷不包含在待入库的卷数据中！','提示',0);
      Exit;
    end;
    if Trim(RKOrdID.Text)='' then
    begin
      Application.MessageBox('入库单号不能为空!','提示',0);
      Exit;
    end;
    if Trim(RKPlace.Text)='' then
    begin
      Application.MessageBox('库位不能为空!','提示',0);
      Exit;
    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_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);
          FieldByName('RKOrdId').Value:=Trim(RKOrdID.Text);
          FieldByName('BaoNo').Value:=Trim(BaoNo.Text);
          FieldByName('RKPlace').Value:=Trim(RKPlace.Text);
          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;
      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;
      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('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:=IntToStr(CRID);
        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;
        FieldByName('AOrdDefstr1').Value:=CDS_Main.fieldbyname('AOrdDefstr1').Value;
        FieldByName('RKOrdID').Value:=Trim(RKOrdID.Text);
        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 TfrmCpRkSaoMNewDB.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 TfrmCpRkSaoMNewDB.Button2Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('订单选择',Tv2,'成品仓库');
  WriteCxGrid('成品出库',Tv1,'成品仓库');
  WriteCxGrid('成品出库Sels',Tv3,'成品仓库');
end;

procedure TfrmCpRkSaoMNewDB.Button3Click(Sender: TObject);
begin
  {if CDS_Main.IsEmpty=False then
  begin
    Application.MessageBox('已扫描不能更改单号！','提示',0);
    Exit;
  end;}
  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(',AOrdDefStr1=(select  AOrdDefStr1 from JYOrder_Sub_AnPai JS where JS.APId=A.APId),A.* ');
                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;
              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('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('MJXH').Value:=Self.ADOQueryTemp.fieldbyname('MJXH').Value;
                    Post;
                  end;
                  Next;
                end;
              end;
              Self.ADOQueryTemp.EnableControls;
            end;
            CDS_Sub.Edit;
            CDS_Sub.FieldByName('SFlag').Value:='2';
            CDS_Sub.Post;
            Next;
          end;
        end;
        CDS_Sub.EnableControls;
      end;
    end;
  finally
    frmOrderSelRK.Free;
  end;
end;

procedure TfrmCpRkSaoMNewDB.InitSubGrid();
begin
  try
    ADOQuerySub.DisableControls;
    with ADOQuerySub do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*');
      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 TfrmCpRkSaoMNewDB.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;
    with CDS_MainSel do
    begin
      First;
      while not Eof do
      begin
        if CDS_MainSel.FieldByName('SSel').AsBoolean=True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete CK_BanCP_CR where BCID='''+Trim(CDS_MainSel.fieldbyname('BCID').AsString)+'''');
            sql.Add('delete CK_BanCP_KC where CRID='+Trim(CDS_MainSel.fieldbyname('CRID').AsString));
            sql.Add('Update WFB_MJJY Set MJStr2=''未入库'' where MJID='''+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('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('MJXH').Value:=Self.CDS_MainSel.fieldbyname('MJXH').Value;
            Post;
          end;
          CDS_MainSel.Delete;
        end else
          Next;
      end;
    end;
    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    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 TfrmCpRkSaoMNewDB.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 TfrmCpRkSaoMNewDB.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible:=False;
end;

procedure TfrmCpRkSaoMNewDB.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 Trim(RKPlace.Text)='' 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_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if CDS_Main.FieldByName('SSel').AsBoolean=True then
        begin
          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
            MovePanel2.Visible:=False;
            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:=FormatDateTime('yyyy-MM-dd',CRTime.DateTime);
            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);
            FieldByName('RKOrdId').Value:=Trim(RKOrdID.Text);
            FieldByName('BaoNo').Value:=Trim(BaoNo.Text);
            FieldByName('RKPlace').Value:=Trim(RKPlace.Text);
            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;
          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('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:=IntToStr(CRID);
            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;
            FieldByName('AOrdDefstr1').Value:=CDS_Main.fieldbyname('AOrdDefstr1').Value;
            FieldByName('RKOrdID').Value:=Trim(RKOrdID.Text);
            Post;
          end;
          CDS_Main.Delete;
        end else
          CDS_Main.Next;
      end;
    end;
    CDS_Main.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;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    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:='';
    CDS_Main.EnableControls;
    MovePanel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('入库异常！','提示',0);
  end;
end;

procedure TfrmCpRkSaoMNewDB.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

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

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

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

procedure TfrmCpRkSaoMNewDB.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 TfrmCpRkSaoMNewDB.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 TfrmCpRkSaoMNewDB.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 TfrmCpRkSaoMNewDB.Button7Click(Sender: TObject);
var
  fPrintFile:String;
begin
  if Trim(RKOrdPS.Text)='' then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\入库单标签.rmf' ;
  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);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\入库单标签.rmf'),'提示',0);
  end;
end;

end.
