unit U_BankFlowList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxPC, StdCtrls, ComCtrls, ExtCtrls, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, ToolWin, cxCalendar, cxCurrencyEdit, cxTextEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, U_BaseList, Vcl.Menus, Vcl.Clipbrd;

type
  TfrmBankFlowList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TINIT: TToolButton;
    TGLV: TToolButton;
    TADD: TToolButton;
    TDEL: TToolButton;
    Texcel: TToolButton;
    TCLOSE: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label2: TLabel;
    BFType: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    NOTE: TEdit;
    DataSource1: TDataSource;
    AdoQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    cxTabControl1: TcxTabControl;
    Label3: TLabel;
    OppCoName: TEdit;
    Label5: TLabel;
    Label8: TLabel;
    CDS_Bank: TClientDataSet;
    ToolButton1: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1Column1: TcxGridDBColumn;
    V1CRTime: TcxGridDBColumn;
    V1Column2: TcxGridDBColumn;
    V1Column3: TcxGridDBColumn;
    V1CRType: TcxGridDBColumn;
    V1InMoney: TcxGridDBColumn;
    V1OutMoney: TcxGridDBColumn;
    V1YEMoney: TcxGridDBColumn;
    V1NOTE: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    V1Column5: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    BFID: TEdit;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Label6: TLabel;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TCLOSEClick(Sender: TObject);
    procedure TADDClick(Sender: TObject);
    procedure TINITClick(Sender: TObject);
    procedure TGLVClick(Sender: TObject);
    procedure TexcelClick(Sender: TObject);
    procedure TDELClick(Sender: TObject);
    procedure BFTypeChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    FBKID, FBKName: string;
    procedure initGrid();
    procedure SetTab();
    procedure GetCurTab();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmBankFlowList: TfrmBankFlowList;

implementation

uses
  U_DataLink, U_RTFun, U_BankFlowInInput, U_BankFlowOutInput, U_BankFlowIOInput;

{$R *.dfm}

procedure TfrmBankFlowList.GetCurTab();
begin
  CDS_Bank.Locate('BankName', Trim(cxTabControl1.Tabs.Strings[cxTabControl1.TabIndex]), []);
  FBKID := Trim(CDS_Bank.FieldByName('BKID').AsString);
  FBKName := Trim(CDS_Bank.FieldByName('BankName').AsString);
end;

procedure TfrmBankFlowList.SetTab();
begin
  cxTabControl1.OnChange := nil;

  cxTabControl1.Tabs.Clear;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from BS_Bank order by SerialNo ');
    open;

  end;
  SCreateCDS(ADOQueryTmp, CDS_Bank);
  SInitCDSData(ADOQueryTmp, CDS_Bank);
  with CDS_Bank do
  begin
    if not CDS_Bank.isempty then
    begin
      while not eof do
      begin
        cxTabControl1.Tabs.Add(trim(fieldbyname('BankName').AsString));
        next;
      end;
    end;
  end;

  if cxTabControl1.Tabs.Count < 1 then
    cxTabControl1.Tabs.Add('初始数据');
//  cxTabControl1.TabIndex:=0;
  cxTabControl1.OnChange := cxTabControl1Change;
end;

procedure TfrmBankFlowList.InitGrid();
var
  MSG: string;
begin
  if Trim(cxTabControl1.Tabs.Strings[cxTabControl1.TabIndex]) = '' then
    Exit;

  GetCurTab();
  with AdoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,YEAmount=dbo.F_Bank_Balance(A.BFID,A.BFTime,A.OurBKID) ');
    sql.Add('from BS_Bank_Flow A ');
    sql.Add('where BFTime>=''' + formatdateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
    sql.Add('and BFTime<''' + formatdateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');

    sql.Add('and OurBKID=' + quotedstr(Trim(FBKID)));
    SQL.Add('order by A.BFTime,BFID');
    open;

  end;
  SCreateCDS(ADOQueryTmp, CDS_Main);
  SInitCDSData(ADOQueryTmp, CDS_Main);

//  with AdoQueryTmp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add(' EXEC  P_CWLS_Judge_lSSC');
//    open;
//  end;
//
//  if not AdoQueryTmp.IsEmpty then
//  begin
//    MSG := '自动生成流水异常：' + AdoQueryTmp.FieldByName('FactoryName').AsString;
//    MSG := MSG + ' 时间：' + AdoQueryTmp.FieldByName('CRTime').AsString;
//    MSG := MSG + ' 金额：' + AdoQueryTmp.FieldByName('RZMoney').AsString;
//
//    application.messagebox(pchar(MSG), '提示', 0);
//  end;
  TV1.DataController.Filter.Clear;
  CDS_Main.Last;
end;

procedure TfrmBankFlowList.N1Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmBankFlowList.FormCreate(Sender: TObject);
begin
  inherited;
  cxgrid1.Align := alclient;

end;

procedure TfrmBankFlowList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption + tv1.Name, Tv1);
  enddate.datetime := SGetServerDateTime(ADOQueryTmp);
  begdate.datetime := strtodate(formatdateTime('yyyy-MM', enddate.DateTime) + '-01');
  SetTab();
  cxTabControl1.Refresh;
  initGrid();
end;

procedure TfrmBankFlowList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmBankFlowList := nil;
end;

procedure TfrmBankFlowList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;

  Action := cafree;
end;

procedure TfrmBankFlowList.TCLOSEClick(Sender: TObject);
begin
  close;
end;

procedure TfrmBankFlowList.TADDClick(Sender: TObject);
begin
  GetCurTab();
  try
    frmBankFlowInInput := TfrmBankFlowInInput.Create(Application);
    with frmBankFlowInInput do
    begin
      OurBKName.TxtCode := FBKID;
      OurBKName.text := FBKName;
      FBFID := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmBankFlowInInput.free;
  end;
end;

procedure TfrmBankFlowList.TINITClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmBankFlowList.ToolButton1Click(Sender: TObject);
begin
  writeCxGrid(self.Caption + tv1.Name, Tv1);
end;

procedure TfrmBankFlowList.ToolButton2Click(Sender: TObject);
begin
  GetCurTab();
  try
    frmBankFlowOutInput := TfrmBankFlowOutInput.Create(Application);
    with frmBankFlowOutInput do
    begin
      OurBKName.TxtCode := FBKID;
      OurBKName.text := FBKName;
      FBFID := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmBankFlowOutInput.free;
  end;

end;

procedure TfrmBankFlowList.ToolButton3Click(Sender: TObject);
begin
  GetCurTab();
  try
    frmBankFlowIOInput := TfrmBankFlowIOInput.Create(Application);
    with frmBankFlowIOInput do
    begin
      OurBKName.TxtCode := FBKID;
      OurBKName.text := FBKName;
      FBFID := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmBankFlowIOInput.free;
  end;
end;

procedure TfrmBankFlowList.TGLVClick(Sender: TObject);
begin
  SDofilter(AdoQueryTmp, SGetFilters(Panel1, 1, 0));
  SCreateCDS(AdoQueryTmp, CDS_Main);
  SInitCDSData(AdoQueryTmp, CDS_Main);
end;

procedure TfrmBankFlowList.TexcelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(Trim(cxTabControl1.Tabs.Strings[cxTabControl1.TabIndex]), cxGrid1);
end;

procedure TfrmBankFlowList.TDELClick(Sender: TObject);
var
  FBFID: string;
begin
  if CDS_Main.IsEmpty then
    exit;

  FBFID := trim(CDS_Main.fieldbyname('BFID').AsString);
  if CDS_Main.fieldbyname('fromID').AsString <> '' then
  begin
    application.messagebox('自动生成输入不能删除!', '提示', 0);
    exit;
  end;
  if Trim(CDS_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    application.messagebox('不能操作他人数据!', '提示', 0);
    exit;
  end;

  if application.MessageBox('确定要删除吗？', '提示', 1) = 2 then
    exit;
  try
    ADOQuerycmd.Connection.BeginTrans;
    with ADOQuerycmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from BS_Bank_Flow where BFID=''' + FBFID + '''');
      sql.Add('delete from BS_Bank_Flow where isnull(fromID,'''')<>'''' and fromID=''' + FBFID + '''');
      execsql;
    end;
    ADOQuerycmd.Connection.CommitTrans;
    initGrid();
  except
    ADOQuerycmd.connection.rollbackTrans;
    application.messagebox('数据删除失败', '提示', 0);
  end;

end;

procedure TfrmBankFlowList.BFTypeChange(Sender: TObject);
begin
  SDofilter(AdoQueryTmp, SGetFilters(Panel1, 1, 0));
  SCreateCDS(AdoQueryTmp, CDS_Main);
  SInitCDSData(AdoQueryTmp, CDS_Main);
end;

procedure TfrmBankFlowList.cxTabControl1Change(Sender: TObject);
begin
  initGrid();
end;

end.

