Looking at the Details for an Invoicing Application in Delphi - New Invoice Form and Code
(Page 3 of 6 )

The new invoice form presents the user with a input form. You select a client from the dropdown box and then enter the amount for which you are invoicing. The total amount including the VAT will automatically be calculated and added to the total amount field of the form. Below is a listing of the code that makes all this happen:
unit newinv;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, StdCtrls, Buttons, RpDefine, RpCon, RpConDS,
RpRave, DBCtrls,math, ADODB;
type
TForm2 = class(TForm)
StaticText4: TStaticText;
StaticText6: TStaticText;
edAmount: TEdit;
btnInsert: TBitBtn;
BitBtn2: TBitBtn;
edTotal: TEdit;
StaticText7: TStaticText;
GroupBox1: TGroupBox;
StaticText1: TStaticText;
StaticText3: TStaticText;
qclients: TADOQuery;
cb1: TComboBox;
Memo1: TMemo;
procedure BitBtn2Click(Sender: TObject);
procedure btnInsertClick(Sender: TObject);
procedure edAmountChange(Sender: TObject);
procedure edTotalClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Memo1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
tot:real;
implementation
uses invmain;
{$R *.dfm}
procedure TForm2.BitBtn2Click(Sender: TObject);
begin
edit1.clear;
edit2.Clear;
edit3.clear;
edit4.Clear;
edit6.Clear;
close;
end;
procedure TForm2.btnInsertClick(Sender: TObject);
var
dt,d:tdatetime;
begin
dt:=now;
shortDateFormat := 'dd-mm-yyyy';
//if not (edamount.Text = '') and not (edtotal.Text='') and not (memo1.Text ='') then begin
if form1.ado1.active then form1.ado1.active:=false;
form1.ado1.TableName:='invoices';
form1.ado1.active:=true;
with form1.ado1 do
begin
Screen.Cursor := crHourGlass;
Insert;
FieldByName('invname').Text:= cb1.Text;
FieldByName('invdate').AsDateTime:=date;
FieldByName('status').text:='pending';
FieldByName('amount').AsCurrency:=strtofloat(edAmount.Text);
FieldByName('total').AsCurrency:=strtofloat(edTotal.Text);
FieldByName('notes').AsString:=memo1.Lines.Text;
post;
//form1.ado1.Active:=false;
// form1.ado1.Active:=true;
Screen.Cursor := crDefault;
{end
else
begin
messagedlg('Please fill in ALL the fields.',mtwarning,[mbOk], 0);
end; }
end;
end;
procedure TForm2.edAmountChange(Sender: TObject);
begin
tot:= strtofloat(edAmount.Text);
end;
procedure TForm2.edTotalClick(Sender: TObject);
var
am:real;
begin
edtotal.Clear;
if tot > 0 then
begin
am:= (form1.vat / 100) * tot;
tot:=tot + am;
tot:=RoundTo(tot,-2);
edTotal.Text:=floattostr(tot);
end;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
qclients.ConnectionString:=form1.con;
qclients.Close;
qclients.SQL.Text:='Select name from clients';
qclients.Open;
while not qclients.EOF do
begin
cb1.Items.Add(qclients.FieldByName('name').AsString);
qclients.Next;
end;
end;
procedure TForm2.FormShow(Sender: TObject);
begin
tot:=0.0;
memo1.SelStart := 0;
memo1.Perform(EM_SCROLLCARET, 0, 0);
memo1.SetFocus;
end;
procedure TForm2.Memo1Click(Sender: TObject);
begin
memo1.SelStart := 0;
memo1.Perform(EM_SCROLLCARET, 0, 0);
memo1.SetFocus;
end;
end.
Once you click on the "Create New Invoice" button, the invoice information is inserted into the invoices table. The following code does that job:
dt:=now;
shortDateFormat := 'dd-mm-yyyy';
//if not (edamount.Text = '') and not (edtotal.Text='') and not (memo1.Text ='') then begin
if form1.ado1.active then form1.ado1.active:=false;
form1.ado1.TableName:='invoices';
form1.ado1.active:=true;
with form1.ado1 do
begin
Screen.Cursor := crHourGlass;
Insert;
FieldByName('invname').Text:= cb1.Text;
FieldByName('invdate').AsDateTime:=date;
FieldByName('status').text:='pending';
FieldByName('amount').AsCurrency:=strtofloat(edAmount.Text);
FieldByName('total').AsCurrency:=strtofloat(edTotal.Text);
FieldByName('notes').AsString:=memo1.Lines.Text;
post;
//form1.ado1.Active:=false;
// form1.ado1.Active:=true;
Screen.Cursor := crDefault;
The code itself is pretty straightforward. It opens up the table and calls the insert procedure. Then it simply inserts the newly-created values in the column fields of the invoice table and posts them to the database.
Next: Invoice Management Code and Output >>
More Delphi-Kylix Articles
More By Chris Neeman