dolibarr 19.0.3
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 $type_code;
120
124 public $type_label;
125
129 public $bank_account;
130
134 public $bank_line;
135
139 public $paiementtype;
140
146 public function __construct($db)
147 {
148 $this->db = $db;
149 }
150
159 public function create($user, $closepaidvat = 0)
160 {
161 global $conf, $langs;
162
163 $error = 0;
164
165 $now = dol_now();
166
167 dol_syslog(get_class($this)."::create", LOG_DEBUG);
168
169 // Validate parametres
170 if (!$this->datepaye) {
171 $this->error = 'ErrorBadValueForParameterCreatePaymentVAT';
172 return -1;
173 }
174
175 // Clean parameters
176 if (isset($this->fk_tva)) {
177 $this->fk_tva = (int) $this->fk_tva;
178 }
179 if (isset($this->amount)) {
180 $this->amount = trim($this->amount);
181 }
182 if (isset($this->fk_typepaiement)) {
183 $this->fk_typepaiement = (int) $this->fk_typepaiement;
184 }
185 if (isset($this->num_paiement)) {
186 $this->num_paiement = trim($this->num_paiement); // deprecated
187 }
188 if (isset($this->num_payment)) {
189 $this->num_payment = trim($this->num_payment);
190 }
191 if (isset($this->note)) {
192 $this->note = trim($this->note);
193 }
194 if (isset($this->fk_bank)) {
195 $this->fk_bank = (int) $this->fk_bank;
196 }
197 if (isset($this->fk_user_creat)) {
198 $this->fk_user_creat = (int) $this->fk_user_creat;
199 }
200 if (isset($this->fk_user_modif)) {
201 $this->fk_user_modif = (int) $this->fk_user_modif;
202 }
203
204 $totalamount = 0;
205 foreach ($this->amounts as $key => $value) { // How payment is dispatch
206 $newvalue = price2num($value, 'MT');
207 $this->amounts[$key] = $newvalue;
208 $totalamount += $newvalue;
209 }
210 $totalamount = price2num($totalamount);
211
212 // Check parameters
213 if ($totalamount == 0) {
214 return -1; // We accept negative amounts for chargebacks, but not null amounts.
215 }
216
217
218 $this->db->begin();
219
220 if ($totalamount != 0) {
221 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_vat (fk_tva, datec, datep, amount,";
222 $sql .= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
223 $sql .= " VALUES ($this->chid, '".$this->db->idate($now)."',";
224 $sql .= " '".$this->db->idate($this->datepaye)."',";
225 $sql .= " ".((float) $totalamount).",";
226 $sql .= " ".((int) $this->paiementtype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".$user->id.",";
227 $sql .= " 0)";
228
229 $resql = $this->db->query($sql);
230 if ($resql) {
231 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_vat");
232
233 // Insert table of amounts / invoices
234 foreach ($this->amounts as $key => $amount) {
235 $contribid = $key;
236 if (is_numeric($amount) && $amount != 0) {
237 $amount = price2num($amount);
238
239 // If we want to closed paid invoices
240 if ($closepaidvat) {
241 $contrib = new Tva($this->db);
242 $contrib->fetch($contribid);
243 $paiement = $contrib->getSommePaiement();
244 //$creditnotes=$contrib->getSumCreditNotesUsed();
245 $creditnotes = 0;
246 //$deposits=$contrib->getSumDepositsUsed();
247 $deposits = 0;
248 $alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
249 $remaintopay = price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT');
250 if ($remaintopay == 0) {
251 $result = $contrib->setPaid($user);
252 } else {
253 dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
254 }
255 }
256 }
257 }
258 } else {
259 $error++;
260 }
261 }
262
263 $result = $this->call_trigger('PAYMENTVAT_CREATE', $user);
264 if ($result < 0) {
265 $error++;
266 }
267
268 if ($totalamount != 0 && !$error) {
269 $this->amount = $totalamount;
270 $this->total = $totalamount; // deprecated
271 $this->db->commit();
272 return $this->id;
273 } else {
274 $this->error = $this->db->error();
275 $this->db->rollback();
276 return -1;
277 }
278 }
279
286 public function fetch($id)
287 {
288 $sql = "SELECT";
289 $sql .= " t.rowid,";
290 $sql .= " t.fk_tva,";
291 $sql .= " t.datec,";
292 $sql .= " t.tms,";
293 $sql .= " t.datep,";
294 $sql .= " t.amount,";
295 $sql .= " t.fk_typepaiement,";
296 $sql .= " t.num_paiement as num_payment,";
297 $sql .= " t.note as note_private,";
298 $sql .= " t.fk_bank,";
299 $sql .= " t.fk_user_creat,";
300 $sql .= " t.fk_user_modif,";
301 $sql .= " pt.code as type_code, pt.libelle as type_label,";
302 $sql .= ' b.fk_account';
303 $sql .= " FROM ".MAIN_DB_PREFIX."payment_vat as t LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepaiement = pt.id";
304 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
305 $sql .= " WHERE t.rowid = ".((int) $id);
306 // TODO link on entity of tax;
307
308 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
309 $resql = $this->db->query($sql);
310 if ($resql) {
311 if ($this->db->num_rows($resql)) {
312 $obj = $this->db->fetch_object($resql);
313
314 $this->id = $obj->rowid;
315 $this->ref = $obj->rowid;
316
317 $this->fk_tva = $obj->fk_tva;
318 $this->datec = $this->db->jdate($obj->datec);
319 $this->tms = $this->db->jdate($obj->tms);
320 $this->datep = $this->db->jdate($obj->datep);
321 $this->amount = $obj->amount;
322 $this->fk_typepaiement = $obj->fk_typepaiement;
323 $this->num_paiement = $obj->num_payment;
324 $this->num_payment = $obj->num_payment;
325 $this->note = $obj->note_private;
326 $this->note_private = $obj->note_private;
327 $this->fk_bank = $obj->fk_bank;
328 $this->fk_user_creat = $obj->fk_user_creat;
329 $this->fk_user_modif = $obj->fk_user_modif;
330
331 $this->type_code = $obj->type_code;
332 $this->type_label = $obj->type_label;
333
334 $this->bank_account = $obj->fk_account;
335 $this->bank_line = $obj->fk_bank;
336 }
337 $this->db->free($resql);
338
339 return 1;
340 } else {
341 $this->error = "Error ".$this->db->lasterror();
342 return -1;
343 }
344 }
345
346
354 public function update($user = null, $notrigger = 0)
355 {
356 global $conf, $langs;
357 $error = 0;
358
359 // Clean parameters
360
361 if (isset($this->fk_tva)) {
362 $this->fk_tva = (int) $this->fk_tva;
363 }
364 if (isset($this->amount)) {
365 $this->amount = trim($this->amount);
366 }
367 if (isset($this->fk_typepaiement)) {
368 $this->fk_typepaiement = (int) $this->fk_typepaiement;
369 }
370 if (isset($this->num_payment)) {
371 $this->num_payment = trim($this->num_payment);
372 }
373 if (isset($this->note)) {
374 $this->note = trim($this->note);
375 }
376 if (isset($this->fk_bank)) {
377 $this->fk_bank = (int) $this->fk_bank;
378 }
379 if (isset($this->fk_user_creat)) {
380 $this->fk_user_creat = (int) $this->fk_user_creat;
381 }
382 if (isset($this->fk_user_modif)) {
383 $this->fk_user_modif = (int) $this->fk_user_modif;
384 }
385
386 // Check parameters
387 // Put here code to add control on parameters values
388
389 // Update request
390 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_vat SET";
391 $sql .= " fk_tva=".(isset($this->fk_tva) ? ((int) $this->fk_tva) : "null").",";
392 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
393 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
394 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
395 $sql .= " amount=".(isset($this->amount) ? (float) price2num($this->amount) : "null").",";
396 $sql .= " fk_typepaiement=".(isset($this->fk_typepaiement) ? ((int) $this->fk_typepaiement) : "null").",";
397 $sql .= " num_paiement=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
398 $sql .= " note=".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : "null").",";
399 $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
400 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
401 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
402 $sql .= " WHERE rowid=".((int) $this->id);
403
404 $this->db->begin();
405
406 dol_syslog(get_class($this)."::update", LOG_DEBUG);
407 $resql = $this->db->query($sql);
408 if (!$resql) {
409 $error++;
410 $this->errors[] = "Error ".$this->db->lasterror();
411 }
412
413 // Commit or rollback
414 if ($error) {
415 foreach ($this->errors as $errmsg) {
416 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
417 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
418 }
419 $this->db->rollback();
420 return -1 * $error;
421 } else {
422 $this->db->commit();
423 return 1;
424 }
425 }
426
427
435 public function delete($user, $notrigger = 0)
436 {
437 $error = 0;
438
439 dol_syslog(get_class($this)."::delete");
440
441 $this->db->begin();
442
443 if ($this->bank_line > 0) {
444 $accline = new AccountLine($this->db);
445 $accline->fetch($this->bank_line);
446 $result = $accline->delete($user);
447 if ($result < 0) {
448 $this->errors[] = $accline->error;
449 $error++;
450 }
451 }
452
453 if (!$error) {
454 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_vat";
455 $sql .= " WHERE rowid=".((int) $this->id);
456
457 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
458 $resql = $this->db->query($sql);
459 if (!$resql) {
460 $error++;
461 $this->errors[] = "Error ".$this->db->lasterror();
462 }
463 }
464
465 // Commit or rollback
466 if ($error) {
467 foreach ($this->errors as $errmsg) {
468 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
469 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
470 }
471 $this->db->rollback();
472 return -1 * $error;
473 } else {
474 $this->db->commit();
475 return 1;
476 }
477 }
478
479
480
488 public function createFromClone(User $user, $fromid)
489 {
490 $error = 0;
491
492 $object = new PaymentVAT($this->db);
493
494 $this->db->begin();
495
496 // Load source object
497 $object->fetch($fromid);
498 $object->id = 0;
499 $object->statut = 0;
500
501 // Clear fields
502
503 // Create clone
504 $object->context['createfromclone'] = 'createfromclone';
505 $result = $object->create($user);
506
507 // Other options
508 if ($result < 0) {
509 $this->error = $object->error;
510 $error++;
511 }
512
513 unset($object->context['createfromclone']);
514
515 // End
516 if (!$error) {
517 $this->db->commit();
518 return $object->id;
519 } else {
520 $this->db->rollback();
521 return -1;
522 }
523 }
524
525
533 public function initAsSpecimen()
534 {
535 $this->id = 0;
536
537 $this->fk_tva = 0;
538 $this->datec = '';
539 $this->tms = '';
540 $this->datep = '';
541 $this->amount = '';
542 $this->fk_typepaiement = '';
543 $this->num_payment = '';
544 $this->note_private = '';
545 $this->note_public = '';
546 $this->fk_bank = 0;
547 $this->fk_user_creat = 0;
548 $this->fk_user_modif = 0;
549 }
550
551
564 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
565 {
566 // Clean data
567 $this->num_payment = trim($this->num_payment ? $this->num_payment : $this->num_paiement);
568
569 $error = 0;
570
571 if (isModEnabled("banque")) {
572 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
573
574 $acc = new Account($this->db);
575 $acc->fetch($accountid);
576
578 if ($mode == 'payment_vat') {
579 $total = -$total;
580 }
581
582 // Insert payment into llx_bank
583 $bank_line_id = $acc->addline(
584 $this->datepaye,
585 $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example")
586 $label,
587 $total,
588 $this->num_payment,
589 '',
590 $user,
591 $emetteur_nom,
592 $emetteur_banque
593 );
594
595 // Update fk_bank in llx_paiement.
596 // We thus know the payment that generated the bank entry
597 if ($bank_line_id > 0) {
598 $result = $this->update_fk_bank($bank_line_id);
599 if ($result <= 0) {
600 $error++;
601 dol_print_error($this->db);
602 }
603
604 // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
605 $url = '';
606 if ($mode == 'payment_vat') {
607 $url = DOL_URL_ROOT.'/compta/payment_vat/card.php?id=';
608 }
609 if ($url) {
610 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
611 if ($result <= 0) {
612 $error++;
613 dol_print_error($this->db);
614 }
615 }
616
617 // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
618 $linkaddedforthirdparty = array();
619 foreach ($this->amounts as $key => $value) {
620 if ($mode == 'payment_vat') {
621 $tva = new Tva($this->db);
622 $tva->fetch($key);
623 $result = $acc->add_url_line($bank_line_id, $tva->id, DOL_URL_ROOT.'/compta/tva/card.php?id=', '('.$tva->label.')', 'vat');
624 if ($result <= 0) {
625 dol_print_error($this->db);
626 }
627 }
628 }
629 } else {
630 $this->error = $acc->error;
631 $error++;
632 }
633 }
634
635 if (!$error) {
636 return 1;
637 } else {
638 return -1;
639 }
640 }
641
642
643 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
650 public function update_fk_bank($id_bank)
651 {
652 // phpcs:enable
653 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_vat SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
654
655 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
656 $result = $this->db->query($sql);
657 if ($result) {
658 return 1;
659 } else {
660 $this->error = $this->db->error();
661 return 0;
662 }
663 }
664
665
672 public function getLibStatut($mode = 0)
673 {
674 return $this->LibStatut($this->statut, $mode);
675 }
676
677 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
685 public function LibStatut($status, $mode = 0)
686 {
687 // phpcs:enable
688 global $langs;
689
690 $langs->load('compta');
691 /*if ($mode == 0)
692 {
693 if ($status == 0) return $langs->trans('ToValidate');
694 if ($status == 1) return $langs->trans('Validated');
695 }
696 if ($mode == 1)
697 {
698 if ($status == 0) return $langs->trans('ToValidate');
699 if ($status == 1) return $langs->trans('Validated');
700 }
701 if ($mode == 2)
702 {
703 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
704 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
705 }
706 if ($mode == 3)
707 {
708 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
709 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
710 }
711 if ($mode == 4)
712 {
713 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
714 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
715 }
716 if ($mode == 5)
717 {
718 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
719 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
720 }
721 if ($mode == 6)
722 {
723 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
724 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
725 }*/
726 return '';
727 }
728
736 public function getNomUrl($withpicto = 0, $maxlen = 0)
737 {
738 global $langs;
739
740 $result = '';
741
742 if (empty($this->ref)) {
743 $this->ref = $this->lib;
744 }
745
746 $label = img_picto('', $this->picto).' <u>'.$langs->trans("VATPayment").'</u>';
747 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
748 if (!empty($this->label)) {
749 $labeltoshow = $this->label;
750 $reg = array();
751 if (preg_match('/^\‍((.*)\‍)$/i', $this->label, $reg)) {
752 // Label generique car entre parentheses. On l'affiche en le traduisant
753 if ($reg[1] == 'paiement') {
754 $reg[1] = 'Payment';
755 }
756 $labeltoshow = $langs->trans($reg[1]);
757 }
758 $label .= '<br><b>'.$langs->trans('Label').':</b> '.$labeltoshow;
759 }
760 if ($this->datep) {
761 $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->datep, 'day');
762 }
763
764 if (!empty($this->id)) {
765 $link = '<a href="'.DOL_URL_ROOT.'/compta/payment_vat/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
766 $linkend = '</a>';
767
768 if ($withpicto) {
769 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
770 }
771 if ($withpicto && $withpicto != 2) {
772 $result .= ' ';
773 }
774 if ($withpicto != 2) {
775 $result .= $link.($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
776 }
777 }
778
779 return $result;
780 }
781}
$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:36
Class to manage Dolibarr users.
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...
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e e e e e statut
Definition invoice.php:1926