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, MMSystem,
  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;
    Label6: 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;
    v3MJID: TcxGridDBColumn;
    Label12: TLabel;
    Label11: TLabel;
    CRType: TComboBox;
    ADOQuery_Other: TADOQuery;
    ADOQuery1: TADOQuery;
    Label13: TLabel;
    v2Column1: TcxGridDBColumn;
    tdNo: TEdit;
    Label14: TLabel;
    Label15: TLabel;
    jzxNo: TEdit;
    Label16: TLabel;
    Button6: TButton;
    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 baoKeyPress(Sender: TObject; var Key: Char);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
    FRBs,FCBs,FKBs:Integer;
    procedure InitGrid();
    procedure InitBs(Flag:string);
    procedure InitSubGrid();
    function  YSData(Order_Main10:TClientDataSet):Boolean;
    function  YSData_other(Ado:TADOQuery;moneyFee:string;money:double):Boolean;
    function  ZD_YSData():Boolean;
    procedure InitPrice();
  public
    { Public declarations }
  end;

var
  frmCpCkSaoMNewSel: TfrmCpCkSaoMNewSel;

implementation
uses
U_DataLink,U_Fun,U_OrderSel,U_ProductOrderNewList_CY_Sel;

{$R *.dfm}
procedure TfrmCpCkSaoMNewSel.InitBs(Flag:string);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select COUNT(distinct A.BaoId) bs 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='+quotedstr(Trim(DCode))+' and (B.KCqty>0 or B.KCKgQty>0) and A.CRType=''检验入库'')');
    Open;
    if Flag='选单' then
    begin
     FRBs:=FieldByName('bs').AsInteger;
     FCBs:=0;
     FKBs:=FRBs;
    end
    else
    begin
     FKBs:=FieldByName('bs').AsInteger;
     FCBs:=FRBs-FKBs
    end;
    Label13.Caption:='选择包数：'+IntToStr(FRBs)+' 出库包数：'+IntToStr(FCBs)+' 库存包数：'+IntToStr(FKBs);
  end;
end;  
function  TfrmCpCkSaoMNewSel.ZD_YSData():Boolean;
var
  CRID,YFID,PriceUnit,OrderUnit,FComTaiTou,FromSubID:String;
  fprice,huilv:double;
begin
  Result:=False;
  fprice:=0;
  huilv:=0;
  PriceUnit:='RMB';
  CRID:='0';
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('update JYOrder_Sub SET Huilv=isnull((select Huilv from JYOrderCon_Sub X where X.SubId=JYOrder_Sub.SOrddefstr10),1) where Huilv=0');

    execsql;
  end;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select distinct A.MainId,A.SubId,A.CRTime,A.QtyUnit,B.OrderNo,B.MPrtCodeName,B.Mprtspec,B.customerNoName as KHName,C.*,fPrice=dbo.F_Get_Order_Price(A.MainID,A.subID) ');
    sql.Add('from CK_BanCP_CR A                                                                                                                                 ');
    sql.Add('inner join JYOrder_Main B on B.MainId=A.MainId                                                                                                     ');
    sql.Add('inner join JYOrder_Sub C on C.SubId=A.SubId                                                                                                        ');
    sql.Add('where CRType=''销售出库''                                                                                                                            ');
    sql.Add('and not exists(select MainId from YF_Money_CR X where X.MainId=A.MainId and X.SubId=A.SubId and X.CRTime=A.CRTime and X.YFType=''自动生成'' and X.CRType=''应收款登记'')  ');
    open;
  end;
  IF ADOQueryTemp.IsEmpty then exit;
  while not ADOQueryTemp.eof do
  begin
      PriceUnit:=Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
      OrderUnit:=Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
      fPrice:= ADOQueryTemp.fieldbyname('fPrice').AsFloat;
      huilv:= ADOQueryTemp.fieldbyname('huilv').AsFloat;
      IF huilv=0 then huilv:=1;
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add(' select * from YF_Money_CR where FactoryName='''+Trim(ADOQueryTemp.fieldbyname('KHName').AsString)+'''');
        sql.Add(' and mainID='''+Trim(ADOQueryTemp.fieldbyname('mainID').AsString)+'''');
        sql.Add(' and subID='''+Trim(ADOQueryTemp.fieldbyname('subID').AsString)+'''');
        sql.Add(' and CRTime='''+FormatDateTime('yyyy-MM-dd',ADOQueryTemp.fieldbyname('CRTime').AsDateTime)+'''') ;
        sql.Add(' and YFName=''销售金额'' ');
        Open;
      end;
      if ADOQuery1.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(ADOQueryTemp.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(ADOQueryTemp.fieldbyname('KHName').AsString);
          FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',ADOQueryTemp.fieldbyname('CRTime').AsDateTime);
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Price').Value:=fprice;
          FieldByName('HuiLv').Value:=huilv;
          FieldByName('BZType').Value:=Trim(PriceUnit);
          FieldByName('QtyUnit').Value:=Trim(ADOQueryTemp.fieldbyname('QtyUnit').AsString);
          FieldByName('ComTaiTou').Value:=Trim(FComTaiTou);
          FieldByName('YFName').Value:='销售金额';
          FieldByName('MainId').Value:=Trim(ADOQueryTemp.fieldbyname('Mainid').AsString);
          FieldByName('subID').Value:=Trim(ADOQueryTemp.fieldbyname('subID').AsString);
          FieldByName('OrderNO').Value:=Trim(ADOQueryTemp.fieldbyname('OrderNO').AsString);
          FieldByName('P_CodeName').Value:=Trim(ADOQueryTemp.fieldbyname('MprtCodeName').AsString);
          FieldByName('P_Spec').Value:=Trim(ADOQueryTemp.fieldbyname('MprtSpec').AsString);
          FieldByName('P_color').Value:=Trim(ADOQueryTemp.fieldbyname('PRTColor').AsString);
          FieldByName('status').Value:='0';
          Post;
        end;
      end
      else
      begin
        YFID:=Trim(ADOQuery1.fieldbyname('YFID').AsString);
      end;
      IF uppercase(OrderUnit)='KG' then
      begin
        with ADOQueryCmd do
        begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(B.MJQty4),0) from CK_BanCP_CR A,WFB_MJJY B where A.MJID=B.MJID and  A.MainID=YF_Money_CR.MainID and A.subID=YF_Money_CR.subID 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.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
        end;
      end
      else
      IF (uppercase(OrderUnit)='Y') and (uppercase(Trim(ADOQueryTemp.fieldbyname('QtyUnit').AsString))='M')  then
      begin
        with ADOQueryCmd do
        begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select cast(isnull(Sum(A.Qty),0)/0.9144 as decimal(18,2)) from CK_BanCP_CR A where A.MainID=YF_Money_CR.MainID and A.subID=YF_Money_CR.subID 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.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
        end;
      end
      else
      IF (uppercase(OrderUnit)='M') and (uppercase(Trim(ADOQueryTemp.fieldbyname('QtyUnit').AsString))='Y')  then
      begin
        with ADOQueryCmd do
        begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select cast(isnull(Sum(A.Qty),0)*0.9144 as decimal(18,2)) from CK_BanCP_CR A where A.MainID=YF_Money_CR.MainID and A.subID=YF_Money_CR.subID 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.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
        end;
      end
      else
      begin
        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.subID=YF_Money_CR.subID 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.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
        end;
      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.subID=YF_Money_CR.subID 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.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        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;
    ADOQueryTemp.next;
  end;

  Result:=true;
end;
function TfrmCpCkSaoMNewSel.YSData_other(Ado:TADOQuery;moneyFee:string;money:double):Boolean;
var
  CRID,YFID:string;
begin
  CRID:='0';
  Result:=False;
  with Ado do
  begin

      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add(' select * from YF_Money_CR where FactoryName='''+Trim(Ado.fieldbyname('customerNoName').AsString)+'''');
        sql.Add(' and mainID='''+Trim(Ado.fieldbyname('mainID').AsString)+'''');
        sql.Add(' and subID='''+Trim(Ado.fieldbyname('subID').AsString)+'''');
        sql.Add(' and YFName='''+trim(moneyFee)+''' ');
        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(Ado.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(Ado.fieldbyname('customerNoName').AsString);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRTime.Date));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Qty').Value:=1;
          FieldByName('Price').Value:=money;
          FieldByName('money').Value:=money;
          FieldByName('BBmoney').Value:=money;
          FieldByName('HuiLv').Value:=Ado.fieldbyname('huilv').AsFloat;
          FieldByName('BZType').Value:=Trim(Ado.fieldbyname('priceUnit').AsString);
          FieldByName('QtyUnit').Value:='次';
          FieldByName('ComTaiTou').Value:=Trim(Ado.fieldbyname('customerNoName').AsString);
          FieldByName('YFName').Value:=trim(moneyFee);
          FieldByName('MainId').Value:=Trim(Ado.fieldbyname('Mainid').AsString);
          FieldByName('subID').Value:=Trim(Ado.fieldbyname('subID').AsString);
          FieldByName('OrderNO').Value:=Trim(Ado.fieldbyname('conNo').AsString);
          FieldByName('P_CodeName').Value:=Trim(Ado.fieldbyname('prtCodeName').AsString);
          FieldByName('P_Spec').Value:=Trim(Ado.fieldbyname('prtSpec').AsString);
          FieldByName('P_color').Value:=Trim(Ado.fieldbyname('PRTColor').AsString);
          FieldByName('status').Value:='0';
          Post;
        end;
      end
      else
      begin
        YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
      end;

  end;
  Result:=True;
end;

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.*,cast(A.BaoNo as int) INTBaoNO,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 MJstr4 from WFB_MJJY where MJID=A.MJID)');
      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);
end;
procedure TfrmCpCkSaoMNewSel.InitPrice();
begin
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:='';
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\重复扫描.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\重复扫描.wav'),0, SND_ASYNC);
      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:='';
      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
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPDATE CK_BanCP_CR SET CRFlag=''出库'',CRType='+Quotedstr(trim(CRType.Text))+',FillTime=getdate() ');
          sql.add(',CRTime='''+trim(FormatDateTime('yyyy-MM-dd',CRTime.DateTime))+'''');
          sql.Add(',CKOrdNo='''+trim(CKOrdNo.Text)+'''');
          sql.Add(',CRNote='''+trim(CRNote.Text)+'''');
          sql.Add(',Filler='''+trim(DName)+'''');
          sql.Add(',tdNo='+quotedstr(Trim(tdNo.Text)));
          sql.Add(',jzxNo='+quotedstr(Trim(jzxNo.Text)));
          sql.Add(' FROM CK_BanCP_CR A ');
          sql.Add('where A.MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          sql.Add('and A.CRFlag=''待出库'' ');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update 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)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('exec P_Update_Order_JD_ByOther @MainType=''单卷出库'' ');
          sql.Add(',@OrdMainID='+quotedstr(Trim(CDS_Main.fieldbyname('MainId').AsString)));
          sql.Add(',@OrdSubID='+quotedstr(Trim(CDS_Main.fieldbyname('SubId').AsString)));
          ExecSQL;
        end;
      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:=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('INTBaoNO').Value:=CDS_Main.fieldbyname('INTBaoNO').Value;
        FieldByName('BaoID').Value:=CDS_Main.fieldbyname('BaoID').Value;
        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);
      InitBs('卷号扫描');
    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 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
  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('MPRTCode').Value:=Trim(CDS_OrderSel.fieldbyname('MPRTCode').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 orderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId), A.*,cast(A.BaoNo as int) INTBaoNO ');
    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 MJstr4 from WFB_MJJY where MJID=A.MJID)');
    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=''检验入库'' and A.CRFlag=''入库'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Main);
  SInitCDSData20(ADOQueryTemp,CDS_Main);
  SCreateCDS20(ADOQueryMain,CDS_MainSel);
  if GetLSNo(ADOQueryCmd,maxno,'CK','CK_BanCP_CR',3,1)=False then
  begin
    Application.MessageBox('取出库单号失败!','提示',0);
    Exit;
  end;
  CKOrdNo.Text:=Trim(maxno);
  InitBs('选单');
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;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    CDS_MainSel.DisableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
      SQL.Add('and MainType='+quotedstr('撤销出库') );
      ExecSQL;
    end;
    with CDS_MainSel do
    begin
      while CDS_MainSel.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' insert into TBValueWhere(DName,MainType,wsql1,wsql2) ');
          sql.Add('values('+quotedstr(Trim(DName))
          +','+quotedstr('撤销出库')
          +','+quotedstr(Trim(CDS_MainSel.fieldbyname('MJID').AsString))
          +','+quotedstr(Trim(CDS_MainSel.fieldbyname('CRID').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('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;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_BanCP_CR Set CRFlag=''待出库'',CRType='''',CRTime=NULL');
      sql.Add(',CRNote='''',CKOrdNo='''' from TBValueWhere A ');
      sql.Add(' where CK_BanCP_CR.CRID=A.wsql2 and A.MainType=''撤销出库'' and A.DName='+quotedstr(trim(DName)));
      sql.Add(' and CK_BanCP_CR.CRFlag=''出库''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      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('  FROM TBValueWhere A where CK_BanCp_KC.CRID=A.wsql2 and A.MainType=''撤销出库'' and A.DName='+quotedstr(trim(DName)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJStr2=''已出库'' FROM TBValueWhere A');
      SQL.Add(' where WFB_MJJY.MJID=A.wsql1 and A.MainType=''撤销出库'' and A.DName='+quotedstr(trim(DName)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Order_JD_ByOther @MainType=''撤销出库'' ');
      sql.Add(',@DName='+quotedstr(Trim(DName)));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.EnableControls;
    CDS_MainSel.EnableControls;
    MovePanel2.Visible:=false;
    InitBs('撤销出库');
    Exit;
  except
    MovePanel2.Visible:=False;
    CDS_Main.EnableControls;
    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
  CKOrdNo.SetFocus;
  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 Trim(CRType.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 ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
      SQL.Add('and MainType='+quotedstr('手工出库') );
      ExecSQL;
    end;
    with CDS_Main do
    begin
      while CDS_Main.Locate('SSel',True,[]) do
      begin
        application.ProcessMessages;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' insert into TBValueWhere(DName,MainType,Value1,Value2,Value3,Value4,Value5,Value6,wsql1,wsql2) ');
          sql.Add('values('+quotedstr(Trim(DName))
          +','+quotedstr('手工出库')
          +','+Quotedstr(trim(CRType.Text))
          +','+quotedstr(Trim(trim(FormatDateTime('yyyy-MM-dd',CRTime.DateTime))))
          +','+quotedstr(Trim(trim(CKOrdNo.Text)))
          +','+quotedstr(Trim(trim(CRNote.Text)))
          +','+quotedstr(Trim(trim(tdNo.Text)))
          +','+quotedstr(Trim(trim(jzxNo.Text)))
          +','+quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString))
          +','+quotedstr(Trim(CDS_Main.fieldbyname('CRID').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:=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('INTBaoNO').Value:=CDS_Main.fieldbyname('INTBaoNO').Value;
          FieldByName('BaoID').Value:=CDS_Main.fieldbyname('BaoID').Value;
          Post;
        end;
        CDS_Main.Delete;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_BanCP_CR SET CRFlag=''出库'',CRType=A.Value1,FillTime=getdate() ');
      sql.add(',CRTime=A.Value2,CKOrdNo=A.Value3,CRNote=A.Value4,tdNo=A.Value5,jzxNo=A.Value6,Filler='+quotedstr(trim(DName)));
      sql.Add('FROM TBValueWhere A where CK_BanCP_CR.MJID=A.wsql1 and CK_BanCP_CR.CRFlag=''待出库'' ');
      SQL.Add('and A.MainType=''手工出库'' and A.DName='+quotedstr(trim(DName)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_BanCp_KC set KCKgQty=0,KCQty=0 FROM TBValueWhere A ');
      SQL.Add(' where CK_BanCp_KC.CRID=A.wsql2 and A.MainType=''手工出库'' and A.DName='+quotedstr(trim(DName)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJStr2=''已出库'' FROM TBValueWhere A');
      SQL.Add(' where WFB_MJJY.MJID=A.wsql1 and A.MainType=''手工出库'' and A.DName='+quotedstr(trim(DName)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Order_JD_ByOther @MainType=''手工出库''');
      sql.Add(',@DName='+quotedstr(Trim(DName)));
      ExecSQL;
    end;
    CDS_MainSel.EnableControls;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    InitBs('手动出库');
  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;
  huilv:=0;
  PriceUnit:='RMB';
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' update JYOrder_Sub SET Huilv=isnull((select Huilv from JYOrderCon_Sub X where X.SubId=JYOrder_Sub.SOrddefstr10),1) where Huilv=0');
    sql.Add(' update JYOrder_Sub SET PRTPrice=(select PRTPrice  from JYOrderCon_Sub X where X.SubId=JYOrder_Sub.Sorddefstr10) ');
    sql.Add(' where subID='''+Trim(Order_Main10.fieldbyname('subID').AsString)+''' and ISNULL(Sorddefstr10,'''')<>'''' ');
    execsql;
  end;
  with Order_Main10 do
  begin
    begin
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select *,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;
      CRID:='0';
      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 subID='''+Trim(Order_Main10.fieldbyname('subID').AsString)+'''');
        sql.Add(' and CRTime='''+FormatDateTime('yyyy-MM-dd',CRTime.Date)+'''') ;
        sql.Add(' and YFName=''销售金额'' ');
        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('P_color').Value:=Trim(Order_Main10.fieldbyname('PRTColor').AsString);
          FieldByName('status').Value:='0';
          Post;
        end;
      end
      else
      begin
        YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
      end;
      IF uppercase(OrderUnit)='KG' then
      begin
        with ADOQueryCmd do
        begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(B.MJQty4),0) from CK_BanCP_CR A,WFB_MJJY B where A.MJID=B.MJID and  A.MainID=YF_Money_CR.MainID and A.subID=YF_Money_CR.subID 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.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
        end;
      end
      else
      IF (uppercase(OrderUnit)='Y') and (uppercase(Trim(Order_Main10.fieldbyname('QtyUnit').AsString))='M')  then
      begin
        with ADOQueryCmd do
        begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select cast(isnull(Sum(A.Qty),0)/0.9144 as decimal(18,2)) from CK_BanCP_CR A where A.MainID=YF_Money_CR.MainID and A.subID=YF_Money_CR.subID 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.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
        end;
      end
      else
      IF (uppercase(OrderUnit)='M') and (uppercase(Trim(Order_Main10.fieldbyname('QtyUnit').AsString))='Y')  then
      begin
        with ADOQueryCmd do
        begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select cast(isnull(Sum(A.Qty),0)*0.9144 as decimal(18,2)) from CK_BanCP_CR A where A.MainID=YF_Money_CR.MainID and A.subID=YF_Money_CR.subID 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.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
        end;
      end
      else
      begin
        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.subID=YF_Money_CR.subID 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.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
        end;
      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.subID=YF_Money_CR.subID 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.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        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.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:='';
      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;
      CDS_Main.DisableControls;
      CDS_MainSel.DisableControls;
      with CDS_Main do
      begin
        while  CDS_Main.Locate('baoID',Trim(bao.Text),[]) do
        begin
          with CDS_MainSel do
          begin
            Append;
            FieldByName('MainId').Value:=CDS_Main.fieldbyname('MainId').Value;
            FieldByName('SubId').Value:=Self.CDS_Main.fieldbyname('SubId').Value;
            FieldByName('OrderNo').Value:=Self.CDS_Main.fieldbyname('OrderNo').Value;
            FieldByName('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('INTBaoNO').Value:=CDS_Main.fieldbyname('INTBaoNO').Value;
            FieldByName('BaoID').Value:=CDS_Main.fieldbyname('BaoID').Value;
            Post;
          end;
          CDS_Main.Delete;
        end;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPDATE CK_BanCP_CR SET CRFlag=''出库'',CRType='+Quotedstr(trim(CRType.Text))+',FillTime=getdate() ');
        sql.add(',CRTime='''+trim(FormatDateTime('yyyy-MM-dd',CRTime.DateTime))+'''');
        sql.Add(',CKOrdNo='''+trim(CKOrdNo.Text)+'''');
        sql.Add(',CRNote='''+trim(CRNote.Text)+'''');
        sql.Add(',Filler='''+trim(DName)+'''');
        sql.Add(',tdNo='+quotedstr(Trim(tdNo.Text)));
        sql.Add(',jzxNo='+quotedstr(Trim(jzxNo.Text)));
        sql.Add('FROM CK_BanCP_CR A ');
        sql.Add('where A.BaoId='''+Trim(bao.Text)+'''');
        sql.Add('and A.CRFlag=''待出库'' ');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_BanCp_KC set KCKgQty=0,KCQty=0 ');
        sql.Add('where exists(select X.MJID from CK_BanCp_CR X inner join WFB_MJJY Y on X.MJID=Y.MJID  where  X.CRFlag=''出库'' and X.CRID=CK_BanCp_KC.CRID and X.CKOrdNo='+quotedstr(Trim(CKOrdNo.Text))+')');
        sql.Add('and  (CK_BanCp_KC.KCKgQty<>0 or CK_BanCp_KC.KCQty<>0 )');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update WFB_MJJY Set MJStr2=''已出库'' ');
        sql.Add('where exists(select X.MJID from CK_BanCp_CR X where X.MJID=WFB_MJJY.MJID and X.CRFlag=''出库'' and X.CKOrdNo='+quotedstr(Trim(CKOrdNo.Text))+')');
        sql.Add(' and MJStr2<>''已出库'' ');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Update_Order_JD_ByOther @MainType=''单包出库''');
        sql.Add(',@BaoId='+quotedstr(Trim(Bao.Text)));
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;

      bao.Text:='';
      CDS_Main.EnableControls;
      CDS_MainSel.EnableControls;
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav'),0, SND_ASYNC);
    except
      bao.Text:='';
      CDS_Main.EnableControls;
      CDS_MainSel.EnableControls;
      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;
    InitBs('包号扫描');
  end;

end;

procedure TfrmCpCkSaoMNewSel.Button6Click(Sender: TObject);
var
  maxno,fsj,Fsqdh:String;
begin
  try
    frmProductOrderNewList_CY_Sel:=TfrmProductOrderNewList_CY_Sel.Create(Application);
    with frmProductOrderNewList_CY_Sel do
    begin
      if ShowModal=1 then
      begin
        Fsqdh:=Trim(Order_Main.fieldbyname('MainId').AsString);
        self.CRNote.Text:=trim(Order_Main.FieldByName('MPRTBZNote').AsString);
        if trim(Order_Main.FieldByName('SQType').AsString)='供应商退货' then CRType.ItemIndex:=6;
        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 exists (select X.MainId from JYOrderCY_Main X inner join JYOrderCy_Sub Y on X.MainId=Y.MainId');
            SQL.Add(' Where A.OrderNo=Y.OrderNo and Y.SOrddefstr10=B.SubId and X.MainId='+quotedstr(Fsqdh)+')');
            Open;
          end;

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

        
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
          ExecSQL;
        end;
        
        with Self.CDS_Sub do
        begin
          DisableControls;
          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(self.CDS_Sub.fieldbyname('SubId').AsString);
              Parameters.ParamByName('Dname').Value:=Trim(DCode);
              ExecSQL;
            end;
            Self.CDS_Sub.Next;
          end;
          EnableControls;
        end;
        with Self.ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          sql.Add('select orderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId), A.*,cast(A.BaoNo as int) INTBaoNO ');
          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 MJstr4 from WFB_MJJY where MJID=A.MJID)');
          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=''检验入库'' and A.CRFlag=''入库'' ');
//          ShowMessage(SQL.Text);
          Open;
        end;
        SCreateCDS20(Self.ADOQueryTemp,Self.CDS_Main);
        SInitCDSData20(Self.ADOQueryTemp,Self.CDS_Main);
        SCreateCDS20(Self.ADOQueryMain,Self.CDS_MainSel);
        CKOrdNo.Text:=Trim(Fsqdh);
        InitBs('选单');
      end;
    end;
  finally
    frmProductOrderNewList_CY_Sel.Free;
  end;
end;

end.
