dolibarr 21.0.0-beta
paymentvat.class.php
1<?php
2/* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2007 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
30
31
36{
40 public $element = 'payment_vat';
41
45 public $table_element = 'payment_vat';
46
50 public $picto = 'payment';
51
55 public $fk_tva;
56
60 public $datec = '';
61
65 public $datep = '';
66
72 public $total;
73
77 public $amount; // Total amount of payment
78
82 public $amounts = array(); // Array of amounts
83
87 public $fk_typepaiement;
88
94 public $num_paiement;
95
100 public $num_payment;
101
105 public $fk_bank;
106
110 public $fk_user_creat;
111
115 public $fk_user_modif;
116
120 public $chid;
121
127 public $lib;
128
132 public $datepaye;
133
137 public $type_code;
138
142 public $type_label;
143
147 public $bank_account;
148
152 public $bank_line;
153
157 public $paiementtype;
158
164 public function __construct($db)
165 {
166 $this->db = $db;
167 }
168
177 public function create($user, $closepaidvat = 0)
178 {
179 $error = 0;
180
181 $now = dol_now();
182
183 dol_syslog(get_class($this)."::create", LOG_DEBUG);
184
185 // Validate parameters
186 if (!$this->datepaye) {
187 $this->error = 'ErrorBadValueForParameterCreatePaymentVAT';
188 return -1;
189 }
190
191 // Clean parameters
192 if (isset($this->fk_tva)) {
193 $this->fk_tva = (int) $this->fk_tva;
194 }
195 if (isset($this->amount)) {
196 $this->amount = (float) $this->amount;
197 }
198 if (isset($this->fk_typepaiement)) {
199 $this->fk_typepaiement = (int) $this->fk_typepaiement;
200 }
201 if (isset($this->num_paiement)) {
202 $this->num_paiement = trim($this->num_paiement); // deprecated
203 }
204 if (isset($this->num_payment)) {
205 $this->num_payment = trim($this->num_payment);
206 }
207 if (isset($this->note)) {
208 $this->note = trim($this->note);
209 }
210 if (isset($this->fk_bank)) {
211 $this->fk_bank = (int) $this->fk_bank;
212 }
213 if (isset($this->fk_user_creat)) {
214 $this->fk_user_creat = (int) $this->fk_user_creat;
215 }
216 if (isset($this->fk_user_modif)) {
217 $this->fk_user_modif = (int) $this->fk_user_modif;
218 }
219
220 $totalamount = 0;
221 foreach ($this->amounts as $key => $value) { // How payment is dispatch
222 $newvalue = (float) price2num($value, 'MT');
223 $this->amounts[$key] = $newvalue;
224 $totalamount += $newvalue;
225 }
226 // $totalamount = price2num($totalamount);
227
228 // Check parameters
229 if ($totalamount == 0) {
230 return -1; // We accept negative amounts for chargebacks, but not null amounts.
231 }
232
233
234 $this->db->begin();
235
236 if ($totalamount != 0) {
237 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_vat (fk_tva, datec, datep, amount,";
238 $sql .= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
239 $sql .= " VALUES ($this->chid, '".$this->db->idate($now)."',";
240 $sql .= " '".$this->db->idate($this->datepaye)."',";
241 $sql .= " ".((float) $totalamount).",";
242 $sql .= " ".((int) $this->paiementtype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".$user->id.",";
243 $sql .= " 0)";
244
245 $resql = $this->db->query($sql);
246 if ($resql) {
247 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_vat");
248
249 // Insert table of amounts / invoices
250 foreach ($this->amounts as $key => $amount) {
251 $contribid = $key;
252 if (is_numeric($amount) && $amount != 0) {
253 $amount = (float) price2num($amount);
254
255 // If we want to closed paid invoices
256 if ($closepaidvat) {
257 $contrib = new Tva($this->db);
258 $contrib->fetch($contribid);
259 $paiement = $contrib->getSommePaiement();
260 //$creditnotes=$contrib->getSumCreditNotesUsed();
261 $creditnotes = 0;
262 //$deposits=$contrib->getSumDepositsUsed();
263 $deposits = 0;
264 $alreadypayed = (float) price2num($paiement + $creditnotes + $deposits, 'MT');
265 $remaintopay = (float) price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT');
266 if ($remaintopay == 0) {
267 $result = $contrib->setPaid($user);
268 } else {
269 dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
270 }
271 }
272 }
273 }
274 } else {
275 $error++;
276 }
277 }
278
279 $result = $this->call_trigger('PAYMENTVAT_CREATE', $user);
280 if ($result < 0) {
281 $error++;
282 }
283
284 if ($totalamount != 0 && !$error) {
285 $this->amount = $totalamount;
286 $this->total = $totalamount; // deprecated
287 $this->db->commit();
288 return $this->id;
289 } else {
290 $this->error = $this->db->error();
291 $this->db->rollback();
292 return -1;
293 }
294 }
295
302 public function fetch($id)
303 {
304 $sql = "SELECT";
305 $sql .= " t.rowid,";
306 $sql .= " t.fk_tva,";
307 $sql .= " t.datec,";
308 $sql .= " t.tms,";
309 $sql .= " t.datep,";
310 $sql .= " t.amount,";
311 $sql .= " t.fk_typepaiement,";
312 $sql .= " t.num_paiement as num_payment,";
313 $sql .= " t.note as note_private,";
314 $sql .= " t.fk_bank,";
315 $sql .= " t.fk_user_creat,";
316 $sql .= " t.fk_user_modif,";
317 $sql .= " pt.code as type_code, pt.libelle as type_label,";
318 $sql .= ' b.fk_account';
319 $sql .= " FROM ".MAIN_DB_PREFIX."payment_vat as t LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepaiement = pt.id";
320 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
321 $sql .= " WHERE t.rowid = ".((int) $id);
322 // TODO link on entity of tax;
323
324 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
325 $resql = $this->db->query($sql);
326 if ($resql) {
327 if ($this->db->num_rows($resql)) {
328 $obj = $this->db->fetch_object($resql);
329
330 $this->id = $obj->rowid;
331 $this->ref = $obj->rowid;
332
333 $this->fk_tva = $obj->fk_tva;
334 $this->datec = $this->db->jdate($obj->datec);
335 $this->tms = $this->db->jdate($obj->tms);
336 $this->datep = $this->db->jdate($obj->datep);
337 $this->amount = $obj->amount;
338 $this->fk_typepaiement = $obj->fk_typepaiement;
339 $this->num_paiement = $obj->num_payment;
340 $this->num_payment = $obj->num_payment;
341 $this->note = $obj->note_private;
342 $this->note_private = $obj->note_private;
343 $this->fk_bank = $obj->fk_bank;
344 $this->fk_user_creat = $obj->fk_user_creat;
345 $this->fk_user_modif = $obj->fk_user_modif;
346
347 $this->type_code = $obj->type_code;
348 $this->type_label = $obj->type_label;
349
350 $this->bank_account = $obj->fk_account;
351 $this->bank_line = $obj->fk_bank;
352 }
353 $this->db->free($resql);
354
355 return 1;
356 } else {
357 $this->error = "Error ".$this->db->lasterror();
358 return -1;
359 }
360 }
361
362
370 public function update($user = null, $notrigger = 0)
371 {
372 global $conf, $langs;
373 $error = 0;
374
375 // Clean parameters
376
377 if (isset($this->fk_tva)) {
378 $this->fk_tva = (int) $this->fk_tva;
379 }
380 if (isset($this->amount)) {
381 $this->amount = (float) $this->amount;
382 }
383 if (isset($this->fk_typepaiement)) {
384 $this->fk_typepaiement = (int) $this->fk_typepaiement;
385 }
386 if (isset($this->num_payment)) {
387 $this->num_payment = trim($this->num_payment);
388 }
389 if (isset($this->note)) {
390 $this->note = trim($this->note);
391 }
392 if (isset($this->fk_bank)) {
393 $this->fk_bank = (int) $this->fk_bank;
394 }
395 if (isset($this->fk_user_creat)) {
396 $this->fk_user_creat = (int) $this->fk_user_creat;
397 }
398 if (isset($this->fk_user_modif)) {
399 $this->fk_user_modif = (int) $this->fk_user_modif;
400 }
401
402 // Check parameters
403 // Put here code to add control on parameters values
404
405 // Update request
406 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_vat SET";
407 $sql .= " fk_tva=".(isset($this->fk_tva) ? ((int) $this->fk_tva) : "null").",";
408 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
409 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
410 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
411 $sql .= " amount=".(isset($this->amount) ? (float) price2num($this->amount) : "null").",";
412 $sql .= " fk_typepaiement=".(isset($this->fk_typepaiement) ? ((int) $this->fk_typepaiement) : "null").",";
413 $sql .= " num_paiement=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
414 $sql .= " note=".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : "null").",";
415 $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
416 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
417 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
418 $sql .= " WHERE rowid=".((int) $this->id);
419
420 $this->db->begin();
421
422 dol_syslog(get_class($this)."::update", LOG_DEBUG);
423 $resql = $this->db->query($sql);
424 if (!$resql) {
425 $error++;
426 $this->errors[] = "Error ".$this->db->lasterror();
427 }
428
429 // Commit or rollback
430 if ($error) {
431 foreach ($this->errors as $errmsg) {
432 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
433 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
434 }
435 $this->db->rollback();
436 return -1 * $error;
437 } else {
438 $this->db->commit();
439 return 1;
440 }
441 }
442
443
451 public function delete($user, $notrigger = 0)
452 {
453 $error = 0;
454
455 dol_syslog(get_class($this)."::delete");
456
457 $this->db->begin();
458
459 if ($this->bank_line > 0) {
460 $accline = new AccountLine($this->db);
461 $accline->fetch($this->bank_line);
462 $result = $accline->delete($user);
463 if ($result < 0) {
464 $this->errors[] = $accline->error;
465 $error++;
466 }
467 }
468
469 if (!$error) {
470 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_vat";
471 $sql .= " WHERE rowid=".((int) $this->id);
472
473 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
474 $resql = $this->db->query($sql);
475 if (!$resql) {
476 $error++;
477 $this->errors[] = "Error ".$this->db->lasterror();
478 }
479 }
480
481 // Commit or rollback
482 if ($error) {
483 foreach ($this->errors as $errmsg) {
484 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
485 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
486 }
487 $this->db->rollback();
488 return -1 * $error;
489 } else {
490 $this->db->commit();
491 return 1;
492 }
493 }
494
495
496
504 public function createFromClone(User $user, $fromid)
505 {
506 $error = 0;
507
508 $object = new PaymentVAT($this->db);
509
510 $this->db->begin();
511
512 // Load source object
513 $object->fetch($fromid);
514 $object->id = 0;
515 $object->statut = 0;
516
517 // Clear fields
518
519 // Create clone
520 $object->context['createfromclone'] = 'createfromclone';
521 $result = $object->create($user);
522
523 // Other options
524 if ($result < 0) {
525 $this->error = $object->error;
526 $error++;
527 }
528
529 unset($object->context['createfromclone']);
530
531 // End
532 if (!$error) {
533 $this->db->commit();
534 return $object->id;
535 } else {
536 $this->db->rollback();
537 return -1;
538 }
539 }
540
541
549 public function initAsSpecimen()
550 {
551 $this->id = 0;
552 $this->fk_tva = 0;
553 $this->datec = dol_now();
554 $this->tms = dol_now();
555 $this->datep = dol_now();
556 $this->amount = 100;
557 $this->fk_typepaiement = 0;
558 $this->num_payment = '123456';
559 $this->note_private = 'Private note';
560 $this->note_public = 'Public note';
561 $this->fk_bank = 0;
562 $this->fk_user_creat = 0;
563 $this->fk_user_modif = 0;
564
565 return 1;
566 }
567
568
581 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
582 {
583 // Clean data
584 $this->num_payment = trim($this->num_payment ? $this->num_payment : $this->num_paiement);
585
586 $error = 0;
587
588 if (isModEnabled("bank")) {
589 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
590
591 $acc = new Account($this->db);
592 $acc->fetch($accountid);
593
594 $total = $this->amount;
595 if ($mode == 'payment_vat') {
596 $total = -$total;
597 }
598
599 // Insert payment into llx_bank
600 $bank_line_id = $acc->addline(
601 $this->datepaye,
602 $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example")
603 $label,
604 $total,
605 $this->num_payment,
606 0,
607 $user,
608 $emetteur_nom,
609 $emetteur_banque
610 );
611
612 // Update fk_bank in llx_paiement.
613 // We thus know the payment that generated the bank entry
614 if ($bank_line_id > 0) {
615 $result = $this->update_fk_bank($bank_line_id);
616 if ($result <= 0) {
617 $error++;
618 dol_print_error($this->db);
619 }
620
621 // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
622 $url = '';
623 if ($mode == 'payment_vat') {
624 $url = DOL_URL_ROOT.'/compta/payment_vat/card.php?id=';
625 }
626 if ($url) {
627 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
628 if ($result <= 0) {
629 $error++;
630 dol_print_error($this->db);
631 }
632 }
633
634 // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
635 $linkaddedforthirdparty = array();
636 foreach ($this->amounts as $key => $value) {
637 if ($mode == 'payment_vat') {
638 $tva = new Tva($this->db);
639 $tva->fetch($key);
640 $result = $acc->add_url_line($bank_line_id, $tva->id, DOL_URL_ROOT.'/compta/tva/card.php?id=', '('.$tva->label.')', 'vat');
641 if ($result <= 0) {
642 dol_print_error($this->db);
643 }
644 }
645 }
646 } else {
647 $this->error = $acc->error;
648 $error++;
649 }
650 }
651
652 if (!$error) {
653 return 1;
654 } else {
655 return -1;
656 }
657 }
658
659
660 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
667 public function update_fk_bank($id_bank)
668 {
669 // phpcs:enable
670 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_vat SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
671
672 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
673 $result = $this->db->query($sql);
674 if ($result) {
675 return 1;
676 } else {
677 $this->error = $this->db->error();
678 return 0;
679 }
680 }
681
682
689 public function getLibStatut($mode = 0)
690 {
691 return $this->LibStatut($this->statut, $mode);
692 }
693
694 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
702 public function LibStatut($status, $mode = 0)
703 {
704 // phpcs:enable
705 global $langs;
706
707 $langs->load('compta');
708 /*if ($mode == 0)
709 {
710 if ($status == 0) return $langs->trans('ToValidate');
711 if ($status == 1) return $langs->trans('Validated');
712 }
713 if ($mode == 1)
714 {
715 if ($status == 0) return $langs->trans('ToValidate');
716 if ($status == 1) return $langs->trans('Validated');
717 }
718 if ($mode == 2)
719 {
720 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
721 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
722 }
723 if ($mode == 3)
724 {
725 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
726 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
727 }
728 if ($mode == 4)
729 {
730 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
731 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
732 }
733 if ($mode == 5)
734 {
735 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
736 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
737 }
738 if ($mode == 6)
739 {
740 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
741 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
742 }*/
743 return '';
744 }
745
753 public function getNomUrl($withpicto = 0, $maxlen = 0)
754 {
755 global $langs;
756
757 $result = '';
758
759 if (empty($this->ref)) {
760 $this->ref = $this->lib;
761 }
762
763 $label = img_picto('', $this->picto).' <u>'.$langs->trans("VATPayment").'</u>';
764 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
765 if (!empty($this->label)) {
766 $labeltoshow = $this->label;
767 $reg = array();
768 if (preg_match('/^\‍((.*)\‍)$/i', $this->label, $reg)) {
769 // Label generique car entre parentheses. On l'affiche en le traduisant
770 if ($reg[1] == 'paiement') {
771 $reg[1] = 'Payment';
772 }
773 $labeltoshow = $langs->trans($reg[1]);
774 }
775 $label .= '<br><b>'.$langs->trans('Label').':</b> '.$labeltoshow;
776 }
777 if ($this->datep) {
778 $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->datep, 'day');
779 }
780
781 if (!empty($this->id)) {
782 $link = '<a href="'.DOL_URL_ROOT.'/compta/payment_vat/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
783 $linkend = '</a>';
784
785 if ($withpicto) {
786 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
787 }
788 if ($withpicto && $withpicto != 2) {
789 $result .= ' ';
790 }
791 if ($withpicto != 2) {
792 $result .= $link.($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
793 }
794 }
795
796 return $result;
797 }
798}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
$object ref
Definition info.php:89
Class to manage bank accounts.
Class to manage bank transaction lines.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage payments of social contributions.
initAsSpecimen()
Initialise an instance with random values.
LibStatut($status, $mode=0)
Return the label of a given status.
create($user, $closepaidvat=0)
Create payment of vat into database.
__construct($db)
Constructor.
update_fk_bank($id_bank)
Update link between vat payment and line in llx_bank generated.
fetch($id)
Load object in memory from database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
getLibStatut($mode=0)
Return the label of the status.
getNomUrl($withpicto=0, $maxlen=0)
Return clickable name (with picto eventually)
update($user=null, $notrigger=0)
Update database.
addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
Add record into bank for payment with links between this bank record and invoices of payment.
Class to manage VAT - Value-added tax (also known in French as TVA - Taxe sur la valeur ajoutée)
Definition tva.class.php:38
Class to manage Dolibarr users.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_now($mode='auto')
Return date for now.
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).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79