paiement_charge.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2016-2018 Frédéric France <frederic.france@netlogic.fr>
  4. * Copyright (C) 2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/paiement_charge.php
  21. * \ingroup tax
  22. * \brief Page to add payment of a tax
  23. */
  24. // Load Dolibarr environment
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/paymentsocialcontribution.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  29. // Load translation files required by the page
  30. $langs->load("bills");
  31. $chid = GETPOST("id", 'int');
  32. $action = GETPOST('action', 'aZ09');
  33. $cancel = GETPOST('cancel');
  34. $amounts = array();
  35. // Security check
  36. $socid = 0;
  37. if ($user->socid > 0) {
  38. $socid = $user->socid;
  39. }
  40. $charge = new ChargeSociales($db);
  41. /*
  42. * Actions
  43. */
  44. if ($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) {
  45. $error = 0;
  46. if ($cancel) {
  47. $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
  48. header("Location: ".$loc);
  49. exit;
  50. }
  51. $datepaye = dol_mktime(12, 0, 0, GETPOST("remonth", "int"), GETPOST("reday", "int"), GETPOST("reyear", "int"));
  52. if (!(GETPOST("paiementtype") > 0)) {
  53. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
  54. $error++;
  55. $action = 'create';
  56. }
  57. if ($datepaye == '') {
  58. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
  59. $error++;
  60. $action = 'create';
  61. }
  62. if (isModEnabled("banque") && !(GETPOST("accountid") > 0)) {
  63. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
  64. $error++;
  65. $action = 'create';
  66. }
  67. if (!$error) {
  68. $paymentid = 0;
  69. // Read possible payments
  70. foreach ($_POST as $key => $value) {
  71. if (substr($key, 0, 7) == 'amount_') {
  72. $other_chid = substr($key, 7);
  73. $amounts[$other_chid] = price2num(GETPOST($key));
  74. }
  75. }
  76. if (count($amounts) <= 0) {
  77. $error++;
  78. setEventMessages($langs->trans("ErrorNoPaymentDefined"), null, 'errors');
  79. $action = 'create';
  80. }
  81. if (!$error) {
  82. $db->begin();
  83. // Create a line of payments
  84. $paiement = new PaymentSocialContribution($db);
  85. $paiement->chid = $chid;
  86. $paiement->datepaye = $datepaye;
  87. $paiement->amounts = $amounts; // Amount list
  88. $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
  89. $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
  90. $paiement->note = GETPOST("note", 'restricthtml');
  91. $paiement->note_private = GETPOST("note", 'restricthtml');
  92. if (!$error) {
  93. $paymentid = $paiement->create($user, (GETPOST('closepaidcontrib') == 'on' ? 1 : 0));
  94. if ($paymentid < 0) {
  95. $error++;
  96. setEventMessages($paiement->error, null, 'errors');
  97. $action = 'create';
  98. }
  99. }
  100. if (!$error) {
  101. $result = $paiement->addPaymentToBank($user, 'payment_sc', '(SocialContributionPayment)', GETPOST('accountid', 'int'), '', '');
  102. if (!($result > 0)) {
  103. $error++;
  104. setEventMessages($paiement->error, null, 'errors');
  105. $action = 'create';
  106. }
  107. }
  108. if (!$error) {
  109. $db->commit();
  110. $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
  111. header('Location: '.$loc);
  112. exit;
  113. } else {
  114. $db->rollback();
  115. }
  116. }
  117. }
  118. }
  119. /*
  120. * View
  121. */
  122. llxHeader();
  123. $form = new Form($db);
  124. // Form of charge payment creation
  125. if ($action == 'create') {
  126. $charge->fetch($chid);
  127. $charge->accountid = $charge->fk_account ? $charge->fk_account : $charge->accountid;
  128. $charge->paiementtype = $charge->mode_reglement_id ? $charge->mode_reglement_id : $charge->paiementtype;
  129. $total = $charge->amount;
  130. if (!empty($conf->use_javascript_ajax)) {
  131. print "\n".'<script type="text/javascript">';
  132. //Add js for AutoFill
  133. print ' $(document).ready(function () {';
  134. print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
  135. var amount = $(this).data("value");
  136. document.getElementById($(this).data(\'rowid\')).value = amount ;
  137. });';
  138. print ' });'."\n";
  139. print ' </script>'."\n";
  140. }
  141. print load_fiche_titre($langs->trans("DoPayment"));
  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="id" 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">';
  149. print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td><a href="'.DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid.'">'.$chid.'</a></td></tr>';
  150. print '<tr><td>'.$langs->trans("Label").'</td><td>'.$charge->label."</td></tr>\n";
  151. print '<tr><td>'.$langs->trans("Type")."</td><td>".$charge->type_label."</td></tr>\n";
  152. print '<tr><td>'.$langs->trans("Period")."</td><td>".dol_print_date($charge->periode, 'day')."</td></tr>\n";
  153. /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($charge->date_ech,'day')."</td></tr>\n";
  154. print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($charge->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  155. $sql = "SELECT sum(p.amount) as total";
  156. $sql .= " FROM ".MAIN_DB_PREFIX."paiementcharge as p";
  157. $sql .= " WHERE p.fk_charge = ".((int) $chid);
  158. $resql = $db->query($sql);
  159. if ($resql) {
  160. $obj = $db->fetch_object($resql);
  161. $sumpaid = $obj->total;
  162. $db->free($resql);
  163. }
  164. /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
  165. print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  166. print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
  167. $datepaye = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
  168. $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOSTISSET("remonth") ? $datepaye : -1) : '';
  169. print $form->selectDate($datepayment, '', '', '', 0, "add_payment", 1, 1, 0, '', '', $charge->date_ech, '', 1, $langs->trans("DateOfSocialContribution"));
  170. print "</td>";
  171. print '</tr>';
  172. print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
  173. $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $charge->paiementtype, "paiementtype");
  174. print "</td>\n";
  175. print '</tr>';
  176. print '<tr>';
  177. print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
  178. print '<td>';
  179. print img_picto('', 'bank_account', 'class="pictofixedwidth"');
  180. $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", 'int') : $charge->accountid, "accountid", 0, '', 2); // Show opend 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><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
  187. print '<tr>';
  188. print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
  189. print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'">'.GETPOST('note', 'alphanohtml').'</textarea></td>';
  190. print '</tr>';
  191. print '</table>';
  192. print dol_get_fiche_end();
  193. /*
  194. * Other unpaid charges
  195. */
  196. $num = 1;
  197. $i = 0;
  198. print '<table class="noborder centpercent">';
  199. print '<tr class="liste_titre">';
  200. //print '<td>'.$langs->trans("SocialContribution").'</td>';
  201. print '<td class="left">'.$langs->trans("DateDue").'</td>';
  202. print '<td class="right">'.$langs->trans("Amount").'</td>';
  203. print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
  204. print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
  205. print '<td class="center">'.$langs->trans("Amount").'</td>';
  206. print "</tr>\n";
  207. $total = 0;
  208. $total_ttc = 0;
  209. $totalrecu = 0;
  210. while ($i < $num) {
  211. $objp = $charge;
  212. print '<tr class="oddeven">';
  213. if ($objp->date_ech > 0) {
  214. print '<td class="left">'.dol_print_date($objp->date_ech, 'day').'</td>'."\n";
  215. } else {
  216. print "<td align=\"center\"><b>!!!</b></td>\n";
  217. }
  218. print '<td class="right"><span class="amount">'.price($objp->amount)."</span></td>";
  219. print '<td class="right"><span class="amount">'.price($sumpaid)."</span></td>";
  220. print '<td class="right"><span class="amount">'.price($objp->amount - $sumpaid)."</span></td>";
  221. print '<td class="center">';
  222. if ($sumpaid < $objp->amount) {
  223. $namef = "amount_".$objp->id;
  224. $nameRemain = "remain_".$objp->id;
  225. if (!empty($conf->use_javascript_ajax)) {
  226. print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
  227. }
  228. $remaintopay = $objp->amount - $sumpaid;
  229. print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">';
  230. print '<input type="text" size="8" name="'.$namef.'" id="'.$namef.'" value="'.GETPOST('amount_'.$objp->id, 'alpha').'">';
  231. } else {
  232. print '-';
  233. }
  234. print "</td>";
  235. print "</tr>\n";
  236. $total += $objp->total;
  237. $total_ttc += $objp->total_ttc;
  238. $totalrecu += $objp->amount;
  239. $i++;
  240. }
  241. if ($i > 1) {
  242. // Print total
  243. print '<tr class="oddeven">';
  244. print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
  245. print '<td class="right"><b>'.price($total_ttc).'</b></td>';
  246. print '<td class="right"><b>'.price($totalrecu).'</b></td>';
  247. print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
  248. print '<td align="center">&nbsp;</td>';
  249. print "</tr>\n";
  250. }
  251. print "</table>";
  252. // Save payment button
  253. print '<br><div class="center"><input type="checkbox" checked name="closepaidcontrib"> '.$langs->trans("ClosePaidContributionsAutomatically");
  254. print '<br><input type="submit" class="button" name="save" value="'.$langs->trans('ToMakePayment').'">';
  255. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  256. print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
  257. print '</div>';
  258. print "</form>\n";
  259. }
  260. llxFooter();
  261. $db->close();