309 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
311 global
$conf, $langs;
320 $totalamount_converted = 0;
321 $atleastonepaymentnotnull = 0;
323 if ($way ==
'dolibarr') {
324 $amounts = &$this->amounts;
325 $amounts_to_update = &$this->multicurrency_amounts;
327 $amounts = &$this->multicurrency_amounts;
328 $amounts_to_update = &$this->amounts;
331 $currencyofpayment =
'';
332 $currencyofinvoices =
'';
333 $currencytxofpayment =
'';
335 foreach ($amounts as $key => $value) {
339 $value_converted =
false;
346 if ($tmparray ===
false) {
347 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
350 $invoice_multicurrency_tx = $tmparray[
'invoice_multicurrency_tx'];
351 $invoice_multicurrency_code = $tmparray[
'invoice_multicurrency_code'];
354 if ($invoice_multicurrency_tx) {
355 if ($way ==
'dolibarr') {
356 $value_converted = (float)
price2num($value * $invoice_multicurrency_tx,
'MU');
361 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
362 $invoice =
new Facture($this->db);
363 $invoice->fetch($key);
364 if ((
float)
price2num($value,
'MT') == (
float) $invoice->getRemainToPay(1)) {
365 $value_converted = (float) $invoice->getRemainToPay(0);
367 $value_converted = (float)
price2num($value / $invoice_multicurrency_tx,
'MU');
371 $invoice_multicurrency_tx =
false;
375 if ($value_converted ===
false) {
377 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
382 $currencyofinvoiceforthisline = empty($this->multicurrency_code[$key]) ? $invoice_multicurrency_code : $this->multicurrency_code[$key];
384 $currencyofpaymentforthisline = empty($this->multicurrency_amounts[$key]) ?
$conf->currency : $this->multicurrency_code[$key];
388 if (empty($currencyofinvoices)) {
389 $currencyofinvoices = $currencyofinvoiceforthisline;
390 } elseif ($currencyofinvoices != $currencyofinvoiceforthisline) {
392 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
396 if (empty($currencyofpayment)) {
397 $currencyofpayment = $currencyofpaymentforthisline;
398 } elseif ($currencyofpayment != $currencyofpaymentforthisline) {
400 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
404 if (empty($currencytxofpayment)) {
405 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] :
"";
408 $totalamount_converted += $value_converted;
409 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
412 $amounts[$key] = $newvalue;
413 $totalamount += $newvalue;
414 if (!empty($newvalue)) {
415 $atleastonepaymentnotnull++;
422 if (empty($currencyofpayment)) {
423 $currencyofpayment =
$conf->currency;
426 if (!empty($currencyofpayment && !empty($this->fk_account))) {
428 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
429 $bankaccount =
new Account($this->db);
430 $bankaccount->fetch((
int) $this->fk_account);
431 $bankcurrencycode = empty($bankaccount->currency_code) ?
$conf->currency : $bankaccount->currency_code;
433 if ($bankcurrencycode !=
$conf->currency) {
435 if ($bankcurrencycode != $currencyofinvoices && $currencyofinvoices !=
$conf->currency) {
436 $langs->load(
"errors");
437 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofinvoices, $bankcurrencycode);
440 if ($bankcurrencycode != $currencyofpayment && $currencyofpayment !=
$conf->currency) {
441 $langs->load(
"errors");
442 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
450 $totalamount = (float)
price2num($totalamount,
'MT');
451 $totalamount_converted = (float)
price2num($totalamount_converted,
'MT');
454 if (empty($totalamount) && empty($atleastonepaymentnotnull)) {
455 $this->errors[] =
'TotalAmountEmpty';
456 $this->error = $langs->trans(
'TotalAmountEmpty');
460 dol_syslog(get_class($this).
"::create insert paiement (closepaidinvoices = ".$closepaidinvoices.
")", LOG_DEBUG);
464 $this->
ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
466 if (empty($this->ref_ext)) {
470 if ($way ==
'dolibarr') {
471 $total = $totalamount;
472 $mtotal = $totalamount_converted;
474 $total = $totalamount_converted;
475 $mtotal = $totalamount;
478 $num_payment = $this->num_payment;
479 $note = ($this->note_private ? $this->note_private : $this->note);
481 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement (entity, ref, ref_ext, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, ext_payment_id, ext_payment_site, fk_user_creat, pos_change)";
482 $sql .=
" VALUES (".((int)
$conf->entity).
", '".$this->db->escape($this->
ref).
"', '".$this->db->escape($this->ref_ext).
"', '".$this->db->idate($now).
"', '".$this->db->idate($this->datepaye).
"', ".((float) $total).
", ".((float) $mtotal).
", ".((int) $this->paiementid).
", ";
483 $sql .=
"'".$this->db->escape($num_payment).
"', '".$this->db->escape($note).
"', ".($this->ext_payment_id ?
"'".$this->db->escape($this->ext_payment_id).
"'" :
"null").
", ".($this->ext_payment_site ?
"'".$this->db->escape($this->ext_payment_site).
"'" :
"null").
", ".((
int) $user->id).
", ".((
float) $this->pos_change).
")";
485 $resql = $this->db->query($sql);
487 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiement');
490 foreach ($this->amounts as $key => $amount) {
492 if (is_numeric($amount) && $amount != 0) {
494 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
495 $sql .=
" VALUES (".((int) $facid).
", ".((int) $this->
id).
", ".((float) $amount).
", ".((float) $this->multicurrency_amounts[$key]).
", ".($currencyofpayment ?
"'".$this->db->escape($currencyofpayment).
"'" :
'NULL').
", ".(!empty($this->multicurrency_tx) ? (float) $currencytxofpayment : 1).
")";
497 dol_syslog(get_class($this).
'::create Amount line '.$key.
' insert paiement_facture', LOG_DEBUG);
498 $resql = $this->db->query($sql);
500 $invoice =
new Facture($this->db);
501 $invoice->fetch($facid);
504 if ($closepaidinvoices) {
505 $paiement = $invoice->getSommePaiement();
506 $creditnotes = $invoice->getSumCreditNotesUsed();
507 $deposits = $invoice->getSumDepositsUsed();
508 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
509 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
514 $affected_types = array(
522 if (!in_array($invoice->type, $affected_types)) {
523 dol_syslog(
"Invoice ".$facid.
" is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
524 } elseif ($remaintopay) {
527 $hookmanager->initHooks(array(
'paymentdao'));
528 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
529 $action =
'CLOSEPAIDINVOICE';
530 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
532 $this->errors[] = $hookmanager->error;
533 $this->error = $hookmanager->error;
535 } elseif ($reshook == 0) {
536 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
544 $amount_ht = $amount_tva = $amount_ttc = array();
545 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
547 @phan-var-force array<string,float> $amount_ht
548 @phan-var-force array<string,float> $amount_tva
549 @phan-var-force array<string,float> $amount_ttc
550 @phan-var-force array<string,float> $multicurrency_amount_ht
551 @phan-var-force array<string,float> $multicurrency_amount_tva
552 @phan-var-force array<string,float> $multicurrency_amount_ttc
557 $discount->fetch(0, $invoice->id);
558 if (empty($discount->id)) {
559 $discount->description =
'(DEPOSIT)';
560 $discount->fk_soc = $invoice->socid;
561 $discount->socid = $invoice->socid;
562 $discount->fk_facture_source = $invoice->id;
567 foreach ($invoice->lines as $line) {
568 if ($line->product_type != 9 && $line->total_ht != 0) {
569 $key = $line->tva_tx.
'|'.(string) $line->vat_src_code;
570 if (!array_key_exists($key, $amount_ht)) {
571 $amount_ht[$key] = 0.0;
572 $amount_tva[$key] = 0.0;
573 $amount_ttc[$key] = 0.0;
574 $multicurrency_amount_ht[$key] = 0.0;
575 $multicurrency_amount_tva[$key] = 0.0;
576 $multicurrency_amount_ttc[$key] = 0.0;
578 $amount_ht[$key] += $line->total_ht;
579 $amount_tva[$key] += $line->total_tva;
580 $amount_ttc[$key] += $line->total_ttc;
581 $multicurrency_amount_ht[$key] += $line->multicurrency_total_ht;
582 $multicurrency_amount_tva[$key] += $line->multicurrency_total_tva;
583 $multicurrency_amount_ttc[$key] += $line->multicurrency_total_ttc;
588 foreach ($amount_ht as $keyfordiscount => $xxx) {
589 $parts = explode(
'|', (
string) $keyfordiscount, 2);
591 $vat_src_code = isset($parts[1]) ? $parts[1] :
'';
592 $discount->amount_ht = abs($amount_ht[$keyfordiscount]);
593 $discount->total_ht = abs($amount_ht[$keyfordiscount]);
594 $discount->amount_tva = abs($amount_tva[$keyfordiscount]);
595 $discount->total_tva = abs($amount_tva[$keyfordiscount]);
596 $discount->amount_ttc = abs($amount_ttc[$keyfordiscount]);
597 $discount->total_ttc = abs($amount_ttc[$keyfordiscount]);
598 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$keyfordiscount]);
599 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$keyfordiscount]);
600 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$keyfordiscount]);
601 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$keyfordiscount]);
602 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$keyfordiscount]);
603 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$keyfordiscount]);
604 $discount->tva_tx = abs((
float) $tva_tx);
605 $discount->vat_src_code = $vat_src_code;
607 $result = $discount->create($user);
616 $this->error = $discount->error;
617 $this->errors = $discount->errors;
624 $invoice->context[
'actionmsgmore'] =
'Invoice set to paid by the payment->create() of payment '.$this->ref.
' because the remain to pay is 0';
626 $result = $invoice->setPaid($user,
'',
'');
628 $this->error = $invoice->error;
629 $this->errors = $invoice->errors;
638 dol_syslog(get_class($this).
'::create Regenerate the document after inserting payment for thirdparty default_lang='.(is_object($invoice->thirdparty) ? $invoice->thirdparty->default_lang :
'null'), LOG_DEBUG);
641 $outputlangs = $langs;
643 $invoice->fetch_thirdparty();
644 $newlang = $invoice->thirdparty->default_lang;
646 if (!empty($newlang)) {
648 $outputlangs->setDefaultLang($newlang);
655 $ret = $invoice->fetch($facid);
657 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
659 dol_syslog(get_class($this).
'::create Regenerate end result='.$result, LOG_DEBUG);
661 $this->warnings = $invoice->warnings;
664 $this->error = $invoice->error;
665 $this->errors = $invoice->errors;
670 $this->error = $this->db->lasterror();
674 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
678 dol_syslog(get_class($this).
'::create Now we call the triggers if no error (error = '.$error.
')', LOG_DEBUG);
682 $result = $this->call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
689 $this->error = $this->db->lasterror();
696 $this->total = $total;
697 $this->multicurrency_amount = $mtotal;
698 $this->multicurrency_currency = $currencyofinvoices;
703 $this->db->rollback();
817 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'', $addbankurl =
'')
828 if ($accountid <= 0) {
829 $this->error =
'Bad value for parameter accountid='.$accountid;
830 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
834 $this->fk_account = $accountid;
836 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
838 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
840 $result = $acc->fetch($this->fk_account);
842 $this->error = $acc->error;
843 $this->errors = $acc->errors;
850 $totalamount = $this->amount;
851 $totalamount_main_currency =
null;
852 if (empty($totalamount)) {
853 $totalamount = $this->total;
859 if ($this->multicurrency_currency ==
$conf->currency) {
860 if ($this->
amount != $this->multicurrency_amount) {
863 $this->error =
'Payment in same currency than company but this->amount != this->multicurrency_amount';
868 if (
$conf->currency != $acc->currency_code) {
869 $totalamount = $this->multicurrency_amount;
870 $totalamount_main_currency = $this->amount;
873 if ($mode ==
'payment_supplier') {
874 $totalamount = -$totalamount;
875 if (isset($totalamount_main_currency)) {
876 $totalamount_main_currency = -$totalamount_main_currency;
881 $bank_line_id = $acc->addline(
883 $this->paiementcode ? $this->paiementcode : $this->paiementid,
894 (float) $totalamount_main_currency
899 if ($bank_line_id > 0) {
903 dol_print_error($this->db);
909 if ($mode ==
'payment') {
910 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
912 if ($mode ==
'payment_supplier') {
913 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
916 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
919 dol_print_error($this->db);
926 $linkaddedforthirdparty = array();
927 foreach ($this->amounts as $key => $value) {
930 if (!is_numeric($value) || $value == 0) {
933 if ($mode ==
'payment') {
936 $fac->fetch_thirdparty();
937 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
938 $result = $acc->add_url_line(
940 $fac->thirdparty->id,
941 DOL_URL_ROOT.
'/comm/card.php?socid=',
942 (
string) $fac->thirdparty->name,
947 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
949 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
952 if ($mode ==
'payment_supplier') {
955 $fac->fetch_thirdparty();
956 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
957 $result = $acc->add_url_line(
959 $fac->thirdparty->id,
960 DOL_URL_ROOT.
'/fourn/card.php?socid=',
961 (
string) $fac->thirdparty->name,
966 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
968 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
975 if (!$error && $addbankurl && in_array($addbankurl, array(
'direct-debit',
'credit-transfer'))) {
976 $result = $acc->add_url_line(
978 $this->id_prelevement,
979 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
986 if (!$error && $label ==
'(InvoiceRefused)') {
987 $result = $acc->add_url_line(
989 $this->id_prelevement,
990 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
991 $this->num_prelevement,
996 if (!$error && !$notrigger) {
998 $result = $this->call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
1005 $this->error = $acc->error;
1006 $this->errors = $acc->errors;
1011 $this->db->commit();
1013 $this->db->rollback();
1018 return $bank_line_id;