-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoice.php
149 lines (134 loc) · 4.89 KB
/
invoice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
// MySQL host name, user name, password, database, and table
require_once("service-admin.inc.php");
$opts['tb'] = 'invoicelines';
$opts['cu'] = 'customers';
// Connect to database
mysql_connect($opts['hn'], $opts['un'], $opts['pw']);
mysql_select_db($opts['db']);
// Get the last 20 generated invoices
$qry = "SELECT invoiceid, ".$opts['tb'].".customerid, customername, description, amount, charge, invoicelinedate FROM ".$opts['tb']." LEFT JOIN ".$opts['cu']." ";
$qry .= "ON (".$opts['tb'].".customerid = ".$opts['cu'].".customerid) WHERE invoiceid = ".$_GET["id"];
$result = mysql_query($qry) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $qry . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
$inv = mysql_fetch_array($result);
$cqry = "SELECT * FROM ".$opts['cu']." WHERE customerid = ".$inv["customerid"];
$cresult = mysql_query($cqry) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $cqry . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
$customer = mysql_fetch_array($cresult);
// Check if we need to compute VAT
// Yes by default
$includevat = true;
if(!empty($customer["vatnr"])) {
if(!is_numeric(substr($customer["vatnr"],0,2)) && (strtoupper(substr($customer["vatnr"],0,2)) != "NL")) {
$includevat = false;
}
}
$pdf=new PDF();
$fontname="Arial";
$alternatefontname="Times";
$pdf->AliasNbPages();
$pdf->AddPage();
$leftmargin = 15;
// Create customer address box
$pdf->SetFont($fontname,'',12);
$pdf->SetY(45);
$pdf->Cell($leftmargin);
$pdf->Cell(80,6,$customer["customername"]);
$pdf->Ln();
if(!empty($customer["attn"])) {
$pdf->Cell($leftmargin);
$pdf->Cell(80,6,$strings[$customer["language"]]["attn"].$customer["attn"]);
$pdf->Ln();
}
$pdf->Cell($leftmargin);
$pdf->Cell(80,6,$customer["street"]);
$pdf->Ln();
$pdf->Cell($leftmargin);
$pdf->Cell(80,6,$customer["zipcode"]." ".$customer["city"]);
$pdf->Ln();
if(!empty($customer["country"])) {
$pdf->Cell($leftmargin);
$pdf->Cell(80,6,$customer["country"]);
$pdf->Ln();
}
// Date and reference
$nicedate = strtolower(date("j-M-Y", strtotime($inv["invoicelinedate"])));
$pdf->SetY(80);
$pdf->Cell($leftmargin);
$pdf->Cell(80,6,$strings[$customer["language"]]["date"].$nicedate);
$pdf->Ln();
$pdf->Cell($leftmargin);
$pdf->Cell(80,6,$strings[$customer["language"]]["subject"].$inv["invoiceid"]);
$pdf->Ln();
$pdf->Ln();
// Introductory text
$pdf->Cell($leftmargin);
$pdf->MultiCell(170,6,$strings[$customer["language"]]["pretext"]);
$pdf->Ln();
// Header row
$pdf->SetFont($alternatefontname,'',10);
$pdf->Cell($leftmargin);
$pdf->Cell(10,6,"#");
$pdf->Cell(120,6,"");
$pdf->Cell(20,6,$strings[$customer["language"]]["itemprice"],0,0,'R');
$pdf->Cell(20,6,$strings[$customer["language"]]["linetotal"],0,0,'R');
$pdf->Ln();
// Build the table with invoiceable items
mysql_data_seek($result, 0);
$total = 0;
$pdf->SetFont($fontname,'',10);
while ($invoiceline = mysql_fetch_array($result)) {
$pdf->Cell($leftmargin);
$pdf->Cell(10,6,$invoiceline["amount"]);
$pdf->Cell(120,6,$invoiceline["description"]);
$pdf->Cell(20,6,EURO." ".number_format($invoiceline["charge"],2,",",""),0,0,'R');
$pdf->Cell(20,6,EURO." ".number_format($invoiceline["amount"]*$invoiceline["charge"],2,",",""),0,0,'R');
$pdf->Ln();
$total += $invoiceline["amount"]*$invoiceline["charge"];
}
// Summation, VAT
$pdf->Cell($leftmargin);
$pdf->Cell(10,6,"",'T');
$pdf->Cell(120,6,$strings[$customer["language"]]["invtotal"],'T');
$pdf->Cell(20,6,"",'T');
$pdf->Cell(20,6,EURO." ".number_format($total,2,",",""),'T',0,'R');
$pdf->Ln();
if($includevat) {
$vat = $total*($VATpct/100);
$pdf->Cell($leftmargin);
$pdf->Cell(10,6,"");
$pdf->Cell(120,6,"$VATpct% ".$strings[$customer["language"]]["vat"]);
$pdf->Cell(20,6,"");
$pdf->Cell(20,6,EURO." ".number_format($vat,2,",",""),0,0,'R');
$pdf->Ln();
$pdf->Cell($leftmargin);
$pdf->Cell(10,6,"",'T');
$pdf->Cell(120,6,$strings[$customer["language"]]["invoiceable"],'T');
$pdf->Cell(20,6,"",'T');
$pdf->Cell(20,6,EURO." ".number_format($total+$vat,2,",",""),'T',0,'R');
$pdf->Ln();
} else {
if(!empty($customer["vatnr"])) {
$pdf->Cell($leftmargin);
$pdf->Cell(10,6,"");
$pdf->Cell(120,6,$strings[$customer["language"]]["your"]." ".$strings[$customer["language"]]["vat"]."nr: ".$customer["vatnr"]);
$pdf->Ln();
}
$pdf->Cell($leftmargin);
$pdf->Cell(10,6,"",'T');
$pdf->Cell(120,6,$strings[$customer["language"]]["invoiceable"],'T');
$pdf->Cell(20,6,"",'T');
$pdf->Cell(20,6,EURO." ".number_format($total,2,",",""),'T',0,'R');
$pdf->Ln();
}
// Finalisation text
$pdf->SetFont($fontname,'',12);
$pdf->Ln();
$pdf->Cell($leftmargin);
$pdf->MultiCell(170,6,$strings[$customer["language"]]["posttext"]);
$pdf->Ln();
$pdf->Cell($leftmargin);
$pdf->MultiCell(170,6,$strings[$customer["language"]]["signature"]);
$pdf->Ln();
$filename = $inv["invoiceid"]." ".$customer["customername"]." ".$inv["invoicelinedate"].".pdf";
$pdf->Output(str_replace(" ", "_", $filename), "I");
?>