/
nons-multiline-quote-new.php
executable file
·846 lines (723 loc) · 24.6 KB
/
nons-multiline-quote-new.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
<?
#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($_GET["invid"]) && isset($_GET["cont"])) {
$_GET["done"] = "";
$OUTPUT = details($_GET);
}else{
if (isset($_POST["key"])) {
switch ($_POST["key"]) {
case "details":
$OUTPUT = details($_POST);
break;
case "update":
$OUTPUT = write($_POST);
break;
default:
$_GET["done"] = "";
$OUTPUT = details($_GET);
}
} else {
$_GET["done"] = "";
$OUTPUT = details($_GET);
}
}
# Get templete
require("template.php");
# Starting dummy
function create_dummy($deptid)
{
$trans_date_setting = getCSetting ("USE_TRANSACTION_DATE");
if (isset ($trans_date_setting) AND $trans_date_setting == "yes"){
$trans_date_value = getCSetting ("TRANSACTION_DATE");
$date_arr = explode ("-", $trans_date_value);
$date_year = $date_arr[0];
$date_month = $date_arr[1];
$date_day = $date_arr[2];
}else {
$date_year = date("Y");
$date_month = date("m");
$date_day = date("d");
}
$odate = "$date_year-$date_month-$date_day";
db_connect();
# Insert purchase to DB
$sql = "
INSERT INTO nons_invoices (
cusname, cusaddr, cusvatno, chrgvat, sdate, odate, subtot, balance, vat, total, done, username, prd, invnum,
typ, div, multiline
) VALUES (
'', '', '', 'yes', CURRENT_DATE, '$odate', 0, 0, 0, 0, 'n', '".USER_NAME."', '".PRD_DB."', 0,
'quo', '".USER_DIV."', 'yes'
)";
$rslt = db_exec($sql) or errDie("Unable to create template Non-Stock Quote.",SELF);
return lastinvid();
}
# details
function details($_POST, $error="")
{
# get vars
extract ($_POST);
# validate input
require_lib("validate");
$v = new validate ();
if( isset($invid) ){
$v->isOk ($invid, "num", 1, 20, "Invalid Non-Stock Quote number.");
} else {
$invid = create_dummy(0);
}
# display errors, if any
if ($v->isError ()) {
$errors = $v->getErrors();
foreach ($errors as $e) {
$error .= "<li class='err'>".$e["msg"]."</li>";
}
$confirm .= "$error<p><input type='button' onClick='JavaScript:history.back();' value='« Correct submission'>";
return $confirm;
}
# Get quote info
db_connect();
$sql = "SELECT * FROM nons_invoices WHERE invid = '$invid' AND div = '".USER_DIV."'";
$invRslt = db_exec ($sql) or errDie ("Unable to get quote information");
if (pg_numrows ($invRslt) < 1) {
return "<li class='err'>Quote Not Found</li>";
}
$inv = pg_fetch_array($invRslt);
# check if quote has been printed
if($inv['done'] == "y"){
$error = "<li class='err'> Error : quote number <b>$invid</b> has already been printed.";
$error .= "<p><input type='button' onClick='JavaScript:history.back();' value='« Correct submission'>";
return $error;
}
$lead = $inv["lead"];
if(strlen($inv['ncdate']) < 1){
$ncdate_year = date("Y");
$ncdate_month = date("m",mktime(0,0,0,date("m"),date("d")+5,date("Y")));
$ncdate_day = date("d",mktime(0,0,0,date("m"),date("d")+5,date("Y")));
}else {
$darr = explode ("-",$inv['ncdate']);
$ncdate_year = $darr['0'];
$ncdate_month = $darr['1'];
$ncdate_day = $darr['2'];
}
/* --- Start Drop Downs --- */
# format date
list($nquo_year, $nquo_month, $nquo_day) = explode("-", $inv['odate']);
# keep the charge vat option stable
if($inv['chrgvat'] == "yes"){
$chy = "checked=yes";
$chn = "";
$chnone = "";
}elseif ($inv['chrgvat'] == "no"){
$chy = "";
$chn = "checked=yes";
$chnone = "";
} else {
$chy = "";
$chn = "";
$chnone = "checked=yes";
}
/* --- End Drop Downs --- */
/* --- Start Products Display --- */
# Select all products
$products = "
<table ".TMPL_tblDflts." width='100%'>
<tr>
<th>DESCRIPTION</th>
<th>QTY</th>
<th>UNIT PRICE</th>
<th>AMOUNT</th>
<th>VAT Code</th>
<th>Remove</th>
<tr>";
# get selected stock in this purchase
db_connect();
$sql = "SELECT * FROM nons_inv_items WHERE invid = '$invid' AND div = '".USER_DIV."'";
$stkdRslt = db_exec($sql);
$i = 0;
while ($stkd = pg_fetch_array($stkdRslt)) {
# keep track of selected stock amounts
$amts[$i] = $stkd['amt'];
$stkd['amt'] = round($stkd['amt'], 2);
$chk = "";
if($stkd['vatex'] == 'y')
$chk = "checked='yes'";
db_conn('cubit');
$Sl = "SELECT * FROM vatcodes ORDER BY code";
$Ri = db_exec($Sl);
$vats = "<select name='vatcodes[]'>";
while($vd = pg_fetch_array($Ri)) {
if($stkd['vatex'] == $vd['id']) {
$sel = "selected";
} else {
$sel = "";
}
$vats .= "<option value='$vd[id]' $sel>$vd[code]</option>";
}
$vats .= "</option>";
$stkd['amt'] = sprint($stkd['amt']);
# put in product
$products .= "
<tr class='".bg_class()."'>
<td align='center' nowrap><input type='hidden' name='des[]' value='$stkd[description]'>".nl2br($stkd['description'])."</td>
<td align='center'><input type='text' size='3' name='qtys[]' value='$stkd[qty]'></td>
<td align='center'><input type='text' size='8' name='unitcost[]' value='$stkd[unitcost]'></td>
<td><input type='hidden' name='amt[]' value='$stkd[amt]'> ".CUR." $stkd[amt]</td>
<td align='center'>$vats</td>
<td><input type='checkbox' name='remprod[]' value='$i'><input type='hidden' name='SCROLL' value='yes'></td>
</tr>";
$i++;
}
# Look above(remprod keys)
$keyy = $i;
# look above(if i = 0 then there are no products)
if( $i == 0 ) {
$done = "";
}
if ( $i == 0 || isset($diffwhBtn) ) {
# add one
$products .= "
<tr class='".bg_class()."'>
<td align='center'>
<table id='tblCtrls' width='420px' height='30px' border='0' cellspacing='0' cellpadding='0' bgcolor='#D6D3CE'>
<tr>
<td class='tdClass'>
<img alt='Bold' class='buttonClass' src='images/bold.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doBold()'>
<img alt='Italic' class='buttonClass' src='images/italic.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doItalic()'>
<img alt='Underline' class='buttonClass' src='images/underline.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doUnderline()'>
<img alt='Left' class='buttonClass' src='images/left.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doLeft()'>
<img alt='Center' class='buttonClass' src='images/center.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doCenter()'>
<img alt='Right' class='buttonClass' src='images/right.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doRight()'>
<img alt='Ordered List' class='buttonClass' src='images/ordlist.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doOrdList()'>
<img alt='Bulleted List' class='buttonClass' src='images/bullist.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doBulList()'>
<img alt='Horizontal Rule' class='buttonClass' src='images/rule.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doRule()'>
</td>
<td class='tdClass' align=right>
<select name='selSize' onChange='doSize(this.options[this.selectedIndex].value)'>
<option value=''>-- Font Size --</option>
<option value='1'>Very Small</option>
<option value='2'>Small</option>
<option value='3'>Medium</option>
<option value='4'>Large</option>
<option value='5'>Larger</option>
<option value='6'>Very Large</option>
</select>
</td>
</tr>
</table>
<iframe name='editArea' id='editArea' style='width: 420px; height:160px; background: #FFFFFF;'></iframe>
<input type='hidden' name='bodydata' value=''>
<input type='hidden' name='counter' value='$i'>
</td>
<td align='center'><input type='text' size='3' name='qtys[$i]' value='1'></td>
<td align='center'><input type='text' size='8' name='unitcost[$i]'></td>
<td>".CUR." 0.00</td>
<td> </td>
<td> </td>
</tr>";
}
$products .= "</table>";
/* --- End Products Display --- */
/* --- Start Some calculations --- */
# Get subtotal
$SUBTOT = $inv['subtot'];
# Get Total
$TOTAL = sprint($inv['total']);
# Get vat
$VAT = sprint($inv['vat']);
if (!isset($showvat))
$showvat = TRUE;
if($showvat == TRUE){
$vat14 = AT14;
}else {
$vat14 = "";
}
/* --- End Some calculations --- */
$sel = "";
if(isset($lead) AND (strlen($lead) > 0))
$sel = "checked='yes'";
$showdoc_html = "'".str_replace ("<div style=\"text-align: left;\"><br></div>","",$bodydata)."'";
if (!isset ($old_customer_select))
$old_customer_select = "";
#get customers
$get_cust = "SELECT cusnum, surname, vatnum, paddr1 FROM customers WHERE blocked = 'no' AND location = 'loc' ORDER BY cusname";
$run_cust = db_exec ($get_cust) or errDie ("Unable to get customer information.");
if (pg_numrows ($run_cust) < 1){
$cust_drop = "<input type='hidden' name='customer_select' value=''>No Customers Found.";
}else {
$cust_drop = "<select name='customer_select' onChange=\"document.editForm.submit();\">";
$cust_drop .= "<option value=''>Select Customer Or Enter Details</option>";
while ($carr = pg_fetch_array ($run_cust)){
if (isset ($customer_select) AND $customer_select == $carr['cusnum']){
$cust_drop .= "<option value='$carr[cusnum]' selected>$carr[surname]</option>";
if ($old_customer_select != $customer_select) {
$inv['cusname'] = $carr['surname'];
$inv['cusaddr'] = $carr['paddr1'];
$inv['cusvatno'] = $carr['vatnum'];
}else {
$inv['cusname'] = $cusname;
$inv['cusaddr'] = $cusaddr;
$inv['cusvatno'] = $cusvatno;
}
}else {
$cust_drop .= "<option value='$carr[cusnum]'>$carr[surname]</option>";
}
}
$cust_drop .= "</select>";
}
$details = "
<script language='JavaScript'>
function update() {
document.editForm.bodydata.value = editArea.document.body.innerHTML;
document.editForm.submit();
}
function Init() {
editArea.document.designMode = 'On';
editArea.document.body.innerHTML = $showdoc_html;
editArea.document.execCommand('justifyleft', false, null);
}
function controlSelOn(ctrl) {
ctrl.style.borderColor = '#000000';
ctrl.style.backgroundColor = '#B5BED6';
ctrl.style.cursor = 'hand';
}
function controlSelOff(ctrl) {
ctrl.style.borderColor = '#D6D3CE';
ctrl.style.backgroundColor = '#D6D3CE';
}
function controlSelDown(ctrl) {
ctrl.style.backgroundColor = '#8492B5';
}
function controlSelUp(ctrl) {
ctrl.style.backgroundColor = '#B5BED6';
}
function doBold() {
editArea.document.execCommand('bold', false, null);
}
function doItalic() {
editArea.document.execCommand('italic', false, null);
}
function doUnderline() {
editArea.document.execCommand('underline', false, null);
}
function doLeft() {
editArea.document.execCommand('justifyleft', false, null);
}
function doCenter() {
editArea.document.execCommand('justifycenter', false, null);
}
function doRight() {
editArea.document.execCommand('justifyright', false, null);
}
function doOrdList() {
editArea.document.execCommand('insertorderedlist', false, null);
}
function doBulList() {
editArea.document.execCommand('insertunorderedlist', false, null);
}
function doRule() {
editArea.document.execCommand('inserthorizontalrule', false, null);
}
function doSize(fSize) {
if(fSize != '')
editArea.document.execCommand('fontsize', false, fSize);
}
window.onload = Init;
</script>
<center>
<h3>New Multi Line Quote</h3>
<form action='".SELF."' method='POST' name='editForm' enctype='multipart/form-data'>
<input type='hidden' name='key' value='update'>
<input type='hidden' name='old_customer_select' value='$customer_select'>
<input type='hidden' name='invid' value='$invid'>
<table ".TMPL_tblDflts." width='95%'>
<tr>
<td valign='top'>
<table ".TMPL_tblDflts.">
<tr>
<th colspan='2'> Customer Details </th>
</tr>
<tr class='".bg_class()."'>
<td>Select Customer</td>
<td>$cust_drop</td>
</tr>
<tr class='".bg_class()."'>
<td>Customer</td>
<td valign='middle'><input type='text' name='cusname' value='$inv[cusname]'></td>
</tr>
<tr class='".bg_class()."'>
<td valign='top'>Customer Address</td>
<td valign='middle'><textarea name='cusaddr' cols='18' rows='3'>$inv[cusaddr]</textarea></td>
</tr>
<tr class='".bg_class()."'>
<td valign='top'>Customer VAT No.</td>
<td valign='middle'><input type='text' name='cusvatno' value='$inv[cusvatno]'></td>
</tr>
<tr class='".bg_class()."'>
<td valign='top'>Next Contact Date</td>
<td valign='center'>".mkDateSelect("ncdate",$ncdate_year,$ncdate_month,$ncdate_day)."</td>
</tr>
<tr class='".bg_class()."'>
<td valign='top'>Add As Lead</td>
<td valign='center'><input type='checkbox' name='lead' $sel value='yes'></td>
</tr>
</table>
</td>
<td valign='top' align='right'>
<table ".TMPL_tblDflts.">
<tr>
<th colspan='2'> Non-Stock Quote Details </th>
</tr>
<tr class='".bg_class()."'>
<td>Non-Stock Quote No.</td>
<td valign='center'>TI $inv[invid]</td>
</tr>
<tr class='".bg_class()."'>
<td>Date</td>
<td valign='center'>".mkDateSelect("nquo",$nquo_year,$nquo_month,$nquo_day)."</td>
</tr>
<tr class='".bg_class()."'>
<td>VAT Inclusive</td>
<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='yes' $chy> No<input type='radio' size='7' name='chrgvat' value='no' $chn></td>
</tr>
</table>
</td>
</tr>
<tr><td><br></td></tr>
<tr>
<td colspan='2'>$products</td>
</tr>
<tr>
<td>
<p>
<table ".TMPL_tblDflts.">
<tr>
<th width='25%'>Quick Links</th>
<th width='25%'>Remarks</th>
<td rowspan='5' valign='top' width='50%'>$error</td>
</tr>
<tr>
<td class='".bg_class()."'><a href='nons-quote-view.php'>View Non-Stock Quotes</a></td>
<td class='".bg_class()."' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>$inv[remarks]</textarea></td>
</tr>
<tr class='".bg_class()."'>
<td><a href='main.php'>Main Menu</a></td>
</tr>
</table>
</td>
<td align='right'>
<table ".TMPL_tblDflts." width='80%'>
<tr class='".bg_class()."'>
<td>SUBTOTAL</td>
<td align='right'>".CUR." <input type='hidden' name='subtot' value='$SUBTOT'>$SUBTOT</td>
</tr>
<tr class='".bg_class()."'>
<td>VAT $vat14</td>
<td align='right'>".CUR." $inv[vat]</td>
</tr>
<tr class='".bg_class()."'>
<th>GRAND TOTAL</th>
<td align='right'>".CUR." <input type='hidden' name='total' value='$TOTAL'>$TOTAL</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align='right'><input name='diffwhBtn' onClick='update();' type='submit' value='Add Item'> |</td>
<td><input type='submit' name='upBtn' onClick='update();' value='Update'>$done</td>
</tr>
</table>
</form>
</center>";
return $details;
}
# details
function write($_POST)
{
#get vars
extract ($_POST);
#only process details if we are not changing the customer
if (isset ($customer_select) AND isset ($old_customer_select) AND ($customer_select != $old_customer_select))
return details ($_POST);
# validate input
require_lib("validate");
$v = new validate ();
$sdate = mkdate($nquo_year, $nquo_month, $nquo_day);
$v->isOk($sdate, "date", 1, 1, "Invalid Date.");
# used to generate errors
$error = "asa@";
// check the quote details
$v->isOK($cusname, "string", 1, 100, "Invalid customer name");
$v->isOK($cusaddr, "string", 0, 100, "Invalid customer address");
$v->isOK($cusvatno, "string", 0, 50, "Invalid customer vat number");
if ( $chrgvat != "yes" && $chrgvat != "no" && $chrgvat!="none")
$v->addError($chrgvat, "Invalid vat option");
if (!isset($bodydata))
$bodydata = "";
$bodydata = str_replace("'","",$bodydata);
//$bodydata = str_replace("<br>","",$bodydata);
$bodydata = str_replace(" "," ",$bodydata);
$bodydata = str_replace(" "," ",$bodydata);
$bodydata = str_replace(" "," ",$bodydata);
$bodydata = str_replace(" "," ",$bodydata);
//[key] was $counter ... but it wasnt set ??
$des[] = $bodydata;
# check quantities
if(isset($qtys)){
foreach($qtys as $keys => $qty){
$v->isOk ($qty, "num", 1, 10, "Invalid Quantity for product number : <b>".($keys+1)."</b>");
$v->isOk ($unitcost[$keys], "float", 1, 20, "Invalid Unit Price for product number : <b>".($keys+1)."</b>.");
// $v->isOk ($des[$keys], "url", 1, 255, "Invalid Description.");
if($qty < 1){
$v->isOk ($qty, "num", 0, 0, "Error : Item Quantity must be at least one. Product number : <b>".($keys+1)."</b>");
}
}
}
# check amt
if(isset($amt)){
foreach($amt as $keys => $amount){
$v->isOk ($amount, "float", 1, 20, "Invalid Amount, please enter all details.");
}
}
# display errors, if any
$err = "";
if ($v->isError ()) {
$errors = $v->getErrors();
foreach ($errors as $e) {
$err .= "<li class='err'>".$e["msg"]."</li>";
}
$_POST['done'] = "";
return details($_POST, $err);
}
# Get purchase info
db_connect();
$sql = "SELECT * FROM nons_invoices WHERE invid = '$invid' AND div = '".USER_DIV."'";
$invRslt = db_exec ($sql) or errDie ("Unable to get purchase information");
if (pg_numrows ($invRslt) < 1) {
return "<li>- invoices Not Found</li>";
}
$inv = pg_fetch_array($invRslt);
$inv['chrgvat'] = $chrgvat;
# check if purchase has been printed
if($inv['done'] == "y"){
$error = "<li class='err'> Error : quote number <b>$invid</b> has already been printed.</li>";
$error .= "<p><input type='button' onClick='JavaScript:history.back();' value='« Correct submission'>";
return $error;
}
$vatamount = 0;
$showvat = TRUE;
# begin updating
pglib_transaction ("BEGIN") or errDie("Unable to start a database transaction.",SELF);
db_connect();
/* -- Start remove old items -- */
# remove old items
$sql = "DELETE FROM nons_inv_items WHERE invid='$invid' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to update quote items in Cubit.",SELF);
$taxex = 0;
/* -- End remove old items -- */
if(isset($qtys)){
foreach($qtys as $keys => $value){
if(isset($remprod)&&in_array($keys, $remprod)){
} else {
# Calculate amount
$amt[$keys] = ($qtys[$keys] * $unitcost[$keys]);
if(!isset($vatcodes[$keys])) {
$vatcodes[$keys] = 0;
}
$Sl = "SELECT * FROM vatcodes WHERE id='$vatcodes[$keys]'";
$Ri = db_exec($Sl);
// if(pg_num_rows($Ri)<1) {
// return "Please select the vatcode for all your stock.";
// }
$vd = pg_fetch_array($Ri);
if($vd['zero'] == "Yes") {
$excluding = "y";
} else {
$excluding = "";
}
if((TAX_VAT != $vd['vat_amount']) AND ($vd['vat_amount'] != "0.00")){
$showvat = FALSE;
}
$vr = vatcalc($amt[$keys],$inv['chrgvat'],$excluding,0,$vd['vat_amount']);
$vrs = explode("|",$vr);
$ivat = $vrs[0];
$iamount = $vrs[1];
$vatamount += $ivat;
$vate = 'n';
if((isset($vatex) && in_array($keys, $vatex))||$vd['zero'] == "Yes"){
$taxex += $amt[$keys];
$vate = 'y';
}
$vate = $vatcodes[$keys];
# insert purchase items
$sql = "
INSERT INTO nons_inv_items (
invid, qty, amt, unitcost, description, vatex, div
) VALUES (
'$invid', '$qtys[$keys]', '$amt[$keys]', '$unitcost[$keys]', '$des[$keys]','$vate', '".USER_DIV."'
)";
$rslt = db_exec($sql) or errDie("Unable to insert quote items to Cubit.",SELF);
}
# everything is set place done button
$_POST["done"] = " | <input name='doneBtn' type='submit' value='Done'>";
}
}else{
$_POST["done"] = "";
}
$_POST['showvat'] = $showvat;
/* --- ----------- Clac --------------------- */
##----------------------NEW----------------------
$sub = 0.00;
if(isset($amt)) {
$sub = sprint(array_sum($amt));
}
$VATP = TAX_VAT;
if($chrgvat == "no"){
$subtotal = sprint($sub);
$subtotal = sprint($subtotal);
// $VAT=sprint(($subtotal-$taxex)*$VATP/100);
$VAT = $vatamount;
$SUBTOT = $sub;
$TOTAL = sprint($subtotal+$VAT);
}elseif($chrgvat == "yes"){
$subtotal = sprint($sub);
$subtotal = sprint($subtotal);
// $VAT=sprint(($subtotal-$taxex)*$VATP/(100+$VATP));
$VAT = $vatamount;
$SUBTOT = sprint($sub - $vatamount);
$TOTAL = sprint($subtotal);
} else {
$subtotal = sprint($sub);
$traddiscmt = sprint($subtotal);
$subtotal = sprint($subtotal);
$VAT = sprint(0);
$SUBTOT = $sub;
$TOTAL = $subtotal;
}
/* --- ----------- Clac --------------------- */
##----------------------END----------------------
/* --- Clac ---
# calculate subtot
if( isset($amt) ){
$SUBTOT = array_sum($amt);
}else{
$SUBTOT = 0.00;
}
$VATP = TAX_VAT;
if($chrgvat == "no"){
$SUBTOT = $SUBTOT;
}elseif($chrgvat == "yes"){
$SUBTOT = sprint(($SUBTOT * 100)/(100 + $VATP));
}else{
$SUBTOT = ($SUBTOT);
}
if($chrgvat != "none"){
$VAT = sprint($SUBTOT * ($VATP/100));
}else{
$VAT = 0;
}
$TOTAL = sprint($SUBTOT + $VAT);
/*# if vat is not included
$VATP = TAX_VAT;
if($chrgvat == "yes"){
$SUBTOT = sprintf("%0.2f", $TOTAL * 100 / (100 + $VATP) );
} elseif($chrgvat == "no") {
$SUBTOT = $TOTAL;
$TOTAL = sprintf("%0.2f", $TOTAL * (100 + $VATP) /100 );
}else{
$SUBTOT = $TOTAL;
}
// compute the sub total (total - vat), done this way because the specified price already includes vat
$VAT = $TOTAL - $SUBTOT;
/* --- End Clac --- */
$ncdate = "$ncdate_year-$ncdate_month-$ncdate_day";
if (!isset($lead))
$lead = "";
# insert purchase to DB
$sql = "
UPDATE nons_invoices
SET cusname = '$cusname', cusaddr = '$cusaddr', cusvatno = '$cusvatno', chrgvat = '$chrgvat', odate = '$sdate',
subtot = '$SUBTOT', vat = '$VAT', total = '$TOTAL', remarks = '$remarks', lead = '$lead', ncdate = '$ncdate'
WHERE invid = '$invid' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to update quote in Cubit.",SELF);
# commit updating
pglib_transaction ("COMMIT") or errDie("Unable to commit a database transaction.",SELF);
if( !isset($doneBtn) ){
return details($_POST);
} else {
$rslt = db_exec($sql) or errDie("Unable to update invoices status in Cubit.",SELF);
#add lead
if(isset($lead) AND ($lead == "yes")){
db_conn("crm");
$sql = "
INSERT INTO leads (
surname, date, by, con, div, supp_id, cust_id, lead_source, birthdate, reports_to_id, assigned_to,
assigned_to_id, account_id, gender, website, salespid, ncdate, team_id, dept_id, tell, hadd, ref
) VALUES (
'$cusname', 'now', '".USER_NAME."', 'No', '".USER_DIV."', '0', '0', '0', 'now', '0', '".USER_NAME."',
'0', '0', 'Male', 'http://', '0', '$ncdate', '0', '0', '', '$cusaddr', ''
)";
$rslt = db_exec($sql) or errDie ("Unable to add lead to database.");
$lead_id = pglib_lastid("leads", "id");
}
// Final Laytout
$write = "
<script>
printer('nons-quote-print.php?invid=$invid');
</script>
<table ".TMPL_tblDflts.">
<tr>
<th colspan='2'>New Non-Stock Quotes</th>
</tr>
<tr class='".bg_class()."'>
<td>Non-Stock Quotes for Customer <b>$cusname</b> has been recorded.</td>
<td><input type='button' onClick=\"printer('nons-quote-print.php?invid=$invid');\" value='Print Quote'></td>
</tr>
</table>
<p>
<table ".TMPL_tblDflts.">
<tr>
<th>Quick Links</th>
</tr>
<tr class='".bg_class()."'>
<td><a href='nons-quote-view.php'>View Non-Stock Quotes</a></td>
</tr>
<tr class='".bg_class()."'>
<td><a href='main.php'>Main Menu</a></td>
</tr>
</table>";
return $write;
// return "
// <script>
// printer('nons-quote-print.php?invid=$invid');
// document.location='nons-multiline-quote-new.php';
// </script>";
}
}
?>