/
general-creditnote.php
executable file
·1377 lines (1168 loc) · 40.5 KB
/
general-creditnote.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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?
#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");
if (isset($_REQUEST["key"])) {
switch ($_REQUEST["key"]) {
case "write":
$OUTPUT = write($_POST);
break;
case "details":
if(isset($_POST['details'])){
$OUTPUT = details($_POST);
}else{
$OUTPUT = details2($_POST);
}
break;
case "recv_print":
$OUTPUT = recv_print();
break;
default:
$OUTPUT = slctacc ($_POST);
}
} else {
$OUTPUT = slctacc ($_POST);
}
$OUTPUT .=
mkQuickLinks(
ql("general-creditnote.php","Generate General Credit Note"),
ql("core/trans-new.php", "Journal Transactions"),
ql("cust-credit-stockinv.php","New Invoice"),
ql("customers-new.php","Add Customer"),
ql("settings/credit-note-accounts.php","Set Accounts For Use On General Credit Note"),
ql("customers-view.php", "View Customers")
);
# get templete
require("template.php");
function slctacc($_GET, $err="")
{
extract ($_GET);
if(!isset($accid))
$accid = "";
if(!isset($cusnum))
$cusnum = 0;
if(!isset($amount))
$amount = 0;
# validate input
require_lib("validate");
$v = new validate ();
$v->isOk ($cusnum, "num", 0, 50, "Invalid customer id.");
# display errors, if any
if ($v->isError ()) {
$confirm = "";
$errors = $v->getErrors();
foreach ($errors as $e) {
$confirm .= "<li class='err'>-".$e["msg"]."</li>";
}
return $confirm;
}
$refnum = getrefnum();
/*refnum*/
// get customer drop
db_connect();
$sql = "SELECT accno,cusnum,cusname,surname FROM customers WHERE div = '".USER_DIV."' ORDER BY surname,cusname,accno";
$cusRslt = db_exec($sql);
$numrows = pg_numrows($cusRslt);
if(empty($numrows)){
return "<li class='err'> There are no Debtors in Cubit.</li><br>";
}
$cust = "<select name='cusnum'>";
while($cus = pg_fetch_array($cusRslt)){
$sel = "";
if($cus['cusnum'] == $cusnum) {$sel = "selected";}
$cust .= "<option $sel value='$cus[cusnum]'>$cus[accno] - $cus[cusname] $cus[surname]</option>";
}
$cust .= "</select>";
#get list off accounts to use for drop down
$get_accs = "SELECT accid FROM credit_note_accounts";
$run_accs = db_exec($get_accs) or errDie ("Unable to get credit note account information.");
if(pg_numrows($run_accs) < 1){
$accs = array ();
}else {
$accs = array ();
while ($aarr = pg_fetch_array($run_accs)){
$accs[] = $aarr['accid'];
}
}
//".mkAccSelect("accid", $accid)."
# Accounts drop down
core_connect();
$accounts = "<select name='accid'>";
$sql = "SELECT * FROM accounts WHERE div = '".USER_DIV."' ORDER BY accname ASC";
$accRslt = db_exec($sql);
if(pg_numrows($accRslt) < 1){
return "<li>There are No accounts in Cubit.</li>";
}
while($acc = pg_fetch_array($accRslt)){
$sel = "";
if(isset($cacc)){
if($cacc == $acc['accid'])
$sel = "selected";
}
# Check Disable
// if(isDisabled($acc['accid']))
// continue;
if(!in_array($acc['accid'],$accs))
continue;
$accounts .= "<option value='$acc[accid]' $sel>$acc[topacc]/$acc[accnum] - $acc[accname]</option>";
}
$accounts .= "</select>";
$entd = "";
$entc = "checked=yes";
if(isset($tran)){
if($tran == "dt"){
$entd = "checked=yes";
$entc = "";
}
}
$vatsel1 = "";
$vatsel2 = "";
$vatsel3 = "";
if(!isset($vatinc) OR strlen($vatinc) < 1){
$vatsel1 = "checked='yes'";
}elseif ($vatinc == "yes"){
$vatsel1 = "checked='yes'";
}elseif ($vatinc == "no"){
$vatsel2 = "checked='yes'";
}else {
$vatsel3 = "checked='yes'";
}
if (!isset ($ct_day)){
$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);
$ct_year = $date_arr[0];
$ct_month = $date_arr[1];
$ct_day = $date_arr[2];
}else {
$ct_year = date("Y");
$ct_month = date("m");
$ct_day = date("d");
}
}
// Accounts (debit)
$view = "
<h3>Generate Credit Note</h3>
<form action='".SELF."' method='POST' name='form'>
<input type='hidden' name='key' value='details'>
<input type='hidden' name='cusnum' value='$cusnum'>
<input type='hidden' name='entry' value='CT'>
<input type='hidden' name='accnum' value=''>
<input type='hidden' name='details' value=''>
<table ".TMPL_tblDflts.">
$err
<tr>
<th>Field</th>
<th>Value</th>
</tr>
<tr class='".bg_class()."'>
<td>Customer</td>
<td>$cust</td>
</tr>
<tr class='".bg_class()."'>
<td>Date</td>
<td>".mkDateSelect("ct", $ct_year, $ct_month, $ct_day)."</td>
</tr>
<tr class='".bg_class()."'>
<td>Reference Number</td>
<td><input type='text' size='10' name='refnum' value='".($refnum++)."'></td>
</tr>
<tr class='".bg_class()."'>
<td>Amount</td>
<td valign='center'>".CUR." <input type='text' size='16' name='amount' value='".sprint ($amount)."'> Amount Includes VAT<input type='radio' name='vatinc' value='yes' $vatsel1> Amount Excludes VAT<input type='radio' name='vatinc' value='no' $vatsel2> Transaction Has No VAT<input type='radio' name='vatinc' value='novat' $vatsel3></td>
</tr>
<tr class='".bg_class()."'>
<td rowspan='3'>Contra Account</td>
<td>$accounts <a href='settings/credit-note-accounts.php'>Set Accounts For Use On General Credit Note</a></td>
</tr>
<tr class='".bg_class()."'>
<td><input type='checkbox' name='gotstock' value='yes' checked='yes'> Were any stock items returned?</td>
</tr>
<tr class='".bg_class()."'>
<td><li class='err'>Stock Can Only Be Returned By Selecting Inventory Account, and unselecting the above checkbox</li></td>
</tr>
".TBL_BR."
<tr>
<td colspan='2' align='right'><input type='submit' value='Next'></td>
</tr>
".TBL_BR."
</table>";
return $view;
}
# Enter Details of Transaction
function details($_POST,$err="")
{
# Get vars
extract ($_POST);
if (isset($back) AND isStock($accid))
return get_stock_items($_POST);
elseif (isset($back))
return slctacc($_POST);
$accid += 0;
$amount = $amount + 0;
# validate input
require_lib("validate");
$v = new validate ();
$v->isOk ($refnum, "num", 1, 10, "Invalid Reference number.");
$v->isOk ($ct_day, "num", 1,2, "Invalid to Date ct_day.");
$v->isOk ($ct_month, "num", 1,2, "Invalid to Date ct_monthth.");
$v->isOk ($ct_year, "num", 1,4, "Invalid to Date Year.");
$date = $ct_day."-".$ct_month."-".$ct_year;
if(!checkdate($ct_month, $ct_day, $ct_year)){
$v->isOk ($date, "num", 1, 1, "Invalid date.");
}
$v->isOk ($accid, "num", 1, 50, "Invalid Contra Account.");
$v->isOk ($cusnum, "num", 1, 50, "Invalid Customer number.");
$v->isOk ($vatinc, "string", 1, 6, "Invalid Transaction VAT Option.");
if (sprint ($amount) <= 0){
$v->addError($amount, "Invalid Or Too Small Amount Entered.");
}
if ($accid == "0")
$v->addError($accid,"No Allowed Accounts Found. <a href='settings/credit-note-accounts.php'>Please Add One First</a>");
# display errors, if any
if ($v->isError ()) {
$confirm = "";
$errors = $v->getErrors();
foreach ($errors as $e) {
$confirm .= "<li class='err'>".$e["msg"]."</li>";
}
return slctacc($_POST, $confirm."<br>");
}
# CHECK IF THIS DATE IS IN THE BLOCKED RANGE
$blocked_date_from = getCSetting("BLOCKED_FROM");
$blocked_date_to = getCSetting ("BLOCKED_TO");
if (strtotime($date) >= strtotime($blocked_date_from) AND strtotime($date) <= strtotime($blocked_date_to) AND !user_is_admin(USER_ID)){
return slctacc($_POST, "<li class='err'>Period Range Is Blocked. Only an administrator can process entries within this period.</li><br>");
}
#if stock returned is selected ... override the setting
if (isset($gotstock) AND strlen($gotstock) > 0){
#get a stock id
db_conn ('exten');
$get_stkid = "SELECT stkacc FROM warehouses ORDER BY whid ASC LIMIT 1";
$run_stkid = db_exec($get_stkid) or errDie ("Unable to get inventory account information.");
if (pg_numrows($run_stkid) < 1){
return "<li class='err'>No Inventory Account Found.</li>";
}else {
$accid = pg_fetch_result ($run_stkid,0,0);
}
}
# get contra account details
$accRs = get("core","*","accounts","accid",$accid);
$acc = pg_fetch_array($accRs);
#### handle the stock we selected
if((isStock($accid) OR isset($gotstock)) AND !isset($stockcontinue)){
#for whatever reason ... we need to get stock ...
return get_stock_items($_POST);
}
db_connect ();
# Select customer
$sql = "SELECT * FROM customers WHERE cusnum = '$cusnum' AND div = '".USER_DIV."'";
$custRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
if(pg_numrows($custRslt) < 1){
return "<li class='err'>Invalid customer ID, or customer has been blocked.</li>";
}else{
$cust = pg_fetch_array($custRslt);
}
db_conn("exten");
$sql = "SELECT * FROM departments WHERE deptid = '$cust[deptid]' AND div = '".USER_DIV."'";
$deptRslt = db_exec($sql);
if(pg_numrows($deptRslt) < 1){
return "<i class='err'>Customer Department Not Found</i>";
}else{
$dept = pg_fetch_array($deptRslt);
}
#get a stock total
$stock_total = 0;
if(isset($stock_items) AND is_array($stock_items))
foreach ($stock_items AS $each => $own){
$stock_total= $stock_total + ($stock_cost[$each] * $own);
}
$stkamount = 0;
$send_stock = "";
$show_stock = "";
if(isset($stock_items) AND is_array($stock_items)){
$unit_total = array_sum ($stock_items);
if ($unit_total == 0)
$stock_unit_avg_cost = 0;
else
$stock_unit_avg_cost = sprint (($amount - $stock_total) / $unit_total);
foreach ($stock_items AS $each => $own){
if($own > 0){
if(!isset($own) OR strlen($own) < 1)
$own = 1;
if(!isset($stock_cost[$each]) OR strlen($stock_cost[$each]) < 1)
$stock_cost[$each] = 0;
$send_stock .= "<input type='hidden' name='stock_items[$each]' value='$own'>\n";
$send_stock .= "<input type='hidden' name='stock_cost[$each]' value='$stock_cost[$each]'>\n";
db_connect ();
$get_stk = "SELECT stkdes,whid FROM stock WHERE stkid = '$each' LIMIT 1";
$run_stk = db_exec($get_stk) or errDie ("Unable to get stock information.");
if(pg_numrows($run_stk) < 1){
$stock_name = "Unknown";
}else {
$stock_name = pg_fetch_result($run_stk,0,0);
$whid = pg_fetch_result ($run_stk,0,1);
db_conn('exten');
$get_cos = "SELECT cosacc FROM warehouses WHERE whid = '$whid' LIMIT 1";
$run_cos = db_exec($get_cos) or errDie ("Unable to get cost of sale information. (1)");
if (pg_numrows($run_cos) < 1){
$show_cos = "";
}else {
$cos_id = pg_fetch_result ($run_cos,0,0);
db_conn ('core');
$get_acc = "SELECT topacc,accnum,accname FROM accounts WHERE accid = '$cos_id' LIMIT 1";
$run_acc = db_exec ($get_acc) or errDie ("Unable to get cost of sale information. (2)");
if (pg_numrows($run_acc) < 1){
$show_cos = "";
}else {
$aarr = pg_fetch_array ($run_acc);
$show_cos = "$aarr[topacc]/$aarr[accnum] - $aarr[accname]";
}
}
}
if (!isset($stock_prof[$each]))
$stock_prof[$each] = sprint ($stock_unit_avg_cost * $own);
$show_stock .= "
<tr class='".bg_class()."'>
<td colspan='2'>$stock_name</td>
<td>$own</td>
<td nowrap>".CUR." ".sprint ($stock_cost[$each])."</td>
<td nowrap>".CUR." ".sprint ($stock_cost[$each] * $own)."</td>
<td><input type='text' size='7' name='stock_prof[$each]' value='$stock_prof[$each]'></td>
<td nowrap>$show_cos</td>
</tr>
";
$stkamount = $stkamount + ($stock_cost[$each] * $own);
}
}
}
if(strlen($send_stock) == 0){
$send_stock = "<input type='hidden' name='stock_items' value='0'>";
$send_stock .= "<input type='hidden' name='stock_cost' value='0'>";
$send_stock .= "<input type='hidden' name='stock_prof' value='0'>";
$get_gds_note = "";
}else {
$send_stock .= "<input type='hidden' name='stockcontinue' value='0'>";
$send_stock .= "<input type='hidden' name='gotstock' value='1'>";
if (isset($gds_note) AND strlen($gds_note) > 0){
$gds_note_sel = "checked='yes'";
}else {
$gds_note_sel = "";
}
$get_gds_note = "
<tr class='".bg_class()."'>
<td>Print Goods Received Note</td>
<td><input type='checkbox' name='gds_note' value='yes' $gds_note_sel></td>
</tr>
";
}
db_conn ('core');
$get_sales_acc = "SELECT topacc,accnum,accname FROM accounts WHERE accid = '$dept[incacc]' LIMIT 1";
$run_sales_acc = db_exec($get_sales_acc) or errDie ("Unable to get sales account information. (1)");
if (pg_numrows($run_sales_acc) < 1){
$show_sales = "";
}else {
$sarr = pg_fetch_array ($run_sales_acc);
$show_sales = "$sarr[topacc]/$sarr[accnum] - $sarr[accname]";
}
if(strlen($show_stock) > 0){
$show_stock = "
<tr>
<th colspan='2'>Stock Description</th>
<th>Number Of Units Returned</th>
<th>Unit Cost</th>
<th>Total</th>
<th>Profit/Loss</th>
<th>Cost Of Sale Account</th>
</tr>
$show_stock
<tr class='".bg_class()."'>
<td colspan='4' align='right'><b>Total:</b></td>
<td nowrap>".CUR." ".sprint($stock_total)."</td>
<td colspan='2' nowrap><li class='err'>Difference ($amount - ".sprint ($stock_total)."): ".CUR." ".sprint ($amount - $stock_total)."</li></td>
</tr>
<tr>
<td colspan='7'><li class='err'>Sales Account To Be Used: $show_sales</li></td>
</tr>";
}
# Probe tran type
if($entry == "CT"){
$tran = "
<tr class='".bg_class()."'>
<td colspan='3'>$acc[topacc]/$acc[accnum] - $acc[accname]</td>
<td colspan='2'>$cust[accno] - $cust[cusname] $cust[surname]</td>
</tr>";
}else{
$tran = "
<tr class='".bg_class()."'>
<td colspan='3'>$cust[accno] - $cust[cusname] $cust[surname]</td>
<td colspan='2'>$acc[topacc]/$acc[accnum] - $acc[accname]</td>
</tr>";
}
if(!isset($amount)) {
$amount = $stkamount;
$details = "";
}
if(!isset($vataccid))
$vataccid = 0;
if(isset($vatinc) AND $vatinc != "novat"){
db_connect ();
$get_vatcodes = "SELECT * FROM vatcodes ORDER BY code";
$run_vatcodes = db_exec($get_vatcodes) or errDie ("Unable to get vat code inoformation.");
if(pg_numrows($run_vatcodes) < 1){
return "No Vatcodes Found. Please Add One First.";
}else {
$vatcode_drop = "<select name='vatcode'>";
while ($varr = pg_fetch_array ($run_vatcodes)){
$vatcode_drop .= "<option value='$varr[id]'>($varr[code]) $varr[description]</option>";
}
$vatcode_drop .= "</select>";
}
db_conn('core');
$vatacc = "<select name='vataccid'>";
$sql = "SELECT * FROM accounts WHERE div = '".USER_DIV."' ORDER BY accname ASC";
$accRslt = db_exec($sql);
if(pg_numrows($accRslt) < 1){
return "<li>There are No accounts in Cubit.</li>";
}
$vatacc_newid = gethook("accnum", "salesacc", "name", "VAT", "VAT");
$vataccid = getCSetting("CRED_NOTE_VAT_ACC");
if (!isset($vataccid) OR (strlen($vataccid) < 1) OR $vataccid == "0")
$vataccid = $vatacc_newid;
// if (!isset($vataccid) OR $vataccid == "0")
// $vataccid = $vatacc_newid;
// while($acc_arr = pg_fetch_array($accRslt)){
// # Check Disable
// if(isDisabled($acc_arr['accid']))
// continue;
// if($vataccid == $acc_arr['accid']) {
// $sel = "selected";
// } else {
// $sel = "";
// }
// $vatacc .= "<option value='$acc_arr[accid]' $sel>$acc_arr[topacc]/$acc_arr[accnum] - $acc_arr[accname]</option>";
// }
// $vatacc .= "</select>";
if(isset($vatdedacc) AND $vatdedacc == "dt")
$dsel1 = "checked='yes'";
else
$dsel1 = "";
if(isset($vatdedacc) AND $vatdedacc == "ct")
$dsel2 = "checked='yes'";
else
$dsel2 = "";
if($dsel1 == "" AND $dsel2 == "")
$dsel1 = "checked='yes'";
if (isStock($accid) AND isset($gotstock)){
$showgetvatacc = "<input type='hidden' name='vatdedacc' value='dt'>";
}else {
$showgetvatacc = "
<tr class='".bg_class()."'>
<td colspan='2'valign='top'>VAT Deductable Account</td>
<td colspan='2'>
<input type='radio' name='vatdedacc' value='dt' $dsel1 />$acc[topacc]/$acc[accnum] - $acc[accname]<br />
<input type='radio' name='vatdedacc' value='ct' $dsel2 />$cust[accno] - $cust[surname]
</td>
</tr>
";
}
db_conn ('core');
$get_vatacc = "SELECT accname FROM accounts WHERE accid = '$vataccid' LIMIT 1";
$run_vatacc = db_exec($get_vatacc) or errDie ("Unable to get vat account details.");
if (pg_numrows($run_vatacc) < 1){
$showvatacc = "";
}else {
$vatacc_id = pg_fetch_result ($run_vatacc,0,0);
$showvatacc = "
<tr class='".bg_class()."'>
<td colspan='2'>VAT Account</td>
<td colspan='3'>$vatacc_id <a target='_blank' href='settings/credit-note-accounts.php'>Change Account</a></td>
</tr>
";
}
// <tr class='".bg_class()."'>
// <td colspan='2'>VAT Account</td>
// <td colspan='3'>$vatacc</td>
// </tr>
$get_vats = "
".TBL_BR."
<input type='hidden' name='vataccid' value='$vataccid'>
<tr>
<th colspan='5'>VAT Detail</th>
</tr>
$showgetvatacc
$showvatacc
<tr class='".bg_class()."'>
<td colspan='2'>VAT Code</td>
<td colspan='3'>$vatcode_drop</td>
</tr>
".TBL_BR."
";
}else {
$get_vats = "
<input type='hidden' name='vatinc' value='novat'>
<input type='hidden' name='vatcode' value='0'>
";
}
// Layout Details
$details = "
<h3>Confirm Credit Note Details</h3>
$err
<form action='".SELF."' method='POST' name='form'>
<input type='hidden' name='key' value='write'>
<input type='hidden' name='type' value='1'>
<input type='hidden' name='date' value='$date'>
<input type='hidden' name='cusnum' value='$cusnum'>
<input type='hidden' name='accid' value='$accid'>
<input type='hidden' name='accname' value='$acc[accname]'>
<input type='hidden' name='entry' value='$entry'>
<input type='hidden' name='ct_day' value='$ct_day'>
<input type='hidden' name='ct_month' value='$ct_month'>
<input type='hidden' name='ct_year' value='$ct_year'>
<input type='hidden' name='vatinc' value='$vatinc'>
<input type='hidden' name='ac' value=''>
<input type='hidden' name='amount' value='$amount'>
<input type='hidden' name='refnum' value='$refnum'>
<input type='hidden' name='difference' value='".sprint ($amount-$stock_total)."'>
$send_stock
<table ".TMPL_tblDflts." width='500'>
<tr>
<th colspan='3'>Debit</th>
<th colspan='2'>Credit</th>
</tr>
$tran
<tr><td><br></td></tr>
<tr><td><br></td></tr>
$show_stock
$get_vats
</table>
<table ".TMPL_tblDflts." width='500'>
".TBL_BR."
<tr>
<th colspan='2'>Transaction Details</th>
</tr>
<tr class='".bg_class()."'>
<td>Transaction Details</td>
<td valign='center'><textarea cols='30' rows='5' name='details'>$details</textarea></td>
</tr>
$get_gds_note
<tr class='".bg_class()."'>
<td>Person Authorising</td>
<td valign='center'><input type='hidden' size='20' name='author' value=".USER_NAME.">".USER_NAME."</td>
</tr>
<tr><td><br></td></tr>
<tr>
<td><input type='submit' name='back' value='« Correction'></td>
<td valign='center' align='right'><input type='submit' value='Confirm »'></td>
</tr>
</table>
</form>";
return $details;
}
# Write
function write($_POST)
{
# Get vars
extract ($_POST);
if(isset($back)) {
return details($_POST);
}
# validate input
require_lib("validate");
$v = new validate ();
$v->isOk ($cusnum, "num", 1, 50, "Invalid Customer number.");
$v->isOk ($accid, "num", 1, 50, "Invalid Contra Account.");
$v->isOk ($refnum, "num", 1, 10, "Invalid Reference number.");
$v->isOk ($amount, "float", 1, 20, "Invalid Amount.");
$v->isOk ($details, "string", 0, 255, "Invalid Details.");
$v->isOk ($author, "string", 1, 30, "Invalid Authorising person name.");
$v->isOk ($vatinc, "string", 1, 6, "Invalid Transaction VAT Option.");
$datea = explode("-", $date);
if(count($datea) == 3){
if(!checkdate($datea[1], $datea[0], $datea[2])){
$v->isOk ($date, "num", 1, 1, "Invalid date.");
}
}else{
$v->isOk ($date, "num", 1, 1, "Invalid date.");
}
# display errors, if any
if ($v->isError ()) {
$write = "";
$errors = $v->getErrors();
foreach ($errors as $e) {
$write .= "<li class='err'>".$e["msg"]."</li>";
}
$write .= "<p><input type='button' onClick='JavaScript:history.back();' value='« Correct submission'>";
return $write;
}
if ((isset($stock_prof) AND is_array ($stock_prof)) AND sprint (array_sum ($stock_prof)) != $difference){
return details ($_POST,"<li class='err'>Please ensure differences matches total difference.</li>");
}
//print "<pre>";
//var_dump ($_POST);
//print "</pre>";
$date = "$datea[2]-$datea[1]-$datea[0]";
# Accounts details
$accRs = get("core","*","accounts","accid",$accid);
$acc = pg_fetch_array($accRs);
# Select customer
db_connect();
$sql = "SELECT * FROM customers WHERE cusnum = '$cusnum' AND div = '".USER_DIV."'";
$custRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
if(pg_numrows($custRslt) < 1){
return "<li class='err'>Invalid customer ID, or customer has been blocked.</li>";
}else{
$cust = pg_fetch_array($custRslt);
}
# Get department
db_conn("exten");
$sql = "SELECT * FROM departments WHERE deptid = '$cust[deptid]' AND div = '".USER_DIV."'";
$deptRslt = db_exec($sql);
if(pg_numrows($deptRslt) < 1){
return "<i class='err'>Customer Department Not Found</i>";
}else{
$dept = pg_fetch_array($deptRslt);
}
# Begin updates
pglib_transaction ("BEGIN") or errDie("Unable to start a database transaction.",SELF);
# Probe tran type
if($entry == "CT"){
$refnum = getrefnum();
#update stock ...
$stock_total = 0;
if(isset($stock_items) AND is_array($stock_items)){
$used_stock = TRUE;
foreach ($stock_items AS $stkid => $unitnum){
db_connect();
$sql = "SELECT * FROM stock WHERE stkid = '$stkid' AND div = '".USER_DIV."'";
$stkRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
if(pg_numrows($stkRslt) < 1){
return "<li> Invalid Stock ID.</li>";
}else{
$stk = pg_fetch_array($stkRslt);
}
if($stk['units'] < 0) {
$min_stock = abs($stk['units']);
if ( $unitnum < $min_stock ) {
$min_stock = $unitnum;
}
} else {
$min_stock=0;
}
# Get warehouse name
db_conn("exten");
$sql = "SELECT * FROM warehouses WHERE whid = '$stk[whid]' AND div = '".USER_DIV."'";
$whRslt = db_exec($sql);
$wh = pg_fetch_array($whRslt);
# calculate actual cost amount
// $temp = $cost;
// $cost = sprint($cost * $unitnum);
#temp = unitprice
$temp = sprint ($stock_cost[$stkid]);
#cost = total price
$cost = sprint ($stock_cost[$stkid]*$unitnum);
$cost_amt = sprint ($cost);
$stock_total = $stock_total + $cost_amt;
$stock_cost[$stkid] = $cost_amt/$unitnum;
#temp = unitprice
$temp = sprint ($stock_cost[$stkid]);
#cost = total price
$cost = sprint ($stock_cost[$stkid]*$unitnum);
$tipo = "Increase";
if($tipo == 'Increase'){
/* do the journals for stock sold before purchase
this will only be done by a purchase */
if($min_stock>0) {
db_conn("exten");
$sql = "SELECT stkacc,cosacc FROM warehouses WHERE whid = '$stk[whid]' AND div = '".USER_DIV."'";
$whRslt = db_exec($sql);
$wh = pg_fetch_array($whRslt);
$stockacc = $wh['stkacc'];
$cosacc = $wh['cosacc'];
}
# Update Stock
db_connect();
$sql = "UPDATE stock
SET units = (units + '$unitnum'),
lcsprice = '$temp',
csamt = (csamt + $cost),
csprice = (
SELECT
CASE WHEN (units != -$unitnum) THEN (csamt+$cost)/(units+$unitnum)
ELSE 0
END
FROM cubit.stock
WHERE stkid = '$stkid' AND div = '".USER_DIV."'
)
WHERE stkid = '$stkid' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to insert stock to Cubit.",SELF);
$sdate = $date;
# stkid, stkcod, stkdes, trantype, edate, qty, csamt, details
stockrec($stk['stkid'], $stk['stkcod'], $stk['stkdes'], 'dt', $sdate, $unitnum, $cost, $details);
db_connect();
if ($unitnum == 0) {
$csprice = 0;
} else {
$csprice = sprint($cost/$unitnum);
}
$sql = "INSERT INTO stockrec(edate, stkid, stkcod, stkdes, trantype, qty, csprice, csamt, details, div)
VALUES('$sdate', '$stk[stkid]', '$stk[stkcod]', '$stk[stkdes]', 'note', '$unitnum', '".sprint ($cost+$stock_prof[$stkid])."', '$csprice', '$details', '".USER_DIV."')";
$recRslt = db_exec($sql);
db_connect();
$sql = "SELECT * FROM stock WHERE stkid = '$stkid' AND div = '".USER_DIV."'";
$stkRslt = db_exec($sql) or errDie("Unable to access databse.", SELF);
if(pg_numrows($stkRslt) < 1){
return "<li> Invalid Stock ID.</li>";
}else{
$stk = pg_fetch_array($stkRslt);
}
# balance transaction
# Debit STock account and Credit Contra Account
#ct was $dept['debtacc']
writetrans($wh['stkacc'], $wh['cosacc'], $date, $refnum, $cost, "Cost Of Sales for: $details for Customer: $cust[surname]");
$cc_trantype = cc_TranTypeAcc($wh['stkacc'], $dept['debtacc']);
}
}
}else {
$used_stock = FALSE;
}
#we'll handle the discrincy seperately
if ($stock_total != 0){
$amount_dif = sprint ($amount - $stock_total);
// $amount = sprint ($stock_total);
}else {
$amount_dif = 0;
// $amount = sprint ($amount);
}
#do vat trans ...
if(isset($vatinc) AND $vatinc != "novat"){
#process vat
db_connect ();
$Sl = "SELECT * FROM vatcodes WHERE id='$vatcode'";
$Ri = db_exec($Sl);
$vd = pg_fetch_array($Ri);
$VATP = $vd['vat_amount'];
#calculate amounts
if($vatinc == 'yes'){
$vatamt = sprint((($amount/($VATP + 100)) * $VATP));
$amt = sprint($amount - $vatamt);
$totamt = sprint($amount);
}else{
$vatamt = sprint((($VATP/100) * $amount));
$amt = sprint($amount);
$totamt = sprint($amount + $vatamt);
}
$datea = explode("-", $date);
$cdate = $date;
# Check VAt Deductable account
if($vatdedacc == 'dt'){
vatr($vd['id'],$cdate,"INPUT",$vd['code'],$refnum,"$details VAT",-$totamt,-$vatamt);
writetrans($vataccid, $dept['debtacc'], $date, $refnum, $vatamt, "VAT Return for: $details");
}elseif($vatdedacc == 'ct'){
vatr($vd['id'],$cdate,"OUTPUT",$vd['code'],$refnum,"$details. VAT",$totamt,$vatamt);
writetrans($accid, $vataccid, $date, $refnum, $vatamt, "VAT Return for: $details");
}
if ($used_stock){
$accid = $dept['incacc'];
$details = "Debtors Control for: $details";
}
writetrans($accid, $dept['debtacc'], $date, $refnum, $amt, $details." - Customer $cust[cusname] $cust[surname]");
}else{
$totamt = sprint($amount);
$amt = sprint ($amount);
$getacc_arr = getAccn("2190","000");
$getgacc = $getacc_arr['accid'];
if ($used_stock){
$accid = $dept['incacc'];
$details = "Debtors Control for: $details";
}
# Write transaction (debit contra account, credit debtors control)
writetrans($accid, $dept['debtacc'], $date, $refnum, $totamt, $details." - Customer $cust[cusname] $cust[surname]");
}
$tran = "
<tr class='".bg_class()."'>
<td>$acc[topacc]/$acc[accnum] - $acc[accname]</td>
<td>$cust[accno] - $cust[cusname] $cust[surname]</td>
</tr>";
$samount = ($amount - ($amount * 2));
recordCT($samount, $cust['cusnum'],$date);
$type = 'c';
}
db_connect();
$stotamt = ($totamt - ($totamt * 2));
$sdate = date("Y-m-d");
# record the payment on the statement
$sql = "
INSERT INTO stmnt
(cusnum, invid, amount, date, type, st, div, allocation_date)
VALUES
('$cust[cusnum]', '0', '$stotamt', '$date', '$details', 'n', '".USER_DIV."', '$date')";
$stmntRslt = db_exec($sql) or errDie("Unable to Insert statement record in Cubit.",SELF);
$sql = "INSERT INTO open_stmnt(cusnum, invid, amount, balance, date, type, st, div) VALUES('$cust[cusnum]', '0', '$samount', '$samount', '$date', '$details', 'n', '".USER_DIV."')";
$stmntRslt = db_exec($sql) or errDie("Unable to Insert statement record in Cubit.",SELF);
# update the customer (make balance more)
$sql = "UPDATE customers SET balance = (balance + '$samount') WHERE cusnum = '$cust[cusnum]' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to update customer in Cubit.",SELF);
# Make ledge record
custledger($cust['cusnum'], $accid, $date, $refnum, $details, $totamt, $type);
db_connect ();
$get_credid = "SELECT last_value FROM seq WHERE type = 'cred_note' LIMIT 1";
$run_credid = db_exec($get_credid) or errDie ("Unable to get credit note number.");
$crednote_num = pg_fetch_result ($run_credid,0,0);
if(!isset($vataccid))
$vataccid = 0;
if(!isset($vatamt))
$vatamt = 0;
if(!isset($vatdedacc))
$vatdedacc = 0;
if(!isset($vatcode))
$vatcode = 0;
#record this credit note for records ...
$ins_sql = "