unit U_FuFeeInPut;

interface

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

type
  TfrmFuFeeInPut = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton4: TToolButton;
    Label4: TLabel;
    Note: TEdit;
    Label12: TLabel;
    JSMoney: TEdit;
    Label3: TLabel;
    CRTime: TDateTimePicker;
    Label5: TLabel;
    Label6: TLabel;
    YFName: TBtnEditC;
    ADOQueryMain: TADOQuery;
    FKBankNo: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    SKTaiTou: TBtnEditC;
    SKBankNo: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolButton1: TToolButton;
    CDS_MX: TClientDataSet;
    DS_HZ: TDataSource;
    Panel2: TPanel;
    Label10: TLabel;
    BZType: TComboBox;
    FKTaiTou: TBtnEditC;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure YFNameBtnUpClick(Sender: TObject);
    procedure YFNameBtnDnClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FKTaiTouBtnUpClick(Sender: TObject);
    procedure SKTaiTouBtnUpClick(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }
    FKType:string;
    procedure InitData();
    function  SaveData():Boolean;

  public
    { Public declarations }
    FMainId:String;

  end;

var
  frmFuFeeInPut: TfrmFuFeeInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GSListBankSel,U_GYSListSelJJ,U_KHFeeList,U_GYSListSel;

{$R *.dfm}



procedure TfrmFuFeeInPut.InitData();
var
  FFYFName:String;
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select A.* ');
    sql.Add('  from YS_Money_CR A');
    sql.Add(' where YFID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQueryMain,Panel1,2);
  SCSHDataNew(ADOQueryMain,Panel2,2);

end;


procedure TfrmFuFeeInPut.FormDestroy(Sender: TObject);
begin
  frmFuFeeInPut:=nil;
end;

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

procedure TfrmFuFeeInPut.TBCloseClick(Sender: TObject);
begin
  Close;
end;
procedure TfrmFuFeeInPut.FormShow(Sender: TObject);
begin
  CRTime.Date:=SGetServerDate(ADOQueryTemp);
  if Trim(FMainId)<>'' then
  begin
    InitData();

  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR  ');
    if Trim(FMainId)<>'' then
    begin
      sql.Add(' where isnull(FeeFKID,'''')='''+Trim(FMainId)+'''');
    end else
    begin
      sql.Add(' where 1=2');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_MX);
  SInitCDSData20(ADOQueryTemp,CDS_MX);
  if CDS_MX.IsEmpty=False then
  begin
    BZType.Enabled:=False;
  end;
end;

function TfrmFuFeeInPut.SaveData():Boolean;
var
  maxId:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CR where YFID='''+Trim(FMainId)+'''');
      Open;
    end;
    FMainId:=Trim(ADOQueryCmd.fieldbyname('YFID').AsString);
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxId,'FF','YS_Money_CR',4,1)=False then
      begin
        Result:=False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！','提示',0);
        Exit;
      end;
    end else
    begin
      maxId:=FMainId;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_CR where YFID='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
        FieldByName('ChkStatusP').Value:='未审核';
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('YFID').Value:=Trim(maxId);

      FieldByName('CRType').Value:='客户费用付款管理';
      FieldByName('CRFlag').Value:='费用付款管理';
      FieldByName('BZType').Value:=Trim(BZType.Text);
      RTSetsavedata(ADOQueryCmd,'YS_Money_CR',Panel1,2);
      Post;
    end;
    FMainId:=Trim(maxId);
    CDS_MX.DisableControls;
    with CDS_MX do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YS_Money_CR Set FeeFKID='''+Trim(FMainId)+''',FKStatus=''已付款'' ');
          sql.Add('where YFID='''+Trim(CDS_MX.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        Next;
      end;
    end;
    CDS_MX.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmFuFeeInPut.ToolButton4Click(Sender: TObject);
var
  FReal:Double;
begin
  ToolBar1.SetFocus;
  if Trim(FKTaiTou.Text)='' then
  begin
    Application.MessageBox('付款抬头不能为空!','提示',0);
    Exit;
  end;
  if Trim(SKTaiTou.Text)='' then
  begin
    Application.MessageBox('收款抬头不能为空!','提示',0);
    Exit;
  end;
  if Trim(BZType.Text)='' then
  begin
    Application.MessageBox('币种不能为空!','提示',0);
    Exit;
  end;
  if Trim(JSMoney.Text)<>'' then
  begin
    if TryStrToFloat(JSMoney.Text,FReal)=False then
    begin
      Application.MessageBox('金额非法数字!','提示',0);
      Exit;
    end;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;


procedure TfrmFuFeeInPut.YFNameBtnUpClick(Sender: TObject);
begin
   try
      frmZDYHelp:=TfrmZDYHelp.Create(Application);
      with frmZDYHelp do
      begin
        flag:='YFNameKH';
        flagname:='项目摘要';
        if ShowModal=1 then
        begin
          YFName.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    finally
      frmZDYHelp.Free;
    end;
end;

procedure TfrmFuFeeInPut.YFNameBtnDnClick(Sender: TObject);
begin
  yfname.Text:='';
end;

procedure TfrmFuFeeInPut.ToolButton1Click(Sender: TObject);
var
  FYFID:String;
begin
  if Trim(BZType.Text)='' then
  begin
    Application.MessageBox('币种不能为空!','提示',0);
    Exit;
  end;
  try
    frmKHFeeList:=TfrmKHFeeList.Create(Application);
    with frmKHFeeList do
    begin
      FBZType:=Trim(Self.BZType.Text);
      if ShowModal=1 then
      begin
        with frmKHFeeList.CDS_HZ do
        begin
          First;
          while Locate('SSel',True,[]) do
          begin
            FYFID:=Trim(CDS_HZ.fieldbyname('YFID').AsString);
            if CDS_MX.Locate('YFID',Trim(FYFID),[])=False then
            begin
              with CDS_MX do
              begin
                Append;
                CDS_MX.FieldByName('YFID').Value:=CDS_HZ.fieldbyname('YFID').Value;
                CDS_MX.FieldByName('CRTime').Value:=CDS_HZ.fieldbyname('CRTime').Value;
                CDS_MX.FieldByName('ConNo').Value:=CDS_HZ.fieldbyname('ConNo').Value;
                CDS_MX.FieldByName('FactoryNo').Value:=CDS_HZ.fieldbyname('FactoryNo').Value;
                CDS_MX.FieldByName('FactoryName').Value:=CDS_HZ.fieldbyname('FactoryName').Value;
                CDS_MX.FieldByName('FKType').Value:=CDS_HZ.fieldbyname('FKType').Value;
                CDS_MX.FieldByName('JSMoney').Value:=CDS_HZ.fieldbyname('JSMoney').Value;
                CDS_MX.FieldByName('YFName').Value:=CDS_HZ.fieldbyname('YFName').Value;
                CDS_MX.FieldByName('ChkStatusP').Value:=CDS_HZ.fieldbyname('ChkStatusP').Value;
                CDS_MX.FieldByName('Note').Value:=CDS_HZ.fieldbyname('Note').Value;
                Post;
              end;
            end;
            CDS_HZ.Delete;
          end;
        end;
      end;
    end;
  finally
    frmKHFeeList.free;
  end;
  if CDS_MX.IsEmpty=False then
  begin
    BZType.Enabled:=False;
  end;
end;

procedure TfrmFuFeeInPut.FKTaiTouBtnUpClick(Sender: TObject);
begin
  try
    frmGSListBankSel:=TfrmGSListBankSel.Create(Application);
    with frmGSListBankSel do
    begin
      if ShowModal=1 then
      begin
        FKBankNo.Text:=Trim(frmGSListBankSel.CDS_HZ3.fieldbyname('BankNo').AsString);
        FKTaiTou.Text:=Trim(frmGSListBankSel.CDS_HZ3.fieldbyname('HuMing').AsString);
      end;
    end;
  finally
    frmGSListBankSel.Free;
  end;
end;

procedure TfrmFuFeeInPut.SKTaiTouBtnUpClick(Sender: TObject);
begin
  try
    frmGYSListSel:=TfrmGYSListSel.Create(Application);
    with frmGYSListSel do
    begin
      if ShowModal=1 then
      begin
        SKTaiTou.Text:=Trim(frmGYSListSel.CDS_HZ3.fieldbyname('HuMing').AsString);
        SKBankNo.Text:=Trim(frmGYSListSel.CDS_HZ3.fieldbyname('BankNo').AsString);
      end;
    end;
  finally
    frmGYSListSel.Free;
  end;
end;

procedure TfrmFuFeeInPut.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_MX.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YS_Money_CR Set FeeFKID=Null,FKStatus=Null ');
    sql.Add('where YFID='''+Trim(CDS_MX.fieldbyname('YFID').AsString)+'''');
    ExecSQL;
  end;
  CDS_MX.Delete;
end;

end.
