dolibarr 18.0.6
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 *
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
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
28
29
34{
38 public $element = 'payment_vat';
39
43 public $table_element = 'payment_vat';
44
48 public $picto = 'payment';
49
53 public $fk_tva;
54
55 public $datec = '';
56 public $tms = '';
57 public $datep = '';
58
63 public $total;
64
65 public $amount; // Total amount of payment
66 public $amounts = array(); // Array of amounts
67
71 public $fk_typepaiement;
72
77 public $num_paiement;
78
82 public $num_payment;
83
87 public $fk_bank;
88
92 public $fk_user_creat;
93
97 public $fk_user_modif;
98
102 public $chid;
103
109 public $lib;
110
114 public $datepaye;
115
119 public $paiementtype;
120
126 public function __construct($db)
127 {
128 $this->db = $db;
129 }
130
139 public function create($user, $closepaidcontrib = 0)
140 {
141 global $conf, $langs;
142
143 $error = 0;
144
145 $now = dol_now();
146
147 dol_syslog(get_class($this)."::create", LOG_DEBUG);
148
149 // Validate parametres
150 if (!$this->datepaye) {
151 $this->error = 'ErrorBadValueForParameterCreatePaymentVAT';
152 return -1;
153 }
154
155 // Clean parameters
156 if (isset($this->fk_tva)) {
157 $this->fk_tva = (int) $this->fk_tva;
158 }
159 if (isset($this->amount)) {
160 $this->amount = trim($this->amount);
161 }
162 if (isset($this->fk_typepaiement)) {
163 $this->fk_typepaiement = (int) $this->fk_typepaiement;
164 }
165 if (isset($this->num_paiement)) {
166 $this->num_paiement = trim($this->num_paiement); // deprecated
167 }
168 if (isset($this->num_payment)) {
169 $this->num_payment = trim($this->num_payment);
170 }
171 if (isset($this->note)) {
172 $this->note = trim($this->note);
173 }
174 if (isset($this->fk_bank)) {
175 $this->fk_bank = (int) $this->fk_bank;
176 }
177 if (isset($this->fk_user_creat)) {
178 $this->fk_user_creat = (int) $this->fk_user_creat;
179 }
180 if (isset($this->fk_user_modif)) {
181 $this->fk_user_modif = (int) $this->fk_user_modif;
182 }
183
184 $totalamount = 0;
185 foreach ($this->amounts as $key => $value) { // How payment is dispatch
186 $newvalue = price2num($value, 'MT');
187 $this->amounts[$key] = $newvalue;
188 $totalamount += $newvalue;
189 }
190 $totalamount = price2num($totalamount);
191
192 // Check parameters
193 if ($totalamount == 0) {
194 return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
195 }
196
197
198 $this->db->begin();
199
200 if ($totalamount != 0) {
201 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_vat (fk_tva, datec, datep, amount,";
202 $sql .= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
203 $sql .= " VALUES ($this->chid, '".$this->db->idate($now)."',";
204 $sql .= " '".$this->db->idate($this->datepaye)."',";
205 $sql .= " ".((float) $totalamount).",";
206 $sql .= " ".((int) $this->paiementtype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".$user->id.",";
207 $sql .= " 0)";
208
209 $resql = $this->db->query($sql);
210 if ($resql) {
211 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_vat");
212
213 // Insere tableau des montants / factures
214 foreach ($this->amounts as $key => $amount) {
215 $contribid = $key;
216 if (is_numeric($amount) && $amount <> 0) {
217 $amount = price2num($amount);
218
219 // If we want to closed payed invoices
220 if ($closepaidcontrib) {
221 $contrib = new Tva($this->db);
222 $contrib->fetch($contribid);
223 $paiement = $contrib->getSommePaiement();
224 //$creditnotes=$contrib->getSumCreditNotesUsed();
225 $creditnotes = 0;
226 //$deposits=$contrib->getSumDepositsUsed();
227 $deposits = 0;
228 $alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
229 $remaintopay = price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT');
230 if ($remaintopay == 0) {
231 $result = $contrib->setPaid($user);
232 } else {
233 dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
234 }
235 }
236 }
237 }
238 } else {
239 $error++;
240 }
241 }
242
243 $result = $this->call_trigger('PAYMENTVAT_CREATE', $user);
244 if ($result < 0) {
245 $error++;
246 }
247
248 if ($totalamount != 0 && !$error) {
249 $this->amount = $totalamount;
250 $this->total = $totalamount; // deprecated
251 $this->db->commit();
252 return $this->id;
253 } else {
254 $this->error = $this->db->error();
255 $this->db->rollback();
256 return -1;
257 }
258 }
259
266 public function fetch($id)
267 {
268 $sql = "SELECT";
269 $sql .= " t.rowid,";
270 $sql .= " t.fk_tva,";
271 $sql .= " t.datec,";
272 $sql .= " t.tms,";
273 $sql .= " t.datep,";
274 $sql .= " t.amount,";
275 $sql .= " t.fk_typepaiement,";
276 $sql .= " t.num_paiement as num_payment,";
277 $sql .= " t.note as note_private,";
278 $sql .= " t.fk_bank,";
279 $sql .= " t.fk_user_creat,";
280 $sql .= " t.fk_user_modif,";
281 $sql .= " pt.code as type_code, pt.libelle as type_label,";
282 $sql .= ' b.fk_account';
283 $sql .= " FROM ".MAIN_DB_PREFIX."payment_vat as t LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepaiement = pt.id";
284 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
285 $sql .= " WHERE t.rowid = ".((int) $id);
286 // TODO link on entity of tax;
287
288 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
289 $resql = $this->db->query($sql);
290 if ($resql) {
291 if ($this->db->num_rows($resql)) {
292 $obj = $this->db->fetch_object($resql);
293
294 $this->id = $obj->rowid;
295 $this->ref = $obj->rowid;
296
297 $this->fk_tva = $obj->fk_tva;
298 $this->datec = $this->db->jdate($obj->datec);
299 $this->tms = $this->db->jdate($obj->tms);
300 $this->datep = $this->db->jdate($obj->datep);
301 $this->amount = $obj->amount;
302 $this->fk_typepaiement = $obj->fk_typepaiement;
303 $this->num_paiement = $obj->num_payment;
304 $this->num_payment = $obj->num_payment;
305 $this->note = $obj->note_private;
306 $this->note_private = $obj->note_private;
307 $this->fk_bank = $obj->fk_bank;
308 $this->fk_user_creat = $obj->fk_user_creat;
309 $this->fk_user_modif = $obj->fk_user_modif;
310
311 $this->type_code = $obj->type_code;
312 $this->type_label = $obj->type_label;
313
314 $this->bank_account = $obj->fk_account;
315 $this->bank_line = $obj->fk_bank;
316 }
317 $this->db->free($resql);
318
319 return 1;
320 } else {
321 $this->error = "Error ".$this->db->lasterror();
322 return -1;
323 }
324 }
325
326
334 public function update($user = null, $notrigger = 0)
335 {
336 global $conf, $langs;
337 $error = 0;
338
339 // Clean parameters
340
341 if (isset($this->fk_tva)) {
342 $this->fk_tva = (int) $this->fk_tva;
343 }
344 if (isset($this->amount)) {
345 $this->amount = trim($this->amount);
346 }
347 if (isset($this->fk_typepaiement)) {
348 $this->fk_typepaiement = (int) $this->fk_typepaiement;
349 }
350 if (isset($this->num_paiement)) {
351 $this->num_paiement = trim($this->num_paiement); // deprecated
352 }
353 if (isset($this->num_payment)) {
354 $this->num_payment = trim($this->num_payment);
355 }
356 if (isset($this->note)) {
357 $this->note = trim($this->note);
358 }
359 if (isset($this->fk_bank)) {
360 $this->fk_bank = (int) $this->fk_bank;
361 }
362 if (isset($this->fk_user_creat)) {
363 $this->fk_user_creat = (int) $this->fk_user_creat;
364 }
365 if (isset($this->fk_user_modif)) {
366 $this->fk_user_modif = (int) $this->fk_user_modif;
367 }
368
369 // Check parameters
370 // Put here code to add control on parameters values
371
372 // Update request
373 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_vat SET";
374 $sql .= " fk_tva=".(isset($this->fk_tva) ? ((int) $this->fk_tva) : "null").",";
375 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
376 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
377 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
378 $sql .= " amount=".(isset($this->amount) ? (float) price2num($this->amount) : "null").",";
379 $sql .= " fk_typepaiement=".(isset($this->fk_typepaiement) ? ((int) $this->fk_typepaiement) : "null").",";
380 $sql .= " num_paiement=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
381 $sql .= " note=".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : "null").",";
382 $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
383 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
384 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
385 $sql .= " WHERE rowid=".((int) $this->id);
386
387 $this->db->begin();
388
389 dol_syslog(get_class($this)."::update", LOG_DEBUG);
390 $resql = $this->db->query($sql);
391 if (!$resql) {
392 $error++;
393 $this->errors[] = "Error ".$this->db->lasterror();
394 }
395
396 // Commit or rollback
397 if ($error) {
398 foreach ($this->errors as $errmsg) {
399 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
400 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
401 }
402 $this->db->rollback();
403 return -1 * $error;
404 } else {
405 $this->db->commit();
406 return 1;
407 }
408 }
409
410
418 public function delete($user, $notrigger = 0)
419 {
420 global $conf, $langs;
421 $error = 0;
422
423 dol_syslog(get_class($this)."::delete");
424
425 $this->db->begin();
426
427 if ($this->bank_line > 0) {
428 $accline = new AccountLine($this->db);
429 $accline->fetch($this->bank_line);
430 $result = $accline->delete();
431 if ($result < 0) {
432 $this->errors[] = $accline->error;
433 $error++;
434 }
435 }
436
437 if (!$error) {
438 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_vat";
439 $sql .= " WHERE rowid=".((int) $this->id);
440
441 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
442 $resql = $this->db->query($sql);
443 if (!$resql) {
444 $error++;
445 $this->errors[] = "Error ".$this->db->lasterror();
446 }
447 }
448
449 // Commit or rollback
450 if ($error) {
451 foreach ($this->errors as $errmsg) {
452 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
453 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
454 }
455 $this->db->rollback();
456 return -1 * $error;
457 } else {
458 $this->db->commit();
459 return 1;
460 }
461 }
462
463
464
472 public function createFromClone(User $user, $fromid)
473 {
474 $error = 0;
475
476 $object = new PaymentSocialContribution($this->db);
477
478 $this->db->begin();
479
480 // Load source object
481 $object->fetch($fromid);
482 $object->id = 0;
483 $object->statut = 0;
484
485 // Clear fields
486 // ...
487
488 // Create clone
489 $object->context['createfromclone'] = 'createfromclone';
490 $result = $object->create($user);
491
492 // Other options
493 if ($result < 0) {
494 $this->error = $object->error;
495 $error++;
496 }
497
498 unset($object->context['createfromclone']);
499
500 // End
501 if (!$error) {
502 $this->db->commit();
503 return $object->id;
504 } else {
505 $this->db->rollback();
506 return -1;
507 }
508 }
509
510
518 public function initAsSpecimen()
519 {
520 $this->id = 0;
521
522 $this->fk_tva = 0;
523 $this->datec = '';
524 $this->tms = '';
525 $this->datep = '';
526 $this->amount = '';
527 $this->fk_typepaiement = '';
528 $this->num_payment = '';
529 $this->note_private = '';
530 $this->note_public = '';
531 $this->fk_bank = 0;
532 $this->fk_user_creat = 0;
533 $this->fk_user_modif = 0;
534 }
535
536
549 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
550 {
551 global $conf;
552
553 // Clean data
554 $this->num_payment = trim($this->num_payment ? $this->num_payment : $this->num_paiement);
555
556 $error = 0;
557
558 if (isModEnabled("banque")) {
559 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
560
561 $acc = new Account($this->db);
562 $acc->fetch($accountid);
563
565 if ($mode == 'payment_vat') {
566 $total = -$total;
567 }
568
569 // Insert payment into llx_bank
570 $bank_line_id = $acc->addline(
571 $this->datepaye,
572 $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example")
573 $label,
574 $total,
575 $this->num_payment,
576 '',
577 $user,
578 $emetteur_nom,
579 $emetteur_banque
580 );
581
582 // Mise a jour fk_bank dans llx_paiement.
583 // On connait ainsi le paiement qui a genere l'ecriture bancaire
584 if ($bank_line_id > 0) {
585 $result = $this->update_fk_bank($bank_line_id);
586 if ($result <= 0) {
587 $error++;
588 dol_print_error($this->db);
589 }
590
591 // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
592 $url = '';
593 if ($mode == 'payment_vat') {
594 $url = DOL_URL_ROOT.'/compta/payment_vat/card.php?id=';
595 }
596 if ($url) {
597 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
598 if ($result <= 0) {
599 $error++;
600 dol_print_error($this->db);
601 }
602 }
603
604 // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
605 $linkaddedforthirdparty = array();
606 foreach ($this->amounts as $key => $value) {
607 if ($mode == 'payment_vat') {
608 $tva = new Tva($this->db);
609 $tva->fetch($key);
610 $result = $acc->add_url_line($bank_line_id, $tva->id, DOL_URL_ROOT.'/compta/tva/card.php?id=', '('.$tva->label.')', 'vat');
611 if ($result <= 0) {
612 dol_print_error($this->db);
613 }
614 }
615 }
616 } else {
617 $this->error = $acc->error;
618 $error++;
619 }
620 }
621
622 if (!$error) {
623 return 1;
624 } else {
625 return -1;
626 }
627 }
628
629
630 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
637 public function update_fk_bank($id_bank)
638 {
639 // phpcs:enable
640 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_vat SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
641
642 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
643 $result = $this->db->query($sql);
644 if ($result) {
645 return 1;
646 } else {
647 $this->error = $this->db->error();
648 return 0;
649 }
650 }
651
652
659 public function getLibStatut($mode = 0)
660 {
661 return $this->LibStatut($this->statut, $mode);
662 }
663
664 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
672 public function LibStatut($status, $mode = 0)
673 {
674 // phpcs:enable
675 global $langs;
676
677 $langs->load('compta');
678 /*if ($mode == 0)
679 {
680 if ($status == 0) return $langs->trans('ToValidate');
681 if ($status == 1) return $langs->trans('Validated');
682 }
683 if ($mode == 1)
684 {
685 if ($status == 0) return $langs->trans('ToValidate');
686 if ($status == 1) return $langs->trans('Validated');
687 }
688 if ($mode == 2)
689 {
690 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
691 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
692 }
693 if ($mode == 3)
694 {
695 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
696 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
697 }
698 if ($mode == 4)
699 {
700 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
701 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
702 }
703 if ($mode == 5)
704 {
705 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
706 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
707 }
708 if ($mode == 6)
709 {
710 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
711 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
712 }*/
713 return '';
714 }
715
723 public function getNomUrl($withpicto = 0, $maxlen = 0)
724 {
725 global $langs;
726
727 $result = '';
728
729 if (empty($this->ref)) {
730 $this->ref = $this->lib;
731 }
732
733 $label = img_picto('', $this->picto).' <u>'.$langs->trans("VATPayment").'</u>';
734 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
735 if (!empty($this->label)) {
736 $labeltoshow = $this->label;
737 $reg = array();
738 if (preg_match('/^\‍((.*)\‍)$/i', $this->label, $reg)) {
739 // Label generique car entre parentheses. On l'affiche en le traduisant
740 if ($reg[1] == 'paiement') {
741 $reg[1] = 'Payment';
742 }
743 $labeltoshow = $langs->trans($reg[1]);
744 }
745 $label .= '<br><b>'.$langs->trans('Label').':</b> '.$labeltoshow;
746 }
747 if ($this->datep) {
748 $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->datep, 'day');
749 }
750
751 if (!empty($this->id)) {
752 $link = '<a href="'.DOL_URL_ROOT.'/compta/payment_vat/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
753 $linkend = '</a>';
754
755 if ($withpicto) {
756 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
757 }
758 if ($withpicto && $withpicto != 2) {
759 $result .= ' ';
760 }
761 if ($withpicto != 2) {
762 $result .= $link.($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
763 }
764 }
765
766 return $result;
767 }
768}
$object ref
Definition info.php:78
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.
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.
__construct($db)
Constructor.
update_fk_bank($id_bank)
Mise a jour du lien entre le paiement de tva et la ligne dans llx_bank generee.
fetch($id)
Load object in memory from database.
create($user, $closepaidcontrib=0)
Create payment of social contribution into database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
getLibStatut($mode=0)
Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
getNomUrl($withpicto=0, $maxlen=0)
Return clicable 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:36
Class to manage Dolibarr users.
print $langs trans("Ref").' m m m statut
Definition index.php:152
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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...