paiement_vat.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2016-2021 Frédéric France <frederic.france@netlogic.fr>
  4. * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.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/bank/class/account.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/paymentvat.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array("banks", "bills"));
  31. $chid = GETPOST("id", 'int');
  32. $action = GETPOST('action', 'alpha');
  33. $cancel = GETPOST('cancel');
  34. $amounts = array();
  35. // Security check
  36. $socid = 0;
  37. if ($user->socid > 0) {
  38. $socid = $user->socid;
  39. }
  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/tva/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", 'int') > 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", 'int') > 0)) {
  62. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
  63. $error++;
  64. $action = 'create';
  65. }
  66. // Read possible payments
  67. foreach ($_POST as $key => $value) {
  68. if (substr($key, 0, 7) == 'amount_') {
  69. $other_chid = substr($key, 7);
  70. $amounts[$other_chid] = price2num(GETPOST($key));
  71. }
  72. }
  73. if (empty($amounts[key($amounts)])) {
  74. $error++;
  75. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
  76. $action = 'create';
  77. }
  78. if (!$error) {
  79. $paymentid = 0;
  80. if (!$error) {
  81. $db->begin();
  82. // Create a line of payments
  83. $paiement = new PaymentVAT($db);
  84. $paiement->chid = $chid;
  85. $paiement->datepaye = $datepaye;
  86. $paiement->amounts = $amounts; // Tableau de montant
  87. $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
  88. $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
  89. $paiement->note = (string) GETPOST("note", 'restricthtml');
  90. $paiement->note_private = (string) GETPOST("note", 'restricthtml');
  91. if (!$error) {
  92. $paymentid = $paiement->create($user, (GETPOST('closepaidvat') == '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_vat', '(VATPayment)', 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/tva/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. // Formulaire de creation d'un paiement de charge
  124. if ($action == 'create') {
  125. $tva = new Tva($db);
  126. $tva->fetch($chid);
  127. $tva->accountid = $tva->fk_account ? $tva->fk_account : $tva->accountid;
  128. $tva->paiementtype = $tva->type_payment;
  129. $total = $tva->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 "<br>\n";
  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="id" 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">';
  150. print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td><a href="'.DOL_URL_ROOT.'/compta/tva/card.php?id='.$chid.'">'.$chid.'</a></td></tr>';
  151. //print '<tr><td>'.$langs->trans("Type")."</td><td>".$tva->type_label."</td></tr>\n";
  152. print '<tr><td>'.$langs->trans("Period")."</td><td>".dol_print_date($tva->datev, 'day')."</td></tr>\n";
  153. print '<tr><td>'.$langs->trans("Label").'</td><td>'.$tva->label."</td></tr>\n";
  154. /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($tva->date_ech,'day')."</td></tr>\n";
  155. print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($tva->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  156. $sql = "SELECT sum(p.amount) as total";
  157. $sql .= " FROM ".MAIN_DB_PREFIX."payment_vat as p";
  158. $sql .= " WHERE p.fk_tva = ".((int) $chid);
  159. $resql = $db->query($sql);
  160. if ($resql) {
  161. $obj = $db->fetch_object($resql);
  162. $sumpaid = $obj->total;
  163. $db->free($resql);
  164. }
  165. /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
  166. print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  167. print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
  168. $datepaye = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
  169. $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOST("remonth", 'int') ? $datepaye : -1) : 0;
  170. print $form->selectDate($datepayment, '', '', '', '', "add_payment", 1, 1);
  171. print "</td>";
  172. print '</tr>';
  173. print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
  174. print $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype", "int") : $tva->paiementtype, "paiementtype", '', 0, 1, 0, 0, 1, 'maxwidth500 widthcentpercentminusx', 1);
  175. print "</td>\n";
  176. print '</tr>';
  177. print '<tr>';
  178. print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
  179. print '<td>';
  180. print img_picto('', 'bank_account', 'pictofixedwidth');
  181. $form->select_comptes(GETPOST("accountid", "int") ? GETPOST("accountid", "int") : $tva->accountid, "accountid", 0, '', 1, '', 0, 'maxwidth500 widthcentpercentminusx'); // Show opend bank account list
  182. print '</td></tr>';
  183. // Number
  184. print '<tr><td>'.$langs->trans('Numero');
  185. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  186. print '</td>';
  187. print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
  188. print '<tr>';
  189. print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
  190. print '<td class="tdtop"><textarea name="note" class="quatrevingtpercent" wrap="soft" rows="'.ROWS_3.'"></textarea></td>';
  191. print '</tr>';
  192. print '</table>';
  193. print dol_get_fiche_end();
  194. /*
  195. * Autres charges impayees
  196. */
  197. $num = 1;
  198. $i = 0;
  199. print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  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 = $tva;
  213. print '<tr class="oddeven">';
  214. if ($objp->datev > 0) {
  215. print '<td class="left">'.dol_print_date($objp->datev, 'day').'</td>'."\n";
  216. } else {
  217. print '<td class="center"><b>!!!</b></td>'."\n";
  218. }
  219. print '<td class="right nowraponall"><span class="amount">'.price($objp->amount)."</span></td>";
  220. print '<td class="right nowraponall"><span class="amount">'.price($sumpaid)."</span></td>";
  221. print '<td class="right nowraponall"><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. /* Disabled, we autofil the amount with remain to pay by default
  227. if (!empty($conf->use_javascript_ajax)) {
  228. print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
  229. } */
  230. $remaintopay = $objp->amount - $sumpaid;
  231. print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">';
  232. print '<input type="text" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$remaintopay.'">';
  233. } else {
  234. print '-';
  235. }
  236. print "</td>";
  237. print "</tr>\n";
  238. $total += $objp->total;
  239. $total_ttc += $objp->total_ttc;
  240. $totalrecu += $objp->amount;
  241. $i++;
  242. }
  243. if ($i > 1) {
  244. // Print total
  245. print '<tr class="oddeven">';
  246. print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
  247. print '<td class="right"><b>'.price($total_ttc).'</b></td>';
  248. print '<td class="right"><b>'.price($totalrecu).'</b></td>';
  249. print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
  250. print '<td align="center">&nbsp;</td>';
  251. print "</tr>\n";
  252. }
  253. print "</table>";
  254. print '</div>';
  255. // Bouton Save payment
  256. print '<br><div class="center"><input type="checkbox" checked name="closepaidvat"> '.$langs->trans("ClosePaidVATAutomatically");
  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" name="cancel" value="'.$langs->trans("Cancel").'">';
  260. print '</div>';
  261. print "</form>\n";
  262. }
  263. llxFooter();
  264. $db->close();