26 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
27 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonincoterm.class.php';
97 public $totaldeposits;
98 public $totalcreditnotes;
101 public $sumpayed_multicurrency;
103 public $sumdeposit_multicurrency;
104 public $sumcreditnote;
105 public $sumcreditnote_multicurrency;
123 $remaintopay =
price2num($this->total_ttc - $alreadypaid,
'MT');
124 if ($this->statut == self::STATUS_CLOSED && $this->close_code ==
'discount_vat') {
139 $table =
'paiement_facture';
140 $field =
'fk_facture';
141 if ($this->element ==
'facture_fourn' || $this->element ==
'invoice_supplier') {
142 $table =
'paiementfourn_facturefourn';
143 $field =
'fk_facturefourn';
146 $sql =
"SELECT sum(amount) as amount, sum(multicurrency_amount) as multicurrency_amount";
147 $sql .=
" FROM ".$this->db->prefix().$table;
148 $sql .=
" WHERE ".$field.
" = ".((int) $this->
id);
150 dol_syslog(get_class($this).
"::getSommePaiement", LOG_DEBUG);
152 $resql = $this->
db->query(
$sql);
154 $obj = $this->
db->fetch_object($resql);
156 $this->
db->free($resql);
159 if ($multicurrency < 0) {
160 $this->sumpayed = $obj->amount;
161 $this->sumpayed_multicurrency = $obj->multicurrency_amount;
162 return array(
'alreadypaid'=>(
float) $obj->amount,
'alreadypaid_multicurrency'=>(
float) $obj->multicurrency_amount);
163 } elseif ($multicurrency) {
164 $this->sumpayed_multicurrency = $obj->multicurrency_amount;
165 return (
float) $obj->multicurrency_amount;
167 $this->sumpayed = $obj->amount;
168 return (
float) $obj->amount;
174 $this->error = $this->
db->lasterror();
194 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
197 $result = $discountstatic->getSumDepositsUsed($this, $multicurrency);
200 if ($multicurrency) {
201 $this->sumdeposit_multicurrency = $result;
203 $this->sumdeposit = $result;
208 $this->error = $discountstatic->error;
221 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
224 $result = $discountstatic->getSumCreditNotesUsed($this, $multicurrency);
226 if ($multicurrency) {
227 $this->sumcreditnote_multicurrency = $result;
229 $this->sumcreditnote = $result;
234 $this->error = $discountstatic->error;
247 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
250 $result = $discountstatic->getSumFromThisCreditNotesNotUsed($this, $multicurrency);
254 $this->error = $discountstatic->error;
268 $sql =
"SELECT rowid";
269 $sql .=
" FROM ".$this->db->prefix().$this->table_element;
270 $sql .=
" WHERE fk_facture_source = ".((int) $this->
id);
271 $sql .=
" AND type = 2";
272 $resql = $this->
db->query(
$sql);
274 $num = $this->
db->num_rows($resql);
277 $row = $this->
db->fetch_row($resql);
278 $idarray[] = $row[0];
295 $sql =
"SELECT rowid";
296 $sql .=
" FROM ".$this->db->prefix().$this->table_element;
297 $sql .=
" WHERE fk_facture_source = ".((int) $this->
id);
298 $sql .=
" AND type < 2";
299 if ($option ==
'validated') {
300 $sql .=
' AND fk_statut = 1';
307 $sql .=
" ORDER BY fk_statut DESC";
309 $resql = $this->
db->query(
$sql);
311 $obj = $this->
db->fetch_object($resql);
334 $table =
'paiement_facture';
335 $table2 =
'paiement';
336 $field =
'fk_facture';
337 $field2 =
'fk_paiement';
338 $field3 =
', p.ref_ext';
339 $field4 =
', p.fk_bank';
340 $sharedentity =
'facture';
341 if ($this->element ==
'facture_fourn' || $this->element ==
'invoice_supplier') {
342 $table =
'paiementfourn_facturefourn';
343 $table2 =
'paiementfourn';
344 $field =
'fk_facturefourn';
345 $field2 =
'fk_paiementfourn';
347 $sharedentity =
'facture_fourn';
350 $sql =
"SELECT p.ref, pf.amount, pf.multicurrency_amount, p.fk_paiement, p.datep, p.num_paiement as num, t.code".$field3 . $field4;
351 $sql .=
" FROM ".$this->db->prefix().$table.
" as pf, ".$this->
db->prefix().$table2.
" as p, ".$this->
db->prefix().
"c_paiement as t";
352 $sql .=
" WHERE pf.".$field.
" = ".((int) $this->
id);
353 $sql .=
" AND pf.".$field2.
" = p.rowid";
354 $sql .=
' AND p.fk_paiement = t.id';
355 $sql .=
' AND p.entity IN ('.getEntity($sharedentity).
')';
357 $sql .=
" AND t.code='PRE'";
360 dol_syslog(get_class($this).
"::getListOfPayments", LOG_DEBUG);
361 $resql = $this->
db->query(
$sql);
363 $num = $this->
db->num_rows($resql);
366 $obj = $this->
db->fetch_object($resql);
367 $tmp = array(
'amount'=>$obj->amount,
'type'=>$obj->code,
'date'=>$obj->datep,
'num'=>$obj->num,
'ref'=>$obj->ref);
368 if (!empty($field3)) {
369 $tmp[
'ref_ext'] = $obj->ref_ext;
371 if (!empty($field4)) {
372 $tmp[
'fk_bank_line'] = $obj->fk_bank;
377 $this->
db->free($resql);
381 if ($this->element ==
'facture' || $this->element ==
'invoice') {
382 $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";
383 $sql .=
' FROM '.$this->db->prefix().
'societe_remise_except as rc, '.$this->
db->prefix().
'facture as f';
384 $sql .=
' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.((int) $this->
id);
385 $sql .=
' AND (f.type = 2 OR f.type = 0 OR f.type = 3)';
386 } elseif ($this->element ==
'facture_fourn' || $this->element ==
'invoice_supplier') {
387 $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";
388 $sql .=
' FROM '.$this->db->prefix().
'societe_remise_except as rc, '.$this->
db->prefix().
'facture_fourn as f';
389 $sql .=
' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = '.((int) $this->
id);
390 $sql .=
' AND (f.type = 2 OR f.type = 0 OR f.type = 3)';
394 $resql = $this->
db->query(
$sql);
396 $num = $this->
db->num_rows($resql);
399 $obj = $this->
db->fetch_object($resql);
400 if ($multicurrency) {
401 $retarray[] = array(
'amount'=>$obj->multicurrency_amount,
'type'=>$obj->type,
'date'=>$obj->date,
'num'=>
'0',
'ref'=>$obj->ref);
403 $retarray[] = array(
'amount'=>$obj->amount,
'type'=>$obj->type,
'date'=>$obj->date,
'num'=>
'',
'ref'=>$obj->ref);
408 $this->error = $this->
db->lasterror();
412 $this->
db->free($resql);
417 $this->error = $this->
db->lasterror();
444 $tmppart = substr($this->
ref, 1, 4);
446 if ($this->statut == self::STATUS_DRAFT && $tmppart ===
'PROV') {
450 if (!empty($conf->global->INVOICE_CAN_NEVER_BE_REMOVED)) {
455 if ($tmppart !==
'PROV') {
457 if ($ventilExportCompta != 0) {
462 if ($this->element !=
'invoice_supplier') {
463 if (empty($this->thirdparty)) {
466 $maxref = $this->getNextNumRef($this->thirdparty,
'last');
470 if (empty($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED) && $maxref !=
'' && $maxref != $this->ref) {
477 if ($this->situation_cycle_ref && method_exists($this,
'is_last_in_cycle')) {
478 $last = $this->is_last_in_cycle();
487 if (empty($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED) && $this->getSommePaiement() > 0) {
501 $alreadydispatched = 0;
503 $type =
'customer_invoice';
504 if ($this->element ==
'invoice_supplier') {
505 $type =
'supplier_invoice';
508 $sql =
" SELECT COUNT(ab.rowid) as nb FROM ".$this->db->prefix().
"accounting_bookkeeping as ab WHERE ab.doc_type='".$this->
db->escape($type).
"' AND ab.fk_doc = ".((int) $this->
id);
509 $resql = $this->
db->query(
$sql);
511 $obj = $this->
db->fetch_object($resql);
513 $alreadydispatched = $obj->nb;
516 $this->error = $this->
db->lasterror();
520 if ($alreadydispatched) {
537 $labellong =
"Unknown";
539 $labellong =
"InvoiceStandard";
540 $labelshort =
"InvoiceStandardShort";
542 $labellong =
"InvoiceReplacement";
543 $labelshort =
"InvoiceReplacementShort";
545 $labellong =
"InvoiceAvoir";
546 $labelshort =
"CreditNote";
548 $labellong =
"InvoiceDeposit";
549 $labelshort =
"Deposit";
551 $labellong =
"InvoiceProForma";
552 $labelshort =
"ProForma";
554 $labellong =
"InvoiceSituation";
555 $labelshort =
"Situation";
560 $out .=
'<span class="badgeneutral" title="'.dol_escape_htmltag($langs->trans($labellong)).
'">';
562 $out .= $langs->trans($withbadge == 2 ? $labelshort : $labellong);
578 return $this->
LibStatut($this->paye, $this->statut, $mode, $alreadypaid, $this->
type);
592 public function LibStatut($paye, $status, $mode = 0, $alreadypaid = -1, $type = -1)
595 global $langs, $hookmanager;
596 $langs->load(
'bills');
602 $statusType =
'status0';
606 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusDraft');
607 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusDraft');
608 } elseif (($status == 3 || $status == 2) && $alreadypaid <= 0) {
610 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusCanceled');
611 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusCanceled');
613 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusClosedUnpaid');
614 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusClosedUnpaid');
616 $statusType =
'status5';
617 } elseif (($status == 3 || $status == 2) && $alreadypaid > 0) {
618 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusClosedPaidPartially');
619 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusClosedPaidPartially');
620 $statusType =
'status9';
621 } elseif ($alreadypaid == 0) {
622 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusNotPaid');
623 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusNotPaid');
624 $statusType =
'status1';
626 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusStarted');
627 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusStarted');
628 $statusType =
'status3';
631 $statusType =
'status6';
633 if ($type == self::TYPE_CREDIT_NOTE) {
634 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusPaidBackOrConverted');
635 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusPaidBackOrConverted');
636 } elseif ($type == self::TYPE_DEPOSIT) {
637 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusConverted');
638 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusConverted');
640 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusPaid');
641 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusPaid');
649 'alreadypaid' => $alreadypaid,
653 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
656 return $hookmanager->resPrint;
661 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
675 if (!$cond_reglement) {
676 $cond_reglement = $this->cond_reglement_code;
678 if (!$cond_reglement) {
679 $cond_reglement = $this->cond_reglement_id;
681 if (!$cond_reglement) {
689 $sqltemp =
"SELECT c.type_cdr, c.nbjour, c.decalage";
690 $sqltemp .=
" FROM ".$this->db->prefix().
"c_payment_term as c";
691 if (is_numeric($cond_reglement)) {
692 $sqltemp .=
" WHERE c.rowid=".((int) $cond_reglement);
694 $sqltemp .=
" WHERE c.entity IN (".getEntity(
'c_payment_term').
")";
695 $sqltemp .=
" AND c.code = '".$this->db->escape($cond_reglement).
"'";
698 dol_syslog(get_class($this).
'::calculate_date_lim_reglement', LOG_DEBUG);
699 $resqltemp = $this->
db->query($sqltemp);
701 if ($this->
db->num_rows($resqltemp)) {
702 $obj = $this->
db->fetch_object($resqltemp);
703 $cdr_nbjour = $obj->nbjour;
704 $cdr_type = $obj->type_cdr;
705 $cdr_decalage = $obj->decalage;
708 $this->error = $this->
db->error();
711 $this->
db->free($resqltemp);
716 if ($cdr_type == 0) {
717 $datelim = $this->date + ($cdr_nbjour * 3600 * 24);
719 $datelim += ($cdr_decalage * 3600 * 24);
720 } elseif ($cdr_type == 1) {
722 $datelim = $this->date + ($cdr_nbjour * 3600 * 24);
724 $mois = date(
'm', $datelim);
725 $annee = date(
'Y', $datelim);
733 $datelim =
dol_mktime(12, 0, 0, $mois, 1, $annee);
734 $datelim -= (3600 * 24);
736 $datelim += ($cdr_decalage * 3600 * 24);
737 } elseif ($cdr_type == 2 && !empty($cdr_decalage)) {
739 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
740 $datelim = $this->date + ($cdr_nbjour * 3600 * 24);
742 $date_piece =
dol_mktime(0, 0, 0, date(
'm', $datelim), date(
'd', $datelim), date(
'Y', $datelim));
743 $date_lim_current =
dol_mktime(0, 0, 0, date(
'm', $datelim), $cdr_decalage, date(
'Y', $datelim));
746 $diff = $date_piece - $date_lim_current;
749 $datelim = $date_lim_current;
751 $datelim = $date_lim_next;
754 return 'Bad value for type_cdr in database for record cond_reglement = '.$cond_reglement;
778 dol_syslog(get_class($this).
"::demande_prelevement", LOG_DEBUG);
780 if ($this->statut > self::STATUS_DRAFT && $this->paye == 0) {
781 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
783 $bac->fetch(0, $this->socid);
785 $sql =
"SELECT count(*)";
786 $sql .=
" FROM ".$this->db->prefix().
"prelevement_demande";
787 if ($type ==
'bank-transfer') {
788 $sql .=
" WHERE fk_facture_fourn = ".((int) $this->
id);
790 $sql .=
" WHERE fk_facture = ".((int) $this->
id);
792 $sql .=
" AND type = 'ban'";
793 $sql .=
" AND traite = 0";
795 dol_syslog(get_class($this).
"::demande_prelevement", LOG_DEBUG);
796 $resql = $this->
db->query(
$sql);
798 $row = $this->
db->fetch_row($resql);
811 if (empty($amount)) {
812 $amount =
price2num($this->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
815 if (is_numeric($amount) && $amount != 0) {
816 $sql =
'INSERT INTO '.$this->db->prefix().
'prelevement_demande(';
817 if ($type ==
'bank-transfer') {
818 $sql .=
'fk_facture_fourn, ';
820 $sql .=
'fk_facture, ';
822 $sql .=
' amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib, sourcetype, type, entity)';
823 $sql .=
" VALUES (".((int) $this->
id);
825 $sql .=
", '".$this->db->idate($now).
"'";
826 $sql .=
", ".((int) $fuser->id);
827 $sql .=
", '".$this->db->escape($bac->code_banque).
"'";
828 $sql .=
", '".$this->db->escape($bac->code_guichet).
"'";
829 $sql .=
", '".$this->db->escape($bac->number).
"'";
830 $sql .=
", '".$this->db->escape($bac->cle_rib).
"'";
831 $sql .=
", '".$this->db->escape($sourcetype).
"'";
833 $sql .=
", ".((int) $conf->entity);
836 dol_syslog(get_class($this).
"::demande_prelevement", LOG_DEBUG);
837 $resql = $this->
db->query(
$sql);
839 $this->error = $this->
db->lasterror();
840 dol_syslog(get_class($this).
'::demandeprelevement Erreur');
844 $this->error =
'WithdrawRequestErrorNilAmount';
845 dol_syslog(get_class($this).
'::demandeprelevement WithdrawRequestErrorNilAmount');
851 $payment_mode_id =
dol_getIdFromCode($this->
db, ($type ==
'bank-transfer' ?
'VIR' :
'PRE'),
'c_paiement',
'code',
'id', 1);
852 if ($payment_mode_id > 0) {
862 $this->error =
"A request already exists";
863 dol_syslog(get_class($this).
'::demandeprelevement Impossible de creer une demande, demande deja en cours');
867 $this->error = $this->
db->error();
868 dol_syslog(get_class($this).
'::demandeprelevement Erreur -2');
872 $this->error =
"Status of invoice does not allow this";
873 dol_syslog(get_class($this).
"::demandeprelevement ".$this->error.
" $this->statut, $this->paye, $this->mode_reglement_id");
891 global $conf, $mysoc, $user, $langs;
893 if (empty($conf->global->STRIPE_SEPA_DIRECT_DEBIT)) {
900 dol_syslog(get_class($this).
"::makeStripeSepaRequest start", LOG_DEBUG);
902 if ($this->statut > self::STATUS_DRAFT && $this->paye == 0) {
904 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
906 $result = $bac->fetch(0, $this->socid, 1,
'ban');
907 if ($result <= 0 || empty($bac->id)) {
908 $this->error = $langs->trans(
"ThirdpartyHasNoDefaultBanAccount");
909 $this->errors[] = $this->error;
910 dol_syslog(get_class($this).
"::makeStripeSepaRequest ".$this->error);
915 $sql =
"SELECT rowid, date_demande, amount, fk_facture, fk_facture_fourn, fk_prelevement_bons";
916 $sql .=
" FROM ".$this->db->prefix().
"prelevement_demande";
917 $sql .=
" WHERE rowid = ".((int) $did);
918 $sql .=
" AND fk_facture = ".((int) $this->
id);
919 $sql .=
" AND traite = 0";
921 dol_syslog(get_class($this).
"::makeStripeSepaRequest load requests to process", LOG_DEBUG);
922 $resql = $this->
db->query(
$sql);
924 $obj = $this->
db->fetch_object($resql);
931 $amount = $obj->amount;
933 if (is_numeric($amount) && $amount != 0) {
934 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companypaymentmode.class.php';
936 $companypaymentmode->fetch($bac->id);
940 $service =
'StripeTest';
942 if (!empty($conf->global->STRIPE_LIVE) && !
GETPOST(
'forcesandbox',
'alpha')) {
943 $service =
'StripeLive';
947 dol_syslog(
"makeStripeSepaRequest amount = ".$amount.
" service=" . $service .
" servicestatus=" . $servicestatus .
" thirdparty_id=" . $this->socid.
" did=".$did);
949 $this->stripechargedone = 0;
950 $this->stripechargeerror = 0;
954 $currency = $conf->currency;
956 global $stripearrayofkeysbyenv;
958 $errorforinvoice = 0;
962 dol_syslog(
"--- Process payment request thirdparty_id=" . $this->thirdparty->id .
", thirdparty_name=" . $this->thirdparty->name .
" ban id=" . $bac->id, LOG_DEBUG);
967 $amounttopay = $amount;
971 $arrayzerounitcurrency = [
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF'];
972 $amountstripe = $amounttopay;
973 if (!in_array($currency, $arrayzerounitcurrency)) {
974 $amountstripe = $amountstripe * 100;
977 $fk_bank_account =
getDolGlobalInt(
'STRIPE_BANK_ACCOUNT_FOR_PAYMENTS');
978 if (!($fk_bank_account > 0)) {
981 dol_syslog(
"Error no bank account defined for Stripe payments", LOG_ERR);
982 $this->errors[] =
"Error bank account for Stripe payments not defined into Stripe module";
990 if (empty($obj->fk_prelevement_bons)) {
992 $nbinvoices = $bon->create(0, 0,
'real',
'ALL',
'', 0,
'direct-debit', $did, $fk_bank_account);
993 if ($nbinvoices <= 0) {
996 dol_syslog(
"Error on BonPrelevement creation", LOG_ERR);
997 $this->errors[] =
"Error on BonPrelevement creation";
1016 dol_syslog(
"Error Line already part of a bank payment order", LOG_ERR);
1017 $this->errors[] =
"The line is already included into a bank payment order. Delete the bank payment order first.";
1022 if ($amountstripe > 0) {
1025 dol_syslog(
"We will try to pay with companypaymentmodeid=" . $companypaymentmode->id .
" stripe_card_ref=" . $companypaymentmode->stripe_card_ref .
" mode=" . $companypaymentmode->status, LOG_DEBUG);
1028 $resultthirdparty = $thirdparty->fetch($this->socid);
1030 include_once DOL_DOCUMENT_ROOT .
'/stripe/class/stripe.class.php';
1034 dol_syslog(
"makeStripeSepaRequest Current Stripe environment is " . $stripearrayofkeysbyenv[$servicestatus][
'publishable_key']);
1036 $stripearrayofkeys = $stripearrayofkeysbyenv[$servicestatus];
1037 \Stripe\Stripe::setApiKey($stripearrayofkeys[
'secret_key']);
1040 dol_syslog(
"makeStripeSepaRequest get stripe connet account", LOG_DEBUG);
1041 $stripeacc = $stripe->getStripeAccount($service, $this->socid);
1042 dol_syslog(
"makeStripeSepaRequest get stripe connect account return " . json_encode($stripeacc), LOG_DEBUG);
1044 $customer = $stripe->customerStripe($thirdparty, $stripeacc, $servicestatus, 0);
1045 if (empty($customer) && !empty($stripe->error)) {
1046 $this->errors[] = $stripe->error;
1051 $postactionmessages = [];
1053 if ($resultthirdparty > 0 && !empty($customer)) {
1057 if ($companypaymentmode->type ==
'ban') {
1062 $stripecard = $stripe->sepaStripe($customer, $companypaymentmode, $stripeacc, $servicestatus, 0);
1066 $FULLTAG =
'DID='.$did.
'-INV=' . $this->
id .
'-CUS=' . $thirdparty->id;
1067 $description =
'Stripe payment from makeStripeSepaRequest: ' . $FULLTAG .
' did='.$did.
' ref=' . $this->ref;
1069 $stripefailurecode =
'';
1070 $stripefailuremessage =
'';
1071 $stripefailuredeclinecode =
'';
1074 dol_syslog(
"* Create payment on SEPA " . $stripecard->id .
", amounttopay=" . $amounttopay .
", amountstripe=" . $amountstripe .
", FULLTAG=" . $FULLTAG, LOG_DEBUG);
1077 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $this, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1, 1, $did);
1079 $charge =
new stdClass();
1081 if ($paymentintent->status ===
'succeeded' || $paymentintent->status ===
'processing') {
1082 $charge->status =
'ok';
1083 $charge->id = $paymentintent->id;
1084 $charge->customer = $customer->id;
1085 } elseif ($paymentintent->status ===
'requires_action') {
1087 dol_syslog(var_export($paymentintent,
true), LOG_DEBUG);
1089 $charge->status =
'failed';
1090 $charge->customer = $customer->id;
1091 $charge->failure_code = $stripe->code;
1092 $charge->failure_message = $stripe->error;
1093 $charge->failure_declinecode = $stripe->declinecode;
1094 $stripefailurecode = $stripe->code;
1095 $stripefailuremessage =
'Action required. Contact the support at ';
1096 $stripefailuredeclinecode = $stripe->declinecode;
1098 dol_syslog(var_export($paymentintent,
true), LOG_DEBUG);
1100 $charge->status =
'failed';
1101 $charge->customer = $customer->id;
1102 $charge->failure_code = $stripe->code;
1103 $charge->failure_message = $stripe->error;
1104 $charge->failure_declinecode = $stripe->declinecode;
1105 $stripefailurecode = $stripe->code;
1106 $stripefailuremessage = $stripe->error;
1107 $stripefailuredeclinecode = $stripe->declinecode;
1115 if (empty($charge) || $charge->status ==
'failed') {
1116 dol_syslog(
'Failed to charge payment mode ' . $stripecard->id .
' stripefailurecode=' . $stripefailurecode .
' stripefailuremessage=' . $stripefailuremessage .
' stripefailuredeclinecode=' . $stripefailuredeclinecode, LOG_WARNING);
1119 $this->stripechargeerror++;
1123 $errmsg = $langs->trans(
"FailedToChargeCard");
1124 if (!empty($charge)) {
1125 if ($stripefailuredeclinecode ==
'authentication_required') {
1126 $errauthenticationmessage = $langs->trans(
"ErrSCAAuthentication");
1127 $errmsg = $errauthenticationmessage;
1128 } elseif (in_array($stripefailuredeclinecode, [
'insufficient_funds',
'generic_decline'])) {
1129 $errmsg .=
': ' . $charge->failure_code;
1130 $errmsg .= ($charge->failure_message ?
' - ' :
'') .
' ' . $charge->failure_message;
1131 if (empty($stripefailurecode)) {
1132 $stripefailurecode = $charge->failure_code;
1134 if (empty($stripefailuremessage)) {
1135 $stripefailuremessage = $charge->failure_message;
1138 $errmsg .=
': failure_code=' . $charge->failure_code;
1139 $errmsg .= ($charge->failure_message ?
' - ' :
'') .
' failure_message=' . $charge->failure_message;
1140 if (empty($stripefailurecode)) {
1141 $stripefailurecode = $charge->failure_code;
1143 if (empty($stripefailuremessage)) {
1144 $stripefailuremessage = $charge->failure_message;
1148 $errmsg .=
': ' . $stripefailurecode .
' - ' . $stripefailuremessage;
1149 $errmsg .= ($stripefailuredeclinecode ?
' - ' . $stripefailuredeclinecode :
'');
1152 $description =
'Stripe payment ERROR from makeStripeSepaRequest: ' . $FULLTAG;
1153 $postactionmessages[] = $errmsg .
' (' . $stripearrayofkeys[
'publishable_key'] .
')';
1154 $this->errors[] = $errmsg;
1156 dol_syslog(
'Successfuly request direct debit ' . $stripecard->id);
1158 $postactionmessages[] =
'Success to request direct debit (' . $charge->id .
' with ' . $stripearrayofkeys[
'publishable_key'] .
')';
1162 $this->stripechargedone++;
1165 $description =
'Stripe payment request OK (' . $charge->id .
') from makeStripeSepaRequest: ' . $FULLTAG;
1171 if (empty($charge) || $charge->status ==
'failed') {
1172 $actioncode =
'PAYMENT_STRIPE_KO';
1173 $extraparams = $stripefailurecode;
1174 $extraparams .= (($extraparams && $stripefailuremessage) ?
' - ' :
'') . $stripefailuremessage;
1175 $extraparams .= (($extraparams && $stripefailuredeclinecode) ?
' - ' :
'') . $stripefailuredeclinecode;
1177 $actioncode =
'PAYMENT_STRIPE_OK';
1183 dol_syslog(
"No direct debit payment method found for this stripe customer " . $customer->id, LOG_WARNING);
1184 $this->errors[] =
'Failed to get direct debit payment method for stripe customer = ' . $customer->id;
1186 $description =
'Failed to find or use the payment mode - no credit card defined for the customer account';
1187 $stripefailurecode =
'BADPAYMENTMODE';
1188 $stripefailuremessage =
'Failed to find or use the payment mode - no credit card defined for the customer account';
1189 $postactionmessages[] = $description .
' (' . $stripearrayofkeys[
'publishable_key'] .
')';
1193 $actioncode =
'PAYMENT_STRIPE_KO';
1199 $stripefailurecode =
'';
1200 $stripefailuremessage =
'';
1208 if ($resultthirdparty <= 0) {
1209 dol_syslog(
'SellYourSaasUtils Failed to load customer for thirdparty_id = ' . $thirdparty->id, LOG_WARNING);
1210 $this->errors[] =
'Failed to load customer for thirdparty_id = ' . $thirdparty->id;
1212 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);
1213 $this->errors[] =
'Failed to get Stripe customer id for thirdparty_id = ' . $thirdparty->id .
" in mode " . $servicestatus .
" in Stripe env " . $stripearrayofkeysbyenv[$servicestatus][
'publishable_key'];
1218 $description =
'Failed to find or use your payment mode (no payment mode for this customer id)';
1219 $stripefailurecode =
'BADPAYMENTMODE';
1220 $stripefailuremessage =
'Failed to find or use your payment mode (no payment mode for this customer id)';
1221 $postactionmessages = [];
1225 $actioncode =
'PAYMENT_STRIPE_KO';
1230 dol_syslog(
"* Record event for credit transfer or direct debit request result - " . $description);
1231 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
1236 $actioncomm->type_code =
'AC_OTH_AUTO';
1237 $actioncomm->code =
'AC_' . $actioncode;
1238 $actioncomm->label = $description;
1239 $actioncomm->note_private = join(
",\n", $postactionmessages);
1240 $actioncomm->fk_project = $this->fk_project;
1241 $actioncomm->datep = $now;
1242 $actioncomm->datef = $now;
1243 $actioncomm->percentage = -1;
1244 $actioncomm->socid = $thirdparty->id;
1245 $actioncomm->contactid = 0;
1246 $actioncomm->authorid = $user->id;
1247 $actioncomm->userownerid = $user->id;
1257 $actioncomm->fk_element = $this->id;
1258 $actioncomm->elementtype = $this->element;
1259 $actioncomm->extraparams =
dol_trunc($extraparams, 250);
1261 $actioncomm->create($user);
1265 $this->postactionmessages = $postactionmessages;
1269 dol_syslog(
'Error ' . $e->getMessage(), LOG_ERR);
1270 $this->errors[] =
'Error ' . $e->getMessage();
1275 dol_syslog(
"Remain to pay is null for the invoice " . $this->
id .
" " . $this->
ref .
". Why is the invoice not classified 'Paid' ?", LOG_WARNING);
1276 $this->errors[] =
"Remain to pay is null for the invoice " . $this->
id .
" " . $this->ref .
". Why is the invoice not classified 'Paid' ?";
1281 if (!$error && !$errorforinvoice) {
1282 $result = $bon->set_infotrans($user, $now, 3);
1286 dol_syslog(
"Error on BonPrelevement creation", LOG_ERR);
1287 $this->errors[] =
"Error on BonPrelevement creation";
1291 if (!$error && !$errorforinvoice) {
1293 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"prelevement_demande SET";
1294 $sql .=
" ext_payment_id = '".$this->db->escape($paymentintent->id).
"',";
1295 $sql .=
" ext_payment_site = '".$this->db->escape($service).
"'";
1296 $sql .=
" WHERE rowid = ".((int) $did);
1298 dol_syslog(get_class($this).
"::makeStripeSepaRequest update to save stripe paymentintent ids", LOG_DEBUG);
1299 $resql = $this->
db->query(
$sql);
1301 $this->error = $this->
db->lasterror();
1302 dol_syslog(get_class($this).
'::makeStripeSepaRequest Erreur');
1307 if (!$error && !$errorforinvoice) {
1308 $this->
db->commit();
1310 $this->
db->rollback();
1313 $this->error =
'WithdrawRequestErrorNilAmount';
1314 dol_syslog(get_class($this).
'::makeStripeSepaRequest WithdrawRequestErrorNilAmount');
1332 $this->error = $this->
db->error();
1333 dol_syslog(get_class($this).
'::makeStripeSepaRequest Erreur -2');
1337 $this->error =
"Status of invoice does not allow this";
1338 dol_syslog(get_class($this).
"::makeStripeSepaRequest ".$this->error.
" $this->statut, $this->paye, $this->mode_reglement_id");
1354 $sql =
'DELETE FROM '.$this->db->prefix().
'prelevement_demande';
1355 $sql .=
' WHERE rowid = '.((int) $did);
1356 $sql .=
' AND traite = 0';
1357 if ($this->
db->query(
$sql)) {
1360 $this->error = $this->
db->lasterror();
1361 dol_syslog(get_class($this).
'::demande_prelevement_delete Error '.$this->error);
1374 global $conf, $mysoc;
1377 $tmplang->setDefaultLang(
'en_US');
1378 $tmplang->load(
"main");
1383 $pricewithtaxstring =
price2num($this->total_ttc, 2, 1);
1384 $pricetaxstring =
price2num($this->total_tva, 2, 1);
1411 $s = pack(
'C1', 1).pack(
'C1', strlen($mysoc->name)).$mysoc->name;
1412 $s .= pack(
'C1', 2).pack(
'C1', strlen($mysoc->tva_intra)).$mysoc->tva_intra;
1413 $s .= pack(
'C1', 3).pack(
'C1', strlen($datestring)).$datestring;
1414 $s .= pack(
'C1', 4).pack(
'C1', strlen($pricewithtaxstring)).$pricewithtaxstring;
1415 $s .= pack(
'C1', 5).pack(
'C1', strlen($pricetaxstring)).$pricetaxstring;
1421 $s = base64_encode($s);
1434 global $conf, $mysoc;
1437 $tmplang->setDefaultLang(
'en_US');
1438 $tmplang->load(
"main");
1440 $pricewithtaxstring =
price2num($this->total_ttc, 2, 1);
1441 $pricetaxstring =
price2num($this->total_tva, 2, 1);
1443 $complementaryinfo =
'';
1457 $complementaryinfo =
'//S1/10/'.str_replace(
'/',
'', $this->
ref).
'/11/'.$datestring;
1458 if ($this->ref_client) {
1459 $complementaryinfo .=
'/20/'.$this->ref_client;
1461 if ($this->thirdparty->tva_intra) {
1462 $complementaryinfo .=
'/30/'.$this->thirdparty->tva_intra;
1465 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
1474 if ($this->fk_account > 0) {
1476 $bankaccount->fetch($this->fk_account);
1477 $s .= $bankaccount->iban.
"\n";
1481 if ($bankaccount->id > 0 &&
getDolGlobalString(
'PDF_SWISS_QRCODE_USE_OWNER_OF_ACCOUNT_AS_CREDITOR')) {
1485 $s .=
dol_trunc($bankaccount->proprio, 70,
'right',
'UTF-8', 1).
"\n";
1486 $addresslinearray = explode(
"\n", $bankaccount->owner_address);
1487 $s .=
dol_trunc(empty($addresslinearray[1]) ?
'' : $addresslinearray[1], 70,
'right',
'UTF-8', 1).
"\n";
1488 $s .=
dol_trunc(empty($addresslinearray[2]) ?
'' : $addresslinearray[2], 70,
'right',
'UTF-8', 1).
"\n";
1494 $s .=
dol_trunc($mysoc->name, 70,
'right',
'UTF-8', 1).
"\n";
1495 $addresslinearray = explode(
"\n", $mysoc->address);
1496 $s .=
dol_trunc(empty($addresslinearray[1]) ?
'' : $addresslinearray[1], 70,
'right',
'UTF-8', 1).
"\n";
1497 $s .=
dol_trunc(empty($addresslinearray[2]) ?
'' : $addresslinearray[2], 70,
'right',
'UTF-8', 1).
"\n";
1498 $s .=
dol_trunc($mysoc->zip, 16,
'right',
'UTF-8', 1).
"\n";
1499 $s .=
dol_trunc($mysoc->town, 35,
'right',
'UTF-8', 1).
"\n";
1500 $s .=
dol_trunc($mysoc->country_code, 2,
'right',
'UTF-8', 1).
"\n";
1511 $s .=
price($pricewithtaxstring, 0,
'none', 0, 0, 2).
"\n";
1512 $s .= ($this->multicurrency_code ? $this->multicurrency_code : $conf->currency).
"\n";
1515 $s .=
dol_trunc($this->thirdparty->name, 70,
'right',
'UTF-8', 1).
"\n";
1516 $addresslinearray = explode(
"\n", $this->thirdparty->address);
1517 $s .=
dol_trunc(empty($addresslinearray[1]) ?
'' : $addresslinearray[1], 70,
'right',
'UTF-8', 1).
"\n";
1518 $s .=
dol_trunc(empty($addresslinearray[2]) ?
'' : $addresslinearray[2], 70,
'right',
'UTF-8', 1).
"\n";
1519 $s .=
dol_trunc($this->thirdparty->zip, 16,
'right',
'UTF-8', 1).
"\n";
1520 $s .=
dol_trunc($this->thirdparty->town, 35,
'right',
'UTF-8', 1).
"\n";
1521 $s .=
dol_trunc($this->thirdparty->country_code, 2,
'right',
'UTF-8', 1).
"\n";
1526 if ($complementaryinfo) {
1527 $s .= $complementaryinfo.
"\n";
1533 if ($complementaryinfo) {
1534 $s .= $complementaryinfo.
"\n";
1544 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
1572 public $product_type = 0;
1578 public $product_ref;
1584 public $product_label;
1590 public $product_desc;
1621 public $vat_src_code;
1633 public $localtax1_tx;
1639 public $localtax2_tx;
1645 public $localtax1_type;
1651 public $localtax2_type;
1657 public $remise_percent;
1682 public $total_localtax1;
1688 public $total_localtax2;
1696 public $date_start_fill;
1697 public $date_end_fill;
1699 public $buy_price_ht;
1712 public $info_bits = 0;
1714 public $special_code = 0;
1716 public $fk_multicurrency;
1717 public $multicurrency_code;
1718 public $multicurrency_subprice;
1719 public $multicurrency_total_ht;
1720 public $multicurrency_total_tva;
1721 public $multicurrency_total_ttc;
1723 public $fk_user_author;
1724 public $fk_user_modif;
1726 public $fk_accounting_account;