PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/chronique/PaymentMethods.php

http://chronique.googlecode.com/
PHP | 289 lines | 224 code | 44 blank | 21 comment | 31 complexity | cedaeb10aa42f72729428ef23310f771 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: PaymentMethods.php 4734 2011-10-29 03:26:27Z daintree $*/
  3. include('includes/session.inc');
  4. $title = _('Payment Methods');
  5. include('includes/header.inc');
  6. echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/transactions.png" title="' . _('Payments') .
  7. '" alt="" />' . ' ' . $title.'</p>';
  8. if ( isset($_GET['SelectedPaymentID']) )
  9. $SelectedPaymentID = $_GET['SelectedPaymentID'];
  10. elseif (isset($_POST['SelectedPaymentID']))
  11. $SelectedPaymentID = $_POST['SelectedPaymentID'];
  12. if (isset($Errors)) {
  13. unset($Errors);
  14. }
  15. $Errors = array();
  16. if (isset($_POST['submit'])) {
  17. //initialise no input errors assumed initially before we test
  18. $InputError = 0;
  19. /* actions to take once the user has clicked the submit button
  20. ie the page has called itself with some user input */
  21. $i=1;
  22. //first off validate inputs sensible
  23. if (ContainsIllegalCharacters($_POST['MethodName'])) {
  24. $InputError = 1;
  25. prnMsg( _('The payment method cannot contain illegal characters'),'error');
  26. $Errors[$i] = 'MethodName';
  27. $i++;
  28. }
  29. if ( trim($_POST['MethodName']) == "") {
  30. $InputError = 1;
  31. prnMsg( _('The payment method may not be empty.'),'error');
  32. $Errors[$i] = 'MethodName';
  33. $i++;
  34. }
  35. if (isset($_POST['SelectedPaymentID']) AND $InputError !=1) {
  36. /*SelectedPaymentID could also exist if submit had not been clicked this code would not run in this case cos submit is false of course see the delete code below*/
  37. // Check the name does not clash
  38. $sql = "SELECT count(*) FROM paymentmethods
  39. WHERE paymentid <> '" . $SelectedPaymentID ."'
  40. AND paymentname ".LIKE." '" . $_POST['MethodName'] . "'";
  41. $result = DB_query($sql,$db);
  42. $myrow = DB_fetch_row($result);
  43. if ( $myrow[0] > 0 ) {
  44. $InputError = 1;
  45. prnMsg( _('The payment method can not be renamed because another with the same name already exists.'),'error');
  46. } else {
  47. // Get the old name and check that the record still exists need to be very careful here
  48. $sql = "SELECT paymentname FROM paymentmethods
  49. WHERE paymentid = '" . $SelectedPaymentID . "'";
  50. $result = DB_query($sql,$db);
  51. if ( DB_num_rows($result) != 0 ) {
  52. $myrow = DB_fetch_row($result);
  53. $OldName = $myrow[0];
  54. $sql = "UPDATE paymentmethods
  55. SET paymentname='" . $_POST['MethodName'] . "',
  56. paymenttype = '" . $_POST['ForPayment'] . "',
  57. receipttype = '" . $_POST['ForReceipt'] . "',
  58. usepreprintedstationery = '" . $_POST['UsePrePrintedStationery']. "'
  59. WHERE paymentname " . LIKE . " '".$OldName."'";
  60. } else {
  61. $InputError = 1;
  62. prnMsg( _('The payment method no longer exists.'),'error');
  63. }
  64. }
  65. $msg = _('Record Updated');
  66. $ErrMsg = _('Could not update payment method');
  67. } elseif ($InputError !=1) {
  68. /*SelectedPaymentID is null cos no item selected on first time round so must be adding a record*/
  69. $sql = "SELECT count(*) FROM paymentmethods
  70. WHERE paymentname LIKE'".$_POST['MethodName'] ."'";
  71. $result = DB_query($sql,$db);
  72. $myrow = DB_fetch_row($result);
  73. if ( $myrow[0] > 0 ) {
  74. $InputError = 1;
  75. prnMsg( _('The payment method can not be created because another with the same name already exists.'),'error');
  76. } else {
  77. $sql = "INSERT INTO paymentmethods ( paymentname,
  78. paymenttype,
  79. receipttype,
  80. usepreprintedstationery)
  81. VALUES ('" . $_POST['MethodName'] ."',
  82. '" . $_POST['ForPayment'] ."',
  83. '" . $_POST['ForReceipt'] ."',
  84. '" . $_POST['UsePrePrintedStationery'] ."')";
  85. }
  86. $msg = _('Record inserted');
  87. $ErrMsg = _('Could not insert payment method');
  88. }
  89. if ($InputError!=1){
  90. $result = DB_query($sql,$db, $ErrMsg);
  91. prnMsg($msg,'success');
  92. echo '<br />';
  93. }
  94. unset ($SelectedPaymentID);
  95. unset ($_POST['SelectedPaymentID']);
  96. unset ($_POST['MethodName']);
  97. unset ($_POST['ForPayment']);
  98. unset ($_POST['ForReceipt']);
  99. unset ($_POST['UsePrePrintedStationery']);
  100. } elseif (isset($_GET['delete'])) {
  101. //the link to delete a selected record was clicked instead of the submit button
  102. // PREVENT DELETES IF DEPENDENT RECORDS IN 'stockmaster'
  103. // Get the original name of the payment method the ID is just a secure way to find the payment method
  104. $sql = "SELECT paymentname FROM paymentmethods
  105. WHERE paymentid = '" . $SelectedPaymentID . "'";
  106. $result = DB_query($sql,$db);
  107. if ( DB_num_rows($result) == 0 ) {
  108. // This is probably the safest way there is
  109. prnMsg( _('Cannot delete this payment method because it no longer exist'),'warn');
  110. } else {
  111. $myrow = DB_fetch_row($result);
  112. $OldMeasureName = $myrow[0];
  113. $sql= "SELECT COUNT(*) FROM banktrans
  114. WHERE banktranstype LIKE '" . $OldMeasureName . "'";
  115. $result = DB_query($sql,$db);
  116. $myrow = DB_fetch_row($result);
  117. if ($myrow[0]>0) {
  118. prnMsg( _('Cannot delete this payment method because bank transactions have been created using this payment method'),'warn');
  119. echo '<br />' . _('There are') . ' ' . $myrow[0] . ' ' . _('bank transactions that refer to this payment method') . '</font>';
  120. } else {
  121. $sql="DELETE FROM paymentmethods WHERE paymentname " . LIKE . " '" . $OldMeasureName . "'";
  122. $result = DB_query($sql,$db);
  123. prnMsg( $OldMeasureName . ' ' . _('payment method has been deleted') . '!','success');
  124. echo '<br />';
  125. } //end if not used
  126. } //end if payment method exist
  127. unset ($SelectedPaymentID);
  128. unset ($_GET['SelectedPaymentID']);
  129. unset($_GET['delete']);
  130. unset ($_POST['SelectedPaymentID']);
  131. unset ($_POST['MethodID']);
  132. unset ($_POST['MethodName']);
  133. unset ($_POST['ForPayment']);
  134. unset ($_POST['ForReceipt']);
  135. }
  136. if (!isset($SelectedPaymentID)) {
  137. /* A payment method could be posted when one has been edited and is being updated
  138. or GOT when selected for modification
  139. SelectedPaymentID will exist because it was sent with the page in a GET .
  140. If its the first time the page has been displayed with no parameters
  141. then none of the above are true and the list of payment methods will be displayed with
  142. links to delete or edit each. These will call the same page again and allow update/input
  143. or deletion of the records*/
  144. $sql = "SELECT paymentid,
  145. paymentname,
  146. paymenttype,
  147. receipttype,
  148. usepreprintedstationery
  149. FROM paymentmethods
  150. ORDER BY paymentid";
  151. $ErrMsg = _('Could not get payment methods because');
  152. $result = DB_query($sql,$db,$ErrMsg);
  153. echo '<table class="selection">
  154. <tr>
  155. <th>' . _('Payment Method') . '</th>
  156. <th>' . _('For Payments') . '</th>
  157. <th>' . _('For Receipts') . '</th>
  158. <th>' . _('Use Pre-printed') .'<br />' . _('Stationery') . '</th>
  159. </tr>';
  160. $k=0; //row colour counter
  161. while ($myrow = DB_fetch_array($result)) {
  162. if ($k==1){
  163. echo '<tr class="EvenTableRows">';
  164. $k=0;
  165. } else {
  166. echo '<tr class="OddTableRows">';
  167. $k++;
  168. }
  169. echo '<td>' . $myrow['paymentname'] . '</td>
  170. <td>' . ($myrow['paymenttype'] ? _('Yes') : _('No')) . '</td>
  171. <td>' . ($myrow['receipttype'] ? _('Yes') : _('No')) . '</td>
  172. <td>' . ($myrow['usepreprintedstationery'] ? _('Yes') : _('No')) . '</td>
  173. <td><a href="' . htmlspecialchars($_SERVER['PHP_SELF']) . '?SelectedPaymentID=' . $myrow['paymentid'] . '">' . _('Edit') . '</a></td>
  174. <td><a href="' . htmlspecialchars($_SERVER['PHP_SELF']) . '?SelectedPaymentID=' . $myrow['paymentid'] . '&delete=1" onclick="return confirm(\'' . _('Are you sure you wish to delete this payment method?') . '\');">' . _('Delete') .'</a></td>
  175. </tr>';
  176. } //END WHILE LIST LOOP
  177. echo '</table><p>';
  178. } //end of ifs and buts!
  179. if (isset($SelectedPaymentID)) {
  180. echo '<div class="centre"><a href=' . htmlspecialchars($_SERVER['PHP_SELF']) . '?' . SID .'>' . _('Review Payment Methods') . '</a></div>';
  181. }
  182. echo '<p>';
  183. if (! isset($_GET['delete'])) {
  184. echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '">';
  185. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
  186. if (isset($SelectedPaymentID)) {
  187. //editing an existing section
  188. $sql = "SELECT paymentid,
  189. paymentname,
  190. paymenttype,
  191. receipttype,
  192. usepreprintedstationery
  193. FROM paymentmethods
  194. WHERE paymentid='" . $SelectedPaymentID . "'";
  195. $result = DB_query($sql, $db);
  196. if ( DB_num_rows($result) == 0 ) {
  197. prnMsg( _('Could not retrieve the requested payment method, please try again.'),'warn');
  198. unset($SelectedPaymentID);
  199. } else {
  200. $myrow = DB_fetch_array($result);
  201. $_POST['MethodID'] = $myrow['paymentid'];
  202. $_POST['MethodName'] = $myrow['paymentname'];
  203. $_POST['ForPayment'] = $myrow['paymenttype'];
  204. $_POST['ForReceipt'] = $myrow['receipttype'];
  205. $_POST['UsePrePrintedStationery'] = $myrow['usepreprintedstationery'];
  206. echo '<input type="hidden" name="SelectedPaymentID" value="' . $_POST['MethodID'] . '">';
  207. echo '<table class="selection">';
  208. }
  209. } else {
  210. $_POST['MethodName']='';
  211. $_POST['ForPayment'] = 1; // Default is use for payment
  212. $_POST['ForReceipt'] = 1; // Default is use for receipts
  213. $_POST['UsePrePrintedStationery'] = 0; // Default is use for receipts
  214. echo '<table class=selection>';
  215. }
  216. echo '<tr>
  217. <td>' . _('Payment Method') . ':' . '</td>
  218. <td><input type="Text" '. (in_array('MethodName',$Errors) ? 'class="inputerror"' : '' ) .' name="MethodName" size="30" maxlength="30" value="' . $_POST['MethodName'] . '"></td>
  219. </tr>';
  220. echo '<tr>
  221. <td>' . _('Use For Payments') . ':' . '</td>
  222. <td><select name="ForPayment">
  223. <option' . ($_POST['ForPayment'] ? ' selected' : '') .' value="1">' . _('Yes') . '</option>
  224. <option' . ($_POST['ForPayment'] ? '' : ' selected') .' value="0">' . _('No') . '</select></td>
  225. </tr>';
  226. echo '<tr>
  227. <td>' . _('Use For Receipts') . ':' . '</td>
  228. <td><select name="ForReceipt">
  229. <option' . ($_POST['ForReceipt'] ? ' selected' : '') .' value="1">' . _('Yes') . '</option>
  230. <option' . ($_POST['ForReceipt'] ? '' : ' selected') .' value="0">' . _('No') . '</option>
  231. </select></td>
  232. </tr>';
  233. echo '<tr>
  234. <td>' . _('Use Pre-printed Stationery') . ':' . '</td>
  235. <td><select name="UsePrePrintedStationery">
  236. <option' . ($_POST['UsePrePrintedStationery'] ? ' selected': '' ) .' value="1">' . _('Yes') . '</option>
  237. <option' . ($_POST['UsePrePrintedStationery']==1 ? '' : ' selected' ) .' value="0">' . _('No') . '</option>
  238. </select></td>
  239. </tr>';
  240. echo '</table>';
  241. echo '<br /><div class="centre"><input type="submit" name="submit" value=' . _('Enter Information') . '></div>';
  242. echo '</form>';
  243. } //end if record deleted no point displaying form to add record
  244. include('includes/footer.inc');
  245. ?>