paiement_charge.php 11 KB

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