308 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
310 global
$conf, $langs;
319 $totalamount_converted = 0;
320 $atleastonepaymentnotnull = 0;
322 if ($way ==
'dolibarr') {
323 $amounts = &$this->amounts;
324 $amounts_to_update = &$this->multicurrency_amounts;
326 $amounts = &$this->multicurrency_amounts;
327 $amounts_to_update = &$this->amounts;
330 $currencyofpayment =
'';
331 $currencyofinvoices =
'';
332 $currencytxofpayment =
'';
334 foreach ($amounts as $key => $value) {
338 $value_converted =
false;
345 if ($tmparray ===
false) {
346 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
349 $invoice_multicurrency_tx = $tmparray[
'invoice_multicurrency_tx'];
350 $invoice_multicurrency_code = $tmparray[
'invoice_multicurrency_code'];
353 if ($invoice_multicurrency_tx) {
354 if ($way ==
'dolibarr') {
355 $value_converted = (float)
price2num($value * $invoice_multicurrency_tx,
'MU');
360 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
361 $invoice =
new Facture($this->db);
362 $invoice->fetch($key);
363 if ((
float)
price2num($value,
'MT') == (
float) $invoice->getRemainToPay(1)) {
364 $value_converted = (float) $invoice->getRemainToPay(0);
366 $value_converted = (float)
price2num($value / $invoice_multicurrency_tx,
'MU');
370 $invoice_multicurrency_tx =
false;
374 if ($value_converted ===
false) {
376 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
381 $currencyofinvoiceforthisline = empty($this->multicurrency_code[$key]) ? $invoice_multicurrency_code : $this->multicurrency_code[$key];
383 $currencyofpaymentforthisline = empty($this->multicurrency_amounts[$key]) ?
$conf->currency : $this->multicurrency_code[$key];
387 if (empty($currencyofinvoices)) {
388 $currencyofinvoices = $currencyofinvoiceforthisline;
389 } elseif ($currencyofinvoices != $currencyofinvoiceforthisline) {
391 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
395 if (empty($currencyofpayment)) {
396 $currencyofpayment = $currencyofpaymentforthisline;
397 } elseif ($currencyofpayment != $currencyofpaymentforthisline) {
399 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
403 if (empty($currencytxofpayment)) {
404 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] :
"";
407 $totalamount_converted += $value_converted;
408 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
411 $amounts[$key] = $newvalue;
412 $totalamount += $newvalue;
413 if (!empty($newvalue)) {
414 $atleastonepaymentnotnull++;
421 if (empty($currencyofpayment)) {
422 $currencyofpayment =
$conf->currency;
425 if (!empty($currencyofpayment)) {
427 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
428 $bankaccount =
new Account($this->db);
429 $bankaccount->fetch($this->fk_account);
430 $bankcurrencycode = empty($bankaccount->currency_code) ?
$conf->currency : $bankaccount->currency_code;
432 if ($bankcurrencycode !=
$conf->currency) {
434 if ($bankcurrencycode != $currencyofinvoices && $currencyofinvoices !=
$conf->currency) {
435 $langs->load(
"errors");
436 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofinvoices, $bankcurrencycode);
439 if ($bankcurrencycode != $currencyofpayment && $currencyofpayment !=
$conf->currency) {
440 $langs->load(
"errors");
441 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
449 $totalamount = (float)
price2num($totalamount,
'MT');
450 $totalamount_converted = (float)
price2num($totalamount_converted,
'MT');
453 if (empty($totalamount) && empty($atleastonepaymentnotnull)) {
454 $this->errors[] =
'TotalAmountEmpty';
455 $this->error = $langs->trans(
'TotalAmountEmpty');
459 dol_syslog(get_class($this).
"::create insert paiement (closepaidinvoices = ".$closepaidinvoices.
")", LOG_DEBUG);
463 $this->
ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
465 if (empty($this->ref_ext)) {
469 if ($way ==
'dolibarr') {
470 $total = $totalamount;
471 $mtotal = $totalamount_converted;
473 $total = $totalamount_converted;
474 $mtotal = $totalamount;
477 $num_payment = $this->num_payment;
478 $note = ($this->note_private ? $this->note_private : $this->note);
480 $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)";
481 $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).
", ";
482 $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).
")";
484 $resql = $this->db->query($sql);
486 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiement');
489 foreach ($this->amounts as $key => $amount) {
491 if (is_numeric($amount) && $amount != 0) {
493 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
494 $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).
")";
496 dol_syslog(get_class($this).
'::create Amount line '.$key.
' insert paiement_facture', LOG_DEBUG);
497 $resql = $this->db->query($sql);
499 $invoice =
new Facture($this->db);
500 $invoice->fetch($facid);
503 if ($closepaidinvoices) {
504 $paiement = $invoice->getSommePaiement();
505 $creditnotes = $invoice->getSumCreditNotesUsed();
506 $deposits = $invoice->getSumDepositsUsed();
507 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
508 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
513 $affected_types = array(
521 if (!in_array($invoice->type, $affected_types)) {
522 dol_syslog(
"Invoice ".$facid.
" is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
523 } elseif ($remaintopay) {
526 $hookmanager->initHooks(array(
'paymentdao'));
527 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
528 $action =
'CLOSEPAIDINVOICE';
529 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
531 $this->errors[] = $hookmanager->error;
532 $this->error = $hookmanager->error;
534 } elseif ($reshook == 0) {
535 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
541 $amount_ht = $amount_tva = $amount_ttc = array();
542 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
544 @phan-var-force array<string,float> $amount_ht
545 @phan-var-force array<string,float> $amount_tva
546 @phan-var-force array<string,float> $amount_ttc
547 @phan-var-force array<string,float> $multicurrency_amount_ht
548 @phan-var-force array<string,float> $multicurrency_amount_tva
549 @phan-var-force array<string,float> $multicurrency_amount_ttc
554 $discount->fetch(0, $invoice->id);
555 if (empty($discount->id)) {
556 $discount->description =
'(DEPOSIT)';
557 $discount->fk_soc = $invoice->socid;
558 $discount->socid = $invoice->socid;
559 $discount->fk_facture_source = $invoice->id;
563 foreach ($invoice->lines as $line) {
564 if ($line->product_type != 9 && $line->total_ht != 0) {
565 if (!array_key_exists($line->tva_tx, $amount_ht)) {
566 $amount_ht[$line->tva_tx] = 0.0;
567 $amount_tva[$line->tva_tx] = 0.0;
568 $amount_ttc[$line->tva_tx] = 0.0;
569 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
570 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
571 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
573 $amount_ht[$line->tva_tx] += $line->total_ht;
574 $amount_tva[$line->tva_tx] += $line->total_tva;
575 $amount_ttc[$line->tva_tx] += $line->total_ttc;
576 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
577 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
578 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
583 foreach ($amount_ht as $tva_tx => $xxx) {
584 $discount->amount_ht = abs($amount_ht[$tva_tx]);
585 $discount->total_ht = abs($amount_ht[$tva_tx]);
586 $discount->amount_tva = abs($amount_tva[$tva_tx]);
587 $discount->total_tva = abs($amount_tva[$tva_tx]);
588 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
589 $discount->total_ttc = abs($amount_ttc[$tva_tx]);
590 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
591 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$tva_tx]);
592 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
593 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$tva_tx]);
594 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
595 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
596 $discount->tva_tx = abs((
float) $tva_tx);
598 $result = $discount->create($user);
607 $this->error = $discount->error;
608 $this->errors = $discount->errors;
615 $invoice->context[
'actionmsgmore'] =
'Invoice set to paid by the payment->create() of payment '.$this->ref.
' because the remain to pay is 0';
617 $result = $invoice->setPaid($user,
'',
'');
619 $this->error = $invoice->error;
620 $this->errors = $invoice->errors;
629 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);
632 $outputlangs = $langs;
634 $invoice->fetch_thirdparty();
635 $newlang = $invoice->thirdparty->default_lang;
637 if (!empty($newlang)) {
639 $outputlangs->setDefaultLang($newlang);
646 $ret = $invoice->fetch($facid);
648 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
650 dol_syslog(get_class($this).
'::create Regenerate end result='.$result, LOG_DEBUG);
653 $this->error = $invoice->error;
654 $this->errors = $invoice->errors;
659 $this->error = $this->db->lasterror();
663 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
667 dol_syslog(get_class($this).
'::create Now we call the triggers if no error (error = '.$error.
')', LOG_DEBUG);
671 $result = $this->
call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
678 $this->error = $this->db->lasterror();
684 $this->amount = $total;
685 $this->total = $total;
686 $this->multicurrency_amount = $mtotal;
687 $this->multicurrency_currency = $currencyofinvoices;
692 $this->db->rollback();
804 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'', $addbankurl =
'')
814 if (isModEnabled(
"bank")) {
815 if ($accountid <= 0) {
816 $this->error =
'Bad value for parameter accountid='.$accountid;
817 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
821 $this->fk_account = $accountid;
823 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
825 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
827 $result = $acc->fetch($this->fk_account);
829 $this->error = $acc->error;
830 $this->errors = $acc->errors;
837 $totalamount = $this->amount;
838 $totalamount_main_currency =
null;
839 if (empty($totalamount)) {
840 $totalamount = $this->total;
846 if ($this->multicurrency_currency ==
$conf->currency) {
847 if ($this->amount != $this->multicurrency_amount) {
850 $this->error =
'Payment in same currency than company but this->amount != this->multicurrency_amount';
855 if (
$conf->currency != $acc->currency_code) {
856 $totalamount = $this->multicurrency_amount;
857 $totalamount_main_currency = $this->amount;
860 if ($mode ==
'payment_supplier') {
861 $totalamount = -$totalamount;
862 if (isset($totalamount_main_currency)) {
863 $totalamount_main_currency = -$totalamount_main_currency;
868 $bank_line_id = $acc->addline(
870 $this->paiementcode ? $this->paiementcode : $this->paiementid,
881 (float) $totalamount_main_currency
886 if ($bank_line_id > 0) {
896 if ($mode ==
'payment') {
897 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
899 if ($mode ==
'payment_supplier') {
900 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
903 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
913 $linkaddedforthirdparty = array();
914 foreach ($this->amounts as $key => $value) {
917 if (!is_numeric($value) || $value == 0) {
920 if ($mode ==
'payment') {
923 $fac->fetch_thirdparty();
924 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
925 $result = $acc->add_url_line(
927 $fac->thirdparty->id,
928 DOL_URL_ROOT.
'/comm/card.php?socid=',
929 (
string) $fac->thirdparty->name,
934 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
936 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
939 if ($mode ==
'payment_supplier') {
942 $fac->fetch_thirdparty();
943 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
944 $result = $acc->add_url_line(
946 $fac->thirdparty->id,
947 DOL_URL_ROOT.
'/fourn/card.php?socid=',
948 (
string) $fac->thirdparty->name,
953 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
955 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
962 if (!$error && $addbankurl && in_array($addbankurl, array(
'direct-debit',
'credit-transfer'))) {
963 $result = $acc->add_url_line(
965 $this->id_prelevement,
966 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
973 if (!$error && $label ==
'(InvoiceRefused)') {
974 $result = $acc->add_url_line(
976 $this->id_prelevement,
977 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
978 $this->num_prelevement,
983 if (!$error && !$notrigger) {
985 $result = $this->
call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
992 $this->error = $acc->error;
993 $this->errors = $acc->errors;
1000 $this->db->rollback();
1005 return $bank_line_id;
1410 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
1412 global
$conf, $langs, $hookmanager;
1414 if (!empty(
$conf->dol_no_mouse_hover)) {
1420 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
1421 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$this->ref;
1422 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1423 if ($dateofpayment) {
1424 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> ';
1426 if ($tmparray[
'seconds'] == 0 && $tmparray[
'minutes'] == 0 && ($tmparray[
'hours'] == 0 || $tmparray[
'hours'] == 12)) {
1432 if ($this->amount) {
1433 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1,
$conf->currency);
1435 if ($mode ==
'withlistofinvoices') {
1437 if (is_array($arraybill) && count($arraybill) > 0) {
1438 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1439 $facturestatic =
new Facture($this->db);
1440 foreach ($arraybill as $billid) {
1441 $facturestatic->fetch($billid);
1442 $label .=
'<br> '.$facturestatic->getNomUrl(1,
'', 0, 0,
'', 1).
' '.$facturestatic->getLibStatut(2, -1);
1448 if (empty($notooltip)) {
1450 $label = $langs->trans(
"Payment");
1451 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
1453 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
1454 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1456 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1459 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id='.$this->id;
1461 $linkstart =
'<a href="'.$url.
'"';
1462 $linkstart .= $linkclose.
'>';
1465 $result .= $linkstart;
1467 $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);
1469 if ($withpicto && $withpicto != 2) {
1470 $result .= ($this->
ref ? $this->
ref : $this->id);
1472 $result .= $linkend;
1474 $hookmanager->initHooks(array($this->element .
'dao'));
1475 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1476 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1478 $result = $hookmanager->resPrint;
1480 $result .= $hookmanager->resPrint;