307 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
309 global
$conf, $langs;
318 $totalamount_converted = 0;
319 $atleastonepaymentnotnull = 0;
321 if ($way ==
'dolibarr') {
322 $amounts = &$this->amounts;
323 $amounts_to_update = &$this->multicurrency_amounts;
325 $amounts = &$this->multicurrency_amounts;
326 $amounts_to_update = &$this->amounts;
329 $currencyofpayment =
'';
330 $currencyofinvoices =
'';
331 $currencytxofpayment =
'';
333 foreach ($amounts as $key => $value) {
337 $value_converted =
false;
339 $invoice_multicurrency_tx = $tmparray[
'invoice_multicurrency_tx'];
340 $invoice_multicurrency_code = $tmparray[
'invoice_multicurrency_code'];
343 if ($invoice_multicurrency_tx) {
344 if ($way ==
'dolibarr') {
345 $value_converted = (float)
price2num($value * $invoice_multicurrency_tx,
'MU');
347 $value_converted = (float)
price2num($value / $invoice_multicurrency_tx,
'MU');
350 $invoice_multicurrency_tx =
false;
354 if ($value_converted ===
false) {
356 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
361 $currencyofinvoiceforthisline = empty($this->multicurrency_code[$key]) ? $invoice_multicurrency_code : $this->multicurrency_code[$key];
363 $currencyofpaymentforthisline = empty($this->multicurrency_amounts[$key]) ?
$conf->currency : $this->multicurrency_code[$key];
367 if (empty($currencyofinvoices)) {
368 $currencyofinvoices = $currencyofinvoiceforthisline;
369 } elseif ($currencyofinvoices != $currencyofinvoiceforthisline) {
371 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
375 if (empty($currencyofpayment)) {
376 $currencyofpayment = $currencyofpaymentforthisline;
377 } elseif ($currencyofpayment != $currencyofpaymentforthisline) {
379 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
383 if (empty($currencytxofpayment)) {
384 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] :
"";
387 $totalamount_converted += $value_converted;
388 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
391 $amounts[$key] = $newvalue;
392 $totalamount += $newvalue;
393 if (!empty($newvalue)) {
394 $atleastonepaymentnotnull++;
401 if (empty($currencyofpayment)) {
402 $currencyofpayment =
$conf->currency;
405 if (!empty($currencyofpayment)) {
407 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
408 $bankaccount =
new Account($this->db);
409 $bankaccount->fetch($this->fk_account);
410 $bankcurrencycode = empty($bankaccount->currency_code) ?
$conf->currency : $bankaccount->currency_code;
412 if ($bankcurrencycode !=
$conf->currency) {
414 if ($bankcurrencycode != $currencyofinvoices && $currencyofinvoices !=
$conf->currency) {
415 $langs->load(
"errors");
416 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofinvoices, $bankcurrencycode);
419 if ($bankcurrencycode != $currencyofpayment && $currencyofpayment !=
$conf->currency) {
420 $langs->load(
"errors");
421 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
429 $totalamount = (float)
price2num($totalamount);
430 $totalamount_converted = (float)
price2num($totalamount_converted);
433 if (empty($totalamount) && empty($atleastonepaymentnotnull)) {
434 $this->errors[] =
'TotalAmountEmpty';
435 $this->error = $langs->trans(
'TotalAmountEmpty');
439 dol_syslog(get_class($this).
"::create insert paiement (closepaidinvoices = ".$closepaidinvoices.
")", LOG_DEBUG);
443 $this->
ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
445 if (empty($this->ref_ext)) {
449 if ($way ==
'dolibarr') {
450 $total = $totalamount;
451 $mtotal = $totalamount_converted;
453 $total = $totalamount_converted;
454 $mtotal = $totalamount;
457 $num_payment = $this->num_payment;
458 $note = ($this->note_private ? $this->note_private : $this->note);
460 $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)";
461 $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).
", ";
462 $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).
")";
464 $resql = $this->db->query($sql);
466 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiement');
469 foreach ($this->amounts as $key => $amount) {
471 if (is_numeric($amount) && $amount != 0) {
473 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
474 $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).
")";
476 dol_syslog(get_class($this).
'::create Amount line '.$key.
' insert paiement_facture', LOG_DEBUG);
477 $resql = $this->db->query($sql);
479 $invoice =
new Facture($this->db);
480 $invoice->fetch($facid);
483 if ($closepaidinvoices) {
484 $paiement = $invoice->getSommePaiement();
485 $creditnotes = $invoice->getSumCreditNotesUsed();
486 $deposits = $invoice->getSumDepositsUsed();
487 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
488 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
493 $affected_types = array(
501 if (!in_array($invoice->type, $affected_types)) {
502 dol_syslog(
"Invoice ".$facid.
" is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
503 } elseif ($remaintopay) {
506 $hookmanager->initHooks(array(
'paymentdao'));
507 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
508 $action =
'CLOSEPAIDINVOICE';
509 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
511 $this->errors[] = $hookmanager->error;
512 $this->error = $hookmanager->error;
514 } elseif ($reshook == 0) {
515 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
521 $amount_ht = $amount_tva = $amount_ttc = array();
522 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
524 @phan-var-force array<string,float> $amount_ht
525 @phan-var-force array<string,float> $amount_tva
526 @phan-var-force array<string,float> $amount_ttc
527 @phan-var-force array<string,float> $multicurrency_amount_ht
528 @phan-var-force array<string,float> $multicurrency_amount_tva
529 @phan-var-force array<string,float> $multicurrency_amount_ttc
534 $discount->fetch(0, $invoice->id);
535 if (empty($discount->id)) {
536 $discount->description =
'(DEPOSIT)';
537 $discount->fk_soc = $invoice->socid;
538 $discount->socid = $invoice->socid;
539 $discount->fk_facture_source = $invoice->id;
543 foreach ($invoice->lines as $line) {
544 if ($line->total_ht != 0) {
545 if (!array_key_exists($line->tva_tx, $amount_ht)) {
546 $amount_ht[$line->tva_tx] = 0.0;
547 $amount_tva[$line->tva_tx] = 0.0;
548 $amount_ttc[$line->tva_tx] = 0.0;
549 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
550 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
551 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
553 $amount_ht[$line->tva_tx] += $line->total_ht;
554 $amount_tva[$line->tva_tx] += $line->total_tva;
555 $amount_ttc[$line->tva_tx] += $line->total_ttc;
556 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
557 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
558 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
563 foreach ($amount_ht as $tva_tx => $xxx) {
564 $discount->amount_ht = abs($amount_ht[$tva_tx]);
565 $discount->amount_tva = abs($amount_tva[$tva_tx]);
566 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
567 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
568 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
569 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
570 $discount->tva_tx = abs((
float) $tva_tx);
572 $result = $discount->create($user);
581 $this->error = $discount->error;
582 $this->errors = $discount->errors;
589 $invoice->context[
'actionmsgmore'] =
'Invoice set to paid by the payment->create() of payment '.$this->ref.
' because the remain to pay is 0';
591 $result = $invoice->setPaid($user,
'',
'');
593 $this->error = $invoice->error;
594 $this->errors = $invoice->errors;
603 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);
606 $outputlangs = $langs;
608 $invoice->fetch_thirdparty();
609 $newlang = $invoice->thirdparty->default_lang;
611 if (!empty($newlang)) {
613 $outputlangs->setDefaultLang($newlang);
620 $ret = $invoice->fetch($facid);
622 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
624 dol_syslog(get_class($this).
'::create Regenerate end result='.$result, LOG_DEBUG);
627 $this->error = $invoice->error;
628 $this->errors = $invoice->errors;
633 $this->error = $this->db->lasterror();
637 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
641 dol_syslog(get_class($this).
'::create Now we call the triggers if no error (error = '.$error.
')', LOG_DEBUG);
645 $result = $this->
call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
652 $this->error = $this->db->lasterror();
658 $this->amount = $total;
659 $this->total = $total;
660 $this->multicurrency_amount = $mtotal;
661 $this->multicurrency_currency = $currencyofinvoices;
666 $this->db->rollback();
778 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'', $addbankurl =
'')
788 if (isModEnabled(
"bank")) {
789 if ($accountid <= 0) {
790 $this->error =
'Bad value for parameter accountid='.$accountid;
791 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
795 $this->fk_account = $accountid;
797 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
799 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
801 $result = $acc->fetch($this->fk_account);
803 $this->error = $acc->error;
804 $this->errors = $acc->errors;
811 $totalamount = $this->amount;
812 $totalamount_main_currency =
null;
813 if (empty($totalamount)) {
814 $totalamount = $this->total;
820 if ($this->multicurrency_currency ==
$conf->currency) {
821 if ($this->amount != $this->multicurrency_amount) {
824 $this->error =
'Payment in same currency than company but this->amount != this->multicurrency_amount';
829 if (
$conf->currency != $acc->currency_code) {
830 $totalamount = $this->multicurrency_amount;
831 $totalamount_main_currency = $this->amount;
834 if ($mode ==
'payment_supplier') {
835 $totalamount = -$totalamount;
836 if (isset($totalamount_main_currency)) {
837 $totalamount_main_currency = -$totalamount_main_currency;
842 $bank_line_id = $acc->addline(
844 $this->paiementcode ? $this->paiementcode : $this->paiementid,
855 $totalamount_main_currency
860 if ($bank_line_id > 0) {
870 if ($mode ==
'payment') {
871 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
873 if ($mode ==
'payment_supplier') {
874 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
877 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
887 $linkaddedforthirdparty = array();
888 foreach ($this->amounts as $key => $value) {
889 if ($mode ==
'payment') {
892 $fac->fetch_thirdparty();
893 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
894 $result = $acc->add_url_line(
896 $fac->thirdparty->id,
897 DOL_URL_ROOT.
'/comm/card.php?socid=',
898 $fac->thirdparty->name,
902 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
904 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
907 if ($mode ==
'payment_supplier') {
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.
'/fourn/card.php?socid=',
916 $fac->thirdparty->name,
920 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
922 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
929 if (!$error && $addbankurl && in_array($addbankurl, array(
'direct-debit',
'credit-transfer'))) {
930 $result = $acc->add_url_line(
932 $this->id_prelevement,
933 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
940 if (!$error && $label ==
'(InvoiceRefused)') {
941 $result = $acc->add_url_line(
943 $this->id_prelevement,
944 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
945 $this->num_prelevement,
950 if (!$error && !$notrigger) {
952 $result = $this->
call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
959 $this->error = $acc->error;
960 $this->errors = $acc->errors;
967 $this->db->rollback();
972 return $bank_line_id;
1377 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
1379 global
$conf, $langs, $hookmanager;
1381 if (!empty(
$conf->dol_no_mouse_hover)) {
1387 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
1388 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$this->ref;
1389 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1390 if ($dateofpayment) {
1391 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> ';
1393 if ($tmparray[
'seconds'] == 0 && $tmparray[
'minutes'] == 0 && ($tmparray[
'hours'] == 0 || $tmparray[
'hours'] == 12)) {
1399 if ($this->amount) {
1400 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1,
$conf->currency);
1402 if ($mode ==
'withlistofinvoices') {
1404 if (is_array($arraybill) && count($arraybill) > 0) {
1405 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1406 $facturestatic =
new Facture($this->db);
1407 foreach ($arraybill as $billid) {
1408 $facturestatic->fetch($billid);
1409 $label .=
'<br> '.$facturestatic->getNomUrl(1,
'', 0, 0,
'', 1).
' '.$facturestatic->getLibStatut(2, -1);
1415 if (empty($notooltip)) {
1417 $label = $langs->trans(
"Payment");
1418 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1420 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1421 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1423 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1426 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id='.$this->id;
1428 $linkstart =
'<a href="'.$url.
'"';
1429 $linkstart .= $linkclose.
'>';
1432 $result .= $linkstart;
1434 $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);
1436 if ($withpicto && $withpicto != 2) {
1437 $result .= ($this->
ref ? $this->
ref : $this->id);
1439 $result .= $linkend;
1441 $hookmanager->initHooks(array($this->element .
'dao'));
1442 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1443 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1445 $result = $hookmanager->resPrint;
1447 $result .= $hookmanager->resPrint;