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) {
340 if ($value_converted ===
false) {
342 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
347 $currencyofinvoiceforthisline = empty($this->multicurrency_code[$key]) ? $conf->currency : $this->multicurrency_code[$key];
349 $currencyofpaymentforthisline = empty($this->multicurrency_amounts[$key]) ? $conf->currency : $this->multicurrency_code[$key];
353 if (empty($currencyofinvoices)) {
354 $currencyofinvoices = $currencyofinvoiceforthisline;
355 } elseif ($currencyofinvoices != $currencyofinvoiceforthisline) {
357 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
361 if (empty($currencyofpayment)) {
362 $currencyofpayment = $currencyofpaymentforthisline;
363 } elseif ($currencyofpayment != $currencyofpaymentforthisline) {
365 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
369 if (empty($currencytxofpayment)) {
370 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] :
"";
373 $totalamount_converted += $value_converted;
374 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
377 $amounts[$key] = $newvalue;
378 $totalamount += $newvalue;
379 if (!empty($newvalue)) {
380 $atleastonepaymentnotnull++;
387 if (empty($currencyofpayment)) {
388 $currencyofpayment = $conf->currency;
391 if (!empty($currencyofpayment)) {
393 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
394 $bankaccount =
new Account($this->db);
395 $bankaccount->fetch($this->fk_account);
396 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
398 if ($bankcurrencycode != $conf->currency) {
400 if ($bankcurrencycode != $currencyofinvoices && $currencyofinvoices != $conf->currency) {
401 $langs->load(
"errors");
402 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofinvoices, $bankcurrencycode);
405 if ($bankcurrencycode != $currencyofpayment && $currencyofpayment != $conf->currency) {
406 $langs->load(
"errors");
407 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
415 $totalamount = (float)
price2num($totalamount);
416 $totalamount_converted = (float)
price2num($totalamount_converted);
419 if (empty($totalamount) && empty($atleastonepaymentnotnull)) {
420 $this->errors[] =
'TotalAmountEmpty';
421 $this->error = $langs->trans(
'TotalAmountEmpty');
425 dol_syslog(get_class($this).
"::create insert paiement (closepaidinvoices = ".$closepaidinvoices.
")", LOG_DEBUG);
429 $this->
ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
431 if (empty($this->ref_ext)) {
435 if ($way ==
'dolibarr') {
436 $total = $totalamount;
437 $mtotal = $totalamount_converted;
439 $total = $totalamount_converted;
440 $mtotal = $totalamount;
443 $num_payment = $this->num_payment;
444 $note = ($this->note_private ? $this->note_private : $this->note);
446 $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)";
447 $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).
", ";
448 $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).
")";
450 $resql = $this->db->query($sql);
452 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiement');
455 foreach ($this->amounts as $key => $amount) {
457 if (is_numeric($amount) && $amount != 0) {
459 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
460 $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).
")";
462 dol_syslog(get_class($this).
'::create Amount line '.$key.
' insert paiement_facture', LOG_DEBUG);
463 $resql = $this->db->query($sql);
465 $invoice =
new Facture($this->db);
466 $invoice->fetch($facid);
469 if ($closepaidinvoices) {
470 $paiement = $invoice->getSommePaiement();
471 $creditnotes = $invoice->getSumCreditNotesUsed();
472 $deposits = $invoice->getSumDepositsUsed();
473 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
474 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
479 $affected_types = array(
487 if (!in_array($invoice->type, $affected_types)) {
488 dol_syslog(
"Invoice ".$facid.
" is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
489 } elseif ($remaintopay) {
492 $hookmanager->initHooks(array(
'paymentdao'));
493 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
494 $action =
'CLOSEPAIDINVOICE';
495 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
497 $this->errors[] = $hookmanager->error;
498 $this->error = $hookmanager->error;
500 } elseif ($reshook == 0) {
501 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
507 $amount_ht = $amount_tva = $amount_ttc = array();
508 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
510 @phan-var-force array<string,float> $amount_ht
511 @phan-var-force array<string,float> $amount_tva
512 @phan-var-force array<string,float> $amount_ttc
513 @phan-var-force array<string,float> $multicurrency_amount_ht
514 @phan-var-force array<string,float> $multicurrency_amount_tva
515 @phan-var-force array<string,float> $multicurrency_amount_ttc
520 $discount->fetch(0, $invoice->id);
521 if (empty($discount->id)) {
522 $discount->description =
'(DEPOSIT)';
523 $discount->fk_soc = $invoice->socid;
524 $discount->socid = $invoice->socid;
525 $discount->fk_facture_source = $invoice->id;
529 foreach ($invoice->lines as $line) {
530 if ($line->total_ht != 0) {
531 if (!array_key_exists($line->tva_tx, $amount_ht)) {
532 $amount_ht[$line->tva_tx] = 0.0;
533 $amount_tva[$line->tva_tx] = 0.0;
534 $amount_ttc[$line->tva_tx] = 0.0;
535 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
536 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
537 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
539 $amount_ht[$line->tva_tx] += $line->total_ht;
540 $amount_tva[$line->tva_tx] += $line->total_tva;
541 $amount_ttc[$line->tva_tx] += $line->total_ttc;
542 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
543 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
544 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
549 foreach ($amount_ht as $tva_tx => $xxx) {
550 $discount->amount_ht = abs($amount_ht[$tva_tx]);
551 $discount->amount_tva = abs($amount_tva[$tva_tx]);
552 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
553 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
554 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
555 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
556 $discount->tva_tx = abs((
float) $tva_tx);
558 $result = $discount->create($user);
567 $this->error = $discount->error;
568 $this->errors = $discount->errors;
575 $invoice->context[
'actionmsgmore'] =
'Invoice set to paid by the payment->create() of payment '.$this->ref.
' because the remain to pay is 0';
577 $result = $invoice->setPaid($user,
'',
'');
579 $this->error = $invoice->error;
580 $this->errors = $invoice->errors;
589 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);
592 $outputlangs = $langs;
594 $invoice->fetch_thirdparty();
595 $newlang = $invoice->thirdparty->default_lang;
597 if (!empty($newlang)) {
599 $outputlangs->setDefaultLang($newlang);
606 $ret = $invoice->fetch($facid);
608 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
610 dol_syslog(get_class($this).
'::create Regenerate end result='.$result, LOG_DEBUG);
613 $this->error = $invoice->error;
614 $this->errors = $invoice->errors;
619 $this->error = $this->db->lasterror();
623 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
627 dol_syslog(get_class($this).
'::create Now we call the triggers if no error (error = '.$error.
')', LOG_DEBUG);
631 $result = $this->
call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
638 $this->error = $this->db->lasterror();
644 $this->amount = $total;
645 $this->total = $total;
646 $this->multicurrency_amount = $mtotal;
647 $this->multicurrency_currency = $currencyofinvoices;
652 $this->db->rollback();
764 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'', $addbankurl =
'')
774 if (isModEnabled(
"bank")) {
775 if ($accountid <= 0) {
776 $this->error =
'Bad value for parameter accountid='.$accountid;
777 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
781 $this->fk_account = $accountid;
783 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
785 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
787 $result = $acc->fetch($this->fk_account);
789 $this->error = $acc->error;
790 $this->errors = $acc->errors;
797 $totalamount = $this->amount;
798 $totalamount_main_currency =
null;
799 if (empty($totalamount)) {
800 $totalamount = $this->total;
806 if ($this->multicurrency_currency == $conf->currency) {
807 if ($this->amount != $this->multicurrency_amount) {
810 $this->error =
'Payment in same currency than company but this->amount != this->multicurrency_amount';
815 if ($conf->currency != $acc->currency_code) {
816 $totalamount = $this->multicurrency_amount;
817 $totalamount_main_currency = $this->amount;
820 if ($mode ==
'payment_supplier') {
821 $totalamount = -$totalamount;
822 if (isset($totalamount_main_currency)) {
823 $totalamount_main_currency = -$totalamount_main_currency;
828 $bank_line_id = $acc->addline(
830 $this->paiementcode ? $this->paiementcode : $this->paiementid,
841 $totalamount_main_currency
846 if ($bank_line_id > 0) {
856 if ($mode ==
'payment') {
857 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
859 if ($mode ==
'payment_supplier') {
860 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
863 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
873 $linkaddedforthirdparty = array();
874 foreach ($this->amounts as $key => $value) {
875 if ($mode ==
'payment') {
878 $fac->fetch_thirdparty();
879 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
880 $result = $acc->add_url_line(
882 $fac->thirdparty->id,
883 DOL_URL_ROOT.
'/comm/card.php?socid=',
884 $fac->thirdparty->name,
888 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
890 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
893 if ($mode ==
'payment_supplier') {
896 $fac->fetch_thirdparty();
897 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
898 $result = $acc->add_url_line(
900 $fac->thirdparty->id,
901 DOL_URL_ROOT.
'/fourn/card.php?socid=',
902 $fac->thirdparty->name,
906 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
908 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
915 if (!$error && $addbankurl && in_array($addbankurl, array(
'direct-debit',
'credit-transfer'))) {
916 $result = $acc->add_url_line(
918 $this->id_prelevement,
919 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
926 if (!$error && $label ==
'(InvoiceRefused)') {
927 $result = $acc->add_url_line(
929 $this->id_prelevement,
930 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
931 $this->num_prelevement,
936 if (!$error && !$notrigger) {
938 $result = $this->
call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
945 $this->error = $acc->error;
946 $this->errors = $acc->errors;
953 $this->db->rollback();
958 return $bank_line_id;
1365 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
1367 global $conf, $langs, $hookmanager;
1369 if (!empty($conf->dol_no_mouse_hover)) {
1375 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
1376 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$this->ref;
1377 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1378 if ($dateofpayment) {
1379 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> ';
1381 if ($tmparray[
'seconds'] == 0 && $tmparray[
'minutes'] == 0 && ($tmparray[
'hours'] == 0 || $tmparray[
'hours'] == 12)) {
1387 if ($this->amount) {
1388 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
1390 if ($mode ==
'withlistofinvoices') {
1392 if (is_array($arraybill) && count($arraybill) > 0) {
1393 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1394 $facturestatic =
new Facture($this->db);
1395 foreach ($arraybill as $billid) {
1396 $facturestatic->fetch($billid);
1397 $label .=
'<br> '.$facturestatic->getNomUrl(1,
'', 0, 0,
'', 1).
' '.$facturestatic->getLibStatut(2, -1);
1403 if (empty($notooltip)) {
1405 $label = $langs->trans(
"Payment");
1406 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1408 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1409 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1411 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1414 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id='.$this->id;
1416 $linkstart =
'<a href="'.$url.
'"';
1417 $linkstart .= $linkclose.
'>';
1420 $result .= $linkstart;
1422 $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);
1424 if ($withpicto && $withpicto != 2) {
1425 $result .= ($this->
ref ? $this->
ref : $this->id);
1427 $result .= $linkend;
1429 $hookmanager->initHooks(array($this->element .
'dao'));
1430 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1431 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1433 $result = $hookmanager->resPrint;
1435 $result .= $hookmanager->resPrint;