forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerAccount.php
404 lines (372 loc) · 18.4 KB
/
CustomerAccount.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
<?php
/* Shows customer account/statement on screen rather than PDF. */
include('includes/session.php');
$Title = _('Customer Account');// Screen identification.
$ViewTopic = 'ARInquiries';// Filename in ManualContents.php's TOC.
$BookMark = 'CustomerAccount';// Anchor's id in the manual's html document.
include('includes/header.php');
// always figure out the SQL required from the inputs available
if (!isset($_GET['CustomerID']) and !isset($_SESSION['CustomerID'])) {
prnMsg(_('To display the account a customer must first be selected from the customer selection screen'), 'info');
echo '<br /><div class="centre"><a href="', $RootPath, '/SelectCustomer.php">', _('Select a Customer Account to Display'), '</a></div>';
include('includes/footer.php');
exit;
} else {
if (isset($_GET['CustomerID'])) {
$_SESSION['CustomerID'] = stripslashes($_GET['CustomerID']);
}
$CustomerID = $_SESSION['CustomerID'];
}
//Check if the users have proper authority
if ($_SESSION['SalesmanLogin'] != '') {
$ViewAllowed = false;
$sql = "SELECT salesman FROM custbranch WHERE debtorno = '" . $CustomerID . "'";
$ErrMsg = _('Failed to retrieve sales data');
$result = DB_query($sql,$ErrMsg);
if(DB_num_rows($result)>0) {
while($myrow = DB_fetch_array($result)) {
if ($_SESSION['SalesmanLogin'] == $myrow['salesman']) {
$ViewAllowed = true;
}
}
} else {
prnMsg(_('There is no salesman data set for this customer'),'error');
include('includes/footer.php');
exit;
}
if (!$ViewAllowed) {
prnMsg(_('You have no authority to review this customer account'),'error');
include('includes/footer.php');
exit;
}
}
if (!isset($_POST['TransAfterDate'])) {
$_POST['TransAfterDate'] = Date($_SESSION['DefaultDateFormat'], Mktime(0, 0, 0, Date('m') - $_SESSION['NumberOfMonthMustBeShown'], Date('d'), Date('Y')));
}
$Transactions = array();
/*now get all the settled transactions which were allocated this month */
$ErrMsg = _('There was a problem retrieving the transactions that were settled over the course of the last month for'). ' ' . $CustomerID . ' ' . _('from the database');
if ($_SESSION['Show_Settled_LastMonth']==1) {
$sql = "SELECT DISTINCT debtortrans.id,
debtortrans.type,
systypes.typename,
debtortrans.branchcode,
debtortrans.reference,
debtortrans.invtext,
debtortrans.order_,
debtortrans.transno,
debtortrans.trandate,
debtortrans.ovamount+debtortrans.ovdiscount+debtortrans.ovfreight+debtortrans.ovgst AS totalamount,
debtortrans.alloc,
debtortrans.ovamount+debtortrans.ovdiscount+debtortrans.ovfreight+debtortrans.ovgst-debtortrans.alloc AS balance,
debtortrans.settled
FROM debtortrans INNER JOIN systypes
ON debtortrans.type=systypes.typeid
INNER JOIN custallocns
ON (debtortrans.id=custallocns.transid_allocfrom
OR debtortrans.id=custallocns.transid_allocto)
WHERE custallocns.datealloc >='" . FormatDateForSQL($_POST['TransAfterDate']) . "'
AND debtortrans.debtorno='" . $CustomerID . "'
AND debtortrans.settled=1
ORDER BY debtortrans.id";
$SetldTrans=DB_query($sql, $ErrMsg);
$NumberOfRecordsReturned = DB_num_rows($SetldTrans);
while ($myrow=DB_fetch_array($SetldTrans)) {
$Transactions[] = $myrow;
}
} else {
$NumberOfRecordsReturned=0;
}
/*now get all the outstanding transaction ie Settled=0 */
$ErrMsg = _('There was a problem retrieving the outstanding transactions for') . ' ' . $CustomerID . ' '. _('from the database') . '.';
$sql = "SELECT debtortrans.id,
debtortrans.type,
systypes.typename,
debtortrans.branchcode,
debtortrans.reference,
debtortrans.invtext,
debtortrans.order_,
debtortrans.transno,
debtortrans.trandate,
debtortrans.ovamount+debtortrans.ovdiscount+debtortrans.ovfreight+debtortrans.ovgst as totalamount,
debtortrans.alloc,
debtortrans.ovamount+debtortrans.ovdiscount+debtortrans.ovfreight+debtortrans.ovgst-debtortrans.alloc as balance,
debtortrans.settled
FROM debtortrans INNER JOIN systypes
ON debtortrans.type=systypes.typeid
WHERE debtortrans.debtorno='" . $CustomerID . "'
AND debtortrans.settled=0";
if ($_SESSION['SalesmanLogin'] != '') {
$sql .= " AND debtortrans.salesperson='" . $_SESSION['SalesmanLogin'] . "'";
}
$sql .= " ORDER BY debtortrans.id";
$OstdgTrans=DB_query($sql, $ErrMsg);
while ($myrow=DB_fetch_array($OstdgTrans)) {
$Transactions[] = $myrow;
}
$NumberOfRecordsReturned += DB_num_rows($OstdgTrans);
$SQL = "SELECT debtorsmaster.name,
debtorsmaster.address1,
debtorsmaster.address2,
debtorsmaster.address3,
debtorsmaster.address4,
debtorsmaster.address5,
debtorsmaster.address6,
currencies.currency,
currencies.decimalplaces,
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 INNER JOIN paymentterms
ON debtorsmaster.paymentterms = paymentterms.termsindicator
INNER JOIN currencies
ON debtorsmaster.currcode = currencies.currabrev
INNER JOIN holdreasons
ON debtorsmaster.holdreason = holdreasons.reasoncode
INNER JOIN debtortrans
ON debtorsmaster.debtorno = debtortrans.debtorno
WHERE
debtorsmaster.debtorno = '" . $CustomerID . "'";
if ($_SESSION['SalesmanLogin'] != '') {
$sql .= " AND debtortrans.salesperson='" . $_SESSION['SalesmanLogin'] . "'";
}
$SQL .= " GROUP BY
debtorsmaster.name,
debtorsmaster.address1,
debtorsmaster.address2,
debtorsmaster.address3,
debtorsmaster.address4,
debtorsmaster.address5,
debtorsmaster.address6,
currencies.decimalplaces,
currencies.currency,
paymentterms.terms,
paymentterms.daysbeforedue,
paymentterms.dayinfollowingmonth,
debtorsmaster.creditlimit,
holdreasons.dissallowinvoices,
holdreasons.reasondescription";
$ErrMsg = _('The customer details could not be retrieved by the SQL because');
$CustomerResult = DB_query($SQL, $ErrMsg);
$CustomerRecord = DB_fetch_array($CustomerResult);
echo '<div class="noprint centre">
<a href="', $RootPath, '/SelectCustomer.php">', _('Back to Customer Screen'), '</a>
</div>';
echo '<table width="100%">
<tr><th colspan="2">', _('Customer Statement For'), ': ', stripslashes($CustomerID), ' - ', $CustomerRecord['name'], '</th></tr>
<tr><td colspan="2">', $CustomerRecord['address1'], '</td></tr>';
if($CustomerRecord['address2']!='') {// If not empty, output this line.
echo '<tr><td colspan="2">', $CustomerRecord['address2'], '</td></tr>';
}
if($CustomerRecord['address3']!='') {// If not empty, output this line.
echo '<tr><td colspan="2">', $CustomerRecord['address3'], '</td></tr>';
}
echo ' <tr><td colspan="2">', $CustomerRecord['address4'], '</td></tr>
<tr><td colspan="2">', $CustomerRecord['address5'], ' ', $CustomerRecord['address6'], '</td></tr>
<tr><th>', _('All amounts stated in'), ':</th><td>', $CustomerRecord['currency'], '</td></tr>
<tr><th>', _('Terms'), ':</th><td>', $CustomerRecord['terms'], '</th></tr>
<tr><th>', _('Credit Limit'), ':</th><td>', locale_number_format($CustomerRecord['creditlimit'], 0), '</td></tr>
<tr><th>', _('Credit Status'), ':</th><td>', $CustomerRecord['reasondescription'], '</td></tr>
</table>';
if ($CustomerRecord['dissallowinvoices'] != 0) {
echo '<br /><b><font color="red" size="4">', _('ACCOUNT ON HOLD'), '</font></b><br />';
}
echo '<br /><form onSubmit="return VerifyForm(this);" action="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '" method="post" class="centre noprint">
<input name="FormID" type="hidden" value="', $_SESSION['FormID'], '" />',
_('Show all transactions after'), ':<input class="date" maxlength="10" name="TransAfterDate" required="required" size="11" tabindex="1" type="text" value="', $_POST['TransAfterDate'], '" />',
'<input name="Refresh Inquiry" tabindex="3" type="submit" value="', _('Refresh Inquiry'), '" />
</form>';
/* Show a table of the invoices returned by the SQL. */
echo '<br /><table class="selection">
<thead>
<tr>
<th class="ascending">', _('Type'), '</th>
<th class="ascending">', _('Number'), '</th>
<th class="ascending">', _('Date'), '</th>
<th>', _('Branch'), '</th>
<th class="ascending">', _('Reference'), '</th>
<th>', _('Comments'), '</th>
<th>', _('Order'), '</th>
<th>', _('Charges'), '</th>
<th>', _('Credits'), '</th>
<th>', _('Allocated'), '</th>
<th>', _('Balance'), '</th>
<th class="noprint" colspan="4"> </th>
</tr>
</thead><tbody>';
$OutstandingOrSettled = '';
if ($_SESSION['InvoicePortraitFormat'] == 1) { //Invoice/credits in portrait
$PrintCustomerTransactionScript = 'PrintCustTransPortrait.php';
} else { //produce pdfs in landscape
$PrintCustomerTransactionScript = 'PrintCustTrans.php';
}
foreach ($Transactions as $MyRow) {
if ($MyRow['settled']==1 AND $OutstandingOrSettled=='') {
echo '<tr><th colspan="11">', _('TRANSACTIONS SETTLED SINCE'), ' ', $_POST['TransAfterDate'], '</th><th class="noprint" colspan="4"> </th></tr>';
$OutstandingOrSettled='Settled';
} elseif (($OutstandingOrSettled=='Settled' OR $OutstandingOrSettled=='') AND $MyRow['settled']==0) {
echo '<tr><th colspan="11">', _('OUTSTANDING TRANSACTIONS'), ' ', $_POST['TransAfterDate'], '</th><th class="noprint" colspan="4"> </th></tr>';
$OutstandingOrSettled='Outstanding';
}
$FormatedTranDate = ConvertSQLDate($MyRow['trandate']);
if ($MyRow['type']==10) { //its an invoice
echo '<tr class="striped_row">
<td>', _($MyRow['typename']), '</td>
<td class="number">', $MyRow['transno'], '</td>
<td>', ConvertSQLDate($MyRow['trandate']), '</td>
<td>', $MyRow['branchcode'], '</td>
<td>', $MyRow['reference'], '</td>
<td style="width:200px">', $MyRow['invtext'], '</td>
<td class="number">', $MyRow['order_'], '</td>
<td class="number">', locale_number_format($MyRow['totalamount'], $CustomerRecord['decimalplaces']), '</td>
<td> </td>
<td class="number">', locale_number_format($MyRow['alloc'], $CustomerRecord['decimalplaces']), '</td>
<td class="number">', locale_number_format($MyRow['balance'], $CustomerRecord['decimalplaces']), '</td>
<td class="noprint" title="', _('Click to preview the invoice'), '">
<a href="', $RootPath, '/PrintCustTrans.php?FromTransNo=', $MyRow['transno'], '&InvOrCredit=Invoice"><img alt="" src="', $RootPath, '/css/', $Theme, '/images/preview.png" /> ', _('HTML'), '</a>
</td>
<td class="noprint" title="', _('Click for PDF'), '">
<a href="', $RootPath, '/', $PrintCustomerTransactionScript, '?FromTransNo=', $MyRow['transno'], '&InvOrCredit=Invoice&PrintPDF=True"><img alt="" src="', $RootPath, '/css/', $Theme, '/images/pdf.png" /> ', _('PDF'), '</a>
</td>
<td class="noprint" title="', _('Click to email the invoice'), '">
<a href="', $RootPath, '/EmailCustTrans.php?FromTransNo=', $MyRow['transno'], '&InvOrCredit=Invoice"><img alt="" src="', $RootPath, '/css/', $Theme, '/images/email.png" />', _('Email'), '</a>
</td>
<td class="noprint"> </td>
</tr>';
} elseif ($MyRow['type'] == 11) {
echo '<tr class="striped_row">
<td>', _($MyRow['typename']), '</td>
<td class="number">', $MyRow['transno'], '</td>
<td>', ConvertSQLDate($MyRow['trandate']), '</td>
<td>', $MyRow['branchcode'], '</td>
<td>', $MyRow['reference'], '</td>
<td style="width:200px">', $MyRow['invtext'], '</td>
<td class="number">', $MyRow['order_'], '</td>
<td> </td>
<td class="number">', locale_number_format($MyRow['totalamount'], $CustomerRecord['decimalplaces']), '</td>
<td class="number">', locale_number_format($MyRow['alloc'], $CustomerRecord['decimalplaces']), '</td>
<td class="number">', locale_number_format($MyRow['balance'], $CustomerRecord['decimalplaces']), '</td>
<td class="noprint" title="', _('Click to preview the credit note'), '">
<a href="', $RootPath, '/PrintCustTrans.php?FromTransNo=', $MyRow['transno'], '&InvOrCredit=Credit"><img alt="" src="', $RootPath, '/css/', $Theme, '/images/preview.png" />', _('HTML'), '</a>
</td>
<td class="noprint" title="', _('Click for PDF'), '">
<a href="', $RootPath, '/', $PrintCustomerTransactionScript, '?FromTransNo=', $MyRow['transno'], '&InvOrCredit=Credit&PrintPDF=True"><img alt="" src="', $RootPath, '/css/', $Theme, '/images/pdf.png" />', _('PDF'), '</a>
</td>
<td class="noprint" title="', _('Click to email the credit note'), '">
<a href="', $RootPath, '/EmailCustTrans.php?FromTransNo=', $MyRow['transno'], '&InvOrCredit=Credit"><img alt="" src="', $RootPath, '/css/', $Theme, '/images/email.png" />', _('Email'), '</a>
</td>
<td class="noprint" title="', _('Click to allocate funds'), '">
<a href="', $RootPath, '/CustomerAllocations.php?AllocTrans=', $MyRow['id'], '"><img alt="" src="', $RootPath, '/css/', $Theme, '/images/allocation.png" />', _('Allocation'), '</a>
</td>
</tr>';
} elseif ($MyRow['type'] == 12 and $MyRow['totalamount'] < 0) {
/* Show transactions where:
* - Is receipt
*/
echo '<tr class="striped_row">
<td>', _($MyRow['typename']), '</td>
<td class="number">', $MyRow['transno'], '</td>
<td>', ConvertSQLDate($MyRow['trandate']), '</td>
<td>', $MyRow['branchcode'], '</td>
<td>', $MyRow['reference'], '</td>
<td style="width:200px">', $MyRow['invtext'], '</td>
<td class="number">', $MyRow['order_'], '</td>
<td> </td>
<td class="number">', locale_number_format($MyRow['totalamount'], $CustomerRecord['decimalplaces']), '</td>
<td class="number">', locale_number_format($MyRow['alloc'], $CustomerRecord['decimalplaces']), '</td>
<td class="number">', locale_number_format($MyRow['balance'], $CustomerRecord['decimalplaces']), '</td>
<td class="noprint" title="', _('Click to allocate funds'), '">
<a href="', $RootPath, '/CustomerAllocations.php?AllocTrans=', $MyRow['id'], '"><img alt="" src="', $RootPath, '/css/', $Theme, '/images/allocation.png" />', _('Allocation'), '</a>
</td>
<td class="noprint"> </td>
<td class="noprint"> </td>
<td class="noprint"> </td>
</tr>';
} elseif ($MyRow['type'] == 12 and $MyRow['totalamount'] > 0) {
/* Show transactions where:
* - Is a negative receipt
* - User cannot view GL transactions
*/
echo '<tr class="striped_row">
<td>', _($MyRow['typename']), '</td>
<td class="number">', $MyRow['transno'], '</td>
<td>', ConvertSQLDate($MyRow['trandate']), '</td>
<td>', $MyRow['branchcode'], '</td>
<td>', $MyRow['reference'], '</td>
<td style="width:200px">', $MyRow['invtext'], '</td>
<td class="number">', $MyRow['order_'], '</td>
<td class="number">', locale_number_format($MyRow['totalamount'], $CustomerRecord['decimalplaces']), '</td>
<td> </td>
<td class="number">', locale_number_format($MyRow['alloc'], $CustomerRecord['decimalplaces']), '</td>
<td class="number">', locale_number_format($MyRow['balance'], $CustomerRecord['decimalplaces']), '</td>
<td class="noprint"> </td>
<td class="noprint"> </td>
<td class="noprint"> </td>
<td class="noprint"> </td>
</tr>';
}
}
//end of while loop
echo '</tbody></table>
<br />
<table class="selection" width="70%">
<tr>
<th style="width:20%">', _('Total Balance'), '</th>
<th style="width:20%">', _('Current'), '</th>
<th style="width:20%">', _('Now Due'), '</th>
<th style="width:20%">', $_SESSION['PastDueDays1'], '-', $_SESSION['PastDueDays2'], ' ', _('Days Overdue'), '</th>
<th style="width:20%">', _('Over'), ' ', $_SESSION['PastDueDays2'], ' ', _('Days Overdue'), '</th>
</tr>
<tr>
<td class="number">', locale_number_format($CustomerRecord['balance'], $CustomerRecord['decimalplaces']), '</td>
<td class="number">', locale_number_format(($CustomerRecord['balance'] - $CustomerRecord['due']), $CustomerRecord['decimalplaces']), '</td>
<td class="number">', locale_number_format(($CustomerRecord['due'] - $CustomerRecord['overdue1']), $CustomerRecord['decimalplaces']), '</td>
<td class="number">', locale_number_format(($CustomerRecord['overdue1'] - $CustomerRecord['overdue2']), $CustomerRecord['decimalplaces']), '</td>
<td class="number">', locale_number_format($CustomerRecord['overdue2'], $CustomerRecord['decimalplaces']), '</td>
</tr>
</table>';
include('includes/footer.php');
?>