unit U_CpCkSaoMNewSel;

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;

type
  TfrmCpCkSaoMNewSel = class(TForm)
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    DataSource1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel1: TPanel;
    BaoID: TEdit;
    Label1: TLabel;
    Button2: TButton;
    Button3: TButton;
    CDS_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuerySub: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    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;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column5: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    Button5: TButton;
    CRTime: TDateTimePicker;
    Label4: TLabel;
    Label5: TLabel;
    CKOrdNo: TEdit;
    ADOQueryPrice: TADOQuery;
    CDSPrice: TClientDataSet;
    Label6: TLabel;
    RKOrdID: TEdit;
    Label7: TLabel;
    v1Column8: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    MovePanel1: TMovePanel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button4: TButton;
    MovePanel2: TMovePanel;
    v1Column9: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    Label8: TLabel;
    CRNote: TEdit;
    bao: TEdit;
    Label9: TLabel;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label10: TLabel;
    THMJID: TEdit;
    v3MJID: TcxGridDBColumn;
    Label12: TLabel;
    v1KHName: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BaoIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button4Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure RKOrdIDKeyPress(Sender: TObject; var Key: Char);
    procedure baoKeyPress(Sender: TObject; var Key: Char);
    procedure THMJIDKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    procedure InitGrid();
    procedure InitSubGrid();
    function  YSData(Order_Main10:TClientDataSet):Boolean;
    procedure InitPrice();
  public
    { Public declarations }
  end;

var
  frmCpCkSaoMNewSel: TfrmCpCkSaoMNewSel;

implementation
uses
U_DataLink,U_Fun,U_OrderSel ;

{$R *.dfm}

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

procedure TfrmCpCkSaoMNewSel.FormDestroy(Sender: TObject);
begin
  frmCpCkSaoMNewSel:=nil;
end;
procedure TfrmCpCkSaoMNewSel.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(',KHName=(select isnull(customerNoName,OrderNo) from JYOrder_Main where MainId=A.MainId)');
      sql.Add(',MPrtCodeName=(select MPrtCodeName from JYOrder_Main where MainId=A.MainId)');
      sql.Add(',Mprtspec=(select Mprtspec from JYOrder_Main where MainId=A.MainId)');
      SQL.Add(',PRTHX=(select PRTHX from JYOrder_Sub where SubId=A.SubId)');
      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);

    SCreateCDS20(ADOQueryMain,CDS_MainSel);
    //SInitCDSData20(ADOQueryMain,CDS_MainSel);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCpCkSaoMNewSel.FormShow(Sender: TObject);
begin
  ReadCxGrid('订单选择',Tv2,'成品仓库');
  ReadCxGrid('成品出库',Tv1,'成品仓库');
  ReadCxGrid('成品出库Sels',Tv3,'成品仓库');
  InitSubGrid();
  InitGrid();
  CRTime.DateTime:=SGetServerDate(ADOQueryTemp);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Mainid,PRTPrice,KHName=''''  from JYOrder_Sub where 1<>1');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDSPrice);
  SInitCDSData20(ADOQueryTemp,CDSPrice);
end;
procedure TfrmCpCkSaoMNewSel.InitPrice();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Mainid,PRTPrice,KHName=Cast('''' as varchar(50))  from JYOrder_Sub where 1<>1');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDSPrice);
  SInitCDSData20(ADOQueryTemp,CDSPrice);
  if CDSPrice.IsEmpty=False then
  begin
    CDSPrice.Delete;
  end;
end;

procedure TfrmCpCkSaoMNewSel.BaoIDKeyPress(Sender: TObject; var Key: Char);
var
  maxno:String;
begin
  if Key=#13 then
  begin
    Label12.Caption:='';
    if CDS_Sub.IsEmpty then
    begin
      BaoID.Text:='';
      Application.MessageBox('未选单不能扫描出库！','提示',0);
      Exit;
    end;
    if Trim(CKOrdNo.Text)='' then
    begin
      Application.MessageBox('出库单号不能为空!','提示',0);
      Exit;
    end;
    if CDS_MainSel.Locate('MJId',Trim(BaoID.Text),[]) then
    begin
      MovePanel1.Visible:=True;
      Edit2.Text:=Trim(BaoID.Text);
      Label12.Caption:='提醒：此卷重复扫描！';
      BaoID.Text:='';

    //  Application.MessageBox('此卷重复扫描！','提示',0);
      Exit;
    end;
    if CDS_Main.Locate('MJId',Trim(BaoID.Text),[])=False then
    begin
      MovePanel1.Visible:=True;
      Edit2.Text:=Trim(BaoID.Text);
      Label12.Caption:='提醒：此卷不包含在待出库的卷数据中！';
      BaoID.Text:='';

    //  Application.MessageBox('此卷不包含在待出库的卷数据中！','提示',0);
      Exit;
    end;
    CDS_Main.Locate('MJId',Trim(BaoID.Text),[]);
    try
      ADOQueryCmd.Connection.BeginTrans;
      with CDS_Main do
      begin
        if GetLSNo(ADOQueryCmd,maxno,'CC','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('BCID').Value:=Trim(maxno);
          FieldByName('CRID').Value:=CDS_Main.fieldbyname('CRID').Value;
          FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',CRTime.Date);
          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('MJID').Value:=CDS_Main.fieldbyname('MJID').Value;
          FieldByName('MainID').Value:=CDS_Main.fieldbyname('MainID').Value;
          FieldByName('SubID').Value:=CDS_Main.fieldbyname('SubID').Value;
          FieldByName('APID').Value:=CDS_Main.fieldbyname('APID').Value;
          FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
          FieldByName('BaoNo').Value:=CDS_Main.fieldbyname('BaoNo').Value;
          FieldByName('BaoID').Value:=CDS_Main.fieldbyname('BaoID').Value;
          FieldByName('RKPlace').Value:=CDS_Main.fieldbyname('RKPlace').AsString;
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRFlag').Value:='出库';
          FieldByName('CRType').Value:='正常出库';
          FieldByName('CKOrdNo').Value:=Trim(CKOrdNo.Text);
          FieldByName('CRNote').Value:=Trim(CRNote.Text);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_BanCp_KC set KCKgQty=0,KCQty=0 where CRID='+CDS_Main.fieldbyname('CRID').AsString);
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
            Close;
            sql.Clear;
            sql.Add(' Update WFB_MJJY Set MJStr2=''已出库'' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
            sql.Add(' update JYOrdercon_Main SET status=''1''  ');
            sql.Add(' from JYOrdercon_Main A  ');
            sql.Add(' inner join JYOrder_Main B on B.conNO=A.conNO  ');
            sql.Add(' where B.MainID='''+Trim(CDS_Main.fieldbyname('MainID').AsString)+''' and A.status<>''1''  ');
            ExecSQL;
        end;
      end;
      if YSData(CDS_Main)=False 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('KHName').Value:=Self.CDS_Main.fieldbyname('KHName').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:=Self.CDS_Main.fieldbyname('CRId').Value;
        FieldByName('APID').Value:=Self.CDS_Main.fieldbyname('APID').Value;
        FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
        FieldByName('BCID').Value:=Trim(maxno);
        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('KHName').Value:=CDS_Main.fieldbyname('KHName').Value;
        FieldByName('RKOrdId').Value:=CDS_Main.fieldbyname('RKOrdId').Value;
        FieldByName('BaoNo').Value:=CDS_Main.fieldbyname('BaoNo').Value;
        //FieldByName('JZXNo').Value:=Trim(JZXNo.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:='';
      Exit;
    except
      BaoID.Text:='';
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('出库异常！','提示',0);
    end;
  end;
end;

procedure TfrmCpCkSaoMNewSel.Button2Click(Sender: TObject);
begin
  tv1.Controller.EditingController.ShowEdit();
  tv3.Controller.EditingController.ShowEdit();
  Close;
  WriteCxGrid('订单选择',Tv2,'成品仓库');
  WriteCxGrid('成品出库',Tv1,'成品仓库');
  WriteCxGrid('成品出库Sels',Tv3,'成品仓库');
end;

procedure TfrmCpCkSaoMNewSel.Button3Click(Sender: TObject);
var
  maxno,fsj:String;
begin
  {if CDS_Main.IsEmpty=False then
  begin
    Application.MessageBox('已扫描不能更改单号！','提示',0);
    Exit;
  end;}

  try
    frmOrderSel:=TfrmOrderSel.Create(Application);
    with frmOrderSel 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;

      end;
    end;
  finally
    frmOrderSel.Free;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
    ExecSQL;
  end;
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into TBSubID select :SubId,:Dname');
        Parameters.ParamByName('SubId').Value:=Trim(CDS_Sub.fieldbyname('SubId').AsString);
        Parameters.ParamByName('Dname').Value:=Trim(DCode);
        ExecSQL;
      end;
      Next;
    end;
  end;
  CDS_Sub.EnableControls;
  with Self.ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
      SQL.Add('select A.*,OrderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId) ');
      sql.Add(',KHName=(select isnull(customerNoName,OrderNo) from JYOrder_Main where MainId=A.MainId)');
      sql.Add(',MPrtCodeName=(select MPrtCodeName from JYOrder_Main where MainId=A.MainId)');
      sql.Add(',Mprtspec=(select Mprtspec from JYOrder_Main where MainId=A.MainId)');
      SQL.Add(',PRTHX=(select PRTHX from JYOrder_Sub where SubId=A.SubId)');
      SQL.Add(',PRTColor=(select PRTHX 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 inner join CK_BanCP_KC B on A.CRID=B.CRID');
    sql.Add(' where exists( select * from TBSubID AA where AA.SubId=A.SubId and AA.DName='''+Trim(DCode)+''') ');
    SQL.Add('  and (B.KCqty>0 or B.KCKgQty>0) and A.CRType=''检验入库'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Main);
  SInitCDSData20(ADOQueryTemp,CDS_Main);
  SCreateCDS20(ADOQueryTemp,CDS_Mainsel);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.MainId,A.PRTPrice,C.CustomerNoName from JYOrder_Sub A inner join TBSubID B on A.SubId=B.SubId');
    sql.Add(' inner join JYOrder_Main C on A.MainId=C.MainId ');
    sql.Add(' where B.DName='''+Trim(DCode)+'''');
    sql.Add(' group by A.MainId,A.PRTPrice,C.CustomerNoName');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDSPrice);
  SInitCDSData20(ADOQueryTemp,CDSPrice);
  if GetLSNo(ADOQueryCmd,maxno,'CK','CK_BanCP_CR',3,1)=False then
  begin
    Application.MessageBox('取出库单号失败!','提示',0);
    Exit;
  end;
  CKOrdNo.Text:=Trim(maxno);
end;

procedure TfrmCpCkSaoMNewSel.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 TfrmCpCkSaoMNewSel.Button1Click(Sender: TObject);
var
  FMainid:String;
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;
  InitPrice();

  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_MainSel.DisableControls;
    with CDS_MainSel do
    begin
      //First;
      while CDS_MainSel.Locate('SSel',True,[])=True 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('UPdate CK_BanCP_KC Set KCKgQty=(select KgQty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
            sql.Add(',KCQty=(select Qty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
            SQL.Add(' where CRID='+CDS_MainSel.fieldbyname('CRID').AsString);
            sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' where MJID='''+Trim(CDS_MainSel.fieldbyname('MJID').AsString)+'''');
            ExecSQL;
          end;
          CDS_Main.DisableControls;
          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('CRId').Value:=Self.CDS_MainSel.fieldbyname('CRId').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;
            FieldByName('KHName').Value:=CDS_MainSel.fieldbyname('KHName').Value;
            FieldByName('RKOrdID').Value:=CDS_MainSel.fieldbyname('RKOrdID').Value;
            FieldByName('BaoNo').Value:=CDS_MainSel.fieldbyname('BaoNo').Value;
            FieldByName('BaoID').Value:=CDS_MainSel.fieldbyname('BaoID').Value;
            Post;
          end;
          CDS_Main.EnableControls;
          if YSData(CDS_MainSel)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('更新应收款失败!','提示',0);
            Exit;
          end;
          CDS_MainSel.Delete;
        end;
      end;
    end;
    CDS_MainSel.EnableControls;
    CDSPrice.EnableControls;   
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel1.Visible:=True;
    if CDS_MainSel.IsEmpty=False then
      Edit1.Text:=IntToStr(Tv3.DataController.Summary.FooterSummaryValues[2])
    else
      Edit1.Text:='0';
    MovePanel2.Visible:=False;
    Exit;
  except
    MovePanel2.Visible:=False;
    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！','提示',0);
  end;
end;

procedure TfrmCpCkSaoMNewSel.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 TfrmCpCkSaoMNewSel.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible:=False;
end;

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

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

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

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

procedure TfrmCpCkSaoMNewSel.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(CKOrdNo.Text)='' then
  begin
    Application.MessageBox('出库单号不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行出库操作吗？','提示',32+4)<>IDYES then exit;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  InitPrice();
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    CDS_MainSel.DisableControls;
    with CDS_Main do
    begin
      //First;
      while CDS_Main.Locate('SSel',True,[])=True do
      begin
        application.ProcessMessages;
        //if CDS_Main.FieldByName('SSel').AsBoolean=True then
        begin
          if GetLSNo(ADOQueryTemp,maxno,'CC','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('BCID').Value:=Trim(maxno);
            FieldByName('CRID').Value:=CDS_Main.fieldbyname('CRID').Value;
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',CRTime.Date);
            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('MJID').Value:=CDS_Main.fieldbyname('MJID').Value;
            FieldByName('MainID').Value:=CDS_Main.fieldbyname('MainID').Value;
            FieldByName('SubID').Value:=CDS_Main.fieldbyname('SubID').Value;
            FieldByName('APID').Value:=CDS_Main.fieldbyname('APID').Value;
            FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
            FieldByName('BaoNo').Value:=CDS_Main.fieldbyname('BaoNo').Value;
            FieldByName('BaoID').Value:=CDS_Main.fieldbyname('BaoID').Value;
            FieldByName('RKPlace').Value:=CDS_Main.fieldbyname('RKPlace').AsString;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('CRFlag').Value:='出库';
            FieldByName('CRType').Value:='正常出库';
            FieldByName('CKOrdNo').Value:=Trim(CKOrdNo.Text);
            FieldByName('CRNote').Value:=Trim(CRNote.Text);
            //FieldByName('JZXNo').Value:=Trim(JZXNo.Text);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_BanCp_KC set KCKgQty=0,KCQty=0 where CRID='+CDS_Main.fieldbyname('CRID').AsString);
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' Update WFB_MJJY Set MJStr2=''已出库'' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
            sql.Add(' update JYOrdercon_Main SET status=''1''  ');
            sql.Add(' from JYOrdercon_Main A  ');
            sql.Add(' inner join JYOrder_Main B on B.conNO=A.conNO  ');
            sql.Add(' where B.MainID='''+Trim(CDS_Main.fieldbyname('MainID').AsString)+''' and A.status<>''1''  ');
            ExecSQL;
          end;
          if YSData(CDS_Main)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('更新应收款失败!','提示',0);
            Exit;
          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('KHName').Value:=Self.CDS_Main.fieldbyname('KHName').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:=Self.CDS_Main.fieldbyname('CRId').Value;
            FieldByName('APID').Value:=Self.CDS_Main.fieldbyname('APID').Value;
            FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
            FieldByName('BCID').Value:=Trim(maxno);
            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('KHName').Value:=CDS_Main.fieldbyname('KHName').Value;
            FieldByName('RKOrdId').Value:=CDS_Main.fieldbyname('RKOrdId').Value;
            FieldByName('BaoNo').Value:=CDS_Main.fieldbyname('BaoNo').Value;
            //FieldByName('JZXNo').Value:=Trim(JZXNo.Text);
            Post;
          end;
      //    CDS_MainSel.EnableControls;
          CDS_Main.Delete;
        end;
      end;
    end;
    CDS_MainSel.EnableControls;
    CDS_Main.EnableControls;
    CDS_Sub.DisableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    Exit;
  except
    BaoID.Text:='';
    CDS_Main.EnableControls;
    MovePanel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('入库异常！','提示',0);
  end;
end;
function TfrmCpCkSaoMNewSel.YSData(Order_Main10:TClientDataSet):Boolean;
var
  CRID,YFID,PriceUnit,OrderUnit,FComTaiTou,FromSubID:String;
  fprice,huilv:double;
begin
  Result:=False;
  fprice:=0;
  with Order_Main10 do
  begin

    begin
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select Top 1 *,fPrice=dbo.F_Get_Order_Price(MainID,subID) from JYOrder_Sub Where subID='''+Trim(Order_Main10.fieldbyname('subID').AsString)+'''');
        Open;
      end;
      IF not ADOQueryTemp.IsEmpty then
      begin
        PriceUnit:=Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
        OrderUnit:=Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
        fPrice:= ADOQueryTemp.fieldbyname('fPrice').AsFloat;
        huilv:= ADOQueryTemp.fieldbyname('huilv').AsFloat;
      end;
      IF huilv=0 then huilv:=1;
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
      end else
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CRID set CRID=CRID+1');
          sql.Add('select * from YF_Money_CRID ');
          Open;
        end;
        CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_KC where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('KHName').AsString);
          FieldByName('ZdyStr1').Value:='应收收';
          Post;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add(' select * from YF_Money_CR where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
        sql.Add(' and mainID='''+Trim(Order_Main10.fieldbyname('mainID').AsString)+'''');
        sql.Add(' and CRTime='''+FormatDateTime('yyyy-MM-dd',CRTime.Date)+'''') ;
       // sql.Add(' and YFTypeId='''+Trim(Order_Main10.fieldbyname('subID').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',3,1)=False then
        begin
          Application.MessageBox('取成品应收最大号失败!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('YFID').Value:=Trim(YFID);
          FieldByName('YFTypeId').Value:=Trim(Order_Main10.fieldbyname('mainID').AsString);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRType').Value:='应收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('KHName').AsString);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRTime.Date));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Price').Value:=fprice;
          FieldByName('HuiLv').Value:=huilv;
          FieldByName('BZType').Value:=Trim(PriceUnit);
          FieldByName('QtyUnit').Value:=Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
          FieldByName('ComTaiTou').Value:=Trim(FComTaiTou);
          FieldByName('YFName').Value:='销售金额';
          FieldByName('MainId').Value:=Trim(Order_Main10.fieldbyname('Mainid').AsString);
     //     FieldByName('subID').Value:=Trim(Order_Main10.fieldbyname('subID').AsString);
          FieldByName('OrderNO').Value:=Trim(Order_Main10.fieldbyname('OrderNO').AsString);
          FieldByName('P_CodeName').Value:=Trim(Order_Main10.fieldbyname('MprtCodeName').AsString);
          FieldByName('P_Spec').Value:=Trim(Order_Main10.fieldbyname('MprtSpec').AsString);
          FieldByName('status').Value:='0';
          Post;
        end;
      end  else
      begin
        YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(A.Qty),0) from CK_BanCP_CR A where A.MainID=YF_Money_CR.MainID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''正常出库'') ');
        sql.Add(',PS=(select isnull(count(A.mainID),0) from CK_BanCP_CR A where A.maiNID=YF_Money_CR.maiNID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''正常出库'')  ');
        sql.Add(' where MainID='''+Trim(Order_Main10.fieldbyname('mainID').AsString)+'''');
        sql.Add(' and CRTime='''+FormatDateTime('yyyy-MM-dd',CRTime.Date)+'''') ;
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where YFId='''+Trim(YFID)+'''');
        Open;
      end;
      if ADOQueryTemp.FieldByName('Qty').Value=0 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from YF_Money_CR where YFID='''+Trim(YFID)+'''');
          ExecSQL;
        end;
      end;
      //Next;
    end;
  end;
  Result:=True;
end;

procedure TfrmCpCkSaoMNewSel.RKOrdIDKeyPress(Sender: TObject;
  var Key: Char);
var
  maxno:String;
begin
  if Key=#13 then
  begin
    Label12.Caption:='';
    if CDS_Sub.IsEmpty then
    begin
      BaoID.Text:='';
      Application.MessageBox('未选单不能扫描出库！','提示',0);
      Exit;
    end;
    if Trim(CKOrdNo.Text)='' then
    begin
      Application.MessageBox('出库单号不能为空!','提示',0);
      Exit;
    end;
    if CDS_Main.Locate('RKOrdId',Trim(RKOrdId.Text),[])=False then
    begin
      MovePanel1.Visible:=True;
      Edit2.Text:=Trim(RKOrdId.Text);
      Label12.Caption:='提醒：此入库单不包含在待出库的卷数据中！';
      RKOrdId.Text:='';

    //  Application.MessageBox('此入库单不包含在待出库的卷数据中！','提示',0);
      Exit;
    end;
    //CDS_Main.Locate('MJId',Trim(BaoID.Text),[]);
    MovePanel2.Visible:=True;
    MovePanel2.Refresh;
    InitPrice();
    try
      ADOQueryCmd.Connection.BeginTrans;
      with CDS_Main do
      begin
        First;
        while CDS_Main.Locate('RKOrdId',Trim(RKOrdId.Text),[])=True do
        begin
          if GetLSNo(ADOQueryCmd,maxno,'CC','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('BCID').Value:=Trim(maxno);
            FieldByName('CRID').Value:=CDS_Main.fieldbyname('CRID').Value;
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',CRTime.Date);
            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('MJID').Value:=CDS_Main.fieldbyname('MJID').Value;
            FieldByName('MainID').Value:=CDS_Main.fieldbyname('MainID').Value;
            FieldByName('SubID').Value:=CDS_Main.fieldbyname('SubID').Value;
            FieldByName('APID').Value:=CDS_Main.fieldbyname('APID').Value;
            FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
            FieldByName('BaoNo').Value:=CDS_Main.fieldbyname('BaoNo').Value;
            FieldByName('BaoID').Value:=CDS_Main.fieldbyname('BaoID').Value;
            FieldByName('RKPlace').Value:=CDS_Main.fieldbyname('RKPlace').AsString;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('CRFlag').Value:='出库';
            FieldByName('CRType').Value:='正常出库';
            FieldByName('CRNote').Value:=Trim(CRNote.Text);
            FieldByName('CKOrdNo').Value:=Trim(CKOrdNo.Text);
            //FieldByName('JZXNo').Value:=Trim(JZXNo.Text);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_BanCp_KC set KCKgQty=0,KCQty=0 where CRID='+CDS_Main.fieldbyname('CRID').AsString);
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' Update WFB_MJJY Set MJStr2=''已出库'' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
            sql.Add(' update JYOrdercon_Main SET status=''1''  ');
            sql.Add(' from JYOrdercon_Main A  ');
            sql.Add(' inner join JYOrder_Main B on B.conNO=A.conNO  ');
            sql.Add(' where B.MainID='''+Trim(CDS_Main.fieldbyname('MainID').AsString)+''' and A.status<>''1''  ');
            ExecSQL;
          end;
         if YSData(CDS_Main)=False then
         begin
           ADOQueryCmd.Connection.RollbackTrans;
           Application.MessageBox('更新应收款失败!','提示',0);
           Exit;
         end;
          if CDSPrice.Locate('MainId',Trim(CDS_Main.fieldbyname('MainId').AsString),[])=False then
          begin
            CDSPrice.Append;
            CDSPrice.FieldByName('MainId').Value:=Trim(CDS_Main.fieldbyname('MainId').AsString);
            CDSPrice.FieldByName('KHName').Value:=Trim(CDS_Main.fieldbyname('KHName').AsString);
            //CDS_Sub.Locate('MainId',Trim(CDS_Main.fieldbyname('MainId').AsString),[]);
            {if Trim(CDS_Sub.fieldbyname('OrderUnit').AsString)<>'Kg' then
            begin
              CDSPrice.FieldByName('JLUnit').Value:='M';
            end else
            begin
              CDSPrice.FieldByName('JLUnit').Value:='Kg';
            end;  }
            CDSPrice.Post;
          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('KHName').Value:=Self.CDS_Main.fieldbyname('KHName').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:=Self.CDS_Main.fieldbyname('CRId').Value;
            FieldByName('APID').Value:=Self.CDS_Main.fieldbyname('APID').Value;
            FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
            FieldByName('BCID').Value:=Trim(maxno);
            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('KHName').Value:=CDS_Main.fieldbyname('KHName').Value;
            FieldByName('RKOrdId').Value:=CDS_Main.fieldbyname('RKOrdId').Value;
            FieldByName('BaoNo').Value:=CDS_Main.fieldbyname('BaoNo').Value;
            //FieldByName('JZXNo').Value:=Trim(JZXNo.Text);
            Post;
          end;
          CDS_Main.Delete;
        end;

      end;
      with CDSPrice do
      begin
        First;
        while not Eof do
        begin
          if YSData(CDSPrice)=False then
          begin
            MovePanel2.Visible:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('更新应收款失败!','提示',0);
            Exit;
          end;
          Next;
        end;
      end;

      ADOQueryCmd.Connection.CommitTrans;

      MovePanel1.Visible:=True;
      if CDS_MainSel.IsEmpty=False then
        Edit1.Text:=IntToStr(Tv3.DataController.Summary.FooterSummaryValues[2])
      else
        Edit1.Text:='0';
      Edit2.Text:=Trim(RKOrdID.Text);
      RKOrdID.Text:='';
      MovePanel2.Visible:=False;
      Exit;
    except
      RKOrdID.Text:='';
      MovePanel2.Visible:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('出库异常！','提示',0);
    end;
  end;
end;

procedure TfrmCpCkSaoMNewSel.baoKeyPress(Sender: TObject; var Key: Char);
var
  maxno:String;
begin
  if Key=#13 then
  begin
    Label12.Caption:='';
    if CDS_Sub.IsEmpty then
    begin
      Bao.Text:='';
      Application.MessageBox('未选单不能扫描出库！','提示',0);
      Exit;
    end;
    if Trim(bao.Text)='' then
    begin
      Application.MessageBox('包号不能为空!','提示',0);
      Exit;
    end;
    if Trim(CKOrdNo.Text)='' then
    begin
      Application.MessageBox('出库单号不能为空!','提示',0);
      Exit;
    end;
    if CDS_Main.Locate('baoID',Trim(bao.Text),[])=False then
    begin
      MovePanel1.Visible:=True;
      Edit2.Text:=Trim(bao.Text);
      Label12.Caption:='提醒：此包不包含在待出库的卷数据中！';
      bao.Text:='';

     // Application.MessageBox('此包不包含在待出库的卷数据中！','提示',0);
      Exit;
    end;
  //  CDS_Main.Locate('baoID',Trim(bao.Text),[]);
    try
      ADOQueryCmd.Connection.BeginTrans;
      with CDS_Main do
      begin
        first;
        while  CDS_Main.Locate('baoID',Trim(bao.Text),[]) do
        begin
        if GetLSNo(ADOQueryCmd,maxno,'CC','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('BCID').Value:=Trim(maxno);
          FieldByName('CRID').Value:=CDS_Main.fieldbyname('CRID').Value;
          FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',CRTime.Date);
          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('MJID').Value:=CDS_Main.fieldbyname('MJID').Value;
          FieldByName('MainID').Value:=CDS_Main.fieldbyname('MainID').Value;
          FieldByName('SubID').Value:=CDS_Main.fieldbyname('SubID').Value;
          FieldByName('APID').Value:=CDS_Main.fieldbyname('APID').Value;
          FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
          FieldByName('BaoNo').Value:=CDS_Main.fieldbyname('BaoNo').Value;
          FieldByName('BaoID').Value:=CDS_Main.fieldbyname('BaoID').Value;
          FieldByName('RKPlace').Value:=CDS_Main.fieldbyname('RKPlace').AsString;
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRFlag').Value:='出库';
          FieldByName('CRType').Value:='正常出库';
          FieldByName('CKOrdNo').Value:=Trim(CKOrdNo.Text);
          FieldByName('CRNote').Value:=Trim(CRNote.Text);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_BanCp_KC set KCKgQty=0,KCQty=0 where CRID='+CDS_Main.fieldbyname('CRID').AsString);
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
            Close;
            sql.Clear;
            sql.Add(' Update WFB_MJJY Set MJStr2=''已出库'' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
            sql.Add(' update JYOrdercon_Main SET status=''1''  ');
            sql.Add(' from JYOrdercon_Main A  ');
            sql.Add(' inner join JYOrder_Main B on B.conNO=A.conNO  ');
            sql.Add(' where B.MainID='''+Trim(CDS_Main.fieldbyname('MainID').AsString)+''' and A.status<>''1''  ');
            ExecSQL;
        end;

      if YSData(CDS_Main)=False then
      begin

        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('更新应收款失败!','提示',0);
        Exit;
      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('KHName').Value:=Self.CDS_Main.fieldbyname('KHName').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:=Self.CDS_Main.fieldbyname('CRId').Value;
        FieldByName('APID').Value:=Self.CDS_Main.fieldbyname('APID').Value;
        FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
        FieldByName('BCID').Value:=Trim(maxno);
        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('KHName').Value:=CDS_Main.fieldbyname('KHName').Value;
        FieldByName('RKOrdId').Value:=CDS_Main.fieldbyname('RKOrdId').Value;
         FieldByName('BaoNo').Value:=CDS_Main.fieldbyname('BaoNo').Value;
        //FieldByName('JZXNo').Value:=Trim(JZXNo.Text);
        Post;
        end;
        CDS_Main.Delete;
       end;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      MovePanel1.Visible:=True;
      if CDS_MainSel.IsEmpty=False then
        Edit1.Text:=IntToStr(Tv3.DataController.Summary.FooterSummaryValues[2])
      else
        Edit1.Text:='0';
      Edit2.Text:=Trim(bao.Text);
      bao.Text:='';
      exit;
    except
      bao.Text:='';
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('出库异常！','提示',0);
    end;
  end;

end;

procedure TfrmCpCkSaoMNewSel.THMJIDKeyPress(Sender: TObject;
  var Key: Char);
begin
 if Key=#13 then
  begin
    if CDS_MainSel.IsEmpty then
    begin
      BaoID.Text:='';
      Application.MessageBox('未出库不能扫描退货！','提示',0);
      Exit;
    end;
    if Trim(THMJID.Text)='' then
    begin
      Application.MessageBox('条码不能为空!','提示',0);
      Exit;
    end;
    if not CDS_MainSel.Locate('MJId',Trim(THMJID.Text),[]) then
    begin
      BaoID.Text:='';
      Application.MessageBox('此卷号不存在！','提示',0);
      Exit;
    end;

    CDS_MainSel.Locate('MJId',Trim(THMJID.Text),[]);
    try
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete CK_BanCP_CR where BCID='''+Trim(CDS_MainSel.fieldbyname('BCID').AsString)+'''');
            sql.Add('UPdate CK_BanCP_KC Set KCKgQty=(select KgQty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
            sql.Add(',KCQty=(select Qty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
            SQL.Add(' where CRID='+CDS_MainSel.fieldbyname('CRID').AsString);
            sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' where MJID='''+Trim(CDS_MainSel.fieldbyname('MJID').AsString)+'''');
            ExecSQL;
          end;
       //   CDS_Main.DisableControls;
          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('CRId').Value:=Self.CDS_MainSel.fieldbyname('CRId').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;
            FieldByName('KHName').Value:=CDS_MainSel.fieldbyname('KHName').Value;
            FieldByName('RKOrdID').Value:=CDS_MainSel.fieldbyname('RKOrdID').Value;
            FieldByName('BaoNo').Value:=CDS_MainSel.fieldbyname('BaoNo').Value;
            FieldByName('BaoID').Value:=CDS_MainSel.fieldbyname('BaoID').Value;
            Post;
          end;
      CDS_MainSel.Delete;
      Edit2.Text:=Trim(THMJID.Text);
      THMJID.Text:='';
      THMJID.SetFocus;
      Exit;
    except
      THMJID.Text:='';
      THMJID.SetFocus;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('撤销出库异常！','提示',0);
    end;
  end;
end;

end.
