dolibarr 21.0.0-beta
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
41// Load translation files required by the page
42$langs->loadLangs(array("banks", "bills"));
43
44$action = GETPOST('action', 'alpha');
45$confirm = GETPOST('confirm', 'alpha');
46$cancel = GETPOST('cancel');
47
48$chid = GETPOSTINT("id");
49$amounts = array();
50
51// Security check
52$socid = 0;
53if ($user->socid > 0) {
54 $socid = $user->socid;
55}
56
57$permissiontoadd = $user->hasRight('tax', 'charges', 'creer');
58
59
60/*
61 * Actions
62 */
63
64if (($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) && $permissiontoadd) {
65 $error = 0;
66
67 if ($cancel) {
68 $loc = DOL_URL_ROOT.'/compta/tva/card.php?id='.$chid;
69 header("Location: ".$loc);
70 exit;
71 }
72
73 $datepaye = dol_mktime(12, 0, 0, GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
74
75 if (!(GETPOSTINT("paiementtype") > 0)) {
76 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
77 $error++;
78 $action = 'create';
79 }
80 if ($datepaye == '') {
81 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
82 $error++;
83 $action = 'create';
84 }
85 if (isModEnabled("bank") && !(GETPOSTINT("accountid") > 0)) {
86 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
87 $error++;
88 $action = 'create';
89 }
90
91 // Read possible payments
92 foreach ($_POST as $key => $value) {
93 if (substr($key, 0, 7) == 'amount_') {
94 $other_chid = substr($key, 7);
95 $amounts[$other_chid] = (float) price2num(GETPOST($key));
96 }
97 }
98
99 if (empty($amounts[key($amounts)])) {
100 $error++;
101 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
102 $action = 'create';
103 }
104
105 if (!$error) {
106 $paymentid = 0;
107
108 if (!$error) {
109 $db->begin();
110
111 // Create a line of payments
112 $paiement = new PaymentVAT($db);
113 $paiement->chid = $chid;
114 $paiement->datepaye = $datepaye;
115 $paiement->amounts = $amounts; // Tableau de montant
116 $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
117 $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
118 $paiement->note = (string) GETPOST("note", 'restricthtml');
119 $paiement->note_private = (string) GETPOST("note", 'restricthtml');
120
121 if (!$error) {
122 $paymentid = $paiement->create($user, (GETPOST('closepaidvat') == 'on' ? 1 : 0));
123 if ($paymentid < 0) {
124 $error++;
125 setEventMessages($paiement->error, null, 'errors');
126 $action = 'create';
127 }
128 }
129
130 if (!$error) {
131 $result = $paiement->addPaymentToBank($user, 'payment_vat', '(VATPayment)', GETPOSTINT('accountid'), '', '');
132 if (!($result > 0)) {
133 $error++;
134 setEventMessages($paiement->error, null, 'errors');
135 $action = 'create';
136 }
137 }
138
139 if (!$error) {
140 $db->commit();
141 $loc = DOL_URL_ROOT.'/compta/tva/card.php?id='.$chid;
142 header('Location: '.$loc);
143 exit;
144 } else {
145 $db->rollback();
146 }
147 }
148 }
149}
150
151
152/*
153 * View
154 */
155
156llxHeader();
157
158$form = new Form($db);
159
160
161// Formulaire de creation d'un paiement de charge
162if ($action == 'create') {
163 $tva = new Tva($db);
164 $tva->fetch($chid);
165 $tva->accountid = $tva->fk_account ? $tva->fk_account : $tva->accountid;
166 $tva->paiementtype = $tva->type_payment;
167
168 $total = $tva->amount;
169 if (!empty($conf->use_javascript_ajax)) {
170 print "\n".'<script type="text/javascript">';
171
172 //Add js for AutoFill
173 print ' $(document).ready(function () {';
174 print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
175 var amount = $(this).data("value");
176 document.getElementById($(this).data(\'rowid\')).value = amount ;
177 });';
178 print ' });'."\n";
179
180 print ' </script>'."\n";
181 }
182
183 print load_fiche_titre($langs->trans("DoPayment"));
184 print "<br>\n";
185
186 print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
187 print '<input type="hidden" name="token" value="'.newToken().'">';
188 print '<input type="hidden" name="id" value="'.$chid.'">';
189 print '<input type="hidden" name="chid" value="'.$chid.'">';
190 print '<input type="hidden" name="action" value="add_payment">';
191
192 print dol_get_fiche_head([], '');
193
194 print '<table class="border centpercent">';
195
196 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>';
197 //print '<tr><td>'.$langs->trans("Type")."</td><td>".$tva->type_label."</td></tr>\n";
198 print '<tr><td>'.$langs->trans("Period")."</td><td>".dol_print_date($tva->datev, 'day')."</td></tr>\n";
199 print '<tr><td>'.$langs->trans("Label").'</td><td>'.$tva->label."</td></tr>\n";
200 /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($tva->date_ech,'day')."</td></tr>\n";
201 print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($tva->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
202
203 $sql = "SELECT sum(p.amount) as total";
204 $sql .= " FROM ".MAIN_DB_PREFIX."payment_vat as p";
205 $sql .= " WHERE p.fk_tva = ".((int) $chid);
206 $sumpaid = 0;
207 $resql = $db->query($sql);
208 if ($resql) {
209 $obj = $db->fetch_object($resql);
210 $sumpaid = $obj->total;
211 $db->free($resql);
212 }
213 /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
214 print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
215
216 print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
217 $datepaye = dol_mktime(12, 0, 0, GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
218 $datepayment = !getDolGlobalString('MAIN_AUTOFILL_DATE') ? (GETPOSTINT("remonth") ? $datepaye : -1) : 0;
219 print $form->selectDate($datepayment, '', 0, 0, 0, "add_payment", 1, 1);
220 print "</td>";
221 print '</tr>';
222
223 print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
224 print $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOSTINT("paiementtype") : $tva->paiementtype, "paiementtype", '', 0, 1, 0, 0, 1, 'maxwidth500 widthcentpercentminusx', 1);
225 print "</td>\n";
226 print '</tr>';
227
228 print '<tr>';
229 print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
230 print '<td>';
231 print img_picto('', 'bank_account', 'class="pictofixedwidth"');
232 $form->select_comptes(GETPOSTINT("accountid") ? GETPOSTINT("accountid") : $tva->accountid, "accountid", 0, '', 1, '', 0, 'maxwidth500 widthcentpercentminusx'); // Show opend bank account list
233 print '</td></tr>';
234
235 // Number
236 print '<tr><td>'.$langs->trans('Numero');
237 print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
238 print '</td>';
239 print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
240
241 print '<tr>';
242 print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
243 print '<td class="tdtop"><textarea name="note" class="quatrevingtpercent" wrap="soft" rows="'.ROWS_3.'"></textarea></td>';
244 print '</tr>';
245
246 print '</table>';
247
248 print dol_get_fiche_end();
249
250 /*
251 * Autres charges impayees
252 */
253 $num = 1;
254 $i = 0;
255
256 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
257 print '<table class="noborder centpercent">';
258 print '<tr class="liste_titre">';
259 //print '<td>'.$langs->trans("SocialContribution").'</td>';
260 print '<td class="left">'.$langs->trans("DateDue").'</td>';
261 print '<td class="right">'.$langs->trans("Amount").'</td>';
262 print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
263 print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
264 print '<td class="center">'.$langs->trans("Amount").'</td>';
265 print "</tr>\n";
266
267 $total = 0;
268 $totalrecu = 0;
269 $total_ttc = 0.;
270
271 while ($i < $num) {
272 $objp = $tva;
273
274 print '<tr class="oddeven">';
275
276 if ($objp->datev > 0) {
277 print '<td class="left">'.dol_print_date($objp->datev, 'day').'</td>'."\n";
278 } else {
279 print '<td class="center"><b>!!!</b></td>'."\n";
280 }
281
282 print '<td class="right nowraponall"><span class="amount">'.price($objp->amount)."</span></td>";
283
284 print '<td class="right nowraponall"><span class="amount">'.price($sumpaid)."</span></td>";
285
286 print '<td class="right nowraponall"><span class="amount">'.price((float) $objp->amount - $sumpaid)."</span></td>";
287
288 print '<td class="center">';
289
290 if ($sumpaid != $objp->amount) {
291 $namef = "amount_".$objp->id;
292 $nameRemain = "remain_".$objp->id;
293 /* Disabled, we autofil the amount with remain to pay by default
294 if (!empty($conf->use_javascript_ajax)) {
295 print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
296 } */
297 $remaintopay = (float) $objp->amount - $sumpaid;
298 print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">';
299 print '<input type="text" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$remaintopay.'">';
300 } else {
301 print '-';
302 }
303 print "</td>";
304
305 print "</tr>\n";
306 $total += $objp->total;
307 $total_ttc += $objp->total_ttc;
308 $totalrecu += $objp->amount;
309 $i++;
310 }
311 if ($i > 1) {
312 // Print total
313 print '<tr class="oddeven">';
314 print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
315 print '<td class="right"><b>'.price($total_ttc).'</b></td>';
316 print '<td class="right"><b>'.price($totalrecu).'</b></td>';
317 print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
318 print '<td align="center">&nbsp;</td>';
319 print "</tr>\n";
320 }
321
322 print "</table>";
323 print '</div>';
324
325 // Bouton Save payment
326 print '<br><div class="center"><input type="checkbox" checked name="closepaidvat"> '.$langs->trans("ClosePaidVATAutomatically");
327 print '<br><input type="submit" class="button" name="save" value="'.$langs->trans('ToMakePayment').'">';
328 print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
329 print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
330 print '</div>';
331
332 print "</form>\n";
333}
334
335llxFooter();
336$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:71
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79