-
Notifications
You must be signed in to change notification settings - Fork 3
/
rec-invoice-proc.php
executable file
·633 lines (535 loc) · 17.3 KB
/
rec-invoice-proc.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<?
#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");
# decide what to do
if (isset($_POST["key"])) {
switch ($_POST["key"]) {
case "process":
if (isset($_POST["set_date"]) or isset ($_POST["set_month"])){
$OUTPUT = details($_POST);
}else {
$OUTPUT = process($_POST);
}
break;
case "write":
$OUTPUT = write($_POST);
break;
case "update_prices":
$OUTPUT = update_prices ($_POST);
break;
default:
# decide what to do
if (isset($_GET["invids"])) {
$OUTPUT = details($_GET);
} else {
$OUTPUT = "<li class='err'>Invalid use of module.</li>";
}
}
} else {
# decide what to do
if (isset($_GET["invids"])) {
if (isset($_GET["edit"]))
$OUTPUT = edit_items ($_GET);
else
$OUTPUT = details($_GET);
} else {
$OUTPUT = "<li class='err'>Please select at least one invoice</li><br><input type='button' onClick=\"document.location='rec-invoice-view.php';\" value='« Correction'>";
}
}
# get templete
require("template.php");
# details
function details($_GET)
{
# get vars
extract ($_GET);
# validate input
require_lib("validate");
$v = new validate ();
foreach($invids as $key => $invid){
$v->isOk ($invid, "num", 1, 20, "Invalid recuring invoice number.");
}
# display errors, if any
if ($v->isError ()) {
$err = "";
$errors = $v->getErrors();
foreach ($errors as $e) {
$err .= "<li class='err'>".$e["msg"]."</li>";
}
$confirm .= "<p><input type='button' onClick='JavaScript:history.back();' value='« Correct submission'>";
return $confirm;
}
if (!isset($all_day))
$all_day = "";
if (!isset($all_month))
$all_month = "";
if (!isset($all_year))
$all_year = "";
if (!isset($just_month))
$just_month = "";
/* --- Start Display --- */
$printInv = "
<h3>Confirm Invoice Process</h3>
<form action='".SELF."' method='POST'>
<input type='hidden' name='key' value='process'>
<table ".TMPL_tblDflts.">
<tr>
<td class='err'>Please Note : This process might take long depending on the number of invoices. It is best to run it overnight.</td>
</tr>
</table>
<table ".TMPL_tblDflts.">
".TBL_BR."
<tr>
<th>Set All Invoices To This Date</th>
<td width='10'></td>
<th>Change All To This Month</th>
</tr>
<tr>
<td class='".bg_class()."' align='center'>
<input type='text' size='2' name='all_day' value='$all_day'>
<input type='text' size='2' name='all_month' value='$all_month'>
<input type='text' size='4' name='all_year' value='$all_year'>
<input type='submit' name='set_date' value='Set All Dates'>
</td>
<td></td>
<td class='".bg_class()."' align='center'>
<input type='text' size='2' name='just_month' value='$just_month'>
<input type='submit' name='set_month' value='Change'>
</td>
</tr>
".TBL_BR."
</table>
<table ".TMPL_tblDflts.">
<tr>
<th>Department</th>
<th>Sales Person</th>
<th>Invoice No.</th>
<th>Invoice Date</th>
<th>Customer Name</th>
<th>Order No</th>
<th>Grand Total</th>
</tr>";
$i = 0;
$inv_tot = 0;
foreach($invids as $key => $invid){
# Get recuring invoice info
db_connect();
$sql = "SELECT * FROM rec_invoices WHERE invid = '$invid' AND div = '".USER_DIV."'";
$invRslt = db_exec ($sql) or errDie ("Unable to get recuring invoice information");
if (pg_numrows ($invRslt) < 1) {
return "<i class='err'>Not Found</i>";
}
$inv = pg_fetch_array($invRslt);
if (isset($inv['cusnum']) AND $inv['cusnum'] != "0"){
#customer set ... check if he's blocked
$cust_sql = "SELECT cusnum FROM customers WHERE cusnum = '$inv[cusnum]' AND blocked != 'yes'";
$run_cust = db_exec ($cust_sql) or errDie ("Unable to get customer information.");
if (pg_numrows($run_cust) < 1) {
#blocked? customer found !!
return "<li class='err'>Invalid Customer/Customer is blocked.</li><br><input type='button' onClick=\"document.location='rec-invoice-view.php';\" value='« Correction'>";
}
}
$inv['total'] = sprint($inv['total']);
$inv['balance'] = sprint($inv['balance']);
# Format date
if ((strlen($all_day) > 0) AND (strlen($all_month) > 0) AND (strlen($all_year) > 0)){
$o_day = $all_day;
$o_month = $all_month;
$o_year = $all_year;
}else {
list($o_year, $o_month, $o_day) = explode("-", $inv['odate']);
}
if (strlen($just_month) > 0)
$o_month = $just_month;
$printInv .= "
<tr class='".bg_class()."'>
<td><input type='hidden' name='invids[]' value='$inv[invid]'>$inv[deptname]</td>
<td>$inv[salespn]</td>
<td>RI $inv[invid]</td>
<td valign='center'>
<table cellspacing='0'>
<tr>
<td>".mkDateSelecta("o",array("$i"),$o_year,$o_month,$o_day)."</td>
</tr>
</table>
</td>
<td>$inv[cusname] $inv[surname]</td>
<td align='right'>$inv[ordno]</td>
<td align='right'>".CUR." ".sprint ($inv['total'])."</td>
</tr>";
$inv_tot += $inv['total'];
$i++;
}
$printInv .= "
<tr class='".bg_class()."'>
<td><b>Totals:</b></td>
<td colspan='5' align='right'>Invoices : $i</td>
<td align='right'>".CUR." ".sprint ($inv_tot)."</td>
</tr>
<tr><td><br></td></tr>
<tr>
<td colspan='6' align='right'><input type='submit' value='Process >>'></td>
</tr>
</form></table>
<p>
<table ".TMPL_tblDflts.">
<tr>
<th>Quick Links</th>
</tr>
<tr class='".bg_class()."'>
<td><a href='rec-invoice-new.php'>New Recurring Invoice</a></td>
</tr>
<tr class='".bg_class()."'>
<td><a href='rec-invoice-view.php'>View Recurring Invoices</a></td>
</tr>
<script>document.write(getQuicklinkSpecial());</script>
</table>";
return $printInv;
}
# Create the company
function process ($_POST)
{
extract($_POST);
# validate input
require_lib("validate");
$v = new validate ();
foreach($invids as $key => $invid){
$v->isOk ($invid, "num", 1, 20, "Invalid recuring invoice number.");
$odate[$key] = $o_year[$key]."-".$o_month[$key]."-".$o_day[$key];
if(!checkdate($o_month[$key], $o_day[$key], $o_year[$key])){
$v->isOk ($odate[$key], "num", 1, 1, "Invalid Invoice Date.");
}
}
# display errors, if any
$err = "";
if ($v->isError ()) {
$errors = $v->getErrors();
foreach ($errors as $e) {
$err .= "<li class='err'>".$e["msg"]."</li>";
}
return $err;
}
$postvars = "";
foreach($invids as $key => $invid){
$postvars .= "
<input type='hidden' name='invids[]' value='$invid'>
<input type='hidden' size='2' name='o_day[]' maxlength='2' value='$o_day[$key]'>
<input type='hidden' size='2' name='o_month[]' maxlength='2' value='$o_month[$key]'>
<input type='hidden' size='2' name='o_year[]' maxlength='2' value='$o_year[$key]'>";
}
$OUTPUT = "
<form action='rec-invoice-proc.php' method='POST' name='postvars'>
<input type='hidden' name='key' value='write'>
$postvars
</form>
<table width='100%' height='100%'>
<tr>
<td align='center' valign='middle'>
<font size='2' color='white'>Please wait while the invoices are being processed. This may take several minutes.</font><br>
<div id='wait_bar_parent' style='border: 1px solid black; width:100px'>
<div id='wait_bar' style='font-size: 15pt'>...</div>
</div>
</td>
</tr>
</table>
<script>
wait_bar = getObjectById('wait_bar')
function moveWaitBar() {
if ( wait_bar.innerHTML == '...................')
wait_bar.innerHTML = '.';
else
wait_bar.innerHTML = wait_bar.innerHTML + '.';
setTimeout('moveWaitBar()', 50);
}
setTimeout('moveWaitBar()', 100);
document.postvars.submit();
</script>";
return $OUTPUT;
}
# Details
function write($_POST)
{
# Set mas execution time to 12 hours
ini_set("max_execution_time", 43200);
# Get vars
extract ($_POST);
# validate input
require_lib("validate");
$v = new validate ();
foreach($invids as $key => $invid){
$v->isOk ($invid, "num", 1, 20, "Invalid recuring invoice number.");
$odate[$key] = $o_year[$key]."-".$o_month[$key]."-".$o_day[$key];
if(!checkdate($o_month[$key], $o_day[$key], $o_year[$key])){
$v->isOk ($odate[$key], "num", 1, 1, "Invalid Invoice Date.");
}
}
# display errors, if any
$err = "";
if ($v->isError ()) {
$errors = $v->getErrors();
foreach ($errors as $e) {
$err .= "<li class='err'>".$e["msg"]."</li>";
}
return $err;
}
$i = 0;
foreach($invids as $key => $invid){
# Get recuring invoice info
db_connect();
$sql = "SELECT * FROM rec_invoices WHERE invid = '$invid' AND div = '".USER_DIV."'";
$invRslt = db_exec ($sql) or errDie ("Unable to get recuring invoice information");
if (pg_numrows ($invRslt) < 1) {
return "<i class='err'>Not Found</i>";
}
$inv = pg_fetch_array($invRslt);
/* - Start Copying - */
pglib_transaction ("BEGIN") or errDie("Unable to start a database transaction.",SELF);
# Insert invoice to DB
$sql = "
INSERT INTO invoices (
deptid, cusnum, deptname, cusacc, cusname, surname,
cusaddr, cusvatno, cordno, ordno, chrgvat, terms,
traddisc, salespn, odate, delchrg, subtot, vat,
discount, delivery, total, balance, comm, printed,
done, prd, serd, div, jobid
) VALUES (
'$inv[deptid]', '$inv[cusnum]', '$inv[deptname]', '$inv[cusacc]', '$inv[cusname]', '$inv[surname]',
'$inv[cusaddr]', '$inv[cusvatno]', '$inv[cordno]', '$inv[ordno]', '$inv[chrgvat]', '$inv[terms]',
'$inv[traddisc]', '$inv[salespn]', '$odate[$key]', '$inv[delchrg]', '$inv[subtot]', '$inv[vat]' ,
'$inv[discount]', '$inv[delivery]', '$inv[total]', '$inv[total]', '$inv[comm]', 'n', 'y', '".PRD_DB."',
'n', '".USER_DIV."', '$invid'
)";
$rslt = db_exec($sql) or errDie("Unable to insert invoice to Cubit.",SELF);
# get next ordnum
$invid = lastinvid();
# get selected stock in this recuring invoice
db_connect();
$sql = "SELECT * FROM recinv_items WHERE invid = '$inv[invid]' AND div = '".USER_DIV."'";
$stkdRslt = db_exec($sql);
$serd = "y";
while($stkd = pg_fetch_array($stkdRslt)){
# Insert one by one per quantity
if(ext_isSerial("stock", "stkid", $stkd['stkid'])){
$stkd['amt'] = sprint($stkd['amt']/$stkd['qty']);
$serd = "n";
for($i = 0; $i < $stkd['qty']; $i++){
# insert invoice items
$sql = "
INSERT INTO inv_items (
invid, whid, stkid, qty, unitcost, amt,
disc, discp, div, vatcode, account,
description
) VALUES (
'$invid', '$stkd[whid]', '$stkd[stkid]', '1', '$stkd[unitcost]', '$stkd[amt]',
'$stkd[disc]', '$stkd[discp]', '".USER_DIV."', '$stkd[vatcode]', '$stkd[account]',
'$stkd[description]'
)";
$rslt = db_exec($sql) or errDie("Unable to insert invoice items to Cubit.",SELF);
}
}else{
# insert invoice items
$sql = "
INSERT INTO inv_items (
invid, whid, stkid, qty, unitcost, amt,
disc, discp, div, vatcode, account,
description
) VALUES (
'$invid', '$stkd[whid]', '$stkd[stkid]', '$stkd[qty]', '$stkd[unitcost]', '$stkd[amt]',
'$stkd[disc]', '$stkd[discp]', '".USER_DIV."', '$stkd[vatcode]', '$stkd[account]',
'$stkd[description]'
)";
$rslt = db_exec($sql) or errDie("Unable to insert invoice items to Cubit.",SELF);
}
# update stock(alloc + qty)
$sql = "UPDATE stock SET alloc = (alloc + '$stkd[qty]') WHERE stkid = '$stkd[stkid]' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to update stock to Cubit.",SELF);
}
# set to not serialised
$sql = "UPDATE invoices SET serd = '$serd' WHERE invid = '$invid' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to update quotes in Cubit.",SELF);
pglib_transaction ("COMMIT") or errDie("Unable to commit a database transaction.",SELF);
/* - End Copying - */
}
// Final Laytout
$write = "
<table ".TMPL_tblDflts.">
<tr>
<th>Recurring Invoices Proccesed</th>
</tr>
<tr class='".bg_class()."'>
<td>New Invoices have been created from Recurring Invoices</td>
</tr>
</table>
<p>
<table ".TMPL_tblDflts.">
<tr>
<th>Quick Links</th>
</tr>
<tr class='".bg_class()."'>
<td><a href='invoice-view.php'>View Invoices</a></td>
</tr>
<tr class='".bg_class()."'>
<td><a href='rec-invoice-view.php'>View Recurring Invoices</a></td>
</tr>
<script>document.write(getQuicklinkSpecial());</script>
</table>";
return $write;
}
function edit_items ($_POST,$err="")
{
extract ($_POST);
db_connect ();
$search_arr = array ();
$send_invids = "";
foreach ($invids AS $each){
$search_arr[] = "invid='$each'";
$send_invids .= "<input type='hidden' name='invids[]' value='$each'>\n";
}
$search_string = implode (" OR ",$search_arr);
#get a list of all the items for these selected recurring invoices ....
$get_invs = "SELECT distinct(stkid) FROM recinv_items WHERE $search_string";
$run_invs = db_exec($get_invs) or errDie ("Unable to get invoice information.");
if (pg_numrows($run_invs) < 1){
#no entries found ?
}else {
while ($iarr = pg_fetch_array ($run_invs)){
#get info for this stock item ...
$get_stk = "SELECT stkdes FROM stock WHERE stkid = '$iarr[stkid]' LIMIT 1";
$run_stk = db_exec($get_stk) or errDie ("Unable to get stock information.");
if (pg_numrows($run_stk) < 1){
$stock_desc = "";
$stock_price = 0;
}else {
$arr = pg_fetch_array ($run_stk);
$stock_desc = $arr['stkdes'];
//price we get from the recurring invoice
$r_sql = "SELECT unitcost FROM recinv_items WHERE stkid = '$iarr[stkid]' AND ($search_string) LIMIT 1";
$run_r = db_exec($r_sql) or errDie ("Unable to get stock unit cost information.");
if (pg_numrows($run_r) < 1){
$unitcost = 0;
}else {
$unitcost = sprint (pg_fetch_result ($run_r,0,0));
}
}
#compile a list of the items ...
$listing .= "
<tr class='".bg_class()."'>
<td width='50%'>$stock_desc <input type='hidden' size='10' name='stkid[]' value='$iarr[stkid]'></td>
<td>".CUR." $unitcost</td>
<td>".CUR." <input type='text' size='10' name='unitcost[]' value='$unitcost'></td>
</tr>";
}
}
$display = "
<h4>Change Price On Selected Invoices</h4>
<table ".TMPL_tblDflts." width='60%'>
<form action='".SELF."' method='POST'>
$err
<input type='hidden' name='key' value='update_prices'>
$send_invids
<tr>
<th>Stock Item</th>
<th>Current Amount</th>
<th>New Amount</th>
</tr>
$listing
".TBL_BR."
<tr>
<td colspan='3' align='right'><input type='submit' value='Update'></td>
</tr>
</form>
</table>";
return $display;
}
function update_prices ($_POST)
{
extract ($_POST);
if (!isset($stkid) OR !is_array ($stkid))
return edit_items($_POST,"<li class='err'>No Items To Update.</li><br>");
$search_arr = array ();
foreach ($invids AS $each){
$search_arr[] = "invid='$each'";
}
$inv_search = implode (" OR ",$search_arr);
db_connect ();
#go through all stock items ... and update the items ...
foreach ($stkid AS $key => $value){
$upd_sql = "UPDATE recinv_items SET unitcost = '$unitcost[$key]', amt = qty * '$unitcost[$key]' WHERE stkid = '$value' AND ($inv_search)";
$run_upd = db_exec($upd_sql) or errDie ("Unable to update recurring invoice item information.");
}
db_connect ();
foreach ($invids AS $invid){
#new invoice ... new totals ...
$total = 0;
$vat = 0;
#update the totals of each ... based on its 'new' items ...
$get_inv = "SELECT * FROM rec_invoices WHERE invid = '$invid' LIMIT 1";
$run_inv = db_exec($get_inv) or errDie ("Unable to get recurring invoice information.");
if (pg_numrows($run_inv) > 0){
#found!
$inv_arr = pg_fetch_array ($run_inv);
#ok .. incl ... go through each item ... checking its vat code ...
$get_items = "SELECT * FROM recinv_items WHERE invid = '$inv_arr[invid]'";
$run_items = db_exec ($get_items) or errDie ("Unable to get received items information.");
if (pg_numrows ($run_items) > 0){
while ($item_arr = pg_fetch_array ($run_items)){
#get vatcode for this item ...
$get_vat = "SELECT vat_amount FROM vatcodes WHERE id = '$item_arr[vatcode]' LIMIT 1";
$run_vat = db_exec($get_vat) or errDie ("Unable to get vat amount for invoice item.");
if (pg_numrows($run_vat) > 0){
#vat code found ...
$vat_perc = sprint (pg_fetch_result ($run_vat,0,0));
}else {
$vat_perc = 0;
}
#add amount to total ..
if ($vat_perc == 0){
$total += $item_arr['amt'];
}else {
if ($inv_arr['chrgvat'] == "exc"){
$total += $item_arr['amt'];
$vat += sprint(($item_arr['amt'] / 100) * $vat_perc);
}else {
$vat_amt = sprint (($item_arr['amt']) - ($item_arr['amt'] / (($vat_perc/100)+1)));
$vat += $vat_amt;
$total += ($item_arr['amt'] - $vat_amt);
}
}
}
}
}
$subtot = sprint ($total);
$total = sprint ($total+$vat);
$vat = sprint ($vat);
#update this invoice with the new totals ...
$upd_sql = "UPDATE rec_invoices SET subtot = '$subtot', vat = '$vat', total = '$total', balance = '$total' WHERE invid = '$invid'";
$run_upd = db_exec($upd_sql) or errDie ("Unable to update recurring invoices information.");
}
header ("Location: rec-invoice-view.php");
}
?>