PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/chronique/BankMatching.php

http://chronique.googlecode.com/
PHP | 347 lines | 295 code | 44 blank | 8 comment | 30 complexity | 28f442eea5190d80b2a1a8453acd6e73 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: BankMatching.php 4735 2011-10-29 05:59:53Z daintree $*/
  3. include('includes/session.inc');
  4. $title = _('Bank Account Matching');
  5. include('includes/header.inc');
  6. if ((isset($_GET['Type'])
  7. AND $_GET['Type']=='Receipts')
  8. OR (isset($_POST['Type'])
  9. AND $_POST['Type']=='Receipts')){
  10. $Type = 'Receipts';
  11. $TypeName =_('Receipts');
  12. echo '<p class="page_title_text">
  13. <img src="'.$rootpath.'/css/'.$theme.'/images/money_add.png" title="' . _('Bank Matching') . '" alt="" />' . ' ' . _('Bank Account Matching - Receipts') . '
  14. </p>';
  15. } elseif ((isset($_GET['Type']) AND $_GET['Type']=='Payments') OR
  16. (isset($_POST['Type']) and $_POST['Type']=='Payments')) {
  17. $Type = 'Payments';
  18. $TypeName =_('Payments');
  19. echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/money_delete.png" title="' . _('Bank Matching') . '" alt="" />' . ' ' . _('Bank Account Matching - Payments') . '</p>';
  20. } else {
  21. prnMsg(_('This page must be called with a bank transaction type') . '. ' . _('It should not be called directly'),'error');
  22. include ('includes/footer.inc');
  23. exit;
  24. }
  25. if (isset($_GET['Account'])) {
  26. $_POST['BankAccount']=$_GET['Account'];
  27. $_POST['ShowTransactions']=true;
  28. $_POST['Ostg_or_All']='Ostg';
  29. $_POST['First20_or_All']='All';
  30. }
  31. if (isset($_POST['Update']) AND $_POST['RowCounter']>1){
  32. for ($Counter=1;$Counter <= $_POST['RowCounter']; $Counter++){
  33. if (isset($_POST['Clear_' . $Counter]) AND $_POST['Clear_' . $Counter]==True){
  34. /*Get amount to be cleared */
  35. $sql = "SELECT amount,
  36. exrate
  37. FROM banktrans
  38. WHERE banktransid='" . $_POST['BankTrans_' . $Counter]."'";
  39. $ErrMsg = _('Could not retrieve transaction information');
  40. $result = DB_query($sql,$db,$ErrMsg);
  41. $myrow=DB_fetch_array($result);
  42. $AmountCleared = round($myrow[0] / $myrow[1],2);
  43. /*Update the banktrans recoord to match it off */
  44. $sql = "UPDATE banktrans SET amountcleared= ". $AmountCleared .
  45. " WHERE banktransid='" . $_POST['BankTrans_' . $Counter] . "'";
  46. $ErrMsg = _('Could not match off this payment because');
  47. $result = DB_query($sql,$db,$ErrMsg);
  48. } elseif ((isset($_POST['AmtClear_' . $Counter])
  49. AND filter_number_format($_POST['AmtClear_' . $Counter])<0 AND $Type=='Payments')
  50. OR ($Type=='Receipts' AND isset($_POST['AmtClear_' . $Counter]) AND filter_number_format($_POST['AmtClear_' . $Counter])>0)){
  51. /*if the amount entered was numeric and negative for a payment or positive for a receipt */
  52. $sql = "UPDATE banktrans SET amountcleared=" . filter_number_format($_POST['AmtClear_' . $Counter]) . "
  53. WHERE banktransid='" . $_POST['BankTrans_' . $Counter]."'";
  54. $ErrMsg = _('Could not update the amount matched off this bank transaction because');
  55. $result = DB_query($sql,$db,$ErrMsg);
  56. } elseif (isset($_POST['Unclear_' . $Counter])
  57. AND $_POST['Unclear_' . $Counter]==True){
  58. $sql = "UPDATE banktrans SET amountcleared = 0
  59. WHERE banktransid='" . $_POST['BankTrans_' . $Counter]."'";
  60. $ErrMsg = _('Could not unclear this bank transaction because');
  61. $result = DB_query($sql,$db,$ErrMsg);
  62. }
  63. }
  64. /*Show the updated position with the same criteria as previously entered*/
  65. $_POST['ShowTransactions'] = True;
  66. }
  67. echo '<div class="page_help_text">' . _('Use this screen to match webERP Receipts and Payments to your Bank Statement. Check your bank statement and click the check-box when you find the matching transaction.') . '</div><br />';
  68. echo '<form action="'. htmlspecialchars($_SERVER['PHP_SELF']) . '" method=post>';
  69. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
  70. echo '<input type="hidden" name="Type" value="' . $Type . '" />';
  71. echo '<table class="selection">
  72. <tr>
  73. <td align="left">' . _('Bank Account') . ':</td>
  74. <td colspan=3><select tabindex="1" name="BankAccount">';
  75. $sql = "SELECT accountcode, bankaccountname FROM bankaccounts";
  76. $resultBankActs = DB_query($sql,$db);
  77. while ($myrow=DB_fetch_array($resultBankActs)){
  78. if (isset($_POST['BankAccount']) and $myrow['accountcode']==$_POST['BankAccount']){
  79. echo '<option selected value="' . $myrow['accountcode'] . '">' . $myrow['bankaccountname'] . '</option>';
  80. } else {
  81. echo '<option value="' . $myrow['accountcode'] . '">' . $myrow['bankaccountname'] . '</option>';
  82. }
  83. }
  84. echo '</select></td>
  85. </tr>';
  86. if (!isset($_POST['BeforeDate']) OR !Is_Date($_POST['BeforeDate'])){
  87. $_POST['BeforeDate'] = Date($_SESSION['DefaultDateFormat']);
  88. }
  89. if (!isset($_POST['AfterDate']) OR !Is_Date($_POST['AfterDate'])){
  90. $_POST['AfterDate'] = Date($_SESSION['DefaultDateFormat'], Mktime(0,0,0,Date('m')-3,Date('d'),Date('y')));
  91. }
  92. // Change to allow input of FROM DATE and then TO DATE, instead of previous back-to-front method, add datepicker
  93. echo '<tr>
  94. <td>' . _('Show') . ' ' . $TypeName . ' ' . _('from') . ':</td>
  95. <td><input tabindex="3" type="text" name="AfterDate" class="date" alt="'.$_SESSION['DefaultDateFormat'].'" size="12" maxlength="10" onChange="isDate(this, this.value, '."'".$_SESSION['DefaultDateFormat']."'".')" value="' . $_POST['AfterDate'] . '" /></td></tr>';
  96. echo '<td>' . _('to') . ':</td>
  97. <td><input tabindex="2" type="text" name="BeforeDate" class="date" alt="'.$_SESSION['DefaultDateFormat'].'" size="12" maxlength="10" onChange="isDate(this, this.value, '."'".$_SESSION['DefaultDateFormat']."'".')" value="' . $_POST['BeforeDate'] . '" /></td>';
  98. echo '<tr>
  99. <td colspan="3">' . _('Choose outstanding') . ' ' . $TypeName . ' ' . _('only or all') . ' ' . $TypeName . ' ' . _('in the date range') . ':</td>
  100. <td><select tabindex="4" name="Ostg_or_All">';
  101. if ($_POST['Ostg_or_All']=='All'){
  102. echo '<option selected value="All">' . _('Show all') . ' ' . $TypeName . ' ' . _('in the date range') . '</option>';
  103. echo '<option value="Ostdg">' . _('Show unmatched') . ' ' . $TypeName . ' ' . _('only') . '</option>';
  104. } else {
  105. echo '<option value="All">' . _('Show all') . ' ' . $TypeName . ' ' . _('in the date range') . '</option>';
  106. echo '<option selected value="Ostdg">' . _('Show unmatched') . ' ' . $TypeName . ' ' . _('only') . '</option>';
  107. }
  108. echo '</select></td>
  109. </tr>';
  110. echo '<tr>
  111. <td colspan="3">' . _('Choose to display only the first 20 matching') . ' ' . $TypeName . ' ' .
  112. _('or all') . ' ' . $TypeName . ' ' . _('meeting the criteria') . ':</td>
  113. <td><select tabindex="5" name="First20_or_All">';
  114. if ($_POST['First20_or_All']=='All'){
  115. echo '<option selected value="All">' . _('Show all') . ' ' . $TypeName . ' ' . _('in the date range') . '</option>';
  116. echo '<option value="First20">' . _('Show only the first 20') . ' ' . $TypeName . '</option>';
  117. } else {
  118. echo '<option value="All">' . _('Show all') . ' ' . $TypeName . ' ' . _('in the date range') . '</option>';
  119. echo '<option selected value="First20">' . _('Show only the first 20') . ' ' . $TypeName . '</option>';
  120. }
  121. echo '</select></td>
  122. </tr>';
  123. echo '</table>
  124. <br />
  125. <div class="centre">
  126. <input tabindex="6" type="submit" name="ShowTransactions" value="' . _('Show selected') . ' ' . $TypeName . '" />
  127. <p>
  128. <a href="' . $rootpath . '/BankReconciliation.php">' . _('Show reconciliation') . '</a>
  129. </p>
  130. </div>
  131. <hr />';
  132. $InputError=0;
  133. if (!Is_Date($_POST['BeforeDate'])){
  134. $InputError =1;
  135. prnMsg(_('The date entered for the field to show') . ' ' . $TypeName . ' ' . _('before') . ', ' .
  136. _('is not entered in a recognised date format') . '. ' . _('Entry is expected in the format') . ' ' .
  137. $_SESSION['DefaultDateFormat'],'error');
  138. }
  139. if (!Is_Date($_POST['AfterDate'])){
  140. $InputError =1;
  141. prnMsg( _('The date entered for the field to show') . ' ' . $Type . ' ' . _('after') . ', ' .
  142. _('is not entered in a recognised date format') . '. ' . _('Entry is expected in the format') . ' ' .
  143. $_SESSION['DefaultDateFormat'],'error');
  144. }
  145. if ($InputError !=1
  146. AND isset($_POST['BankAccount'])
  147. AND $_POST['BankAccount']!=''
  148. AND isset($_POST['ShowTransactions'])){
  149. $SQLBeforeDate = FormatDateForSQL($_POST['BeforeDate']);
  150. $SQLAfterDate = FormatDateForSQL($_POST['AfterDate']);
  151. $BankResult = DB_query("SELECT decimalplaces,
  152. currcode
  153. FROM bankaccounts INNER JOIN currencies
  154. ON bankaccounts.currcode=currencies.currabrev
  155. WHERE accountcode='" . $_POST['BankAccount'] . "'",
  156. $db);
  157. $BankRow = DB_fetch_array($BankResult);
  158. $CurrDecimalPlaces = $BankRow['decimalplaces'];
  159. $CurrCode = $BankRow['currcode'];
  160. if ($_POST['Ostg_or_All']=='All'){
  161. if ($Type=='Payments'){
  162. $sql = "SELECT banktransid,
  163. ref,
  164. amountcleared,
  165. transdate,
  166. amount/exrate as amt,
  167. banktranstype
  168. FROM banktrans
  169. WHERE amount < 0
  170. AND transdate >= '". $SQLAfterDate . "'
  171. AND transdate <= '" . $SQLBeforeDate . "'
  172. AND bankact='" .$_POST['BankAccount'] . "'
  173. ORDER BY transdate";
  174. } else { /* Type must == Receipts */
  175. $sql = "SELECT banktransid,
  176. ref,
  177. amountcleared,
  178. transdate,
  179. amount/exrate as amt,
  180. banktranstype
  181. FROM banktrans
  182. WHERE amount >0
  183. AND transdate >= '". $SQLAfterDate . "'
  184. AND transdate <= '" . $SQLBeforeDate . "'
  185. AND bankact='" .$_POST['BankAccount'] . "'
  186. ORDER BY transdate";
  187. }
  188. } else { /*it must be only the outstanding bank trans required */
  189. if ($Type=='Payments'){
  190. $sql = "SELECT banktransid,
  191. ref,
  192. amountcleared,
  193. transdate,
  194. amount/exrate as amt,
  195. banktranstype
  196. FROM banktrans
  197. WHERE amount <0
  198. AND transdate >= '". $SQLAfterDate . "'
  199. AND transdate <= '" . $SQLBeforeDate . "'
  200. AND bankact=" .$_POST['BankAccount'] . "
  201. AND ABS(amountcleared - (amount / exrate)) > 0.009
  202. ORDER BY transdate";
  203. } else { /* Type must == Receipts */
  204. $sql = "SELECT banktransid,
  205. ref,
  206. amountcleared,
  207. transdate,
  208. amount/exrate as amt,
  209. banktranstype
  210. FROM banktrans
  211. WHERE amount >0
  212. AND transdate >= '". $SQLAfterDate . "'
  213. AND transdate <= '" . $SQLBeforeDate . "'
  214. AND bankact='" .$_POST['BankAccount'] . "'
  215. AND ABS(amountcleared - (amount / exrate)) > 0.009
  216. ORDER BY transdate";
  217. }
  218. }
  219. if ($_POST['First20_or_All']!='All'){
  220. $sql = $sql . " LIMIT 20";
  221. }
  222. $ErrMsg = _('The payments with the selected criteria could not be retrieved because');
  223. $PaymentsResult = DB_query($sql, $db, $ErrMsg);
  224. $TableHeader = '<tr>
  225. <th>'. _('Ref'). '</th>
  226. <th>' . $TypeName . '</th>
  227. <th>' . _('Date') . '</th>
  228. <th>' . _('Amount') . ' ' . $CurrCode .'</th>
  229. <th>' . _('Outstanding') . ' ' . $CurrCode . '</th>
  230. <th colspan="3">' . _('Clear') . ' / ' . _('Unclear') . '</th>
  231. </tr>';
  232. echo '<table class="selection">' . $TableHeader;
  233. $j = 1; //page length counter
  234. $k=0; //row colour counter
  235. $i = 1; //no of rows counter
  236. while ($myrow=DB_fetch_array($PaymentsResult)) {
  237. $DisplayTranDate = ConvertSQLDate($myrow['transdate']);
  238. $Outstanding = $myrow['amt']- $myrow['amountcleared'];
  239. if (ABS($Outstanding)<0.009){ /*the payment is cleared dont show the check box*/
  240. printf('<tr bgcolor="#CCCEEE">
  241. <td>%s</td>
  242. <td>%s</td>
  243. <td>%s</td>
  244. <td class="number">%s</td>
  245. <td class="number">%s</td>
  246. <td colspan="2">%s</td>
  247. <td><input type="checkbox" name="Unclear_%s" /><input type="hidden" name="BankTrans_%s" value="%s" /></td>
  248. </tr>',
  249. $myrow['ref'],
  250. $myrow['banktranstype'],
  251. $DisplayTranDate,
  252. locale_number_format($myrow['amt'],$CurrDecimalPlaces),
  253. locale_number_format($Outstanding,$CurrDecimalPlaces),
  254. _('Unclear'),
  255. $i,
  256. $i,
  257. $myrow['banktransid']);
  258. } else{
  259. if ($k==1){
  260. echo '<tr class="EvenTableRows">';
  261. $k=0;
  262. } else {
  263. echo '<tr class="OddTableRows">';
  264. $k=1;
  265. }
  266. printf('<td>%s</td>
  267. <td>%s</td>
  268. <td>%s</td>
  269. <td class="number">%s</td>
  270. <td class="number">%s</td>
  271. <td><input type="checkbox" name="Clear_%s" /><input type="hidden" name="BankTrans_%s" value="%s" /></td>
  272. <td colspan="2"><input type="text" maxlength="15" size="15" class="number" name="AmtClear_%s" /></td>
  273. </tr>',
  274. $myrow['ref'],
  275. $myrow['banktranstype'],
  276. $DisplayTranDate,
  277. locale_number_format($myrow['amt'],$CurrDecimalPlaces),
  278. locale_number_format($Outstanding,$CurrDecimalPlaces),
  279. $i,
  280. $i,
  281. $myrow['banktransid'],
  282. $i
  283. );
  284. }
  285. $j++;
  286. If ($j == 12){
  287. $j=1;
  288. echo $TableHeader;
  289. }
  290. //end of page full new headings if
  291. $i++;
  292. }
  293. //end of while loop
  294. echo '</table>
  295. <br />
  296. <div class="centre">
  297. <input type="hidden" name="RowCounter" value="' . $i . '" />
  298. <input type="submit" name="Update" value="' . _('Update Matching') . '" />
  299. </div>';
  300. }
  301. echo '</form>';
  302. include('includes/footer.inc');
  303. ?>