dolibarr 22.0.5
paiement.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
5 * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
6 * Copyright (C) 2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
7 * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
8 * Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
9 * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
10 * Copyright (C) 2018 Thibault FOUCART <support@ptibogxiv.net>
11 * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
12 * Copyright (C) 2020 Andreu Bisquerra Gaya <jove@bisquerra.com>
13 * Copyright (C) 2021 OpenDsi <support@open-dsi.fr>
14 * Copyright (C) 2023 Joachim Kueter <git-jk@bloxera.com>
15 * Copyright (C) 2023 Sylvain Legrand <technique@infras.fr>
16 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <https://www.gnu.org/licenses/>.
30 */
31
37require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
38require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
39
40
45{
49 public $element = 'payment';
50
54 public $table_element = 'paiement';
55
59 public $picto = 'payment';
60
64 public $facid;
65
69 public $socid;
70
74 public $datepaye;
75
79 public $date;
80
86 public $total;
87
93 public $montant;
94
98 public $amount;
99
103 public $multicurrency_amount;
104
108 public $multicurrency_currency;
109
113 public $amounts = array();
114
118 public $multicurrency_amounts = array();
119
123 public $multicurrency_tx = array();
124
128 public $multicurrency_code = array();
129
133 public $pos_change = 0.0;
134
138 public $author;
139
143 public $paiementid;
144
148 public $paiementcode;
149
153 public $type_label;
154
158 public $type_code;
159
165 public $num_paiement;
166
171 public $num_payment;
172
176 public $id_prelevement;
177
181 public $num_prelevement;
182
186 public $ext_payment_id;
187
191 public $ext_payment_site;
192
198 public $bank_account;
199
203 public $fk_account;
204
208 public $bank_line;
209
210 // fk_paiement dans llx_paiement est l'id du type de paiement (7 pour CHQ, ...)
211 // fk_paiement dans llx_paiement_facture est le rowid du paiement
215 public $fk_paiement; // Type of payment
216
220 public $ref_ext;
221
222
228 public function __construct($db)
229 {
230 $this->db = $db;
231 }
232
241 public function fetch($id, $ref = '', $fk_bank = 0)
242 {
243 $sql = 'SELECT p.rowid, p.ref, p.ref_ext, p.datep as dp, p.amount, p.statut, p.ext_payment_id, p.ext_payment_site, p.fk_bank, p.multicurrency_amount,';
244 $sql .= ' c.code as type_code, c.libelle as type_label,';
245 $sql .= ' p.num_paiement as num_payment, p.note,';
246 $sql .= ' b.fk_account';
247 $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement as p LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id';
248 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid';
249 $sql .= ' WHERE p.entity IN ('.getEntity('invoice').')';
250 if ($id > 0) {
251 $sql .= ' AND p.rowid = '.((int) $id);
252 } elseif ($ref) {
253 $sql .= " AND p.ref = '".$this->db->escape($ref)."'";
254 } elseif ($fk_bank) {
255 $sql .= ' AND p.fk_bank = '.((int) $fk_bank);
256 }
257
258 $resql = $this->db->query($sql);
259 if ($resql) {
260 if ($this->db->num_rows($resql)) {
261 $obj = $this->db->fetch_object($resql);
262
263 $this->id = $obj->rowid;
264 $this->ref = $obj->ref ? $obj->ref : $obj->rowid;
265 $this->ref_ext = $obj->ref_ext;
266 $this->date = $this->db->jdate($obj->dp);
267 $this->datepaye = $this->db->jdate($obj->dp);
268 $this->num_payment = $obj->num_payment;
269 $this->montant = $obj->amount; // deprecated
270 $this->amount = $obj->amount;
271 $this->multicurrency_amount = $obj->multicurrency_amount;
272 $this->note = $obj->note;
273 $this->note_private = $obj->note;
274 $this->type_label = $obj->type_label;
275 $this->type_code = $obj->type_code;
276 $this->statut = $obj->statut;
277 $this->ext_payment_id = $obj->ext_payment_id;
278 $this->ext_payment_site = $obj->ext_payment_site;
279
280 $this->bank_account = $obj->fk_account; // deprecated
281 $this->fk_account = $obj->fk_account;
282 $this->bank_line = $obj->fk_bank;
283
284 $this->db->free($resql);
285 return 1;
286 } else {
287 $this->db->free($resql);
288 return 0;
289 }
290 } else {
291 dol_print_error($this->db);
292 return -1;
293 }
294 }
295
308 public function create($user, $closepaidinvoices = 0, $thirdparty = null)
309 {
310 global $conf, $langs;
311
312 $error = 0;
313 $way = $this->getWay(); // 'dolibarr' to use amount, 'customer' to use foreign multicurrency amount
314
315 $now = dol_now();
316
317 // Clean parameters
318 $totalamount = 0;
319 $totalamount_converted = 0;
320 $atleastonepaymentnotnull = 0;
321
322 if ($way == 'dolibarr') { // Payments were entered into the column of main currency
323 $amounts = &$this->amounts;
324 $amounts_to_update = &$this->multicurrency_amounts;
325 } else { // Payments were entered into the column of foreign currency
326 $amounts = &$this->multicurrency_amounts;
327 $amounts_to_update = &$this->amounts;
328 }
329
330 $currencyofpayment = '';
331 $currencyofinvoices = '';
332 $currencytxofpayment = '';
333
334 foreach ($amounts as $key => $value) { // How payment is dispatched. $key is ID of invoice
335 if (empty($value)) {
336 continue;
337 }
338 $value_converted = false;
339 $tmparray = MultiCurrency::getInvoiceRate($key, 'facture');
340 // getInvoiceRate() returns false when the invoice cannot be read, so the result must
341 // be tested before being indexed, as PaiementFourn::create() already does. Without
342 // this the rate is read from a boolean, which emits a PHP warning and leaves it null,
343 // so no conversion is made and the payment is refused later on with
344 // 'Payment in same currency than company but this->amount != this->multicurrency_amount'.
345 if ($tmparray === false) {
346 $this->error = $langs->trans('FailedToFoundTheConversionRateForInvoice');
347 return -1;
348 }
349 $invoice_multicurrency_tx = $tmparray['invoice_multicurrency_tx'];
350 $invoice_multicurrency_code = $tmparray['invoice_multicurrency_code'];
351
352 // $key is id of invoice, $value is amount, $way is 'dolibarr' if amount is in main currency, 'customer' if in foreign currency
353 if ($invoice_multicurrency_tx) {
354 if ($way == 'dolibarr') {
355 $value_converted = (float) price2num($value * $invoice_multicurrency_tx, 'MU');
356 } else {
357 // A payment that settles the whole remaining amount in the foreign currency must
358 // clear the invoice: dividing by the rate and rounding can leave or take one cent,
359 // which keeps the invoice partially paid, so take the remaining amount directly.
360 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
361 $invoice = new Facture($this->db);
362 $invoice->fetch($key);
363 if ((float) price2num($value, 'MT') == (float) $invoice->getRemainToPay(1)) {
364 $value_converted = (float) $invoice->getRemainToPay(0);
365 } else {
366 $value_converted = (float) price2num($value / $invoice_multicurrency_tx, 'MU');
367 }
368 }
369 } else {
370 $invoice_multicurrency_tx = false;
371 }
372
373 // Add controls of input validity
374 if ($value_converted === false) {
375 // We failed to find the conversion for one invoice
376 $this->error = $langs->trans('FailedToFoundTheConversionRateForInvoice');
377 return -1;
378 }
379
380 // Set the currency of the invoice
381 $currencyofinvoiceforthisline = empty($this->multicurrency_code[$key]) ? $invoice_multicurrency_code : $this->multicurrency_code[$key];
382 // If a payment was entered into the section of the foreign currency of invoice, we want to pay in the currency of invoice
383 $currencyofpaymentforthisline = empty($this->multicurrency_amounts[$key]) ? $conf->currency : $this->multicurrency_code[$key];
384
385 //var_dump("Invoice ID: ".$key.", amount in company cur:".$this->amounts[$key]." amount in invoice cur:".$this->multicurrency_amounts[$key]." => currencyofinvoice= ".$currencyofinvoiceforthisline." - currencyofpaymentforthisline =".$currencyofpaymentforthisline);
386
387 if (empty($currencyofinvoices)) {
388 $currencyofinvoices = $currencyofinvoiceforthisline;
389 } elseif ($currencyofinvoices != $currencyofinvoiceforthisline) {
390 // If we have invoices with different currencies in the payment, we stop here
391 $this->error = 'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
392 return -1;
393 }
394
395 if (empty($currencyofpayment)) {
396 $currencyofpayment = $currencyofpaymentforthisline;
397 } elseif ($currencyofpayment != $currencyofpaymentforthisline) {
398 // If we have invoices with different currencies in the payment, we stop here
399 $this->error = 'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
400 return -1;
401 }
402
403 if (empty($currencytxofpayment)) {
404 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] : "";
405 }
406
407 $totalamount_converted += $value_converted; // Total in currency of the invoice
408 $amounts_to_update[$key] = price2num($value_converted, 'MT');
409
410 $newvalue = price2num($value, 'MT');
411 $amounts[$key] = $newvalue;
412 $totalamount += $newvalue;
413 if (!empty($newvalue)) {
414 $atleastonepaymentnotnull++;
415 }
416
417 //var_dump('currencytxofpayment = '.$currencytxofpayment." totalamount_converted =".$totalamount_converted);
418 //print '<br>';
419 }
420
421 if (empty($currencyofpayment)) { // Should not happen. For the case the multicurrency_code was not saved into invoices
422 $currencyofpayment = $conf->currency;
423 }
424
425 if (!empty($currencyofpayment)) {
426 // We must check that the currency of invoices is the same than the currency of the bank
427 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
428 $bankaccount = new Account($this->db);
429 $bankaccount->fetch($this->fk_account);
430 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
431
432 if ($bankcurrencycode != $conf->currency) {
433 // If we try to pay on a bank with a different currency
434 if ($bankcurrencycode != $currencyofinvoices && $currencyofinvoices != $conf->currency) {
435 $langs->load("errors");
436 $this->error = $langs->trans('ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofinvoices, $bankcurrencycode);
437 return -1;
438 }
439 if ($bankcurrencycode != $currencyofpayment && $currencyofpayment != $conf->currency) {
440 $langs->load("errors");
441 $this->error = $langs->trans('ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
442 return -1;
443 }
444 } else {
445 // No problem in this case
446 }
447 }
448
449 $totalamount = (float) price2num($totalamount, 'MT');
450 $totalamount_converted = (float) price2num($totalamount_converted, 'MT');
451
452 // Check parameters
453 if (empty($totalamount) && empty($atleastonepaymentnotnull)) { // We accept negative amounts for withdraw reject but not empty arrays
454 $this->errors[] = 'TotalAmountEmpty';
455 $this->error = $langs->trans('TotalAmountEmpty');
456 return -1;
457 }
458
459 dol_syslog(get_class($this)."::create insert paiement (closepaidinvoices = ".$closepaidinvoices.")", LOG_DEBUG);
460
461 $this->db->begin();
462
463 $this->ref = $this->getNextNumRef(is_object($thirdparty) ? $thirdparty : '');
464
465 if (empty($this->ref_ext)) {
466 $this->ref_ext = '';
467 }
468
469 if ($way == 'dolibarr') {
470 $total = $totalamount;
471 $mtotal = $totalamount_converted;
472 } else {
473 $total = $totalamount_converted;
474 $mtotal = $totalamount;
475 }
476
477 $num_payment = $this->num_payment;
478 $note = ($this->note_private ? $this->note_private : $this->note);
479
480 $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement (entity, ref, ref_ext, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, ext_payment_id, ext_payment_site, fk_user_creat, pos_change)";
481 $sql .= " VALUES (".((int) $conf->entity).", '".$this->db->escape($this->ref)."', '".$this->db->escape($this->ref_ext)."', '".$this->db->idate($now)."', '".$this->db->idate($this->datepaye)."', ".((float) $total).", ".((float) $mtotal).", ".((int) $this->paiementid).", ";
482 $sql .= "'".$this->db->escape($num_payment)."', '".$this->db->escape($note)."', ".($this->ext_payment_id ? "'".$this->db->escape($this->ext_payment_id)."'" : "null").", ".($this->ext_payment_site ? "'".$this->db->escape($this->ext_payment_site)."'" : "null").", ".((int) $user->id).", ".((float) $this->pos_change).")";
483
484 $resql = $this->db->query($sql);
485 if ($resql) {
486 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'paiement');
487
488 // Insert links amount / invoices
489 foreach ($this->amounts as $key => $amount) {
490 $facid = $key;
491 if (is_numeric($amount) && $amount != 0) {
492 $amount = price2num($amount);
493 $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
494 $sql .= " VALUES (".((int) $facid).", ".((int) $this->id).", ".((float) $amount).", ".((float) $this->multicurrency_amounts[$key]).", ".($currencyofpayment ? "'".$this->db->escape($currencyofpayment)."'" : 'NULL').", ".(!empty($this->multicurrency_tx) ? (float) $currencytxofpayment : 1).")";
495
496 dol_syslog(get_class($this).'::create Amount line '.$key.' insert paiement_facture', LOG_DEBUG);
497 $resql = $this->db->query($sql);
498 if ($resql) {
499 $invoice = new Facture($this->db);
500 $invoice->fetch($facid);
501
502 // If we want to closed paid invoices
503 if ($closepaidinvoices) {
504 $paiement = $invoice->getSommePaiement();
505 $creditnotes = $invoice->getSumCreditNotesUsed();
506 $deposits = $invoice->getSumDepositsUsed();
507 $alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
508 $remaintopay = price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits, 'MT');
509
510 //var_dump($invoice->total_ttc.' - '.$paiement.' -'.$creditnotes.' - '.$deposits.' - '.$remaintopay);exit;
511
512 //Invoice types that are eligible for changing status to paid
513 $affected_types = array(
519 );
520
521 if (!in_array($invoice->type, $affected_types)) {
522 dol_syslog("Invoice ".$facid." is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
523 } elseif ($remaintopay) {
524 // hook to have an option to automatically close a closable invoice with less payment than the total amount (e.g. agreed cash discount terms)
525 global $hookmanager;
526 $hookmanager->initHooks(array('paymentdao'));
527 $parameters = array('facid' => $facid, 'invoice' => $invoice, 'remaintopay' => $remaintopay);
528 $action = 'CLOSEPAIDINVOICE';
529 $reshook = $hookmanager->executeHooks('createPayment', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
530 if ($reshook < 0) {
531 $this->errors[] = $hookmanager->error;
532 $this->error = $hookmanager->error;
533 $error++;
534 } elseif ($reshook == 0) {
535 dol_syslog("Remain to pay for invoice " . $facid . " not null. We do nothing more.");
536 }
537 // } else if ($mustwait) dol_syslog("There is ".$mustwait." differed payment to process, we do nothing more.");
538 } else {
539 // If invoice is a down payment, we also convert down payment to discount
540 if ($invoice->type == Facture::TYPE_DEPOSIT) {
541 $amount_ht = $amount_tva = $amount_ttc = array();
542 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
543 '
544 @phan-var-force array<string,float> $amount_ht
545 @phan-var-force array<string,float> $amount_tva
546 @phan-var-force array<string,float> $amount_ttc
547 @phan-var-force array<string,float> $multicurrency_amount_ht
548 @phan-var-force array<string,float> $multicurrency_amount_tva
549 @phan-var-force array<string,float> $multicurrency_amount_ttc
550 ';
551
552 // Insert one discount by VAT rate category
553 $discount = new DiscountAbsolute($this->db);
554 $discount->fetch(0, $invoice->id);
555 if (empty($discount->id)) { // If the invoice was not yet converted into a discount (this may have been done manually before we come here)
556 $discount->description = '(DEPOSIT)';
557 $discount->fk_soc = $invoice->socid;
558 $discount->socid = $invoice->socid;
559 $discount->fk_facture_source = $invoice->id;
560
561 // Loop on each vat rate
562 $i = 0;
563 foreach ($invoice->lines as $line) {
564 if ($line->product_type != 9 && $line->total_ht != 0) { // no need to create discount if amount is null or is special product
565 if (!array_key_exists($line->tva_tx, $amount_ht)) {
566 $amount_ht[$line->tva_tx] = 0.0;
567 $amount_tva[$line->tva_tx] = 0.0;
568 $amount_ttc[$line->tva_tx] = 0.0;
569 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
570 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
571 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
572 }
573 $amount_ht[$line->tva_tx] += $line->total_ht;
574 $amount_tva[$line->tva_tx] += $line->total_tva;
575 $amount_ttc[$line->tva_tx] += $line->total_ttc;
576 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
577 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
578 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
579 $i++;
580 }
581 }
582
583 foreach ($amount_ht as $tva_tx => $xxx) {
584 $discount->amount_ht = abs($amount_ht[$tva_tx]);
585 $discount->total_ht = abs($amount_ht[$tva_tx]);
586 $discount->amount_tva = abs($amount_tva[$tva_tx]);
587 $discount->total_tva = abs($amount_tva[$tva_tx]);
588 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
589 $discount->total_ttc = abs($amount_ttc[$tva_tx]);
590 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
591 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$tva_tx]);
592 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
593 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$tva_tx]);
594 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
595 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
596 $discount->tva_tx = abs((float) $tva_tx);
597
598 $result = $discount->create($user);
599 if ($result < 0) {
600 $error++;
601 break;
602 }
603 }
604 }
605
606 if ($error) {
607 $this->error = $discount->error;
608 $this->errors = $discount->errors;
609 $error++;
610 }
611 }
612
613 // Set invoice to paid
614 if (!$error) {
615 $invoice->context['actionmsgmore'] = 'Invoice set to paid by the payment->create() of payment '.$this->ref.' because the remain to pay is 0';
616
617 $result = $invoice->setPaid($user, '', '');
618 if ($result < 0) {
619 $this->error = $invoice->error;
620 $this->errors = $invoice->errors;
621 $error++;
622 }
623 }
624 }
625 }
626
627 // Regenerate documents of invoices
628 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
629 dol_syslog(get_class($this).'::create Regenerate the document after inserting payment for thirdparty default_lang='.(is_object($invoice->thirdparty) ? $invoice->thirdparty->default_lang : 'null'), LOG_DEBUG);
630
631 $newlang = '';
632 $outputlangs = $langs;
633 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
634 $invoice->fetch_thirdparty();
635 $newlang = $invoice->thirdparty->default_lang;
636 }
637 if (!empty($newlang)) {
638 $outputlangs = new Translate("", $conf);
639 $outputlangs->setDefaultLang($newlang);
640 }
641
642 $hidedetails = getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 1 : 0;
643 $hidedesc = getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DESC') ? 1 : 0;
644 $hideref = getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_REF') ? 1 : 0;
645
646 $ret = $invoice->fetch($facid); // Reload to get new records
647
648 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
649
650 dol_syslog(get_class($this).'::create Regenerate end result='.$result, LOG_DEBUG);
651
652 if ($result < 0) {
653 $this->error = $invoice->error;
654 $this->errors = $invoice->errors;
655 $error++;
656 }
657 }
658 } else {
659 $this->error = $this->db->lasterror();
660 $error++;
661 }
662 } else {
663 dol_syslog(get_class($this).'::Create Amount line '.$key.' not a number. We discard it.');
664 }
665 }
666
667 dol_syslog(get_class($this).'::create Now we call the triggers if no error (error = '.$error.')', LOG_DEBUG);
668
669 if (!$error) { // All payments into $this->amounts were recorded without errors
670 // Appel des triggers
671 $result = $this->call_trigger('PAYMENT_CUSTOMER_CREATE', $user);
672 if ($result < 0) {
673 $error++;
674 }
675 // Fin appel triggers
676 }
677 } else {
678 $this->error = $this->db->lasterror();
679 $error++;
680 }
681
682 if (!$error) {
683 // Set some properties that may be used by other process after calling the create
684 $this->amount = $total;
685 $this->total = $total; // deprecated
686 $this->multicurrency_amount = $mtotal;
687 $this->multicurrency_currency = $currencyofinvoices;
688
689 $this->db->commit();
690 return $this->id;
691 } else {
692 $this->db->rollback();
693 return -1;
694 }
695 }
696
697
707 public function delete($user, $notrigger = 0)
708 {
709 $bank_line_id = $this->bank_line;
710
711 $this->db->begin();
712
713 // Verifier si paiement porte pas sur une facture classee
714 // Si c'est le cas, on refuse la suppression
715 $billsarray = $this->getBillsArray('f.fk_statut > 1');
716 if (is_array($billsarray)) {
717 if (count($billsarray)) {
718 $this->error = "ErrorDeletePaymentLinkedToAClosedInvoiceNotPossible";
719 $this->db->rollback();
720 return -1;
721 }
722 } else {
723 $this->db->rollback();
724 return -2;
725 }
726
727 // Delete bank urls. If payment is on a conciliated line, return error.
728 if ($bank_line_id > 0) {
729 $accline = new AccountLine($this->db);
730
731 $result = $accline->fetch($bank_line_id);
732 if ($result == 0) {
733 $accline->id = $accline->rowid = $bank_line_id; // If not found, we set artificially rowid to allow delete of llx_bank_url
734 }
735
736 // Delete bank account url lines linked to payment
737 $result = $accline->delete_urls($user);
738 if ($result < 0) {
739 $this->error = $accline->error;
740 $this->db->rollback();
741 return -3;
742 }
743
744 // Delete bank account lines linked to payment
745 $result = $accline->delete($user);
746 if ($result < 0) {
747 $this->error = $accline->error;
748 $this->db->rollback();
749 return -4;
750 }
751 }
752
753 if (!$notrigger) {
754 // Call triggers
755 $result = $this->call_trigger('PAYMENT_CUSTOMER_DELETE', $user);
756 if ($result < 0) {
757 $this->db->rollback();
758 return -1;
759 }
760 // End call triggers
761 }
762
763 // Delete payment (into paiement_facture and paiement)
764 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement_facture';
765 $sql .= ' WHERE fk_paiement = '.((int) $this->id);
766 dol_syslog($sql);
767 $result = $this->db->query($sql);
768 if ($result) {
769 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement';
770 $sql .= " WHERE rowid = ".((int) $this->id);
771 dol_syslog($sql);
772 $result = $this->db->query($sql);
773 if (!$result) {
774 $this->error = $this->db->lasterror();
775 $this->db->rollback();
776 return -3;
777 }
778
779 $this->db->commit();
780 return 1;
781 } else {
782 $this->error = $this->db->error;
783 $this->db->rollback();
784 return -5;
785 }
786 }
787
788
804 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode = '', $addbankurl = '')
805 {
806 global $conf, $user;
807
808 $error = 0;
809 $bank_line_id = 0;
810
811 // Note: ->amount (amount in company currency) ant multicurrency_amount (amount in )was set by the ->create beforecalling this.
812 // The create had also checked that currency of payment is same than currency of bank account
813
814 if (isModEnabled("bank")) {
815 if ($accountid <= 0) {
816 $this->error = 'Bad value for parameter accountid='.$accountid;
817 dol_syslog(get_class($this).'::addPaymentToBank '.$this->error, LOG_ERR);
818 return -1;
819 }
820
821 $this->fk_account = $accountid;
822
823 dol_syslog("addPaymentToBank ".$user->id.", ".$mode.", ".$label.", ".$this->fk_account.", ".$emetteur_nom.", ".$emetteur_banque);
824
825 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
826 $acc = new Account($this->db);
827 $result = $acc->fetch($this->fk_account);
828 if ($result < 0) {
829 $this->error = $acc->error;
830 $this->errors = $acc->errors;
831 $error++;
832 return -1;
833 }
834
835 $this->db->begin();
836
837 $totalamount = $this->amount;
838 $totalamount_main_currency = null;
839 if (empty($totalamount)) {
840 $totalamount = $this->total; // For backward compatibility
841 }
842
843 // this->amount is amount of payment in company currency
844 // this->multicurrency_amount of payment in other currency
845 // this->multicurrency_currency is the currency of the payment (may be same than the company one)
846 if ($this->multicurrency_currency == $conf->currency) {
847 if ($this->amount != $this->multicurrency_amount) {
848 // Add protection, should not happen
849 $error++;
850 $this->error = 'Payment in same currency than company but this->amount != this->multicurrency_amount';
851 }
852 }
853
854 // if company currency != bank currency, then we received an amount in customer currency (currently I don't manage the case : my currency is USD, the customer currency is EUR and he paid me in GBP. Seems no sense for me)
855 if ($conf->currency != $acc->currency_code) {
856 $totalamount = $this->multicurrency_amount; // We will insert into llx_bank.amount in foreign currency of invoice
857 $totalamount_main_currency = $this->amount; // We will also save the amount in main currency into column llx_bank.amount_main_currency
858 }
859
860 if ($mode == 'payment_supplier') {
861 $totalamount = -$totalamount;
862 if (isset($totalamount_main_currency)) {
863 $totalamount_main_currency = -$totalamount_main_currency;
864 }
865 }
866
867 // Insert payment into llx_bank
868 $bank_line_id = $acc->addline(
869 $this->datepaye,
870 $this->paiementcode ? $this->paiementcode : $this->paiementid, // Payment mode code ('CB', 'CHQ' or 'VIR' for example). Use payment id if not defined for backward compatibility.
871 $label,
872 $totalamount, // Sign must be positive when we receive money (customer payment), negative when you give money (supplier invoice or credit note)
873 $this->num_payment,
874 0,
875 $user,
876 $emetteur_nom,
877 $emetteur_banque,
878 $accountancycode,
879 0,
880 '',
881 (float) $totalamount_main_currency
882 );
883
884 // Mise a jour fk_bank dans llx_paiement
885 // On connait ainsi le paiement qui a genere l'ecriture bancaire
886 if ($bank_line_id > 0) {
887 $result = $this->update_fk_bank($bank_line_id);
888 if ($result <= 0) {
889 $error++;
890 dol_print_error($this->db);
891 }
892
893 // Add link 'payment', 'payment_supplier' in bank_url between payment and bank transaction
894 if (!$error) {
895 $url = '';
896 if ($mode == 'payment') {
897 $url = DOL_URL_ROOT.'/compta/paiement/card.php?id=';
898 }
899 if ($mode == 'payment_supplier') {
900 $url = DOL_URL_ROOT.'/fourn/paiement/card.php?id=';
901 }
902 if ($url) {
903 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
904 if ($result <= 0) {
905 $error++;
906 dol_print_error($this->db);
907 }
908 }
909 }
910
911 // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
912 if (!$error) {
913 $linkaddedforthirdparty = array();
914 foreach ($this->amounts as $key => $value) { // We should have invoices always for same third party but we loop in case of.
915 // Skip the invoices that were listed but not paid, the same way create() does. Without this,
916 // a 'company' link is added to the bank line for a third party that received no payment.
917 if (!is_numeric($value) || $value == 0) {
918 continue;
919 }
920 if ($mode == 'payment') {
921 $fac = new Facture($this->db);
922 $fac->fetch($key);
923 $fac->fetch_thirdparty();
924 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) { // Not yet done for this thirdparty @phan-suppress-current-line PhanPossiblyUndeclaredVariable
925 $result = $acc->add_url_line(
926 $bank_line_id,
927 $fac->thirdparty->id,
928 DOL_URL_ROOT.'/comm/card.php?socid=',
929 (string) $fac->thirdparty->name,
930 'company'
931 );
932 if ($result <= 0) {
933 $error++;
934 dol_syslog(get_class($this).'::addPaymentToBank '.$this->db->lasterror());
935 }
936 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id; // Mark as done for this thirdparty
937 }
938 }
939 if ($mode == 'payment_supplier') {
940 $fac = new FactureFournisseur($this->db);
941 $fac->fetch($key);
942 $fac->fetch_thirdparty();
943 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) { // Not yet done for this thirdparty
944 $result = $acc->add_url_line(
945 $bank_line_id,
946 $fac->thirdparty->id,
947 DOL_URL_ROOT.'/fourn/card.php?socid=',
948 (string) $fac->thirdparty->name,
949 'company'
950 );
951 if ($result <= 0) {
952 $error++;
953 dol_syslog(get_class($this).'::addPaymentToBank '.$this->db->lasterror());
954 }
955 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id; // Mark as done for this thirdparty
956 }
957 }
958 }
959 }
960
961 // Add a link to the Direct Debit ('direct-debit') or Credit transfer ('credit-transfer') file in bank_url
962 if (!$error && $addbankurl && in_array($addbankurl, array('direct-debit', 'credit-transfer'))) {
963 $result = $acc->add_url_line(
964 $bank_line_id,
965 $this->id_prelevement,
966 DOL_URL_ROOT.'/compta/prelevement/card.php?id=',
967 $this->num_payment,
968 $addbankurl
969 );
970 }
971
972 // Add link to the Direct Debit if invoice refused ('InvoiceRefused') in bank_url
973 if (!$error && $label == '(InvoiceRefused)') {
974 $result = $acc->add_url_line(
975 $bank_line_id,
976 $this->id_prelevement,
977 DOL_URL_ROOT.'/compta/prelevement/card.php?id=',
978 $this->num_prelevement,
979 'withdraw'
980 );
981 }
982
983 if (!$error && !$notrigger) {
984 // Appel des triggers
985 $result = $this->call_trigger('PAYMENT_ADD_TO_BANK', $user);
986 if ($result < 0) {
987 $error++;
988 }
989 // Fin appel triggers
990 }
991 } else {
992 $this->error = $acc->error;
993 $this->errors = $acc->errors;
994 $error++;
995 }
996
997 if (!$error) {
998 $this->db->commit();
999 } else {
1000 $this->db->rollback();
1001 }
1002 }
1003
1004 if (!$error) {
1005 return $bank_line_id;
1006 } else {
1007 return -1;
1008 }
1009 }
1010
1011
1012 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1019 public function update_fk_bank($id_bank)
1020 {
1021 // phpcs:enable
1022 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' set fk_bank = '.((int) $id_bank);
1023 $sql .= " WHERE rowid = ".((int) $this->id);
1024
1025 dol_syslog(get_class($this).'::update_fk_bank', LOG_DEBUG);
1026 $result = $this->db->query($sql);
1027 if ($result) {
1028 return 1;
1029 } else {
1030 $this->error = $this->db->lasterror();
1031 dol_syslog(get_class($this).'::update_fk_bank '.$this->error);
1032 return -1;
1033 }
1034 }
1035
1036 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1043 public function update_date($date)
1044 {
1045 // phpcs:enable
1046 $error = 0;
1047
1048 if (!empty($date) && $this->statut != 1) {
1049 $this->db->begin();
1050
1051 dol_syslog(get_class($this)."::update_date with date = ".$date, LOG_DEBUG);
1052
1053 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1054 $sql .= " SET datep = '".$this->db->idate($date)."'";
1055 $sql .= " WHERE rowid = ".((int) $this->id);
1056
1057 $result = $this->db->query($sql);
1058 if (!$result) {
1059 $error++;
1060 $this->error = 'Error -1 '.$this->db->error();
1061 }
1062
1063 $type = $this->element;
1064
1065 $sql = "UPDATE ".MAIN_DB_PREFIX.'bank';
1066 $sql .= " SET dateo = '".$this->db->idate($date)."', datev = '".$this->db->idate($date)."'";
1067 $sql .= " WHERE rowid IN (SELECT fk_bank FROM ".MAIN_DB_PREFIX."bank_url WHERE type = '".$this->db->escape($type)."' AND url_id = ".((int) $this->id).")";
1068 $sql .= " AND rappro = 0";
1069
1070 $result = $this->db->query($sql);
1071 if (!$result) {
1072 $error++;
1073 $this->error = 'Error -1 '.$this->db->error();
1074 }
1075
1076 if (!$error) {
1077 $this->datepaye = $date;
1078 $this->date = $date;
1079
1080 $this->db->commit();
1081 return 0;
1082 } else {
1083 $this->db->rollback();
1084 return -2;
1085 }
1086 }
1087 return -1; //no date given or already validated
1088 }
1089
1090 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1097 public function update_num($num_payment)
1098 {
1099 // phpcs:enable
1100 if (!empty($num_payment) && $this->statut != 1) {
1101 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1102 $sql .= " SET num_paiement = '".$this->db->escape($num_payment)."'";
1103 $sql .= " WHERE rowid = ".((int) $this->id);
1104
1105 dol_syslog(get_class($this)."::update_num", LOG_DEBUG);
1106 $result = $this->db->query($sql);
1107 if ($result) {
1108 $this->num_payment = $this->db->escape($num_payment);
1109 return 0;
1110 } else {
1111 $this->error = 'Error -1 '.$this->db->error();
1112 return -2;
1113 }
1114 }
1115 return -1; //no num given or already validated
1116 }
1117
1125 public function valide($user = null)
1126 {
1127 return $this->validate($user);
1128 }
1129
1136 public function validate($user = null)
1137 {
1138 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET statut = 1 WHERE rowid = '.((int) $this->id);
1139
1140 dol_syslog(get_class($this).'::valide', LOG_DEBUG);
1141 $result = $this->db->query($sql);
1142 if ($result) {
1143 return 1;
1144 } else {
1145 $this->error = $this->db->lasterror();
1146 dol_syslog(get_class($this).'::valide '.$this->error);
1147 return -1;
1148 }
1149 }
1150
1157 public function reject($user = null)
1158 {
1159 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET statut = 2 WHERE rowid = '.((int) $this->id);
1160
1161 dol_syslog(get_class($this).'::reject', LOG_DEBUG);
1162 $result = $this->db->query($sql);
1163 if ($result) {
1164 return 1;
1165 } else {
1166 $this->error = $this->db->lasterror();
1167 dol_syslog(get_class($this).'::reject '.$this->error);
1168 return -1;
1169 }
1170 }
1171
1178 public function info($id)
1179 {
1180 $sql = 'SELECT p.rowid, p.datec, p.fk_user_creat, p.fk_user_modif, p.tms';
1181 $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement as p';
1182 $sql .= ' WHERE p.rowid = '.((int) $id);
1183
1184 dol_syslog(get_class($this).'::info', LOG_DEBUG);
1185 $result = $this->db->query($sql);
1186
1187 if ($result) {
1188 if ($this->db->num_rows($result)) {
1189 $obj = $this->db->fetch_object($result);
1190
1191 $this->id = $obj->rowid;
1192
1193 $this->user_creation_id = $obj->fk_user_creat;
1194 $this->user_modification_id = $obj->fk_user_modif;
1195 $this->date_creation = $this->db->jdate($obj->datec);
1196 $this->date_modification = $this->db->jdate($obj->tms);
1197 }
1198 $this->db->free($result);
1199 } else {
1200 dol_print_error($this->db);
1201 }
1202 }
1203
1211 public function getBillsArray($filter = '')
1212 {
1213 $sql = 'SELECT pf.fk_facture';
1214 $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'facture as f'; // We keep link on invoice to allow use of some filters on invoice
1215 $sql .= ' WHERE pf.fk_facture = f.rowid AND pf.fk_paiement = '.((int) $this->id);
1216 if ($filter) {
1217 $sql .= ' AND '.$filter;
1218 }
1219 $resql = $this->db->query($sql);
1220 if ($resql) {
1221 $i = 0;
1222 $num = $this->db->num_rows($resql);
1223 $billsarray = array();
1224
1225 while ($i < $num) {
1226 $obj = $this->db->fetch_object($resql);
1227 $billsarray[$i] = $obj->fk_facture;
1228 $i++;
1229 }
1230
1231 return $billsarray;
1232 } else {
1233 $this->error = $this->db->error();
1234 dol_syslog(get_class($this).'::getBillsArray Error '.$this->error.' -', LOG_DEBUG);
1235 return -1;
1236 }
1237 }
1238
1245 public function getAmountsArray()
1246 {
1247 $sql = 'SELECT pf.fk_facture, pf.amount';
1248 $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf';
1249 $sql .= ' WHERE pf.fk_paiement = '.((int) $this->id);
1250 $resql = $this->db->query($sql);
1251 if ($resql) {
1252 $i = 0;
1253 $num = $this->db->num_rows($resql);
1254 $amounts = array();
1255
1256 while ($i < $num) {
1257 $obj = $this->db->fetch_object($resql);
1258 $amounts[$obj->fk_facture] = $obj->amount;
1259 $i++;
1260 }
1261
1262 return $amounts;
1263 } else {
1264 $this->error = $this->db->error();
1265 dol_syslog(get_class($this).'::getAmountsArray Error '.$this->error.' -', LOG_DEBUG);
1266 return -1;
1267 }
1268 }
1269
1278 public function getNextNumRef($soc, $mode = 'next')
1279 {
1280 global $conf, $db, $langs;
1281 $langs->load("bills");
1282
1283 // Clean parameters (if not defined or using deprecated value)
1284 if (!getDolGlobalString('PAYMENT_ADDON')) {
1285 $conf->global->PAYMENT_ADDON = 'mod_payment_cicada';
1286 } elseif (getDolGlobalString('PAYMENT_ADDON') == 'ant') {
1287 $conf->global->PAYMENT_ADDON = 'mod_payment_ant';
1288 } elseif (getDolGlobalString('PAYMENT_ADDON') == 'cicada') {
1289 $conf->global->PAYMENT_ADDON = 'mod_payment_cicada';
1290 }
1291
1292 if (getDolGlobalString('PAYMENT_ADDON')) {
1293 $mybool = false;
1294
1295 $file = getDolGlobalString('PAYMENT_ADDON') . ".php";
1296 $classname = getDolGlobalString('PAYMENT_ADDON');
1297
1298 // Include file with class
1299 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
1300
1301 foreach ($dirmodels as $reldir) {
1302 $dir = dol_buildpath($reldir."core/modules/payment/");
1303
1304 // Load file with numbering class (if found)
1305 if (is_file($dir.$file) && is_readable($dir.$file)) {
1306 $mybool = (include_once $dir.$file) || $mybool;
1307 }
1308 }
1309
1310 // For compatibility
1311 if (!$mybool) {
1312 $file = getDolGlobalString('PAYMENT_ADDON') . ".php";
1313 $classname = "mod_payment_" . getDolGlobalString('PAYMENT_ADDON');
1314 $classname = preg_replace('/\-.*$/', '', $classname);
1315 // Include file with class
1316 foreach ($conf->file->dol_document_root as $dirroot) {
1317 $dir = $dirroot."/core/modules/payment/";
1318
1319 // Load file with numbering class (if found)
1320 if (is_file($dir.$file) && is_readable($dir.$file)) {
1321 $mybool = (include_once $dir.$file) || $mybool;
1322 }
1323 }
1324 }
1325
1326 if (!$mybool) {
1327 dol_print_error(null, "Failed to include file ".$file);
1328 return '';
1329 }
1330
1331 $obj = new $classname();
1332 '@phan-var-force ModeleNumRefPayments $obj';
1333
1334 $numref = $obj->getNextValue($soc, $this);
1335
1340 if ($mode != 'last' && !$numref) {
1341 dol_print_error($db, "Payment::getNextNumRef ".$obj->error);
1342 return "";
1343 }
1344
1345 return $numref;
1346 } else {
1347 $langs->load("errors");
1348 print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Invoice"));
1349 return "";
1350 }
1351 }
1352
1358 public function getWay()
1359 {
1360 $way = 'dolibarr';
1361 if (isModEnabled('multicurrency')) {
1362 foreach ($this->multicurrency_amounts as $value) {
1363 if (!empty($value)) { // one value found into multicurrency_amounts so payment is in invoice currency
1364 $way = 'customer';
1365 break;
1366 }
1367 }
1368 }
1369
1370 return $way;
1371 }
1372
1381 public function initAsSpecimen($option = '')
1382 {
1383 global $user, $langs, $conf;
1384
1385 $now = dol_now();
1386 $arraynow = dol_getdate($now);
1387 $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
1388
1389 // Initialize parameters
1390 $this->id = 0;
1391 $this->ref = 'SPECIMEN';
1392 $this->specimen = 1;
1393 $this->facid = 1;
1394 $this->datepaye = $nownotime;
1395
1396 return 1;
1397 }
1398
1399
1410 public function getNomUrl($withpicto = 0, $option = '', $mode = 'withlistofinvoices', $notooltip = 0, $morecss = '')
1411 {
1412 global $conf, $langs, $hookmanager;
1413
1414 if (!empty($conf->dol_no_mouse_hover)) {
1415 $notooltip = 1; // Force disable tooltips
1416 }
1417
1418 $result = '';
1419
1420 $label = img_picto('', $this->picto).' <u>'.$langs->trans("Payment").'</u><br>';
1421 $label .= '<strong>'.$langs->trans("Ref").':</strong> '.$this->ref;
1422 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1423 if ($dateofpayment) {
1424 $label .= '<br><strong>'.$langs->trans("Date").':</strong> ';
1425 $tmparray = dol_getdate($dateofpayment);
1426 if ($tmparray['seconds'] == 0 && $tmparray['minutes'] == 0 && ($tmparray['hours'] == 0 || $tmparray['hours'] == 12)) { // We set hours to 0:00 or 12:00 because we don't know it
1427 $label .= dol_print_date($dateofpayment, 'day');
1428 } else { // Hours was set to real date of payment (special case for POS for example)
1429 $label .= dol_print_date($dateofpayment, 'dayhour', 'tzuser');
1430 }
1431 }
1432 if ($this->amount) {
1433 $label .= '<br><strong>'.$langs->trans("Amount").':</strong> '.price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
1434 }
1435 if ($mode == 'withlistofinvoices') {
1436 $arraybill = $this->getBillsArray();
1437 if (is_array($arraybill) && count($arraybill) > 0) {
1438 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1439 $facturestatic = new Facture($this->db);
1440 foreach ($arraybill as $billid) {
1441 $facturestatic->fetch($billid);
1442 $label .= '<br> '.$facturestatic->getNomUrl(1, '', 0, 0, '', 1).' '.$facturestatic->getLibStatut(2, -1);
1443 }
1444 }
1445 }
1446
1447 $linkclose = '';
1448 if (empty($notooltip)) {
1449 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1450 $label = $langs->trans("Payment");
1451 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1452 }
1453 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
1454 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1455 } else {
1456 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1457 }
1458
1459 $url = DOL_URL_ROOT.'/compta/paiement/card.php?id='.$this->id;
1460
1461 $linkstart = '<a href="'.$url.'"';
1462 $linkstart .= $linkclose.'>';
1463 $linkend = '</a>';
1464
1465 $result .= $linkstart;
1466 if ($withpicto) {
1467 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1468 }
1469 if ($withpicto && $withpicto != 2) {
1470 $result .= ($this->ref ? $this->ref : $this->id);
1471 }
1472 $result .= $linkend;
1473 global $action;
1474 $hookmanager->initHooks(array($this->element . 'dao'));
1475 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1476 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1477 if ($reshook > 0) {
1478 $result = $hookmanager->resPrint;
1479 } else {
1480 $result .= $hookmanager->resPrint;
1481 }
1482 return $result;
1483 }
1484
1491 public function getLibStatut($mode = 0)
1492 {
1493 return $this->LibStatut($this->statut, $mode);
1494 }
1495
1496 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1504 public function LibStatut($status, $mode = 0)
1505 {
1506 // phpcs:enable
1507 global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
1508
1509 $langs->load('compta');
1510 /*if ($mode == 0)
1511 {
1512 if ($status == 0) return $langs->trans('ToValidate');
1513 if ($status == 1) return $langs->trans('Validated');
1514 }
1515 if ($mode == 1)
1516 {
1517 if ($status == 0) return $langs->trans('ToValidate');
1518 if ($status == 1) return $langs->trans('Validated');
1519 }
1520 if ($mode == 2)
1521 {
1522 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
1523 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
1524 }
1525 if ($mode == 3)
1526 {
1527 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
1528 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
1529 }
1530 if ($mode == 4)
1531 {
1532 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
1533 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
1534 }
1535 if ($mode == 5)
1536 {
1537 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
1538 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
1539 }
1540 if ($mode == 6)
1541 {
1542 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
1543 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
1544 }*/
1545 return '';
1546 }
1547
1548 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1556 public function fetch_thirdparty($force_thirdparty_id = 0)
1557 {
1558 // phpcs:enable
1559 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1560
1561 if (empty($force_thirdparty_id)) {
1562 $billsarray = $this->getBillsArray(); // From payment, the fk_soc isn't available, we should load the first supplier invoice to get him
1563 if (!empty($billsarray)) {
1564 $invoice = new Facture($this->db);
1565 if ($invoice->fetch($billsarray[0]) > 0) {
1566 $force_thirdparty_id = $invoice->socid;
1567 }
1568 }
1569 }
1570
1571 return parent::fetch_thirdparty($force_thirdparty_id);
1572 }
1573
1574
1580 public function isReconciled()
1581 {
1582 $accountline = new AccountLine($this->db);
1583 $accountline->fetch($this->bank_line);
1584 return $accountline->rappro ? true : false;
1585 }
1586}
$object ref
Definition info.php:90
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 absolute discounts.
Class to manage suppliers invoices.
Class to manage invoices.
const TYPE_REPLACEMENT
Replacement invoice.
const TYPE_STANDARD
Standard invoice.
const TYPE_SITUATION
Situation invoice.
const TYPE_DEPOSIT
Deposit invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
static getInvoiceRate($fk_facture, $table='facture')
Get current invoite rate.
Class to manage payments of customer invoices.
addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger=0, $accountancycode='', $addbankurl='')
Add a record into bank for payment + links between this bank record and sources of payment.
__construct($db)
Constructor.
update_num($num_payment)
Updates the payment number.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id into this->thirdparty.
create($user, $closepaidinvoices=0, $thirdparty=null)
Create payment of invoices into database.
getAmountsArray()
Return list of amounts of payments.
getNomUrl($withpicto=0, $option='', $mode='withlistofinvoices', $notooltip=0, $morecss='')
Return clickable name (with picto eventually)
isReconciled()
Return if payment is reconciled.
fetch($id, $ref='', $fk_bank=0)
Load payment from database.
update_fk_bank($id_bank)
Update the link between the Payment and the line generated in llx_bank.
getWay()
get the right way of payment
validate($user=null)
Validate payment.
reject($user=null)
Reject payment.
initAsSpecimen($option='')
Initialise an instance with random values.
LibStatut($status, $mode=0)
Return the label of a given status.
update_date($date)
Updates the payment date.
getNextNumRef($soc, $mode='next')
Return next reference of customer invoice not already used (or last reference) according to numbering...
getBillsArray($filter='')
Return list of invoices the payment is related to.
valide($user=null)
Validate payment.
info($id)
Information sur l'objet.
getLibStatut($mode=0)
Return the label of the status.
Class to manage translations.
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...
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
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 '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79