paiement_vat.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/paymentvat.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array("banks", "bills"));
  30. $chid = GETPOST("id", 'int');
  31. $action = GETPOST('action', 'alpha');
  32. $cancel = GETPOST('cancel');
  33. $amounts = array();
  34. // Security check
  35. $socid = 0;
  36. if ($user->socid > 0) {
  37. $socid = $user->socid;
  38. }
  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/tva/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", 'int') > 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 (isModEnabled('banque') && !(GETPOST("accountid", 'int') > 0)) {
  61. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
  62. $error++;
  63. $action = 'create';
  64. }
  65. // Read possible payments
  66. foreach ($_POST as $key => $value) {
  67. if (substr($key, 0, 7) == 'amount_') {
  68. $other_chid = substr($key, 7);
  69. $amounts[$other_chid] = price2num(GETPOST($key));
  70. }
  71. }
  72. if (empty($amounts[key($amounts)])) {
  73. $error++;
  74. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
  75. $action = 'create';
  76. }
  77. if (!$error) {
  78. $paymentid = 0;
  79. if (!$error) {
  80. $db->begin();
  81. // Create a line of payments
  82. $paiement = new PaymentVAT($db);
  83. $paiement->chid = $chid;
  84. $paiement->datepaye = $datepaye;
  85. $paiement->amounts = $amounts; // Tableau de montant
  86. $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
  87. $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
  88. $paiement->note = (string) GETPOST("note", 'restricthtml');
  89. $paiement->note_private = (string) GETPOST("note", 'restricthtml');
  90. if (!$error) {
  91. $paymentid = $paiement->create($user, (GETPOST('closepaidvat') == '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_vat', '(VATPayment)', 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/tva/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. // Formulaire de creation d'un paiement de charge
  123. if ($action == 'create') {
  124. $tva = new Tva($db);
  125. $tva->fetch($chid);
  126. $tva->accountid = $tva->fk_account ? $tva->fk_account : $tva->accountid;
  127. $tva->paiementtype = $tva->type_payment;
  128. $total = $tva->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. 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/tva/card.php?id='.$chid.'">'.$chid.'</a></td></tr>';
  150. //print '<tr><td>'.$langs->trans("Type")."</td><td>".$tva->type_label."</td></tr>\n";
  151. print '<tr><td>'.$langs->trans("Period")."</td><td>".dol_print_date($tva->datev, 'day')."</td></tr>\n";
  152. print '<tr><td>'.$langs->trans("Label").'</td><td>'.$tva->label."</td></tr>\n";
  153. /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($tva->date_ech,'day')."</td></tr>\n";
  154. print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($tva->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
  155. $sql = "SELECT sum(p.amount) as total";
  156. $sql .= " FROM ".MAIN_DB_PREFIX."payment_vat as p";
  157. $sql .= " WHERE p.fk_tva = ".((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) ? (GETPOST("remonth", 'int') ? $datepaye : -1) : 0;
  169. print $form->selectDate($datepayment, '', '', '', '', "add_payment", 1, 1);
  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", "int") : $tva->paiementtype, "paiementtype", '', 0, 1, 0, 0, 1, 'maxwidth500 widthcentpercentminusx');
  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', 'pictofixedwidth');
  180. $form->select_comptes(GETPOST("accountid", "int") ? GETPOST("accountid", "int") : $tva->accountid, "accountid", 0, '', 1, '', 0, 'maxwidth500 widthcentpercentminusx'); // 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" class="quatrevingtpercent" wrap="soft" rows="'.ROWS_3.'"></textarea></td>';
  190. print '</tr>';
  191. print '</table>';
  192. print dol_get_fiche_end();
  193. /*
  194. * Autres charges impayees
  195. */
  196. $num = 1;
  197. $i = 0;
  198. 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
  199. print '<table class="noborder centpercent">';
  200. print '<tr class="liste_titre">';
  201. //print '<td>'.$langs->trans("SocialContribution").'</td>';
  202. print '<td class="left">'.$langs->trans("DateDue").'</td>';
  203. print '<td class="right">'.$langs->trans("Amount").'</td>';
  204. print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
  205. print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
  206. print '<td class="center">'.$langs->trans("Amount").'</td>';
  207. print "</tr>\n";
  208. $total = 0;
  209. $totalrecu = 0;
  210. while ($i < $num) {
  211. $objp = $tva;
  212. print '<tr class="oddeven">';
  213. if ($objp->datev > 0) {
  214. print '<td class="left">'.dol_print_date($objp->datev, 'day').'</td>'."\n";
  215. } else {
  216. print '<td class="center"><b>!!!</b></td>'."\n";
  217. }
  218. print '<td class="right nowraponall"><span class="amount">'.price($objp->amount)."</span></td>";
  219. print '<td class="right nowraponall"><span class="amount">'.price($sumpaid)."</span></td>";
  220. print '<td class="right nowraponall"><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. /* Disabled, we autofil the amount with remain to pay by default
  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" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$remaintopay.'">';
  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->amount;
  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. print '</div>';
  254. // Bouton Save payment
  255. print '<br><div class="center"><input type="checkbox" checked name="closepaidvat"> '.$langs->trans("ClosePaidVATAutomatically");
  256. print '<br><input type="submit" class="button" name="save" value="'.$langs->trans('ToMakePayment').'">';
  257. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  258. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  259. print '</div>';
  260. print "</form>\n";
  261. }
  262. llxFooter();
  263. $db->close();