unit U_BankFlowIOInput;

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

type
  TfrmBankFlowIOInput = 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;
    ToolButton1: TToolButton;
    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 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
  frmBankFlowIOInput: TfrmBankFlowIOInput;

implementation

uses
  U_ZDYHelp, U_RTFun, U_DataLink, U_BankSel, U_CompanySel;

{$R *.dfm}
procedure TfrmBankFlowIOInput.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;
  end;

end;

function TfrmBankFlowIOInput.savedata(): Boolean;
var
  MaxNo, ZMaxNo: 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;

    ZMaxNo := 'Z' + MaxNo;
    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 := 0;
      fieldbyname('OutAmount').Value := strtofloatdef(trim(Amount.Text), 0);
      fieldbyname('status').Value := '0';
      fieldbyname('OurBKID').Value := OurBKName.TxtCode;
      fieldbyname('OppBKID').Value := OppBKName.TxtCode;
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('delete from BS_Bank_Flow where fromID=''' + Trim(MaxNo) + ''' ');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from BS_Bank_Flow where 1=2 ');
      Open;
      Append;
      RTSetsavedata(ADOQueryCmd, 'BS_Bank_Flow', Panel1, 0);
      fieldbyname('fillid').Value := trim(DCode);
      fieldbyname('filler').Value := trim(Dname);
      FieldByName('fromID').Value := Trim(MaxNo);
      FieldByName('BFID').Value := Trim(ZMaxNo);
      FieldByName('BFType').Value := '转入';
      fieldbyname('inAmount').Value := strtofloatdef(trim(Amount.Text), 0);
      fieldbyname('OutAmount').Value := 0;
      fieldbyname('OurBKID').Value := OppBKName.TxtCode;
      fieldbyname('OurBKName').Value := OppBKName.Text;
      fieldbyname('OppBKID').Value := OurBKName.TxtCode;
      fieldbyname('OppBKName').Value := OurBKName.Text;
      fieldbyname('status').Value := '0';
      Post;
    end;

    ADOquerycmd.Connection.CommitTrans;
    FBFID := MaxNo;

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

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

procedure TfrmBankFlowIOInput.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 TfrmBankFlowIOInput.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 TfrmBankFlowIOInput.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cafree;
end;

procedure TfrmBankFlowIOInput.FormDestroy(Sender: TObject);
begin
  frmBankFlowIOInput := nil;
end;

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

  initData();

end;

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

procedure TfrmBankFlowIOInput.OppBKNameBtnUpClick(Sender: TObject);
begin
  try
    frmBankSel := TfrmBankSel.Create(self);
    with frmBankSel do
    begin
      if showmodal = 1 then
      begin
        SELF.OppBKName.TxtCode := Trim(CDS_1.fieldbyname('BKID').AsString);
        SELF.OppBKName.Text := Trim(CDS_1.fieldbyname('BankName').AsString);
      end;
    end;
  finally
    frmBankSel.Free;
  end;
end;

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

procedure TfrmBankFlowIOInput.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.

