dolibarr 23.0.4
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-2025 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->fetch_optionals();
285 $this->db->free($resql);
286 return 1;
287 } else {
288 $this->db->free($resql);
289 return 0;
290 }
291 } else {
292 dol_print_error($this->db);
293 return -1;
294 }
295 }
296
309 public function create($user, $closepaidinvoices = 0, $thirdparty = null)
310 {
311 global $conf, $langs;
312
313 $error = 0;
314 $way = $this->getWay(); // 'dolibarr' to use amount, 'customer' to use foreign multicurrency amount
315
316 $now = dol_now();
317
318 // Clean parameters
319 $totalamount = 0;
320 $totalamount_converted = 0;
321 $atleastonepaymentnotnull = 0;
322
323 if ($way == 'dolibarr') { // Payments were entered into the column of main currency
324 $amounts = &$this->amounts;
325 $amounts_to_update = &$this->multicurrency_amounts;
326 } else { // Payments were entered into the column of foreign currency
327 $amounts = &$this->multicurrency_amounts;
328 $amounts_to_update = &$this->amounts;
329 }
330
331 $currencyofpayment = '';
332 $currencyofinvoices = '';
333 $currencytxofpayment = '';
334
335 foreach ($amounts as $key => $value) { // How payment is dispatched. $key is ID of invoice
336 if (empty($value)) {
337 continue;
338 }
339 $value_converted = false;
340 $tmparray = MultiCurrency::getInvoiceRate($key, 'facture');
341 // getInvoiceRate() returns false when the invoice cannot be read, so the result must
342 // be tested before being indexed, as PaiementFourn::create() already does. Without
343 // this the rate is read from a boolean, which emits a PHP warning and leaves it null,
344 // so no conversion is made and the payment is refused later on with
345 // 'Payment in same currency than company but this->amount != this->multicurrency_amount'.
346 if ($tmparray === false) {
347 $this->error = $langs->trans('FailedToFoundTheConversionRateForInvoice');
348 return -1;
349 }
350 $invoice_multicurrency_tx = $tmparray['invoice_multicurrency_tx'];
351 $invoice_multicurrency_code = $tmparray['invoice_multicurrency_code'];
352
353 // $key is id of invoice, $value is amount, $way is 'dolibarr' if amount is in main currency, 'customer' if in foreign currency
354 if ($invoice_multicurrency_tx) {
355 if ($way == 'dolibarr') {
356 $value_converted = (float) price2num($value * $invoice_multicurrency_tx, 'MU');
357 } else {
358 // A payment that settles the whole remaining amount in the foreign currency must
359 // clear the invoice: dividing by the rate and rounding can leave or take one cent,
360 // which keeps the invoice partially paid, so take the remaining amount directly.
361 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
362 $invoice = new Facture($this->db);
363 $invoice->fetch($key);
364 if ((float) price2num($value, 'MT') == (float) $invoice->getRemainToPay(1)) {
365 $value_converted = (float) $invoice->getRemainToPay(0);
366 } else {
367 $value_converted = (float) price2num($value / $invoice_multicurrency_tx, 'MU');
368 }
369 }
370 } else {
371 $invoice_multicurrency_tx = false;
372 }
373
374 // Add controls of input validity
375 if ($value_converted === false) {
376 // We failed to find the conversion for one invoice
377 $this->error = $langs->trans('FailedToFoundTheConversionRateForInvoice');
378 return -1;
379 }
380
381 // Set the currency of the invoice
382 $currencyofinvoiceforthisline = empty($this->multicurrency_code[$key]) ? $invoice_multicurrency_code : $this->multicurrency_code[$key];
383 // If a payment was entered into the section of the foreign currency of invoice, we want to pay in the currency of invoice
384 $currencyofpaymentforthisline = empty($this->multicurrency_amounts[$key]) ? $conf->currency : $this->multicurrency_code[$key];
385
386 //var_dump("Invoice ID: ".$key.", amount in company cur:".$this->amounts[$key]." amount in invoice cur:".$this->multicurrency_amounts[$key]." => currencyofinvoice= ".$currencyofinvoiceforthisline." - currencyofpaymentforthisline =".$currencyofpaymentforthisline);
387
388 if (empty($currencyofinvoices)) {
389 $currencyofinvoices = $currencyofinvoiceforthisline;
390 } elseif ($currencyofinvoices != $currencyofinvoiceforthisline) {
391 // If we have invoices with different currencies in the payment, we stop here
392 $this->error = 'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
393 return -1;
394 }
395
396 if (empty($currencyofpayment)) {
397 $currencyofpayment = $currencyofpaymentforthisline;
398 } elseif ($currencyofpayment != $currencyofpaymentforthisline) {
399 // If we have invoices with different currencies in the payment, we stop here
400 $this->error = 'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
401 return -1;
402 }
403
404 if (empty($currencytxofpayment)) {
405 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] : "";
406 }
407
408 $totalamount_converted += $value_converted; // Total in currency of the invoice
409 $amounts_to_update[$key] = price2num($value_converted, 'MT');
410
411 $newvalue = price2num($value, 'MT');
412 $amounts[$key] = $newvalue;
413 $totalamount += $newvalue;
414 if (!empty($newvalue)) {
415 $atleastonepaymentnotnull++;
416 }
417
418 //var_dump('currencytxofpayment = '.$currencytxofpayment." totalamount_converted =".$totalamount_converted);
419 //print '<br>';
420 }
421
422 if (empty($currencyofpayment)) { // Should not happen. For the case the multicurrency_code was not saved into invoices
423 $currencyofpayment = $conf->currency;
424 }
425
426 if (!empty($currencyofpayment && !empty($this->fk_account))) {
427 // We must check that the currency of invoices is the same than the currency of the bank
428 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
429 $bankaccount = new Account($this->db);
430 $bankaccount->fetch((int) $this->fk_account);
431 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
432
433 if ($bankcurrencycode != $conf->currency) {
434 // If we try to pay on a bank with a different currency
435 if ($bankcurrencycode != $currencyofinvoices && $currencyofinvoices != $conf->currency) {
436 $langs->load("errors");
437 $this->error = $langs->trans('ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofinvoices, $bankcurrencycode);
438 return -1;
439 }
440 if ($bankcurrencycode != $currencyofpayment && $currencyofpayment != $conf->currency) {
441 $langs->load("errors");
442 $this->error = $langs->trans('ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
443 return -1;
444 }
445 } else {
446 // No problem in this case
447 }
448 }
449
450 $totalamount = (float) price2num($totalamount, 'MT');
451 $totalamount_converted = (float) price2num($totalamount_converted, 'MT');
452
453 // Check parameters
454 if (empty($totalamount) && empty($atleastonepaymentnotnull)) { // We accept negative amounts for withdraw reject but not empty arrays
455 $this->errors[] = 'TotalAmountEmpty';
456 $this->error = $langs->trans('TotalAmountEmpty');
457 return -1;
458 }
459
460 dol_syslog(get_class($this)."::create insert paiement (closepaidinvoices = ".$closepaidinvoices.")", LOG_DEBUG);
461
462 $this->db->begin();
463
464 $this->ref = $this->getNextNumRef(is_object($thirdparty) ? $thirdparty : '');
465
466 if (empty($this->ref_ext)) {
467 $this->ref_ext = '';
468 }
469
470 if ($way == 'dolibarr') {
471 $total = $totalamount;
472 $mtotal = $totalamount_converted;
473 } else {
474 $total = $totalamount_converted;
475 $mtotal = $totalamount;
476 }
477
478 $num_payment = $this->num_payment;
479 $note = ($this->note_private ? $this->note_private : $this->note);
480
481 $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)";
482 $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).", ";
483 $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).")";
484
485 $resql = $this->db->query($sql);
486 if ($resql) {
487 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'paiement');
488
489 // Insert links amount / invoices
490 foreach ($this->amounts as $key => $amount) {
491 $facid = $key;
492 if (is_numeric($amount) && $amount != 0) {
493 $amount = price2num($amount);
494 $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
495 $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).")";
496
497 dol_syslog(get_class($this).'::create Amount line '.$key.' insert paiement_facture', LOG_DEBUG);
498 $resql = $this->db->query($sql);
499 if ($resql) {
500 $invoice = new Facture($this->db);
501 $invoice->fetch($facid);
502
503 // If we want to closed paid invoices
504 if ($closepaidinvoices) {
505 $paiement = $invoice->getSommePaiement();
506 $creditnotes = $invoice->getSumCreditNotesUsed();
507 $deposits = $invoice->getSumDepositsUsed();
508 $alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
509 $remaintopay = price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits, 'MT');
510
511 //var_dump($invoice->total_ttc.' - '.$paiement.' -'.$creditnotes.' - '.$deposits.' - '.$remaintopay);exit;
512
513 //Invoice types that are eligible for changing status to paid
514 $affected_types = array(
520 );
521
522 if (!in_array($invoice->type, $affected_types)) {
523 dol_syslog("Invoice ".$facid." is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
524 } elseif ($remaintopay) {
525 // hook to have an option to automatically close a closable invoice with less payment than the total amount (e.g. agreed cash discount terms)
526 global $hookmanager;
527 $hookmanager->initHooks(array('paymentdao'));
528 $parameters = array('facid' => $facid, 'invoice' => $invoice, 'remaintopay' => $remaintopay);
529 $action = 'CLOSEPAIDINVOICE';
530 $reshook = $hookmanager->executeHooks('createPayment', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
531 if ($reshook < 0) {
532 $this->errors[] = $hookmanager->error;
533 $this->error = $hookmanager->error;
534 $error++;
535 } elseif ($reshook == 0) {
536 dol_syslog("Remain to pay for invoice " . $facid . " not null. We do nothing more.");
537 }
538 // } else if ($mustwait) dol_syslog("There is ".$mustwait." differed payment to process, we do nothing more.");
539 } else {
540 // If invoice is a down payment, we also convert down payment to discount
541 if ($invoice->type == Facture::TYPE_DEPOSIT) {
542 $amount_ht = $amount_tva = $amount_ttc = array();
543 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
544 '
545 @phan-var-force array<string,float> $amount_ht
546 @phan-var-force array<string,float> $amount_tva
547 @phan-var-force array<string,float> $amount_ttc
548 @phan-var-force array<string,float> $multicurrency_amount_ht
549 @phan-var-force array<string,float> $multicurrency_amount_tva
550 @phan-var-force array<string,float> $multicurrency_amount_ttc
551 ';
552
553 // Insert one discount by VAT rate category
554 $discount = new DiscountAbsolute($this->db);
555 $discount->fetch(0, $invoice->id);
556 if (empty($discount->id)) { // If the invoice was not yet converted into a discount (this may have been done manually before we come here)
557 $discount->description = '(DEPOSIT)';
558 $discount->fk_soc = $invoice->socid;
559 $discount->socid = $invoice->socid;
560 $discount->fk_facture_source = $invoice->id;
561
562 // Loop on each vat rate
563 // Bucket by tva_tx and vat_src_code so the generated discount keeps the source VAT code
564 $i = 0;
565 foreach ($invoice->lines as $line) {
566 if ($line->product_type != 9 && $line->total_ht != 0) { // no need to create discount if amount is null or is special product
567 $key = $line->tva_tx.'|'.(string) $line->vat_src_code;
568 if (!array_key_exists($key, $amount_ht)) {
569 $amount_ht[$key] = 0.0;
570 $amount_tva[$key] = 0.0;
571 $amount_ttc[$key] = 0.0;
572 $multicurrency_amount_ht[$key] = 0.0;
573 $multicurrency_amount_tva[$key] = 0.0;
574 $multicurrency_amount_ttc[$key] = 0.0;
575 }
576 $amount_ht[$key] += $line->total_ht;
577 $amount_tva[$key] += $line->total_tva;
578 $amount_ttc[$key] += $line->total_ttc;
579 $multicurrency_amount_ht[$key] += $line->multicurrency_total_ht;
580 $multicurrency_amount_tva[$key] += $line->multicurrency_total_tva;
581 $multicurrency_amount_ttc[$key] += $line->multicurrency_total_ttc;
582 $i++;
583 }
584 }
585
586 foreach ($amount_ht as $key => $xxx) {
587 $parts = explode('|', (string) $key, 2);
588 $tva_tx = $parts[0];
589 $vat_src_code = isset($parts[1]) ? $parts[1] : '';
590 $discount->amount_ht = abs($amount_ht[$key]);
591 $discount->total_ht = abs($amount_ht[$key]);
592 $discount->amount_tva = abs($amount_tva[$key]);
593 $discount->total_tva = abs($amount_tva[$key]);
594 $discount->amount_ttc = abs($amount_ttc[$key]);
595 $discount->total_ttc = abs($amount_ttc[$key]);
596 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$key]);
597 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$key]);
598 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$key]);
599 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$key]);
600 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$key]);
601 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$key]);
602 $discount->tva_tx = abs((float) $tva_tx);
603 $discount->vat_src_code = $vat_src_code;
604
605 $result = $discount->create($user);
606 if ($result < 0) {
607 $error++;
608 break;
609 }
610 }
611 }
612
613 if ($error) {
614 $this->error = $discount->error;
615 $this->errors = $discount->errors;
616 $error++;
617 }
618 }
619
620 // Set invoice to paid
621 if (!$error) {
622 $invoice->context['actionmsgmore'] = 'Invoice set to paid by the payment->create() of payment '.$this->ref.' because the remain to pay is 0';
623
624 $result = $invoice->setPaid($user, '', '');
625 if ($result < 0) {
626 $this->error = $invoice->error;
627 $this->errors = $invoice->errors;
628 $error++;
629 }
630 }
631 }
632 }
633
634 // Regenerate documents of invoices
635 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
636 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);
637
638 $newlang = '';
639 $outputlangs = $langs;
640 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
641 $invoice->fetch_thirdparty();
642 $newlang = $invoice->thirdparty->default_lang;
643 }
644 if (!empty($newlang)) {
645 $outputlangs = new Translate("", $conf);
646 $outputlangs->setDefaultLang($newlang);
647 }
648
649 $hidedetails = getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 1 : 0;
650 $hidedesc = getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DESC') ? 1 : 0;
651 $hideref = getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_REF') ? 1 : 0;
652
653 $ret = $invoice->fetch($facid); // Reload to get new records
654
655 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
656
657 dol_syslog(get_class($this).'::create Regenerate end result='.$result, LOG_DEBUG);
658
659 $this->warnings = $invoice->warnings;
660
661 if ($result < 0) {
662 $this->error = $invoice->error;
663 $this->errors = $invoice->errors;
664 $error++;
665 }
666 }
667 } else {
668 $this->error = $this->db->lasterror();
669 $error++;
670 }
671 } else {
672 dol_syslog(get_class($this).'::Create Amount line '.$key.' not a number. We discard it.');
673 }
674 }
675
676 dol_syslog(get_class($this).'::create Now we call the triggers if no error (error = '.$error.')', LOG_DEBUG);
677
678 if (!$error) { // All payments into $this->amounts were recorded without errors
679 // Appel des triggers
680 $result = $this->call_trigger('PAYMENT_CUSTOMER_CREATE', $user);
681 if ($result < 0) {
682 $error++;
683 }
684 // Fin appel triggers
685 }
686 } else {
687 $this->error = $this->db->lasterror();
688 $error++;
689 }
690
691 if (!$error) {
692 // Set some properties that may be used by other process after calling the create
693 $this->amount = $total;
694 $this->total = $total; // deprecated
695 $this->multicurrency_amount = $mtotal;
696 $this->multicurrency_currency = $currencyofinvoices;
697
698 $this->db->commit();
699 return $this->id;
700 } else {
701 $this->db->rollback();
702 return -1;
703 }
704 }
705
706
716 public function delete($user, $notrigger = 0)
717 {
718 $bank_line_id = $this->bank_line;
719
720 $this->db->begin();
721
722 // Verifier si paiement porte pas sur une facture classee
723 // Si c'est le cas, on refuse la suppression
724 $billsarray = $this->getBillsArray('f.fk_statut > 1');
725 if (is_array($billsarray)) {
726 if (count($billsarray)) {
727 $this->error = "ErrorDeletePaymentLinkedToAClosedInvoiceNotPossible";
728 $this->db->rollback();
729 return -1;
730 }
731 } else {
732 $this->db->rollback();
733 return -2;
734 }
735
736 // Delete bank urls. If payment is on a conciliated line, return error.
737 if ($bank_line_id > 0) {
738 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
739 $accline = new AccountLine($this->db);
740
741 $result = $accline->fetch($bank_line_id);
742 if ($result == 0) {
743 $accline->id = $accline->rowid = $bank_line_id; // If not found, we set artificially rowid to allow delete of llx_bank_url
744 }
745
746 // Delete bank account url lines linked to payment
747 $result = $accline->delete_urls($user);
748 if ($result < 0) {
749 $this->error = $accline->error;
750 $this->db->rollback();
751 return -3;
752 }
753
754 // Delete bank account lines linked to payment
755 $result = $accline->delete($user);
756 if ($result < 0) {
757 $this->error = $accline->error;
758 $this->db->rollback();
759 return -4;
760 }
761 }
762
763 if (!$notrigger) {
764 // Call triggers
765 $result = $this->call_trigger('PAYMENT_CUSTOMER_DELETE', $user);
766 if ($result < 0) {
767 $this->db->rollback();
768 return -1;
769 }
770 // End call triggers
771 }
772
773 $this->deleteExtraFields();
774 // Delete payment (into paiement_facture and paiement)
775 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement_facture';
776 $sql .= ' WHERE fk_paiement = '.((int) $this->id);
777 dol_syslog($sql);
778 $result = $this->db->query($sql);
779 if ($result) {
780 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement';
781 $sql .= " WHERE rowid = ".((int) $this->id);
782 dol_syslog($sql);
783 $result = $this->db->query($sql);
784 if (!$result) {
785 $this->error = $this->db->lasterror();
786 $this->db->rollback();
787 return -3;
788 }
789
790 $this->db->commit();
791 return 1;
792 } else {
793 $this->error = $this->db->error;
794 $this->db->rollback();
795 return -5;
796 }
797 }
798
799
815 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode = '', $addbankurl = '')
816 {
817 global $conf, $user;
818
819 $error = 0;
820 $bank_line_id = 0;
821
822 // Note: ->amount (amount in company currency) ant multicurrency_amount (amount in )was set by the ->create beforecalling this.
823 // The create had also checked that currency of payment is same than currency of bank account
824
825 if (isModEnabled("bank")) {
826 if ($accountid <= 0) {
827 $this->error = 'Bad value for parameter accountid='.$accountid;
828 dol_syslog(get_class($this).'::addPaymentToBank '.$this->error, LOG_ERR);
829 return -1;
830 }
831
832 $this->fk_account = $accountid;
833
834 dol_syslog("addPaymentToBank ".$user->id.", ".$mode.", ".$label.", ".$this->fk_account.", ".$emetteur_nom.", ".$emetteur_banque);
835
836 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
837 $acc = new Account($this->db);
838 $result = $acc->fetch($this->fk_account);
839 if ($result < 0) {
840 $this->error = $acc->error;
841 $this->errors = $acc->errors;
842 $error++;
843 return -1;
844 }
845
846 $this->db->begin();
847
848 $totalamount = $this->amount;
849 $totalamount_main_currency = null;
850 if (empty($totalamount)) {
851 $totalamount = $this->total; // For backward compatibility
852 }
853
854 // this->amount is amount of payment in company currency
855 // this->multicurrency_amount of payment in other currency
856 // this->multicurrency_currency is the currency of the payment (may be same than the company one)
857 if ($this->multicurrency_currency == $conf->currency) {
858 if ($this->amount != $this->multicurrency_amount) {
859 // Add protection, should not happen
860 $error++;
861 $this->error = 'Payment in same currency than company but this->amount != this->multicurrency_amount';
862 }
863 }
864
865 // 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)
866 if ($conf->currency != $acc->currency_code) {
867 $totalamount = $this->multicurrency_amount; // We will insert into llx_bank.amount in foreign currency of invoice
868 $totalamount_main_currency = $this->amount; // We will also save the amount in main currency into column llx_bank.amount_main_currency
869 }
870
871 if ($mode == 'payment_supplier') {
872 $totalamount = -$totalamount;
873 if (isset($totalamount_main_currency)) {
874 $totalamount_main_currency = -$totalamount_main_currency;
875 }
876 }
877
878 // Insert payment into llx_bank
879 $bank_line_id = $acc->addline(
880 $this->datepaye,
881 $this->paiementcode ? $this->paiementcode : $this->paiementid, // Payment mode code ('CB', 'CHQ' or 'VIR' for example). Use payment id if not defined for backward compatibility.
882 $label,
883 $totalamount, // Sign must be positive when we receive money (customer payment), negative when you give money (supplier invoice or credit note)
884 $this->num_payment,
885 0,
886 $user,
887 $emetteur_nom,
888 $emetteur_banque,
889 $accountancycode,
890 0,
891 '',
892 (float) $totalamount_main_currency
893 );
894
895 // Mise a jour fk_bank dans llx_paiement
896 // On connait ainsi le paiement qui a genere l'ecriture bancaire
897 if ($bank_line_id > 0) {
898 $result = $this->update_fk_bank($bank_line_id);
899 if ($result <= 0) {
900 $error++;
901 dol_print_error($this->db);
902 }
903
904 // Add link 'payment', 'payment_supplier' in bank_url between payment and bank transaction
905 if (!$error) {
906 $url = '';
907 if ($mode == 'payment') {
908 $url = DOL_URL_ROOT.'/compta/paiement/card.php?id=';
909 }
910 if ($mode == 'payment_supplier') {
911 $url = DOL_URL_ROOT.'/fourn/paiement/card.php?id=';
912 }
913 if ($url) {
914 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
915 if ($result <= 0) {
916 $error++;
917 dol_print_error($this->db);
918 }
919 }
920 }
921
922 // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
923 if (!$error) {
924 $linkaddedforthirdparty = array();
925 foreach ($this->amounts as $key => $value) { // We should have invoices always for same third party but we loop in case of.
926 // Skip the invoices that were listed but not paid, the same way create() does. Without this,
927 // a 'company' link is added to the bank line for a third party that received no payment.
928 if (!is_numeric($value) || $value == 0) {
929 continue;
930 }
931 if ($mode == 'payment') {
932 $fac = new Facture($this->db);
933 $fac->fetch($key);
934 $fac->fetch_thirdparty();
935 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) { // Not yet done for this thirdparty @phan-suppress-current-line PhanPossiblyUndeclaredVariable
936 $result = $acc->add_url_line(
937 $bank_line_id,
938 $fac->thirdparty->id,
939 DOL_URL_ROOT.'/comm/card.php?socid=',
940 (string) $fac->thirdparty->name,
941 'company'
942 );
943 if ($result <= 0) {
944 $error++;
945 dol_syslog(get_class($this).'::addPaymentToBank '.$this->db->lasterror());
946 }
947 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id; // Mark as done for this thirdparty
948 }
949 }
950 if ($mode == 'payment_supplier') {
951 $fac = new FactureFournisseur($this->db);
952 $fac->fetch($key);
953 $fac->fetch_thirdparty();
954 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) { // Not yet done for this thirdparty
955 $result = $acc->add_url_line(
956 $bank_line_id,
957 $fac->thirdparty->id,
958 DOL_URL_ROOT.'/fourn/card.php?socid=',
959 (string) $fac->thirdparty->name,
960 'company'
961 );
962 if ($result <= 0) {
963 $error++;
964 dol_syslog(get_class($this).'::addPaymentToBank '.$this->db->lasterror());
965 }
966 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id; // Mark as done for this thirdparty
967 }
968 }
969 }
970 }
971
972 // Add a link to the Direct Debit ('direct-debit') or Credit transfer ('credit-transfer') file in bank_url
973 if (!$error && $addbankurl && in_array($addbankurl, array('direct-debit', 'credit-transfer'))) {
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_payment,
979 $addbankurl
980 );
981 }
982
983 // Add link to the Direct Debit if invoice refused ('InvoiceRefused') in bank_url
984 if (!$error && $label == '(InvoiceRefused)') {
985 $result = $acc->add_url_line(
986 $bank_line_id,
987 $this->id_prelevement,
988 DOL_URL_ROOT.'/compta/prelevement/card.php?id=',
989 $this->num_prelevement,
990 'withdraw'
991 );
992 }
993
994 if (!$error && !$notrigger) {
995 // Appel des triggers
996 $result = $this->call_trigger('PAYMENT_ADD_TO_BANK', $user);
997 if ($result < 0) {
998 $error++;
999 }
1000 // Fin appel triggers
1001 }
1002 } else {
1003 $this->error = $acc->error;
1004 $this->errors = $acc->errors;
1005 $error++;
1006 }
1007
1008 if (!$error) {
1009 $this->db->commit();
1010 } else {
1011 $this->db->rollback();
1012 }
1013 }
1014
1015 if (!$error) {
1016 return $bank_line_id;
1017 } else {
1018 return -1;
1019 }
1020 }
1021
1022
1023 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1030 public function update_fk_bank($id_bank)
1031 {
1032 // phpcs:enable
1033 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' set fk_bank = '.((int) $id_bank);
1034 $sql .= " WHERE rowid = ".((int) $this->id);
1035
1036 dol_syslog(get_class($this).'::update_fk_bank', LOG_DEBUG);
1037 $result = $this->db->query($sql);
1038 if ($result) {
1039 return 1;
1040 } else {
1041 $this->error = $this->db->lasterror();
1042 dol_syslog(get_class($this).'::update_fk_bank '.$this->error);
1043 return -1;
1044 }
1045 }
1046
1047 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1054 public function update_date($date)
1055 {
1056 // phpcs:enable
1057 $error = 0;
1058
1059 if (!empty($date) && $this->statut != 1) {
1060 $this->db->begin();
1061
1062 dol_syslog(get_class($this)."::update_date with date = ".$date, LOG_DEBUG);
1063
1064 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1065 $sql .= " SET datep = '".$this->db->idate($date)."'";
1066 $sql .= " WHERE rowid = ".((int) $this->id);
1067
1068 $result = $this->db->query($sql);
1069 if (!$result) {
1070 $error++;
1071 $this->error = 'Error -1 '.$this->db->error();
1072 }
1073
1074 $type = $this->element;
1075
1076 $sql = "UPDATE ".MAIN_DB_PREFIX.'bank';
1077 $sql .= " SET dateo = '".$this->db->idate($date)."', datev = '".$this->db->idate($date)."'";
1078 $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).")";
1079 $sql .= " AND rappro = 0";
1080
1081 $result = $this->db->query($sql);
1082 if (!$result) {
1083 $error++;
1084 $this->error = 'Error -1 '.$this->db->error();
1085 }
1086
1087 if (!$error) {
1088 $this->datepaye = $date;
1089 $this->date = $date;
1090
1091 $this->db->commit();
1092 return 0;
1093 } else {
1094 $this->db->rollback();
1095 return -2;
1096 }
1097 }
1098 return -1; //no date given or already validated
1099 }
1100
1101 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1108 public function update_num($num_payment)
1109 {
1110 // phpcs:enable
1111 if (!empty($num_payment) && $this->statut != 1) {
1112 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1113 $sql .= " SET num_paiement = '".$this->db->escape($num_payment)."'";
1114 $sql .= " WHERE rowid = ".((int) $this->id);
1115
1116 dol_syslog(get_class($this)."::update_num", LOG_DEBUG);
1117 $result = $this->db->query($sql);
1118 if ($result) {
1119 $this->num_payment = $this->db->escape($num_payment);
1120 return 0;
1121 } else {
1122 $this->error = 'Error -1 '.$this->db->error();
1123 return -2;
1124 }
1125 }
1126 return -1; //no num given or already validated
1127 }
1128
1136 public function valide($user = null)
1137 {
1138 return $this->validate($user);
1139 }
1140
1147 public function validate($user = null)
1148 {
1149 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET statut = 1 WHERE rowid = '.((int) $this->id);
1150
1151 dol_syslog(get_class($this).'::valide', LOG_DEBUG);
1152 $result = $this->db->query($sql);
1153 if ($result) {
1154 return 1;
1155 } else {
1156 $this->error = $this->db->lasterror();
1157 dol_syslog(get_class($this).'::valide '.$this->error);
1158 return -1;
1159 }
1160 }
1161
1168 public function reject($user = null)
1169 {
1170 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET statut = 2 WHERE rowid = '.((int) $this->id);
1171
1172 dol_syslog(get_class($this).'::reject', LOG_DEBUG);
1173 $result = $this->db->query($sql);
1174 if ($result) {
1175 return 1;
1176 } else {
1177 $this->error = $this->db->lasterror();
1178 dol_syslog(get_class($this).'::reject '.$this->error);
1179 return -1;
1180 }
1181 }
1182
1189 public function info($id)
1190 {
1191 $sql = 'SELECT p.rowid, p.datec, p.fk_user_creat, p.fk_user_modif, p.tms';
1192 $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement as p';
1193 $sql .= ' WHERE p.rowid = '.((int) $id);
1194
1195 dol_syslog(get_class($this).'::info', LOG_DEBUG);
1196 $result = $this->db->query($sql);
1197
1198 if ($result) {
1199 if ($this->db->num_rows($result)) {
1200 $obj = $this->db->fetch_object($result);
1201
1202 $this->id = $obj->rowid;
1203
1204 $this->user_creation_id = $obj->fk_user_creat;
1205 $this->user_modification_id = $obj->fk_user_modif;
1206 $this->date_creation = $this->db->jdate($obj->datec);
1207 $this->date_modification = $this->db->jdate($obj->tms);
1208 }
1209 $this->db->free($result);
1210 } else {
1211 dol_print_error($this->db);
1212 }
1213 }
1214
1222 public function getBillsArray($filter = '')
1223 {
1224 $sql = 'SELECT pf.fk_facture';
1225 $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
1226 $sql .= ' WHERE pf.fk_facture = f.rowid AND pf.fk_paiement = '.((int) $this->id);
1227 if ($filter) {
1228 $sql .= ' AND '.$filter;
1229 }
1230 $resql = $this->db->query($sql);
1231 if ($resql) {
1232 $i = 0;
1233 $num = $this->db->num_rows($resql);
1234 $billsarray = array();
1235
1236 while ($i < $num) {
1237 $obj = $this->db->fetch_object($resql);
1238 $billsarray[$i] = $obj->fk_facture;
1239 $i++;
1240 }
1241
1242 return $billsarray;
1243 } else {
1244 $this->error = $this->db->error();
1245 dol_syslog(get_class($this).'::getBillsArray Error '.$this->error.' -', LOG_DEBUG);
1246 return -1;
1247 }
1248 }
1249
1256 public function getAmountsArray()
1257 {
1258 $sql = 'SELECT pf.fk_facture, pf.amount';
1259 $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf';
1260 $sql .= ' WHERE pf.fk_paiement = '.((int) $this->id);
1261 $resql = $this->db->query($sql);
1262 if ($resql) {
1263 $i = 0;
1264 $num = $this->db->num_rows($resql);
1265 $amounts = array();
1266
1267 while ($i < $num) {
1268 $obj = $this->db->fetch_object($resql);
1269 $amounts[$obj->fk_facture] = $obj->amount;
1270 $i++;
1271 }
1272
1273 return $amounts;
1274 } else {
1275 $this->error = $this->db->error();
1276 dol_syslog(get_class($this).'::getAmountsArray Error '.$this->error.' -', LOG_DEBUG);
1277 return -1;
1278 }
1279 }
1280
1289 public function getNextNumRef($soc, $mode = 'next')
1290 {
1291 global $conf, $db, $langs;
1292 $langs->load("bills");
1293
1294 // Clean parameters (if not defined or using deprecated value)
1295 if (!getDolGlobalString('PAYMENT_ADDON')) {
1296 $conf->global->PAYMENT_ADDON = 'mod_payment_cicada';
1297 } elseif (getDolGlobalString('PAYMENT_ADDON') == 'ant') {
1298 $conf->global->PAYMENT_ADDON = 'mod_payment_ant';
1299 } elseif (getDolGlobalString('PAYMENT_ADDON') == 'cicada') {
1300 $conf->global->PAYMENT_ADDON = 'mod_payment_cicada';
1301 }
1302
1303 if (getDolGlobalString('PAYMENT_ADDON')) {
1304 $mybool = false;
1305
1306 $file = getDolGlobalString('PAYMENT_ADDON') . ".php";
1307 $classname = getDolGlobalString('PAYMENT_ADDON');
1308
1309 // Include file with class
1310 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
1311
1312 foreach ($dirmodels as $reldir) {
1313 $dir = dol_buildpath($reldir."core/modules/payment/");
1314
1315 // Load file with numbering class (if found)
1316 if (is_file($dir.$file) && is_readable($dir.$file)) {
1317 $mybool = (include_once $dir.$file) || $mybool;
1318 }
1319 }
1320
1321 // For compatibility
1322 if (!$mybool) {
1323 $file = getDolGlobalString('PAYMENT_ADDON') . ".php";
1324 $classname = "mod_payment_" . getDolGlobalString('PAYMENT_ADDON');
1325 $classname = preg_replace('/\-.*$/', '', $classname);
1326 // Include file with class
1327 foreach ($conf->file->dol_document_root as $dirroot) {
1328 $dir = $dirroot."/core/modules/payment/";
1329
1330 // Load file with numbering class (if found)
1331 if (is_file($dir.$file) && is_readable($dir.$file)) {
1332 $mybool = (include_once $dir.$file) || $mybool;
1333 }
1334 }
1335 }
1336
1337 if (!$mybool) {
1338 dol_print_error(null, "Failed to include file ".$file);
1339 return '';
1340 }
1341
1342 $obj = new $classname();
1343 '@phan-var-force ModeleNumRefPayments $obj';
1344
1345 $numref = $obj->getNextValue($soc, $this);
1346
1351 if ($mode != 'last' && !$numref) {
1352 dol_print_error($db, "Payment::getNextNumRef ".$obj->error);
1353 return "";
1354 }
1355
1356 return $numref;
1357 } else {
1358 $langs->load("errors");
1359 print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Invoice"));
1360 return "";
1361 }
1362 }
1363
1369 public function getWay()
1370 {
1371 $way = 'dolibarr';
1372 if (isModEnabled('multicurrency')) {
1373 foreach ($this->multicurrency_amounts as $value) {
1374 if (!empty($value)) { // one value found into multicurrency_amounts so payment is in invoice currency
1375 $way = 'customer';
1376 break;
1377 }
1378 }
1379 }
1380
1381 return $way;
1382 }
1383
1392 public function initAsSpecimen($option = '')
1393 {
1394 global $user, $langs, $conf;
1395
1396 $now = dol_now();
1397 $arraynow = dol_getdate($now);
1398 $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
1399
1400 // Initialize parameters
1401 $this->id = 0;
1402 $this->ref = 'SPECIMEN';
1403 $this->specimen = 1;
1404 $this->facid = 1;
1405 $this->datepaye = $nownotime;
1406
1407 return 1;
1408 }
1409
1410
1421 public function getNomUrl($withpicto = 0, $option = '', $mode = 'withlistofinvoices', $notooltip = 0, $morecss = '')
1422 {
1423 global $conf, $langs, $hookmanager;
1424
1425 if (!empty($conf->dol_no_mouse_hover)) {
1426 $notooltip = 1; // Force disable tooltips
1427 }
1428
1429 $result = '';
1430
1431 $label = img_picto('', $this->picto).' <u>'.$langs->trans("Payment").'</u><br>';
1432 $label .= '<strong>'.$langs->trans("Ref").':</strong> '.$this->ref;
1433 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1434 if ($dateofpayment) {
1435 $label .= '<br><strong>'.$langs->trans("Date").':</strong> ';
1436 $tmparray = dol_getdate($dateofpayment);
1437 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
1438 $label .= dol_print_date($dateofpayment, 'day');
1439 } else { // Hours was set to real date of payment (special case for POS for example)
1440 $label .= dol_print_date($dateofpayment, 'dayhour', 'tzuser');
1441 }
1442 }
1443 if ($this->amount) {
1444 $label .= '<br><strong>'.$langs->trans("Amount").':</strong> '.price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
1445 }
1446 if ($mode == 'withlistofinvoices') {
1447 $arraybill = $this->getBillsArray();
1448 if (is_array($arraybill) && count($arraybill) > 0) {
1449 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1450 $facturestatic = new Facture($this->db);
1451 foreach ($arraybill as $billid) {
1452 $facturestatic->fetch($billid);
1453 $label .= '<br> '.$facturestatic->getNomUrl(1, '', 0, 0, '', 1).' '.$facturestatic->getLibStatut(2, -1);
1454 }
1455 }
1456 }
1457
1458 $linkclose = '';
1459 if (empty($notooltip)) {
1460 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1461 $label = $langs->trans("Payment");
1462 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1463 }
1464 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
1465 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1466 } else {
1467 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1468 }
1469
1470 $url = DOL_URL_ROOT.'/compta/paiement/card.php?id='.$this->id;
1471
1472 $linkstart = '<a href="'.$url.'"';
1473 $linkstart .= $linkclose.'>';
1474 $linkend = '</a>';
1475
1476 $result .= $linkstart;
1477 if ($withpicto) {
1478 $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);
1479 }
1480 if ($withpicto && $withpicto != 2) {
1481 $result .= ($this->ref ? $this->ref : $this->id);
1482 }
1483 $result .= $linkend;
1484 global $action;
1485 $hookmanager->initHooks(array($this->element . 'dao'));
1486 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1487 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1488 if ($reshook > 0) {
1489 $result = $hookmanager->resPrint;
1490 } else {
1491 $result .= $hookmanager->resPrint;
1492 }
1493 return $result;
1494 }
1495
1502 public function getLibStatut($mode = 0)
1503 {
1504 return $this->LibStatut($this->statut, $mode);
1505 }
1506
1507 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1515 public function LibStatut($status, $mode = 0)
1516 {
1517 // phpcs:enable
1518 global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
1519
1520 $langs->load('compta');
1521 /*if ($mode == 0)
1522 {
1523 if ($status == 0) return $langs->trans('ToValidate');
1524 if ($status == 1) return $langs->trans('Validated');
1525 }
1526 if ($mode == 1)
1527 {
1528 if ($status == 0) return $langs->trans('ToValidate');
1529 if ($status == 1) return $langs->trans('Validated');
1530 }
1531 if ($mode == 2)
1532 {
1533 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
1534 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
1535 }
1536 if ($mode == 3)
1537 {
1538 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
1539 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
1540 }
1541 if ($mode == 4)
1542 {
1543 if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
1544 if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
1545 }
1546 if ($mode == 5)
1547 {
1548 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
1549 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
1550 }
1551 if ($mode == 6)
1552 {
1553 if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
1554 if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
1555 }*/
1556 return '';
1557 }
1558
1559 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1567 public function fetch_thirdparty($force_thirdparty_id = 0)
1568 {
1569 // phpcs:enable
1570 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1571
1572 if (empty($force_thirdparty_id)) {
1573 $billsarray = $this->getBillsArray(); // From payment, the fk_soc isn't available, we should load the first supplier invoice to get him
1574 if (!empty($billsarray)) {
1575 $invoice = new Facture($this->db);
1576 if ($invoice->fetch($billsarray[0]) > 0) {
1577 $force_thirdparty_id = $invoice->socid;
1578 }
1579 }
1580 }
1581
1582 return parent::fetch_thirdparty($force_thirdparty_id);
1583 }
1584
1585
1591 public function isReconciled()
1592 {
1593 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1594 $accountline = new AccountLine($this->db);
1595 $accountline->fetch($this->bank_line);
1596 return $accountline->rappro ? true : false;
1597 }
1598}
$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,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteExtraFields()
Delete all extra fields values for the current object.
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_now($mode='gmt')
Return date for now.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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.
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right PaymentTypeShortLIQ right SELECT p pos_change as p datep as p p num_paiement as f pf amount as amount
Definition receipt.php:471
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:469