unit U_PKFKList;

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, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC, cxCheckBox, Menus;


type
  TfrmPKFKList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    ToolButton3: TToolButton;
    Panel1: TPanel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    FactoryName: TEdit;
    Label6: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1ShipDate: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column22: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    v1Column1: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ToolButton2: TToolButton;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    Label3: TLabel;
    PayMent: TEdit;
    Label4: TLabel;
    ComTaiTou: TEdit;
    v1Column18: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton5: TToolButton;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    ClientDataSet3: TClientDataSet;
    Label5: TLabel;
    v1Column4: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Huilv: TcxGridDBColumn;
    Label1: TLabel;
    BZType: TComboBox;
    Label7: TLabel;
    JBPerson: TEdit;
    Label8: TLabel;
    PKName: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);

  private
    FInt,PFInt:Integer;
    canshu1,canshu2:string;
    FNowDate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    function  SaveFKData():Boolean;
    function  SaveFKQTData():Boolean;
    function  HXYFK(FKID:string):Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPKFKList: TfrmPKFKList;

implementation
uses
  U_DataLink,U_RTFun,U_PKInPut,U_ModuleNote, U_ZDYHelp;

{$R *.dfm}

procedure TfrmPKFKList.FormDestroy(Sender: TObject);
begin
  frmPKFKList:=nil;
end;

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

procedure TfrmPKFKList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('资金申请付款列表',Tv1,'财务管理');

end;

procedure TfrmPKFKList.InitGrid();
var
  fsj:String;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.*');
      SQL.Add(' from YF_Money_PaiKuan A where 1=1 ');
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add('  and A.PKDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add('  and A.PKDate<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.Date+1))+'''');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add('  and A.FKTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add('  and A.FKTime<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.Date+1))+'''');
      end;

      sql.Add(' and isnull(ChkStatus,'''')=''审核通过'' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(FKID,'''')='''' ');
      end  else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(FKID,'''')<>'''' ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmPKFKList.InitForm();
begin
  FNowDate:=SGetServerDate(ADOQueryTemp);
  EndDate.Date:=FNowDate+7;
  BegDate.Date:=FNowDate-7;
  ReadCxGrid('资金申请付款列表',Tv1,'财务管理');
end;

function TfrmPKFKList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete YF_Money_PaiKuan  where PKId='''+Trim(Order_Main.fieldbyname('PKId').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmPKFKList.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('资金申请付款列表',cxGrid1);
end;

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

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

procedure TfrmPKFKList.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmPKFKList.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='资金申请付款';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmPKFKList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
end;
procedure TfrmPKFKList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmPKFKList.FactoryNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmPKFKList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmPKFKList.ToolButton4Click(Sender: TObject);
var
   fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>2 then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\资金审批表.rmf' ;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DName)+'''');
    ExecSQL;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into TBSubID');
          sql.Add(' select '''+Trim(Order_Main.fieldbyname('PKID').AsString)+'''');
          sql.Add(','''+Trim(DName)+'''');
          ExecSQL;
        end;
        Edit;
        FieldByName('PrtFlag').Value:=True;
        Post;
      end;
      Next;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_PaiKuan Set PrtFlag=1,PrtTime=getdate(),Prter='''+Trim(DName)+'''');
    sql.Add(' where PKID in(select SubId from TBSubID A where A.DName='''+Trim(DName)+''')');
    ExecSQL;
  end;
  Order_Main.EnableControls;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select AA.*,');
    sql.Add(' Case when isnull(FactoryNameOther,'''')<>'''' then RTrim(FactoryNameOther)+''/''+RTrim(FactoryName) ');
    sql.Add(' else FactoryNameQC end as FactoryNameHZ');
    sql.Add(' from (');
    sql.Add(' select A.PKID,A.ComTaiTou,A.PKYongTu,A.PayMent,A.FactoryNameOther,A.FactoryName,isnull(A.PKMoney,0) PKMoney,A.ChkNote  ');
    sql.Add(' ,isnull(A.BQKKMoney,0) BQKKMoney,isnull(A.HZKKMoney,0) HZKKMoney,isnull(A.BQYFMoney,0) BQYFMoney,isnull(A.HZYFMoney,0) HZYFMoney');
    sql.Add(',isnull(A.DDuiZhangMoney,0) DDuiZhangMoney,isnull(A.MaxHZYFMoney,0) MaxHZYFMoney,A.PKDate,A.Filler');
    sql.Add(' ,FYear=Rtrim(Convert(varchar(4),A.PKDate,120))+''年''');
    sql.Add(' ,FactoryNameQC=(select KHName from ZH_KH_Info B where B.KHNameJC=A.FactoryName and B.Type=''GYS'' ) ');
    sql.Add(' from YF_Money_PaiKuan A');
    sql.Add(' where PKID in(select SubId from TBSubID where DName='''+Trim(DName)+'''))AA');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['FMonth']:=Order_Main.fieldbyname('KQDate10').AsString;
    RM1.ShowPrintDialog:=True;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'资金审批表'),'提示',0);
  end;   

end;
procedure TfrmPKFKList.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmPKFKList.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,False);
end;

procedure TfrmPKFKList.ToolButton2Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Order_Main.Locate('SSel;FKMoney',VarArrayOf([True,null]),[loPartialKey])=True then
  begin
    Application.MessageBox('付款金额不能为空!','提示',0);
    Exit;
  end;
  if Order_Main.Locate('SSel;FKTime',VarArrayOf([True,null]),[loPartialKey])=True then
  begin
    Application.MessageBox('付款日期不能为空!','提示',0);
    Exit;
  end;
  if Order_Main.Locate('SSel;ComTaiTou',VarArrayOf([True,null]),[loPartialKey])=True then
  begin
    Application.MessageBox('付款单位不能为空!','提示',0);
    Exit;
  end;
  if Order_Main.Locate('SSel;BZType;HuiLv',VarArrayOf([True,'美金',null]),[loPartialKey])=True then
  begin
    Application.MessageBox('汇率不能为空!','提示',0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean=True then
      begin
        if FieldByName('FKMoney').Value=0 then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('付款金额不能为零!','提示',0);
          Exit;
        end;
        if FieldByName('FKMoney').Value>FieldByName('PKMoney').Value then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('付款金额不能大于申请金额!','提示',0);
          Exit;
        end;
        
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
  if Application.MessageBox('确定要付款吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel',True,[]) do
      begin
        if Trim(Order_Main.fieldbyname('HZType').AsString)='生产' then
        begin
          SaveFKData();
        end else
        begin
          SaveFKQTData();
        end;
        Order_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;
function TfrmPKFKList.SaveFKData():Boolean;
var
  maxId,CRID:String;
begin
  try
    //ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where FactoryNo='''+Trim(Order_Main.fieldbyname('FactoryNo').AsString)+'''');
      Open;
    end;
    if ADOQueryCmd.IsEmpty=False then
    begin
      CRID:=ADOQueryCmd.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('FactoryNo').Value:=Trim(Order_Main.fieldbyname('FactoryNo').AsString);
        FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('FactoryName').AsString);
        FieldByName('ZdyStr1').Value:='应付付';
        Post;
      end;
    end;
    if GetLSNo(ADOQueryCmd,maxId,'SF','YF_Money_CR',4,1)=False then
    begin
      Result:=False;
      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(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='付款登记';
      FieldByName('CRFlag').Value:='应付付';
      FieldByName('QtyFlag').Value:=-1;
      FieldByName('FactoryNo').Value:=Trim(Order_Main.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('FactoryName').AsString);
      FieldByName('CRTime').Value:=Order_Main.fieldbyname('FKTime').Value;
      FieldByName('Money').Value:=Order_Main.fieldbyname('FKMoney').Value;
      FieldByName('BBMoney').Value:=Order_Main.fieldbyname('FKMoney').Value;
      FieldByName('BZType').Value:='人民币';
      FieldByName('HuiLv').Value:=1;
      FieldByName('Note').Value:=Order_Main.fieldbyname('FKNote').Value;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
      sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
      sql.Add(' where CRID='+CRID);
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_PaiKuan Set FKPerson='''+Trim(DName)+'''');
      sql.Add(',FKID='''+Trim(maxId)+'''');
      sql.Add(',FKTime='''+Trim(FormatDateTime('yyyy-MM-dd',Order_Main.fieldbyname('FKTime').AsDateTime))+'''');
      sql.Add(',FKMoney='+Trim(Order_Main.fieldbyname('FKMoney').AsString));
      sql.Add(',FKNote='''+Trim(Order_Main.fieldbyname('FKNote').AsString)+'''');
      sql.Add(' where PKID='''+Trim(Order_Main.fieldbyname('PKID').AsString)+'''');
      ExecSQL;
    end;
    Result:=True;
    {if HXYFK(maxId)=True then
    begin
      Result:=True;
    end else
    begin
      Result:=False;
    end; }

  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存付款失败!','提示',0);
  end;
end;
function TfrmPKFKList.SaveFKQTData():Boolean;
var
  maxId:String;
begin
  try
    //ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'QF','YF_Money_CR',4,1)=False then
    begin
      Result:=False;
      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(maxId);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='付款登记';
      FieldByName('CRFlag').Value:='应付付';
      FieldByName('QtyFlag').Value:=-1;
      FieldByName('FactoryNo').Value:=Trim(Order_Main.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('FactoryName').AsString);
      FieldByName('CRTime').Value:=Order_Main.fieldbyname('FKTime').Value;
      FieldByName('Money').Value:=Order_Main.fieldbyname('FKMoney').Value;
      FieldByName('BBMoney').Value:=Order_Main.fieldbyname('FKMoney').Value
                                     *Order_Main.fieldbyname('HuiLv').Value;
      FieldByName('Note').Value:=Order_Main.fieldbyname('FKNote').Value;
      FieldByName('BZType').Value:=Order_Main.fieldbyname('BZType').Value;
      FieldByName('HuiLv').Value:=Order_Main.fieldbyname('HuiLv').Value;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_PaiKuan Set FKPerson='''+Trim(DName)+'''');
      sql.Add(',FKID='''+Trim(maxId)+'''');
      sql.Add(',FKTime='''+Trim(FormatDateTime('yyyy-MM-dd',Order_Main.fieldbyname('FKTime').AsDateTime))+'''');
      sql.Add(',FKMoney='+Trim(Order_Main.fieldbyname('FKMoney').AsString));
      sql.Add(',FKNote='''+Trim(Order_Main.fieldbyname('FKNote').AsString)+'''');
      sql.Add(',HuiLv='+Trim(Order_Main.fieldbyname('HuiLv').AsString));
      sql.Add(' where PKID='''+Trim(Order_Main.fieldbyname('PKID').AsString)+'''');
      ExecSQL;
    end;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存其他付款失败!','提示',0);
  end;
end;
function  TfrmPKFKList.HXYFK(FKID:string):Boolean;
var
  factoryname,FFKMoney,maxno,FXType:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select FXType,');
    sql.Add(' DHXMoney=Money-isnull((select isnull(Sum(HXMoney),0) from YF_Money_CR_HX B where B.FKID=A.YFID),0)');
    sql.Add(' from YF_Money_CR A');
    SQL.Add(' where YFID='''+Trim(FKID)+'''');
    Open;
  end;
  FFKMoney:=ADOQueryTemp.fieldbyname('DHXMoney').AsString;
  FXType:=Trim(ADOQueryTemp.fieldbyname('FXType').AsString);
  if StrToFloat(FFKMoney)>0 then
  begin
    if Trim(FXType)='' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.*,YingFuMoney=Money-YiFuMoney ');
        sql.Add(' ,DZMoney=(select isnull(Money,0) from JYOrder_Main_MD_HZ HZ where HZ.MHID=A.MHID)');
        sql.Add(' ,DZChaEMoney=(select isnull(ChaEMoney,0) from JYOrder_Main_MD_HZ HZ where HZ.MHID=A.MHID)');
        sql.Add(' from YF_Money_CR A ');
        sql.Add(' where FactoryName='''+Trim(Order_Main.fieldbyname('FactoryName').AsString)+'''');
        //SQL.Add(' and CRID='+Trim(ClientDataSet2.fieldbyname('CRID').AsString));
        sql.Add(' and CRType=''应付款登记'' ');
        sql.Add(' and (Money-YiFuMoney)<>0');
        sql.Add(' and not exists(select * from JYOrder_Main_MD_HZ C where C.YFID=A.YFID and isnull(C.ItemName,'''')=''返现'')');
        sql.Add(' order by CRTime,Money');
        Open;
      end;
    end else
    if Trim(FXType)='返现' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.*,YingFuMoney=Money-YiFuMoney ');
        sql.Add(' ,DZMoney=(select isnull(Money,0) from JYOrder_Main_MD_HZ HZ where HZ.MHID=A.MHID)');
        sql.Add(' ,DZChaEMoney=(select isnull(ChaEMoney,0) from JYOrder_Main_MD_HZ HZ where HZ.MHID=A.MHID)');
        sql.Add(' from YF_Money_CR A ');
        sql.Add(' where isnull(A.MHID,'''')='''+Trim(Order_Main.fieldbyname('MHID').AsString)+'''');
        sql.Add(' and CRType=''应付款登记'' ');
        sql.Add(' and (Money-YiFuMoney)<>0');
        Open;
      end;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet3);
    SInitCDSData20(ADOQueryTemp,ClientDataSet3);
    with ClientDataSet3 do
    begin
      First;
      while not Eof do
      begin
        if ClientDataSet3.FieldByName('DZMoney').Value<0 then
        begin
          if ClientDataSet3.FieldByName('DZChaEMoney').Value<>0 then
          begin
            Next;
          end;
        end;
        if ClientDataSet3.FieldByName('YingFuMoney').Value<=StrToFloat(FFKMoney) then  //如果应付金额小于等于付款金额
        begin
          if StrToFloat(FFKMoney)>0 then
          begin
            if GetLSNo(ADOQueryCmd,maxno,'YH','YF_Money_CR_HX',4,1)=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取核销流水号失败!','提示',0);
              Exit;
            end;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from YF_Money_CR_HX where 1<>1');
              Open;
            end;
            with ADOQueryCmd do
            begin
              Append;
              FieldByName('YHID').Value:=Trim(maxno);
              FieldByName('YFID').Value:=Trim(ClientDataSet3.fieldbyname('YFID').AsString);
              FieldByName('FKID').Value:=Trim(FKID);
              FieldByName('HXMoney').Value:=ClientDataSet3.FieldByName('YingFuMoney').Value;
              FieldByName('Filler').Value:=Trim(DName);
              Post;
            end;

            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update YF_Money_CR Set YiFuMoney=(select isnull(Sum(HXMoney),0) from YF_Money_CR_HX A where A.YFID=YF_Money_CR.YFID)');
              sql.Add(' where YFID='''+Trim(ClientDataSet3.fieldbyname('YFID').AsString)+'''');
              ExecSQL;
            end;
            FFKMoney:=FloatToStr(StrToFloat(FFKMoney)-ClientDataSet3.FieldByName('YingFuMoney').Value);
            ClientDataSet3.Delete;
          end else
          begin
            ClientDataSet3.Last;
          end;
        end else
        //如果应付金额大于付款金额
        begin
          if StrToFloat(FFKMoney)=0 then
          begin
            ClientDataSet3.Last;
          end;
          if GetLSNo(ADOQueryCmd,maxno,'YH','YF_Money_CR_HX',4,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取核销流水号失败!','提示',0);
            Exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR_HX where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('YHID').Value:=Trim(maxno);
            FieldByName('YFID').Value:=Trim(ClientDataSet3.fieldbyname('YFID').AsString);
            FieldByName('FKID').Value:=Trim(FKID);
            FieldByName('HXMoney').Value:=FFKMoney;
            FieldByName('Filler').Value:=Trim(DName);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update YF_Money_CR Set YiFuMoney=(select isnull(Sum(HXMoney),0) from YF_Money_CR_HX A where A.YFID=YF_Money_CR.YFID)');
            sql.Add(' where YFID='''+Trim(ClientDataSet3.fieldbyname('YFID').AsString)+'''');
            ExecSQL;
          end;
          FFKMoney:='0';
          ClientDataSet3.Delete;
          ClientDataSet3.Last;
        end;
      end;
    end;
    Result:=True;
  end;
end;
procedure TfrmPKFKList.ToolButton5Click(Sender: TObject);
var
  CRID:String;
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  {Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_PaiKuan ');
          sql.Add(' where PKID='''+Trim(Order_Main.fieldbyname('PKID').AsString)+'''');
          sql.Add(' and OKFlag=1 ');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('已确认，不能撤销!','提示',0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;}
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where YFID='''+Trim(Order_Main.fieldbyname('FKID').AsString)+'''');
          Open;
        end;
        CRID:=Trim(ADOQueryTemp.fieldbyname('CRID').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_PaiKuan Set FKTime=NUll,FKPerson=Null');
          if Trim(Order_Main.fieldbyname('BZType').AsString)='美金' then
          begin
            sql.Add(',HuiLv=Null ');
          end;
          sql.Add(',FKNote=Null,FKID=Null,FKMoney=Null ');
          sql.Add(' where PKID='''+Trim(Order_Main.fieldbyname('PKID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('delete YF_Money_CR where YFID='''+Trim(Order_Main.fieldbyname('FKID').AsString)+'''');
          ExecSQL;
        end;
        if Trim(CRID)<>'' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('UPdate YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
            sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
            sql.Add(' where CRID='+CRID);
            ExecSQL;
          end;
        end;
        Order_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmPKFKList.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Order_Main.IsEmpty then exit;
  if Trim(Order_Main.fieldbyname('BZType').AsString)='人民币' then
  begin
    v1Huilv.Options.Editing:=False;
  end else
  begin
    v1Huilv.Options.Editing:=True;
  end;
end;

end.
