payment.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /* Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
  3. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/don/payment/payment.php
  20. * \ingroup donations
  21. * \brief Page to add payment of a donation
  22. */
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/don/class/paymentdonation.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  27. $langs->load("bills");
  28. $chid = GETPOST("rowid", 'int');
  29. $action = GETPOST('action', 'aZ09');
  30. $amounts = array();
  31. $cancel = GETPOST('cancel');
  32. // Security check
  33. $socid = 0;
  34. if ($user->socid > 0) {
  35. $socid = $user->socid;
  36. }
  37. $object = new Don($db);
  38. /*
  39. * Actions
  40. */
  41. if ($action == 'add_payment') {
  42. $error = 0;
  43. if ($cancel) {
  44. $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
  45. header("Location: ".$loc);
  46. exit;
  47. }
  48. $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth"), GETPOST("reday"), GETPOST("reyear"));
  49. if (!(GETPOST("paymenttype") > 0)) {
  50. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
  51. $error++;
  52. }
  53. if ($datepaid == '') {
  54. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
  55. $error++;
  56. }
  57. if (!empty($conf->banque->enabled) && !(GETPOST("accountid", 'int') > 0)) {
  58. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
  59. $error++;
  60. }
  61. if (!$error) {
  62. $paymentid = 0;
  63. // Read possible payments
  64. foreach ($_POST as $key => $value) {
  65. if (substr($key, 0, 7) == 'amount_') {
  66. $other_chid = substr($key, 7);
  67. $amounts[$other_chid] = price2num(GETPOST($key));
  68. }
  69. }
  70. if (count($amounts) <= 0) {
  71. $error++;
  72. $errmsg = 'ErrorNoPaymentDefined';
  73. setEventMessages($errmsg, null, 'errors');
  74. }
  75. if (!$error) {
  76. $db->begin();
  77. // Create a line of payments
  78. $payment = new PaymentDonation($db);
  79. $payment->chid = $chid;
  80. $payment->datepaid = $datepaid;
  81. $payment->amounts = $amounts; // Tableau de montant
  82. $payment->paymenttype = GETPOST("paymenttype", 'int');
  83. $payment->num_payment = GETPOST("num_payment", 'alphanohtml');
  84. $payment->note_public = GETPOST("note_public", 'restricthtml');
  85. if (!$error) {
  86. $paymentid = $payment->create($user);
  87. if ($paymentid < 0) {
  88. $errmsg = $payment->error;
  89. setEventMessages($errmsg, null, 'errors');
  90. $error++;
  91. }
  92. }
  93. if (!$error) {
  94. $result = $payment->addPaymentToBank($user, 'payment_donation', '(DonationPayment)', GETPOST('accountid', 'int'), '', '');
  95. if (!$result > 0) {
  96. $errmsg = $payment->error;
  97. setEventMessages($errmsg, null, 'errors');
  98. $error++;
  99. }
  100. }
  101. if (!$error) {
  102. $db->commit();
  103. $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
  104. header('Location: '.$loc);
  105. exit;
  106. } else {
  107. $db->rollback();
  108. }
  109. }
  110. }
  111. $action = 'create';
  112. }
  113. /*
  114. * View
  115. */
  116. $form = new Form($db);
  117. llxHeader();
  118. $sql = "SELECT sum(p.amount) as total";
  119. $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as p";
  120. $sql .= " WHERE p.fk_donation = ".((int) $chid);
  121. $resql = $db->query($sql);
  122. if ($resql) {
  123. $obj = $db->fetch_object($resql);
  124. $sumpaid = $obj->total;
  125. $db->free($resql);
  126. }
  127. // Form to create donation payment
  128. if ($action == 'create') {
  129. $object->fetch($chid);
  130. $total = $object->amount;
  131. print load_fiche_titre($langs->trans("DoPayment"));
  132. if (!empty($conf->use_javascript_ajax)) {
  133. print "\n".'<script type="text/javascript">';
  134. //Add js for AutoFill
  135. print ' $(document).ready(function () {';
  136. print ' $(".AutoFillAmout").on(\'click touchstart\', function(){
  137. $("input[name="+$(this).data(\'rowname\')+"]").val($(this).data("value")).trigger("change");
  138. });';
  139. print ' });'."\n";
  140. print ' </script>'."\n";
  141. }
  142. print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
  143. print '<input type="hidden" name="token" value="'.newToken().'">';
  144. print '<input type="hidden" name="rowid" value="'.$chid.'">';
  145. print '<input type="hidden" name="chid" value="'.$chid.'">';
  146. print '<input type="hidden" name="action" value="add_payment">';
  147. print dol_get_fiche_head();
  148. print '<table class="border centpercent tableforfieldcreate">';
  149. print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
  150. $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth"), GETPOST("reday"), GETPOST("reyear"));
  151. $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOST("remonth") ? $datepaid : -1) : 0;
  152. print $form->selectDate($datepayment, '', 0, 0, 0, "add_payment", 1, 1, 0, '', '', $object->date, '', 1, $langs->trans("DonationDate"));
  153. print "</td>";
  154. print '</tr>';
  155. print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
  156. $form->select_types_paiements(GETPOSTISSET("paymenttype") ? GETPOST("paymenttype") : $object->paymenttype, "paymenttype");
  157. print "</td>\n";
  158. print '</tr>';
  159. print '<tr>';
  160. print '<td class="fieldrequired">'.$langs->trans('AccountToCredit').'</td>';
  161. print '<td colspan="2">';
  162. $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid") : $object->accountid, "accountid", 0, '', 2); // Show open bank account list
  163. print '</td></tr>';
  164. // Number
  165. print '<tr><td>'.$langs->trans('Numero');
  166. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  167. print '</td>';
  168. print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
  169. print '<tr>';
  170. print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
  171. print '<td class="tdtop" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
  172. print '</tr>';
  173. print '</table>';
  174. print dol_get_fiche_end();
  175. /*
  176. * List of payments on donation
  177. */
  178. $num = 1;
  179. $i = 0;
  180. print '<table class="noborder centpercent">';
  181. print '<tr class="liste_titre">';
  182. print '<td>'.$langs->trans("Donation").'</td>';
  183. print '<td class="right">'.$langs->trans("Amount").'</td>';
  184. print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
  185. print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
  186. print '<td class="center">'.$langs->trans("Amount").'</td>';
  187. print "</tr>\n";
  188. $total = 0;
  189. $totalrecu = 0;
  190. while ($i < $num) {
  191. $objp = $object;
  192. print '<tr class="oddeven">';
  193. print '<td>'.$object->getNomUrl(1)."</td>";
  194. print '<td class="right">'.price($objp->amount)."</td>";
  195. print '<td class="right">'.price($sumpaid)."</td>";
  196. print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
  197. print '<td class="center">';
  198. if ($sumpaid < $objp->amount) {
  199. $namef = "amount_".$objp->id;
  200. if (!empty($conf->use_javascript_ajax)) {
  201. print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmout' data-rowname='".$namef."' data-value='".price($objp->amount - $sumpaid)."'");
  202. }
  203. print '<input type="text" size="8" name="'.$namef.'">';
  204. } else {
  205. print '-';
  206. }
  207. print "</td>";
  208. print "</tr>\n";
  209. /*$total+=$objp->total;
  210. $total_ttc+=$objp->total_ttc;
  211. $totalrecu+=$objp->am;*/ //Useless code ?
  212. $i++;
  213. }
  214. /*if ($i > 1)
  215. {
  216. // Print total
  217. print '<tr class="oddeven">';
  218. print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
  219. print "<td class=\"right\"><b>".price($total_ttc)."</b></td>";
  220. print "<td class=\"right\"><b>".price($totalrecu)."</b></td>";
  221. print "<td class=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
  222. print '<td class="center">&nbsp;</td>';
  223. print "</tr>\n";
  224. }*/ //Useless code ?
  225. print "</table>";
  226. print $form->buttonsSaveCancel();
  227. print "</form>\n";
  228. }
  229. llxFooter();
  230. $db->close();