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;
340 $invoice_multicurrency_tx = $tmparray[
'invoice_multicurrency_tx'];
341 $invoice_multicurrency_code = $tmparray[
'invoice_multicurrency_code'];
344 if ($invoice_multicurrency_tx) {
345 if ($way ==
'dolibarr') {
346 $value_converted = (float)
price2num($value * $invoice_multicurrency_tx,
'MU');
348 $value_converted = (float)
price2num($value / $invoice_multicurrency_tx,
'MU');
351 $invoice_multicurrency_tx =
false;
355 if ($value_converted ===
false) {
357 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
362 $currencyofinvoiceforthisline = empty($this->multicurrency_code[$key]) ? $invoice_multicurrency_code : $this->multicurrency_code[$key];
364 $currencyofpaymentforthisline = empty($this->multicurrency_amounts[$key]) ?
$conf->currency : $this->multicurrency_code[$key];
368 if (empty($currencyofinvoices)) {
369 $currencyofinvoices = $currencyofinvoiceforthisline;
370 } elseif ($currencyofinvoices != $currencyofinvoiceforthisline) {
372 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
376 if (empty($currencyofpayment)) {
377 $currencyofpayment = $currencyofpaymentforthisline;
378 } elseif ($currencyofpayment != $currencyofpaymentforthisline) {
380 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
384 if (empty($currencytxofpayment)) {
385 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] :
"";
388 $totalamount_converted += $value_converted;
389 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
392 $amounts[$key] = $newvalue;
393 $totalamount += $newvalue;
394 if (!empty($newvalue)) {
395 $atleastonepaymentnotnull++;
402 if (empty($currencyofpayment)) {
403 $currencyofpayment =
$conf->currency;
406 if (!empty($currencyofpayment)) {
408 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
409 $bankaccount =
new Account($this->db);
410 $bankaccount->fetch($this->fk_account);
411 $bankcurrencycode = empty($bankaccount->currency_code) ?
$conf->currency : $bankaccount->currency_code;
413 if ($bankcurrencycode !=
$conf->currency) {
415 if ($bankcurrencycode != $currencyofinvoices && $currencyofinvoices !=
$conf->currency) {
416 $langs->load(
"errors");
417 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofinvoices, $bankcurrencycode);
420 if ($bankcurrencycode != $currencyofpayment && $currencyofpayment !=
$conf->currency) {
421 $langs->load(
"errors");
422 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
430 $totalamount = (float)
price2num($totalamount,
'MT');
431 $totalamount_converted = (float)
price2num($totalamount_converted,
'MT');
434 if (empty($totalamount) && empty($atleastonepaymentnotnull)) {
435 $this->errors[] =
'TotalAmountEmpty';
436 $this->error = $langs->trans(
'TotalAmountEmpty');
440 dol_syslog(get_class($this).
"::create insert paiement (closepaidinvoices = ".$closepaidinvoices.
")", LOG_DEBUG);
444 $this->
ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
446 if (empty($this->ref_ext)) {
450 if ($way ==
'dolibarr') {
451 $total = $totalamount;
452 $mtotal = $totalamount_converted;
454 $total = $totalamount_converted;
455 $mtotal = $totalamount;
458 $num_payment = $this->num_payment;
459 $note = ($this->note_private ? $this->note_private : $this->note);
461 $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)";
462 $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).
", ";
463 $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).
")";
465 $resql = $this->db->query($sql);
467 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiement');
470 foreach ($this->amounts as $key => $amount) {
472 if (is_numeric($amount) && $amount != 0) {
474 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
475 $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).
")";
477 dol_syslog(get_class($this).
'::create Amount line '.$key.
' insert paiement_facture', LOG_DEBUG);
478 $resql = $this->db->query($sql);
480 $invoice =
new Facture($this->db);
481 $invoice->fetch($facid);
484 if ($closepaidinvoices) {
485 $paiement = $invoice->getSommePaiement();
486 $creditnotes = $invoice->getSumCreditNotesUsed();
487 $deposits = $invoice->getSumDepositsUsed();
488 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
489 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
494 $affected_types = array(
502 if (!in_array($invoice->type, $affected_types)) {
503 dol_syslog(
"Invoice ".$facid.
" is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
504 } elseif ($remaintopay) {
507 $hookmanager->initHooks(array(
'paymentdao'));
508 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
509 $action =
'CLOSEPAIDINVOICE';
510 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
512 $this->errors[] = $hookmanager->error;
513 $this->error = $hookmanager->error;
515 } elseif ($reshook == 0) {
516 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
522 $amount_ht = $amount_tva = $amount_ttc = array();
523 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
525 @phan-var-force array<string,float> $amount_ht
526 @phan-var-force array<string,float> $amount_tva
527 @phan-var-force array<string,float> $amount_ttc
528 @phan-var-force array<string,float> $multicurrency_amount_ht
529 @phan-var-force array<string,float> $multicurrency_amount_tva
530 @phan-var-force array<string,float> $multicurrency_amount_ttc
535 $discount->fetch(0, $invoice->id);
536 if (empty($discount->id)) {
537 $discount->description =
'(DEPOSIT)';
538 $discount->fk_soc = $invoice->socid;
539 $discount->socid = $invoice->socid;
540 $discount->fk_facture_source = $invoice->id;
544 foreach ($invoice->lines as $line) {
545 if ($line->product_type != 9 && $line->total_ht != 0) {
546 if (!array_key_exists($line->tva_tx, $amount_ht)) {
547 $amount_ht[$line->tva_tx] = 0.0;
548 $amount_tva[$line->tva_tx] = 0.0;
549 $amount_ttc[$line->tva_tx] = 0.0;
550 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
551 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
552 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
554 $amount_ht[$line->tva_tx] += $line->total_ht;
555 $amount_tva[$line->tva_tx] += $line->total_tva;
556 $amount_ttc[$line->tva_tx] += $line->total_ttc;
557 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
558 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
559 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
564 foreach ($amount_ht as $tva_tx => $xxx) {
565 $discount->amount_ht = abs($amount_ht[$tva_tx]);
566 $discount->total_ht = abs($amount_ht[$tva_tx]);
567 $discount->amount_tva = abs($amount_tva[$tva_tx]);
568 $discount->total_tva = abs($amount_tva[$tva_tx]);
569 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
570 $discount->total_ttc = abs($amount_ttc[$tva_tx]);
571 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
572 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$tva_tx]);
573 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
574 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$tva_tx]);
575 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
576 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
577 $discount->tva_tx = abs((
float) $tva_tx);
579 $result = $discount->create($user);
588 $this->error = $discount->error;
589 $this->errors = $discount->errors;
596 $invoice->context[
'actionmsgmore'] =
'Invoice set to paid by the payment->create() of payment '.$this->ref.
' because the remain to pay is 0';
598 $result = $invoice->setPaid($user,
'',
'');
600 $this->error = $invoice->error;
601 $this->errors = $invoice->errors;
610 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);
613 $outputlangs = $langs;
615 $invoice->fetch_thirdparty();
616 $newlang = $invoice->thirdparty->default_lang;
618 if (!empty($newlang)) {
620 $outputlangs->setDefaultLang($newlang);
627 $ret = $invoice->fetch($facid);
629 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
631 dol_syslog(get_class($this).
'::create Regenerate end result='.$result, LOG_DEBUG);
634 $this->error = $invoice->error;
635 $this->errors = $invoice->errors;
640 $this->error = $this->db->lasterror();
644 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
648 dol_syslog(get_class($this).
'::create Now we call the triggers if no error (error = '.$error.
')', LOG_DEBUG);
652 $result = $this->
call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
659 $this->error = $this->db->lasterror();
665 $this->amount = $total;
666 $this->total = $total;
667 $this->multicurrency_amount = $mtotal;
668 $this->multicurrency_currency = $currencyofinvoices;
673 $this->db->rollback();
785 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'', $addbankurl =
'')
795 if (isModEnabled(
"bank")) {
796 if ($accountid <= 0) {
797 $this->error =
'Bad value for parameter accountid='.$accountid;
798 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
802 $this->fk_account = $accountid;
804 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
806 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
808 $result = $acc->fetch($this->fk_account);
810 $this->error = $acc->error;
811 $this->errors = $acc->errors;
818 $totalamount = $this->amount;
819 $totalamount_main_currency =
null;
820 if (empty($totalamount)) {
821 $totalamount = $this->total;
827 if ($this->multicurrency_currency ==
$conf->currency) {
828 if ($this->amount != $this->multicurrency_amount) {
831 $this->error =
'Payment in same currency than company but this->amount != this->multicurrency_amount';
836 if (
$conf->currency != $acc->currency_code) {
837 $totalamount = $this->multicurrency_amount;
838 $totalamount_main_currency = $this->amount;
841 if ($mode ==
'payment_supplier') {
842 $totalamount = -$totalamount;
843 if (isset($totalamount_main_currency)) {
844 $totalamount_main_currency = -$totalamount_main_currency;
849 $bank_line_id = $acc->addline(
851 $this->paiementcode ? $this->paiementcode : $this->paiementid,
862 (float) $totalamount_main_currency
867 if ($bank_line_id > 0) {
877 if ($mode ==
'payment') {
878 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
880 if ($mode ==
'payment_supplier') {
881 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
884 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
894 $linkaddedforthirdparty = array();
895 foreach ($this->amounts as $key => $value) {
896 if ($mode ==
'payment') {
899 $fac->fetch_thirdparty();
900 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
901 $result = $acc->add_url_line(
903 $fac->thirdparty->id,
904 DOL_URL_ROOT.
'/comm/card.php?socid=',
905 (
string) $fac->thirdparty->name,
910 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
912 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
915 if ($mode ==
'payment_supplier') {
918 $fac->fetch_thirdparty();
919 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
920 $result = $acc->add_url_line(
922 $fac->thirdparty->id,
923 DOL_URL_ROOT.
'/fourn/card.php?socid=',
924 (
string) $fac->thirdparty->name,
929 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
931 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
938 if (!$error && $addbankurl && in_array($addbankurl, array(
'direct-debit',
'credit-transfer'))) {
939 $result = $acc->add_url_line(
941 $this->id_prelevement,
942 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
949 if (!$error && $label ==
'(InvoiceRefused)') {
950 $result = $acc->add_url_line(
952 $this->id_prelevement,
953 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
954 $this->num_prelevement,
959 if (!$error && !$notrigger) {
961 $result = $this->
call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
968 $this->error = $acc->error;
969 $this->errors = $acc->errors;
976 $this->db->rollback();
981 return $bank_line_id;
1386 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
1388 global
$conf, $langs, $hookmanager;
1390 if (!empty(
$conf->dol_no_mouse_hover)) {
1396 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
1397 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$this->ref;
1398 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1399 if ($dateofpayment) {
1400 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> ';
1402 if ($tmparray[
'seconds'] == 0 && $tmparray[
'minutes'] == 0 && ($tmparray[
'hours'] == 0 || $tmparray[
'hours'] == 12)) {
1408 if ($this->amount) {
1409 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1,
$conf->currency);
1411 if ($mode ==
'withlistofinvoices') {
1413 if (is_array($arraybill) && count($arraybill) > 0) {
1414 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1415 $facturestatic =
new Facture($this->db);
1416 foreach ($arraybill as $billid) {
1417 $facturestatic->fetch($billid);
1418 $label .=
'<br> '.$facturestatic->getNomUrl(1,
'', 0, 0,
'', 1).
' '.$facturestatic->getLibStatut(2, -1);
1424 if (empty($notooltip)) {
1426 $label = $langs->trans(
"Payment");
1427 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
1429 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
1430 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1432 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1435 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id='.$this->id;
1437 $linkstart =
'<a href="'.$url.
'"';
1438 $linkstart .= $linkclose.
'>';
1441 $result .= $linkstart;
1443 $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);
1445 if ($withpicto && $withpicto != 2) {
1446 $result .= ($this->
ref ? $this->
ref : $this->id);
1448 $result .= $linkend;
1450 $hookmanager->initHooks(array($this->element .
'dao'));
1451 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1452 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1454 $result = $hookmanager->resPrint;
1456 $result .= $hookmanager->resPrint;