231 public function fetch($id, $ref =
'', $fk_bank = 0)
233 $sql =
'SELECT p.rowid, p.ref, p.ref_ext, p.datep as dp, p.amount, p.statut, p.ext_payment_id, p.ext_payment_site, p.fk_bank, p.multicurrency_amount,';
234 $sql .=
' c.code as type_code, c.libelle as type_label,';
235 $sql .=
' p.num_paiement as num_payment, p.note,';
236 $sql .=
' b.fk_account';
237 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement as p LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
238 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON p.fk_bank = b.rowid';
239 $sql .=
' WHERE p.entity IN ('.getEntity(
'invoice').
')';
241 $sql .=
' AND p.rowid = '.((int) $id);
243 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
244 } elseif ($fk_bank) {
245 $sql .=
' AND p.fk_bank = '.((int) $fk_bank);
248 $resql = $this->db->query($sql);
250 if ($this->db->num_rows($resql)) {
251 $obj = $this->db->fetch_object($resql);
253 $this->
id = $obj->rowid;
254 $this->
ref = $obj->ref ? $obj->ref : $obj->rowid;
255 $this->ref_ext = $obj->ref_ext;
256 $this->date = $this->db->jdate($obj->dp);
257 $this->datepaye = $this->db->jdate($obj->dp);
258 $this->num_payment = $obj->num_payment;
259 $this->montant = $obj->amount;
260 $this->amount = $obj->amount;
261 $this->multicurrency_amount = $obj->multicurrency_amount;
262 $this->note = $obj->note;
263 $this->note_private = $obj->note;
264 $this->type_label = $obj->type_label;
265 $this->type_code = $obj->type_code;
266 $this->
statut = $obj->statut;
267 $this->ext_payment_id = $obj->ext_payment_id;
268 $this->ext_payment_site = $obj->ext_payment_site;
270 $this->bank_account = $obj->fk_account;
271 $this->fk_account = $obj->fk_account;
272 $this->bank_line = $obj->fk_bank;
274 $this->db->free($resql);
277 $this->db->free($resql);
296 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
298 global $conf, $langs;
307 $totalamount_converted = 0;
308 $atleastonepaymentnotnull = 0;
310 if ($way ==
'dolibarr') {
311 $amounts = &$this->amounts;
312 $amounts_to_update = &$this->multicurrency_amounts;
314 $amounts = &$this->multicurrency_amounts;
315 $amounts_to_update = &$this->amounts;
318 $currencyofpayment =
'';
319 $currencytxofpayment =
'';
321 foreach ($amounts as $key => $value) {
328 if ($value_converted ===
false) {
330 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
333 if (empty($currencyofpayment)) {
334 $currencyofpayment = isset($this->multicurrency_code[$key]) ? $this->multicurrency_code[$key] :
"";
335 } elseif ($currencyofpayment != $this->multicurrency_code[$key]) {
337 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
340 if (empty($currencytxofpayment)) {
341 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] :
"";
344 $totalamount_converted += $value_converted;
345 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
348 $amounts[$key] = $newvalue;
349 $totalamount += $newvalue;
350 if (!empty($newvalue)) {
351 $atleastonepaymentnotnull++;
355 if (empty($currencyofpayment)) {
356 $currencyofpayment = $conf->currency;
359 if (!empty($currencyofpayment)) {
361 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
362 $bankaccount =
new Account($this->db);
363 $bankaccount->fetch($this->fk_account);
364 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
365 if ($currencyofpayment != $bankcurrencycode && $currencyofpayment != $conf->currency && $bankcurrencycode != $conf->currency) {
366 $langs->load(
"errors");
367 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
373 $totalamount = (float)
price2num($totalamount);
374 $totalamount_converted = (float)
price2num($totalamount_converted);
377 if (empty($totalamount) && empty($atleastonepaymentnotnull)) {
378 $this->errors[] =
'TotalAmountEmpty';
379 $this->error = $langs->trans(
'TotalAmountEmpty');
383 dol_syslog(get_class($this).
"::create insert paiement (closepaidinvoices = ".$closepaidinvoices.
")", LOG_DEBUG);
387 $this->
ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
389 if (empty($this->ref_ext)) {
393 if ($way ==
'dolibarr') {
395 $mtotal = $totalamount_converted;
397 $total = $totalamount_converted;
398 $mtotal = $totalamount;
401 $num_payment = $this->num_payment;
402 $note = ($this->note_private ? $this->note_private : $this->note);
404 $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)";
405 $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).
", ";
406 $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).
")";
408 $resql = $this->db->query($sql);
410 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiement');
413 foreach ($this->amounts as $key => $amount) {
415 if (is_numeric($amount) && $amount != 0) {
417 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
418 $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).
")";
420 dol_syslog(get_class($this).
'::create Amount line '.$key.
' insert paiement_facture', LOG_DEBUG);
421 $resql = $this->db->query($sql);
423 $invoice =
new Facture($this->db);
424 $invoice->fetch($facid);
427 if ($closepaidinvoices) {
428 $paiement = $invoice->getSommePaiement();
429 $creditnotes = $invoice->getSumCreditNotesUsed();
430 $deposits = $invoice->getSumDepositsUsed();
431 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
432 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
437 $affected_types = array(
445 if (!in_array($invoice->type, $affected_types)) {
446 dol_syslog(
"Invoice ".$facid.
" is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
447 } elseif ($remaintopay) {
450 $hookmanager->initHooks(array(
'paymentdao'));
451 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
452 $action =
'CLOSEPAIDINVOICE';
453 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
455 $this->errors[] = $hookmanager->error;
456 $this->error = $hookmanager->error;
458 } elseif ($reshook == 0) {
459 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
465 $amount_ht = $amount_tva = $amount_ttc = array();
466 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
470 $discount->fetch(
'', $invoice->id);
471 if (empty($discount->id)) {
472 $discount->description =
'(DEPOSIT)';
473 $discount->fk_soc = $invoice->socid;
474 $discount->socid = $invoice->socid;
475 $discount->fk_facture_source = $invoice->id;
479 foreach ($invoice->lines as $line) {
480 if ($line->total_ht != 0) {
481 $amount_ht[$line->tva_tx] += $line->total_ht;
482 $amount_tva[$line->tva_tx] += $line->total_tva;
483 $amount_ttc[$line->tva_tx] += $line->total_ttc;
484 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
485 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
486 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
491 foreach ($amount_ht as $tva_tx => $xxx) {
492 $discount->amount_ht = abs($amount_ht[$tva_tx]);
493 $discount->amount_tva = abs($amount_tva[$tva_tx]);
494 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
495 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
496 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
497 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
498 $discount->tva_tx = abs($tva_tx);
500 $result = $discount->create($user);
509 $this->error = $discount->error;
510 $this->errors = $discount->errors;
517 $result = $invoice->setPaid($user,
'',
'');
519 $this->error = $invoice->error;
520 $this->errors = $invoice->errors;
529 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);
532 $outputlangs = $langs;
534 $invoice->fetch_thirdparty();
535 $newlang = $invoice->thirdparty->default_lang;
537 if (!empty($newlang)) {
539 $outputlangs->setDefaultLang($newlang);
546 $ret = $invoice->fetch($facid);
548 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
550 dol_syslog(get_class($this).
'::create Regenerate end result='.$result, LOG_DEBUG);
553 $this->error = $invoice->error;
554 $this->errors = $invoice->errors;
559 $this->error = $this->db->lasterror();
563 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
567 dol_syslog(get_class($this).
'::create Now we call the triggers if no error (error = '.$error.
')', LOG_DEBUG);
571 $result = $this->
call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
578 $this->error = $this->db->lasterror();
585 $this->multicurrency_amount = $mtotal;
589 $this->db->rollback();
703 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'', $addbankurl =
'')
710 if (isModEnabled(
"bank")) {
711 if ($accountid <= 0) {
712 $this->error =
'Bad value for parameter accountid='.$accountid;
713 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
717 $this->fk_account = $accountid;
719 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
721 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
723 $result = $acc->fetch($this->fk_account);
725 $this->error = $acc->error;
726 $this->errors = $acc->errors;
733 $totalamount = $this->amount;
734 $totalamount_main_currency =
null;
735 if (empty($totalamount)) {
740 if (isModEnabled(
'multicurrency') && $conf->currency != $acc->currency_code) {
741 $totalamount = $this->multicurrency_amount;
742 $totalamount_main_currency = $this->amount;
745 if ($mode ==
'payment_supplier') {
746 $totalamount = -$totalamount;
747 if (isset($totalamount_main_currency)) {
748 $totalamount_main_currency = -$totalamount_main_currency;
753 $bank_line_id = $acc->addline(
755 $this->paiementcode ? $this->paiementcode : $this->paiementid,
766 $totalamount_main_currency
771 if ($bank_line_id > 0) {
781 if ($mode ==
'payment') {
782 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
784 if ($mode ==
'payment_supplier') {
785 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
788 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
798 $linkaddedforthirdparty = array();
799 foreach ($this->amounts as $key => $value) {
800 if ($mode ==
'payment') {
803 $fac->fetch_thirdparty();
804 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
805 $result = $acc->add_url_line(
807 $fac->thirdparty->id,
808 DOL_URL_ROOT.
'/comm/card.php?socid=',
809 $fac->thirdparty->name,
813 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
815 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
818 if ($mode ==
'payment_supplier') {
821 $fac->fetch_thirdparty();
822 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
823 $result = $acc->add_url_line(
825 $fac->thirdparty->id,
826 DOL_URL_ROOT.
'/fourn/card.php?socid=',
827 $fac->thirdparty->name,
831 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
833 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
840 if (!$error && $addbankurl && in_array($addbankurl, array(
'direct-debit',
'credit-transfer'))) {
841 $result = $acc->add_url_line(
843 $this->id_prelevement,
844 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
851 if (!$error && $label ==
'(InvoiceRefused)') {
852 $result = $acc->add_url_line(
854 $this->id_prelevement,
855 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
856 $this->num_prelevement,
861 if (!$error && !$notrigger) {
863 $result = $this->
call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
870 $this->error = $acc->error;
871 $this->errors = $acc->errors;
878 $this->db->rollback();
883 return $bank_line_id;
1292 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
1294 global $conf, $langs, $hookmanager;
1296 if (!empty($conf->dol_no_mouse_hover)) {
1302 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
1303 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$this->ref;
1304 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1305 if ($dateofpayment) {
1306 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> ';
1308 if ($tmparray[
'seconds'] == 0 && $tmparray[
'minutes'] == 0 && ($tmparray[
'hours'] == 0 || $tmparray[
'hours'] == 12)) {
1314 if ($this->amount) {
1315 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
1317 if ($mode ==
'withlistofinvoices') {
1319 if (is_array($arraybill) && count($arraybill) > 0) {
1320 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1321 $facturestatic =
new Facture($this->db);
1322 foreach ($arraybill as $billid) {
1323 $facturestatic->fetch($billid);
1324 $label .=
'<br> '.$facturestatic->getNomUrl(1,
'', 0, 0,
'', 1).
' '.$facturestatic->getLibStatut(2, 1);
1330 if (empty($notooltip)) {
1332 $label = $langs->trans(
"Payment");
1333 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1335 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1336 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1338 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1341 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id='.$this->id;
1343 $linkstart =
'<a href="'.$url.
'"';
1344 $linkstart .= $linkclose.
'>';
1347 $result .= $linkstart;
1349 $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);
1351 if ($withpicto && $withpicto != 2) {
1352 $result .= ($this->
ref ? $this->
ref : $this->id);
1354 $result .= $linkend;
1356 $hookmanager->initHooks(array($this->element .
'dao'));
1357 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1358 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1360 $result = $hookmanager->resPrint;
1362 $result .= $hookmanager->resPrint;