dolibarr 24.0.3
commoninvoice.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
3 * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
4 * Copyright (C) 2012-2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
5 * Copyright (C) 2023 Nick Fragoulis
6 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024-2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
32
36abstract class CommonInvoice extends CommonObject
37{
38 use CommonIncoterm;
39
43 public $title;
44
48 public $type = self::TYPE_STANDARD;
49
53 public $subtype;
54
60 public $fk_soc;
64 public $socid;
65
69 public $paye;
70
76 public $date;
77
81 public $date_lim_reglement;
82
86 public $cond_reglement_id; // Id in llx_c_paiement
90 public $cond_reglement_code; // Code in llx_c_paiement
94 public $cond_reglement_label;
98 public $cond_reglement_doc;
99
103 public $mode_reglement_id;
107 public $mode_reglement_code;
108
112 public $mode_reglement;
113
117 public $total_ht;
121 public $total_tva;
125 public $total_localtax1;
129 public $total_localtax2;
133 public $total_ttc;
137 public $revenuestamp;
138
142 public $totalpaid;
146 public $totaldeposits;
150 public $totalcreditnotes;
151
155 public $remaintopay;
159 public $nbofopendirectdebitorcredittransfer;
160
164 public $creditnote_ids;
165
169 public $stripechargedone;
170
174 public $stripechargeerror;
175
179 public $payment_reference;
180
184 public $dispute_status = 0;
185
186
191 public $description;
192
198 public $ref_client;
199
203 public $situation_cycle_ref;
204
208 public $retained_warranty_fk_cond_reglement;
209
215 public $close_code;
216
221 public $close_note;
222
223
228 public $postactionmessages;
229
230
234 const TYPE_STANDARD = 0;
235
240
245
249 const TYPE_DEPOSIT = 3;
250
255 const TYPE_PROFORMA = 4;
256
260 const TYPE_SITUATION = 5;
261
265 const STATUS_DRAFT = 0;
266
271
279 const STATUS_CLOSED = 2;
280
289
290
291 const CLOSECODE_DISCOUNTVAT = 'discount_vat'; // Abandoned (the remain to pay) - escompte
292 const CLOSECODE_BADDEBT = 'badcustomer'; // Abandoned (the remain to pay) - bad customer
293 const CLOSECODE_BANKCHARGE = 'bankcharge'; // Abandoned (the remain to pay) - bank charge
294 const CLOSECODE_WITHHOLDINGTAX = 'withholdingtax'; // Abandoned (the remain to pay) - source tax
295 const CLOSECODE_OTHER = 'other'; // Abandoned (the remain to pay) - other
296 const CLOSECODE_ABANDONED = 'abandon'; // Abandoned (no payment at all) - other
297 const CLOSECODE_REPLACED = 'replaced'; // Abandoned (no payment at all) - replacedby another invoice (feature disabled by default)
298
299 const ARRAY_OF_DISPUTE_STATUS = array(
300 0 => array('label' => "None"),
301 1 => array('label' => "DisputeOpen"),
302 8 => array('label' => "DisputeLost"),
303 9 => array('label' => "DisputeWon")
304 );
305
306
314 public function getRemainToPay($multicurrency = 0)
315 {
316 $alreadypaid = 0.0;
317 $alreadypaid += $this->getSommePaiement($multicurrency);
318 $alreadypaid += $this->getSumDepositsUsed($multicurrency);
319 $alreadypaid += $this->getSumCreditNotesUsed($multicurrency);
320
321 if ((int) $multicurrency > 0) {
322 $totalamount = $this->multicurrency_total_ttc;
323 } else {
324 $totalamount = $this->total_ttc;
325 }
326 $remaintopay = price2num($totalamount - $alreadypaid, 'MT');
327 if ($this->status == self::STATUS_CLOSED && $this->close_code == 'discount_vat') { // If invoice closed with discount for anticipated payment
328 $remaintopay = 0.0;
329 }
330 return $remaintopay;
331 }
332
342 public function getSommePaiement($multicurrency = 0)
343 {
344 $table = 'paiement_facture';
345 $field = 'fk_facture';
346 if ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier') {
347 $table = 'paiementfourn_facturefourn';
348 $field = 'fk_facturefourn';
349 }
350
351 $sql = "SELECT sum(amount) as amount, sum(multicurrency_amount) as multicurrency_amount";
352 $sql .= " FROM ".$this->db->prefix().$table;
353 $sql .= " WHERE ".$this->db->sanitize($field)." = ".((int) $this->id);
354
355 dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
356
357 $resql = $this->db->query($sql);
358 if ($resql) {
359 $obj = $this->db->fetch_object($resql);
360
361 $this->db->free($resql);
362
363 if ($obj) {
364 if ($multicurrency < 0) {
365 $this->totalpaid = $obj->amount;
366 $this->totalpaid_multicurrency = $obj->multicurrency_amount;
367 return array('alreadypaid' => (float) $obj->amount, 'alreadypaid_multicurrency' => (float) $obj->multicurrency_amount);
368 } elseif ($multicurrency) {
369 $this->totalpaid_multicurrency = $obj->multicurrency_amount;
370 return (float) $obj->multicurrency_amount;
371 } else {
372 $this->totalpaid = $obj->amount;
373 return (float) $obj->amount;
374 }
375 } else {
376 return 0;
377 }
378 } else {
379 $this->error = $this->db->lasterror();
380 return -1;
381 }
382 }
383
393 public function getSumDepositsUsed($multicurrency = 0)
394 {
395 /*if ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier') {
396 // FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS was never supported for purchase invoice, so we can return 0 with no need of SQL for this case.
397 return 0.0;
398 }*/
399
400 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
401
402 $discountstatic = new DiscountAbsolute($this->db);
403 $result = $discountstatic->getSumDepositsUsed($this, $multicurrency);
404
405 if ($result >= 0) {
406 if ($multicurrency) {
407 $this->totaldeposits_multicurrency = $result;
408 } else {
409 $this->totaldeposits = $result;
410 }
411
412 return $result;
413 } else {
414 $this->error = $discountstatic->error;
415 return -1;
416 }
417 }
418
426 public function getSumCreditNotesUsed($multicurrency = 0)
427 {
428 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
429
430 $discountstatic = new DiscountAbsolute($this->db);
431 $result = $discountstatic->getSumCreditNotesUsed($this, $multicurrency);
432 if (is_numeric($result)) {
433 if ($multicurrency) {
434 $this->totalcreditnotes_multicurrency = $result;
435 } else {
436 $this->totalcreditnotes = $result;
437 }
438
439 return $result;
440 } else {
441 $this->error = $discountstatic->error;
442 return $result;
443 }
444 }
445
452 public function getSumFromThisCreditNotesNotUsed($multicurrency = 0)
453 {
454 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
455
456 $discountstatic = new DiscountAbsolute($this->db);
457 $result = $discountstatic->getSumFromThisCreditNotesNotUsed($this, $multicurrency);
458 if ($result >= 0) {
459 return $result;
460 } else {
461 $this->error = $discountstatic->error;
462 return -1;
463 }
464 }
465
472 {
473 $idarray = array();
474
475 $sql = "SELECT rowid";
476 $sql .= " FROM ".$this->db->prefix().$this->table_element;
477 $sql .= " WHERE fk_facture_source = ".((int) $this->id);
478 $sql .= " AND type = ".self::TYPE_CREDIT_NOTE;
479
480 $resql = $this->db->query($sql);
481 if ($resql) {
482 $num = $this->db->num_rows($resql);
483 $i = 0;
484 while ($i < $num) {
485 $row = $this->db->fetch_row($resql);
486 $idarray[] = $row[0];
487 $i++;
488 }
489 } else {
490 dol_print_error($this->db);
491 }
492
493 $this->creditnote_ids = $idarray;
494
495 return $idarray;
496 }
497
504 public function getIdReplacingInvoice($option = '')
505 {
506 $sql = "SELECT rowid";
507 $sql .= " FROM ".$this->db->prefix().$this->table_element;
508 $sql .= " WHERE fk_facture_source = ".((int) $this->id);
509 $sql .= " AND type < 2";
510 if ($option == 'validated') {
511 $sql .= ' AND fk_statut = 1';
512 }
513 // PROTECTION BAD DATA
514 // In case the database is corrupted and there is a valid replectement invoice
515 // and another no, priority is given to the valid one.
516 // Should not happen (unless concurrent access and 2 people have created a
517 // replacement invoice for the same invoice at the same time)
518 $sql .= " ORDER BY fk_statut DESC";
519
520 $resql = $this->db->query($sql);
521 if ($resql) {
522 $obj = $this->db->fetch_object($resql);
523 if ($obj) {
524 // If there is any
525 return $obj->rowid;
526 } else {
527 // If no invoice replaces it
528 return 0;
529 }
530 } else {
531 return -1;
532 }
533 }
534
542 {
543 $listofopendirectdebitorcredittransfer = array();
544
545 // TODO Add a cache to store array of open requests for each invoice ID
546
547 $sql = "SELECT pfd.rowid, pfd.traite, pfd.date_demande as date_demande, pfd.date_traite as date_traite, pfd.amount";
548 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_demande as pfd";
549 if ($type == 'bank-transfer') {
550 $sql .= " WHERE fk_facture_fourn = ".((int) $this->id);
551 } else {
552 $sql .= " WHERE fk_facture = ".((int) $this->id);
553 }
554 $sql .= " AND pfd.traite = 0";
555 $sql .= " AND pfd.type = 'ban'";
556 $sql .= " ORDER BY pfd.date_demande DESC";
557
558 $resql = $this->db->query($sql);
559 $num = 0;
560 if ($resql) {
561 $num = $this->db->num_rows($resql);
562 $i = 0;
563 while ($i < $num) {
564 $obj = $this->db->fetch_object($resql);
565 if ($obj) {
566 $listofopendirectdebitorcredittransfer[] = array(
567 'id' => (int) $obj->rowid,
568 'invoiceid' => (int) $this->id,
569 'date' => $this->db->jdate($obj->date_demande),
570 'amount' => (float) $obj->amount
571 );
572 }
573
574 $i++;
575 }
576 } else {
577 $this->error = $this->db->lasterror();
578 }
579
580 $this->nbofopendirectdebitorcredittransfer = $num;
581
582 return $listofopendirectdebitorcredittransfer;
583 }
584
592 {
593 dol_syslog(get_class($this).'::setRetainedWarrantyPaymentTerms('.$id.')');
594 if ($this->status >= 0 || $this->element == 'societe') {
595 $fieldname = 'retained_warranty_fk_cond_reglement';
596
597 $sql = 'UPDATE '.$this->db->prefix().$this->table_element;
598 $sql .= " SET ".$this->db->sanitize($fieldname)." = ".((int) $id);
599 $sql .= ' WHERE rowid='.((int) $this->id);
600
601 if ($this->db->query($sql)) {
602 $this->retained_warranty_fk_cond_reglement = $id;
603 return 1;
604 } else {
605 dol_syslog(get_class($this).'::setRetainedWarrantyPaymentTerms Error '.$sql.' - '.$this->db->error());
606 $this->error = $this->db->error();
607 return -1;
608 }
609 } else {
610 dol_syslog(get_class($this).'::setRetainedWarrantyPaymentTerms, status of the object is incompatible');
611 $this->error = 'Status of the object is incompatible '.$this->status;
612 return -2;
613 }
614 }
615
626 public function getListOfPayments($filtertype = '', $multicurrency = 0, $mode = 0)
627 {
628 $retarray = array();
629 $this->error = ''; // By default no error, list can be empty.
630
631 $table = 'paiement_facture';
632 $table2 = 'paiement';
633
634 $field = 'fk_facture';
635 $field2 = 'fk_paiement';
636 $field3 = 'p.ref_ext';
637 $field4 = 'p.fk_bank'; // Bank line id
638
639 $sharedentity = 'facture';
640
641 if ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier') {
642 $table = 'paiementfourn_facturefourn';
643 $table2 = 'paiementfourn';
644
645 $field = 'fk_facturefourn';
646 $field2 = 'fk_paiementfourn';
647 $field3 = '';
648
649 $sharedentity = 'facture_fourn';
650 }
651
652 // List of payments
653 if (empty($mode) || $mode == 1) {
654 $sql = "SELECT p.ref, pf.amount, pf.multicurrency_amount, p.fk_paiement, p.datep, p.num_paiement as num, t.code".($field3 ? ", ".$this->db->sanitize($field3) : "") . (", ".$this->db->sanitize($field4));
655 $sql .= " FROM ".$this->db->prefix().$table." as pf, ".$this->db->prefix().$table2." as p, ".$this->db->prefix()."c_paiement as t";
656 $sql .= " WHERE pf.".$this->db->sanitize($field)." = ".((int) $this->id);
657 $sql .= " AND pf.".$this->db->sanitize($field2)." = p.rowid";
658 $sql .= ' AND p.fk_paiement = t.id';
659 $sql .= ' AND p.entity IN ('.getEntity($sharedentity).')';
660 if ($filtertype) {
661 $sql .= " AND t.code='PRE'";
662 }
663
664 dol_syslog(get_class($this)."::getListOfPayments filtertype=".$filtertype." multicurrency=".$multicurrency." mode=".$mode, LOG_DEBUG);
665
666 $resql = $this->db->query($sql);
667 if ($resql) {
668 $num = $this->db->num_rows($resql);
669 $i = 0;
670 while ($i < $num) {
671 $obj = $this->db->fetch_object($resql);
672 if ($multicurrency) {
673 $tmp = array('amount' => $obj->multicurrency_amount, 'type' => $obj->code, 'typeline' => 'payment', 'date' => $obj->datep, 'num' => $obj->num, 'ref' => $obj->ref);
674 } else {
675 $tmp = array('amount' => $obj->amount, 'type' => $obj->code, 'typeline' => 'payment', 'date' => $obj->datep, 'num' => $obj->num, 'ref' => $obj->ref);
676 }
677 if (!empty($field3)) {
678 $tmp['ref_ext'] = $obj->ref_ext;
679 }
680 if (!empty($field4)) {
681 $tmp['fk_bank_line'] = $obj->fk_bank;
682 }
683 $retarray[] = $tmp;
684 $i++;
685 }
686 $this->db->free($resql);
687 } else {
688 $this->error = $this->db->lasterror();
689 dol_print_error($this->db);
690 return array();
691 }
692 }
693
694 // Look for credit notes and discounts and deposits
695 if (empty($mode) || $mode == 2) {
696 $sql = '';
697 if ($this->element == 'facture' || $this->element == 'invoice') {
698 $sql = "SELECT rc.amount_ttc as amount, rc.multicurrency_amount_ttc as multicurrency_amount, rc.datec as date, f.ref as ref, rc.description as type";
699 $sql .= ' FROM '.$this->db->prefix().'societe_remise_except as rc, '.$this->db->prefix().'facture as f';
700 $sql .= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.((int) $this->id);
701 $sql .= ' AND (f.type = 2 OR f.type = 0 OR f.type = 3)'; // Find discount coming from credit note or excess received or deposits (payments from deposits are always null except if FACTURE_DEPOSITS_ARE_JUST_PAYMENTS is set)
702 } elseif ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier') {
703 $sql = "SELECT rc.amount_ttc as amount, rc.multicurrency_amount_ttc as multicurrency_amount, rc.datec as date, f.ref as ref, rc.description as type";
704 $sql .= ' FROM '.$this->db->prefix().'societe_remise_except as rc, '.$this->db->prefix().'facture_fourn as f';
705 $sql .= ' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = '.((int) $this->id);
706 $sql .= ' AND (f.type = 2 OR f.type = 0 OR f.type = 3)'; // Find discount coming from credit note or excess received or deposits (payments from deposits are always null except if FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS is set)
707 }
708
709 if ($sql) {
710 $resql = $this->db->query($sql);
711 if ($resql) {
712 $num = $this->db->num_rows($resql);
713 $i = 0;
714 while ($i < $num) {
715 $obj = $this->db->fetch_object($resql);
716 if ($multicurrency) {
717 $retarray[] = array('amount' => $obj->multicurrency_amount, 'type' => $obj->type, 'typeline' => 'discount', 'date' => $obj->date, 'num' => '0', 'ref' => $obj->ref);
718 } else {
719 $retarray[] = array('amount' => $obj->amount, 'type' => $obj->type, 'typeline' => 'discount', 'date' => $obj->date, 'num' => '', 'ref' => $obj->ref);
720 }
721 $i++;
722 }
723 } else {
724 $this->error = $this->db->lasterror();
725 dol_print_error($this->db);
726 return array();
727 }
728 $this->db->free($resql);
729 } else {
730 $this->error = $this->db->lasterror();
731 dol_print_error($this->db);
732 return array();
733 }
734 }
735
736 return $retarray;
737 }
738
753 public function isEditable()
754 {
755 // phpcs:enable
756 global $hookmanager, $action;
757
758 // We check if invoice is a temporary number (PROVxxxx)
759 $tmppart = substr($this->ref, 1, 4);
760
761 if ($this->status == self::STATUS_DRAFT && $tmppart === 'PROV') { // If draft invoice and ref not yet defined
762 return 1;
763 }
764
765 /*
766 if (getDolGlobalInt('INVOICE_CAN_NEVER_BE_REMOVED')) {
767 return 0;
768 }
769 */
770
771 // If not a draft invoice and not temporary invoice
772 if ($tmppart !== 'PROV') {
773 if ($this instanceof Facture) {
774 /* @var Facture $this */
775 // If sent by email, we refuse
776 if ((int) $this->email_sent_counter > 0) {
777 return -5;
778 }
779
780 // If printed, we refuse
781 if ((int) $this->pos_print_counter > 0) {
782 return -6;
783 }
784
785 include_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/blockedlog.lib.php';
786 if (isALNERunningVersion() && !empty($this->module_source)) {
787 $this->error = 'Action to modify an invoice from an external module like the Point Of Sale is not allowed';
788 return -7;
789 // Note, edit status to draft is also blocked by the trigger of blockedlog module for action BILL_UNVALIDATE that do a test on isEditable().
790 }
791 }
792
793 // If in accountancy, we refuse
794 $ventilExportCompta = $this->getVentilExportCompta();
795 if ($ventilExportCompta != 0) {
796 return -1;
797 }
798
799 // Get last number of validated invoice
800 if ($this->element != 'invoice_supplier') {
801 /*
802 if (empty($this->thirdparty)) {
803 $this->fetch_thirdparty(); // We need to have this->thirdparty defined, in case of the numbering rule uses tags that depend on thirdparty (like {t} tag).
804 }
805 $maxref = $this->getNextNumRef($this->thirdparty, 'last');
806 // $maxref can be '' (means not found) if there is no invoice yet, but also if there is no invoice for the new period when there is a reset at each period
807
808 // If invoice to delete is not the last one, we refuse
809 if ($maxref != '' && $maxref != $this->ref) {
810 return -2;
811 }
812 */
813
814 // TODO If there is payment in bookkeeping, check the payment is not dispatched in accounting and return -2.
815 // ...
816
817 // If invoice is situation type, we refuse if it is not the last in situation cycle
818 if (!getDolGlobalString('INVOICE_SITUATION_CAN_BE_REMOVED_EVEN_IF_NOT_LAST') && $this->situation_cycle_ref && method_exists($this, 'is_last_in_cycle')) {
819 $last = $this->is_last_in_cycle();
820 if (!$last) {
821 return -3;
822 }
823 }
824 }
825
826 // Test if there is at least one payment. If yes, we refuse.
827 if ($this->getSommePaiement() > 0) {
828 return -4;
829 }
830
831 $parameters = array();
832 $reshook = $hookmanager->executeHooks('isEditable', $parameters, $this, $action);
833 if (!empty($hookmanager->resArray['result'])) {
834 $this->error = $hookmanager->resArray['error'];
835 if (!empty($hookmanager->resArray['errors'])) {
836 $this->errors = array_merge($this->errors, $hookmanager->resArray['errors']);
837 }
838 if (!in_array($this->error, $this->errors)) {
839 $this->errors[] = $this->error;
840 }
841 return $hookmanager->resArray['result'];
842 }
843 }
844
845 return 2;
846 }
847
853 public function isReplacable()
854 {
855 global $hookmanager, $action;
856
857 // We check if invoice is a temporary number (PROVxxxx)
858 $tmppart = substr($this->ref, 1, 4);
859
860 if ($this->status == self::STATUS_DRAFT) { // If draft invoice, no
861 return -1;
862 }
863
864 // If not a draft invoice and not temporary invoice
865 if ($tmppart !== 'PROV') {
866 // If in accountancy, we refuse
867 $ventilExportCompta = $this->getVentilExportCompta();
868 if ($ventilExportCompta != 0) {
869 return -1;
870 }
871
872 // If invoice is situation type, we refuse if it is not the last in situation cycle
873 if (!getDolGlobalString('INVOICE_SITUATION_CAN_BE_REMOVED_EVEN_IF_NOT_LAST') && $this->situation_cycle_ref && method_exists($this, 'is_last_in_cycle')) {
874 $last = $this->is_last_in_cycle();
875 if (!$last) {
876 return -3;
877 }
878 }
879
880 // Test if there is at least one payment. If yes, we refuse to delete.
881 if ($this->getSommePaiement() > 0) {
882 return -4;
883 }
884
885 include_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/blockedlog.lib.php';
886 if (isALNERunningVersion()) {
887 $this->error = 'Action not allowed on a certified version (or candidate for certification)';
888 return -7;
889 }
890
891 $parameters = array();
892 $reshook = $hookmanager->executeHooks('isReplacable', $parameters, $this, $action);
893 if (!empty($hookmanager->resArray['result'])) {
894 $this->error = $hookmanager->resArray['error'];
895 if (!empty($hookmanager->resArray['errors'])) {
896 $this->errors = array_merge($this->errors, $hookmanager->resArray['errors']);
897 }
898 if (!in_array($this->error, $this->errors)) {
899 $this->errors[] = $this->error;
900 }
901 return $hookmanager->resArray['result'];
902 }
903
904 return 1;
905 }
906
907 return 0;
908 }
909
910 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
928 public function is_erasable()
929 {
930 // phpcs:enable
931 global $hookmanager, $action;
932
933 // We check if invoice is a temporary number (PROVxxxx)
934 $tmppart = substr($this->ref, 1, 4);
935
936 if ($this->status == self::STATUS_DRAFT && $tmppart === 'PROV') { // If draft invoice and ref not yet defined
937 return 1;
938 }
939
940 if (getDolGlobalInt('INVOICE_CAN_NEVER_BE_REMOVED')) {
941 return 0;
942 }
943
944 // If not a draft invoice and not temporary invoice
945 if ($tmppart !== 'PROV') {
946 if ($this instanceof Facture) {
947 /* @var Facture $this */
948 // If sent by email, we refuse
949 if ((int) $this->email_sent_counter > 0) {
950 return -5;
951 }
952
953 // If printed, we refuse
954 if ((int) $this->pos_print_counter > 0) {
955 return -6;
956 }
957
958 include_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/blockedlog.lib.php';
959 if (isALNERunningVersion()) {
960 $this->error = 'Action not allowed on the certified version (or candidate for certification)';
961 return -7;
962 }
963 }
964
965 // If in accountancy, we refuse
966 $ventilExportCompta = $this->getVentilExportCompta();
967 if ($ventilExportCompta != 0) {
968 return -1;
969 }
970
971 // Get last number of validated invoice
972 if ($this->element != 'invoice_supplier') {
973 if (empty($this->thirdparty)) {
974 $this->fetch_thirdparty(); // We need to have this->thirdparty defined, in case of the numbering rule uses tags that depend on thirdparty (like {t} tag).
975 }
976 $maxref = $this->getNextNumRef($this->thirdparty, 'last');
977 // $maxref can be '' (means not found) if there is no invoice yet, but also if there is no invoice for the new period when there is a reset at each period
978
979 // If invoice to delete is not the last one, we refuse
980 if ($maxref != '' && $maxref != $this->ref) {
981 return -2;
982 }
983
984 // TODO If there is payment in bookkeeping, check the payment is not dispatched in accounting and return -2.
985 // ...
986
987 // If invoice is situation type, we refuse if it is not the last in situation cycle
988 if (!getDolGlobalString('INVOICE_SITUATION_CAN_BE_REMOVED_EVEN_IF_NOT_LAST') && $this->situation_cycle_ref && method_exists($this, 'is_last_in_cycle')) {
989 $last = $this->is_last_in_cycle();
990 if (!$last) {
991 return -3;
992 }
993 }
994 }
995
996 // Test if there is at least one payment. If yes, we refuse.
997 if ($this->getSommePaiement() > 0) {
998 return -4;
999 }
1000
1001 $parameters = array();
1002 $reshook = $hookmanager->executeHooks('isErasable', $parameters, $this, $action);
1003 if (!empty($hookmanager->resArray['result'])) {
1004 $this->error = $hookmanager->resArray['error'];
1005 if (!empty($hookmanager->resArray['errors'])) {
1006 $this->errors = array_merge($this->errors, $hookmanager->resArray['errors']);
1007 }
1008 if (!in_array($this->error, $this->errors)) {
1009 $this->errors[] = $this->error;
1010 }
1011 return $hookmanager->resArray['result'];
1012 }
1013 }
1014
1015 return 2;
1016 }
1017
1025 public function getVentilExportCompta($mode = 0)
1026 {
1027 if (!isModEnabled('accounting')) {
1028 return 0;
1029 }
1030
1031 $alreadydispatched = 0;
1032
1033 $type = 'customer_invoice';
1034 if ($this->element == 'invoice_supplier') {
1035 $type = 'supplier_invoice';
1036 }
1037
1038 $sql = " SELECT ".($mode ? 'DISTINCT piece_num' : 'COUNT(ab.rowid)')." as nb";
1039 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping as ab";
1040 $sql .= " WHERE ab.doc_type = '".$this->db->escape($type)."' AND ab.fk_doc = ".((int) $this->id);
1041
1042 $resql = $this->db->query($sql);
1043 if ($resql) {
1044 $obj = $this->db->fetch_object($resql);
1045 if ($obj) {
1046 $alreadydispatched = $obj->nb;
1047 }
1048 } else {
1049 $this->error = $this->db->lasterror();
1050 return -1;
1051 }
1052
1053 if ($alreadydispatched) {
1054 return $alreadydispatched;
1055 }
1056 return 0;
1057 }
1058
1066 public function getNextNumRef($soc, $mode = 'next')
1067 {
1068 // TODO Must be implemented into main class
1069 return '';
1070 }
1071
1078 public function getLibType($withbadge = 0)
1079 {
1080 global $langs;
1081
1082 $labellong = "Unknown";
1083 $labelshort = "Unknown";
1084 if ($this->type == CommonInvoice::TYPE_STANDARD) {
1085 $labellong = "InvoiceStandard";
1086 $labelshort = "InvoiceStandardShort";
1087 } elseif ($this->type == CommonInvoice::TYPE_REPLACEMENT) {
1088 $labellong = "InvoiceReplacement";
1089 $labelshort = "InvoiceReplacementShort";
1090 } elseif ($this->type == CommonInvoice::TYPE_CREDIT_NOTE) {
1091 $labellong = "InvoiceAvoir";
1092 $labelshort = "CreditNote";
1093 } elseif ($this->type == CommonInvoice::TYPE_DEPOSIT) {
1094 $labellong = "InvoiceDeposit";
1095 $labelshort = "Deposit";
1096 } elseif ($this->type == CommonInvoice::TYPE_PROFORMA) { // @phan-suppress-current-line PhanDeprecatedClassConstant
1097 $labellong = "InvoiceProForma"; // Not used.
1098 $labelshort = "ProForma";
1099 } elseif ($this->type == CommonInvoice::TYPE_SITUATION) {
1100 $labellong = "InvoiceSituation";
1101 $labelshort = "Situation";
1102 }
1103
1104 $out = '';
1105 if ($withbadge) {
1106 $out .= '<span class="badgeneutral" title="'.dol_escape_htmltag($langs->trans($labellong)).'">';
1107 }
1108 $out .= $langs->trans($withbadge == 2 ? $labelshort : $labellong);
1109 if ($withbadge) {
1110 $out .= '</span>';
1111 }
1112 return $out;
1113 }
1114
1121 public function getSubtypeLabel($table = '')
1122 {
1123 $subtypeLabel = '';
1124 if ($table === 'facture' || $table === 'facture_fourn') {
1125 $sql = "SELECT s.label FROM " . $this->db->prefix() . $table . " AS f";
1126 $sql .= " INNER JOIN " . $this->db->prefix() . "c_invoice_subtype AS s ON f.subtype = s.rowid";
1127 $sql .= " WHERE f.ref = '".$this->db->escape($this->ref)."'";
1128 } elseif ($table === 'facture_rec' || $table === 'facture_fourn_rec') {
1129 $sql = "SELECT s.label FROM " . $this->db->prefix() . $table . " AS f";
1130 $sql .= " INNER JOIN " . $this->db->prefix() . "c_invoice_subtype AS s ON f.subtype = s.rowid";
1131 $sql .= " WHERE f.titre = '".$this->db->escape($this->title)."'";
1132 } else {
1133 return -1;
1134 }
1135
1136 $resql = $this->db->query($sql);
1137 if ($resql) {
1138 while ($obj = $this->db->fetch_object($resql)) {
1139 $subtypeLabel = $obj->label;
1140 }
1141 } else {
1142 dol_print_error($this->db);
1143 return -1;
1144 }
1145
1146 return $subtypeLabel;
1147 }
1148
1155 public function getArrayOfInvoiceSubtypes($mode = 0)
1156 {
1157 global $mysoc;
1158
1159 $effs = array();
1160
1161 $sql = "SELECT rowid, code, label as label";
1162 $sql .= " FROM " . MAIN_DB_PREFIX . 'c_invoice_subtype';
1163 $sql .= " WHERE active = 1 AND fk_country = ".((int) $mysoc->country_id)." AND entity IN(".getEntity('c_invoice_subtype').")";
1164 $sql .= " ORDER by rowid, code";
1165
1166 dol_syslog(get_class($this) . '::getArrayOfInvoiceSubtypes', LOG_DEBUG);
1167 $resql = $this->db->query($sql);
1168 if ($resql) {
1169 $num = $this->db->num_rows($resql);
1170 $i = 0;
1171
1172 while ($i < $num) {
1173 $objp = $this->db->fetch_object($resql);
1174 if (!$mode) {
1175 $key = $objp->rowid;
1176 $effs[$key] = $objp->label;
1177 } else {
1178 $key = $objp->code;
1179 $effs[$key] = $objp->rowid;
1180 }
1181
1182 $i++;
1183 }
1184 $this->db->free($resql);
1185 }
1186
1187 return $effs;
1188 }
1189
1197 public function getLibStatut($mode = 0, $alreadypaid = -1)
1198 {
1199 $moreparams = array(
1200 'nbofopendirectdebitorcredittransfer' => $this->nbofopendirectdebitorcredittransfer,
1201 'close_code' => $this->close_code,
1202 'close_note' => $this->close_note,
1203 'dispute_status' => $this->dispute_status
1204 );
1205
1206 return $this->LibStatut($this->paye, $this->status, $mode, $alreadypaid, $this->type, $moreparams);
1207 }
1208
1209 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1221 public function LibStatut($paye, $status, $mode = 0, $alreadypaid = -1, $type = -1, $moreparams = array())
1222 {
1223 // phpcs:enable
1224 global $langs, $hookmanager;
1225 $langs->load('bills');
1226
1227 if ($type == -1) {
1228 $type = $this->type;
1229 }
1230
1231 // For backward compatibility
1232 if (is_int($moreparams)) {
1233 $moreparams = array('nbofopendirectdebitorcredittransfer' => (int) $moreparams);
1234 }
1235
1236 $nbofopendirectdebitorcredittransfer = 0;
1237 foreach ($moreparams as $moreparamkey => $moreparamvalue) {
1238 if ($moreparamkey == 'nbofopendirectdebitorcredittransfer') {
1239 $nbofopendirectdebitorcredittransfer = $moreparamvalue;
1240 }
1241 }
1242
1243 $statusType = 'status0';
1244 $prefix = 'Short';
1245 if (!$paye) {
1246 if ($status == 0) {
1247 $labelStatus = $langs->transnoentitiesnoconv('BillStatusDraft');
1248 $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusDraft');
1249 } elseif (($status == 3 || $status == 2) && $alreadypaid <= 0) {
1250 if ($status == 3) {
1251 $labelStatus = $langs->transnoentitiesnoconv('BillStatusCanceled');
1252 $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusCanceled');
1253 } else {
1254 $labelStatus = $langs->transnoentitiesnoconv('BillStatusClosedUnpaid');
1255 $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusClosedUnpaid');
1256 }
1257 $statusType = 'status5';
1258 } elseif (($status == 3 || $status == 2) && $alreadypaid > 0) {
1259 $labelStatus = $langs->transnoentitiesnoconv('BillStatusClosedPaidPartially');
1260 $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusClosedPaidPartially');
1261 $statusType = 'status9';
1262 } elseif ($alreadypaid == 0 && $nbofopendirectdebitorcredittransfer == 0) {
1263 $labelStatus = $langs->transnoentitiesnoconv('BillStatusNotPaid');
1264 $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusNotPaid');
1265 $statusType = 'status1';
1266 } else {
1267 $labelStatus = $langs->transnoentitiesnoconv('BillStatusStarted');
1268 $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusStarted');
1269 $statusType = 'status3';
1270 }
1271 } else {
1272 $statusType = 'status6';
1273 if ($type == self::TYPE_CREDIT_NOTE) {
1274 $labelStatus = $langs->transnoentitiesnoconv('BillStatusPaidBackOrConverted'); // credit note
1275 $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusPaidBackOrConverted'); // credit note
1276 } elseif ($type == self::TYPE_DEPOSIT) {
1277 $labelStatus = $langs->transnoentitiesnoconv('BillStatusConverted'); // deposit invoice
1278 $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusConverted'); // deposit invoice
1279 } else {
1280 $labelStatus = $langs->transnoentitiesnoconv('BillStatusPaid');
1281 $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusPaid');
1282 }
1283 }
1284
1285 $paramsBadge = array('badgeParams' => array('attr' => array(
1286 'data-status-element' => $this->element,
1287 'data-already-paid' => $alreadypaid > 0 ? 1 : 0,
1288 'data-status' => (int) $status
1289 )));
1290
1291 $parameters = array(
1292 'status' => $status,
1293 'mode' => $mode,
1294 'paye' => $paye,
1295 'alreadypaid' => $alreadypaid,
1296 'type' => $type,
1297 'paramsBadge' => & $paramsBadge
1298 );
1299
1300 $reshook = $hookmanager->executeHooks('LibStatut', $parameters, $this); // Note that $action and $object may have been modified by hook
1301
1302 if ($reshook > 0) {
1303 return $hookmanager->resPrint;
1304 }
1305
1306 if (!empty($moreparams['close_code'])) {
1307 $titlestringtoshow = '';
1308
1309 if ($moreparams['close_code'] == self::CLOSECODE_DISCOUNTVAT) {
1310 $titlestringtoshow = $langs->trans("HelpEscompte");
1311 } elseif ($moreparams['close_code'] == self::CLOSECODE_BADDEBT) {
1312 $titlestringtoshow = $langs->trans("ConfirmClassifyPaidPartiallyReasonBadCustomer");
1313 } elseif ($moreparams['close_code'] == self::CLOSECODE_BANKCHARGE) {
1314 $titlestringtoshow = $langs->trans("ConfirmClassifyPaidPartiallyReasonBankCharge");
1315 } elseif ($moreparams['close_code'] == self::CLOSECODE_WITHHOLDINGTAX) {
1316 $titlestringtoshow = $langs->trans("ConfirmClassifyPaidPartiallyReasonWithholdingTax");
1317 } elseif ($moreparams['close_code'] == self::CLOSECODE_OTHER) {
1318 $titlestringtoshow = $langs->trans("Other");
1319 }
1320
1321 //$paramsbutton = array('badgeParams' => array('attr' => array('title' => 'rrrr')));
1322 $paramsBadge['badgeParams' ]['attr']['title'] = $titlestringtoshow;
1323 }
1324
1325 if (isset($moreparams['dispute_status']) && $moreparams['dispute_status']) {
1326 $labelStatus .= ' - ';
1327 if ($moreparams['dispute_status'] == 8) {
1328 $labelStatus .= $langs->trans("DisputeLost");
1329 } elseif ($moreparams['dispute_status'] == 9) {
1330 $labelStatus .= $langs->trans("DisputeWon");
1331 } else {
1332 $labelStatus .= $langs->trans("DisputeOpen");
1333 $statusType = 'status8';
1334 }
1335 }
1336
1337 $statusbadge = dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode, '', $paramsBadge);
1338
1339 return $statusbadge;
1340 }
1341
1342 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1350 public function calculate_date_lim_reglement($cond_reglement = 0)
1351 {
1352 // phpcs:enable
1353 if (!$cond_reglement) {
1354 $cond_reglement = $this->cond_reglement_code;
1355 }
1356 if (!$cond_reglement) {
1357 $cond_reglement = $this->cond_reglement_id;
1358 }
1359 if (!$cond_reglement) {
1360 return $this->date;
1361 }
1362
1363 $cdr_nbjour = 0;
1364 $cdr_type = 0;
1365 $cdr_decalage = 0;
1366
1367 $sqltemp = "SELECT c.type_cdr, c.nbjour, c.decalage";
1368 $sqltemp .= " FROM ".$this->db->prefix()."c_payment_term as c";
1369 if (is_numeric($cond_reglement)) {
1370 $sqltemp .= " WHERE c.rowid=".((int) $cond_reglement);
1371 } else {
1372 $sqltemp .= " WHERE c.entity IN (".getEntity('c_payment_term').")";
1373 $sqltemp .= " AND c.code = '".$this->db->escape($cond_reglement)."'";
1374 }
1375
1376 dol_syslog(get_class($this).'::calculate_date_lim_reglement', LOG_DEBUG);
1377 $resqltemp = $this->db->query($sqltemp);
1378 if ($resqltemp) {
1379 if ($this->db->num_rows($resqltemp)) {
1380 $obj = $this->db->fetch_object($resqltemp);
1381 $cdr_nbjour = $obj->nbjour;
1382 $cdr_type = $obj->type_cdr;
1383 $cdr_decalage = $obj->decalage;
1384 }
1385 } else {
1386 $this->error = $this->db->error();
1387 return -1;
1388 }
1389 $this->db->free($resqltemp);
1390
1391 /* Define date limit */
1392
1393 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
1394
1395 // Note: $this->date is midnight in the server timezone, so the delay must be added as calendar days and not as
1396 // a number of seconds. A day is not always 24 hours long: when the delay crosses a daylight saving time change,
1397 // adding 86400 seconds per day shifts the result by one hour and can move it to the previous day.
1398
1399 // 0 : adding the number of days
1400 if ($cdr_type == 0) {
1401 $datelim = dol_time_plus_duree($this->date, $cdr_nbjour, 'd', 0, 'tzserver');
1402
1403 $datelim = dol_time_plus_duree($datelim, $cdr_decalage, 'd', 0, 'tzserver');
1404 } elseif ($cdr_type == 1) {
1405 // 1 : application of the "end of the month" rule
1406 $datelim = dol_time_plus_duree($this->date, $cdr_nbjour, 'd', 0, 'tzserver');
1407
1408 $mois = date('m', $datelim);
1409 $annee = date('Y', $datelim);
1410 if ($mois == 12) {
1411 $mois = 1;
1412 $annee += 1;
1413 } else {
1414 $mois += 1;
1415 }
1416 // We move at the beginning of the next month, and we take a day off
1417 $datelim = dol_mktime(12, 0, 0, $mois, 1, $annee, 'gmt');
1418 $datelim -= (3600 * 24);
1419
1420 $datelim += ($cdr_decalage * 3600 * 24);
1421 } elseif ($cdr_type == 2 && !empty($cdr_decalage)) {
1422 // 2 : application of the rule, the N of the current or next month
1423 $datelim = dol_time_plus_duree($this->date, $cdr_nbjour, 'd', 0, 'tzserver');
1424
1425 $date_piece = dol_mktime(0, 0, 0, (int) date('m', $datelim), (int) date('d', $datelim), (int) date('Y', $datelim), 'gmt'); // without hours, minutes and seconds
1426 $date_lim_current = dol_mktime(0, 0, 0, (int) date('m', $datelim), (int) $cdr_decalage, (int) date('Y', $datelim), 'gmt'); // without hours, minutes and seconds
1427 $date_lim_next = dol_time_plus_duree((int) $date_lim_current, 1, 'm'); // Add 1 month
1428
1429 $diff = $date_piece - $date_lim_current;
1430
1431 if ($diff <= 0) {
1432 $datelim = $date_lim_current;
1433 } else {
1434 $datelim = $date_lim_next;
1435 }
1436 } else {
1437 return 'Bad value for type_cdr in database for record cond_reglement = '.$cond_reglement;
1438 }
1439
1440 return $datelim;
1441 }
1442
1443 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1456 public function demande_prelevement(User $fuser, float $amount = 0, string $type = 'direct-debit', string $sourcetype = 'facture', int $checkduplicateamongall = 0, int $ribId = 0)
1457 {
1458 // phpcs:enable
1459 global $conf;
1460
1461 $error = 0;
1462
1463 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
1464
1465 if ($this->status > self::STATUS_DRAFT && $this->paye == 0) {
1466 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
1467 $bac = new CompanyBankAccount($this->db);
1468 $bac->fetch($ribId, '', $this->socid);
1469
1470 // Option to allow multiple partial requests
1471 // If WITHDRAW_STRICT_CHECK_AMOUNT is enabled, we check the sum of amounts instead of just counting requests
1472 if (getDolGlobalString('WITHDRAW_STRICT_CHECK_AMOUNT')) {
1473 // Calculate sum of amounts already requested instead of just counting requests
1474 // This allows multiple partial requests as long as the total doesn't exceed the remain to pay
1475 $total_already_requested = 0;
1476 // Step 1: Get pending requests with no transfer receipt yet (traite = 0)
1477 $sql = "SELECT COALESCE(SUM(amount), 0) as total_requested";
1478 $sql .= " FROM ".$this->db->prefix()."prelevement_demande";
1479 if ($type == 'bank-transfer') {
1480 $sql .= " WHERE fk_facture_fourn = ".((int) $this->id);
1481 } else {
1482 $sql .= " WHERE fk_facture = ".((int) $this->id);
1483 }
1484 $sql .= " AND type = 'ban'"; // To exclude record done for some online payments
1485 $sql .= " AND traite = 0"; // Not yet in a transfer receipt
1486 dol_syslog(get_class($this)."::demande_prelevement - get pending requests not yet in receipt", LOG_DEBUG);
1487 $resql = $this->db->query($sql);
1488 if ($resql) {
1489 $obj = $this->db->fetch_object($resql);
1490 $total_already_requested += $obj ? (float) $obj->total_requested : 0;
1491 } else {
1492 $this->error = $this->db->error();
1493 dol_syslog(get_class($this).'::demandeprelevement Error -2a');
1494 return -2;
1495 }
1496 // Step 2: Get pending requests in transfer receipt but not yet credited
1497 $sql = "SELECT COALESCE(SUM(pl.amount), 0) as total_requested";
1498 $sql .= " FROM ".$this->db->prefix()."prelevement_lignes as pl";
1499 $sql .= " INNER JOIN ".$this->db->prefix()."prelevement as p ON p.fk_prelevement_lignes = pl.rowid";
1500 if ($type == 'bank-transfer') {
1501 $sql .= " WHERE p.fk_facture_fourn = ".((int) $this->id);
1502 } else {
1503 $sql .= " WHERE p.fk_facture = ".((int) $this->id);
1504 }
1505 $sql .= " AND (pl.statut IS NULL OR pl.statut = 0)"; // Not yet processed (statut 2 = credited)
1506 dol_syslog(get_class($this)."::demande_prelevement - get requests in non-credited receipts", LOG_DEBUG);
1507 $resql = $this->db->query($sql);
1508 if ($resql) {
1509 $obj = $this->db->fetch_object($resql);
1510 $total_already_requested += $obj ? (float) $obj->total_requested : 0;
1511 // Calculate remain to pay
1512 $totalpaid = $this->getSommePaiement();
1513 $totalcreditnotes = $this->getSumCreditNotesUsed();
1514 $totaldeposits = $this->getSumDepositsUsed();
1515 $resteapayer = (float) price2num($this->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
1516 // Calculate remaining amount available for new requests
1517 $remaining_for_request = (float) price2num($resteapayer - $total_already_requested, 'MT');
1518 // If no amount specified, use the remaining available
1519 if (empty($amount)) {
1520 $amount = $remaining_for_request;
1521 }
1522 // Check if there's still room for new requests
1523 $can_create_request = ($remaining_for_request > 0 && $amount <= $remaining_for_request);
1524 } else {
1525 $this->error = $this->db->error();
1526 dol_syslog(get_class($this).'::demandeprelevement Error -2b');
1527 return -2;
1528 }
1529 } else {
1530 // Original behavior: only check if a request already exists (count)
1531 $sql = "SELECT count(rowid) as nb";
1532 $sql .= " FROM ".$this->db->prefix()."prelevement_demande";
1533 if ($type == 'bank-transfer') {
1534 $sql .= " WHERE fk_facture_fourn = ".((int) $this->id);
1535 } else {
1536 $sql .= " WHERE fk_facture = ".((int) $this->id);
1537 }
1538 $sql .= " AND type = 'ban'"; // To exclude record done for some online payments
1539 if (empty($checkduplicateamongall)) {
1540 $sql .= " AND traite = 0";
1541 }
1542
1543 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
1544
1545 $resql = $this->db->query($sql);
1546 if ($resql) {
1547 $obj = $this->db->fetch_object($resql);
1548 $can_create_request = ($obj && $obj->nb == 0);
1549 // Calculate amount if not specified
1550 if (empty($amount)) {
1551 $totalpaid = $this->getSommePaiement();
1552 $totalcreditnotes = $this->getSumCreditNotesUsed();
1553 $totaldeposits = $this->getSumDepositsUsed();
1554 //print "totalpaid=".$totalpaid." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits;
1555 // We can also use bcadd to avoid pb with floating points
1556 // For example print 239.2 - 229.3 - 9.9; does not return 0.
1557 //$resteapayer=bcadd($this->total_ttc,$totalpaid,$conf->global->MAIN_MAX_DECIMALS_TOT);
1558 //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
1559 $amount = price2num($this->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
1560 }
1561 $remaining_for_request = $amount; // For error message compatibility
1562 } else {
1563 $this->error = $this->db->error();
1564 dol_syslog(get_class($this).'::demandeprelevement Error -2');
1565 return -2;
1566 }
1567 }
1568 // Common code for both modes
1569 if ($can_create_request) {
1570 $now = dol_now();
1571 if (is_numeric($amount) && $amount != 0) {
1572 $sql = 'INSERT INTO '.$this->db->prefix().'prelevement_demande(';
1573 if ($type == 'bank-transfer') {
1574 $sql .= 'fk_facture_fourn, ';
1575 } else {
1576 $sql .= 'fk_facture, ';
1577 }
1578 $sql .= ' amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib, sourcetype, type, entity';
1579 if (empty($bac->id)) {
1580 $sql .= ')';
1581 } else {
1582 $sql .= ', fk_societe_rib)';
1583 }
1584 $sql .= " VALUES (".((int) $this->id);
1585 $sql .= ", ".((float) price2num($amount));
1586 $sql .= ", '".$this->db->idate($now)."'";
1587 $sql .= ", ".((int) $fuser->id);
1588 $sql .= ", '".$this->db->escape($bac->code_banque)."'";
1589 $sql .= ", '".$this->db->escape($bac->code_guichet)."'";
1590 $sql .= ", '".$this->db->escape($bac->number)."'";
1591 $sql .= ", '".$this->db->escape($bac->cle_rib)."'";
1592 $sql .= ", '".$this->db->escape($sourcetype)."'";
1593 $sql .= ", 'ban'";
1594 $sql .= ", ".((int) $conf->entity);
1595 if (!empty($bac->id)) {
1596 $sql .= ", '".$this->db->escape((string) $bac->id)."'";
1597 }
1598 $sql .= ")";
1599 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
1600 $resql = $this->db->query($sql);
1601 if (!$resql) {
1602 $this->error = $this->db->lasterror();
1603 dol_syslog(get_class($this).'::demandeprelevement Erreur');
1604 $error++;
1605 }
1606 } else {
1607 $this->error = 'WithdrawRequestErrorNilAmount';
1608 dol_syslog(get_class($this).'::demandeprelevement WithdrawRequestErrorNilAmount');
1609 $error++;
1610 }
1611 if (!$error) {
1612 // Force payment mode of invoice to withdraw
1613 $payment_mode_id = dol_getIdFromCode($this->db, ($type == 'bank-transfer' ? 'VIR' : 'PRE'), 'c_paiement', 'code', 'id', 1);
1614 if ($payment_mode_id > 0) {
1615 $result = $this->setPaymentMethods($payment_mode_id);
1616 }
1617 }
1618 if ($error) {
1619 return -1;
1620 }
1621 return 1;
1622 } else {
1623 // Updated error message for when amount exceeds remaining or request already exists
1624 if (getDolGlobalString('WITHDRAW_STRICT_CHECK_AMOUNT')) {
1625 if ($remaining_for_request <= 0) {
1626 $this->error = "AmountRequestedAlreadyReachesTotal";
1627 } else {
1628 $this->error = "AmountExceedsRemainingToRequest";
1629 }
1630 } else {
1631 $this->error = "A request already exists";
1632 }
1633 return 0;
1634 }
1635 } else {
1636 $this->error = "Status of invoice does not allow this";
1637 dol_syslog(get_class($this)."::demandeprelevement ".$this->error." $this->status, $this->paye, $this->mode_reglement_id");
1638 return -3;
1639 }
1640 }
1641
1642
1654 public function makeStripeCardRequest($fuser, $id, $sourcetype = 'facture')
1655 {
1656 // TODO See in sellyoursaas
1657 return 0;
1658 }
1659
1672 public function makeStripeSepaRequest($fuser, $did, $type = 'direct-debit', $sourcetype = 'facture', $service = '', $forcestripe = '')
1673 {
1674 global $conf, $user, $langs;
1675
1676 if ($type != 'bank-transfer' && $type != 'credit-transfer' && !getDolGlobalString('STRIPE_SEPA_DIRECT_DEBIT')) {
1677 return 0;
1678 }
1679 if ($type != 'direct-debit' && !getDolGlobalString('STRIPE_SEPA_CREDIT_TRANSFER')) {
1680 return 0;
1681 }
1682 // Set a default value for service if not provided
1683 if (empty($service)) {
1684 $service = 'StripeTest';
1685 if (getDolGlobalString('STRIPE_LIVE')/* && !GETPOST('forcesandbox', 'alpha')*/) {
1686 $service = 'StripeLive';
1687 }
1688 }
1689
1690 $error = 0;
1691
1692 dol_syslog(get_class($this)."::makeStripeSepaRequest start did=".$did." type=".$type." service=".$service." sourcetype=".$sourcetype." forcestripe=".$forcestripe, LOG_DEBUG);
1693
1694 if ($this->status > self::STATUS_DRAFT && $this->paye == 0) {
1695 // Get the default payment mode for BAN payment of the third party
1696 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
1697 $bac = new CompanyBankAccount($this->db); // Table societe_rib
1698 $result = $bac->fetch(0, '', $this->socid, 1, 'ban');
1699 if ($result <= 0 || empty($bac->id)) {
1700 $this->error = $langs->trans("ThirdpartyHasNoDefaultBankAccount");
1701 $this->errors[] = $this->error;
1702 dol_syslog(get_class($this)."::makeStripeSepaRequest ".$this->error);
1703 return -1;
1704 }
1705
1706 // Load the pending payment request to process (with rowid=$did)
1707 $sql = "SELECT rowid, date_demande, amount, fk_facture, fk_facture_fourn, fk_salary, fk_prelevement_bons";
1708 $sql .= " FROM ".$this->db->prefix()."prelevement_demande";
1709 $sql .= " WHERE rowid = ".((int) $did);
1710 if ($type != 'bank-transfer' && $type != 'credit-transfer') {
1711 $sql .= " AND fk_facture = ".((int) $this->id); // Add a protection to not pay another invoice than current one
1712 }
1713 if ($type != 'direct-debit') {
1714 if ($sourcetype == 'salary') {
1715 $sql .= " AND fk_salary = ".((int) $this->id); // Add a protection to not pay another salary than current one
1716 } else {
1717 $sql .= " AND fk_facture_fourn = ".((int) $this->id); // Add a protection to not pay another invoice than current one
1718 }
1719 }
1720 $sql .= " AND traite = 0"; // To not process payment request that were already converted into a direct debit or credit transfer order (Note: fk_prelevement_bons is also empty when traite = 0)
1721
1722 dol_syslog(get_class($this)."::makeStripeSepaRequest load requests to process", LOG_DEBUG);
1723 $resql = $this->db->query($sql);
1724 if ($resql) {
1725 $obj = $this->db->fetch_object($resql);
1726 if (!$obj) {
1727 dol_print_error($this->db, 'CantFindRequestWithId');
1728 return -2;
1729 }
1730
1731 // amount to pay
1732 $amount = $obj->amount;
1733
1734 if (is_numeric($amount) && $amount != 0) {
1735 require_once DOL_DOCUMENT_ROOT.'/societe/class/companypaymentmode.class.php';
1736 $companypaymentmode = new CompanyPaymentMode($this->db); // table societe_rib
1737 $companypaymentmode->fetch($bac->id);
1738 $sepaSequenceType = strtoupper((string) $companypaymentmode->frstrecur);
1739 if (!in_array($sepaSequenceType, array('FRST', 'RCUR', 'OOFF', 'FNAL'), true)) {
1740 $sepaSequenceType = 'FRST';
1741 }
1742
1743 $this->stripechargedone = 0;
1744 $this->stripechargeerror = 0;
1745
1746 $now = dol_now();
1747
1748 $currency = $conf->currency;
1749
1750 $errorforinvoice = 0; // We reset the $errorforinvoice at each invoice loop
1751
1752 $this->fetch_thirdparty();
1753
1754 dol_syslog("makeStripeSepaRequest Process payment request amount=".$amount." thirdparty_id=" . $this->thirdparty->id . ", thirdparty_name=" . $this->thirdparty->name . " ban id=" . $bac->id, LOG_DEBUG);
1755
1756 //$alreadypayed = $this->getSommePaiement();
1757 //$amount_credit_notes_included = $this->getSumCreditNotesUsed();
1758 //$amounttopay = $this->total_ttc - $alreadypayed - $amount_credit_notes_included;
1759 $amounttopay = $amount;
1760
1761 // Correct the amount according to unit of currency
1762 // See https://support.stripe.com/questions/which-zero-decimal-currencies-does-stripe-support
1763 $arrayzerounitcurrency = ['BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'VND', 'VUV', 'XAF', 'XOF', 'XPF'];
1764 $amountstripe = $amounttopay;
1765 if (!in_array($currency, $arrayzerounitcurrency)) {
1766 $amountstripe *= 100;
1767 }
1768
1769 $fk_bank_account = getDolGlobalInt('STRIPE_BANK_ACCOUNT_FOR_PAYMENTS'); // Bank account used for SEPA direct debit or credit transfer. Must be the Stripe account in Dolibarr.
1770 if (!($fk_bank_account > 0)) {
1771 $error++;
1772 $errorforinvoice++;
1773 dol_syslog("makeStripeSepaRequest Error no bank account defined for Stripe payments", LOG_ERR);
1774 $this->error = "Error bank account for Stripe payments not defined into Stripe module";
1775 $this->errors[] = $this->error;
1776 }
1777
1778 $this->db->begin();
1779
1780 // Create a prelevement_bon
1781 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1782 $bon = new BonPrelevement($this->db);
1783 if (!$error) {
1784 if (empty($obj->fk_prelevement_bons)) {
1785 // This creates a record into llx_prelevement_bons and updates link with llx_prelevement_demande
1786 $nbinvoices = $bon->create('0', '0', 'real', $sepaSequenceType, 0, 0, $type, $did, $fk_bank_account);
1787 if ($nbinvoices <= 0) {
1788 $error++;
1789 $errorforinvoice++;
1790 dol_syslog("makeStripeSepaRequest Error on BonPrelevement creation", LOG_ERR);
1791 $this->error = "Error on BonPrelevement creation";
1792 $this->errors[] = $this->error;
1793 }
1794 /*
1795 if (!$error) {
1796 // Update the direct debit payment request of the processed request to save the id of the prelevement_bon
1797 $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_demande SET";
1798 $sql .= " fk_prelevement_bons = ".((int) $bon->id);
1799 $sql .= " WHERE rowid = ".((int) $did);
1800
1801 $result = $this->db->query($sql);
1802 if ($result < 0) {
1803 $error++;
1804 $this->error = "Error on updating fk_prelevement_bons to ".$bon->id;
1805 $this->errors[] = $this->error;
1806 }
1807 }
1808 */
1809 } else {
1810 $error++;
1811 $errorforinvoice++;
1812 dol_syslog("makeStripeSepaRequest Error Line already part of a bank payment order", LOG_ERR);
1813 $this->error = "The line is already included into a bank payment order. Delete the bank payment order first.";
1814 $this->errors[] = $this->error;
1815 }
1816 }
1817
1818 $paymentintent = null;
1819 if (!$error) {
1820 if ($amountstripe > 0) {
1821 try {
1822 global $savstripearrayofkeysbyenv;
1823 global $stripearrayofkeysbyenv;
1824 $servicestatus = 0;
1825 if ($service == 'StripeLive') {
1826 $servicestatus = 1;
1827 }
1828
1829 //var_dump($companypaymentmode);
1830 dol_syslog("makeStripeSepaRequest We will try to pay with companypaymentmodeid=" . $companypaymentmode->id . " stripe_card_ref=" . $companypaymentmode->stripe_card_ref . " mode=" . $companypaymentmode->status, LOG_DEBUG);
1831
1832 $thirdparty = new Societe($this->db);
1833 $resultthirdparty = $thirdparty->fetch($this->socid);
1834
1835 include_once DOL_DOCUMENT_ROOT . '/stripe/class/stripe.class.php'; // This include the include of htdocs/stripe/config.php
1836 // So it inits or erases the $stripearrayofkeysbyenv
1837 $stripe = new Stripe($this->db);
1838
1839 if (empty($savstripearrayofkeysbyenv)) {
1840 $savstripearrayofkeysbyenv = $stripearrayofkeysbyenv;
1841 }
1842 dol_syslog("makeStripeSepaRequest Current Stripe environment is " . $stripearrayofkeysbyenv[$servicestatus]['publishable_key']);
1843 dol_syslog("makeStripeSepaRequest Current Saved Stripe environment is ".$savstripearrayofkeysbyenv[$servicestatus]['publishable_key']);
1844
1845 $foundalternativestripeaccount = '';
1846
1847 // Force stripe to another value (by default this value is empty)
1848 if (! empty($forcestripe)) {
1849 dol_syslog("makeStripeSepaRequest A dedicated stripe account was forced, so we switch to it.");
1850
1851 $tmparray = explode('@', $forcestripe);
1852 if (! empty($tmparray[1])) {
1853 $tmparray2 = explode(':', $tmparray[1]);
1854 if (! empty($tmparray2[1])) {
1855 $stripearrayofkeysbyenv[$servicestatus]["publishable_key"] = $tmparray2[0];
1856 $stripearrayofkeysbyenv[$servicestatus]["secret_key"] = $tmparray2[1];
1857
1858 $stripearrayofkeys = $stripearrayofkeysbyenv[$servicestatus];
1859 \Stripe\Stripe::setApiKey($stripearrayofkeys['secret_key']);
1860
1861 $foundalternativestripeaccount = $tmparray[0]; // Store the customer id
1862
1863 dol_syslog("makeStripeSepaRequest We use now customer=".$foundalternativestripeaccount." publishable_key=".$stripearrayofkeys['publishable_key'], LOG_DEBUG);
1864 }
1865 }
1866
1867 if (! $foundalternativestripeaccount) {
1868 $stripearrayofkeysbyenv = $savstripearrayofkeysbyenv;
1869
1870 $stripearrayofkeys = $savstripearrayofkeysbyenv[$servicestatus];
1871 \Stripe\Stripe::setApiKey($stripearrayofkeys['secret_key']);
1872 dol_syslog("makeStripeSepaRequest We found a bad value for Stripe Account for thirdparty id=".$thirdparty->id.", so we ignore it and keep using the global one, so ".$stripearrayofkeys['publishable_key'], LOG_WARNING);
1873 }
1874 } else {
1875 $stripearrayofkeysbyenv = $savstripearrayofkeysbyenv;
1876
1877 $stripearrayofkeys = $savstripearrayofkeysbyenv[$servicestatus];
1878 \Stripe\Stripe::setApiKey($stripearrayofkeys['secret_key']);
1879 dol_syslog("makeStripeSepaRequest No dedicated Stripe Account requested, so we use global one, so ".$stripearrayofkeys['publishable_key'], LOG_DEBUG);
1880 }
1881
1882 $stripeacc = $stripe->getStripeAccount($service, $this->socid); // Get Stripe OAuth connect account if it exists (no network access here)
1883
1884 if ($foundalternativestripeaccount) {
1885 if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
1886 $customer = \Stripe\Customer::retrieve(array('id' => "$foundalternativestripeaccount", 'expand[]' => 'sources'));
1887 } else {
1888 $customer = \Stripe\Customer::retrieve(array('id' => "$foundalternativestripeaccount", 'expand[]' => 'sources'), array("stripe_account" => $stripeacc));
1889 }
1890 } else {
1891 $customer = $stripe->customerStripe($thirdparty, $stripeacc, $servicestatus, 0);
1892 if (empty($customer) && ! empty($stripe->error)) {
1893 $this->error = $stripe->error;
1894 $this->errors[] = $this->error;
1895 }
1896 /*if (!empty($customer) && empty($customer->sources)) {
1897 $customer = null;
1898 $this->error = '\Stripe\Customer::retrieve did not returned the sources';
1899 $this->errors[] = $this->error;
1900 }*/
1901 }
1902
1903 // $nbhoursbetweentries = (empty($conf->global->SELLYOURSAAS_NBHOURSBETWEENTRIES) ? 49 : $conf->global->SELLYOURSAAS_NBHOURSBETWEENTRIES); // Must have more that 48 hours + 1 between each try (so 1 try every 3 daily batch)
1904 // $nbdaysbeforeendoftries = (empty($conf->global->SELLYOURSAAS_NBDAYSBEFOREENDOFTRIES) ? 35 : $conf->global->SELLYOURSAAS_NBDAYSBEFOREENDOFTRIES);
1905 $postactionmessages = [];
1906
1907 if ($resultthirdparty > 0 && !empty($customer)) {
1908 if (!$error) { // Payment was not canceled
1909 $stripecard = null;
1910 if ($companypaymentmode->type == 'ban') {
1911 // Check into societe_rib if a payment mode for Stripe and ban payment exists
1912 // To make a Stripe SEPA payment request, we must have the payment mode source already saved into societe_rib and retrieved with ->sepaStripe
1913 // The payment mode source is created when we create the bank account on Stripe with paymentmodes.php?action=create
1914 $stripecard = $stripe->sepaStripe($customer, $companypaymentmode, $stripeacc, $servicestatus, 0);
1915 } else {
1916 $error++;
1917 $this->error = 'The payment mode type is not "ban"';
1918 }
1919
1920 if ($stripecard) { // Can be src_... (for sepa) or pm_... (new card mode). Note that card_... (old card mode) should not happen here.
1921 $FULLTAG = 'DID='.$did.'-INV=' . $this->id . '-CUS=' . $thirdparty->id;
1922 $description = 'Stripe payment from makeStripeSepaRequest: ' . $FULLTAG . ' did='.$did.' ref=' . $this->ref;
1923
1924 $stripefailurecode = '';
1925 $stripefailuremessage = '';
1926 $stripefailuredeclinecode = '';
1927
1928 // Using new SCA method
1929 dol_syslog("* Create payment on SEPA " . $stripecard->id . ", amounttopay=" . $amounttopay . ", amountstripe=" . $amountstripe . ", FULLTAG=" . $FULLTAG, LOG_DEBUG);
1930
1931 // Create payment intent and charge payment (confirmnow = true)
1932 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $this, $customer->id, $stripeacc, $servicestatus, 0, 'automatic', true, $stripecard->id, 1, 1, $did);
1933
1934 $charge = new stdClass();
1935
1936 if ($paymentintent->status === 'succeeded' || $paymentintent->status === 'processing') {
1937 $charge->status = 'ok';
1938 $charge->id = $paymentintent->id;
1939 $charge->customer = $customer->id;
1940 } elseif ($paymentintent->status === 'requires_action') {
1941 //paymentintent->status may be => 'requires_action' (no error in such a case)
1942 dol_syslog(formatLogObject($paymentintent), LOG_DEBUG);
1943
1944 $charge->status = 'failed';
1945 $charge->customer = $customer->id;
1946 $charge->failure_code = $stripe->code;
1947 $charge->failure_message = $stripe->error;
1948 $charge->failure_declinecode = $stripe->declinecode;
1949 $stripefailurecode = $stripe->code;
1950 $stripefailuremessage = 'Action required. Contact the support at ';// . $conf->global->SELLYOURSAAS_MAIN_EMAIL;
1951 $stripefailuredeclinecode = $stripe->declinecode;
1952 } else {
1953 dol_syslog(formatLogObject($paymentintent), LOG_DEBUG);
1954
1955 $charge->status = 'failed';
1956 $charge->customer = $customer->id;
1957 $charge->failure_code = $stripe->code;
1958 $charge->failure_message = $stripe->error;
1959 $charge->failure_declinecode = $stripe->declinecode;
1960 $stripefailurecode = $stripe->code;
1961 $stripefailuremessage = $stripe->error;
1962 $stripefailuredeclinecode = $stripe->declinecode;
1963 }
1964
1965 //var_dump("stripefailurecode=".$stripefailurecode." stripefailuremessage=".$stripefailuremessage." stripefailuredeclinecode=".$stripefailuredeclinecode);
1966 //exit;
1967
1968
1969 // Return $charge = array('id'=>'ch_XXXX', 'status'=>'succeeded|pending|failed', 'failure_code'=>, 'failure_message'=>...)
1970 if (empty($charge) || $charge->status == 'failed') {
1971 dol_syslog('Failed to charge payment mode ' . $stripecard->id . ' stripefailurecode=' . $stripefailurecode . ' stripefailuremessage=' . $stripefailuremessage . ' stripefailuredeclinecode=' . $stripefailuredeclinecode, LOG_WARNING);
1972
1973 // Save a stripe payment was in error
1974 $this->stripechargeerror++;
1975
1976 $error++;
1977 $errorforinvoice++;
1978 $errmsg = $langs->trans("FailedToChargeSEPA");
1979 if (!empty($charge)) {
1980 if ($stripefailuredeclinecode == 'authentication_required') {
1981 $errauthenticationmessage = $langs->trans("ErrSCAAuthentication");
1982 $errmsg = $errauthenticationmessage;
1983 } elseif (in_array($stripefailuredeclinecode, ['insufficient_funds', 'generic_decline'])) {
1984 $errmsg .= ': ' . $charge->failure_code;
1985 $errmsg .= ($charge->failure_message ? ' - ' : '') . ' ' . $charge->failure_message;
1986 if (empty($stripefailurecode)) {
1987 $stripefailurecode = $charge->failure_code;
1988 }
1989 if (empty($stripefailuremessage)) {
1990 $stripefailuremessage = $charge->failure_message;
1991 }
1992 } else {
1993 $errmsg .= ': failure_code=' . $charge->failure_code;
1994 $errmsg .= ($charge->failure_message ? ' - ' : '') . ' failure_message=' . $charge->failure_message;
1995 if (empty($stripefailurecode)) {
1996 $stripefailurecode = $charge->failure_code;
1997 }
1998 if (empty($stripefailuremessage)) {
1999 $stripefailuremessage = $charge->failure_message;
2000 }
2001 }
2002 } else {
2003 $errmsg .= ': ' . $stripefailurecode . ' - ' . $stripefailuremessage;
2004 $errmsg .= ($stripefailuredeclinecode ? ' - ' . $stripefailuredeclinecode : '');
2005 }
2006
2007 $description = 'Stripe payment ERROR from makeStripeSepaRequest: ' . $FULLTAG;
2008 $postactionmessages[] = $errmsg . ' (' . $stripearrayofkeys['publishable_key'] . ')';
2009
2010 $this->error = $errmsg;
2011 $this->errors[] = $this->error;
2012 } else {
2013 dol_syslog('Successfuly request '.$type.' '.$stripecard->id);
2014
2015 $postactionmessages[] = 'Success to request '.$type.' (' . $charge->id . ' with ' . $stripearrayofkeys['publishable_key'] . ')';
2016
2017 // Save a stripe payment was done in real life so later we will be able to force a commit on recorded payments
2018 // even if in batch mode (method doTakePaymentStripe), we will always make all action in one transaction with a forced commit.
2019 $this->stripechargedone++;
2020
2021 // Default description used for label of event. Will be overwrite by another value later.
2022 $description = 'Stripe payment request OK (' . $charge->id . ') from makeStripeSepaRequest: ' . $FULLTAG;
2023 }
2024
2025 $object = $this;
2026
2027 // Track an event
2028 if (empty($charge) || $charge->status == 'failed') {
2029 $actioncode = 'PAYMENT_STRIPE_KO';
2030 $extraparams = $stripefailurecode;
2031 $extraparams .= (($extraparams && $stripefailuremessage) ? ' - ' : '') . $stripefailuremessage;
2032 $extraparams .= (($extraparams && $stripefailuredeclinecode) ? ' - ' : '') . $stripefailuredeclinecode;
2033 } else {
2034 $actioncode = 'PAYMENT_STRIPE_OK';
2035 $extraparams = array();
2036 }
2037 } else {
2038 $error++;
2039 $errorforinvoice++;
2040 dol_syslog("No ban payment method found for this stripe customer " . $customer->id, LOG_WARNING);
2041
2042 $this->error = 'Failed to get direct debit payment method for stripe customer = ' . $customer->id;
2043 $this->errors[] = $this->error;
2044
2045 $description = 'Failed to find or use the payment mode - no ban defined for the thirdparty account';
2046 $stripefailurecode = 'BADPAYMENTMODE';
2047 $stripefailuremessage = 'Failed to find or use the payment mode - no ban defined for the thirdparty account';
2048 $postactionmessages[] = $description . ' (' . $stripearrayofkeys['publishable_key'] . ')';
2049
2050 $object = $this;
2051
2052 $actioncode = 'PAYMENT_STRIPE_KO';
2053 $extraparams = array();
2054 }
2055 } else {
2056 // If error because payment was canceled for a logical reason, we do nothing (no event added)
2057 $description = '';
2058 $stripefailurecode = '';
2059 $stripefailuremessage = '';
2060
2061 $object = $this;
2062
2063 $actioncode = '';
2064 $extraparams = array();
2065 }
2066 } else { // Else of the if ($resultthirdparty > 0 && ! empty($customer)) {
2067 if ($resultthirdparty <= 0) {
2068 dol_syslog('SellYourSaasUtils Failed to load customer for thirdparty_id = ' . $thirdparty->id, LOG_WARNING);
2069 $this->error = 'Failed to load Stripe account for thirdparty_id = ' . $thirdparty->id;
2070 $this->errors[] = $this->error;
2071 } else { // $customer stripe not found
2072 dol_syslog('SellYourSaasUtils Failed to get Stripe customer id for thirdparty_id = ' . $thirdparty->id . " in mode " . $servicestatus . " in Stripe env " . $stripearrayofkeysbyenv[$servicestatus]['publishable_key'], LOG_WARNING);
2073 $this->error = 'Failed to get Stripe account id for thirdparty_id = ' . $thirdparty->id . " in mode " . $servicestatus . " in Stripe env " . $stripearrayofkeysbyenv[$servicestatus]['publishable_key'];
2074 $this->errors[] = $this->error;
2075 }
2076 $error++;
2077 $errorforinvoice++;
2078
2079 $description = 'Failed to find or use your payment mode (no payment mode for this customer id)';
2080 $stripefailurecode = 'BADPAYMENTMODE';
2081 $stripefailuremessage = 'Failed to find or use your payment mode (no payment mode for this customer id)';
2082 $postactionmessages = [];
2083
2084 $object = $this;
2085
2086 $actioncode = 'PAYMENT_STRIPE_KO';
2087 $extraparams = array();
2088 }
2089
2090 if ($description) {
2091 dol_syslog("* Record event for credit transfer or direct debit request result - " . $description);
2092 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
2093
2094 // Insert record of payment (success or error)
2095 $actioncomm = new ActionComm($this->db);
2096
2097 $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
2098 $actioncomm->code = 'AC_' . $actioncode;
2099 $actioncomm->label = $description;
2100 $actioncomm->note_private = implode(",\n", $postactionmessages);
2101 $actioncomm->fk_project = $this->fk_project;
2102 $actioncomm->datep = $now;
2103 $actioncomm->datef = $now;
2104 $actioncomm->percentage = -1; // Not applicable
2105 $actioncomm->socid = $thirdparty->id;
2106 $actioncomm->contactid = 0;
2107 $actioncomm->authorid = $user->id; // User saving action
2108 $actioncomm->userownerid = $user->id; // Owner of action
2109 // Fields when action is a real email (content is already into note)
2110 /*$actioncomm->email_msgid = $object->email_msgid;
2111 $actioncomm->email_from = $object->email_from;
2112 $actioncomm->email_sender= $object->email_sender;
2113 $actioncomm->email_to = $object->email_to;
2114 $actioncomm->email_tocc = $object->email_tocc;
2115 $actioncomm->email_tobcc = $object->email_tobcc;
2116 $actioncomm->email_subject = $object->email_subject;
2117 $actioncomm->errors_to = $object->errors_to;*/
2118 $actioncomm->fk_element = $this->id;
2119 $actioncomm->elementid = $this->id;
2120 $actioncomm->elementtype = $this->element;
2121 $actioncomm->extraparams = $extraparams; // Can be null, empty string or array()
2122
2123 $actioncomm->create($user);
2124 }
2125
2126 $this->description = $description;
2127 $this->postactionmessages = $postactionmessages;
2128 } catch (Exception $e) {
2129 $error++;
2130 $errorforinvoice++;
2131 dol_syslog('Error ' . $e->getMessage(), LOG_ERR);
2132 $this->error = 'Error ' . $e->getMessage();
2133 $this->errors[] = $this->error;
2134 }
2135 } else { // If remain to pay is null
2136 $error++;
2137 $errorforinvoice++;
2138 dol_syslog("Remain to pay is null for the invoice " . $this->id . " " . $this->ref . ". Why is the invoice not classified 'Paid' ?", LOG_WARNING);
2139 $this->error = "Remain to pay is null for the invoice " . $this->id . " " . $this->ref . ". Why is the invoice not classified 'Paid' ?";
2140 $this->errors[] = $this->error;
2141 }
2142 }
2143
2144 // Set status of the order to "Transferred" with method 'api'
2145 if (!$error && !$errorforinvoice) {
2146 $result = $bon->set_infotrans($user, $now, 3);
2147 if ($result < 0) {
2148 $error++;
2149 $errorforinvoice++;
2150 dol_syslog("Error on BonPrelevement creation", LOG_ERR);
2151 $this->error = "Error on BonPrelevement creation";
2152 $this->errors[] = $this->error;
2153 }
2154 }
2155
2156 if (!$error && !$errorforinvoice && $paymentintent !== null) {
2157 // Update the direct debit payment request of the processed invoice to save the id of the prelevement_bon
2158 $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_demande SET";
2159 $sql .= " ext_payment_id = '".$this->db->escape($paymentintent->id)."',";
2160 $sql .= " ext_payment_site = '".$this->db->escape($service)."'";
2161 $sql .= " WHERE rowid = ".((int) $did);
2162
2163 dol_syslog(get_class($this)."::makeStripeSepaRequest update to save stripe paymentintent ids", LOG_DEBUG);
2164 $resql = $this->db->query($sql);
2165 if (!$resql) {
2166 $this->error = $this->db->lasterror();
2167 dol_syslog(get_class($this).'::makeStripeSepaRequest Erreur');
2168 $error++;
2169 }
2170 }
2171
2172 if (!$error && !$errorforinvoice) {
2173 $this->db->commit();
2174 } else {
2175 $this->db->rollback();
2176 }
2177 } else {
2178 $this->error = 'WithdrawRequestErrorNilAmount';
2179 dol_syslog(get_class($this).'::makeStripeSepaRequest WithdrawRequestErrorNilAmount');
2180 $error++;
2181 }
2182
2183 /*
2184 if (!$error) {
2185 // Force payment mode of the invoice to withdraw
2186 $payment_mode_id = dol_getIdFromCode($this->db, ($type == 'bank-transfer' ? 'VIR' : 'PRE'), 'c_paiement', 'code', 'id', 1);
2187 if ($payment_mode_id > 0) {
2188 $result = $this->setPaymentMethods($payment_mode_id);
2189 }
2190 }*/
2191
2192 if ($error) {
2193 return -1;
2194 }
2195 return 1;
2196 } else {
2197 $this->error = $this->db->error();
2198 dol_syslog(get_class($this).'::makeStripeSepaRequest Erreur -2');
2199 return -2;
2200 }
2201 } else {
2202 $this->error = "Status of invoice does not allow this";
2203 dol_syslog(get_class($this)."::makeStripeSepaRequest ".$this->error." ".$this->status." ,".$this->paye.", ".$this->mode_reglement_id, LOG_WARNING);
2204 return -3;
2205 }
2206 }
2207
2208 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2216 public function demande_prelevement_delete($fuser, $did)
2217 {
2218 // phpcs:enable
2219 $sql = 'DELETE FROM '.$this->db->prefix().'prelevement_demande';
2220 $sql .= ' WHERE rowid = '.((int) $did);
2221 $sql .= ' AND traite = 0';
2222 if ($this->db->query($sql)) {
2223 return 0;
2224 } else {
2225 $this->error = $this->db->lasterror();
2226 dol_syslog(get_class($this).'::demande_prelevement_delete Error '.$this->error);
2227 return -1;
2228 }
2229 }
2230
2236 public function buildEPCQrCodeString()
2237 {
2238 global $mysoc;
2239
2240 // Get the amount to pay
2241 $amount_to_pay = $this->getRemainToPay();
2242
2243 // Prevent negative values (e.g. overpayments)
2244 $amount_to_pay = max(0, $amount_to_pay);
2245
2246 // Ensure numeric formatting for EPC QR code
2247 $amount_to_pay = price2num($amount_to_pay, 'MT');
2248
2249 // Initialize an array to hold the lines of the QR code
2250 $lines = array();
2251
2252 // Add the standard elements to the QR code
2253 $lines = [
2254 'BCD', // Service Tag (optional)
2255 '002', // Version (optional)
2256 '1', // Character set (optional)
2257 'SCT', // Identification (optional)
2258 ];
2259
2260 // Add the bank account information
2261 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
2262
2263 $idofbankaccountouse = $this->fk_account;
2264 if (empty($idofbankaccountouse)) {
2265 $idofbankaccountouse = $this->fk_bank; // for backward compatibility
2266 }
2267 if (empty($idofbankaccountouse)) {
2268 $idofbankaccountouse = getDolGlobalInt('FACTURE_RIB_NUMBER');
2269 }
2270
2271 if ($idofbankaccountouse > 0) {
2272 $bankAccount = new Account($this->db);
2273 $bankAccount->fetch($idofbankaccountouse);
2274 $lines[] = $bankAccount->bic; //BIC (required)
2275 if (!empty($bankAccount->owner_name)) {
2276 $lines[] = $bankAccount->owner_name; //Owner of the bank account, if present (required)
2277 } else {
2278 $lines[] = $mysoc->name; //Name (required)
2279 }
2280 $lines[] = $bankAccount->iban; //IBAN (required)
2281 } else {
2282 $lines[] = ""; //BIC (required)
2283 $lines[] = $mysoc->name; //Name (required)
2284 $lines[] = ""; //IBAN (required)
2285 }
2286
2287 // Add the amount and reference
2288 $lines[] = 'EUR' . $amount_to_pay; // Amount (optional)
2289 $lines[] = ''; // Purpose (optional)
2290 $lines[] = ''; // Payment reference (optional)
2291 $lines[] = $this->ref; // Remittance Information (optional)
2292
2293 // Join the lines with newline characters and return the result
2294 return implode("\n", $lines);
2295 }
2301 public function buildZATCAQRString()
2302 {
2303 global $conf, $mysoc;
2304
2305 $tmplang = new Translate('', $conf);
2306 $tmplang->setDefaultLang('en_US');
2307 $tmplang->load("main");
2308
2309 $datestring = dol_print_date($this->date, 'dayhourrfc');
2310 //$pricewithtaxstring = price($this->total_ttc, 0, $tmplang, 0, -1, 2);
2311 //$pricetaxstring = price($this->total_tva, 0, $tmplang, 0, -1, 2);
2312 $pricewithtaxstring = price2num($this->total_ttc, 2, 1);
2313 $pricetaxstring = price2num($this->total_tva, 2, 1);
2314
2315 /*
2316 $name = implode(unpack("H*", $this->thirdparty->name));
2317 $vatnumber = implode(unpack("H*", $this->thirdparty->tva_intra));
2318 $date = implode(unpack("H*", $datestring));
2319 $pricewithtax = implode(unpack("H*", price2num($pricewithtaxstring, 2)));
2320 $pricetax = implode(unpack("H*", $pricetaxstring));
2321
2322 //var_dump(strlen($this->thirdparty->name));
2323 //var_dump(str_pad(dechex('9'), 2, '0', STR_PAD_LEFT));
2324 //var_dump($this->thirdparty->name);
2325 //var_dump(implode(unpack("H*", $this->thirdparty->name)));
2326 //var_dump(price($this->total_tva, 0, $tmplang, 0, -1, 2));
2327
2328 $s = '01'.str_pad(dechex(strlen($this->thirdparty->name)), 2, '0', STR_PAD_LEFT).$name;
2329 $s .= '02'.str_pad(dechex(strlen($this->thirdparty->tva_intra)), 2, '0', STR_PAD_LEFT).$vatnumber;
2330 $s .= '03'.str_pad(dechex(strlen($datestring)), 2, '0', STR_PAD_LEFT).$date;
2331 $s .= '04'.str_pad(dechex(strlen($pricewithtaxstring)), 2, '0', STR_PAD_LEFT).$pricewithtax;
2332 $s .= '05'.str_pad(dechex(strlen($pricetaxstring)), 2, '0', STR_PAD_LEFT).$pricetax;
2333 $s .= ''; // Hash of xml invoice
2334 $s .= ''; // ecda signature
2335 $s .= ''; // ecda public key
2336 $s .= ''; // ecda signature of public key stamp
2337 */
2338 $mysocname = $mysoc->name ?? '';
2339 $mysoctva_intra = $mysoc->tva_intra ?? '';
2340 // Using TLV format
2341 $s = pack('C1', 1).pack('C1', strlen($mysocname)).$mysocname;
2342 $s .= pack('C1', 2).pack('C1', strlen($mysoctva_intra)).$mysoctva_intra;
2343 $s .= pack('C1', 3).pack('C1', strlen($datestring)).$datestring;
2344 $s .= pack('C1', 4).pack('C1', strlen($pricewithtaxstring)).$pricewithtaxstring;
2345 $s .= pack('C1', 5).pack('C1', strlen($pricetaxstring)).$pricetaxstring;
2346 $s .= ''; // Hash of xml invoice
2347 $s .= ''; // ecda signature
2348 $s .= ''; // ecda public key
2349 $s .= ''; // ecda signature of public key stamp
2350
2351 $s = base64_encode($s);
2352
2353 return $s;
2354 }
2355
2356
2363 {
2364 global $conf, $mysoc;
2365
2366 $tmplang = new Translate('', $conf);
2367 $tmplang->setDefaultLang('en_US');
2368 $tmplang->load("main");
2369
2370 $pricewithtaxstring = price2num($this->total_ttc, 2, 1);
2371 $pricetaxstring = price2num($this->total_tva, 2, 1);
2372
2373 $complementaryinfo = '';
2374 /*
2375 Example: //S1/10/10201409/11/190512/20/1400.000-53/30/106017086/31/180508/32/7.7/40/2:10;0:30
2376 /10/ Invoice number -- 10201409
2377 /11/ Invoice date -- 12.05.2019
2378 /20/ Customer reference -- 1400.000-53
2379 /30/ VAT IDE number -- CHE-106.017.086 TVA
2380 /31/ Service date for VAT accounting -- 08.05.2018
2381 /32/ VAT rate on total invoice amount -- 7.7%
2382 /40/ Terms -- 2% discount at 10 days, net payment at 30 days
2383 */
2384 $datestring = dol_print_date($this->date, '%y%m%d');
2385 //$pricewithtaxstring = price($this->total_ttc, 0, $tmplang, 0, -1, 2);
2386 //$pricetaxstring = price($this->total_tva, 0, $tmplang, 0, -1, 2);
2387 $complementaryinfo = '//S1/10/'.str_replace('/', '', $this->ref).'/11/'.$datestring;
2388 if ($this->ref_client) {
2389 $complementaryinfo .= '/20/'.$this->ref_client;
2390 }
2391 if ($this->thirdparty->tva_intra) {
2392 $complementaryinfo .= '/30/'.$this->thirdparty->tva_intra;
2393 }
2394
2395 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
2396 $bankaccount = new Account($this->db);
2397
2398 // Header
2399 $s = '';
2400 $s .= "SPC\n";
2401 $s .= "0200\n";
2402 $s .= "1\n";
2403 // Info Seller ("Account / Payable to")
2404 if ($this->fk_account > 0) {
2405 // Bank BAN if country is LI or CH. TODO Add a test to check than IBAN start with CH or LI
2406 $bankaccount->fetch($this->fk_account);
2407 $s .= $bankaccount->iban."\n";
2408 } else {
2409 $s .= "\n";
2410 }
2411 if ($bankaccount->id > 0 && getDolGlobalString('PDF_SWISS_QRCODE_USE_OWNER_OF_ACCOUNT_AS_CREDITOR')) {
2412 // If a bank account is provided and we ask to use it as creditor, we use the bank address
2413 // TODO In a future, we may always use this address, and if name/address/zip/town/country differs from $mysoc, we can use the address of $mysoc into the final seller field ?
2414 $s .= "S\n";
2415 $s .= dol_trunc($bankaccount->owner_name, 70, 'right', 'UTF-8', 1)."\n";
2416 $addresslinearray = explode("\n", $bankaccount->owner_address);
2417 $s .= dol_trunc(empty($addresslinearray[1]) ? '' : $addresslinearray[1], 70, 'right', 'UTF-8', 1)."\n"; // address line 1
2418 $s .= dol_trunc(empty($addresslinearray[2]) ? '' : $addresslinearray[2], 70, 'right', 'UTF-8', 1)."\n"; // address line 2
2419 /*$s .= dol_trunc($mysoc->zip, 16, 'right', 'UTF-8', 1)."\n";
2420 $s .= dol_trunc($mysoc->town, 35, 'right', 'UTF-8', 1)."\n";
2421 $s .= dol_trunc($mysoc->country_code, 2, 'right', 'UTF-8', 1)."\n";*/
2422 } else {
2423 $s .= "S\n";
2424 $s .= dol_trunc((string) $mysoc->name, 70, 'right', 'UTF-8', 1)."\n";
2425 $addresslinearray = explode("\n", $mysoc->address);
2426 $s .= dol_trunc(empty($addresslinearray[1]) ? '' : $addresslinearray[1], 70, 'right', 'UTF-8', 1)."\n"; // address line 1
2427 $s .= dol_trunc(empty($addresslinearray[2]) ? '' : $addresslinearray[2], 70, 'right', 'UTF-8', 1)."\n"; // address line 2
2428 $s .= dol_trunc($mysoc->zip, 16, 'right', 'UTF-8', 1)."\n";
2429 $s .= dol_trunc($mysoc->town, 35, 'right', 'UTF-8', 1)."\n";
2430 $s .= dol_trunc($mysoc->country_code, 2, 'right', 'UTF-8', 1)."\n";
2431 }
2432 // Final seller (Ultimate seller) ("Final creditor" = "In favour of")
2433 $s .= "\n";
2434 $s .= "\n";
2435 $s .= "\n";
2436 $s .= "\n";
2437 $s .= "\n";
2438 $s .= "\n";
2439 $s .= "\n";
2440 // Amount of payment (to do?)
2441 $s .= price($pricewithtaxstring, 0, 'none', 0, 0, 2)."\n";
2442 $s .= ($this->multicurrency_code ? $this->multicurrency_code : $conf->currency)."\n";
2443 // Buyer
2444 $s .= "S\n";
2445 $s .= dol_trunc((string) $this->thirdparty->name, 70, 'right', 'UTF-8', 1)."\n";
2446 $addresslinearray = explode("\n", $this->thirdparty->address);
2447 $s .= dol_trunc(empty($addresslinearray[1]) ? '' : $addresslinearray[1], 70, 'right', 'UTF-8', 1)."\n"; // address line 1
2448 $s .= dol_trunc(empty($addresslinearray[2]) ? '' : $addresslinearray[2], 70, 'right', 'UTF-8', 1)."\n"; // address line 2
2449 $s .= dol_trunc($this->thirdparty->zip, 16, 'right', 'UTF-8', 1)."\n";
2450 $s .= dol_trunc($this->thirdparty->town, 35, 'right', 'UTF-8', 1)."\n";
2451 $s .= dol_trunc($this->thirdparty->country_code, 2, 'right', 'UTF-8', 1)."\n";
2452 // ID of payment
2453 $s .= "NON\n"; // NON or QRR
2454 $s .= "\n"; // QR Code reference if previous field is QRR
2455 // Free text
2456 if ($complementaryinfo) {
2457 $s .= $complementaryinfo."\n";
2458 } else {
2459 $s .= "\n";
2460 }
2461 $s .= "EPD\n";
2462 // More text, complementary info
2463 if ($complementaryinfo) {
2464 $s .= $complementaryinfo."\n";
2465 }
2466 $s .= "\n";
2467 //var_dump($s);exit;
2468 return $s;
2469 }
2470}
2471
2472
2473
2474require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
2475
2480{
2486 public $label;
2487
2493 public $ref; // Product ref (deprecated)
2499 public $libelle; // Product label (deprecated)
2500
2505 public $product_type = 0;
2506
2511 public $product_ref;
2512
2517 public $product_label;
2518
2523 public $product_desc;
2524
2529 public $qty;
2530
2535 public $subprice;
2536
2542 public $price;
2543
2548 public $fk_product;
2549
2554 public $vat_src_code;
2555
2560 public $tva_tx;
2561
2566 public $localtax1_tx;
2567
2572 public $localtax2_tx;
2573
2579 public $localtax1_type;
2580
2586 public $localtax2_type;
2587
2592 public $remise_percent;
2593
2599 public $remise;
2600
2605 public $total_ht;
2606
2611 public $total_tva;
2612
2617 public $total_localtax1;
2618
2623 public $total_localtax2;
2624
2629 public $total_ttc;
2630
2634 public $revenuestamp;
2635
2636
2640 public $date_start_fill; // If set to 1, when invoice is created from a template invoice, it will also auto set the field date_start at creation
2644 public $date_end_fill; // If set to 1, when invoice is created from a template invoice, it will also auto set the field date_end at creation
2645
2649 public $buy_price_ht;
2654 public $buyprice;
2659 public $pa_ht;
2660
2664 public $marge_tx;
2668 public $marque_tx;
2669
2676 public $info_bits = 0;
2677
2686 public $special_code = 0;
2687
2692 public $fk_user_author;
2693
2698 public $fk_user_modif;
2699
2703 public $fk_code_ventilation;
2704
2708 public $situation_percent = 100;
2709
2715 public function isDepositLine()
2716 {
2717 // Do not take into account lines of the type "deposit."
2718 $reg = array();
2719 if (preg_match('/^\‍((.*)\‍)$/', $this->desc, $reg)) {
2720 if ($reg[1] == 'DEPOSIT') {
2721 return true;
2722 }
2723 }
2724 return false;
2725 }
2726}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
isALNERunningVersion($blockedlogtestalreadydone=0, $blockedlogmodulealreadydone=0)
Return if the application is executed with the LNE requirements on.
$object ref
Definition info.php:90
Class to manage bank accounts.
Class to manage agenda events (actions)
Class to manage withdrawal receipts.
Superclass for invoice classes.
const TYPE_CREDIT_NOTE
Credit note invoice.
const STATUS_CLOSED
Classified paid.
getSumCreditNotesUsed($multicurrency=0)
Return amount (with tax) of all credit notes invoices + excess received used by invoice.
getRemainToPay($multicurrency=0)
Return remain amount to pay.
buildZATCAQRString()
Build string for ZATCA QR Code (Arabi Saudia)
makeStripeCardRequest($fuser, $id, $sourcetype='facture')
Create a payment with Stripe card Must take amount using Stripe and record an event into llx_actionco...
getVentilExportCompta($mode=0)
Return if an invoice was transferred into accountnancy.
const TYPE_STANDARD
Standard invoice.
buildEPCQrCodeString()
Build string for EPC QR Code.
makeStripeSepaRequest($fuser, $did, $type='direct-debit', $sourcetype='facture', $service='', $forcestripe='')
Create a direct debit order into prelevement_bons for a given prelevement_request,...
getSubtypeLabel($table='')
Return label of invoice subtype.
demande_prelevement_delete($fuser, $did)
Remove a direct debit request or a credit transfer request.
const TYPE_PROFORMA
Proforma invoice.
buildSwitzerlandQRString()
Build string for QR-Bill (Switzerland)
const STATUS_VALIDATED
Validated (need to be paid)
getListOfPayments($filtertype='', $multicurrency=0, $mode=0)
Return list of payments.
getListOfOpenDirectDebitOrCreditTransfer($type)
Return list of open direct debit or credit transfer.
const TYPE_SITUATION
Situation invoice.
getSumDepositsUsed($multicurrency=0)
Return amount (with tax) of all deposits invoices used by invoice.
LibStatut($paye, $status, $mode=0, $alreadypaid=-1, $type=-1, $moreparams=array())
Return label of a status.
getSommePaiement($multicurrency=0)
Return amount of payments already done.
isReplacable()
Return if an invoice can be replaced by a Replacement invoice (also called "Correction invoice").
demande_prelevement(User $fuser, float $amount=0, string $type='direct-debit', string $sourcetype='facture', int $checkduplicateamongall=0, int $ribId=0)
Create a withdrawal request for a direct debit order or a credit transfer order.
const TYPE_DEPOSIT
Deposit invoice.
const STATUS_ABANDONED
Classified abandoned and no payment done.
calculate_date_lim_reglement($cond_reglement=0)
Returns an invoice payment deadline based on the invoice settlement conditions and billing date.
const TYPE_REPLACEMENT
Replacement invoice.
isEditable()
Return if an invoice can be set back to draft.
getSumFromThisCreditNotesNotUsed($multicurrency=0)
Return amount (with tax) of all converted amount for this credit note.
getListIdAvoirFromInvoice()
Returns array of credit note ids from the invoice.
setRetainedWarrantyPaymentTerms($id)
Change the retained warranty payments terms.
const STATUS_DRAFT
Draft status.
getIdReplacingInvoice($option='')
Returns the id of the invoice that replaces it.
getLibType($withbadge=0)
Return label of type of invoice.
is_erasable()
Return if an invoice can be deleted Rule is: If invoice is draft and has a temporary ref -> yes (1) I...
getArrayOfInvoiceSubtypes($mode=0)
Retrieve a list of invoice subtype labels or codes.
getLibStatut($mode=0, $alreadypaid=-1)
Return label of object status.
getNextNumRef($soc, $mode='next')
Return next reference of invoice not already used (or last reference)
Parent class of all other business classes for details of elements (invoices, contracts,...
isDepositLine()
Check if a line is a deposit line.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
setPaymentMethods($id)
Change the payments methods.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage bank accounts description of third parties.
Class for CompanyPaymentMode.
Class to manage absolute discounts.
Class to manage invoices.
Class to manage third parties objects (customers, suppliers, prospects...)
Stripe class @TODO No reason to extend CommonObject.
Class to manage translations.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
global $mysoc
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0, $tz='')
Add a delay to a date.
Definition date.lib.php:129
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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...
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
if(!function_exists( 'utf8_encode')) if(!function_exists('utf8_decode')) if(!function_exists( 'str_starts_with')) if(!function_exists('str_ends_with')) if(!function_exists( 'str_contains')) formatLogObject($data)
Return a string serialized to be output on log with dol_syslog() An option allow to output log in one...
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...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
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.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
print $langs trans('Date')." left Ref 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 Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:492
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:137