unit U_ProductOrderMoneyList_GXCX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxDropDownEdit, cxTextEdit, cxButtonEdit,cxPC, jpeg,
  IdBaseComponent, IdComponent,IdTCPConnection, IdTCPClient, IdFTP,ShellAPI,IniFiles;

type
  TfrmProductOrderMoneyList_GXCX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    v1OrderNo: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Chker: TClientDataSet;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu3: TcxGridPopupMenu;
    ADOQuery1: TADOQuery;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    SaveDialog1: TSaveDialog;
    ADOQuery2: TADOQuery;
    v1PSXH: TcxGridDBColumn;
    v1PSName: TcxGridDBColumn;
    v1PSSH: TcxGridDBColumn;
    v1PSNote: TcxGridDBColumn;
    Panel1: TPanel;
    Label3: TLabel;
    OrderNo: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure v1OrdDateCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure v1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ReadINIFile();
    procedure cxTabControl1Change(Sender: TObject);
  private
    canshu1,canshu2,canshu3:String;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridFH();
    procedure InitImage();
    procedure setstatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    FWFPMoney,FWSXMoney:Integer;
    FConNO:string;
    { Public declarations }
  end;

var
  frmProductOrderMoneyList_GXCX: TfrmProductOrderMoneyList_GXCX;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun, U_ZDYHelp, U_ZDYHelpSel,U_ContractListSel,
  U_Order_KCTX;

{$R *.dfm}
procedure TfrmProductOrderMoneyList_GXCX.setstatus();
begin
  
end;

procedure TfrmProductOrderMoneyList_GXCX.ReadINIFile();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;


procedure TfrmProductOrderMoneyList_GXCX.InitImage();
begin

end;

procedure TfrmProductOrderMoneyList_GXCX.FormDestroy(Sender: TObject);
begin
  frmProductOrderMoneyList_GXCX:=nil;
end;

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

procedure TfrmProductOrderMoneyList_GXCX.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
  canshu3:=Trim(DParameters3);
  DQdate:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmProductOrderMoneyList_GXCX.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('指示单列表FF',Tv1,'生产指示单管理');
end;

procedure TfrmProductOrderMoneyList_GXCX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.* ');
      sql.add('from JYOrder_Main A ');
      sql.Add('inner join JYOrder_Process B on A.Mainid=B.Mainid ');
      sql.add('where ConNO='''+trim(FConNO)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderMoneyList_GXCX.InitGridFH();
begin
  
end;

procedure TfrmProductOrderMoneyList_GXCX.InitForm();
begin
  ReadCxGrid('指示单列表FF',Tv1,'生产指示单管理');
  setstatus();
  InitGrid();
end;

procedure TfrmProductOrderMoneyList_GXCX.TBFindClick(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

function TfrmProductOrderMoneyList_GXCX.DelData():Boolean;
begin
  
end;

procedure TfrmProductOrderMoneyList_GXCX.TBPrintClick(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
   i,j:Integer;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.FieldByName('ChkStatus').AsString)<>'审核通过' then
  begin
    RM1.PreviewButtons:=[pbExit];
  end else
  begin
    RM1.PreviewButtons:=[pbZoom,pbLoad,pbSave,pbPrint,pbFind,pbPageSetup,pbExit,pbExport,pbNavigator];
  end;
  if Trim(Order_Main.fieldbyname('JYType').AsString)='' then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  end else
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\包装指示单V2.rmf' ;
  end;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,ColorCount=(select isnull(Count(*),0) from JYOrder_Sub where MainId=A.MainId),  ');
    sql.add('OrderUnit=(select Top 1 OrderUnit from JYOrder_Sub where MainId=A.MainId),');
    sql.add('SHICHANG=(select top 1 E.ShiChang from KH_QuYu E inner join JYOrderCon_Main B on isnull(isnull(E.GuoJiaEng,E.GuoJia),E.GanKouEng)=B.ToPlace ');
    sql.add('inner join JYOrderCon_Sub C on B.Mainid=C.Mainid where C.Subid=A.ConSubid),');
    sql.add('ZQty=(select sum(PRTOrderQty) from JYOrder_Sub where MainId=A.MainId)');
    //sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId ');
    sql.Add(' from JYOrder_Main A  ');
    sql.Add(' where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    if Trim(Order_Main.fieldbyname('JYType').AsString)='' then
      Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0)
    else
      Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\包装指示单V2.rmf'),'提示',0);
  end;
  //Panel4.Visible:=True;
end;

procedure TfrmProductOrderMoneyList_GXCX.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderMoneyList_GXCX.OrderNoChange(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmProductOrderMoneyList_GXCX.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmProductOrderMoneyList_GXCX.TBTPClick(Sender: TObject);
 var
   FQty,FQty1,FMxQty,FPQty,FMxQtyS,FPQtyS:String;
begin
end;

procedure TfrmProductOrderMoneyList_GXCX.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderMoneyList_GXCX.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmProductOrderMoneyList_GXCX.v1OrdDateCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate10(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate10(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmProductOrderMoneyList_GXCX.N1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单10.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单10.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrderMoneyList_GXCX.N2Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrderMoneyList_GXCX.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
    try
      frmOrderInPut:=TfrmOrderInPut.Create(Application);
      with frmOrderInPut do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        ToolBar2.Visible:=False;
        ToolBar3.Visible:=False;
        ToolBar4.Visible:=False;
        TBSave.Visible:=False;
        ScrollBox1.Enabled:=False;
        //Tv1.OptionsSelection.CellSelect:=False;
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmOrderInPut.Free;
    end;
end;

procedure TfrmProductOrderMoneyList_GXCX.OrderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(OrderNo.Text)<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.add('exec P_View_Order :begdate,:enddate,:WSql') ;
        if Trim(canshu1)<>'高权限'  then
        begin
          Parameters.ParamByName('WSql').Value:=' and FillCode='''+Trim(DCode)+'''';
        end;
        begin
          Parameters.ParamByName('WSql').Value:=' and orderno like '''+'%'+Trim(OrderNo.Text)+'%'+'''';
        end;
        Parameters.ParamByName('begdate').Value:='1899-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        ExecSQL;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderMoneyList_GXCX.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from JYOrder_Sub');
    sql.Add(' where Mainid='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

procedure TfrmProductOrderMoneyList_GXCX.Button4Click(Sender: TObject);
var
  maxsubno:String;
  FRLMoney,FSXMoney:double;
begin
  if ClientDataSet2.IsEmpty then Exit;
  if ClientDataSet2.Locate('FPDate',Null,[]) then
  begin
    Application.MessageBox('认领日期不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('FPType',Null,[]) then
  begin
    Application.MessageBox('类型不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('FPMoney',Null,[]) then
  begin
    Application.MessageBox('认领金额不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  Panel1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存子表
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('FPId').AsString)='' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select Sum(FPMoney) SumFPMoney,Sum(SXMoney) SumSXMoney,Sum(OtherMoney) SumOtherMoney from SK_Money_FP where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
            Open;
          end;
          FRLMoney:=ADOQueryCmd.fieldbyname('SumFPMoney').AsCurrency+ClientDataSet2.fieldbyname('FPMoney').AsCurrency+ADOQueryCmd.fieldbyname('SumOtherMoney').AsCurrency+ClientDataSet2.fieldbyname('OtherMoney').AsCurrency;
          FSXMoney:=ADOQueryCmd.fieldbyname('SumSXMoney').AsCurrency+ClientDataSet2.fieldbyname('SXMoney').AsCurrency;
          if FRLMoney>ClientDataSet2.FieldByName('RZMoney').asfloat then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('认领总金额超过入账金额','提示');
            exit;
          end;
          if FSXMoney>ClientDataSet2.FieldByName('SJSXMoney').asfloat then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('认领总手续费超过手续费','提示');
            exit;
          end;
          if GetLSNo(ADOQueryTemp,maxsubno,'FP','SK_Money_FP',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxsubno:=Trim(ClientDataSet2.fieldbyname('FPId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from SK_Money_FP where FPId='''+Trim(maxsubno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('FPId').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end
          else begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('FPId').Value:=Trim(maxsubno);
          FieldByName('YFID').Value:=Trim(ClientDataSet2.fieldbyname('YFID').AsString);
          FieldByName('FPTYpe').Value:=ClientDataSet2.fieldbyname('FPTYpe').Value;
          FieldByName('FPDate').Value:=ClientDataSet2.fieldbyname('FPDate').Value;
          FieldByName('WBID').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
          if Trim(ClientDataSet2.fieldbyname('FPMoney').AsString)='' then
          begin
            fieldbyname('FPMoney').Value:=0
          end else
          begin
            fieldbyname('FPMoney').Value:=Trim(ClientDataSet2.fieldbyname('FPMoney').AsString);
          end;
          fieldbyname('SXMoney').Value:=CLientDataSet2.fieldbyname('SXMoney').AsFloat;
          fieldbyname('OtherMoney').Value:=CLientDataSet2.fieldbyname('OtherMoney').AsFloat;
          FieldByName('Note').Value:=Trim(ClientDataSet2.fieldbyname('Note').AsString);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_Main Set DJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_Main.MainID and A.FPType=''定金'') ');
          sql.Add(',HKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_Main.MainID and A.FPType=''货款'') ');
          sql.Add(',SXMoney=(select Sum(SXMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_Main.MainID) ');
          sql.add(',JYOtherMoney=(select Sum(OtherMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_Main.MainID)');
          sql.add(',SCHuiLv=(Case when (select Top 1 C.HuiLv from YF_Money_CR C inner join SK_Money_FP A on C.YFID=A.YFID ');
          sql.add(' where A.WBID=JYOrder_Main.MainID and C.CRType=''收款登记'' and C.BZType=''$'' Order by A.FPDate desc)<>0 then ');
          sql.add(' (select Top 1 C.HuiLv from YF_Money_CR C inner join SK_Money_FP A on C.YFID=A.YFID ');
          sql.add(' where A.WBID=JYOrder_Main.MainID and C.CRType=''收款登记'' and C.BZType=''$'' Order by A.FPDate desc)*1.17-0.35 else NULL end)');
          sql.Add(' where MainID='''+Trim(Order_Main.fieldbyname('MainID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR Set FPDJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''定金'' and (isnull(A.WBID,'''')='''' or isnull(A.TTID,'''')='''')),');
          sql.Add('YSXMoney=(select Sum(SXMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and (isnull(A.WBID,'''')='''' or isnull(A.TTID,'''')='''' or A.FPType=''货款'')), ');
          sql.Add('TKMoney=(select Sum(OtherMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and (isnull(A.WBID,'''')='''' or isnull(A.TTID,'''')='''' or A.FPType=''货款'')) ');
          sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR Set FPHKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''货款'') ');
          sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CR Set WFPMoney=isnull(RZMoney,0)-isnull(FPDJMoney,0)-isnull(FPHKMoney,0)-isnull(TKMoney,0) ');
          sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        ClientDataSet2.Edit;
        ClientDataSet2.FieldByName('FPId').Value:=Trim(maxsubno);
        //ClientDataSet2.Post;
        FRLMoney:=0;
        FSXMoney:=0;
        Next;
      end;
    end;
    //initGrid();
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;


procedure TfrmProductOrderMoneyList_GXCX.Button3Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(ClientDataSet2.fieldbyname('FPID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SK_Money_FP where FPID='''+Trim(ClientDataSet2.fieldbyname('FPID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrder_Main Set DJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.WBID=JYOrder_Main.MainID and A.FPType=''定金'' )');
      sql.Add(',HKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.WBID=JYOrder_Main.MainID and A.FPType=''货款'' )');
      sql.add(',SXMoney=(select Sum(SXMoney) from SK_Money_FP A ');
      sql.Add(' where A.WBID=JYOrder_Main.MainID) ');
      sql.Add('where MainId='''+Trim(Order_Main.fieldbyname('MainID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate YF_Money_CR Set FPDJMoney=(select isnull(Sum(FPMoney),0) from SK_Money_FP A ');
      sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''定金'' )');
      sql.Add(',FPHKMoney=(select isnull(Sum(FPMoney),0) from SK_Money_FP A ');
      sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''货款'' )');
      sql.add(',YSXMoney=(select isnull(Sum(SXMoney),0) from SK_Money_FP A ');
      sql.Add(' where A.YFID=YF_Money_CR.YFID) ');
      sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set WFPMoney=isnull(Money,0)-isnull(FPDJMoney,0)-isnull(FPHKMoney,0)-isnull(SXMoney,0)');
      sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet2.Delete;
end;

procedure TfrmProductOrderMoneyList_GXCX.v1Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:String;
begin
  if Trim(canshu1)<>'高权限' then Exit;
  mvalue:=TcxTextEdit(Sender).EditingText;
  with Order_Main do
  begin
    Edit;
    FieldByName('OrdPerson2').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main Set OrdPerson2='''+Trim(mvalue)+''',EditTime=getdate(),Editer='''+Trim(DName)+'''');
    sql.Add(' where Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmProductOrderMoneyList_GXCX.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  InitGrid();
end;

end.
