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;
341 $invoice_multicurrency_tx = $tmparray[
'invoice_multicurrency_tx'];
342 $invoice_multicurrency_code = $tmparray[
'invoice_multicurrency_code'];
345 if ($invoice_multicurrency_tx) {
346 if ($way ==
'dolibarr') {
347 $value_converted = (float)
price2num($value * $invoice_multicurrency_tx,
'MU');
349 $value_converted = (float)
price2num($value / $invoice_multicurrency_tx,
'MU');
352 $invoice_multicurrency_tx =
false;
356 if ($value_converted ===
false) {
358 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
363 $currencyofinvoiceforthisline = empty($this->multicurrency_code[$key]) ? $invoice_multicurrency_code : $this->multicurrency_code[$key];
365 $currencyofpaymentforthisline = empty($this->multicurrency_amounts[$key]) ?
$conf->currency : $this->multicurrency_code[$key];
369 if (empty($currencyofinvoices)) {
370 $currencyofinvoices = $currencyofinvoiceforthisline;
371 } elseif ($currencyofinvoices != $currencyofinvoiceforthisline) {
373 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
377 if (empty($currencyofpayment)) {
378 $currencyofpayment = $currencyofpaymentforthisline;
379 } elseif ($currencyofpayment != $currencyofpaymentforthisline) {
381 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
385 if (empty($currencytxofpayment)) {
386 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] :
"";
389 $totalamount_converted += $value_converted;
390 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
393 $amounts[$key] = $newvalue;
394 $totalamount += $newvalue;
395 if (!empty($newvalue)) {
396 $atleastonepaymentnotnull++;
403 if (empty($currencyofpayment)) {
404 $currencyofpayment =
$conf->currency;
407 if (!empty($currencyofpayment && !empty($this->fk_account))) {
409 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
410 $bankaccount =
new Account($this->db);
411 $bankaccount->fetch((
int) $this->fk_account);
412 $bankcurrencycode = empty($bankaccount->currency_code) ?
$conf->currency : $bankaccount->currency_code;
414 if ($bankcurrencycode !=
$conf->currency) {
416 if ($bankcurrencycode != $currencyofinvoices && $currencyofinvoices !=
$conf->currency) {
417 $langs->load(
"errors");
418 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofinvoices, $bankcurrencycode);
421 if ($bankcurrencycode != $currencyofpayment && $currencyofpayment !=
$conf->currency) {
422 $langs->load(
"errors");
423 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
431 $totalamount = (float)
price2num($totalamount,
'MT');
432 $totalamount_converted = (float)
price2num($totalamount_converted,
'MT');
435 if (empty($totalamount) && empty($atleastonepaymentnotnull)) {
436 $this->errors[] =
'TotalAmountEmpty';
437 $this->error = $langs->trans(
'TotalAmountEmpty');
441 dol_syslog(get_class($this).
"::create insert paiement (closepaidinvoices = ".$closepaidinvoices.
")", LOG_DEBUG);
445 $this->
ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
447 if (empty($this->ref_ext)) {
451 if ($way ==
'dolibarr') {
452 $total = $totalamount;
453 $mtotal = $totalamount_converted;
455 $total = $totalamount_converted;
456 $mtotal = $totalamount;
459 $num_payment = $this->num_payment;
460 $note = ($this->note_private ? $this->note_private : $this->note);
462 $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)";
463 $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).
", ";
464 $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).
")";
466 $resql = $this->db->query($sql);
468 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiement');
471 foreach ($this->amounts as $key => $amount) {
473 if (is_numeric($amount) && $amount != 0) {
475 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
476 $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).
")";
478 dol_syslog(get_class($this).
'::create Amount line '.$key.
' insert paiement_facture', LOG_DEBUG);
479 $resql = $this->db->query($sql);
481 $invoice =
new Facture($this->db);
482 $invoice->fetch($facid);
485 if ($closepaidinvoices) {
486 $paiement = $invoice->getSommePaiement();
487 $creditnotes = $invoice->getSumCreditNotesUsed();
488 $deposits = $invoice->getSumDepositsUsed();
489 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
490 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
495 $affected_types = array(
503 if (!in_array($invoice->type, $affected_types)) {
504 dol_syslog(
"Invoice ".$facid.
" is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
505 } elseif ($remaintopay) {
508 $hookmanager->initHooks(array(
'paymentdao'));
509 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
510 $action =
'CLOSEPAIDINVOICE';
511 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
513 $this->errors[] = $hookmanager->error;
514 $this->error = $hookmanager->error;
516 } elseif ($reshook == 0) {
517 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
525 $amount_ht = $amount_tva = $amount_ttc = array();
526 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
528 @phan-var-force array<string,float> $amount_ht
529 @phan-var-force array<string,float> $amount_tva
530 @phan-var-force array<string,float> $amount_ttc
531 @phan-var-force array<string,float> $multicurrency_amount_ht
532 @phan-var-force array<string,float> $multicurrency_amount_tva
533 @phan-var-force array<string,float> $multicurrency_amount_ttc
538 $discount->fetch(0, $invoice->id);
539 if (empty($discount->id)) {
540 $discount->description =
'(DEPOSIT)';
541 $discount->fk_soc = $invoice->socid;
542 $discount->socid = $invoice->socid;
543 $discount->fk_facture_source = $invoice->id;
548 foreach ($invoice->lines as $line) {
549 if ($line->product_type != 9 && $line->total_ht != 0) {
550 $key = $line->tva_tx.
'|'.(
string) $line->vat_src_code;
551 if (!array_key_exists($key, $amount_ht)) {
552 $amount_ht[$key] = 0.0;
553 $amount_tva[$key] = 0.0;
554 $amount_ttc[$key] = 0.0;
555 $multicurrency_amount_ht[$key] = 0.0;
556 $multicurrency_amount_tva[$key] = 0.0;
557 $multicurrency_amount_ttc[$key] = 0.0;
559 $amount_ht[$key] += $line->total_ht;
560 $amount_tva[$key] += $line->total_tva;
561 $amount_ttc[$key] += $line->total_ttc;
562 $multicurrency_amount_ht[$key] += $line->multicurrency_total_ht;
563 $multicurrency_amount_tva[$key] += $line->multicurrency_total_tva;
564 $multicurrency_amount_ttc[$key] += $line->multicurrency_total_ttc;
569 foreach ($amount_ht as $keyfordiscount => $xxx) {
570 $parts = explode(
'|', (
string) $keyfordiscount, 2);
572 $vat_src_code = isset($parts[1]) ? $parts[1] :
'';
573 $discount->amount_ht = abs($amount_ht[$keyfordiscount]);
574 $discount->total_ht = abs($amount_ht[$keyfordiscount]);
575 $discount->amount_tva = abs($amount_tva[$keyfordiscount]);
576 $discount->total_tva = abs($amount_tva[$keyfordiscount]);
577 $discount->amount_ttc = abs($amount_ttc[$keyfordiscount]);
578 $discount->total_ttc = abs($amount_ttc[$keyfordiscount]);
579 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$keyfordiscount]);
580 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$keyfordiscount]);
581 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$keyfordiscount]);
582 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$keyfordiscount]);
583 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$keyfordiscount]);
584 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$keyfordiscount]);
585 $discount->tva_tx = abs((
float) $tva_tx);
586 $discount->vat_src_code = $vat_src_code;
588 $result = $discount->create($user);
597 $this->error = $discount->error;
598 $this->errors = $discount->errors;
605 $invoice->context[
'actionmsgmore'] =
'Invoice set to paid by the payment->create() of payment '.$this->ref.
' because the remain to pay is 0';
607 $result = $invoice->setPaid($user,
'',
'');
609 $this->error = $invoice->error;
610 $this->errors = $invoice->errors;
619 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);
622 $outputlangs = $langs;
624 $invoice->fetch_thirdparty();
625 $newlang = $invoice->thirdparty->default_lang;
627 if (!empty($newlang)) {
629 $outputlangs->setDefaultLang($newlang);
636 $ret = $invoice->fetch($facid);
638 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
640 dol_syslog(get_class($this).
'::create Regenerate end result='.$result, LOG_DEBUG);
642 $this->warnings = $invoice->warnings;
645 $this->error = $invoice->error;
646 $this->errors = $invoice->errors;
651 $this->error = $this->db->lasterror();
655 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
659 dol_syslog(get_class($this).
'::create Now we call the triggers if no error (error = '.$error.
')', LOG_DEBUG);
663 $result = $this->call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
670 $this->error = $this->db->lasterror();
677 $this->total = $total;
678 $this->multicurrency_amount = $mtotal;
679 $this->multicurrency_currency = $currencyofinvoices;
684 $this->db->rollback();
798 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'', $addbankurl =
'')
809 if ($accountid <= 0) {
810 $this->error =
'Bad value for parameter accountid='.$accountid;
811 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
815 $this->fk_account = $accountid;
817 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
819 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
821 $result = $acc->fetch($this->fk_account);
823 $this->error = $acc->error;
824 $this->errors = $acc->errors;
831 $totalamount = $this->amount;
832 $totalamount_main_currency =
null;
833 if (empty($totalamount)) {
834 $totalamount = $this->total;
840 if ($this->multicurrency_currency ==
$conf->currency) {
841 if ($this->
amount != $this->multicurrency_amount) {
844 $this->error =
'Payment in same currency than company but this->amount != this->multicurrency_amount';
849 if (
$conf->currency != $acc->currency_code) {
850 $totalamount = $this->multicurrency_amount;
851 $totalamount_main_currency = $this->amount;
854 if ($mode ==
'payment_supplier') {
855 $totalamount = -$totalamount;
856 if (isset($totalamount_main_currency)) {
857 $totalamount_main_currency = -$totalamount_main_currency;
862 $bank_line_id = $acc->addline(
864 $this->paiementcode ? $this->paiementcode : $this->paiementid,
875 (float) $totalamount_main_currency
880 if ($bank_line_id > 0) {
890 if ($mode ==
'payment') {
891 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
893 if ($mode ==
'payment_supplier') {
894 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
897 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
907 $linkaddedforthirdparty = array();
908 foreach ($this->amounts as $key => $value) {
909 if ($mode ==
'payment') {
912 $fac->fetch_thirdparty();
913 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
914 $result = $acc->add_url_line(
916 $fac->thirdparty->id,
917 DOL_URL_ROOT.
'/comm/card.php?socid=',
918 (
string) $fac->thirdparty->name,
923 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
925 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
928 if ($mode ==
'payment_supplier') {
931 $fac->fetch_thirdparty();
932 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
933 $result = $acc->add_url_line(
935 $fac->thirdparty->id,
936 DOL_URL_ROOT.
'/fourn/card.php?socid=',
937 (
string) $fac->thirdparty->name,
942 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
944 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
951 if (!$error && $addbankurl && in_array($addbankurl, array(
'direct-debit',
'credit-transfer'))) {
952 $result = $acc->add_url_line(
954 $this->id_prelevement,
955 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
962 if (!$error && $label ==
'(InvoiceRefused)') {
963 $result = $acc->add_url_line(
965 $this->id_prelevement,
966 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
967 $this->num_prelevement,
972 if (!$error && !$notrigger) {
974 $result = $this->call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
981 $this->error = $acc->error;
982 $this->errors = $acc->errors;
989 $this->db->rollback();
994 return $bank_line_id;