unit U_BankFlowInInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, DB, ADODB, cxControls, cxContainer, cxEdit,
  cxTextEdit, cxCurrencyEdit, BtnEdit, StdCtrls, ExtCtrls, cxGraphics,
  cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmBankFlowInInput = class(TForm)
    ToolBar1: TToolBar;
    TSAVE: TToolButton;
    Tclose: TToolButton;
    Panel1: TPanel;
    Label11: TLabel;
    Label5: TLabel;
    Label2: TLabel;
    note: TMemo;
    BFTime: TDateTimePicker;
    ADOQueryTMP: TADOQuery;
    ADOQueryCMD: TADOQuery;
    Amount: TcxCurrencyEdit;
    Label3: TLabel;
    Label4: TLabel;
    Label7: TLabel;
    ToolButton1: TToolButton;
    OppCoName: TBtnEditC;
    OurBKName: TBtnEditC;
    OppBKName: TBtnEditC;
    procedure TcloseClick(Sender: TObject);
    procedure TSAVEClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure OppCoNameBtnUpClick(Sender: TObject);
    procedure OppBKNameBtnUpClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure OurBKNameBtnUpClick(Sender: TObject);
    procedure OppCoNameBtnDnClick(Sender: TObject);
  private
    function savedata(): Boolean;
    procedure initdata();
    { Private declarations }
  public
    FBFID: string;
    { Public declarations }
  end;

var
  frmBankFlowInInput: TfrmBankFlowInInput;

implementation

uses
  U_ZDYHelp, U_RTFun, U_DataLink, U_BankSel, U_CompanySel;

{$R *.dfm}
procedure TfrmBankFlowInInput.initdata();
begin
  with ADOQueryTMP do
  begin
    close;
    sql.clear;
    sql.Add('select * from BS_Bank_Flow ');
    sql.Add('where BFID=' + quotedstr(trim(FBFID)));
    open;

  end;
  if not ADOQueryTMP.isempty then
  begin
    SCSHData(ADOQueryTMP, Panel1, 0);
    OurBKName.TxtCode := ADOQueryTMP.FieldByName('OurBKID').AsString;
    OppBKName.TxtCode := ADOQueryTMP.FieldByName('OppBKID').AsString;
    OppCoName.TxtCode := ADOQueryTMP.FieldByName('OppCoNO').AsString;
  end;

end;

function TfrmBankFlowInInput.savedata(): Boolean;
var
  MaxNo: string;
begin
  try
    ADOQueryCMD.Connection.BeginTrans;
    if Trim(FBFID) = '' then
    begin
      if GetLSNo(ADOQueryCMD, MaxNo, 'YL', 'BS_Bank_Flow', 4, 1) = False then
        raise Exception.Create('取最大号失败！');
    end
    else
      MaxNo := FBFID;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from BS_Bank_Flow where BFID=''' + Trim(MaxNo) + ''' ');
      Open;
      if Trim(FBFID) = '' then
      begin
        Append;
        fieldbyname('fillid').Value := trim(DCode);
        fieldbyname('filler').Value := trim(Dname);
      end
      else
      begin
        Edit;
        fieldbyname('Editid').Value := trim(DCode);
        Fieldbyname('Editer').Value := trim(Dname);
        Fieldbyname('Edittime').Value := SGetServerDateTime(adoqueryTmp);
      end;
      RTSetsavedata(ADOQueryCmd, 'BS_Bank_Flow', Panel1, 0);
      FieldByName('BFID').Value := Trim(MaxNo);
      FieldByName('BFType').Value := '收款';
      fieldbyname('inAmount').Value := strtofloatdef(trim(Amount.Text), 0);
      fieldbyname('OutAmount').Value := 0;
      fieldbyname('status').Value := '0';
      fieldbyname('OurBKID').Value := OurBKName.TxtCode;
      fieldbyname('OppBKID').Value := OppBKName.TxtCode;
      fieldbyname('OppCoNo').Value := OppCoName.TxtCode;
      Post;
    end;


  {

      with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('delete from BS_Bank_Flow where fromBFID=''' + Trim(FBFID) + ''' ');
      execsql;
    end;
   if trim(CRType.Text) = '账户互转' then
    begin
      if GetLSNo(ADOQueryCMD, fid, 'YL', 'BS_Bank_Flow', 4, 1) = False then
      begin
        ADOQueryCMD.Connection.RollbackTrans;
        Application.MessageBox('取流水号失败!', '提示');
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from BS_Bank_Flow where BFID=''' + Trim(fid) + ''' ');
        Open;

        append;
        fieldbyname('filler').Value := trim(Dname);
        RTSetsavedata(ADOQueryCmd, 'BS_Bank_Flow', Panel1, 0);
        FieldByName('BFID').Value := Trim(fid);
        fieldbyname('Bkid').Value := trim(OppBKName.TxtCode);
        fieldbyname('OurBKName').Value := trim(OppBKName.Text);
        fieldbyname('ToBkid').Value := trim(OurBKName.TxtCode);
        fieldbyname('OppBKName').Value := trim(OurBKName.Text);
        fieldbyname('inAmount').Value := strtofloatdef(trim(OutAmount.Text), 0);
        fieldbyname('OutAmount').Value := strtofloatdef(trim(InAmount.Text), 0);
        fieldbyname('status').Value := '0';
        fieldbyname('fromBFID').Value := trim(FBFID);
        Post;
      end;
    end;
    if trim(CRType.Text) = '结汇' then
    begin
      if GetLSNo(ADOQueryCMD, fid, 'YL', 'BS_Bank_Flow', 4, 1) = False then
      begin
        ADOQueryCMD.Connection.RollbackTrans;
        Application.MessageBox('取流水号失败!', '提示');
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from BS_Bank_Flow where BFID=''' + Trim(fid) + ''' ');
        Open;

        append;
        fieldbyname('filler').Value := trim(Dname);
        RTSetsavedata(ADOQueryCmd, 'BS_Bank_Flow', Panel1, 0);
        FieldByName('BFID').Value := Trim(fid);
        fieldbyname('Bkid').Value := trim(OppBKName.TxtCode);
        fieldbyname('OurBKName').Value := trim(OppBKName.Text);
        fieldbyname('ToBkid').Value := trim(OurBKName.TxtCode);
        fieldbyname('OppBKName').Value := trim(OurBKName.Text);
        fieldbyname('inAmount').Value := strtofloatdef(trim(JHAmount.Text), 0);
        fieldbyname('OutAmount').Value := strtofloatdef(trim(InAmount.Text), 0);
        fieldbyname('JHAmount').Value := strtofloatdef(trim(JHAmount.Text), 0);
        fieldbyname('JHHulLv').Value := strtofloatdef(trim(JHHulLv.Text), 0);
        fieldbyname('status').Value := '0';
        fieldbyname('fromBFID').Value := trim(FBFID);
        Post;
      end;
    end;    }
    ADOquerycmd.Connection.CommitTrans;
    FBFID := MaxNo;

    Result := True;
  except
    Result := false;
    ADOquerycmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmBankFlowInInput.TcloseClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmBankFlowInInput.ToolButton1Click(Sender: TObject);
begin
  if OurBKName.Text = '' then
  begin
    Application.MessageBox('我方账户不能为空！', '提示', 0);
    Exit;
  end;
  if OppBKName.Text = '' then
  begin
    Application.MessageBox('对方账户不能为空！', '提示', 0);
    Exit;
  end;
  if Amount.Text = '' then
  begin
    Application.MessageBox('金额不能为空！', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Amount.Text := '';
    FBFID := '';
  end;

end;

procedure TfrmBankFlowInInput.TSAVEClick(Sender: TObject);
begin

  if OurBKName.Text = '' then
  begin
    Application.MessageBox('我方账户不能为空！', '提示', 0);
    Exit;
  end;

  if OppBKName.Text = '' then
  begin
    Application.MessageBox('对方账户不能为空！', '提示', 0);
    Exit;
  end;
  if Amount.Text = '' then
  begin
    Application.MessageBox('金额不能为空！', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;

end;

procedure TfrmBankFlowInInput.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cafree;
end;

procedure TfrmBankFlowInInput.FormDestroy(Sender: TObject);
begin
  frmBankFlowInInput := nil;
end;

procedure TfrmBankFlowInInput.FormShow(Sender: TObject);
begin
  BFTime.DateTime := SGetServerDate(adoqueryTmp);

  initData();

end;

procedure TfrmBankFlowInInput.FormCreate(Sender: TObject);
begin
  Panel1.Align := alclient;
end;

procedure TfrmBankFlowInInput.OppBKNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := 'Bank';
      flag := self.OppCoName.TxtCode;
      flagname := '对方账户';
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBankFlowInInput.OppCoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmBankFlowInInput.OppCoNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      if ShowModal = 1 then
      begin
        OppCoName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
        OppCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmBankFlowInInput.OurBKNameBtnUpClick(Sender: TObject);
begin
  try
    frmBankSel := TfrmBankSel.Create(self);
    with frmBankSel do
    begin

      if showmodal = 1 then
      begin
        SELF.OurBKName.TxtCode := Trim(CDS_1.fieldbyname('BKID').AsString);
        SELF.OurBKName.Text := Trim(CDS_1.fieldbyname('BankName').AsString);
      end;
    end;
  finally
    frmBankSel.Free;
  end;
end;

end.

