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, MMSystem, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxSkinsCore, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter;

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;
    v3Column1: TcxGridDBColumn;
    Button1: TButton;
    cxGridPopupMenu4: TcxGridPopupMenu;
    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;
    v3Column3: 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;
    v3BaoNo: TcxGridDBColumn;
    ComboBox1: TComboBox;
    Edit3: TEdit;
    Label8: TLabel;
    v1BaoNo: TcxGridDBColumn;
    v1baoID: TcxGridDBColumn;
    v3Baoid: TcxGridDBColumn;
    Label9: TLabel;
    MJID: TEdit;
    v1mjid: TcxGridDBColumn;
    v3MJID: TcxGridDBColumn;
    Label12: TLabel;
    v2Column1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    MJType: TComboBox;
    Label2: TLabel;
    v1Column8: TcxGridDBColumn;
    Edit1: TEdit;
    Label3: TLabel;
    Label10: TLabel;
    Tv2Column1: 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 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 MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure MJTypeChange(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
  private
    { Private declarations }
    FBaoId: string;
    procedure InitGrid();
    procedure InitSubGrid();
    procedure SavedataCK();
    procedure JSbaoNum();
    procedure XDJSbaoNum();
  public
    { Public declarations }
  end;

var
  frmCpRkSaoMNew: TfrmCpRkSaoMNew;

implementation

uses
  U_DataLink, U_Fun, U_OrderSelRK, U_ZDYHelp;

{$R *.dfm}
procedure TfrmCpRkSaoMNew.XDJSbaoNum();
var
  i: integer;
  baoID: string;
  strlist: Tstringlist;
begin
  i := 0;
  baoID := '';
  if CDS_Main.IsEmpty then
  begin
    Label10.Caption := '选单包数：0';
    exit;
  end;
  strlist := Tstringlist.Create;
  try
    with CDS_Main do
    begin
      DisableControls;
      first;
      while not eof do
      begin

        if (trim(fieldbyname('BaoID').AsString) <> '') then
        begin
          if strlist.IndexOf(trim(fieldbyname('BaoID').AsString)) < 0 then
          begin
            strlist.Add(trim(fieldbyname('BaoID').AsString));
          end;
        end;
     {   IF (trim(fieldbyname('BaoID').AsString)<>trim(baoID)) and (trim(fieldbyname('BaoID').AsString)<>'') then
        begin
          i:=i+1;
          baoID:=trim(fieldbyname('BaoID').AsString);
        end;  }
        Next;
      end;
      EnableControls;
    end;
    Label10.Caption := '选单包数：' + inttostr(strlist.Count);
  finally
    strlist.Free;
  end;
end;

procedure TfrmCpRkSaoMNew.JSbaoNum();
var
  i: integer;
  baoID: string;
  strlist: Tstringlist;
begin
  i := 0;
  baoID := '';
  if CDS_MainSel.IsEmpty then
  begin
    Label12.Caption := '入库包数：0';
    exit;
  end;
  strlist := Tstringlist.Create;
  try
    with CDS_MainSel do
    begin
      DisableControls;
      first;
      while not eof do
      begin

        if (trim(fieldbyname('BaoID').AsString) <> '') then
        begin
          if strlist.IndexOf(trim(fieldbyname('BaoID').AsString)) < 0 then
          begin
            strlist.Add(trim(fieldbyname('BaoID').AsString));
          end;
        end;
     {   IF (trim(fieldbyname('BaoID').AsString)<>trim(baoID)) and (trim(fieldbyname('BaoID').AsString)<>'') then
        begin
          i:=i+1;
          baoID:=trim(fieldbyname('BaoID').AsString);
        end;  }
        Next;
      end;
      EnableControls;
    end;
    Label12.Caption := '入库包数：' + inttostr(strlist.Count);
  finally
    strlist.Free;
  end;
end;

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.*,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('from CK_BanCP_CR A');
      sql.add('where 1=2');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

    SCreateCDS20(ADOQueryMain, CDS_MainSel);
    SInitCDSData20(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
    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 Trim(RKPlace.Text)='' then
    begin
      Application.MessageBox('库位不能为空!','提示',0);
      Exit;
    end;  }
    {if Trim(BaoNo.Text)='' then
    begin
      if Trim(Edit3.Text)='√' then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select BaoInt=max(Cast(BaoNo as int))+1 from CK_BanCP_CR where Mainid='''+Trim(CDS_Main.fieldbyname('Mainid').AsString)+'''');
          Open;
        end;
        BaoNo.Text:=ADOQueryTemp.fieldbyname('BaoInt').AsString;
      end;
    end;}
    {with ADOQueryMain do
    begin
      close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY where isnull(MJStr2,'''')<>''已入库'' and ');
      sql.add('(Baoid='''+trim(Baoid.Text)+''' or MJID='''+trim(Baoid.Text)+''')');
      open;
      if isempty then
      begin
        BaoID.Text:='';
        application.MessageBox('此卷已入库或没有此单号！','提示');
        exit;
      end;
    end;}
    if CDS_Main.Locate('MJId', Trim(baoID.Text), []) = False then
    begin
      baoID.Text := '';
 //     Application.MessageBox('此卷不包含在待入库的卷数据中！','提示',0);
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\扫描错误.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\扫描错误.wav'), 0, SND_ASYNC);
      Exit;
    end;
    CDS_Main.Locate('MJId', Trim(baoID.Text), []);

    try
      ADOQueryCmd.Connection.BeginTrans;
    {with CDS_Main do
    begin
     first;
     while not eof do
     begin
      IF CDS_Main.Locate('BaoId',Trim(BaoID.Text),[])=True then
      begin}
      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 Trim(FBaoId)='' then
        begin
          if Trim(CDS_Sub.fieldbyname('BaoNo').AsString)<>'' then
          begin
            if GetLSNo(ADOQueryCmd,FBaoId,'9','CK_BanCP_CR',3,1)=False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取成品仓库包最大号失败！','提示',0);
              Exit;
            end;
          end;
        end else
        begin
          if Trim(BaoNo.Text)='' then
          begin
            FBaoId:='';
          end;
        end;}
        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(CDS_Main.fieldbyname('BaoNo').AsString);
          FieldByName('BaoID').Value := Trim(CDS_Main.fieldbyname('BaoID').AsString);
          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;
          FieldByName('BaoNo').Value := Trim(CDS_Main.fieldbyname('BaoNo').AsString);
          FieldByName('BaoID').Value := Trim(CDS_Main.fieldbyname('BaoID').AsString);
          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('SOrddefstr1').Value := CDS_Main.fieldbyname('SOrddefstr1').Value;
        FieldByName('MJXH').Value := CDS_Main.fieldbyname('MJXH').Value;
        FieldByName('AOrdDefstr1').Value := CDS_Main.fieldbyname('AOrdDefstr1').Value;
        FieldByName('BaoNo').Value := Trim(CDS_Main.fieldbyname('BaoNo').asstring);
        FieldByName('BaoID').Value := Trim(CDS_Main.fieldbyname('BaoID').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 := '';
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\正确.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\正确.wav'), 0, SND_ASYNC);
      JSbaoNum();
      Exit;
    except
      baoID.Text := '';
      ADOQueryCmd.Connection.RollbackTrans;
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\保存失败.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\保存失败.wav'), 0, SND_ASYNC);
  //    Application.MessageBox('入库异常！','提示',0);
    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('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('BaoNo').Value := Trim(CDS_Main.fieldbyname('BaoNo').AsString);
    FieldByName('BaoID').Value := Trim(CDS_Main.fieldbyname('BaoID').AsString);
    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;
    FieldByName('BaoNo').Value := Trim(CDS_Main.fieldbyname('BaoNo').AsString);
    FieldByName('BaoID').Value := Trim(CDS_Main.fieldbyname('BaoID').AsString);
    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
  Close;
  WriteCxGrid('订单选择', Tv2, '成品仓库');
  WriteCxGrid('成品出库', Tv1, '成品仓库');
  WriteCxGrid('成品出库Sels', Tv3, '成品仓库');
end;

procedure TfrmCpRkSaoMNew.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('PRTCodeName').Value := Trim(CDS_OrderSel.fieldbyname('PRTCodeName').AsString);
                  FieldByName('Sorddefstr5').Value := Trim(CDS_OrderSel.fieldbyname('Sorddefstr5').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('SOrddefstr1').Value := Trim(CDS_OrderSel.fieldbyname('SOrddefstr1').AsString);
                  FieldByName('PRTMF').Value := CDS_OrderSel.fieldbyname('PRTMF').AsString;
                  FieldByName('PRTKZ').Value := CDS_OrderSel.fieldbyname('PRTKZ').AsString;
                  //FieldByName('BaoNo').Value:=Trim(CDS_OrderSel.fieldbyname('BaoNo').AsString);
                  //FieldByName('BaoID').Value:=Trim(CDS_OrderSel.fieldbyname('BaoID').AsString);
                  Post;
                end;
              end;
            end;
            Next;
          end;
        end;
        CDS_OrderSel.EnableControls;
        CDS_Sub.DisableControls;
        CDS_Main.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 where SubId=A.SubId)');
                sql.Add(',AOrdDefStr1=A.MJStr4,A.* ');
                sql.Add(' from WFB_MJJY A ');
                sql.Add(' where  isnull(A.CKNO,'''')='''' and (MJLen>0 OR MJMaoZ>0) and A.Subid=''' + trim(CDS_Sub.fieldbyname('subId').AsString) + '''');
                Open; //isnull(A.MJStr2,'''')=''未入库''
              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('SOrddefstr1').Value := Self.ADOQueryTemp.fieldbyname('SOrddefstr1').Value;
                    FieldByName('AOrddefstr1').Value := Self.ADOQueryTemp.fieldbyname('AOrddefstr1').Value;
                    FieldByName('MJXH').Value := Self.ADOQueryTemp.fieldbyname('MJXH').Value;
                    FieldByName('baoNo').Value := Self.ADOQueryTemp.fieldbyname('baoNo').Value;
                    FieldByName('baoID').Value := Self.ADOQueryTemp.fieldbyname('baoID').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;
        CDS_Main.EnableControls;
      end;
    end;
  finally
    frmOrderSelRK.Free;
  end;
  XDJSbaoNum();
  Button6.Click;

end;

procedure TfrmCpRkSaoMNew.InitSubGrid();
begin
  try
    ADOQuerySub.DisableControls;
    with ADOQuerySub do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,C.*,Cast('''' as varchar(20))');
      sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.Mainid');
      sql.add(' inner join WFB_MJJY C on C.Subid=B.Subid ');
      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;
    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('SOrddefstr1').Value := CDS_MainSel.fieldbyname('SOrddefstr1').Value;
            FieldByName('AOrddefstr1').Value := Self.CDS_MainSel.fieldbyname('AOrddefstr1').Value;
            FieldByName('MJXH').Value := Self.CDS_MainSel.fieldbyname('MJXH').Value;
            FieldByName('BaoNo').Value := trim(Self.CDS_MainSel.fieldbyname('BaoNo').AsString);
            FieldByName('BaoID').Value := trim(Self.CDS_MainSel.fieldbyname('BaoID').AsString);
            Post;
          end;
          CDS_MainSel.Delete;
        end
        else
          Next;
      end;
    end;
    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    CDS_MainSel.DisableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销成功！', '提示', 0);
  end;
  MovePanel2.Visible := False;
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('BaoId').AsString) = Trim(CDS_Sub.fieldbyname('baoId').AsString) then
      begin
        CDS_Main.Delete;
      end
      else
        Next;
    end;
  end;
  CDS_Main.EnableControls;
  CDS_Sub.Delete;
end;

procedure TfrmCpRkSaoMNew.Button5Click(Sender: TObject);
var
  maxno, sqlstr: string;
  CRID, i, SJ: 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 GetLSNo(ADOQueryCmd, maxno, '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('Update CK_BanCP_CRID Set CRID=CRID+1');
    sql.Add('select CRID from CK_BanCP_CRID');
    Open;
  end;
  CRID := ADOQueryCmd.fieldbyname('CRID').AsInteger;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    CDS_MainSel.DisableControls;
    sqlstr := '';
    i := 1;
    with CDS_Main do
    begin
      first;
      while not Eof do
      begin
        if CDS_Main.FieldByName('SSel').AsBoolean then
        begin
          MaxCkNo := trim(maxno) + inttostr(i);
          CRID := CRID + 1;
          sqlstr := sqlstr + 'insert into CK_BanCP_CR(mainID,subID,CRType,CRFlag,CRTime,CRID,BCID,RKOrdId,APID,MJID,' + 'CPType,Qty,QtyUnit,KGQty,Filler,FillTime,BaoNo,BaoID,RKPlace,CRNote) ';
          sqlstr := sqlstr + ' values( ';
          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('mainID').AsString) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('subID').AsString) + ''', ';
          sqlstr := sqlstr + ' ''检验入库'', ';
          sqlstr := sqlstr + ' ''入库'', ';
          sqlstr := sqlstr + ' ''' + FormatDateTime('yyyy-MM-dd', CRTime.DateTime) + ''', ';
          sqlstr := sqlstr + ' ''' + IntToStr(CRID) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(MaxCkNo) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(RKOrdID.Text) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('APID').AsString) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('CPType').AsString) + ''', ';
          sqlstr := sqlstr + ' ''' + floattostr(CDS_Main.fieldbyname('Qty').AsFloat) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('QtyUnit').AsString) + ''', ';
          sqlstr := sqlstr + ' ''' + floattostr(CDS_Main.fieldbyname('KGQty').AsFloat) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(DName) + ''', ';
          sqlstr := sqlstr + ' getdate(), ';
          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('BaoNo').AsString) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('BaoID').AsString) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('RKPlace').AsString) + ''', ';
          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('CRnote').AsString) + ''' ';
          sqlstr := sqlstr + ' ) ';

//          sqlstr := sqlstr + 'insert into CK_BanCP_KC(CRID,BCID,MJID,' + 'KCKGQty,KCQty,KCQtyUnit,BaoNo,BaoID) ';
//          sqlstr := sqlstr + ' values( ';
//          sqlstr := sqlstr + ' ''' + IntToStr(CRID) + ''', ';
//          sqlstr := sqlstr + ' ''' + Trim(MaxCkNo) + ''', ';
//          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + ''', ';
//          sqlstr := sqlstr + ' ''' + floattostr(CDS_Main.fieldbyname('KGQty').AsFloat) + ''', ';
//          sqlstr := sqlstr + ' ''' + floattostr(CDS_Main.fieldbyname('Qty').AsFloat) + ''', ';
//          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('QtyUnit').AsString) + ''', ';
//          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('BaoNo').AsString) + ''', ';
//          sqlstr := sqlstr + ' ''' + Trim(CDS_Main.fieldbyname('BaoID').AsString) + ''' ';
//          sqlstr := sqlstr + ' ) ';
//
//          sqlstr := sqlstr + ' Update WFB_MJJY Set MJStr2=''已入库'' where MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + ''' ';
          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('SOrddefstr1').Value := Self.CDS_Main.fieldbyname('SOrddefstr1').Value;
            FieldByName('MJXH').Value := Self.CDS_Main.fieldbyname('MJXH').Value;
            FieldByName('AOrdDefstr1').Value := CDS_Main.fieldbyname('AOrdDefstr1').Value;
            FieldByName('RKOrdID').Value := Trim(RKOrdID.Text);
            FieldByName('BaoNo').Value := Trim(CDS_Main.fieldbyname('BaoNo').asstring);
            FieldByName('BaoID').Value := Trim(CDS_Main.fieldbyname('BaoID').asstring);
            Post;
          end;
        end;
        i := i + 1;
        CDS_Main.Delete;

      end;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add(sqlstr);
//      showmessage(sql.Text);
      execsql;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('Update CK_BanCP_CRID Set CRID=(select max(CRID) from CK_BanCP_CR) ');
      execsql;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add(' insert into CK_BanCP_KC(CRID,BCID,MJID,KCKGQty,KCQty,KCQtyUnit,baono,baoid) ');
      sql.Add(' select CRID,BCID,MJID,KGQty,Qty,QtyUnit,baono,baoid');
      sql.Add(' from CK_BanCP_CR ');
      sql.Add(' where RKOrdId=''' + Trim(RKOrdID.Text) + ''' and  not  EXISTS(select MJID from CK_BanCP_KC X where X.MJID=CK_BanCP_CR.MJID ) ');
//      ShowMessage(SQL.text);
      execsql;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' where exists(select MJID from CK_BanCP_CR X where X.MJID=WFB_MJJY.MJID and X.RKOrdId=''' + Trim(RKOrdID.Text) + ''') ');
      execsql;
    end;

    CDS_MainSel.EnableControls;
    CDS_Main.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible := False;
    JSbaoNum();
    Exit;
  except
    baoID.Text := '';
    CDS_Main.EnableControls;
    MovePanel2.Visible := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('入库异常！', '提示', 0);
  end;
end;
//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;
//
//  MovePanel2.Visible:=True;
//  MovePanel2.Refresh;
//  try
//    ADOQueryCmd.Connection.BeginTrans;
//    CDS_Main.DisableControls;
//    CDS_MainSel.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('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(CDS_Main.fieldbyname('BaoNo').asstring);
//            FieldByName('BaoID').Value:=Trim(CDS_Main.fieldbyname('BaoID').asstring);
//            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;
//            FieldByName('BaoNo').Value:=Trim(CDS_Main.fieldbyname('BaoNo').asstring);
//            FieldByName('BaoID').Value:=Trim(CDS_Main.fieldbyname('BaoID').asstring);
//            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('SOrddefstr1').Value:=Self.CDS_Main.fieldbyname('SOrddefstr1').Value;
//            FieldByName('MJXH').Value:=Self.CDS_Main.fieldbyname('MJXH').Value;
//            FieldByName('AOrdDefstr1').Value:=CDS_Main.fieldbyname('AOrdDefstr1').Value;
//            FieldByName('RKOrdID').Value:=Trim(RKOrdID.Text);
//            FieldByName('BaoNo').Value:=Trim(CDS_Main.fieldbyname('BaoNo').asstring);
//            FieldByName('BaoID').Value:=Trim(CDS_Main.fieldbyname('BaoID').asstring);
//            Post;
//          end;
//          CDS_Main.Delete;
//          application.ProcessMessages;
//        end else
//          CDS_Main.Next;
//      end;
//    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;
//    ADOQueryCmd.Connection.CommitTrans;
//    MovePanel2.Visible:=False;
//    JSbaoNum();
//    Exit;
//  except
//    BaoID.Text:='';
//    CDS_Main.EnableControls;
//    MovePanel2.Visible:=False;
//    ADOQueryCmd.Connection.RollbackTrans;
//    Application.MessageBox('入库异常！','提示',0);
//  end;
//end;

procedure TfrmCpRkSaoMNew.N1Click(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(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, 'RK', '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;
begin
  //if Trim(RKOrdPS.Text)='' then Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\入库单标签.rmf';
  with ADOQueryPrt do
  begin
    Close;
    sql.Clear;
    sql.Add('select AAA.* ,');
    SQL.Add('BSL=(Select Count(*) from (select BaoId from CK_BanCP_CR CR where CR.RKOrdID=AAA.RKOrdID and CR.Subid=AAA.Subid and isnull(BaoNo,'''')<>'''' group by BaoId) SS),');
    SQL.Add('JSL=(select Count(*) from CK_BanCP_CR CR where CR.RKOrdID=AAA.RKOrdID and CR.Subid=AAA.Subid)');
    sql.Add('from(select RKOrdID,A.QtyUnit,D.OrderNo,C.PRTColor,C.SOrddefstr4,Sum(WM.MJQty4) MJJingZ,A.SubId, ');
    sql.Add('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 A.RKOrdID=''' + Trim(RKOrdID.Text) + '''');
    sql.Add(' Group by A.RKOrdID,A.QtyUnit,D.OrderNo,C.PRTColor,C.SOrddefstr4,A.SubId)AAA');
    Open;
  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);
    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;
begin
  if Trim(Edit3.Text) = '' then
    Exit;
  if Trim(BaoNo.Text) = '' then
  begin
    BaoNo.Text := '1';
    Exit;
  end
  else
  begin
    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,B.AOrdDefStr1,D.OrderNo,C.PRTColor,C.SOrddefstr4,Sum(WM.MJQty4) MJJingZ,Count(*) JSl,');
    sql.Add('Sum(WM.MJMaoZ) MJMaoZ,Sum(WM.MJLen) MJLen,A.QtyUnit');
    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) + '''');
    sql.Add(' Group by A.BaoId,A.BaoNo,B.AOrdDefStr1,D.OrderNo,C.PRTColor,BaoId,A.QtyUnit,C.SOrddefstr4');
    Open;
  end;
  if ADOQueryPrt.RecordCount > 1 then
  begin
    Application.MessageBox('打包错误，请撤销入库!', '提示', 0);
    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);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\包标签.rmf'), '提示', 0);
  end;
end;

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

procedure TfrmCpRkSaoMNew.MJIDKeyPress(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 Trim(RKOrdID.Text) = '' then
    begin
      Application.MessageBox('入库单号不能为空!', '提示', 0);
      Exit;
    end;
    {if Trim(RKPlace.Text)='' then
    begin
      Application.MessageBox('库位不能为空!','提示',0);
      Exit;
    end;}
    if CDS_Main.Locate('baoid', Trim(MJID.Text), []) = False then
    begin
      MJID.Text := '';
   //   Application.MessageBox('此卷不包含在待入库的卷数据中！','提示');
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\扫描错误.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\扫描错误.wav'), 0, SND_ASYNC);
      Exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with CDS_Main do
      begin
        First;
        while CDS_Main.Locate('baoid', Trim(mjid.Text), []) = True 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('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(CDS_Main.fieldbyname('BaoNo').AsString);
            FieldByName('BaoID').Value := Trim(CDS_Main.fieldbyname('BaoID').AsString);
            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;
            FieldByName('BaoNo').Value := Trim(CDS_Main.fieldbyname('BaoNo').AsString);
            FieldByName('BaoID').Value := Trim(CDS_Main.fieldbyname('BaoID').AsString);
            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('RKOrdID').Value := Trim(RKOrdID.Text);
            FieldByName('PRTColor').Value := CDS_Main.fieldbyname('PRTColor').Value;
            FieldByName('SOrddefstr1').Value := CDS_Main.fieldbyname('SOrddefstr1').Value;
            FieldByName('MJXH').Value := CDS_Main.fieldbyname('MJXH').Value;
            FieldByName('BaoNo').Value := Trim(CDS_Main.fieldbyname('BaoNo').asstring);
            FieldByName('BaoID').Value := Trim(CDS_Main.fieldbyname('BaoID').asstring);
            Post;
          end;
          CDS_Main.Delete;
        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;
      MJID.Text := '';
      JSbaoNum();
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\正确.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\正确.wav'), 0, SND_ASYNC);
      Exit;
    except
      MJID.Text := '';
      ADOQueryCmd.Connection.RollbackTrans;
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\保存失败.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\保存失败.wav'), 0, SND_ASYNC);
//      Application.MessageBox('入库异常！','提示',0);
    end;
  end;
end;

procedure TfrmCpRkSaoMNew.MJTypeChange(Sender: TObject);
begin
//  if ADOQueryTemp.Active=False then Exit;
//  SDofilter(ADOQueryTemp,SGetFilters(Panel1,1,2));
//  SCreateCDS20(ADOQueryTemp,CDS_Main);
//  SInitCDSData20(ADOQueryTemp,CDS_Main);
  CDS_Main.EmptyDataSet;
  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 where SubId=A.SubId)');
    sql.Add(',AOrdDefStr1=A.MJStr4,A.* ');
    sql.Add(' from WFB_MJJY A ');
    sql.Add(' where isnull(A.MJStr2,'''')=''未入库'' and (MJLen>0 OR MJMaoZ>0) and A.Subid=''' + trim(CDS_Sub.fieldbyname('subId').AsString) + ''' and A.MJType=''' + MJType.Text + '''');
    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('SOrddefstr1').Value := Self.ADOQueryTemp.fieldbyname('SOrddefstr1').Value;
        FieldByName('AOrddefstr1').Value := Self.ADOQueryTemp.fieldbyname('AOrddefstr1').Value;
        FieldByName('MJXH').Value := Self.ADOQueryTemp.fieldbyname('MJXH').Value;
        FieldByName('baoNo').Value := Self.ADOQueryTemp.fieldbyname('baoNo').Value;
        FieldByName('baoID').Value := Self.ADOQueryTemp.fieldbyname('baoID').Value;
        Post;
      end;
      Next;
    end;
  end;
  Self.ADOQueryTemp.EnableControls;
end;

procedure TfrmCpRkSaoMNew.Edit1Change(Sender: TObject);
begin
//  if ADOQueryTemp.Active=False then Exit;
//  SDofilter(ADOQueryTemp,SGetFilters(Panel1,1,2));
//  SCreateCDS20(ADOQueryTemp,CDS_Main);
//  SInitCDSData20(ADOQueryTemp,CDS_Main);
  CDS_Main.EmptyDataSet;
  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 where SubId=A.SubId)');
    sql.Add(',AOrdDefStr1=A.MJStr4,A.* ');
    sql.Add(' from WFB_MJJY A ');
    sql.Add(' where isnull(A.MJStr2,'''')<>''已入库'' and (MJLen>0 OR MJMaoZ>0) and A.Subid=''' + trim(CDS_Sub.fieldbyname('subId').AsString) + '''');
    if Edit1.Text <> '' then
    begin
      sql.Add(' and A.MJStr4=''' + Edit1.Text + '''')
    end;
    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('SOrddefstr1').Value := Self.ADOQueryTemp.fieldbyname('SOrddefstr1').Value;
        FieldByName('AOrddefstr1').Value := Self.ADOQueryTemp.fieldbyname('AOrddefstr1').Value;
        FieldByName('MJXH').Value := Self.ADOQueryTemp.fieldbyname('MJXH').Value;
        FieldByName('baoNo').Value := Self.ADOQueryTemp.fieldbyname('baoNo').Value;
        FieldByName('baoID').Value := Self.ADOQueryTemp.fieldbyname('baoID').Value;
        Post;
      end;
      Next;
    end;
  end;
  Self.ADOQueryTemp.EnableControls;
end;

end.

