/
invoice-email.php
executable file
·404 lines (343 loc) · 10.3 KB
/
invoice-email.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?
#This program is copyright by Andre Coetzee email: ac@main.me
#and is licensed under the GPL v3
#
#
#
#
#Please add yourself to: http://www.accounting-123.com
#Developers, Software Vendors, Support, Accountants, Users
#
#
#The full software license can be found here:
#http://www.accounting-123.com/a.php?a=153/GPLv3
#
#
#
#
#
#
#
#
#
#
#
# Get settings
require("settings.php");
require("core-settings.php");
require("libs/ext.lib.php");
require_lib("mail.smtp");
# Decide what to do
if (isset($_GET["invid"])) {
$OUTPUT = details($_GET);
} else {
$OUTPUT = "<li class=err>Invalid use of module.";
}
# Get templete
require("template.php");
# Details
function details($_GET)
{
# get vars
foreach ($_GET as $key => $value) {
$$key = $value;
}
# validate input
require_lib("validate");
$v = new validate ();
$v->isOk ($invid, "num", 1, 20, "Invalid invoice number.");
# display errors, if any
if ($v->isError ()) {
$err = "";
$errors = $v->getErrors();
foreach ($errors as $e) {
$err .= "<li class=err>".$e["msg"];
}
$confirm .= "<p><input type=button onClick='JavaScript:history.back();' value='« Correct submission'>";
return $confirm;
}
# Get invoice info
db_connect();
$sql = "SELECT * FROM invoices WHERE invid = '$invid' AND div = '".USER_DIV."'";
$invRslt = db_exec ($sql) or errDie ("Unable to get invoice information");
if (pg_numrows ($invRslt) < 1) {
return "<i class=err>Not Found</i>";
}
$inv = pg_fetch_array($invRslt);
# Check if invoice has been printed
if($inv['printed'] != "y"){
$error = "<li class=err> Error : Invoice number <b>$invid</b> has not been printed yet.";
$error .= "<p><input type=button onClick='JavaScript:history.back();' value='« Correct submission'>";
return $error;
}
/* --- Start some checks --- */
# Check if stock was selected(yes = put done button)
db_connect();
$sql = "SELECT stkid FROM inv_items WHERE invid = '$inv[invid]' AND div = '".USER_DIV."'";
$crslt = db_exec($sql);
if(pg_numrows($crslt) < 1){
$error = "<li class=err> Error : Invoice number <b>$invid</b> has no items.";
$error .= "<p><input type=button onClick='JavaScript:history.back();' value='« Correct submission'>";
return $error;
}
/* --- End some checks --- */
/* --- Start Products Display --- */
# Products layout
$products = "";
$disc = 0;
$taxex = 0;
# Get selected stock in this invoice
db_connect();
$sql = "SELECT * FROM inv_items WHERE invid = '$invid' AND div = '".USER_DIV."'";
$stkdRslt = db_exec($sql);
while($stkd = pg_fetch_array($stkdRslt)){
# Get warehouse name
db_conn("exten");
$sql = "SELECT whname FROM warehouses WHERE whid = '$stkd[whid]' AND div = '".USER_DIV."'";
$whRslt = db_exec($sql);
$wh = pg_fetch_array($whRslt);
# Get selected stock in this warehouse
db_connect();
$sql = "SELECT * FROM stock WHERE stkid = '$stkd[stkid]' AND div = '".USER_DIV."'";
$stkRslt = db_exec($sql);
$stk = pg_fetch_array($stkRslt);
$sp = " ";
# Check Tax Excempt
if($stk['exvat'] == 'yes'){
$taxex += ($stkd['amt']);
$ex = "#";
}else{
$ex = " ";
}
# Keep track of discounts
$disc += $stkd['disc'];
# Put in product
$products .="<tr valign=top>
<td>$stk[stkcod]</td>
<td>$ex $sp $stk[stkdes]</td>
<td>$stkd[qty]</td>
<td>$stkd[unitcost]</td>
<td>$stkd[disc]</td>
<td>".CUR." $stkd[amt]</td>
</tr>";
}
/* --- Start Some calculations --- */
# Subtotal
$SUBTOT = sprint($inv['subtot']);
# Calculate tradediscm
if(strlen($inv['traddisc']) > 0){
$traddiscm = sprint((($inv['traddisc']/100) * $SUBTOT));
}else{
$traddiscm = "0.00";
}
# Calculate subtotal
$VATP = TAX_VAT;
$SUBTOT = sprint($inv['subtot']);
$VAT = sprint($inv['vat']);
$TOTAL = sprint($inv['total']);
$inv['delchrg'] = sprint($inv['delchrg']);
/* --- End Some calculations --- */
# todays date
$date = date("d-m-Y");
$sdate = date("Y-m-d");
# Avoid little box
if(strlen($inv['comm']) > 0){
$inv['comm'] = "<table border=1 cellspacing='0' bordercolor='#000000'>
<tr><td>".nl2br($inv['comm'])."</td></tr>
</table>";
}
# Update number of prints
$inv['prints']++;
db_connect();
$Sql = "UPDATE invoices SET prints = '$inv[prints]' WHERE invid = '$invid' AND div = '".USER_DIV."'";
//$upRslt = db_exec($Sql) or errDie ("Unable to update invoice information");
/* -- Final Layout -- */
$details = "<html><center><h2>Tax Invoice</h2>
<table ".TMPL_tblDflts." width=770>
<tr><td valign=top width=30%>
<table ".TMPL_tblDflts.">
<tr>
<td>$inv[surname]</td>
</tr>
<tr>
<td>".nl2br($inv['cusaddr'])."</td>
</tr>
<tr>
<td>(Vat No. $inv[cusvatno])</td>
</tr>
</table>
</td><td valign=top width=30%>
".COMP_NAME."<br>
".COMP_ADDRESS."<br>
".COMP_PADDR."<br>
".COMP_TEL."<br>
".COMP_FAX."<br>
Reg No. ".COMP_REGNO."<br>
VAT No. ".COMP_VATNO."<br>
</td><td align=left width=20%>
<img src='compinfo/getimg.php' width=230 height=47>
</td><td valign=bottom align=right width=20%>
<table cellpadding='2' cellspacing='0' border=1 bordercolor='#000000'>
<tr>
<td><b>Invoice No.</b></td>
<td valign=center>$inv[invnum]</td>
</tr>
<tr>
<td><b>Proforma Inv No.</b></td>
<td>$inv[docref]</td>
</tr>
<tr>
<td><b>Order No.</b></td>
<td valign=center>$inv[ordno]</td>
</tr>
<tr>
<td><b>Terms</b></td>
<td valign=center>$inv[terms] Days</td>
</tr>
<tr>
<td><b>Invoice Date</b></td>
<td valign=center>$inv[odate]</td>
</tr>
</table>
</td></tr>
<tr><td><br></td></tr>
<tr><td colspan=4>
<table cellpadding='5' cellspacing='0' border=1 width=100% bordercolor='#000000'>
<tr>
<th>ITEM NUMBER</th>
<th width=45%>DESCRIPTION</th>
<th>QTY</th>
<th>UNIT PRICE</th>
<th>UNIT DISCOUNT</th>
<th>AMOUNT</th>
<tr>
$products
</table>
</td></tr>
<tr><td>
$inv[comm]
</td><td>
".BNK_BANKDET."
</td><td align=right colspan=2>
<table cellpadding='5' cellspacing='0' border=1 width=50% bordercolor='#000000'>
<tr>
<td><b>SUBTOTAL</b></td>
<td align=right>".CUR." $SUBTOT</td>
</tr>
<tr>
<td><b>Trade Discount</b></td>
<td align=right>".CUR." $inv[discount]</td>
</tr>
<tr>
<td><b>Delivery Charge</b></td>
<td align=right>".CUR." $inv[delivery]</td>
</tr>
<tr>
<td><b>VAT @ $VATP%</b></td>
<td align=right>".CUR." $VAT</td>
</tr>
<tr>
<th><b>GRAND TOTAL<b></th>
<td align=right>".CUR." $TOTAL</td>
</tr>
</table>
</td></tr>
<tr><td><br></td></tr>
<tr><td>
<table cellpadding='2' cellspacing='0' border=1>
<tr><td colspan=2>VAT Exempt indicator = #</td></tr>
</table>
</td><td><br></td></tr>
</table></center></html>";
$body=$details;
$es = qryEmailSettings();
db_conn("cubit");
$Sl="SELECT * FROM customers WHERE cusnum='$inv[cusnum]'";
$Ri=db_exec($Sl);
$cd=pg_fetch_array($Ri);
if(strlen($cd['email']) <1) {
return "This customer does not have an email address";
}
//$send_cc="mg@mailbox.co.za";
//$send_bcc="mg@mailbox.co.za";
$send_cc="";
$send_bcc="";
$smtp_data['signature']=$es['sig'];
$smtp_data['smtp_from']=$es['fromname'];
$smtp_data['smtp_reply']=$es['reply'];
$smtp_data['smtp_host']=$es['smtp_host'];
$smtp_data['smtp_auth']=$es['smtp_auth'];
$smtp_data['smtp_user']=$es['smtp_user'];
$smtp_data['smtp_pass']=$es['smtp_pass'];
//db_conn('cubit');
// $rslt = db_exec("SELECT smtp_from, smtp_reply, signature, smtp_host, smtp_auth, smtp_user, smtp_pass
// FROM mail_accounts");
// $smtp_data = pg_fetch_array($rslt);
// build msg body
$body = "$body\n\n$smtp_data[signature]";
// determine whether or not here is an attachment
//$has_attachment = is_uploaded_file($attachment["tmp_name"]);
$has_attachment =FALSE;
// modify message and create content_type header depending on whether or not an attachment was posted
if ( $has_attachment == FALSE ) {
$content_type = "text/html;charset=US-ASCII";
$transfer_encoding = "8bit";
} else { // has attachment
$content_type = "multipart/mixed";
// create the main body
$body_text = "Content-Type: text/plain; charset=US-ASCII\n";
$body_text .= "Content-Transfer-Encoding: base64\n";
$body_text .= "\n" . chunk_split(base64_encode($body));
// get the attachment data
if ( ($fd = fopen($attachment["tmp_name"], "r")) == TRUE ) {
$attachment_data = "";
while ( ! feof($fd) ) {
$attachment_data .= fgets( $fd, 4096 );
}
fclose($fd);
// delete the temporary file
unlink($attachment["tmp_name"]);
$attachment_data = chunk_split(base64_encode($attachment_data));
$attachment_headers = "Content-Type: $attachment[type]; name=\"$attachment[name]\"\n";
$attachment_headers .= "Content-Transfer-Encoding: base64\n";
$attachment_headers .= "Content-Disposition: attachment; filename=\"$attachment[name]\"\n";
$attachment_data = "$attachment_headers\n$attachment_data";
} else { // error opening the attachment file
$attachment_data = "";
}
// generate a unique boundary ( md5 of filename + ":=" + filesize )
$boundary = md5($attachment["name"]) . "=:" . $attachment["size"];
$content_type .= "; boundary=\"$boundary\"";
// put together the body
$body = "\n--$boundary\n$body_text\n\n--$boundary\n$attachment_data\n\n--$boundary--\n
";
}
print $body;
// build headers
$headers[] = "From: $smtp_data[smtp_from]";
$headers[] = "To: $cd[email]";
$headers[] = "Reply-To: $smtp_data[smtp_reply]";
$headers[] = "X-Mailer: Cubit Mail";
$headers[] = "Return-Path: $smtp_data[smtp_reply]";
$headers[] = "Content-Type: $content_type";
$headers[] = "cc: $send_cc";
$headers[] = "bcc: $send_bcc";
// create the mime header if should
if ( $has_attachment == TRUE ) {
$headers[] = "MIME-Version: 1.0";
}
// create the header variable (it is done this way, to make management of headers easier, since there
// may be no tabs and unnecesary whitespace in mail headers)
//$headers[] = "\n"; // add another new line to finish the headers
$headers = implode("\n", $headers);
// send the message
$sendmail = & new clsSMTPMail;
$OUTPUT = $sendmail->sendMessages($smtp_data["smtp_host"], 25, $smtp_data["smtp_auth"], $smtp_data["smtp_user"],
$smtp_data["smtp_pass"],$cd['email'], $smtp_data["smtp_from"], "Invoice: $inv[invnum]", $body, $headers);
/*if ( mail($send_to, $subject, $body, $headers) == TRUE )
$OUTPUT = "Successfully sent mail to $send_to.<br>";
else
$OUTPUT = "Error sending mail.<br>";*/
return $OUTPUT;
}
?>