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.");
523 $amount_ht = $amount_tva = $amount_ttc = array();
524 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
526 @phan-var-force array<string,float> $amount_ht
527 @phan-var-force array<string,float> $amount_tva
528 @phan-var-force array<string,float> $amount_ttc
529 @phan-var-force array<string,float> $multicurrency_amount_ht
530 @phan-var-force array<string,float> $multicurrency_amount_tva
531 @phan-var-force array<string,float> $multicurrency_amount_ttc
536 $discount->fetch(0, $invoice->id);
537 if (empty($discount->id)) {
538 $discount->description =
'(DEPOSIT)';
539 $discount->fk_soc = $invoice->socid;
540 $discount->socid = $invoice->socid;
541 $discount->fk_facture_source = $invoice->id;
546 foreach ($invoice->lines as $line) {
547 if ($line->product_type != 9 && $line->total_ht != 0) {
548 $key = $line->tva_tx.
'|'.(string) $line->vat_src_code;
549 if (!array_key_exists($key, $amount_ht)) {
550 $amount_ht[$key] = 0.0;
551 $amount_tva[$key] = 0.0;
552 $amount_ttc[$key] = 0.0;
553 $multicurrency_amount_ht[$key] = 0.0;
554 $multicurrency_amount_tva[$key] = 0.0;
555 $multicurrency_amount_ttc[$key] = 0.0;
557 $amount_ht[$key] += $line->total_ht;
558 $amount_tva[$key] += $line->total_tva;
559 $amount_ttc[$key] += $line->total_ttc;
560 $multicurrency_amount_ht[$key] += $line->multicurrency_total_ht;
561 $multicurrency_amount_tva[$key] += $line->multicurrency_total_tva;
562 $multicurrency_amount_ttc[$key] += $line->multicurrency_total_ttc;
567 foreach ($amount_ht as $key => $xxx) {
568 $parts = explode(
'|', (
string) $key, 2);
570 $vat_src_code = isset($parts[1]) ? $parts[1] :
'';
571 $discount->amount_ht = abs($amount_ht[$key]);
572 $discount->total_ht = abs($amount_ht[$key]);
573 $discount->amount_tva = abs($amount_tva[$key]);
574 $discount->total_tva = abs($amount_tva[$key]);
575 $discount->amount_ttc = abs($amount_ttc[$key]);
576 $discount->total_ttc = abs($amount_ttc[$key]);
577 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$key]);
578 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$key]);
579 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$key]);
580 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$key]);
581 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$key]);
582 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$key]);
583 $discount->tva_tx = abs((
float) $tva_tx);
584 $discount->vat_src_code = $vat_src_code;
586 $result = $discount->create($user);
595 $this->error = $discount->error;
596 $this->errors = $discount->errors;
603 $invoice->context[
'actionmsgmore'] =
'Invoice set to paid by the payment->create() of payment '.$this->ref.
' because the remain to pay is 0';
605 $result = $invoice->setPaid($user,
'',
'');
607 $this->error = $invoice->error;
608 $this->errors = $invoice->errors;
617 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);
620 $outputlangs = $langs;
622 $invoice->fetch_thirdparty();
623 $newlang = $invoice->thirdparty->default_lang;
625 if (!empty($newlang)) {
627 $outputlangs->setDefaultLang($newlang);
634 $ret = $invoice->fetch($facid);
636 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
638 dol_syslog(get_class($this).
'::create Regenerate end result='.$result, LOG_DEBUG);
640 $this->warnings = $invoice->warnings;
643 $this->error = $invoice->error;
644 $this->errors = $invoice->errors;
649 $this->error = $this->db->lasterror();
653 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
657 dol_syslog(get_class($this).
'::create Now we call the triggers if no error (error = '.$error.
')', LOG_DEBUG);
661 $result = $this->call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
668 $this->error = $this->db->lasterror();
675 $this->total = $total;
676 $this->multicurrency_amount = $mtotal;
677 $this->multicurrency_currency = $currencyofinvoices;
682 $this->db->rollback();
796 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'', $addbankurl =
'')
807 if ($accountid <= 0) {
808 $this->error =
'Bad value for parameter accountid='.$accountid;
809 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
813 $this->fk_account = $accountid;
815 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
817 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
819 $result = $acc->fetch($this->fk_account);
821 $this->error = $acc->error;
822 $this->errors = $acc->errors;
829 $totalamount = $this->amount;
830 $totalamount_main_currency =
null;
831 if (empty($totalamount)) {
832 $totalamount = $this->total;
838 if ($this->multicurrency_currency == $conf->currency) {
839 if ($this->
amount != $this->multicurrency_amount) {
842 $this->error =
'Payment in same currency than company but this->amount != this->multicurrency_amount';
847 if ($conf->currency != $acc->currency_code) {
848 $totalamount = $this->multicurrency_amount;
849 $totalamount_main_currency = $this->amount;
852 if ($mode ==
'payment_supplier') {
853 $totalamount = -$totalamount;
854 if (isset($totalamount_main_currency)) {
855 $totalamount_main_currency = -$totalamount_main_currency;
860 $bank_line_id = $acc->addline(
862 $this->paiementcode ? $this->paiementcode : $this->paiementid,
873 (float) $totalamount_main_currency
878 if ($bank_line_id > 0) {
888 if ($mode ==
'payment') {
889 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
891 if ($mode ==
'payment_supplier') {
892 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
895 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
905 $linkaddedforthirdparty = array();
906 foreach ($this->amounts as $key => $value) {
907 if ($mode ==
'payment') {
910 $fac->fetch_thirdparty();
911 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
912 $result = $acc->add_url_line(
914 $fac->thirdparty->id,
915 DOL_URL_ROOT.
'/comm/card.php?socid=',
916 (
string) $fac->thirdparty->name,
921 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
923 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
926 if ($mode ==
'payment_supplier') {
929 $fac->fetch_thirdparty();
930 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
931 $result = $acc->add_url_line(
933 $fac->thirdparty->id,
934 DOL_URL_ROOT.
'/fourn/card.php?socid=',
935 (
string) $fac->thirdparty->name,
940 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
942 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
949 if (!$error && $addbankurl && in_array($addbankurl, array(
'direct-debit',
'credit-transfer'))) {
950 $result = $acc->add_url_line(
952 $this->id_prelevement,
953 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
960 if (!$error && $label ==
'(InvoiceRefused)') {
961 $result = $acc->add_url_line(
963 $this->id_prelevement,
964 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
965 $this->num_prelevement,
970 if (!$error && !$notrigger) {
972 $result = $this->call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
979 $this->error = $acc->error;
980 $this->errors = $acc->errors;
987 $this->db->rollback();
992 return $bank_line_id;
1397 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
1399 global $conf, $langs, $hookmanager;
1401 if (!empty($conf->dol_no_mouse_hover)) {
1407 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
1408 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$this->ref;
1409 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1410 if ($dateofpayment) {
1411 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> ';
1413 if ($tmparray[
'seconds'] == 0 && $tmparray[
'minutes'] == 0 && ($tmparray[
'hours'] == 0 || $tmparray[
'hours'] == 12)) {
1420 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->
amount, 0, $langs, 1, -1, -1, $conf->currency);
1422 if ($mode ==
'withlistofinvoices') {
1424 if (is_array($arraybill) && count($arraybill) > 0) {
1425 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1426 $facturestatic =
new Facture($this->db);
1427 foreach ($arraybill as $billid) {
1428 $facturestatic->fetch($billid);
1429 $label .=
'<br> '.$facturestatic->getNomUrl(1,
'', 0, 0,
'', 1).
' '.$facturestatic->getLibStatut(2, -1);
1435 if (empty($notooltip)) {
1437 $label = $langs->trans(
"Payment");
1438 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
1440 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
1441 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1443 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1446 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id='.$this->id;
1448 $linkstart =
'<a href="'.$url.
'"';
1449 $linkstart .= $linkclose.
'>';
1452 $result .= $linkstart;
1454 $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);
1456 if ($withpicto && $withpicto != 2) {
1457 $result .= ($this->
ref ? $this->
ref : $this->id);
1459 $result .= $linkend;
1461 $hookmanager->initHooks(array($this->element .
'dao'));
1462 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1463 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1465 $result = $hookmanager->resPrint;
1467 $result .= $hookmanager->resPrint;