-
Notifications
You must be signed in to change notification settings - Fork 3
/
calc-int.php
executable file
·518 lines (413 loc) · 14.2 KB
/
calc-int.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
<?
#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 ("libs/ext.lib.php");
require ("core-settings.php");
if (isset($_POST['key'])) {
switch ($_POST["key"]) {
case "calc":
$OUTPUT = calculate ();
break;
default:
$OUTPUT = confirm ();
}
} else {
$OUTPUT = confirm ();
}
# get template
require("template.php");
# confirm
function confirm()
{
db_conn('cubit');
$Sl = "SELECT * FROM monthcloses WHERE type='Interest' ORDER BY id DESC LIMIT 1";
$Rx = db_exec($Sl) or errDie("Unable to get monthclose from db.");
if(pg_numrows($Rx) < 1) {
$Note = "This is the first time you are calculating interest";
} else {
$data = pg_fetch_array($Rx);
$Note = "<li class='err'>The last interest calculation was on $data[closedate] by $data[closeby].</li>";
}
// Layout
$confirm = "
<h3>Confirm Interest Calculation</h3>
$Note
<table ".TMPL_tblDflts.">
<form action='".SELF."' method='POST'>
<input type='hidden' name='key' value='calc'>
<tr>
<th colspan='2'>Date</th>
</tr>
<tr class='".bg_class()."'>
<td colspan='2' align='center'>".date("d F Y")."</td>
</tr>
<tr><td><br></td></tr>
<tr>
<td align='right'><input type='button' value='« Back' onClick='javascript:history.back()'></td>
<td align='left'><input type='submit' value='Calculate »'></td>
</tr>
</form>
</table>
<p>
<table ".TMPL_tblDflts." width='100'>
<tr>
<th>Quick Links</th>
</tr>
<script>document.write(getQuicklinkSpecial());</script>
</table>";
return $confirm;
}
# write
function calculate ()
{
db_connect();
# Get Interest type
$sql = "SELECT value FROM set WHERE label = 'INT_TYPE'";
$intRs = db_exec($sql);
if(pg_numrows($intRs) < 1){
return "<li class='err'> Interest Calculation method has not been set. Please set <a href='set-int-type.php'>this option</a> first.";
}
$int = pg_fetch_array($intRs);
$ints = calcPerc($int['value']);
// var_dump($ints);
db_conn('cubit');
$date = date("Y-m-d");
$Sl = "INSERT INTO monthcloses (type, closedate, closeby) VALUES ('Interest', '$date', '".USER_NAME."')";
$Rx = db_exec($Sl) or errDie("Monthend was done, but error makeing record of it.");
$write = "
<table ".TMPL_tblDflts.">
<tr>
<th>Interest has been calculated</th>
</tr>
<tr class='".bg_class()."'>
<td>Date : ".date("d F Y")."</td>
</tr>
</table>
<p>
<table ".TMPL_tblDflts." width='100'>
<tr>
<th>Quick Links</th>
</tr>
<script>document.write(getQuicklinkSpecial());</script>
</table>";
return $write;
}
function calcPerc($type)
{
core_connect();
$intacc = gethook("accnum", "salesacc", "name", "SalesInt");
db_connect();
# Get all client that must be charged interest
$sql = "SELECT surname, cusnum, balance, fbalance, deptid, intrate, location, fcid FROM customers WHERE chrgint = 'yes' AND (balance > 0 OR fbalance > 0) AND div = '".USER_DIV."'";
$rs = db_exec($sql) or errDie("Unable to get clients from Cubit.");
if(pg_numrows($rs) < 1){
# Return if they are not charging any interest
return;
}
#go through matching customers ...
while($clnt = pg_fetch_array($rs)){
if($clnt['location'] != 'int'){
#local customer ...
#figure out the percentage ... based on setting ...
if($type == 'brac'){
# Get from bracket
$sql = "SELECT percentage as perc FROM intbracs WHERE min <= '$clnt[balance]' AND max >= '$clnt[balance]'";
$bRs = db_exec($sql);
if(pg_numrows($bRs) > 0){
$brac = pg_fetch_array($bRs);
$perc = $brac['perc'];
}else{
$perc = 0;
}
}elseif($type[0] == 'r'){
$perc = $clnt['intrate'];
}else{
$perc = $type;
}
//calculate the amount to charge interest on ...
$overdue = getOverdue($clnt['cusnum']);
# Get Perc of overdue
$pamt = sprint((($perc/12)/100) * $overdue);
$totamt = ($pamt + $clnt['balance']);
$ret[$clnt['cusnum']] = $totamt;
# Get department
db_conn("exten");
$sql = "SELECT * FROM departments WHERE deptid = '$clnt[deptid]' AND div = '".USER_DIV."'";
$deptRslt = db_exec($sql);
if(pg_numrows($deptRslt) < 1){
return "<i class='err'>Department Not Found</i>";
}else{
$dept = pg_fetch_array($deptRslt);
}
if($pamt > 0) {
$sdate = date("Y-m-d");
$invnum = intInvoice($clnt['cusnum'], $pamt,$intacc);
db_connect();
# Record the payment on the statement
$sql = "
INSERT INTO stmnt (
cusnum, invid, amount, date,
type, div, allocation_date
) VALUES (
'$clnt[cusnum]', '$invnum', '$pamt', '$sdate',
'Interest on Outstanding balance', '".USER_DIV."', '$sdate'
)";
$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, div
) VALUES (
'$clnt[cusnum]', '$invnum', '$pamt', '$pamt','$sdate',
'Interest on Outstanding balance', '".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 + '$pamt'::numeric(13,2)) WHERE cusnum = '$clnt[cusnum]' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.",SELF);
# Make ledge record
custledger($clnt['cusnum'], $intacc, $sdate, 0, "Interest on Outstanding balance", $pamt, "d");
# Write transaction (debit debtors control, credit contra account)
$refnum = getrefnum();
writetrans($dept['debtacc'], $intacc, date("d-m-Y"), $refnum, $pamt, "Interest Received from customer : $clnt[surname].");
recordDT($pamt, $clnt['cusnum']);
}
}else{
if($type == 'brac'){
# Get from bracket
$sql = "SELECT percentage as perc FROM intbracs WHERE min <= '$clnt[balance]' AND max >= '$clnt[balance]'";
$bRs = db_exec($sql);
if(pg_numrows($bRs) > 0){
$brac = pg_fetch_array($bRs);
$perc = $brac['perc'];
}else{
$perc = 0;
}
}elseif($type[0] == 'r'){
$perc = $clnt['intrate'];
}else{
$perc = $type;
}
// $overdue = getfOverdue($clnt['cusnum']);
$overdue = getOverdue($clnt['cusnum']);
$rate = sprint($clnt['balance'] / $clnt['fbalance']);
# Get Perc of overdue
$pamt = sprint((($perc/12)/100) * $overdue);
$totamt = ($pamt + $clnt['fbalance']);
$ret[$clnt['cusnum']] = $totamt;
if($rate == 0) $rate = 1;
$ltotamt = sprint($totamt * $rate);
$lpamt = sprint($ltotamt - $clnt['balance']);
# Get department
db_conn("exten");
$sql = "SELECT * FROM departments WHERE deptid = '$clnt[deptid]' AND div = '".USER_DIV."'";
$deptRslt = db_exec($sql);
if(pg_numrows($deptRslt) < 1){
return "<i class='err'>Department Not Found</i>";
}else{
$dept = pg_fetch_array($deptRslt);
}
if($pamt > 0) {
$sdate = date("Y-m-d");
$invnum = fintInvoice($clnt['cusnum'], $pamt, $rate);
db_connect();
# Record the payment on the statement
$sql = "
INSERT INTO stmnt (
cusnum, invid, amount, date,
type, div, allocation_date
) VALUES (
'$clnt[cusnum]', '$invnum', '$pamt','$sdate',
'Interest on Outstanding balance', '".USER_DIV."', '$sdate'
)";
$stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.",SELF);
# Update the customer (make balance more)
$sql = "UPDATE customers SET fbalance = (fbalance + '$pamt'::numeric(13,2)), balance = '$ltotamt'::numeric(13,2) WHERE cusnum = '$clnt[cusnum]' AND div = '".USER_DIV."'";
$rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.",SELF);
# Make ledge record
custledger($clnt['cusnum'], $intacc, $sdate, 0, "Interest on Outstanding balance", $lpamt, "d");
# Write transaction (debit debtors control, credit contra account)
$refnum = getrefnum();
writetrans($dept['debtacc'], $intacc, date("d-m-Y"), $refnum, $lpamt, "Interest Received from customer : $clnt[surname].");
frecordDT($pamt, $lpamt, $clnt['cusnum'], $clnt['fcid']);
}
}
}
return $ret;
}
# Get clients overdue amount
function getOverdue($cusnum)
{
if (!isset($cusnum) OR strlen($cusnum) < 1){
return 0;
}
db_connect();
#get customer info ...
$get_cust = "SELECT * FROM customers WHERE cusnum = '$cusnum' LIMIT 1";
$run_cust = db_exec($get_cust) or errDie ("Unable to get customer information.");
if (pg_numrows($run_cust) > 0){
$cust_data = pg_fetch_array ($run_cust);
}else {
return 0;
}
$to_month = date ("m");
$to_date = date ("Y-m-d");
$from_date = date ("Y-m-d",mktime (0,0,0,date("m"),"01",date("Y")));
$age30 = cust_age($cust_data['cusnum'], 59, $cust_data['fcid'], $cust_data['location'], $to_month, $to_date, $from_date);
$age60 = cust_age($cust_data['cusnum'], 89, $cust_data['fcid'], $cust_data['location'], $to_month, $to_date, $from_date);
$age90 = cust_age($cust_data['cusnum'], 119, $cust_data['fcid'], $cust_data['location'], $to_month, $to_date, $from_date);
$age120 = cust_age($cust_data['cusnum'], 149, $cust_data['fcid'], $cust_data['location'], $to_month, $to_date, $from_date);
return $age120 + $age30 + $age60 + $age90;
// # Get the client's overdue period
// $sql = "SELECT cusnum,overdue,location FROM customers WHERE cusnum = '$cusnum' AND div = '".USER_DIV."'";
// $clntRs = db_exec($sql) or errDie("Unable to get clients from Cubit.");
// $cust = pg_fetch_array($clntRs);
//
// # Check type of age analisys
// if(div_isset("DEBT_AGE", "mon")){
// $overd = oageage($cust['cusnum'], ($cust['overdue']/30) - 1, $cust['location']);
// }else{
// $overd = oage($cust['cusnum'], ($cust['overdue'])- 1, $cust['location']);
// }
// return $overd;
}
# Get clients overdue amount
function getfOverdue($cusnum)
{
db_connect();
# Get the client's overdue period
$sql = "SELECT cusnum,overdue,fcid,location FROM customers WHERE cusnum = '$cusnum' AND div = '".USER_DIV."'";
$clntRs = db_exec($sql) or errDie("Unable to get clients from Cubit.");
$cust = pg_fetch_array($clntRs);
# Check type of age analisys
if(div_isset("DEBT_AGE", "mon")){
$overd = oageage($cust['cusnum'], ($cust['overdue']/30) - 1, $cust['location']);
}else{
$overd = oage($cust['cusnum'], ($cust['overdue'])- 1, $cust['location']);
}
return $overd;
}
# Records for DT
function recordDT($amount, $cusnum)
{
db_connect();
# Check for previous transactions
$sql = "SELECT * FROM custran WHERE cusnum = '$cusnum' AND age = 0 AND balance < 0 AND div = '".USER_DIV."' ORDER BY odate ASC";
$rs = db_exec($sql) or errDie("Unable to get analysis records from Cubit.",SELF);
if(pg_numrows($rs) > 0){
while($dat = pg_fetch_array($rs)){
if(floatval($amount) > 0){
if($dat['balance'] <= $amount){
# Remove make amount less
$sql = "UPDATE custran SET balance = (balance + '$amount'::numeric(13,2)) WHERE id = '$dat[id]' AND div = '".USER_DIV."'";
$dRs = db_exec($sql) or errDie("Unable to update analysis records from Cubit.",SELF);
$amount = 0;
}else{
# remove small ones
if($dat['balance'] < $amount){
$amount -= $dat['balance'];
$sql = "DELETE FROM custran WHERE id = '$dat[id]' AND div = '".USER_DIV."'";
$dRs = db_exec($sql) or errDie("Unable to update analysis records from Cubit.",SELF);
}
}
}
}
if($amount > 0){
/* Make transaction record for age analysis */
$odate = date("Y-m-d");
$sql = "
INSERT INTO custran (
cusnum, odate, balance, div
) VALUES (
'$cusnum', '$odate', '$amount', '".USER_DIV."'
)";
$purcRslt = db_exec($sql) or errDie("Unable to update int purchases information in Cubit.",SELF);
}
}else{
/* Make transaction record for age analysis */
$odate = date("Y-m-d");
$sql = "
INSERT INTO custran (
cusnum, odate, balance, div
) VALUES (
'$cusnum', '$odate', '$amount', '".USER_DIV."'
)";
$purcRslt = db_exec($sql) or errDie("Unable to update int purchases information in Cubit.",SELF);
}
# Remove all empty entries
$sql = "DELETE FROM custran WHERE fbalance = 0::numeric(13,2) AND balance = 0::numeric(13,2) AND div = '".USER_DIV."'";
$rs = db_exec($sql);
}
# records for CT
function frecordDT($amount, $lamount, $cusnum, $fcid)
{
db_connect();
/* Make transaction record for age analysis */
$odate = date("Y-m-d");
$sql = "
INSERT INTO custran (
cusnum, odate, fcid, balance, fbalance, div
) VALUES (
'$cusnum', '$odate', '$fcid', '$lamount', '$amount', '".USER_DIV."'
)";
$purcRslt = db_exec($sql) or errDie("Unable to update int purchases information in Cubit.",SELF);
# Remove all empty entries
$sql = "DELETE FROM custran WHERE fbalance = 0::numeric(13,2) AND balance = 0::numeric(13,2) AND div = '".USER_DIV."'";
$rs = db_exec($sql);
}
function oage($cusnum, $days, $loc)
{
$bal = "balance";
if($loc == 'int')
$bal = "fbalance";
# Get the current oustanding
$sql = "SELECT sum($bal) FROM invoices WHERE cusnum = '$cusnum' AND printed = 'y' AND odate < '".extlib_ago($days)."' AND div = '".USER_DIV."'";
$rs = db_exec($sql) or errDie("Unable to access database");
$sum = pg_fetch_array($rs);
# Get the current oustanding on transactions
$sql = "SELECT sum($bal) FROM custran WHERE cusnum = '$cusnum' AND odate < '".extlib_ago($days)."' AND div = '".USER_DIV."'";
$rs = db_exec($sql) or errDie("Unable to access database");
$sumb = pg_fetch_array($rs);
# Take care of nasty zero
return sprint($sum['sum'] + $sumb ['sum'] );
}
function oageage($cusnum, $age, $loc)
{
$bal = "balance";
if($loc == 'int')
$bal = "fbalance";
# Get the current oustanding
$sql = "SELECT sum($bal) FROM invoices WHERE cusnum = '$cusnum' AND printed = 'y' AND age > '$age' AND div = '".USER_DIV."'";
$rs = db_exec($sql) or errDie("Unable to access database");
$sum = pg_fetch_array($rs);
# Get the current oustanding on transactions
$sql = "SELECT sum($bal) FROM custran WHERE cusnum = '$cusnum' AND age > '$age' AND div = '".USER_DIV."'";
$rs = db_exec($sql) or errDie("Unable to access database");
$sumb = pg_fetch_array($rs);
# Take care of nasty zero
return sprint($sum['sum'] + $sumb ['sum']);
}
?>