dolibarr 21.0.0-alpha
paiement_vat.php
1<?php
2/* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2016-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
5 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
30require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/paymentvat.class.php';
31require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array("banks", "bills"));
35
36$action = GETPOST('action', 'alpha');
37$confirm = GETPOST('confirm', 'alpha');
38$cancel = GETPOST('cancel');
39
40$chid = GETPOSTINT("id");
41$amounts = array();
42
43// Security check
44$socid = 0;
45if ($user->socid > 0) {
46 $socid = $user->socid;
47}
48
49$permissiontoadd = $user->hasRight('tax', 'charges', 'creer');
50
51
52/*
53 * Actions
54 */
55
56if (($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) && $permissiontoadd) {
57 $error = 0;
58
59 if ($cancel) {
60 $loc = DOL_URL_ROOT.'/compta/tva/card.php?id='.$chid;
61 header("Location: ".$loc);
62 exit;
63 }
64
65 $datepaye = dol_mktime(12, 0, 0, GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
66
67 if (!(GETPOSTINT("paiementtype") > 0)) {
68 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
69 $error++;
70 $action = 'create';
71 }
72 if ($datepaye == '') {
73 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
74 $error++;
75 $action = 'create';
76 }
77 if (isModEnabled("bank") && !(GETPOSTINT("accountid") > 0)) {
78 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
79 $error++;
80 $action = 'create';
81 }
82
83 // Read possible payments
84 foreach ($_POST as $key => $value) {
85 if (substr($key, 0, 7) == 'amount_') {
86 $other_chid = substr($key, 7);
87 $amounts[$other_chid] = (float) price2num(GETPOST($key));
88 }
89 }
90
91 if (empty($amounts[key($amounts)])) {
92 $error++;
93 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
94 $action = 'create';
95 }
96
97 if (!$error) {
98 $paymentid = 0;
99
100 if (!$error) {
101 $db->begin();
102
103 // Create a line of payments
104 $paiement = new PaymentVAT($db);
105 $paiement->chid = $chid;
106 $paiement->datepaye = $datepaye;
107 $paiement->amounts = $amounts; // Tableau de montant
108 $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
109 $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
110 $paiement->note = (string) GETPOST("note", 'restricthtml');
111 $paiement->note_private = (string) GETPOST("note", 'restricthtml');
112
113 if (!$error) {
114 $paymentid = $paiement->create($user, (GETPOST('closepaidvat') == 'on' ? 1 : 0));
115 if ($paymentid < 0) {
116 $error++;
117 setEventMessages($paiement->error, null, 'errors');
118 $action = 'create';
119 }
120 }
121
122 if (!$error) {
123 $result = $paiement->addPaymentToBank($user, 'payment_vat', '(VATPayment)', GETPOSTINT('accountid'), '', '');
124 if (!($result > 0)) {
125 $error++;
126 setEventMessages($paiement->error, null, 'errors');
127 $action = 'create';
128 }
129 }
130
131 if (!$error) {
132 $db->commit();
133 $loc = DOL_URL_ROOT.'/compta/tva/card.php?id='.$chid;
134 header('Location: '.$loc);
135 exit;
136 } else {
137 $db->rollback();
138 }
139 }
140 }
141}
142
143
144/*
145 * View
146 */
147
148llxHeader();
149
150$form = new Form($db);
151
152
153// Formulaire de creation d'un paiement de charge
154if ($action == 'create') {
155 $tva = new Tva($db);
156 $tva->fetch($chid);
157 $tva->accountid = $tva->fk_account ? $tva->fk_account : $tva->accountid;
158 $tva->paiementtype = $tva->type_payment;
159
160 $total = $tva->amount;
161 if (!empty($conf->use_javascript_ajax)) {
162 print "\n".'<script type="text/javascript">';
163
164 //Add js for AutoFill
165 print ' $(document).ready(function () {';
166 print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
167 var amount = $(this).data("value");
168 document.getElementById($(this).data(\'rowid\')).value = amount ;
169 });';
170 print ' });'."\n";
171
172 print ' </script>'."\n";
173 }
174
175 print load_fiche_titre($langs->trans("DoPayment"));
176 print "<br>\n";
177
178 print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
179 print '<input type="hidden" name="token" value="'.newToken().'">';
180 print '<input type="hidden" name="id" value="'.$chid.'">';
181 print '<input type="hidden" name="chid" value="'.$chid.'">';
182 print '<input type="hidden" name="action" value="add_payment">';
183
184 print dol_get_fiche_head([], '');
185
186 print '<table class="border centpercent">';
187
188 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>';
189 //print '<tr><td>'.$langs->trans("Type")."</td><td>".$tva->type_label."</td></tr>\n";
190 print '<tr><td>'.$langs->trans("Period")."</td><td>".dol_print_date($tva->datev, 'day')."</td></tr>\n";
191 print '<tr><td>'.$langs->trans("Label").'</td><td>'.$tva->label."</td></tr>\n";
192 /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($tva->date_ech,'day')."</td></tr>\n";
193 print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($tva->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
194
195 $sql = "SELECT sum(p.amount) as total";
196 $sql .= " FROM ".MAIN_DB_PREFIX."payment_vat as p";
197 $sql .= " WHERE p.fk_tva = ".((int) $chid);
198 $resql = $db->query($sql);
199 if ($resql) {
200 $obj = $db->fetch_object($resql);
201 $sumpaid = $obj->total;
202 $db->free($resql);
203 }
204 /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
205 print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
206
207 print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
208 $datepaye = dol_mktime(12, 0, 0, GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
209 $datepayment = !getDolGlobalString('MAIN_AUTOFILL_DATE') ? (GETPOSTINT("remonth") ? $datepaye : -1) : 0;
210 print $form->selectDate($datepayment, '', 0, 0, 0, "add_payment", 1, 1);
211 print "</td>";
212 print '</tr>';
213
214 print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
215 print $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOSTINT("paiementtype") : $tva->paiementtype, "paiementtype", '', 0, 1, 0, 0, 1, 'maxwidth500 widthcentpercentminusx', 1);
216 print "</td>\n";
217 print '</tr>';
218
219 print '<tr>';
220 print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
221 print '<td>';
222 print img_picto('', 'bank_account', 'class="pictofixedwidth"');
223 $form->select_comptes(GETPOSTINT("accountid") ? GETPOSTINT("accountid") : $tva->accountid, "accountid", 0, '', 1, '', 0, 'maxwidth500 widthcentpercentminusx'); // Show opend bank account list
224 print '</td></tr>';
225
226 // Number
227 print '<tr><td>'.$langs->trans('Numero');
228 print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
229 print '</td>';
230 print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
231
232 print '<tr>';
233 print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
234 print '<td class="tdtop"><textarea name="note" class="quatrevingtpercent" wrap="soft" rows="'.ROWS_3.'"></textarea></td>';
235 print '</tr>';
236
237 print '</table>';
238
239 print dol_get_fiche_end();
240
241 /*
242 * Autres charges impayees
243 */
244 $num = 1;
245 $i = 0;
246
247 print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
248 print '<table class="noborder centpercent">';
249 print '<tr class="liste_titre">';
250 //print '<td>'.$langs->trans("SocialContribution").'</td>';
251 print '<td class="left">'.$langs->trans("DateDue").'</td>';
252 print '<td class="right">'.$langs->trans("Amount").'</td>';
253 print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
254 print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
255 print '<td class="center">'.$langs->trans("Amount").'</td>';
256 print "</tr>\n";
257
258 $total = 0;
259 $totalrecu = 0;
260 $total_ttc = 0.;
261
262 while ($i < $num) {
263 $objp = $tva;
264
265 print '<tr class="oddeven">';
266
267 if ($objp->datev > 0) {
268 print '<td class="left">'.dol_print_date($objp->datev, 'day').'</td>'."\n";
269 } else {
270 print '<td class="center"><b>!!!</b></td>'."\n";
271 }
272
273 print '<td class="right nowraponall"><span class="amount">'.price($objp->amount)."</span></td>";
274
275 print '<td class="right nowraponall"><span class="amount">'.price($sumpaid)."</span></td>";
276
277 print '<td class="right nowraponall"><span class="amount">'.price((float) $objp->amount - $sumpaid)."</span></td>";
278
279 print '<td class="center">';
280
281 if ($sumpaid != $objp->amount) {
282 $namef = "amount_".$objp->id;
283 $nameRemain = "remain_".$objp->id;
284 /* Disabled, we autofil the amount with remain to pay by default
285 if (!empty($conf->use_javascript_ajax)) {
286 print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
287 } */
288 $remaintopay = (float) $objp->amount - $sumpaid;
289 print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">';
290 print '<input type="text" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$remaintopay.'">';
291 } else {
292 print '-';
293 }
294 print "</td>";
295
296 print "</tr>\n";
297 $total += $objp->total;
298 $total_ttc += $objp->total_ttc;
299 $totalrecu += $objp->amount;
300 $i++;
301 }
302 if ($i > 1) {
303 // Print total
304 print '<tr class="oddeven">';
305 print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
306 print '<td class="right"><b>'.price($total_ttc).'</b></td>';
307 print '<td class="right"><b>'.price($totalrecu).'</b></td>';
308 print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
309 print '<td align="center">&nbsp;</td>';
310 print "</tr>\n";
311 }
312
313 print "</table>";
314 print '</div>';
315
316 // Bouton Save payment
317 print '<br><div class="center"><input type="checkbox" checked name="closepaidvat"> '.$langs->trans("ClosePaidVATAutomatically");
318 print '<br><input type="submit" class="button" name="save" value="'.$langs->trans('ToMakePayment').'">';
319 print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
320 print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
321 print '</div>';
322
323 print "</form>\n";
324}
325
326llxFooter();
327$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:70
Class to manage generation of HTML components Only common components must be here.
Class to manage payments of social contributions.
Class to manage VAT - Value-added tax (also known in French as TVA - Taxe sur la valeur ajoutée)
Definition tva.class.php:38
llxFooter()
Footer empty.
Definition document.php:107
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.