⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accounts.pas

📁 家庭收支管理
💻 PAS
字号:
unit Accounts;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBCtrlsEh, StdCtrls, Mask, GridsEh, DBGridEh;

type
  TAccountsForm = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label8: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    btnNew: TButton;
    btnDelete: TButton;
    edt_AccountName: TDBEditEh;
    cmb_AccountType: TDBComboBoxEh;
    edt_Account: TDBEditEh;
    edt_BankAccount: TDBEditEh;
    edt_BankName: TDBEditEh;
    cmb_Currency: TDBComboBoxEh;
    DBGridEh: TDBGridEh;
    edt_original: TDBNumberEditEh;
    btnExit: TButton;
    btnModify: TButton;
    cbx_Apply: TDBCheckBoxEh;
    procedure btnNewClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnModifyClick(Sender: TObject);
    procedure WMNCHitTest(var Msg:TMessage);message WM_NCHITTEST;
    procedure cmb_AccountTypeChange(Sender: TObject);
    procedure cbx_ApplyClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure DataChanged(DataSet: TDataSet);
  end;

var
  AccountsForm: TAccountsForm;

implementation

{$R *.dfm}

uses DataModule;

procedure TAccountsForm.WMNCHitTest(var Msg:TMessage);
begin
  inherited;
  Msg.Result:=HTCLIENT;
end;

procedure TAccountsForm.DataChanged(DataSet: TDataSet);
begin
  edt_AccountName.Text:=DataForm.tbl_Account.FieldByName('AccountName').AsString;
  cmb_AccountType.ItemIndex:=DataForm.tbl_Account.FieldByName('AccountType').AsInteger;
  edt_Account.Text:=DataForm.tbl_Account.FieldByName('Account').AsString;;
  edt_BankAccount.Text:=DataForm.tbl_Account.FieldByName('BankAccount').AsString;;
  edt_BankName.Text:=DataForm.tbl_Account.FieldByName('BankName').AsString;;
  if DataForm.tbl_Account.FieldByName('Currency').AsString='RMB' then
    cmb_Currency.ItemIndex:=0
  else
    cmb_Currency.ItemIndex:=1;
  edt_original.Text:=DataForm.tbl_Account.FieldByName('original').AsString;;
  cbx_Apply.Checked:=DataForm.tbl_Account.FieldByName('Apply').AsBoolean;
end;

procedure TAccountsForm.btnNewClick(Sender: TObject);
var BM:pointer;
begin
  if btnNew.Caption='新增' then
  begin
    btnNew.Caption:='确定';
    edt_AccountName.Text:='';
    cmb_AccountType.ItemIndex:=-1;
    edt_Account.Text:='';
    edt_BankAccount.Text:='';
    edt_BankName.Text:='';
    cmb_Currency.ItemIndex:=-1;
    edt_original.Value:=0;
    cbx_apply.Checked:=True;
    edt_AccountName.Enabled:=True;
    cmb_AccountType.Enabled:=True;
    //edt_Account.Enabled:=True;
    edt_BankAccount.Enabled:=True;
    edt_BankName.Enabled:=True;
    cmb_Currency.Enabled:=True;
    edt_original.Enabled:=True;
    cbx_apply.Enabled:=True;
    btnDelete.Enabled:=False;
    btnExit.Enabled:=False;
    btnModify.Enabled:=False;
  end else begin
    BM:=DataForm.DataSource.DataSet.GetBookmark;
    if application.MessageBox(pchar('您确定要增加帐号为 [ '+edt_Account.Text+' ],名称为 [ '+edt_AccountName.Text+' ] 的数据吗?'),'提示信息',MB_OKCANCEL+MB_ICONQUESTION)=ID_CANCEL then
      DataForm.DataSource.DataSet.GotoBookmark(BM)
    else begin
      DataForm.ABSQuery1.SQL.Clear;
      DataForm.ABSQuery1.SQL.Add('select * from zhanghu where Account=:cs');
      DataForm.ABSQuery1.ParamByName('cs').AsString:=edt_Account.Text;
      DataForm.ABSQuery1.Open;
      if DataForm.ABSQuery1.RecordCount>0 then
        application.MessageBox('您输入的核算帐号已经存在,请重新输入!','提示信息',mb_ok+MB_ICONERROR)
      else begin
        DataForm.ABSQuery1.SQL.Clear;
        DataForm.ABSQuery1.SQL.Add('insert into zhanghu (AccountType,Account,AccountName,BankAccount,Currency,BankName,Original,Present,Apply) values (:cs4,:cs1,:cs2,:cs3,:cs5,:cs6,:cs7,:cs8,:cs9)');
        DataForm.ABSQuery1.ParamByName('cs4').AsInteger:=cmb_AccountType.ItemIndex;
        DataForm.ABSQuery1.ParamByName('cs1').AsString:=edt_Account.Text;
        DataForm.ABSQuery1.ParamByName('cs2').AsString:=edt_AccountName.Text;
        DataForm.ABSQuery1.ParamByName('cs3').AsString:=edt_BankAccount.Text;
        if cmb_Currency.ItemIndex=0 then
          DataForm.ABSQuery1.ParamByName('cs5').AsString:='RMB'
        else
          DataForm.ABSQuery1.ParamByName('cs5').AsString:='USD';
        DataForm.ABSQuery1.ParamByName('cs6').AsString:=edt_BankName.Text;
        DataForm.ABSQuery1.ParamByName('cs7').AsString:=edt_Original.Text;
        DataForm.ABSQuery1.ParamByName('cs8').AsString:=edt_Original.Text;
        DataForm.ABSQuery1.ParamByName('cs9').AsBoolean:=cbx_Apply.Checked;
        DataForm.ABSQuery1.ExecSQL;
        application.MessageBox('帐户已成功初始化,按<确定>后继续!','提示信息',mb_ok+MB_ICONINFORMATION);
      end;  
    end;
    btnNew.Caption:='新增';
    edt_AccountName.Enabled:=False;
    cmb_AccountType.Enabled:=False;
    edt_BankAccount.Enabled:=False;
    edt_BankName.Enabled:=False;
    cmb_Currency.Enabled:=False;
    edt_original.Enabled:=False;
    cbx_apply.Enabled:=False;
    btnDelete.Enabled:=True;
    btnExit.Enabled:=True;
    btnModify.Enabled:=True;
    if BM<>nil then DataForm.DataSource.DataSet.FreeBookmark(BM);
    DBGridEh.DataSource.DataSet.Refresh;
  end;
end;

procedure TAccountsForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    Key:=#0;
    Perform(CM_DialogKey,VK_TAB,0);
  end;  
end;

procedure TAccountsForm.FormCreate(Sender: TObject);
begin
  DataForm.DataSource.DataSet:=DataForm.tbl_Account;
  DBGridEh.DataSource:=DataForm.DataSource;
  DataForm.tbl_Account.AfterScroll:=DataChanged;
  DataForm.tbl_Account.Open;
  DataForm.tbl_Account.First;
end;

procedure TAccountsForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  DataForm.tbl_Account.Close;
  DataForm.DataSource.DataSet:=nil;
  DBGridEh.DataSource:=nil;
  DataForm.tbl_Account.AfterScroll:=nil;
  Action:=CaFree;
end;

procedure TAccountsForm.btnDeleteClick(Sender: TObject);
begin
  if application.MessageBox(pchar('您确定要删除帐号为 [ '+edt_Account.Text+' ],名称为 [ '+edt_AccountName.Text+' ] 的数据吗?'),'提示信息',MB_OKCANCEL+MB_ICONQUESTION)=ID_CANCEL then exit;
  if DataForm.tbl_Account.FieldByName('Present').AsFloat=0 then
    DataForm.tbl_Account.Delete
  else application.MessageBox(pchar('帐户 [ '+edt_Account.Text+' ] - [ '+edt_AccountName.Text+' ] 当前余额不为零,不能删除!'),'提示信息',MB_OK+MB_ICONINFORMATION);
end;

procedure TAccountsForm.btnModifyClick(Sender: TObject);
begin
  if btnModify.Caption='修改' then
  begin
    edt_AccountName.Enabled:=True;
    edt_BankAccount.Enabled:=True;
    edt_BankName.Enabled:=True;
    cbx_apply.Enabled:=True;
    DBGridEh.Enabled:=False;
    btnNew.Enabled:=False;
    btnDelete.Enabled:=False;
    btnExit.Enabled:=False;
    btnModify.Caption:='确认修改';
  end else begin
    edt_AccountName.Enabled:=False;
    edt_BankAccount.Enabled:=False;
    edt_BankName.Enabled:=False;
    cbx_apply.Enabled:=False;
    DBGridEh.Enabled:=True;
    btnNew.Enabled:=True;
    btnDelete.Enabled:=True;
    btnExit.Enabled:=True;
    btnModify.Caption:='修改';
    if application.MessageBox(pchar('您确定要修改帐号为 [ '+edt_Account.Text+' ],名称为 [ '+edt_AccountName.Text+' ] 的数据吗?'),'提示信息',MB_OKCANCEL+MB_ICONQUESTION)=ID_CANCEL then exit;
    DataForm.tbl_Account.Edit;
    DataForm.tbl_Account.FieldByName('AccountName').AsString:=edt_AccountName.Text;
    DataForm.tbl_Account.FieldByName('BankAccount').AsString:=edt_BankAccount.Text;
    DataForm.tbl_Account.FieldByName('BankName').AsString:=edt_BankName.Text;
    DataForm.tbl_Account.FieldByName('Apply').AsBoolean:=cbx_Apply.Checked;
    DataForm.tbl_Account.CheckBrowseMode;
  end;
end;

procedure TAccountsForm.cmb_AccountTypeChange(Sender: TObject);
var
  i:integer;
  TF:boolean;
  LB:string;
begin
  if cmb_AccountType.ItemIndex=-1 then exit;
  screen.Cursor:=crHourglass;
  i:=1;
  TF:=true;
  case cmb_AccountType.ItemIndex of
    0:LB:='XJ0000';
    1:LB:='CK0000';
    2:LB:='TZ0000';
    3:LB:='DK0000';
    4:LB:='QT0000';
  end;
  DataForm.ABSQuery2.SQL.Clear;
  DataForm.ABSQuery2.SQL.Add('select * from zhanghu where Account=:cs');
  while TF do
  begin
    DataForm.ABSQuery2.ParamByName('cs').AsString:=LB+copy(intTostr(10000+i),2,4);
    DataForm.ABSQuery2.Open;
    if DataForm.ABSQuery2.RecordCount=0 then
    begin
      TF:=false;
      edt_Account.Text:=LB+copy(intTostr(10000+i),2,4);
    end;
    DataForm.ABSQuery2.Close;
    inc(i);
  end;
  screen.Cursor:=crDefault;
end;

procedure TAccountsForm.cbx_ApplyClick(Sender: TObject);
begin
  if (not cbx_Apply.Checked) and (DataForm.tbl_Account.FieldByName('Present').AsFloat<>0) then
  begin
    cbx_Apply.Checked:=True;
    application.MessageBox(pchar('帐户 [ '+edt_Account.Text+' ] - [ '+edt_AccountName.Text+' ] 当前余额不为零,不能修改为不启用!'),'提示信息',MB_OK+MB_ICONINFORMATION);
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -