dolibarr 20.0.2
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
57 public $datec = '';
58
59 public $datep = '';
60
66 public $total;
67
71 public $amount; // Total amount of payment
72
76 public $amounts = array(); // Array of amounts
77
81 public $fk_typepaiement;
82
88 public $num_paiement;
89
94 public $num_payment;
95
99 public $fk_bank;
100
104 public $fk_user_creat;
105
109 public $fk_user_modif;
110
114 public $chid;
115
121 public $lib;
122
126 public $datepaye;
127
131 public $type_code;
132
136 public $type_label;
137
141 public $bank_account;
142
146 public $bank_line;
147
151 public $paiementtype;
152
158 public function __construct($db)
159 {
160 $this->db = $db;
161 }
162
171 public function create($user, $closepaidvat = 0)
172 {
173 $error = 0;
174
175 $now = dol_now();
176
177 dol_syslog(get_class($this)."::create", LOG_DEBUG);
178
179 // Validate parameters
180 if (!$this->datepaye) {
181 $this->error = 'ErrorBadValueForParameterCreatePaymentVAT';
182 return -1;
183 }
184
185 // Clean parameters
186 if (isset($this->fk_tva)) {
187 $this->fk_tva = (int) $this->fk_tva;
188 }
189 if (isset($this->amount)) {
190 $this->amount = (float) $this->amount;
191 }
192 if (isset($this->fk_typepaiement)) {
193 $this->fk_typepaiement = (int) $this->fk_typepaiement;
194 }
195 if (isset($this->num_paiement)) {
196 $this->num_paiement = trim($this->num_paiement); // deprecated
197 }
198 if (isset($this->num_payment)) {
199 $this->num_payment = trim($this->num_payment);
200 }
201 if (isset($this->note)) {
202 $this->note = trim($this->note);
203 }
204 if (isset($this->fk_bank)) {
205 $this->fk_bank = (int) $this->fk_bank;
206 }
207 if (isset($this->fk_user_creat)) {
208 $this->fk_user_creat = (int) $this->fk_user_creat;
209 }
210 if (isset($this->fk_user_modif)) {
211 $this->fk_user_modif = (int) $this->fk_user_modif;
212 }
213
214 $totalamount = 0;
215 foreach ($this->amounts as $key => $value) { // How payment is dispatch
216 $newvalue = (float) price2num($value, 'MT');
217 $this->amounts[$key] = $newvalue;
218 $totalamount += $newvalue;
219 }
220 // $totalamount = price2num($totalamount);
221
222 // Check parameters
223 if ($totalamount == 0) {
224 return -1; // We accept negative amounts for chargebacks, but not null amounts.
225 }
226
227
228 $this->db->begin();
229
230 if ($totalamount != 0) {
231 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_vat (fk_tva, datec, datep, amount,";
232 $sql .= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
233 $sql .= " VALUES ($this->chid, '".$this->db->idate($now)."',";
234 $sql .= " '".$this->db->idate($this->datepaye)."',";
235 $sql .= " ".((float) $totalamount).",";
236 $sql .= " ".((int) $this->paiementtype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".$user->id.",";
237 $sql .= " 0)";
238
239 $resql = $this->db->query($sql);
240 if ($resql) {
241 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_vat");
242
243 // Insert table of amounts / invoices
244 foreach ($this->amounts as $key => $amount) {
245 $contribid = $key;
246 if (is_numeric($amount) && $amount != 0) {
247 $amount = (float) price2num($amount);
248
249 // If we want to closed paid invoices
250 if ($closepaidvat) {
251 $contrib = new Tva($this->db);
252 $contrib->fetch($contribid);
253 $paiement = $contrib->getSommePaiement();
254 //$creditnotes=$contrib->getSumCreditNotesUsed();
255 $creditnotes = 0;
256 //$deposits=$contrib->getSumDepositsUsed();
257 $deposits = 0;
258 $alreadypayed = (float) price2num($paiement + $creditnotes + $deposits, 'MT');
259 $remaintopay = (float) price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT');
260 if ($remaintopay == 0) {
261 $result = $contrib->setPaid($user);
262 } else {
263 dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
264 }
265 }
266 }
267 }
268 } else {
269 $error++;
270 }
271 }
272
273 $result = $this->call_trigger('PAYMENTVAT_CREATE', $user);
274 if ($result < 0) {
275 $error++;
276 }
277
278 if ($totalamount != 0 && !$error) {
279 $this->amount = $totalamount;
280 $this->total = $totalamount; // deprecated
281 $this->db->commit();
282 return $this->id;
283 } else {
284 $this->error = $this->db->error();
285 $this->db->rollback();
286 return -1;
287 }
288 }
289
296 public function fetch($id)
297 {
298 $sql = "SELECT";
299 $sql .= " t.rowid,";
300 $sql .= " t.fk_tva,";
301 $sql .= " t.datec,";
302 $sql .= " t.tms,";
303 $sql .= " t.datep,";
304 $sql .= " t.amount,";
305 $sql .= " t.fk_typepaiement,";
306 $sql .= " t.num_paiement as num_payment,";
307 $sql .= " t.note as note_private,";
308 $sql .= " t.fk_bank,";
309 $sql .= " t.fk_user_creat,";
310 $sql .= " t.fk_user_modif,";
311 $sql .= " pt.code as type_code, pt.libelle as type_label,";
312 $sql .= ' b.fk_account';
313 $sql .= " FROM ".MAIN_DB_PREFIX."payment_vat as t LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepaiement = pt.id";
314 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
315 $sql .= " WHERE t.rowid = ".((int) $id);
316 // TODO link on entity of tax;
317
318 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
319 $resql = $this->db->query($sql);
320 if ($resql) {
321 if ($this->db->num_rows($resql)) {
322 $obj = $this->db->fetch_object($resql);
323
324 $this->id = $obj->rowid;
325 $this->ref = $obj->rowid;
326
327 $this->fk_tva = $obj->fk_tva;
328 $this->datec = $this->db->jdate($obj->datec);
329 $this->tms = $this->db->jdate($obj->tms);
330 $this->datep = $this->db->jdate($obj->datep);
331 $this->amount = $obj->amount;
332 $this->fk_typepaiement = $obj->fk_typepaiement;
333 $this->num_paiement = $obj->num_payment;
334 $this->num_payment = $obj->num_payment;
335 $this->note = $obj->note_private;
336 $this->note_private = $obj->note_private;
337 $this->fk_bank = $obj->fk_bank;
338 $this->fk_user_creat = $obj->fk_user_creat;
339 $this->fk_user_modif = $obj->fk_user_modif;
340
341 $this->type_code = $obj->type_code;
342 $this->type_label = $obj->type_label;
343
344 $this->bank_account = $obj->fk_account;
345 $this->bank_line = $obj->fk_bank;
346 }
347 $this->db->free($resql);
348
349 return 1;
350 } else {
351 $this->error = "Error ".$this->db->lasterror();
352 return -1;
353 }
354 }
355
356
364 public function update($user = null, $notrigger = 0)
365 {
366 global $conf, $langs;
367 $error = 0;
368
369 // Clean parameters
370
371 if (isset($this->fk_tva)) {
372 $this->fk_tva = (int) $this->fk_tva;
373 }
374 if (isset($this->amount)) {
375 $this->amount = (float) $this->amount;
376 }
377 if (isset($this->fk_typepaiement)) {
378 $this->fk_typepaiement = (int) $this->fk_typepaiement;
379 }
380 if (isset($this->num_payment)) {
381 $this->num_payment = trim($this->num_payment);
382 }
383 if (isset($this->note)) {
384 $this->note = trim($this->note);
385 }
386 if (isset($this->fk_bank)) {
387 $this->fk_bank = (int) $this->fk_bank;
388 }
389 if (isset($this->fk_user_creat)) {
390 $this->fk_user_creat = (int) $this->fk_user_creat;
391 }
392 if (isset($this->fk_user_modif)) {
393 $this->fk_user_modif = (int) $this->fk_user_modif;
394 }
395
396 // Check parameters
397 // Put here code to add control on parameters values
398
399 // Update request
400 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_vat SET";
401 $sql .= " fk_tva=".(isset($this->fk_tva) ? ((int) $this->fk_tva) : "null").",";
402 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
403 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
404 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
405 $sql .= " amount=".(isset($this->amount) ? (float) price2num($this->amount) : "null").",";
406 $sql .= " fk_typepaiement=".(isset($this->fk_typepaiement) ? ((int) $this->fk_typepaiement) : "null").",";
407 $sql .= " num_paiement=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
408 $sql .= " note=".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : "null").",";
409 $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
410 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
411 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
412 $sql .= " WHERE rowid=".((int) $this->id);
413
414 $this->db->begin();
415
416 dol_syslog(get_class($this)."::update", LOG_DEBUG);
417 $resql = $this->db->query($sql);
418 if (!$resql) {
419 $error++;
420 $this->errors[] = "Error ".$this->db->lasterror();
421 }
422
423 // Commit or rollback
424 if ($error) {
425 foreach ($this->errors as $errmsg) {
426 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
427 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
428 }
429 $this->db->rollback();
430 return -1 * $error;
431 } else {
432 $this->db->commit();
433 return 1;
434 }
435 }
436
437
445 public function delete($user, $notrigger = 0)
446 {
447 $error = 0;
448
449 dol_syslog(get_class($this)."::delete");
450
451 $this->db->begin();
452
453 if ($this->bank_line > 0) {
454 $accline = new AccountLine($this->db);
455 $accline->fetch($this->bank_line);
456 $result = $accline->delete($user);
457 if ($result < 0) {
458 $this->errors[] = $accline->error;
459 $error++;
460 }
461 }
462
463 if (!$error) {
464 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_vat";
465 $sql .= " WHERE rowid=".((int) $this->id);
466
467 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
468 $resql = $this->db->query($sql);
469 if (!$resql) {
470 $error++;
471 $this->errors[] = "Error ".$this->db->lasterror();
472 }
473 }
474
475 // Commit or rollback
476 if ($error) {
477 foreach ($this->errors as $errmsg) {
478 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
479 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
480 }
481 $this->db->rollback();
482 return -1 * $error;
483 } else {
484 $this->db->commit();
485 return 1;
486 }
487 }
488
489
490
498 public function createFromClone(User $user, $fromid)
499 {
500 $error = 0;
501
502 $object = new PaymentVAT($this->db);
503
504 $this->db->begin();
505
506 // Load source object
507 $object->fetch($fromid);
508 $object->id = 0;
509 $object->statut = 0;
510
511 // Clear fields
512
513 // Create clone
514 $object->context['createfromclone'] = 'createfromclone';
515 $result = $object->create($user);
516
517 // Other options
518 if ($result < 0) {
519 $this->error = $object->error;
520 $error++;
521 }
522
523 unset($object->context['createfromclone']);
524
525 // End
526 if (!$error) {
527 $this->db->commit();
528 return $object->id;
529 } else {
530 $this->db->rollback();
531 return -1;
532 }
533 }
534
535
543 public function initAsSpecimen()
544 {
545 $this->id = 0;
546 $this->fk_tva = 0;
547 $this->datec = dol_now();
548 $this->tms = dol_now();
549 $this->datep = dol_now();
550 $this->amount = 100;
551 $this->fk_typepaiement = 0;
552 $this->num_payment = '123456';
553 $this->note_private = 'Private note';
554 $this->note_public = 'Public note';
555 $this->fk_bank = 0;
556 $this->fk_user_creat = 0;
557 $this->fk_user_modif = 0;
558
559 return 1;
560 }
561
562
575 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
576 {
577 // Clean data
578 $this->num_payment = trim($this->num_payment ? $this->num_payment : $this->num_paiement);
579
580 $error = 0;
581
582 if (isModEnabled("bank")) {
583 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
584
585 $acc = new Account($this->db);
586 $acc->fetch($accountid);
587
588 $total = $this->amount;
589 if ($mode == 'payment_vat') {
590 $total = -$total;
591 }
592
593 // Insert payment into llx_bank
594 $bank_line_id = $acc->addline(
595 $this->datepaye,
596 $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example")
597 $label,
598 $total,
599 $this->num_payment,
600 '',
601 $user,
602 $emetteur_nom,
603 $emetteur_banque
604 );
605
606 // Update fk_bank in llx_paiement.
607 // We thus know the payment that generated the bank entry
608 if ($bank_line_id > 0) {
609 $result = $this->update_fk_bank($bank_line_id);
610 if ($result <= 0) {
611 $error++;
612 dol_print_error($this->db);
613 }
614
615 // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
616 $url = '';
617 if ($mode == 'payment_vat') {
618 $url = DOL_URL_ROOT.'/compta/payment_vat/card.php?id=';
619 }
620 if ($url) {
621 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
622 if ($result <= 0) {
623 $error++;
624 dol_print_error($this->db);
625 }
626 }
627
628 // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
629 $linkaddedforthirdparty = array();
630 foreach ($this->amounts as $key => $value) {
631 if ($mode == 'payment_vat') {
632 $tva = new Tva($this->db);
633 $tva->fetch($key);
634 $result = $acc->add_url_line($bank_line_id, $tva->id, DOL_URL_ROOT.'/compta/tva/card.php?id=', '('.$tva->label.')', 'vat');
635 if ($result <= 0) {
636 dol_print_error($this->db);
637 }
638 }
639 }
640 } else {
641 $this->error = $acc->error;
642 $error++;
643 }
644 }
645
646 if (!$error) {
647 return 1;
648 } else {
649 return -1;
650 }
651 }
652
653
654 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
661 public function update_fk_bank($id_bank)
662 {
663 // phpcs:enable
664 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_vat SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
665
666 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
667 $result = $this->db->query($sql);
668 if ($result) {
669 return 1;
670 } else {
671 $this->error = $this->db->error();
672 return 0;
673 }
674 }
675
676
683 public function getLibStatut($mode = 0)
684 {
685 return $this->LibStatut($this->statut, $mode);
686 }
687
688 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
696 public function LibStatut($status, $mode = 0)
697 {
698 // phpcs:enable
699 global $langs;
700
701 $langs->load('compta');
702 /*if ($mode == 0)
703 {
704 if ($status == 0) return $langs->trans('ToValidate');
705 if ($status == 1) return $langs->trans('Validated');
706 }
707 if ($mode == 1)
708 {
709 if ($status == 0) return $langs->trans('ToValidate');
710 if ($status == 1) return $langs->trans('Validated');
711 }
712 if ($mode == 2)
713 {
714 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
715 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
716 }
717 if ($mode == 3)
718 {
719 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
720 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
721 }
722 if ($mode == 4)
723 {
724 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
725 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
726 }
727 if ($mode == 5)
728 {
729 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
730 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
731 }
732 if ($mode == 6)
733 {
734 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
735 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
736 }*/
737 return '';
738 }
739
747 public function getNomUrl($withpicto = 0, $maxlen = 0)
748 {
749 global $langs;
750
751 $result = '';
752
753 if (empty($this->ref)) {
754 $this->ref = $this->lib;
755 }
756
757 $label = img_picto('', $this->picto).' <u>'.$langs->trans("VATPayment").'</u>';
758 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
759 if (!empty($this->label)) {
760 $labeltoshow = $this->label;
761 $reg = array();
762 if (preg_match('/^\‍((.*)\‍)$/i', $this->label, $reg)) {
763 // Label generique car entre parentheses. On l'affiche en le traduisant
764 if ($reg[1] == 'paiement') {
765 $reg[1] = 'Payment';
766 }
767 $labeltoshow = $langs->trans($reg[1]);
768 }
769 $label .= '<br><b>'.$langs->trans('Label').':</b> '.$labeltoshow;
770 }
771 if ($this->datep) {
772 $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->datep, 'day');
773 }
774
775 if (!empty($this->id)) {
776 $link = '<a href="'.DOL_URL_ROOT.'/compta/payment_vat/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
777 $linkend = '</a>';
778
779 if ($withpicto) {
780 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
781 }
782 if ($withpicto && $withpicto != 2) {
783 $result .= ' ';
784 }
785 if ($withpicto != 2) {
786 $result .= $link.($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
787 }
788 }
789
790 return $result;
791 }
792}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
$object ref
Definition info.php:79
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.
Put here description of your class.
Definition tva.class.php:37
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...
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e e e e e statut
Definition invoice.php:2010