/
core-settings.php
executable file
·2460 lines (2135 loc) · 76.9 KB
/
core-settings.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
<?
/**
* Generally used functions/constants, login logic also
* @package Cubit
* @subpackage CoreSettings
*/
# If this script is called by itself, abort
if (basename (getenv ("SCRIPT_NAME")) == "core-settings.php") {
exit;
}
$allowed = array("yr-close.php", "set-bal-sheet-edit.php", "set-bal-sheet.php");
# Get all diabled accounts
db_conn("exten");
$sql = "SELECT debtacc,credacc FROM departments";
$rs = db_exec($sql);
while($dis = pg_fetch_array($rs)){
$DISABLE[] = $dis['credacc'];
$DISABLE[] = $dis['debtacc'];
}
$sql = "SELECT stkacc FROM warehouses";
$disRslt = db_exec($sql) or errDie("Could not retrieve warehouses Information from the Database.",SELF);
while($dis = pg_fetch_array($disRslt)){
$DISABLE[] = $dis['stkacc'];
}
db_conn('cubit');
$sql = "SELECT value FROM set WHERE label = 'BLOCK'";
$Ri=db_exec($sql);
$data=pg_fetch_array($Ri);
$blocked = array();
if ( $data['value'] == "use" ) {
db_conn('core');
$Sl = "SELECT accid FROM trial_bal WHERE vat='t'";
$Ri = db_exec($Sl);
while ( $data = pg_fetch_array($Ri) ) {
$blocked[] = $data['accid'];
}
}
/**
* account types: all accounts
*
*/
define("ACCTYPE_ALL", false);
/**
* account types: balance accounts
*
*/
define("ACCTYPE_B", 0x01);
/**
* account types: expense accounts
*
*/
define("ACCTYPE_E", 0x02);
/**
* account types: income accounts
*
*/
define("ACCTYPE_I", 0x04);
/**
* account types: income/expense accounts
*
*/
define("ACCTYPE_IE", 0x08);
/**
* functions
*/
/**
* returns current year net profit/loss
*
* @param int $div branch id
* @param year $year optionally a previous year schema name
* @return unknown
*/
function getNetProfit($div = USER_DIV, $year = "core") {
$sql = "SELECT SUM(tb.credit)-SUM(tb.debit) AS netprofit
FROM core.accounts acc LEFT JOIN $year.trial_bal tb
ON acc.accid=tb.accid AND acc.div=tb.div
WHERE (acc.acctype='I' OR acc.acctype='E') AND acc.div='$div'
AND tb.period='12'";
$rslt = db_exec($sql) or errDie("Error calculating current year profit or loss (QR).");
return sprint(pg_fetch_result($rslt, 0, 0));
}
/**
* returns a previous year net profit/loss
*
* function can be ignored and getNetProfit() can be used instead.
*
* @param int $div branch id
* @ignore
* @param year $year optionally a previous year schema name
* @return unknown
*/
function getNetProfit_py($div = USER_DIV, $year = YR_DB) {
$sql = "SELECT SUM(yb.credit)-SUM(yb.debit) AS netprofit
FROM core.accounts acc LEFT JOIN $year.year_balance yb
ON acc.accid=yb.accid AND acc.div=yb.div
WHERE (acc.acctype='I' OR acc.acctype='E') AND acc.div='$div'";
$rslt = db_exec($sql) or errDie("Error calculating previous year profit or loss (QR).");
$p = sprint(pg_fetch_result($rslt, 0, 0));
return $p;
}
/**
* records a vat report entry
*
* @param int $cid
* @param string $date
* @param string $type
* @param string $code
* @param int $ref
* @param string $description
* @param float $amount
* @param float $vat
*/
function vatr($cid, $date, $type, $code, $ref, $description, $amount, $vat)
{
$amount += 0;
$cid += 0;
if ($amount != 0 && $cid != 0) {
db_conn('cubit');
$sdate = date("Y-m-d");
$cid += 0;
$Sl = "
INSERT INTO vatreport (
cid, date, sdate, type, code, ref, description, amount, vat
) VALUES (
'$cid', '$date', '$sdate', '$type', '$code', '$ref', '$description', '$amount', '$vat'
)";
$Ri = db_exec($Sl) or errDie("unable to isert vat record.$Sl");
}
}
/**
* returns vatcode id from vatcode number
*
* @param int $codenum vatcode number
* @return int
*/
function vatcode($codenum) {
if (($vc = qryVatcodeC($codenum)) === false) {
errDie("Vatcode $codenum not found.");
} else {
return $vc["id"];
}
}
/**
* returns vat amount from vatcode number
*
* @param int $id vatcode id
* @return int
*/
function vatamountID($id) {
if (($vc = qryVatcode($id)) === false) {
return TAX_VAT;
} else {
return $vc["vat_amount"];
}
}
/**
* returns vat amount from vatcode number
*
* @param int $codenum vatcode number
* @return int
*/
function vatamountCODE($codenum) {
if (($vc = qryVatcodeC($codenum)) === false) {
return TAX_VAT;
} else {
return $vc["vat_amount"];
}
}
/**
* alias for getMonthName()
*
* @param int $month
* @return string
*/
function prdname($prd){
return getMonthName($prd);
}
/**
* returns next available reference number
*
* @return int
*/
function getrefnum(){
$rn = new dbSelect("max_refnum", "core", grp(
m("cols", "max(refnum)")
));
$rn->run();
return $rn->fetch_result() + 1;
}
/**
* returns bank ledger account id from bank account
*
* @param int $bankid
* @return int
*/
function getbankaccid($bankid){
# Get hook account number
core_connect();
$sql = "SELECT * FROM bankacc WHERE accid = '$bankid' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to retrieve bank account link from Cubit",SELF);
# Check if link exists
if(pg_numrows($rslt) < 1){
errDie("<li class='err'> ERROR : The bank account that you selected doesn't appear to have an account linked to it.");
}
if (($banklnk = pg_fetch_array($rslt)) !== false) {
return $banklnk['accnum'];
} else {
return false;
}
}
/**
* returns a bank account id for bank account linked to specified ledger account
*
* @param int $accid
* @return int
*/
function getbankid($accid){
# Get hook account number
core_connect();
$sql = "SELECT * FROM bankacc WHERE accnum = '$accid' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to retrieve bank account link from Cubit",SELF);
# Check if link exists
if(pg_numrows($rslt) < 1){
errDie("<li class='err'> ERROR : The bank account that you selected doesn't appear to have an account linked to it.");
}
$banklnk = pg_fetch_array($rslt);
return $banklnk['accid'];
}
/**
* checks if account is linked to bank account
*
* @param int $accid
* @return bool
*/
function isbank($accid){
# Get hook account number
core_connect();
$sql = "SELECT * FROM bankacc WHERE accnum = '$accid' AND accid != 0 AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to retrieve check bank account link.",SELF);
# Check if link exists
if(pg_numrows($rslt) > 0){
return true;
}else{
return false;
}
}
/**
* @ignore
*/
function isBankRec($accid, $trantype, $date, $name, $descript, $cheqnum, $totamt, $accinv, $chrgvat = "nov", $vat = 0){
# Date format
$date = explode("-", $date);
$date = "$date[2]-$date[1]-$date[0]";
if(isbank($accid)){
$bankid = getbankid($accid);
# Record the payment record
db_connect();
$sql = "
INSERT INTO cashbook (
bankid, trantype, date, name, descript, cheqnum,
amount, vat, chrgvat, banked, accinv, div
) VALUES (
'$bankid', '$trantype', '$date', '$name', '$descript', '$cheqnum',
'$totamt', '$vat', '$chrgvat', 'no', '$accinv', '".USER_DIV."'
)";
$Rslt = db_exec ($sql) or errDie ("Unable to add bank payment to database.",SELF);
}
}
/**
* @ignore
*/
function isBankmRec($accid, $trantype, $date, $name, $descript, $cheqnum, $totamt, $accinv, $amounts, $accids, $vats, $chrgvats){
# Date format
$date = explode("-", $date);
$date = "$date[2]-$date[1]-$date[0]";
if(isbank($accid)){
$bankid = getbankid($accid);
# Record the payment record
db_connect();
$sql = "
INSERT INTO cashbook (
bankid, trantype, date, name, descript, cheqnum,
amount, vat, chrgvat, banked, accids, amounts,
chrgvats, vats, div
) VALUES (
'$bankid', '$trantype', '$date', '$name', '$descript', '$cheqnum',
'$totamt', '0', 'nov', 'no', '$accids', '$amounts',
'$chrgvats', '$vats', '".USER_DIV."'
)";
$Rslt = db_exec ($sql) or errDie ("Unable to add bank payment to database.",SELF);
}
}
/**
* checks if account is linked to petty cash account
*
* @param int $accid
* @return bool
*/
function ispetty($accid){
# Get hook account number
core_connect();
$sql = "SELECT * FROM bankacc WHERE accnum = '$accid' AND name = 'Petty Cash' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to retrieve check bank account link.",SELF);
# Check if link exists
if(pg_numrows($rslt) > 0){
return true;
}else{
return false;
}
}
/**
* @ignore
*/
function pettyrec($accid, $date, $type, $descript, $amount, $details){
if($type == 'dt'){
$typedef = 'Transfer';
}else{
$typedef = 'Req';
$amount = -$amount;
}
if(ispetty($accid)){
db_connect();
# Record tranfer for patty cash report
$sql = "
INSERT INTO pettyrec (
date, type, det, amount, name, div
) VALUES (
'$date', '$typedef', '$descript', '$amount', '$details', '".USER_DIV."'
)";
$Rslt = db_exec ($sql) or errDie ("Unable to add bank payment to database.",SELF);
}
}
/**
* @ignore
*/
function intInvoice($cusnum, $total,$account)
{
db_connect();
$sql = "SELECT * FROM customers WHERE cusnum = '$cusnum' AND div = '".USER_DIV."'";
$custRslt = db_exec ($sql) or errDie ("Unable to view customer");
$cus = pg_fetch_array($custRslt);
# Default data
$sdate = date("Y-m-d");
$invnum = divlastid('inv', USER_DIV);
db_connect();
# Insert purchase to DB
$sql = "
INSERT INTO nons_invoices (
cusid, cusname, cusaddr, cusvatno, chrgvat,
sdate, subtot, balance, vat, total,
done, username, prd, invnum, typ,
descrip, div, ctyp, odate
) VALUES (
'$cus[cusnum]', '$cus[surname]', '$cus[addr1]', '$cus[vatnum]', 'none',
'$sdate', '$total', '$total', 0, '$total',
'y', '".USER_NAME."', '".PRD_DB."', '$invnum', 'inv',
'Interest Charged', '".USER_DIV."', 's', '$sdate'
)";
$rslt = db_exec($sql) or errDie("Unable to create Interest Invoice.",SELF);
# Get next ordnum
$invid = lastinvid();
$Sl = "SELECT * FROM vatcodes WHERE zero='Yes'";
$Ri = db_exec($Sl);
$vd = pg_fetch_array($Ri);
# Insert purchase items
$sql = "
INSERT INTO nons_inv_items (
invid, qty, amt, unitcost, description,
div, vatex, accid
) VALUES (
'$invid', '1', '$total', '$total', 'Interest on Outstanding Invoices.',
'".USER_DIV."','$vd[id]','$account'
)";
$rslt = db_exec($sql) or errDie("Unable to insert invoice items to Cubit.",SELF);
return $invnum;
}
/**
* @ignore
*/
function fintInvoice($cusnum, $total, $rate)
{
$ftotal = sprint($total * $rate);
db_connect();
$sql = "SELECT * FROM customers WHERE cusnum = '$cusnum' AND div = '".USER_DIV."'";
$custRslt = db_exec ($sql) or errDie ("Unable to view customer");
$cus = pg_fetch_array($custRslt);
$curr = getSymbol($cus['fcid']);
$xrate = getRate ($cus['fcid']);
# Default data
$sdate = date("Y-m-d");
$invnum = divlastid('inv', USER_DIV);
db_connect();
# Insert purchase to DB
$sql = "
INSERT INTO nons_invoices (
cusid, cusname, cusaddr, cusvatno, chrgvat,
fcid, currency, xrate, sdate, subtot,
balance, fbalance, vat, total, done,
username, prd, invnum, typ, ctyp,
tval, location, descrip, div, odate
) VALUES (
'$cus[cusnum]', '$cus[cusname] $cus[surname]', '$cus[addr1]', '$cus[vatnum]', 'none',
'$cus[fcid]', '$curr[symbol]', '$xrate', '$sdate', '$total',
'$ftotal', '$total', 0, '$total', 'y',
'".USER_NAME."', '".PRD_DB."', '$invnum', 'inv', 's',
'$cusnum', 'int', 'Interest Charged', '".USER_DIV."', '$sdate'
)";
$rslt = db_exec($sql) or errDie("Unable to create Interest Invoice.",SELF);
# Get next ordnum
$invid = lastinvid();
# Insert purchase items
$sql = "
INSERT INTO nons_inv_items (
invid, qty, amt, cunitcost, unitcost,
description, div
) VALUES (
'$invid', '1', '$total', '$ftotal', '$total',
'Interest on Outstanding Invoices.', '".USER_DIV."'
)";
$rslt = db_exec($sql) or errDie("Unable to insert invoice items to Cubit.",SELF);
return $invnum;
}
/**
* @ignore
*/
function core_createcat($catname, $div, $type) {
core_connect();
# In case no upper case
$type = strtoupper($type);
switch($type){
case "I":
$tab = "income";
break;
case "B":
$tab = "balance";
break;
case "E":
$tab = "expenditure";
break;
default:
return "<li> Invalid Category type";
}
# Make seq
$seq = $tab."_seq";
# Insert Category
$sql = "INSERT INTO $tab (catid, catname, div) VALUES ('$type' || nextval('$seq'), '$catname', '$div')";
$catRslt = db_exec ($sql) or errDie ("Unable to add Category to Database.");
# Get last inserted id for new cat
$catid = pglib_getlastid ("$seq");
$catid = $type.$catid;
return $catid;
}
/**
* @ignore
*/
function core_createacc($topacc, $accnum, $accname, $catid, $acctype, $vat, $div)
{
global $PRDMON;
# In case no upper case
$acctype = strtoupper($acctype);
core_connect();
# Check account number on selected branch
$sql = "SELECT * FROM accounts WHERE topacc = '$topacc' AND accnum = '$accnum' AND div = '$div'";
$cRslt = db_exec ($sql) or errDie ("Unable to retrieve Account details from database.");
if (pg_numrows($cRslt) > 0) {
return 1;
}
# Check account name on selected branch
$sql = "SELECT * FROM accounts WHERE accname = '$accname' AND div = '$div'";
$cRslt = db_exec ($sql) or errDie ("Unable to retrieve Account details from database.");
if (pg_numrows($cRslt) > 0) {
return 2;
}
# Write to DB
$Sql = "
INSERT INTO accounts (
topacc, accnum, accname, acctype, catid, div
) VALUES (
'$topacc', '$accnum', '$accname', '$acctype', '$catid', '$div'
)";
$accRslt = db_exec ($Sql) or errDie ("Unable to add Account to Database.", SELF);
# Get last inserted id for new acc
$accid = pglib_lastid ("accounts", "accid");
# Insert account into trial Balance
foreach ($PRDMON as $map_prd => $map_mon) {
$query = "
INSERT INTO trial_bal (
accid, topacc, accnum, accname, div, month, period
) VALUES (
'$accid', '$topacc', '$accnum', '$accname', '$div', '$map_mon', '$map_prd'
)";
$trialRslt = db_exec($query) or errDie ("Unable to add Account to Database.", SELF);
}
# Return Zero on success
return 0;
}
/**
* checks if an account exists
*
* @param int $accid
* @return bool
*/
function accExist($accid){
$tranRs = get("core", "accid", "accounts", "accid", $accid);
if(pg_numrows($tranRs) > 0){
return true;
}else{
return false;
}
}
/**
* @ignore
*/
function getSetDes($label){
$label = strtoupper($label);
$setRs = undget("cubit", "descript", "set", "upper(label)", $label);
if(pg_numrows($setRs) > 0){
$set = pg_fetch_array($setRs);
return $set['descript'];
}
}
/**
* @ignore
*/
function getErrAcc(){
# Get error account
$errRs = undget("core", "*", "accounts", "topacc", "999' AND accnum = '998' AND div = '".USER_DIV);
if(pg_numrows($errRs) < 1){
$catid = core_createcat("ERROR ACCOUNTS", USER_DIV, "B");
if(core_createacc("999", "998", "Error Transactions", $catid, "B", "f", USER_DIV) != 0){
errDie ("Unable to create Error Trnsactions account.");
}
$errRs = undget("core", "*", "accounts", "topacc", "999' AND accnum = '998' AND div = '".USER_DIV);
}
$err = pg_fetch_array($errRs);
return $err['accid'];
}
/**
* @ignore
*/
function getAcc($accid){
# Get Error account
$accRs = get("core", "accname, topacc, accnum, acctype", "accounts", "accid", $accid);
$acc = pg_fetch_array($accRs);
return $acc;
}
/**
* @ignore
*/
function getAccn($topacc, $accnum){
# Get account
$accRs = undget("core", "*", "accounts", "topacc", "$topacc' AND accnum = '$accnum' AND div = '".USER_DIV);
if(pg_numrows($accRs) < 1){
return false;
}
$acc = pg_fetch_array($accRs);
return $acc;
}
/**
* returns array with PRD_DB and PRD_NAME to use determined by date
*
* @param string $data
* @return array
*/
function getPRD($date = false) {
if ($date === false) $date = date("Y-m-d");
list($year,$month,$day) = explode("-", $date);
$PRD_DB = date("n", mktime(0, 0, 0, $month, $day, $year));
$PRD_NAME = date("F", mktime(0, 0, 0, $month, $day, $year));
return array($PRD_DB, $PRD_NAME);
}
/**
* returns which period schema to use for a date
*
* @param string $date
* @return int
*/
function getPRDDB($date = false) {
if ($date === false) $date = date("Y-m-d");
return extractMonth($date);
}
# Write Trans(debit_account_id, credit_account_id, date, refnum, amount_[11111.00], details)
/**
* writes a transaction into the general ledger
*
* @param int $dtacc debit account id
* @param int $ctacc credit account id
* @param string $date
* @param int $refnum
* @param float $amount
* @param string $details description for ledger entry
*/
function writetrans($dtacc, $ctacc, $date, $refnum, $amount, $details) {
global $MONPRD, $PRDMON;
$amount = sprint($amount);
if($amount < 0.01) {
return;
}
# validate input
require_lib("validate");
$v = new validate ();
$v->isOk ($ctacc, "num", 1, 50, "Invalid Account to be Credited ($ctacc) for transaction \"$details\".");
$v->isOk ($dtacc, "num", 1, 50, "Invalid Account to be Debited ($dtacc) for transaction \"$details\".");
$v->isOk ($date, "date", 1, 14, "Invalid date for transaction \"$details\".");
$v->isOk ($refnum, "string", 1, 50, "Invalid reference number for transaction \"$details\".");
$v->isOk ($amount, "float", 1, 40, "Invalid amount $amount for transaction \"$details\".");
$details = str_replace(":", " ", $details);
$v->isOk ($details, "string", 0, 2048, "Invalid Details [$details].");
# Date format
$date = explode("-", $date);
// $day ,$mon , $year
if(checkdate($date[1], $date[0], $date[2])){
$mon = $date[1];
$yr = $date[2];
$date = "$date[2]-$date[1]-$date[0]";
// $year, $mon, $day
}elseif(checkdate($date[1], $date[2], $date[0])){
$mon = $date[1];
$yr = $date[0];
$date = "$date[0]-$date[1]-$date[2]";
}else{
$v->addError("", "Invalid date for transaction. ".(DEBUG>0?"\"$details\"":""));
}
if (isset($yr)) {
$curyr = getActiveFinYear();
if ($yr > $curyr || ($yr == $curyr && $mon > $PRDMON[12])) {
$v->addError("", "Cannot do transaction in future financial year. ".(DEBUG>0?"\"$details\"":""));
}
}
if(floatval($amount) == floatval(0)){
return;
}
if ($v->isError()) {
$OUTPUT = $v->genErrors();
$OUTPUT .= "<p><input type='button' onclick='javascript:history.back();' value='« Correct submission'></p>";
pglib_transaction("ROLLBACK");
require("template.php");
}
if(!accExist($dtacc)){
$dtacc = getErrAcc();
$details = $details." - Debit account was not found, transaction has been posted to error account.";
db_connect();
$Sql = "INSERT INTO req (sender, recipient, message, timesent, viewed) VALUES ('SYSTEM', '', 'Debit account was not found for transaction with Ref No. $refnum, transaction has been posted to error account.', CURRENT_TIMESTAMP, 0)";
$Rslt = db_exec ($Sql) or errDie ("Unable to add to database.", SELF);
}
if(!accExist($ctacc)){
$ctacc = getErrAcc();
$details = $details." - Credit account was not found, transaction has been posted to error account.";
db_connect();
$Sql = "INSERT INTO req (sender, recipient, message, timesent, viewed) VALUES ('SYSTEM', '', 'Credit account was not found for transaction with Ref No. $refnum, transaction has been posted to error account.', CURRENT_TIMESTAMP, 0)";
$Rslt = db_exec ($Sql) or errDie ("Unable to add to database.", SELF);
}
# Get account information
$dacc = getAcc($dtacc);
$cacc = getAcc($ctacc);
$sdate=date("Y-m-d");
list($PRD_DB, $PRD_NAME) = getPRD($date);
list($CUR_PRD_DB, $CUR_PRD_NAME) = getPRD(date("Y-m-d"));
# Insert the records into the transect table
//if (PRD_STATE != "py") {
db_conn($PRD_DB);
$sql = "
INSERT INTO transect (
debit, daccname, dtopacc, daccnum , credit,
caccname, ctopacc, caccnum, date, refnum,
amount, author, details, div, sdate, custom_refnum
) VALUES (
'$dtacc', '$dacc[accname]', '$dacc[topacc]', '$dacc[accnum]', '$ctacc',
'$cacc[accname]', '$cacc[topacc]', '$cacc[accnum]', '$date', '".getrefnum()."',
'$amount', '".USER_NAME."', '$details', '".USER_DIV."', '$sdate', '$refnum'
)";
$transRslt = db_exec($sql) or errDie("Unable to insert Transaction details to database",SELF);
//}
# Update the balances by adding appropriate values to the trial_bal Table
core_connect();
# Begin sql transaction
# pglib_transaction ("BEGIN") or errDie("Unable to start a database transaction.",SELF);
if (PRD_STATE != "py") {
$ctbal = "UPDATE trial_bal SET credit = (credit + '$amount')
WHERE accid = '$ctacc' AND period>='$MONPRD[$PRD_DB]' AND div = '".USER_DIV."'";
$dtbal = "UPDATE trial_bal SET debit = (debit + '$amount')
WHERE accid = '$dtacc' AND period>='$MONPRD[$PRD_DB]' AND div = '".USER_DIV."'";
$ctbalRslt = db_exec($ctbal) or errDie("Unable to update credit balance for credited account.",SELF);
$dtbalRslt = db_exec($dtbal) or errDie("Unable to update debit balance for debited account.",SELF);
} else {
$ctbal = "UPDATE trial_bal SET credit = (credit + '$amount')
WHERE accid = '$ctacc' AND period<'1' AND div = '".USER_DIV."'";
//FP MOVEMENT FIX
// AND (acctype='I' OR acctype='E')";
$dtbal = "UPDATE trial_bal SET debit = (debit + '$amount')
WHERE accid = '$dtacc' AND period<'1' AND div = '".USER_DIV."'";
//FP MOVEMENT FIX
// AND (acctype='I' OR acctype='E')";
$ctbalRslt = db_exec($ctbal) or errDie("Unable to update credit balance for credited account.",SELF);
$dtbalRslt = db_exec($dtbal) or errDie("Unable to update debit balance for debited account.",SELF);
}
if (PRD_STATE == "py") {
db_conn(YR_DB);
$ctbal = "UPDATE year_balance SET credit=(credit+'$amount') WHERE accid='$ctacc'";
$dtbal = "UPDATE year_balance SET debit=(debit+'$amount') WHERE accid='$dtacc'";
//db_exec($ctbal) or errDie("Error updating previous year balance (CT).");
//db_exec($dtbal) or errDie("Error updating previous year balance (DT).");
}
# commit sql transaction
# pglib_transaction ("COMMIT") or errDie("Unable to finish a database transaction.",SELF);
// insert into audit db
if (PRD_STATE == "py") {
$audit_db = YR_NAME . "_audit";
// $start_prd = 1;
//FP WHY UPDATE WHOLE OF PREVIOUS YEAR ????
$start_prd = $MONPRD[$PRD_DB];
$actyear = PYR_NAME;
} else {
$audit_db = "audit";
$start_prd = $MONPRD[$PRD_DB];
$actyear = YR_NAME;
}
db_conn($audit_db);
$sql = "
INSERT INTO ".$PRD_NAME." (
debit, credit, date, refnum, amount,
author, details, div, actyear, custom_refnum
) VALUES (
'$dtacc', '$ctacc', '$date', '".getrefnum()."', '$amount',
'".USER_NAME."', '$details', '".USER_DIV."', '$actyear', '$refnum'
)";
$transRslt = db_exec($sql) or errDie("Unable to insert Transaction details to database",SELF);
if (true || $MONPRD[$PRD_DB] < $MONPRD[$CUR_PRD_DB]) {
for ($iPRD = $start_prd; $iPRD <= 12; ++$iPRD) {
//print "wt: $iPRD - $PRDMON[$iPRD] - $PRD_DB<Br>";
$iPRD_NAME = date("F", mktime(0, 0, 0, $PRDMON[$iPRD], 1, 2000));
db_conn(YR_DB);
$ctbal = "UPDATE ".$iPRD_NAME." SET credit = (credit + '$amount') WHERE accid = '$ctacc' AND div = '".USER_DIV."'";
$dtbal = "UPDATE ".$iPRD_NAME." SET debit = (debit + '$amount') WHERE accid = '$dtacc' AND div = '".USER_DIV."'";
$ctbalRslt = db_exec($ctbal) or errDie("Unable to update credit balance for credited account.",SELF);
$dtbalRslt = db_exec($dtbal) or errDie("Unable to update debit balance for debited account.",SELF);
}
# Update opening balances for current period
if (PRD_STATE != "py") {
db_conn($CUR_PRD_DB);
$ctbal = "UPDATE openbal SET credit = (credit + '$amount') WHERE accid = '$ctacc' AND div = '".USER_DIV."'";
$dtbal = "UPDATE openbal SET debit = (debit + '$amount') WHERE accid = '$dtacc' AND div = '".USER_DIV."'";
$ctbalRslt = db_exec($ctbal) or errDie("Unable to update credit balance for credited account.",SELF);
$dtbalRslt = db_exec($dtbal) or errDie("Unable to update debit balance for debited account.",SELF);
}
}
//errDie("done");
if (PRD_STATE != "py") {
# Record vat transactions
core_connect();
$sql = "SELECT accnum FROM salesacc WHERE name = 'VATIN' AND div = '".USER_DIV."'";
$vatRslt = db_exec($sql);
if(pg_numrows($vatRslt) > 0){
$vat = pg_fetch_array($vatRslt);
$vatacc = $vat['accnum'];
if($vatacc == $ctacc){
db_connect();
$sql = "INSERT INTO vatrec (edate, ref, amount, descript, div,chrgvat) VALUES ('$date', '$refnum', '$amount', 'VAT : $details', '".USER_DIV."', 'VATIN')";
$stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.",SELF);
}elseif($vatacc == $dtacc){
db_connect();
$sql = "INSERT INTO vatrec(edate, ref, amount, descript, div,chrgvat) VALUES('$date', '$refnum', '-$amount', 'VAT : $details', '".USER_DIV."','VATIN')";
$stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.",SELF);
}
}
core_connect();
$sql = "SELECT accnum FROM salesacc WHERE name = 'VATOUT' AND div = '".USER_DIV."'";
$vatRslt = db_exec($sql);
if(pg_numrows($vatRslt) > 0){
$vat = pg_fetch_array($vatRslt);
$vatacc = $vat['accnum'];
if($vatacc == $ctacc){
db_connect();
$sql = "INSERT INTO vatrec(edate, ref, amount, descript, div,chrgvat) VALUES('$date', '$refnum', '$amount', 'VAT : $details', '".USER_DIV."','VATOUT')";
$stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.",SELF);
}elseif($vatacc == $dtacc){
db_connect();
$sql = "INSERT INTO vatrec(edate, ref, amount, descript, div,chrgvat) VALUES('$date', '$refnum', '-$amount', 'VAT : $details', '".USER_DIV."','VATOUT')";
$stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.",SELF);
}
}
}
if (PRD_STATE == "py") {
ledgerCT_py($ctacc, $dtacc, $date, $refnum, $details, $amount);
ledgerDT_py($dtacc, $ctacc, $date, $refnum, $details, $amount);
writetrans_update_py($dtacc, $ctacc, $amount,$PRD_DB);
}
ledgerCT($ctacc, $dtacc, $date, $refnum, $details, $amount);
ledgerDT($dtacc, $ctacc, $date, $refnum, $details, $amount);
$vatacc1 = gethook("accnum", "salesacc", "name", "VAT","VAT");
}
# Record Trans(debit_account_id, credit_account_id, date, refnum, amount_[11111.00], details)
/**
* writes a transaction into the general ledger
*
* @param string $ttype transaction type to record
* @param int $dtacc debit account id
* @param int $ctacc credit account id
* @param string $date
* @param int $refnum
* @param float $amount
* @param float $vat vat amount
* @param string $details description for ledger entry
*/
function recordtrans($ttype, $dtacc, $ctacc, $date, $refnum, $amount,$vat, $details,$iid='0') {
global $MONPRD, $PRDMON;
$amount=sprint($amount);
// if($amount<0.01) {
// return;
// }
# validate input
require_lib("validate");
$v = new validate ();
$v->isOk ($ctacc, "num", 1, 50, "Invalid Account to be Credited ($ctacc) for transaction \"$details\".");
$v->isOk ($dtacc, "num", 1, 50, "Invalid Account to be Debited ($dtacc) for transaction \"$details\".");
$v->isOk ($date, "date", 1, 14, "Invalid date for transaction \"$details\".");
$v->isOk ($refnum, "num", 1, 50, "Invalid reference number for transaction \"$details\".");
$v->isOk ($amount, "float", 1, 40, "Invalid amount $amount for transaction \"$details\".");
$details = str_replace(":", " ", $details);
$detailscheck = str_replace ("|","",$details);
$v->isOk ($detailscheck, "string", 0, 2048, "Invalid Details [$details].");
# Date format
$date = explode("-", $date);
// $day ,$mon , $year
if(checkdate($date[1], $date[0], $date[2])){
$mon = $date[1];
$yr = $date[2];
$date = "$date[2]-$date[1]-$date[0]";
// $year, $mon, $day
}elseif(checkdate($date[1], $date[2], $date[0])){
$mon = $date[1];
$yr = $date[0];
$date = "$date[0]-$date[1]-$date[2]";
}else{
$v->addError("", "Invalid date for transaction. ".(DEBUG>0?"\"$details\"":""));
}
if (isset($yr)) {
$curyr = getActiveFinYear();
if ($yr > $curyr || ($yr == $curyr && $mon > $PRDMON[12])) {
$v->addError("", "Cannot do transaction in future financial year. ".(DEBUG>0?"\"$details\"":""));
}
}
if(floatval($amount) == floatval(0)){
return;
}
if ($v->isError()) {
$OUTPUT = $v->genErrors();
$OUTPUT .= "<p><input type='button' onclick='javascript:history.back();' value='« Correct submission'></p>";
pglib_transaction("ROLLBACK");
require("template.php");
}
if(!accExist($dtacc) AND ($dtacc != "0") AND ($dtacc != "1") AND ($dtacc != "9999")){
$dtacc = getErrAcc();
$details = $details." - Debit account was not found, transaction has been posted to error account.";
}
if(!accExist($ctacc) AND ($ctacc != "0") AND ($ctacc != "1") AND ($ctacc != "9999")){
$ctacc = getErrAcc();
$details = $details." - Credit account was not found, transaction has been posted to error account.";
}
# Get account information
$dacc = getAcc($dtacc);
$cacc = getAcc($ctacc);
$sdate=date("Y-m-d");
list($PRD_DB, $PRD_NAME) = getPRD($date);