-
Notifications
You must be signed in to change notification settings - Fork 3
/
AgedDebtors.php
516 lines (471 loc) · 25 KB
/
AgedDebtors.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
<?php
/* $Id$ */
include('includes/session.inc');
if (isset($_POST['PrintPDF'])
and isset($_POST['FromCriteria'])
and strlen($_POST['FromCriteria'])>=1
and isset($_POST['ToCriteria'])
and strlen($_POST['ToCriteria'])>=1) {
include('includes/PDFStarter.php');
$pdf->addInfo('Title',_('Aged Customer Balance Listing'));
$pdf->addInfo('Subject',_('Aged Customer Balances'));
$FontSize = 12;
$PageNumber = 0;
$line_height = 12;
/*Now figure out the aged analysis for the customer range under review */
if (trim($_POST['Salesman'])!=''){
$SalesLimit = " AND debtorsmaster.debtorno IN (SELECT DISTINCT debtorno FROM custbranch WHERE salesman = '".$_POST['Salesman']."') ";
} else {
$SalesLimit = "";
}
if ($_POST['All_Or_Overdues']=='All'){
$SQL = "SELECT debtorsmaster.debtorno,
debtorsmaster.name,
currencies.currency,
debtorsmaster.currcode,
paymentterms.terms,
debtorsmaster.creditlimit,
holdreasons.dissallowinvoices,
holdreasons.reasondescription,
SUM(
debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
) AS balance,
SUM(
CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
CASE WHEN (TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate)) >= paymentterms.daysbeforedue
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
ELSE
CASE WHEN TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') .")) >= 0
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc ELSE 0 END
END
) AS due,
SUM(
CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
CASE WHEN (TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate)) > paymentterms.daysbeforedue AND TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= (paymentterms.daysbeforedue + " . $_SESSION['PastDueDays1'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc ELSE 0 END
ELSE
CASE WHEN TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL ('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= " . $_SESSION['PastDueDays1'] . "
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
END
) AS overdue1,
SUM(
CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
CASE WHEN (TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate)) > paymentterms.daysbeforedue AND TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= (paymentterms.daysbeforedue + " . $_SESSION['PastDueDays2'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc ELSE 0 END
ELSE
CASE WHEN TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL ('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= " . $_SESSION['PastDueDays2'] . "
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
END
) AS overdue2
FROM debtorsmaster,
paymentterms,
holdreasons,
currencies,
debtortrans
WHERE debtorsmaster.paymentterms = paymentterms.termsindicator
AND debtorsmaster.currcode = currencies.currabrev
AND debtorsmaster.holdreason = holdreasons.reasoncode
AND debtorsmaster.debtorno = debtortrans.debtorno
AND debtorsmaster.debtorno >= '" . $_POST['FromCriteria'] . "'
AND debtorsmaster.debtorno <= '" . $_POST['ToCriteria'] . "'
AND debtorsmaster.currcode ='" . $_POST['Currency'] . "'
" . $SalesLimit . "
GROUP BY CONVERT(debtorsmaster.debtorno , SIGNED),
debtorsmaster.name,
currencies.currency,
paymentterms.terms,
paymentterms.daysbeforedue,
paymentterms.dayinfollowingmonth,
debtorsmaster.creditlimit,
holdreasons.dissallowinvoices,
holdreasons.reasondescription
HAVING
ROUND(ABS(SUM(debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc)),currencies.decimalplaces) >0.005";
} elseif ($_POST['All_Or_Overdues']=='OverduesOnly') {
$SQL = "SELECT debtorsmaster.debtorno,
debtorsmaster.name,
currencies.currency,
debtorsmaster.currcode,
paymentterms.terms,
debtorsmaster.creditlimit,
holdreasons.dissallowinvoices,
holdreasons.reasondescription,
SUM(
debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
) AS balance,
SUM(
CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
CASE WHEN TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= paymentterms.daysbeforedue
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
ELSE
CASE WHEN (TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= 0 )
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc ELSE 0 END
END
) AS due,
SUM(
CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
CASE WHEN TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) > paymentterms.daysbeforedue AND TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= (paymentterms.daysbeforedue + " . $_SESSION['PastDueDays1'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
ELSE
CASE WHEN (TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= " . $_SESSION['PastDueDays1'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
END
) AS overdue1,
SUM(
CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
CASE WHEN TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) > paymentterms.daysbeforedue AND TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= (paymentterms.daysbeforedue + " . $_SESSION['PastDueDays2'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
ELSE
CASE WHEN (TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= " . $_SESSION['PastDueDays2'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
END
) AS overdue2
FROM debtorsmaster,
paymentterms,
holdreasons,
currencies,
debtortrans
WHERE debtorsmaster.paymentterms = paymentterms.termsindicator
AND debtorsmaster.currcode = currencies.currabrev
AND debtorsmaster.holdreason = holdreasons.reasoncode
AND debtorsmaster.debtorno = debtortrans.debtorno
AND debtorsmaster.debtorno >= '" . $_POST['FromCriteria'] . "'
AND debtorsmaster.debtorno <= '" . $_POST['ToCriteria'] . "'
AND debtorsmaster.currcode ='" . $_POST['Currency'] . "'
" . $SalesLimit . "
GROUP BY CONVERT(debtorsmaster.debtorno , SIGNED),
debtorsmaster.name,
currencies.currency,
paymentterms.terms,
paymentterms.daysbeforedue,
paymentterms.dayinfollowingmonth,
debtorsmaster.creditlimit,
holdreasons.dissallowinvoices,
holdreasons.reasondescription
HAVING SUM(
CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
CASE WHEN TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) > paymentterms.daysbeforedue AND TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= (paymentterms.daysbeforedue + " . $_SESSION['PastDueDays1'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
ELSE
CASE WHEN (TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= " . $_SESSION['PastDueDays1'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
END
) > 0.01";
} elseif ($_POST['All_Or_Overdues']=='HeldOnly'){
$SQL = "SELECT debtorsmaster.debtorno,
debtorsmaster.name,
currencies.currency,
debtorsmaster.currcode,
paymentterms.terms,
debtorsmaster.creditlimit,
holdreasons.dissallowinvoices,
holdreasons.reasondescription,
SUM(debtortrans.ovamount +
debtortrans.ovgst +
debtortrans.ovfreight +
debtortrans.ovdiscount -
debtortrans.alloc) AS balance,
SUM(
CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
CASE WHEN TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= paymentterms.daysbeforedue
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
ELSE
CASE WHEN (TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate," . INTERVAL('1', 'MONTH') . ")," . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= 0)
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
END
) AS due,
SUM(
CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
CASE WHEN TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) > paymentterms.daysbeforedue
AND TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= (paymentterms.daysbeforedue + " . $_SESSION['PastDueDays1'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc ELSE 0 END
ELSE
CASE WHEN (TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= " . $_SESSION['PastDueDays1'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
END
) AS overdue1,
SUM(
CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
CASE WHEN TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) > paymentterms.daysbeforedue
AND TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= (paymentterms.daysbeforedue + " . $_SESSION['PastDueDays2'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
ELSE
CASE WHEN (TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= ".$_SESSION['PastDueDays2'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END
END
) AS overdue2
FROM debtorsmaster,
paymentterms,
holdreasons,
currencies,
debtortrans
WHERE debtorsmaster.paymentterms = paymentterms.termsindicator
AND debtorsmaster.currcode = currencies.currabrev
AND debtorsmaster.holdreason = holdreasons.reasoncode
AND debtorsmaster.debtorno = debtortrans.debtorno
AND holdreasons.dissallowinvoices=1
AND debtorsmaster.debtorno >= '" . $_POST['FromCriteria'] . "'
AND debtorsmaster.debtorno <= '" . $_POST['ToCriteria'] . "'
AND debtorsmaster.currcode ='" . $_POST['Currency'] . "'
" . $SalesLimit . "
GROUP BY CONVERT(debtorsmaster.debtorno , SIGNED),
debtorsmaster.name,
currencies.currency,
paymentterms.terms,
paymentterms.daysbeforedue,
paymentterms.dayinfollowingmonth,
debtorsmaster.creditlimit,
holdreasons.dissallowinvoices,
holdreasons.reasondescription
HAVING ABS(SUM(
debtortrans.ovamount +
debtortrans.ovgst +
debtortrans.ovfreight +
debtortrans.ovdiscount -
debtortrans.alloc)) >0.005";
}
$CustomerResult = DB_query($SQL,$db,'','',False,False); /*dont trap errors handled below*/
//$ListCount = count ($CustomerResult); // Javier
if (DB_error_no($db) !=0) {
$title = _('Aged Customer Account Analysis') . ' - ' . _('Problem Report') . '.... ';
include('includes/header.inc');
prnMsg(_('The customer details could not be retrieved by the SQL because') . ' ' . DB_error_msg($db),'error');
echo '<br /><a href="' . $rootpath . '/index.php">' . _('Back to the menu') . '</a>';
if ($debug==1){
echo '<br />' . $SQL;
}
include('includes/footer.inc');
exit;
}
include ('includes/PDFAgedDebtorsPageHeader.inc');
$TotBal=0;
$TotCurr=0;
$TotDue=0;
$TotOD1=0;
$TotOD2=0;
$ListCount = DB_num_rows($CustomerResult);
while ($AgedAnalysis = DB_fetch_array($CustomerResult,$db)){
$DisplayDue = locale_money_format($AgedAnalysis['due']-$AgedAnalysis['overdue1'],$AgedAnalysis['currcode']);
$DisplayCurrent = locale_money_format($AgedAnalysis['balance']-$AgedAnalysis['due'],$AgedAnalysis['currcode']);
$DisplayBalance = locale_money_format($AgedAnalysis['balance'],$AgedAnalysis['currcode']);
$DisplayOverdue1 = locale_money_format($AgedAnalysis['overdue1']-$AgedAnalysis['overdue2'],$AgedAnalysis['currcode']);
$DisplayOverdue2 = locale_money_format($AgedAnalysis['overdue2'],$AgedAnalysis['currcode']);
$TotBal += $AgedAnalysis['balance'];
$TotDue += ($AgedAnalysis['due']-$AgedAnalysis['overdue1']);
$TotCurr += ($AgedAnalysis['balance']-$AgedAnalysis['due']);
$TotOD1 += ($AgedAnalysis['overdue1']-$AgedAnalysis['overdue2']);
$TotOD2 += $AgedAnalysis['overdue2'];
$LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,220-$Left_Margin,$FontSize,$AgedAnalysis['debtorno'] . ' - ' . $AgedAnalysis['name'],'left');
$LeftOvers = $pdf->addTextWrap(220,$YPos,60,$FontSize,$DisplayBalance,'right');
$LeftOvers = $pdf->addTextWrap(280,$YPos,60,$FontSize,$DisplayCurrent,'right');
$LeftOvers = $pdf->addTextWrap(340,$YPos,60,$FontSize,$DisplayDue,'right');
$LeftOvers = $pdf->addTextWrap(400,$YPos,60,$FontSize,$DisplayOverdue1,'right');
$LeftOvers = $pdf->addTextWrap(460,$YPos,60,$FontSize,$DisplayOverdue2,'right');
$YPos -=$line_height;
if ($YPos < $Bottom_Margin + $line_height){
include('includes/PDFAgedDebtorsPageHeader.inc');
}
if ($_POST['DetailedReport']=='Yes'){
/*draw a line under the customer aged analysis*/
$pdf->line($Page_Width-$Right_Margin, $YPos+10,$Left_Margin, $YPos+10);
$sql = "SELECT systypes.typename,
debtortrans.transno,
debtortrans.trandate,
(debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc) as balance,
(CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
(CASE WHEN (TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate)) >= paymentterms.daysbeforedue
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END)
ELSE
(CASE WHEN TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= 0
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END)
END) AS due,
(CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
(CASE WHEN TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) > paymentterms.daysbeforedue AND TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= (paymentterms.daysbeforedue + " . $_SESSION['PastDueDays1'] . ") THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc ELSE 0 END)
ELSE
(CASE WHEN (TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))', 'DAY') . ")) >= " . $_SESSION['PastDueDays1'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END)
END) AS overdue1,
(CASE WHEN (paymentterms.daysbeforedue > 0)
THEN
(CASE WHEN TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) > paymentterms.daysbeforedue AND TO_DAYS(Now()) - TO_DAYS(debtortrans.trandate) >= (paymentterms.daysbeforedue + " . $_SESSION['PastDueDays2'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END)
ELSE
(CASE WHEN (TO_DAYS(Now()) - TO_DAYS(DATE_ADD(DATE_ADD(debtortrans.trandate, " . INTERVAL('1', 'MONTH') . "), " . INTERVAL('(paymentterms.dayinfollowingmonth - DAYOFMONTH(debtortrans.trandate))','DAY') . ")) >= " . $_SESSION['PastDueDays2'] . ")
THEN debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc
ELSE 0 END)
END) AS overdue2
FROM debtorsmaster,
paymentterms,
debtortrans,
systypes
WHERE systypes.typeid = debtortrans.type
AND debtorsmaster.paymentterms = paymentterms.termsindicator
AND debtorsmaster.debtorno = debtortrans.debtorno
AND debtortrans.debtorno = '" . $AgedAnalysis['debtorno'] . "'
AND ABS(debtortrans.ovamount + debtortrans.ovgst + debtortrans.ovfreight + debtortrans.ovdiscount - debtortrans.alloc)>0.004";
$DetailResult = DB_query($sql,$db,'','',False,False); /*Dont trap errors */
if (DB_error_no($db) !=0) {
$title = _('Aged Customer Account Analysis') . ' - ' . _('Problem Report') . '....';
include('includes/header.inc');
prnMsg(_('The details of outstanding transactions for customer') . ' - ' . $AgedAnalysis['debtorno'] . ' ' . _('could not be retrieved because') . ' - ' . DB_error_msg($db),'error');
echo '<br /><a href="' . $rootpath . '/index.php">' . _('Back to the menu') . '</a>';
if ($debug==1){
echo '<br />' . _('The SQL that failed was') . '<br />' . $sql;
}
include('includes/footer.inc');
exit;
}
while ($DetailTrans = DB_fetch_array($DetailResult)){
$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos,60,$FontSize,$DetailTrans['typename'],'left');
$LeftOvers = $pdf->addTextWrap($Left_Margin+65,$YPos,60,$FontSize,$DetailTrans['transno'],'left');
$DisplayTranDate = ConvertSQLDate($DetailTrans['trandate']);
$LeftOvers = $pdf->addTextWrap($Left_Margin+125,$YPos,75,$FontSize,$DisplayTranDate,'left');
$DisplayDue = locale_money_format($DetailTrans['due']-$DetailTrans['overdue1'],$AgedAnalysis['currcode']);
$DisplayCurrent = locale_money_format($DetailTrans['balance']-$DetailTrans['due'],$AgedAnalysis['currcode']);
$DisplayBalance = locale_money_format($DetailTrans['balance'],$AgedAnalysis['currcode']);
$DisplayOverdue1 = locale_money_format($DetailTrans['overdue1']-$DetailTrans['overdue2'],$AgedAnalysis['currcode']);
$DisplayOverdue2 = locale_money_format($DetailTrans['overdue2'],$AgedAnalysis['currcode']);
$LeftOvers = $pdf->addTextWrap(220,$YPos,60,$FontSize,$DisplayBalance,'right');
$LeftOvers = $pdf->addTextWrap(280,$YPos,60,$FontSize,$DisplayCurrent,'right');
$LeftOvers = $pdf->addTextWrap(340,$YPos,60,$FontSize,$DisplayDue,'right');
$LeftOvers = $pdf->addTextWrap(400,$YPos,60,$FontSize,$DisplayOverdue1,'right');
$LeftOvers = $pdf->addTextWrap(460,$YPos,60,$FontSize,$DisplayOverdue2,'right');
$YPos -=$line_height;
if ($YPos < $Bottom_Margin + $line_height){
$PageNumber++;
include('includes/PDFAgedDebtorsPageHeader.inc');
}
} /*end while there are detail transactions to show */
$FontSize=8;
/*draw a line under the detailed transactions before the next customer aged analysis*/
$pdf->line($Page_Width-$Right_Margin, $YPos+10,$Left_Margin, $YPos+10);
} /*Its a detailed report */
} /*end customer aged analysis while loop */
$YPos -=$line_height;
if ($YPos < $Bottom_Margin + (2*$line_height)){
$PageNumber++;
include('includes/PDFAgedDebtorsPageHeader.inc');
} elseif ($_POST['DetailedReport']=='Yes') {
//dont do a line if the totals have to go on a new page
$pdf->line($Page_Width-$Right_Margin, $YPos+10 ,220, $YPos+10);
}
$DisplayTotBalance = locale_money_format($TotBal,$_SESSION['CompanyRecord']['currencydefault']);
$DisplayTotDue = locale_money_format($TotDue,$_SESSION['CompanyRecord']['currencydefault']);
$DisplayTotCurrent = locale_money_format($TotCurr,$_SESSION['CompanyRecord']['currencydefault']);
$DisplayTotOverdue1 = locale_money_format($TotOD1,$_SESSION['CompanyRecord']['currencydefault']);
$DisplayTotOverdue2 = locale_money_format($TotOD2,$_SESSION['CompanyRecord']['currencydefault']);
$LeftOvers = $pdf->addTextWrap(220,$YPos,60,$FontSize,$DisplayTotBalance,'right');
$LeftOvers = $pdf->addTextWrap(280,$YPos,60,$FontSize,$DisplayTotCurrent,'right');
$LeftOvers = $pdf->addTextWrap(340,$YPos,60,$FontSize,$DisplayTotDue,'right');
$LeftOvers = $pdf->addTextWrap(400,$YPos,60,$FontSize,$DisplayTotOverdue1,'right');
$LeftOvers = $pdf->addTextWrap(460,$YPos,60,$FontSize,$DisplayTotOverdue2,'right');
if ($ListCount == 0) {
$title = _('Aged Customer Account Analysis') . ' - ' . _('Problem Report') . '....';
include('includes/header.inc');
prnMsg(_('There are no customers with balances meeting the criteria specified to list'),'info');
echo '<br /><a href="' . $rootpath . '/index.php">' . _('Back to the menu') . '</a>';
include('includes/footer.inc');
exit;
} else {
$pdf->OutputD($_SESSION['DatabaseName'] . '_' . 'AgedDebtors_' . date('Y-m-d') . '.pdf');
$pdf-> __destruct();
}
} else { /*The option to print PDF was not hit */
$title=_('Aged Debtor Analysis');
include('includes/header.inc');
echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/magnifier.png" title="' . _('Search') . '" alt="" />' . ' ' . $title.'</p><br />';
if ((!isset($_POST['FromCriteria']) or !isset($_POST['ToCriteria']))) {
/*if $FromCriteria is not set then show a form to allow input */
echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '" method="post">
<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />
<table class="selection">
<tr>
<td>' . _('From Customer Code') . ':' . '</font></td>
<td><input tabindex="1" type="text" maxlength="6" size="7" name="FromCriteria" value="0" /></td>
</tr>
<tr>
<td>' . _('To Customer Code') . ':' . '</td>
<td><input tabindex="2" type="text" maxlength="6" size="7" name="ToCriteria" value="zzzzzz" /></td>
</tr>
<tr>
<td>' . _('All balances or overdues only') . ':' . '</td>
<td><select tabindex="3" name="All_Or_Overdues">
<option selected="selected" value="All">' . _('All customers with balances') . '</option>
<option value="OverduesOnly">' . _('Overdue accounts only') . '</option>
<option value="HeldOnly">' . _('Held accounts only') . '</option>
</select>
</td>
</tr>
<tr>
<td>' . _('Only Show Customers Of') . ':' . '</td>
<td><select tabindex="4" name="Salesman">';
$sql = "SELECT salesmancode, salesmanname FROM salesman";
$result=DB_query($sql,$db);
echo '<option value="">' . _('All Sales people') . '</option>';
while ($myrow=DB_fetch_array($result)){
echo '<option value="' . $myrow['salesmancode'] . '">' . $myrow['salesmanname'] . '</option>';
}
echo '</select></td>
</tr>
<tr>
<td>' . _('Only show customers trading in') . ':' . '</td>
<td><select tabindex="5" name="Currency">';
$sql = "SELECT currency, currabrev FROM currencies";
$result=DB_query($sql,$db);
while ($myrow=DB_fetch_array($result)){
if ($myrow['currabrev'] == $_SESSION['CompanyRecord']['currencydefault']){
echo '<option selected="selected" value="' . $myrow['currabrev'] . '">' . $myrow['currency'] . '</option>';
} else {
echo '<option value="' . $myrow['currabrev'] . '">' . $myrow['currency'] . '</option>';
}
}
echo '</select></td>
</tr>
<tr>
<td>' . _('Summary or detailed report') . ':' . '</td>
<td><select tabindex="6" name="DetailedReport">
<option selected="selected" value="No">' . _('Summary Report') . '</option>
<option value="Yes">' . _('Detailed Report') . '</option>
</select>
</td>
</tr>
</table>
<br />
<div class="centre">
<button tabindex="7" type="submit" name="PrintPDF">' . _('Print PDF') , '</button>
</div><br />';
}
include('includes/footer.inc');
} /*end of else not PrintPDF */
?>