-
Notifications
You must be signed in to change notification settings - Fork 3
/
BankReconciliation.php
354 lines (288 loc) · 13.2 KB
/
BankReconciliation.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
<?php
/* $Id$*/
include ('includes/session.inc');
$title = _('Bank Reconciliation');
include('includes/header.inc');
echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '">';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/money_add.png" title="' . _('Search') . '" alt="" />' . ' ' . $title.'</p><br />';
if (isset($_GET['Account'])) {
$_POST['BankAccount']=$_GET['Account'];
$_POST['ShowRec']=true;
}
if (isset($_POST['PostExchangeDifference']) and is_numeric($_POST['DoExchangeDifference'])){
if (!is_numeric($_POST['BankStatementBalance'])){
prnMsg(_('The entry in the bank statement balance is not numeric. The balance on the bank statement should be entered. The exchange difference has not been calculated and no general ledger journal has been created'),'warn');
echo '<br />' . $_POST['BankStatementBalance'];
} else {
/* Now need to get the currency of the account and the current table ex rate */
$SQL = "SELECT rate,
bankaccountname,
decimalplaces AS currdecimalplaces
FROM bankaccounts INNER JOIN currencies
ON bankaccounts.currcode=currencies.currabrev
WHERE bankaccounts.accountcode = '" . $_POST['BankAccount']."'";
$ErrMsg = _('Could not retrieve the exchange rate for the selected bank account');
$CurrencyResult = DB_query($SQL,$db);
$CurrencyRow = DB_fetch_row($CurrencyResult);
$ExRate = $CurrencyRow[0];
$BankAccountName = $CurrencyRow[1];
$CalculatedBalance = $_POST['DoExchangeDifference'];
$ExchangeDifference = ($CalculatedBalance - $_POST['BankStatementBalance'])/$ExRate;
include ('includes/SQL_CommonFunctions.inc');
$ExDiffTransNo = GetNextTransNo(36,$db);
/*Post the exchange difference to the last day of the month prior to current date*/
$PostingDate = Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, Date('m'), 0,Date('Y')));
$PeriodNo = GetPeriod($PostingDate,$db);
$result = DB_Txn_Begin($db);
//yet to code the journal
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
defaulttag,
narrative,
amount)
VALUES (36,
'" . $ExDiffTransNo . "',
'" . FormatDateForSQL($PostingDate) . "',
'" . $PeriodNo . "',
'" . $_SESSION['CompanyRecord']['exchangediffact'] . "',
'" . $_SESSION['DefaultTag'] . "',
'" . $BankAccountName . ' ' . _('reconciliation on') . " " .
Date($_SESSION['DefaultDateFormat']) . "','" . $ExchangeDifference . "')";
$ErrMsg = _('Cannot insert a GL entry for the exchange difference because');
$DbgMsg = _('The SQL that failed to insert the exchange difference GL entry was');
$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
defaulttag,
narrative,
amount)
VALUES (36,
'" . $ExDiffTransNo . "',
'" . FormatDateForSQL($PostingDate) . "',
'" . $PeriodNo . "',
'" . $_POST['BankAccount'] . "',
'" . $_SESSION['DefaultTag'] . "',
'" . $BankAccountName . ' ' . _('reconciliation on') . ' ' . Date($_SESSION['DefaultDateFormat']) . "',
'" . (-$ExchangeDifference) . "')";
$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
$result = DB_Txn_Commit($db);
prnMsg(_('Exchange difference of') . ' ' . number_format($ExchangeDifference,2) . ' ' . _('has been posted'),'success');
} //end if the bank statement balance was numeric
}
echo '<table class="selection">';
$SQL = "SELECT bankaccountname, accountcode FROM bankaccounts";
$ErrMsg = _('The bank accounts could not be retrieved by the SQL because');
$DbgMsg = _('The SQL used to retrieve the bank accounts was');
$AccountsResults = DB_query($SQL,$db,$ErrMsg,$DbgMsg);
echo '<tr><td>' . _('Bank Account') . ':</td><td><select tabindex="1" name="BankAccount">';
if (DB_num_rows($AccountsResults)==0){
echo '</select></td></tr></table><br />' . _('Bank Accounts have not yet been defined') . '. ' . _('You must first') .
'<a href="' . $rootpath . '/BankAccounts.php">' . _('define the bank accounts') . '</a>' . ' ' . _('and general ledger accounts to be affected') . '.';
include('includes/footer.inc');
exit;
} else {
while ($myrow=DB_fetch_array($AccountsResults)){
/*list the bank account names */
if (isset($_POST['BankAccount']) and $_POST['BankAccount']==$myrow['accountcode']){
echo '<option selected="True" value="' . $myrow['accountcode'] . '">' . $myrow['bankaccountname'] . '</option>';
} else {
echo '<option value="' . $myrow['accountcode'] . '">' . $myrow['bankaccountname'] . '</option>';
}
}
echo '</select></td></tr>';
}
/*Now do the posting while the user is thinking about the bank account to select */
include ('includes/GLPostings.inc');
echo '</table><br /><div class="centre"><button type="submit" tabindex="2" name="ShowRec">' . _('Show bank reconciliation statement') . '</button></div><br />';
if (isset($_POST['ShowRec']) OR isset($_POST['DoExchangeDifference'])){
/*Get the balance of the bank account concerned */
$sql = "SELECT MAX(period) FROM chartdetails WHERE accountcode='" . $_POST['BankAccount']."'";
$PrdResult = DB_query($sql, $db);
$myrow = DB_fetch_row($PrdResult);
$LastPeriod = $myrow[0];
$SQL = "SELECT bfwd+actual AS balance
FROM chartdetails WHERE period='" . $LastPeriod . "' AND accountcode='" . $_POST['BankAccount']."'";
$ErrMsg = _('The bank account balance could not be returned by the SQL because');
$BalanceResult = DB_query($SQL,$db,$ErrMsg);
$myrow = DB_fetch_row($BalanceResult);
$Balance = $myrow[0];
/* Now need to get the currency of the account and the current table ex rate */
$SQL = "SELECT rate,
bankaccounts.currcode,
bankaccounts.bankaccountname
FROM bankaccounts INNER JOIN currencies
ON bankaccounts.currcode=currencies.currabrev
WHERE bankaccounts.accountcode = '" . $_POST['BankAccount']."'";
$ErrMsg = _('Could not retrieve the currency and exchange rate for the selected bank account');
$CurrencyResult = DB_query($SQL,$db);
$CurrencyRow = DB_fetch_row($CurrencyResult);
$ExRate = $CurrencyRow[0];
$BankCurrCode = $CurrencyRow[1];
$BankAccountName = $CurrencyRow[2];
echo '<table class="selection">
<tr class="EvenTableRows"><td colspan="6"><b>' . $BankAccountName . ' ' . _('Balance as at') . ' ' . Date($_SESSION['DefaultDateFormat']);
if ($_SESSION['CompanyRecord']['currencydefault']!=$BankCurrCode){
echo ' (' . $BankCurrCode . ' @ ' . $ExRate .')';
}
echo '</b></td>
<td valign="bottom" class="number"><b>' . number_format($Balance*$ExRate,2) . '</b></td></tr>';
$SQL = "SELECT amount/exrate AS amt,
amountcleared,
(amount/exrate)-amountcleared as outstanding,
ref,
chequeno,
transdate,
systypes.typename,
transno
FROM banktrans,
systypes
WHERE banktrans.type = systypes.typeid
AND banktrans.bankact='" . $_POST['BankAccount'] . "'
AND amount < 0
AND ABS((amount/exrate)-amountcleared)>0.009 ORDER BY transdate";
echo '<tr></tr>'; /*Bang in a blank line */
$ErrMsg = _('The unpresented cheques could not be retrieved by the SQL because');
$UPChequesResult = DB_query($SQL, $db, $ErrMsg);
echo '<tr><td colspan="6"><b>' . _('Add back unpresented cheques') . ':</b></td></tr>';
$TableHeader = '<tr>
<th>' . _('Date') . '</th>
<th>' . _('Type') . '</th>
<th>' . _('Number') . '</th>
<th>' . _('Reference') . '</th>
<th>' . _('Cheque No') . '</th>
<th>' . _('Orig Amount') . '</th>
<th>' . _('Outstanding') . '</th>
</tr>';
echo $TableHeader;
$j = 1;
$k=0; //row colour counter
$TotalUnpresentedCheques =0;
while ($myrow=DB_fetch_array($UPChequesResult)) {
if ($k==1){
echo '<tr class="EvenTableRows">';
$k=0;
} else {
echo '<tr class="OddTableRows">';
$k++;
}
printf('<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td class="number">%01.2f</td>
<td class="number">%01.2f</td>
</tr>',
ConvertSQLDate($myrow['transdate']),
$myrow['typename'],
$myrow['transno'],
$myrow['ref'],
$myrow['chequeno'],
$myrow['amt'],
$myrow['outstanding']);
$TotalUnpresentedCheques +=$myrow['outstanding'];
$j++;
If ($j == 18){
$j=1;
echo $TableHeader;
}
}
//end of while loop
echo '<tr></tr>
<tr class="EvenTableRows"><td colspan="6">' . _('Total of all unpresented cheques') . '</td><td class="number">' . number_format($TotalUnpresentedCheques,2) . '</td></tr>';
$SQL = "SELECT amount/exrate AS amt,
amountcleared,
(amount/exrate)-amountcleared as outstanding,
ref,
chequeno,
transdate,
systypes.typename,
transno
FROM banktrans,
systypes
WHERE banktrans.type = systypes.typeid
AND banktrans.bankact='" . $_POST['BankAccount'] . "'
AND amount > 0
AND ABS((amount/exrate)-amountcleared)>0.009 ORDER BY transdate";
echo '<tr></tr>'; /*Bang in a blank line */
$ErrMsg = _('The uncleared deposits could not be retrieved by the SQL because');
$UPChequesResult = DB_query($SQL,$db,$ErrMsg);
echo '<tr><td colspan="6"><b>' . _('Less deposits not cleared') . ':</b></td></tr>';
$TableHeader = '<tr>
<th>' . _('Date') . '</th>
<th>' . _('Type') . '</th>
<th>' . _('Number') . '</th>
<th>' . _('Reference') . '</th>
<th>' . _('Cheque No') . '</th>
<th>' . _('Orig Amount') . '</th>
<th>' . _('Outstanding') . '</th>
</tr>';
echo '<tr>' . $TableHeader;
$j = 1;
$k=0; //row colour counter
$TotalUnclearedDeposits =0;
while ($myrow=DB_fetch_array($UPChequesResult)) {
if ($k==1){
echo '<tr class="EvenTableRows">';
$k=0;
} else {
echo '<tr class="OddTableRows">';
$k++;
}
printf('<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td class="number">%01.2f</td>
<td class="number">%01.2f</td>
</tr>',
ConvertSQLDate($myrow['transdate']),
$myrow['typename'],
$myrow['transno'],
$myrow['ref'],
$myrow['chequeno'],
$myrow['amt'],
$myrow['outstanding']
);
$TotalUnclearedDeposits +=$myrow['outstanding'];
$j++;
If ($j == 18){
$j=1;
echo $TableHeader;
}
}
//end of while loop
echo '<tr></tr><tr class="EvenTableRows"><td colspan="6">' . _('Total of all uncleared deposits') . '</td><td class="number">' . number_format($TotalUnclearedDeposits,2) . '</td></tr>';
$FXStatementBalance = ($Balance*$ExRate) - $TotalUnpresentedCheques -$TotalUnclearedDeposits;
echo '<tr></tr><tr class="EvenTableRows"><td colspan="6"><b>' . _('Bank statement balance should be') . ' (' . $BankCurrCode . ')</b></td><td class="number">' . number_format($FXStatementBalance,2) . '</td></tr>';
if (isset($_POST['DoExchangeDifference'])){
echo '<input type="hidden" name="DoExchangeDifference" value="' . $FXStatementBalance . '" />';
echo '<tr><td colspan="6">' . _('Enter the actual bank statement balance') . ' (' . $BankCurrCode . ')</b></td>
<td class="number"><input type="text" name="BankStatementBalance" maxlength="15" size="15" value="' . $_POST['BankStatementBalance'] . '" /><td></tr>';
echo '<tr><td colspan="7" align="center"><button type="submit" name="PostExchangeDifference" onclick="return confirm(\'' . _('This will create a general ledger journal to write off the exchange difference in the current balance of the account. It is important that the exchange rate above reflects the current value of the bank account currency') . ' - ' . _('Are You Sure?') . '\');">' . _('Calculate and Post Exchange Difference') . '</button></td></tr>';
}
if ($_SESSION['CompanyRecord']['currencydefault']!=$BankCurrCode AND !isset($_POST['DoExchangeDifference'])){
echo '<tr><td colspan="7"><hr></td></tr>
<tr><td colspan="7">' . _('It is normal for foreign currency accounts to have exchange differences that need to be reflected as the exchange rate varies. This reconciliation is prepared using the exchange rate set up in the currencies table (see the set-up tab). This table must be maintained with the current exchange rate before running the reconciliation. If you wish to create a journal to reflect the exchange difference based on the current exchange rate to correct the reconciliation to the actual bank statement balance click below.') . '</td></tr>';
echo '<tr><td colspan="7" align="center"><button type="submit" name="DoExchangeDifference">' . _('Calculate and Post Exchange Difference') . '</button></td></tr>';
}
echo '</table>';
}
if (isset($_POST['BankAccount'])) {
echo '<br /><div class="centre"><a tabindex="4" href="' . $rootpath . '/BankMatching.php?Type=Payments&Account='.$_POST['BankAccount'].'">' . _('Match off cleared payments') . '</a>';
echo '<br /><a tabindex="5" href="' . $rootpath . '/BankMatching.php?Type=Receipts&Account='.$_POST['BankAccount'].'">' . _('Match off cleared deposits') . '</a></div>';
} else {
echo '<br /><div class="centre"><a tabindex="4" href="' . $rootpath . '/BankMatching.php?Type=Payments">' . _('Match off cleared payments') . '</a>';
echo '<br /><a tabindex="5" href="' . $rootpath . '/BankMatching.php?Type=Receipts">' . _('Match off cleared deposits') . '</a></div>';
}
echo '</form>';
include('includes/footer.inc');
?>