paiement_charge.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/paymentsocialcontribution.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  28. // Load translation files required by the page
  29. $langs->load("bills");
  30. $chid = GETPOST("id", 'int');
  31. $action = GETPOST('action', 'aZ09');
  32. $cancel = GETPOST('cancel');
  33. $amounts = array();
  34. // Security check
  35. $socid = 0;
  36. if ($user->socid > 0) {
  37. $socid = $user->socid;
  38. }
  39. $charge = new ChargeSociales($db);
  40. /*
  41. * Actions
  42. */
  43. if ($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) {
  44. $error = 0;
  45. if ($cancel) {
  46. $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
  47. header("Location: ".$loc);
  48. exit;
  49. }
  50. $datepaye = dol_mktime(12, 0, 0, GETPOST("remonth", "int"), GETPOST("reday", "int"), GETPOST("reyear", "int"));
  51. if (!(GETPOST("paiementtype") > 0)) {
  52. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
  53. $error++;
  54. $action = 'create';
  55. }
  56. if ($datepaye == '') {
  57. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
  58. $error++;
  59. $action = 'create';
  60. }
  61. if (isModEnabled('banque') && !(GETPOST("accountid") > 0)) {
  62. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
  63. $error++;
  64. $action = 'create';
  65. }
  66. if (!$error) {
  67. $paymentid = 0;
  68. // Read possible payments
  69. foreach ($_POST as $key => $value) {
  70. if (substr($key, 0, 7) == 'amount_') {
  71. $other_chid = substr($key, 7);
  72. $amounts[$other_chid] = price2num(GETPOST($key));
  73. }
  74. }
  75. if (count($amounts) <= 0) {
  76. $error++;
  77. setEventMessages($langs->trans("ErrorNoPaymentDefined"), null, 'errors');
  78. $action = 'create';
  79. }
  80. if (!$error) {
  81. $db->begin();
  82. // Create a line of payments
  83. $paiement = new PaymentSocialContribution($db);
  84. $paiement->chid = $chid;
  85. $paiement->datepaye = $datepaye;
  86. $paiement->amounts = $amounts; // Amount list
  87. $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
  88. $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
  89. $paiement->note = GETPOST("note", 'restricthtml');
  90. $paiement->note_private = GETPOST("note", 'restricthtml');
  91. if (!$error) {
  92. $paymentid = $paiement->create($user, (GETPOST('closepaidcontrib') == 'on' ? 1 : 0));
  93. if ($paymentid < 0) {
  94. $error++;
  95. setEventMessages($paiement->error, null, 'errors');
  96. $action = 'create';
  97. }
  98. }
  99. if (!$error) {
  100. $result = $paiement->addPaymentToBank($user, 'payment_sc', '(SocialContributionPayment)', GETPOST('accountid', 'int'), '', '');
  101. if (!($result > 0)) {
  102. $error++;
  103. setEventMessages($paiement->error, null, 'errors');
  104. $action = 'create';
  105. }
  106. }
  107. if (!$error) {
  108. $db->commit();
  109. $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
  110. header('Location: '.$loc);
  111. exit;
  112. } else {
  113. $db->rollback();
  114. }
  115. }
  116. }
  117. }
  118. /*
  119. * View
  120. */
  121. llxHeader();
  122. $form = new Form($db);
  123. // Form of charge payment creation
  124. if ($action == 'create') {
  125. $charge->fetch($chid);
  126. $charge->accountid = $charge->fk_account ? $charge->fk_account : $charge->accountid;
  127. $charge->paiementtype = $charge->mode_reglement_id ? $charge->mode_reglement_id : $charge->paiementtype;
  128. $total = $charge->amount;
  129. if (!empty($conf->use_javascript_ajax)) {
  130. print "\n".'<script type="text/javascript">';
  131. //Add js for AutoFill
  132. print ' $(document).ready(function () {';
  133. print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
  134. var amount = $(this).data("value");
  135. document.getElementById($(this).data(\'rowid\')).value = amount ;
  136. });';
  137. print ' });'."\n";
  138. print ' </script>'."\n";
  139. }
  140. print load_fiche_titre($langs->trans("DoPayment"));
  141. print "<br>\n";
  142. if ($mesg) {
  143. print "<div class=\"error\">$mesg</div>";
  144. }
  145. print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
  146. print '<input type="hidden" name="token" value="'.newToken().'">';
  147. print '<input type="hidden" name="id" value="'.$chid.'">';
  148. print '<input type="hidden" name="chid" value="'.$chid.'">';
  149. print '<input type="hidden" name="action" value="add_payment">';
  150. print dol_get_fiche_head('', '');
  151. print '<table class="border centpercent">';
  152. 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>';
  153. print '<tr><td>'.$langs->trans("Label").'</td><td>'.$charge->label."</td></tr>\n";
  154. print '<tr><td>'.$langs->trans("Type")."</td><td>".$charge->type_label."</td></tr>\n";
  155. print '<tr><td>'.$langs->trans("Period")."</td><td>".dol_print_date($charge->periode, 'day')."</td></tr>\n";
  156. /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($charge->date_ech,'day')."</td></tr>\n";
  157. print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($charge->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  158. $sql = "SELECT sum(p.amount) as total";
  159. $sql .= " FROM ".MAIN_DB_PREFIX."paiementcharge as p";
  160. $sql .= " WHERE p.fk_charge = ".((int) $chid);
  161. $resql = $db->query($sql);
  162. if ($resql) {
  163. $obj = $db->fetch_object($resql);
  164. $sumpaid = $obj->total;
  165. $db->free($resql);
  166. }
  167. /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
  168. print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  169. print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
  170. $datepaye = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
  171. $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOSTISSET("remonth") ? $datepaye : -1) : '';
  172. print $form->selectDate($datepayment, '', '', '', 0, "add_payment", 1, 1, 0, '', '', $charge->date_ech, '', 1, $langs->trans("DateOfSocialContribution"));
  173. print "</td>";
  174. print '</tr>';
  175. print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
  176. $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $charge->paiementtype, "paiementtype");
  177. print "</td>\n";
  178. print '</tr>';
  179. print '<tr>';
  180. print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
  181. print '<td>';
  182. print img_picto('', 'bank_account', 'class="pictofixedwidth"');
  183. $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", 'int') : $charge->accountid, "accountid", 0, '', 2); // Show opend bank account list
  184. print '</td></tr>';
  185. // Number
  186. print '<tr><td>'.$langs->trans('Numero');
  187. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  188. print '</td>';
  189. print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
  190. print '<tr>';
  191. print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
  192. print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'">'.GETPOST('note', 'alphanohtml').'</textarea></td>';
  193. print '</tr>';
  194. print '</table>';
  195. print dol_get_fiche_end();
  196. /*
  197. * Other unpaid charges
  198. */
  199. $num = 1;
  200. $i = 0;
  201. print '<table class="noborder centpercent">';
  202. print '<tr class="liste_titre">';
  203. //print '<td>'.$langs->trans("SocialContribution").'</td>';
  204. print '<td class="left">'.$langs->trans("DateDue").'</td>';
  205. print '<td class="right">'.$langs->trans("Amount").'</td>';
  206. print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
  207. print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
  208. print '<td class="center">'.$langs->trans("Amount").'</td>';
  209. print "</tr>\n";
  210. $total = 0;
  211. $total_ttc = 0;
  212. $totalrecu = 0;
  213. while ($i < $num) {
  214. $objp = $charge;
  215. print '<tr class="oddeven">';
  216. if ($objp->date_ech > 0) {
  217. print '<td class="left">'.dol_print_date($objp->date_ech, 'day').'</td>'."\n";
  218. } else {
  219. print "<td align=\"center\"><b>!!!</b></td>\n";
  220. }
  221. print '<td class="right"><span class="amount">'.price($objp->amount)."</span></td>";
  222. print '<td class="right"><span class="amount">'.price($sumpaid)."</span></td>";
  223. print '<td class="right"><span class="amount">'.price($objp->amount - $sumpaid)."</span></td>";
  224. print '<td class="center">';
  225. if ($sumpaid < $objp->amount) {
  226. $namef = "amount_".$objp->id;
  227. $nameRemain = "remain_".$objp->id;
  228. if (!empty($conf->use_javascript_ajax)) {
  229. print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
  230. }
  231. $remaintopay = $objp->amount - $sumpaid;
  232. print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">';
  233. print '<input type="text" size="8" name="'.$namef.'" id="'.$namef.'" value="'.GETPOST('amount_'.$objp->id, 'alpha').'">';
  234. } else {
  235. print '-';
  236. }
  237. print "</td>";
  238. print "</tr>\n";
  239. $total += $objp->total;
  240. $total_ttc += $objp->total_ttc;
  241. $totalrecu += $objp->amount;
  242. $i++;
  243. }
  244. if ($i > 1) {
  245. // Print total
  246. print '<tr class="oddeven">';
  247. print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
  248. print '<td class="right"><b>'.price($total_ttc).'</b></td>';
  249. print '<td class="right"><b>'.price($totalrecu).'</b></td>';
  250. print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
  251. print '<td align="center">&nbsp;</td>';
  252. print "</tr>\n";
  253. }
  254. print "</table>";
  255. // Save payment button
  256. print '<br><div class="center"><input type="checkbox" checked name="closepaidcontrib"> '.$langs->trans("ClosePaidContributionsAutomatically");
  257. print '<br><input type="submit" class="button" name="save" value="'.$langs->trans('ToMakePayment').'">';
  258. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  259. print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
  260. print '</div>';
  261. print "</form>\n";
  262. }
  263. llxFooter();
  264. $db->close();