unit U_CKProductBCPOutList;

interface

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

type
  TfrmCKProductBCPOutList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    Label4: TLabel;
    MPRTCodeName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    v1Column3: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    Label10: TLabel;
    CkOrdNo: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    TBCKCX: TToolButton;
    v1Column11: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    v1Column12: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    PRTColor: TComboBox;
    Label11: TLabel;
    AOrdDefStr1: TComboBox;
    ComboBox1: TComboBox;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    MJXHM: TEdit;
    Label12: TLabel;
    CheckBox1: TCheckBox;
    Panel4: TPanel;
    Label13: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    conNo: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    KHConNo: TEdit;
    Label17: TLabel;
    apid: TEdit;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    PRTKuanNO: TEdit;
    Label18: TLabel;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    v1rkplace: TcxGridDBColumn;
    kuwei: TEdit;
    Label19: TLabel;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    PRTHX: TEdit;
    Label20: TLabel;
    v1SOrddefstr1: TcxGridDBColumn;
    SOrddefstr1: TEdit;
    Label21: TLabel;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    RM3: TRMGridReport;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    Panel3: TPanel;
    Label22: TLabel;
    tdNo1: TEdit;
    Label23: TLabel;
    jzxNo1: TEdit;
    Button1: TButton;
    Button2: TButton;
    ToolButton4: TToolButton;
    Label24: TLabel;
    Label25: TLabel;
    tdNo: TEdit;
    jzxNo: TEdit;
    CDS_MDMX: TClientDataSet;
    CDS_MDHJ: TClientDataSet;
    RMDBMDMX: TRMDBDataSet;
    RMDBMDHJ: TRMDBDataSet;
    Label14: TLabel;
    CRType: TComboBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBCKCXClick(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure PRTColorChange(Sender: TObject);
    procedure AOrdDefStr1Change(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
        FLeft,FTop:Integer;
    procedure InitGrid();
    function  YSData(Order_Main10:TClientDataSet):Boolean;

    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKProductBCPOutList: TfrmCKProductBCPOutList;

implementation
uses
  U_DataLink,U_Fun, U_JYMJIDPRINT;

{$R *.dfm}


procedure TfrmCKProductBCPOutList.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPOutList:=nil;
end;


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

procedure TfrmCKProductBCPOutList.FormCreate(Sender: TObject);
begin
  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductBCPOutList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,cast(A.baono as int) intbaono,B.OrderNo,B.conNO,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.PrtkuanNo,C.PRTColor,C.SOrddefstr1,C.SOrddefstr4,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTPrice,C.PRTHX ');
      sql.Add(',C.SOrddefstr4,D.MJQty3,D.MJQty4');
      sql.Add(',khconNo=(select Top 1 KHConNo from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
      sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
      sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM ');
      SQL.Add(',kuwei=(select TOP 1 rkplace from CK_BanCP_CR X where X.mjid=A.mjid and X.CRFlag=''入库'')');
      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.mainID=A.mainID and C.SubId=A.SubId');
      Sql.add(' inner join WFB_MJJY D on D.MJId=A.MJId');
    //  sql.Add(' inner join JYOrder_Sub_AnPai E on A.APID=E.APID');
      sql.add(' where A.CRTime>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+''' ');
      sql.Add(' and A.CRTime<'''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+''' ');
      SQL.Add(' and A.CRFlag=''出库'' ');
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCPOutList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCKProductBCPOutList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmCKProductBCPOutList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('仓库出库列表',Tv1,'成品仓库');
  Close;
end;

procedure TfrmCKProductBCPOutList.FormShow(Sender: TObject);
begin
  ReadCxGrid('仓库出库列表',Tv1,'成品仓库');
  //InitGrid();
  if Trim(DParameters2)='管理' then
  begin
    TBCKCX.Visible:=True;
  //  ToolButton1.Visible:=true;
  end;
end;

procedure TfrmCKProductBCPOutList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  SelExportData(Tv1,ADOQueryMain,'');
end;

procedure TfrmCKProductBCPOutList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCKProductBCPOutList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutList.TBPrintClick(Sender: TObject);
var
  fPrintFile,fPrintFile10,FMainID:String;
begin
  if CDS_Main.IsEmpty then Exit;
  IF trim(ComboBox1.Text)='' then  exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示',0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  FMainID:='';
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        If Fieldbyname('Ssel').AsBoolean then
        begin
          IF trim(ComboBox1.Text)<>'特殊码单' then
          begin
            IF FMainID='' then
            begin
              FMainID:=Trim(CDS_Main.fieldbyname('mainID').AsString);
            end
            else
            begin
              IF Trim(CDS_Main.fieldbyname('mainID').AsString)<>FMainID then
              begin
                application.MessageBox('选择的不是同一个指示单，不能一起打印！','提示信息',0);
                ADOQueryCmd.Connection.RollbackTrans;
                EnableControls;
                exit;
              end;
            end;
          end;

          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value:=Trim(CDS_Main.fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value:=Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;

  IF ((trim(ComboBox1.Text)='明细码单')or(trim(ComboBox1.Text)='重量码单')) then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD1 ''1'','+quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_HZ);
    SInitCDSData20(ADOQueryTemp,CDS_HZ);
  end;

  IF trim(ComboBox1.Text)='汇总码单' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD3 ''1'','+quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_HZ);
    SInitCDSData20(ADOQueryTemp,CDS_HZ);
  end;
  IF trim(ComboBox1.Text)='特殊码单' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD11 ''2'','+quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_MDHJ);
    SInitCDSData20(ADOQueryTemp,CDS_MDHJ);

     with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD11 ''1'','+quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_MDMX);
    SInitCDSData20(ADOQueryTemp,CDS_MDMX);
  end;

  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(ComboBox1.Text)+'.rmf' ;
  if FileExists(fPrintFile) then
  begin
     IF trim(ComboBox1.Text)='特殊码单' then
     begin
      RM2.LoadFromFile(fPrintFile);
      RM2.ShowReport;
     end
     else
     begin
      RM3.LoadFromFile(fPrintFile);
      RM3.ShowReport;
     end;

  end
  else
  begin
    Application.MessageBox(PChar('没有找'+fPrintFile),'提示',0);
  end;
end;



procedure TfrmCKProductBCPOutList.orderNoChange(Sender: TObject);
begin
//  if Length(orderNo.Text)<4 then Exit;
  TBFind.Click;
end;

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

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

procedure TfrmCKProductBCPOutList.TBCKCXClick(Sender: TObject);
var
  FFMainId,FPrice:String;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.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;
    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,[])=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_Main.fieldbyname('MJID').AsString))
          +','+quotedstr(Trim(CDS_Main.fieldbyname('CRID').AsString))
          +')');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' select YFTypeID from YF_Money_CR A');
      sql.Add('where exists(select CKOrdNo from CK_BanCP_CR X where X.CRFlag=''出库'' and A.YFTypeID=X.CKOrdNo and X.subId=A.subId and X.MainId=A.MainId  ');
      sql.Add(' and exists(select MJID from TBValueWhere Y where X.MJID=Y.wsql1 and Y.MainType=''撤销出库'' and Y.DName='+quotedstr(trim(DName)));
      sql.Add(' ))');
      open;
    end;

    if ADOQueryCmd.RecordCount>0 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      MovePanel2.Visible:=False;
      CDS_Main.EnableControls;
      Application.MessageBox('已产生费用不能撤销!','提示',0);
      InitGrid();
      Exit;
    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 A.KgQty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRFlag=''待出库'') ');//原A.CRType=''检验入库'' 2019-07-08 Roy改
      sql.Add(',KCQty=(select A.Qty from CK_BanCP_CR A where A.CRID=CK_BanCP_KC.CRID and A.CRFlag=''待出库'') ');  //原A.CRType=''检验入库'' 2019-07-08 Roy改
      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;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    Exit;
  except
    MovePanel2.Visible:=False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！','提示',0);
  end;
end;
function TfrmCKProductBCPOutList.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 subID='''+Trim(Order_Main10.fieldbyname('subID').AsString)+'''');
        sql.Add(' and CRTime='''+FormatDateTime('yyyy-MM-dd',Order_Main10.fieldbyname('CRTime').AsDateTime)+'''') ;
        sql.Add(' and YFName=''销售金额'' ');
       // 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',Order_Main10.fieldbyname('CRTime').AsDateTime));
          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;
      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 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 TfrmCKProductBCPOutList.orderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(orderNo.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        SQL.Add('select A.*,B.OrderNo,B.conNO,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.PrtkuanNo,C.PRTColor,C.SOrddefstr1,C.SOrddefstr4,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTPrice,C.PRTHX ');
        sql.Add(',C.SOrddefstr4,D.MJQty3,D.MJQty4');
        sql.Add(',khconNo=(select Top 1 KHConNo from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
        sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
        sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM ');
        sql.add('from CK_BanCP_CR A ');
        Sql.add(' inner join JYOrder_Main B on A.MainId=B.MainId');
        Sql.add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
        Sql.add(' inner join WFB_MJJY D on A.MJId=D.MJId');
        sql.add('where B.OrderNo like '''+'%'+Trim(orderNo.Text)+'%'+'''');
        SQL.Add(' and CRFlag=''出库'' ');
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmCKProductBCPOutList.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty=False then
  begin
    InitOrderColor(Trim(CDS_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
    InitBCGangNo(Trim(CDS_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
  end;
end;

procedure TfrmCKProductBCPOutList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
  if CDS_Main.IsEmpty=False then
  begin
    //InitOrderColor(Trim(CDS_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
    InitBCGangNo(Trim(CDS_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
  end;
end;

procedure TfrmCKProductBCPOutList.AOrdDefStr1Change(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutList.CheckBox1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,CheckBox1.Checked);
end;

procedure TfrmCKProductBCPOutList.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  Panel4.Visible:=true;
  Panel4.Left:=FLeft;
  Panel4.Top:=FTop+110;
  Panel10.Caption:=Trim(TV1.Controller.FocusedColumn.Caption);
  RichEdit1.Text:=CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
  application.ProcessMessages;
end;

procedure TfrmCKProductBCPOutList.Tv1MouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft:=X;
  FTop:=Y;
end;

procedure TfrmCKProductBCPOutList.Image2Click(Sender: TObject);
begin
 Panel4.Visible:=False;
end;

procedure TfrmCKProductBCPOutList.ToolButton3Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  with ADOqueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
    ExecSQL;
  end;
    with ADOqueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * FROM TBSubID where 1=2 ');
      open;
    end;
  ADOqueryCmd.Connection.BeginTrans;
  try
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value:=Trim(fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value:=Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOqueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  with ADOqueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Get_JYMJID_Main ');
    sql.Add(' @MJID='+quotedstr(''));
    sql.Add(',@flag=''0'' ');
    sql.Add(',@userID='+quotedstr(trim(DCode)));
    open;
  end;

  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\检验汇总信息.rmf';;
  if NOT FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',MB_ICONERROR);
    exit;
  end;
  Rm2.LoadFromFile(fPrintFile);
  Rm2.ShowReport;
end;

procedure TfrmCKProductBCPOutList.ToolButton2Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  frmJYMJIDPRINT:=TfrmJYMJIDPRINT.create(self);
  with frmJYMJIDPRINT do
  begin
    fMJID:=trim(self.CDS_Main.fieldbyname('MJID').AsString);
    fflag:=1;
    show;
  end;
end;

procedure TfrmCKProductBCPOutList.Button2Click(Sender: TObject);
begin
  Panel3.Visible:=false;
end;

procedure TfrmCKProductBCPOutList.Button1Click(Sender: TObject);
var
  FFMainId,FPrice:String;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示',0);
    Exit;
  end;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
      SQL.Add('and MainType='+quotedstr('修改提单') );
      ExecSQL;
    end;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if CDS_Main.FieldByName('SSel').AsBoolean=True then
        begin
          Edit;
          FieldByName('tdNo').Value:=trim(tdNo1.Text);
          FieldByName('jzxNo').Value:=trim(jzxNo1.Text);
          Post;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' insert into TBValueWhere(DName,MainType,Value1,Value2,wsql1,wsql2) ');
            sql.Add(' values( '+quotedstr(Trim(DName))
            +','+quotedstr('修改提单')
            +','+Quotedstr(trim(tdNo1.Text))
            +','+quotedstr(Trim(jzxNo1.Text))
            +','+quotedstr(Trim(CDS_Main.fieldbyname('CRID').AsString))
            +','+quotedstr('出库')
            +')');
            ExecSQL;
          end;
        end;
        Next;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPDATE CK_BanCP_CR SET  ');
        sql.add('tdNo=A.Value1,jzxNo=A.Value2');
        sql.Add('FROM TBValueWhere A where CK_BanCP_CR.CRID=A.wsql1 and CK_BanCP_CR.CRFlag=A.wsql2 ');
        SQL.Add('and A.MainType=''修改提单'' and A.DName='+quotedstr(trim(DName)));
        ExecSQL;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    Application.MessageBox('修改数据成功！','提示',0);
    Exit;
  except
    MovePanel2.Visible:=False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('修改数据异常！','提示',0);
  end;

end;

procedure TfrmCKProductBCPOutList.ToolButton4Click(Sender: TObject);
begin
  Panel3.Visible:=true;
end;

end.
