PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/chronique/BankReconciliation.php

http://chronique.googlecode.com/
PHP | 394 lines | 316 code | 68 blank | 10 comment | 26 complexity | e7db19270b6abf80eb98e1c062ac878d MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /* $Id: BankReconciliation.php 4735 2011-10-29 05:59:53Z daintree $*/
  3. include ('includes/session.inc');
  4. $title = _('Bank Reconciliation');
  5. include('includes/header.inc');
  6. echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '">';
  7. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
  8. echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/money_add.png" title="' . _('Search') . '" alt="" />' . ' ' . $title.'</p><br />';
  9. if (isset($_GET['Account'])) {
  10. $_POST['BankAccount']=$_GET['Account'];
  11. $_POST['ShowRec']=true;
  12. }
  13. if (isset($_POST['BankStatementBalance'])){
  14. $_POST['BankStatementBalance'] = filter_number_format($_POST['BankStatementBalance']);
  15. }
  16. if (isset($_POST['PostExchangeDifference']) AND is_numeric(filter_number_format($_POST['DoExchangeDifference']))){
  17. if (!is_numeric($_POST['BankStatementBalance'])){
  18. 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');
  19. echo '<p>' . $_POST['BankStatementBalance'];
  20. } else {
  21. /* Now need to get the currency of the account and the current table ex rate */
  22. $SQL = "SELECT rate,
  23. bankaccountname,
  24. decimalplaces AS currdecimalplaces
  25. FROM bankaccounts INNER JOIN currencies
  26. ON bankaccounts.currcode=currencies.currabrev
  27. WHERE bankaccounts.accountcode = '" . $_POST['BankAccount']."'";
  28. $ErrMsg = _('Could not retrieve the exchange rate for the selected bank account');
  29. $CurrencyResult = DB_query($SQL,$db);
  30. $CurrencyRow = DB_fetch_array($CurrencyResult);
  31. $CalculatedBalance = filter_number_format($_POST['DoExchangeDifference']);
  32. $ExchangeDifference = ($CalculatedBalance - filter_number_format($_POST['BankStatementBalance']))/$CurrencyRow['rate'];
  33. include ('includes/SQL_CommonFunctions.inc');
  34. $ExDiffTransNo = GetNextTransNo(36,$db);
  35. /*Post the exchange difference to the last day of the month prior to current date*/
  36. $PostingDate = Date($_SESSION['DefaultDateFormat'],mktime(0,0,0, Date('m'), 0,Date('Y')));
  37. $PeriodNo = GetPeriod($PostingDate,$db);
  38. $result = DB_Txn_Begin($db);
  39. //yet to code the journal
  40. $SQL = "INSERT INTO gltrans (type,
  41. typeno,
  42. trandate,
  43. periodno,
  44. account,
  45. narrative,
  46. amount)
  47. VALUES (36,
  48. '" . $ExDiffTransNo . "',
  49. '" . FormatDateForSQL($PostingDate) . "',
  50. '" . $PeriodNo . "',
  51. '" . $_SESSION['CompanyRecord']['exchangediffact'] . "',
  52. '" . $CurrencyRow['bankaccountname'] . ' ' . _('reconciliation on') . " " .
  53. Date($_SESSION['DefaultDateFormat']) . "','" . $ExchangeDifference . "')";
  54. $ErrMsg = _('Cannot insert a GL entry for the exchange difference because');
  55. $DbgMsg = _('The SQL that failed to insert the exchange difference GL entry was');
  56. $result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
  57. $SQL = "INSERT INTO gltrans (type,
  58. typeno,
  59. trandate,
  60. periodno,
  61. account,
  62. narrative,
  63. amount)
  64. VALUES (36,
  65. '" . $ExDiffTransNo . "',
  66. '" . FormatDateForSQL($PostingDate) . "',
  67. '" . $PeriodNo . "',
  68. '" . $_POST['BankAccount'] . "',
  69. '" . $CurrencyRow['bankaccountname'] . ' ' . _('reconciliation on') . ' ' . Date($_SESSION['DefaultDateFormat']) . "',
  70. '" . (-$ExchangeDifference) . "')";
  71. $result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
  72. $result = DB_Txn_Commit($db);
  73. prnMsg(_('Exchange difference of') . ' ' . locale_number_format($ExchangeDifference,$_SESSION['CompanyRecord']['decimalplaces']) . ' ' . _('has been posted'),'success');
  74. } //end if the bank statement balance was numeric
  75. }
  76. echo '<table class="selection">';
  77. $SQL = "SELECT bankaccountname, accountcode FROM bankaccounts";
  78. $ErrMsg = _('The bank accounts could not be retrieved by the SQL because');
  79. $DbgMsg = _('The SQL used to retrieve the bank accounts was');
  80. $AccountsResults = DB_query($SQL,$db,$ErrMsg,$DbgMsg);
  81. echo '<tr><td>' . _('Bank Account') . ':</td>
  82. <td><select tabindex="1" name="BankAccount">';
  83. if (DB_num_rows($AccountsResults)==0){
  84. echo '</select></td>
  85. </tr>
  86. </table>
  87. <p>' . _('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') . '.';
  88. include('includes/footer.inc');
  89. exit;
  90. } else {
  91. while ($myrow=DB_fetch_array($AccountsResults)){
  92. /*list the bank account names */
  93. if (isset($_POST['BankAccount']) and $_POST['BankAccount']==$myrow['accountcode']){
  94. echo '<option selected value="' . $myrow['accountcode'] . '">' . $myrow['bankaccountname'] . '</option>';
  95. } else {
  96. echo '<option value="' . $myrow['accountcode'] . '">' . $myrow['bankaccountname'] . '</option>';
  97. }
  98. }
  99. echo '</select></td>
  100. </tr>';
  101. }
  102. /*Now do the posting while the user is thinking about the bank account to select */
  103. include ('includes/GLPostings.inc');
  104. echo '</table>
  105. <p>
  106. <div class="centre">
  107. <input type=submit tabindex="2" name="ShowRec" value="' . _('Show bank reconciliation statement') . '" />
  108. </div>
  109. </p>
  110. <br />';
  111. if (isset($_POST['ShowRec']) OR isset($_POST['DoExchangeDifference'])){
  112. /*Get the balance of the bank account concerned */
  113. $sql = "SELECT MAX(period)
  114. FROM chartdetails
  115. WHERE accountcode='" . $_POST['BankAccount']."'";
  116. $PrdResult = DB_query($sql, $db);
  117. $myrow = DB_fetch_row($PrdResult);
  118. $LastPeriod = $myrow[0];
  119. $SQL = "SELECT bfwd+actual AS balance
  120. FROM chartdetails
  121. WHERE period='" . $LastPeriod . "'
  122. AND accountcode='" . $_POST['BankAccount']."'";
  123. $ErrMsg = _('The bank account balance could not be returned by the SQL because');
  124. $BalanceResult = DB_query($SQL,$db,$ErrMsg);
  125. $myrow = DB_fetch_row($BalanceResult);
  126. $Balance = $myrow[0];
  127. /* Now need to get the currency of the account and the current table ex rate */
  128. $SQL = "SELECT rate,
  129. bankaccounts.currcode,
  130. bankaccounts.bankaccountname,
  131. currencies.decimalplaces AS currdecimalplaces
  132. FROM bankaccounts INNER JOIN currencies
  133. ON bankaccounts.currcode=currencies.currabrev
  134. WHERE bankaccounts.accountcode = '" . $_POST['BankAccount']."'";
  135. $ErrMsg = _('Could not retrieve the currency and exchange rate for the selected bank account');
  136. $CurrencyResult = DB_query($SQL,$db);
  137. $CurrencyRow = DB_fetch_array($CurrencyResult);
  138. echo '<table class="selection">
  139. <tr class="EvenTableRows">
  140. <td colspan="6"><b>' . $CurrencyRow['bankaccountname'] . ' ' . _('Balance as at') . ' ' . Date($_SESSION['DefaultDateFormat']);
  141. if ($_SESSION['CompanyRecord']['currencydefault']!=$CurrencyRow['currcode']){
  142. echo ' (' . $CurrencyRow['currcode'] . ' @ ' . $CurrencyRow['rate'] .')';
  143. }
  144. echo '</b></td>
  145. <td valign="bottom" class="number"><b>' . locale_number_format($Balance*$CurrencyRow['rate'],$CurrencyRow['currdecimalplaces']) . '</b></td></tr>';
  146. $SQL = "SELECT amount/exrate AS amt,
  147. amountcleared,
  148. (amount/exrate)-amountcleared as outstanding,
  149. ref,
  150. transdate,
  151. systypes.typename,
  152. transno
  153. FROM banktrans,
  154. systypes
  155. WHERE banktrans.type = systypes.typeid
  156. AND banktrans.bankact='" . $_POST['BankAccount'] . "'
  157. AND amount < 0
  158. AND ABS((amount/exrate)-amountcleared)>0.009 ORDER BY transdate";
  159. echo '<tr></tr>'; /*Bang in a blank line */
  160. $ErrMsg = _('The unpresented cheques could not be retrieved by the SQL because');
  161. $UPChequesResult = DB_query($SQL, $db, $ErrMsg);
  162. echo '<tr>
  163. <td colspan="6"><b>' . _('Add back unpresented cheques') . ':</b></td>
  164. </tr>';
  165. $TableHeader = '<tr>
  166. <th>' . _('Date') . '</th>
  167. <th>' . _('Type') . '</th>
  168. <th>' . _('Number') . '</th>
  169. <th>' . _('Reference') . '</th>
  170. <th>' . _('Orig Amount') . '</th>
  171. <th>' . _('Outstanding') . '</th>
  172. </tr>';
  173. echo $TableHeader;
  174. $j = 1;
  175. $k=0; //row colour counter
  176. $TotalUnpresentedCheques =0;
  177. while ($myrow=DB_fetch_array($UPChequesResult)) {
  178. if ($k==1){
  179. echo '<tr class="EvenTableRows">';
  180. $k=0;
  181. } else {
  182. echo '<tr class="OddTableRows">';
  183. $k++;
  184. }
  185. printf('<td>%s</td>
  186. <td>%s</td>
  187. <td>%s</td>
  188. <td>%s</td>
  189. <td class="number">%s</td>
  190. <td class="number">%s</td>
  191. </tr>',
  192. ConvertSQLDate($myrow['transdate']),
  193. $myrow['typename'],
  194. $myrow['transno'],
  195. $myrow['ref'],
  196. locale_number_format($myrow['amt'],$CurrencyRow['currdecimalplaces']),
  197. locale_number_format($myrow['outstanding'],$CurrencyRow['currdecimalplaces']));
  198. $TotalUnpresentedCheques +=$myrow['outstanding'];
  199. $j++;
  200. If ($j == 18){
  201. $j=1;
  202. echo $TableHeader;
  203. }
  204. }
  205. //end of while loop
  206. echo '<tr></tr>
  207. <tr class="EvenTableRows">
  208. <td colspan="6">' . _('Total of all unpresented cheques') . '</td>
  209. <td class="number">' . locale_number_format($TotalUnpresentedCheques,$CurrencyRow['currdecimalplaces']) . '</td>
  210. </tr>';
  211. $SQL = "SELECT amount/exrate AS amt,
  212. amountcleared,
  213. (amount/exrate)-amountcleared AS outstanding,
  214. ref,
  215. transdate,
  216. systypes.typename,
  217. transno
  218. FROM banktrans INNER JOIN systypes
  219. ON banktrans.type = systypes.typeid
  220. WHERE banktrans.bankact='" . $_POST['BankAccount'] . "'
  221. AND amount > 0
  222. AND ABS((amount/exrate)-amountcleared)>0.009 ORDER BY transdate";
  223. echo '<tr></tr>'; /*Bang in a blank line */
  224. $ErrMsg = _('The uncleared deposits could not be retrieved by the SQL because');
  225. $UPChequesResult = DB_query($SQL,$db,$ErrMsg);
  226. echo '<tr><td colspan="6"><b>' . _('Less deposits not cleared') . ':</b></td></tr>';
  227. $TableHeader = '<tr>
  228. <th>' . _('Date') . '</th>
  229. <th>' . _('Type') . '</th>
  230. <th>' . _('Number') . '</th>
  231. <th>' . _('Reference') . '</th>
  232. <th>' . _('Orig Amount') . '</th>
  233. <th>' . _('Outstanding') . '</th>
  234. </tr>';
  235. echo $TableHeader;
  236. $j = 1;
  237. $k=0; //row colour counter
  238. $TotalUnclearedDeposits =0;
  239. while ($myrow=DB_fetch_array($UPChequesResult)) {
  240. if ($k==1){
  241. echo '<tr class="EvenTableRows">';
  242. $k=0;
  243. } else {
  244. echo '<tr class="OddTableRows">';
  245. $k++;
  246. }
  247. printf('<td>%s</td>
  248. <td>%s</td>
  249. <td>%s</td>
  250. <td>%s</td>
  251. <td class="number">%s</td>
  252. <td class="number">%s</td>
  253. </tr>',
  254. ConvertSQLDate($myrow['transdate']),
  255. $myrow['typename'],
  256. $myrow['transno'],
  257. $myrow['ref'],
  258. locale_number_format($myrow['amt'],$CurrencyRow['currdecimalplaces']),
  259. locale_number_format($myrow['outstanding'],$CurrencyRow['currdecimalplaces']) );
  260. $TotalUnclearedDeposits +=$myrow['outstanding'];
  261. $j++;
  262. if ($j == 18){
  263. $j=1;
  264. echo $TableHeader;
  265. }
  266. }
  267. //end of while loop
  268. echo '<tr>
  269. </tr>
  270. <tr class="EvenTableRows">
  271. <td colspan="6">' . _('Total of all uncleared deposits') . '</td>
  272. <td class="number">' . locale_number_format($TotalUnclearedDeposits,$CurrencyRow['currdecimalplaces']) . '</td>
  273. </tr>';
  274. $FXStatementBalance = ($Balance*$CurrencyRow['rate'] - $TotalUnpresentedCheques -$TotalUnclearedDeposits);
  275. echo '<tr>
  276. </tr>
  277. <tr class="EvenTableRows">
  278. <td colspan="6"><b>' . _('Bank statement balance should be') . ' (' . $CurrencyRow['currcode'] . ')</b></td>
  279. <td class="number">' . locale_number_format($FXStatementBalance,$CurrencyRow['currdecimalplaces']) . '</td>
  280. </tr>';
  281. if (isset($_POST['DoExchangeDifference'])){
  282. echo '<input type="hidden" name="DoExchangeDifference" value=' . $FXStatementBalance . ' />';
  283. if (!isset($_POST['BankStatementBalance'])){
  284. $_POST['BankStatementBalance'] =0;
  285. }
  286. echo '<tr>
  287. <td colspan="6">' . _('Enter the actual bank statement balance') . ' (' . $CurrencyRow['currcode'] . ')</b></td>
  288. <td class="number"><input type="text" name="BankStatementBalance" maxlength="15" size="15" value=' . locale_number_format($_POST['BankStatementBalance'],$CurrencyRow['currdecimalplaces']) . ' /><td>
  289. </tr>
  290. <tr>
  291. <td colspan="7" align="center"><input type="submit" name="PostExchangeDifference" value="' . _('Calculate and Post Exchange Difference') . '" 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?') . '\');" /></td>
  292. </tr>';
  293. }
  294. if ($_SESSION['CompanyRecord']['currencydefault']!=$CurrencyRow['currcode'] AND !isset($_POST['DoExchangeDifference'])){
  295. echo '<tr>
  296. <td colspan="7"><hr /></td>
  297. </tr>
  298. <tr>
  299. <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>
  300. </tr>
  301. <tr>
  302. <td colspan="7" align="center"><input type=submit name="DoExchangeDifference" value="' . _('Calculate and Post Exchange Difference') . '" /></td>
  303. </tr>';
  304. }
  305. echo '</table>';
  306. }
  307. if (isset($_POST['BankAccount'])) {
  308. echo '<div class="centre">
  309. <p>
  310. <a tabindex="4" href="' . $rootpath . '/BankMatching.php?Type=Payments&Account='.$_POST['BankAccount'].'">' . _('Match off cleared payments') . '</a>
  311. </p>
  312. <br />
  313. <a tabindex="5" href="' . $rootpath . '/BankMatching.php?Type=Receipts&Account='.$_POST['BankAccount'].'">' . _('Match off cleared deposits') . '</a>
  314. </div>';
  315. } else {
  316. echo '<div class="centre">
  317. <p>
  318. <a tabindex="4" href="' . $rootpath . '/BankMatching.php?Type=Payments">' . _('Match off cleared payments') . '</a>
  319. </p>
  320. <br />
  321. <a tabindex="5" href="' . $rootpath . '/BankMatching.php?Type=Receipts">' . _('Match off cleared deposits') . '</a>
  322. </div>';
  323. }
  324. echo '</form>';
  325. include('includes/footer.inc');
  326. ?>