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.");
542 $amount_ht = $amount_tva = $amount_ttc = array();
543 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
545 @phan-var-force array<string,float> $amount_ht
546 @phan-var-force array<string,float> $amount_tva
547 @phan-var-force array<string,float> $amount_ttc
548 @phan-var-force array<string,float> $multicurrency_amount_ht
549 @phan-var-force array<string,float> $multicurrency_amount_tva
550 @phan-var-force array<string,float> $multicurrency_amount_ttc
555 $discount->fetch(0, $invoice->id);
556 if (empty($discount->id)) {
557 $discount->description =
'(DEPOSIT)';
558 $discount->fk_soc = $invoice->socid;
559 $discount->socid = $invoice->socid;
560 $discount->fk_facture_source = $invoice->id;
565 foreach ($invoice->lines as $line) {
566 if ($line->product_type != 9 && $line->total_ht != 0) {
567 $key = $line->tva_tx.
'|'.(string) $line->vat_src_code;
568 if (!array_key_exists($key, $amount_ht)) {
569 $amount_ht[$key] = 0.0;
570 $amount_tva[$key] = 0.0;
571 $amount_ttc[$key] = 0.0;
572 $multicurrency_amount_ht[$key] = 0.0;
573 $multicurrency_amount_tva[$key] = 0.0;
574 $multicurrency_amount_ttc[$key] = 0.0;
576 $amount_ht[$key] += $line->total_ht;
577 $amount_tva[$key] += $line->total_tva;
578 $amount_ttc[$key] += $line->total_ttc;
579 $multicurrency_amount_ht[$key] += $line->multicurrency_total_ht;
580 $multicurrency_amount_tva[$key] += $line->multicurrency_total_tva;
581 $multicurrency_amount_ttc[$key] += $line->multicurrency_total_ttc;
586 foreach ($amount_ht as $key => $xxx) {
587 $parts = explode(
'|', (
string) $key, 2);
589 $vat_src_code = isset($parts[1]) ? $parts[1] :
'';
590 $discount->amount_ht = abs($amount_ht[$key]);
591 $discount->total_ht = abs($amount_ht[$key]);
592 $discount->amount_tva = abs($amount_tva[$key]);
593 $discount->total_tva = abs($amount_tva[$key]);
594 $discount->amount_ttc = abs($amount_ttc[$key]);
595 $discount->total_ttc = abs($amount_ttc[$key]);
596 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$key]);
597 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$key]);
598 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$key]);
599 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$key]);
600 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$key]);
601 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$key]);
602 $discount->tva_tx = abs((
float) $tva_tx);
603 $discount->vat_src_code = $vat_src_code;
605 $result = $discount->create($user);
614 $this->error = $discount->error;
615 $this->errors = $discount->errors;
622 $invoice->context[
'actionmsgmore'] =
'Invoice set to paid by the payment->create() of payment '.$this->ref.
' because the remain to pay is 0';
624 $result = $invoice->setPaid($user,
'',
'');
626 $this->error = $invoice->error;
627 $this->errors = $invoice->errors;
636 dol_syslog(get_class($this).
'::create Regenerate the document after inserting payment for thirdparty default_lang='.(is_object($invoice->thirdparty) ? $invoice->thirdparty->default_lang :
'null'), LOG_DEBUG);
639 $outputlangs = $langs;
641 $invoice->fetch_thirdparty();
642 $newlang = $invoice->thirdparty->default_lang;
644 if (!empty($newlang)) {
646 $outputlangs->setDefaultLang($newlang);
653 $ret = $invoice->fetch($facid);
655 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
657 dol_syslog(get_class($this).
'::create Regenerate end result='.$result, LOG_DEBUG);
659 $this->warnings = $invoice->warnings;
662 $this->error = $invoice->error;
663 $this->errors = $invoice->errors;
668 $this->error = $this->db->lasterror();
672 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
676 dol_syslog(get_class($this).
'::create Now we call the triggers if no error (error = '.$error.
')', LOG_DEBUG);
680 $result = $this->call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
687 $this->error = $this->db->lasterror();
694 $this->total = $total;
695 $this->multicurrency_amount = $mtotal;
696 $this->multicurrency_currency = $currencyofinvoices;
701 $this->db->rollback();
815 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'', $addbankurl =
'')
826 if ($accountid <= 0) {
827 $this->error =
'Bad value for parameter accountid='.$accountid;
828 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
832 $this->fk_account = $accountid;
834 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
836 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
838 $result = $acc->fetch($this->fk_account);
840 $this->error = $acc->error;
841 $this->errors = $acc->errors;
848 $totalamount = $this->amount;
849 $totalamount_main_currency =
null;
850 if (empty($totalamount)) {
851 $totalamount = $this->total;
857 if ($this->multicurrency_currency == $conf->currency) {
858 if ($this->
amount != $this->multicurrency_amount) {
861 $this->error =
'Payment in same currency than company but this->amount != this->multicurrency_amount';
866 if ($conf->currency != $acc->currency_code) {
867 $totalamount = $this->multicurrency_amount;
868 $totalamount_main_currency = $this->amount;
871 if ($mode ==
'payment_supplier') {
872 $totalamount = -$totalamount;
873 if (isset($totalamount_main_currency)) {
874 $totalamount_main_currency = -$totalamount_main_currency;
879 $bank_line_id = $acc->addline(
881 $this->paiementcode ? $this->paiementcode : $this->paiementid,
892 (float) $totalamount_main_currency
897 if ($bank_line_id > 0) {
907 if ($mode ==
'payment') {
908 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
910 if ($mode ==
'payment_supplier') {
911 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
914 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
924 $linkaddedforthirdparty = array();
925 foreach ($this->amounts as $key => $value) {
928 if (!is_numeric($value) || $value == 0) {
931 if ($mode ==
'payment') {
934 $fac->fetch_thirdparty();
935 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
936 $result = $acc->add_url_line(
938 $fac->thirdparty->id,
939 DOL_URL_ROOT.
'/comm/card.php?socid=',
940 (
string) $fac->thirdparty->name,
945 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
947 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
950 if ($mode ==
'payment_supplier') {
953 $fac->fetch_thirdparty();
954 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
955 $result = $acc->add_url_line(
957 $fac->thirdparty->id,
958 DOL_URL_ROOT.
'/fourn/card.php?socid=',
959 (
string) $fac->thirdparty->name,
964 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
966 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
973 if (!$error && $addbankurl && in_array($addbankurl, array(
'direct-debit',
'credit-transfer'))) {
974 $result = $acc->add_url_line(
976 $this->id_prelevement,
977 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
984 if (!$error && $label ==
'(InvoiceRefused)') {
985 $result = $acc->add_url_line(
987 $this->id_prelevement,
988 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
989 $this->num_prelevement,
994 if (!$error && !$notrigger) {
996 $result = $this->call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
1003 $this->error = $acc->error;
1004 $this->errors = $acc->errors;
1009 $this->db->commit();
1011 $this->db->rollback();
1016 return $bank_line_id;
1421 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
1423 global $conf, $langs, $hookmanager;
1425 if (!empty($conf->dol_no_mouse_hover)) {
1431 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
1432 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$this->ref;
1433 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1434 if ($dateofpayment) {
1435 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> ';
1437 if ($tmparray[
'seconds'] == 0 && $tmparray[
'minutes'] == 0 && ($tmparray[
'hours'] == 0 || $tmparray[
'hours'] == 12)) {
1444 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->
amount, 0, $langs, 1, -1, -1, $conf->currency);
1446 if ($mode ==
'withlistofinvoices') {
1448 if (is_array($arraybill) && count($arraybill) > 0) {
1449 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1450 $facturestatic =
new Facture($this->db);
1451 foreach ($arraybill as $billid) {
1452 $facturestatic->fetch($billid);
1453 $label .=
'<br> '.$facturestatic->getNomUrl(1,
'', 0, 0,
'', 1).
' '.$facturestatic->getLibStatut(2, -1);
1459 if (empty($notooltip)) {
1461 $label = $langs->trans(
"Payment");
1462 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
1464 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
1465 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1467 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1470 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id='.$this->id;
1472 $linkstart =
'<a href="'.$url.
'"';
1473 $linkstart .= $linkclose.
'>';
1476 $result .= $linkstart;
1478 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1480 if ($withpicto && $withpicto != 2) {
1481 $result .= ($this->
ref ? $this->
ref : $this->id);
1483 $result .= $linkend;
1485 $hookmanager->initHooks(array($this->element .
'dao'));
1486 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1487 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1489 $result = $hookmanager->resPrint;
1491 $result .= $hookmanager->resPrint;