payment.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. // Load Dolibarr environment
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/don/class/paymentdonation.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  28. $langs->load("bills");
  29. $chid = GETPOST("rowid", 'int');
  30. $action = GETPOST('action', 'aZ09');
  31. $amounts = array();
  32. $cancel = GETPOST('cancel');
  33. // Security check
  34. $socid = 0;
  35. if ($user->socid > 0) {
  36. $socid = $user->socid;
  37. }
  38. $object = new Don($db);
  39. /*
  40. * Actions
  41. */
  42. if ($action == 'add_payment') {
  43. $error = 0;
  44. if ($cancel) {
  45. $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
  46. header("Location: ".$loc);
  47. exit;
  48. }
  49. $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth"), GETPOST("reday"), GETPOST("reyear"));
  50. if (!(GETPOST("paymenttype") > 0)) {
  51. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
  52. $error++;
  53. }
  54. if ($datepaid == '') {
  55. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
  56. $error++;
  57. }
  58. if (isModEnabled("banque") && !(GETPOST("accountid", 'int') > 0)) {
  59. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
  60. $error++;
  61. }
  62. if (!$error) {
  63. $paymentid = 0;
  64. // Read possible payments
  65. foreach ($_POST as $key => $value) {
  66. if (substr($key, 0, 7) == 'amount_') {
  67. $other_chid = substr($key, 7);
  68. $amounts[$other_chid] = price2num(GETPOST($key));
  69. }
  70. }
  71. if (count($amounts) <= 0) {
  72. $error++;
  73. $errmsg = 'ErrorNoPaymentDefined';
  74. setEventMessages($errmsg, null, 'errors');
  75. }
  76. if (!$error) {
  77. $db->begin();
  78. // Create a line of payments
  79. $payment = new PaymentDonation($db);
  80. $payment->chid = $chid;
  81. $payment->datep = $datepaid;
  82. $payment->amounts = $amounts; // Tableau de montant
  83. $payment->paymenttype = GETPOST("paymenttype", 'int');
  84. $payment->num_payment = GETPOST("num_payment", 'alphanohtml');
  85. $payment->note_public = GETPOST("note_public", 'restricthtml');
  86. if (!$error) {
  87. $paymentid = $payment->create($user);
  88. if ($paymentid < 0) {
  89. $errmsg = $payment->error;
  90. setEventMessages($errmsg, null, 'errors');
  91. $error++;
  92. }
  93. }
  94. if (!$error) {
  95. $result = $payment->addPaymentToBank($user, 'payment_donation', '(DonationPayment)', GETPOST('accountid', 'int'), '', '');
  96. if (!($result > 0)) {
  97. $errmsg = $payment->error;
  98. setEventMessages($errmsg, null, 'errors');
  99. $error++;
  100. }
  101. }
  102. if (!$error) {
  103. $db->commit();
  104. $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
  105. header('Location: '.$loc);
  106. exit;
  107. } else {
  108. $db->rollback();
  109. }
  110. }
  111. }
  112. $action = 'create';
  113. }
  114. /*
  115. * View
  116. */
  117. $form = new Form($db);
  118. llxHeader();
  119. $sql = "SELECT sum(p.amount) as total";
  120. $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as p";
  121. $sql .= " WHERE p.fk_donation = ".((int) $chid);
  122. $resql = $db->query($sql);
  123. if ($resql) {
  124. $obj = $db->fetch_object($resql);
  125. $sumpaid = $obj->total;
  126. $db->free($resql);
  127. }
  128. // Form to create donation payment
  129. if ($action == 'create') {
  130. $object->fetch($chid);
  131. $total = $object->amount;
  132. print load_fiche_titre($langs->trans("DoPayment"));
  133. if (!empty($conf->use_javascript_ajax)) {
  134. print "\n".'<script type="text/javascript">';
  135. //Add js for AutoFill
  136. print ' $(document).ready(function () {';
  137. print ' $(".AutoFillAmout").on(\'click touchstart\', function(){
  138. $("input[name="+$(this).data(\'rowname\')+"]").val($(this).data("value")).trigger("change");
  139. });';
  140. print ' });'."\n";
  141. print ' </script>'."\n";
  142. }
  143. print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
  144. print '<input type="hidden" name="token" value="'.newToken().'">';
  145. print '<input type="hidden" name="rowid" value="'.$chid.'">';
  146. print '<input type="hidden" name="chid" value="'.$chid.'">';
  147. print '<input type="hidden" name="action" value="add_payment">';
  148. print dol_get_fiche_head();
  149. print '<table class="border centpercent tableforfieldcreate">';
  150. print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
  151. $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth"), GETPOST("reday"), GETPOST("reyear"));
  152. $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOST("remonth") ? $datepaid : -1) : 0;
  153. print $form->selectDate($datepayment, '', 0, 0, 0, "add_payment", 1, 1, 0, '', '', $object->date, '', 1, $langs->trans("DonationDate"));
  154. print "</td>";
  155. print '</tr>';
  156. print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
  157. $form->select_types_paiements(GETPOSTISSET("paymenttype") ? GETPOST("paymenttype") : $object->fk_typepayment, "paymenttype");
  158. print "</td>\n";
  159. print '</tr>';
  160. print '<tr>';
  161. print '<td class="fieldrequired">'.$langs->trans('AccountToCredit').'</td>';
  162. print '<td colspan="2">';
  163. $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid") : "0", "accountid", 0, '', 2); // Show open bank account list
  164. print '</td></tr>';
  165. // Number
  166. print '<tr><td>'.$langs->trans('Numero');
  167. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  168. print '</td>';
  169. print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
  170. print '<tr>';
  171. print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
  172. print '<td class="tdtop" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
  173. print '</tr>';
  174. print '</table>';
  175. print dol_get_fiche_end();
  176. /*
  177. * List of payments on donation
  178. */
  179. $num = 1;
  180. $i = 0;
  181. print '<table class="noborder centpercent">';
  182. print '<tr class="liste_titre">';
  183. print '<td>'.$langs->trans("Donation").'</td>';
  184. print '<td class="right">'.$langs->trans("Amount").'</td>';
  185. print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
  186. print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
  187. print '<td class="center">'.$langs->trans("Amount").'</td>';
  188. print "</tr>\n";
  189. $total = 0;
  190. $totalrecu = 0;
  191. while ($i < $num) {
  192. $objp = $object;
  193. print '<tr class="oddeven">';
  194. print '<td>'.$object->getNomUrl(1)."</td>";
  195. print '<td class="right">'.price($objp->amount)."</td>";
  196. print '<td class="right">'.price($sumpaid)."</td>";
  197. print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
  198. print '<td class="center">';
  199. if ($sumpaid < $objp->amount) {
  200. $namef = "amount_".$objp->id;
  201. if (!empty($conf->use_javascript_ajax)) {
  202. print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmout' data-rowname='".$namef."' data-value='".price($objp->amount - $sumpaid)."'");
  203. }
  204. print '<input type="text" size="8" name="'.$namef.'">';
  205. } else {
  206. print '-';
  207. }
  208. print "</td>";
  209. print "</tr>\n";
  210. /*$total+=$objp->total;
  211. $total_ttc+=$objp->total_ttc;
  212. $totalrecu+=$objp->am;*/ //Useless code ?
  213. $i++;
  214. }
  215. /*if ($i > 1)
  216. {
  217. // Print total
  218. print '<tr class="oddeven">';
  219. print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
  220. print "<td class=\"right\"><b>".price($total_ttc)."</b></td>";
  221. print "<td class=\"right\"><b>".price($totalrecu)."</b></td>";
  222. print "<td class=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
  223. print '<td class="center">&nbsp;</td>';
  224. print "</tr>\n";
  225. }*/ //Useless code ?
  226. print "</table>";
  227. print $form->buttonsSaveCancel();
  228. print "</form>\n";
  229. }
  230. llxFooter();
  231. $db->close();