payment.php 8.6 KB

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