unit U_YWJSChk;

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, Menus;

type
  TfrmYWJSChk = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton4: TToolButton;
    Label4: TLabel;
    Note: TEdit;
    Label3: TLabel;
    CRTime: TDateTimePicker;
    ADOQueryMain: TADOQuery;
    Label6: TLabel;
    HSPerson: TBtnEditA;
    Label12: TLabel;
    HuiLv: TEdit;
    Label16: TLabel;
    Edit4: TEdit;
    Label17: TLabel;
    Edit5: TEdit;
    Label18: TLabel;
    Edit6: TEdit;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column5: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Tv3Column1: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v0Column1: TcxGridDBColumn;
    v0Column2: TcxGridDBColumn;
    v0Column3: TcxGridDBColumn;
    Label1: TLabel;
    Edit7: TEdit;
    v2Column4: TcxGridDBColumn;
    Label5: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Label2: TLabel;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    v1Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    HelpNote: TRichEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Tv3CellClick(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,canshu1:String;

  end;

var
  frmYWJSChk: TfrmYWJSChk;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GSListBankSel,U_KHListSelJJ;

{$R *.dfm}



procedure TfrmYWJSChk.InitData();
var
  FFYFName:String;
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.BankNo,B.BankName,B.HuMingJC ');
    sql.Add('  from YS_Money_CR A');
    sql.Add(' left join KH_Sub_Bank B on A.BKNO=B.BKNO');
    sql.Add(' left join KH_Main C on B.KHMainId=C.KHMainId');
    sql.Add(' where YFID='''+Trim(FMainId)+'''');
    Open;
  end;
  FFYFName:=Trim(ADOQueryMain.fieldbyname('FKType').AsString);

  SCSHDataNew(ADOQueryMain,Panel1,2);
  SCSHDataNew(ADOQueryMain,Panel1,0);
 
end;


procedure TfrmYWJSChk.FormDestroy(Sender: TObject);
begin
  frmYWJSChk:=nil;
end;

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

procedure TfrmYWJSChk.TBCloseClick(Sender: TObject);
begin
  Close;
end;
procedure TfrmYWJSChk.FormShow(Sender: TObject);
begin
  CRTime.Date:=SGetServerDate(ADOQueryTemp);
  if Trim(canshu1)='' then
  begin
    HSPerson.Enabled:=False;
  end else
  begin
    HSPerson.Enabled:=True;
  end;
  HSPerson.Text:=Trim(DName);
 { if Trim(FMainId)<>'' then
  begin
    InitData();
  end; }
end;    

function TfrmYWJSChk.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,'YU','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:='未审核';
      end else
      begin
        Edit;
      end;
      FieldByName('YFID').Value:=Trim(maxId);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='客户费用管理';
      FieldByName('CRFlag').Value:='费用管理';
      

      RTSetsavedata(ADOQueryCmd,'YS_Money_CR',Panel1,2);
      Post;
    end;
    FMainId:=Trim(maxId);
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmYWJSChk.ToolButton4Click(Sender: TObject);
var
  FReal:Double;
begin
  ToolBar1.SetFocus;


  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;


procedure TfrmYWJSChk.N1Click(Sender: TObject);
var
  Freal:Double;
  FBegdate,FEndDate:String;
begin
  if Trim(HuiLv.Text)='' then
  begin
    Application.MessageBox('汇率不能为空!','提示',0);
    Exit;
  end;
  if TryStrToFloat(HuiLv.Text,Freal)=False then
  begin
    Application.MessageBox('汇率非法数字!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1* from YW_HS_CR where HSPerson='''+Trim(DName)+''' and QtyFlag=1 ');
    sql.Add('order by CRDate desc ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    FBegdate:='2020-01-01';
  end else
  begin
    FBegdate:=Trim(FormatDateTime('yyyy-MM-dd',ADOQueryTemp.fieldbyname('CRDate').AsDateTime+1));
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_KH_QK :FBegDate,:DName,:HuiLv');
    Parameters.ParamByName('FBegDate').Value:=FBegdate;
    Parameters.ParamByName('DName').Value:=Trim(DName);
    Parameters.ParamByName('HuiLv').Value:=StrToFloat(HuiLv.Text);
    open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_KH_ChongHong :FBegDate,:DName,:HuiLv');
    Parameters.ParamByName('FBegDate').Value:=FBegdate;
    Parameters.ParamByName('DName').Value:=Trim(DName);
    Parameters.ParamByName('HuiLv').Value:=StrToFloat(HuiLv.Text);
    open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);
end;

procedure TfrmYWJSChk.Tv3CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if ClientDataSet3.IsEmpty then
  begin
    HelpNote.Text:='';
  end else
  begin
    HelpNote.Text:=Trim(ClientDataSet3.fieldbyname('Note').AsString);
  end;
end;

end.
