181 public function fetch($id, $ref =
'', $fk_bank = 0)
183 $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,';
184 $sql .=
' c.code as type_code, c.libelle as type_label,';
185 $sql .=
' p.num_paiement as num_payment, p.note,';
186 $sql .=
' b.fk_account';
187 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement as p LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
188 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON p.fk_bank = b.rowid';
189 $sql .=
' WHERE p.entity IN ('.getEntity(
'invoice').
')';
191 $sql .=
' AND p.rowid = '.((int) $id);
193 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
194 } elseif ($fk_bank) {
195 $sql .=
' AND p.fk_bank = '.((int) $fk_bank);
198 $resql = $this->db->query($sql);
200 if ($this->db->num_rows($resql)) {
201 $obj = $this->db->fetch_object($resql);
203 $this->
id = $obj->rowid;
204 $this->
ref = $obj->ref ? $obj->ref : $obj->rowid;
205 $this->ref_ext = $obj->ref_ext;
206 $this->date = $this->db->jdate($obj->dp);
207 $this->datepaye = $this->db->jdate($obj->dp);
208 $this->num_payment = $obj->num_payment;
209 $this->montant = $obj->amount;
210 $this->amount = $obj->amount;
211 $this->multicurrency_amount = $obj->multicurrency_amount;
212 $this->note = $obj->note;
213 $this->note_private = $obj->note;
214 $this->type_label = $obj->type_label;
215 $this->type_code = $obj->type_code;
216 $this->
statut = $obj->statut;
217 $this->ext_payment_id = $obj->ext_payment_id;
218 $this->ext_payment_site = $obj->ext_payment_site;
220 $this->bank_account = $obj->fk_account;
221 $this->fk_account = $obj->fk_account;
222 $this->bank_line = $obj->fk_bank;
224 $this->db->free($resql);
227 $this->db->free($resql);
246 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
248 global $conf, $langs;
257 $totalamount_converted = 0;
258 $atleastonepaymentnotnull = 0;
260 if ($way ==
'dolibarr') {
261 $amounts = &$this->amounts;
262 $amounts_to_update = &$this->multicurrency_amounts;
264 $amounts = &$this->multicurrency_amounts;
265 $amounts_to_update = &$this->amounts;
268 $currencyofpayment =
'';
269 $currencytxofpayment =
'';
271 foreach ($amounts as $key => $value) {
278 if ($value_converted ===
false) {
280 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
283 if (empty($currencyofpayment)) {
284 $currencyofpayment = isset($this->multicurrency_code[$key]) ? $this->multicurrency_code[$key] :
"";
285 } elseif ($currencyofpayment != $this->multicurrency_code[$key]) {
287 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
290 if (empty($currencytxofpayment)) {
291 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] :
"";
294 $totalamount_converted += $value_converted;
295 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
298 $amounts[$key] = $newvalue;
299 $totalamount += $newvalue;
300 if (!empty($newvalue)) {
301 $atleastonepaymentnotnull++;
305 if (empty($currencyofpayment)) {
306 $currencyofpayment = $conf->currency;
309 if (!empty($currencyofpayment)) {
311 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
312 $bankaccount =
new Account($this->db);
313 $bankaccount->fetch($this->fk_account);
314 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
315 if ($currencyofpayment != $bankcurrencycode && $currencyofpayment != $conf->currency && $bankcurrencycode != $conf->currency) {
316 $langs->load(
"errors");
317 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
324 $totalamount_converted =
price2num($totalamount_converted);
327 if (empty($totalamount) && empty($atleastonepaymentnotnull)) {
328 $this->errors[] =
'TotalAmountEmpty';
329 $this->error = $langs->trans(
'TotalAmountEmpty');
333 dol_syslog(get_class($this).
"::create insert paiement (closepaidinvoices = ".$closepaidinvoices.
")", LOG_DEBUG);
337 $this->
ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
339 if (empty($this->ref_ext)) {
343 if ($way ==
'dolibarr') {
345 $mtotal = $totalamount_converted;
347 $total = $totalamount_converted;
348 $mtotal = $totalamount;
351 $num_payment = $this->num_payment;
354 $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)";
355 $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).
", ";
356 $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).
")";
358 $resql = $this->db->query($sql);
360 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiement');
363 foreach ($this->amounts as $key => $amount) {
365 if (is_numeric($amount) && $amount != 0) {
367 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
368 $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).
")";
370 dol_syslog(get_class($this).
'::create Amount line '.$key.
' insert paiement_facture', LOG_DEBUG);
371 $resql = $this->db->query($sql);
373 $invoice =
new Facture($this->db);
374 $invoice->fetch($facid);
377 if ($closepaidinvoices) {
378 $paiement = $invoice->getSommePaiement();
379 $creditnotes = $invoice->getSumCreditNotesUsed();
380 $deposits = $invoice->getSumDepositsUsed();
381 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
382 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
387 $affected_types = array(
395 if (!in_array($invoice->type, $affected_types)) {
396 dol_syslog(
"Invoice ".$facid.
" is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
397 } elseif ($remaintopay) {
400 $hookmanager->initHooks(array(
'paymentdao'));
401 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
402 $action =
'CLOSEPAIDINVOICE';
403 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
405 $this->errors[] = $hookmanager->error;
406 $this->error = $hookmanager->error;
408 } elseif ($reshook == 0) {
409 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
415 $amount_ht = $amount_tva = $amount_ttc = array();
416 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
420 $discount->fetch(
'', $invoice->id);
421 if (empty($discount->id)) {
422 $discount->description =
'(DEPOSIT)';
423 $discount->fk_soc = $invoice->socid;
424 $discount->fk_facture_source = $invoice->id;
428 foreach ($invoice->lines as $line) {
429 if ($line->total_ht != 0) {
430 $amount_ht[$line->tva_tx] += $line->total_ht;
431 $amount_tva[$line->tva_tx] += $line->total_tva;
432 $amount_ttc[$line->tva_tx] += $line->total_ttc;
433 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
434 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
435 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
440 foreach ($amount_ht as $tva_tx => $xxx) {
441 $discount->amount_ht = abs($amount_ht[$tva_tx]);
442 $discount->amount_tva = abs($amount_tva[$tva_tx]);
443 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
444 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
445 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
446 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
447 $discount->tva_tx = abs($tva_tx);
449 $result = $discount->create($user);
458 $this->error = $discount->error;
459 $this->errors = $discount->errors;
466 $result = $invoice->setPaid($user,
'',
'');
468 $this->error = $invoice->error;
469 $this->errors = $invoice->errors;
478 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);
481 $outputlangs = $langs;
483 $invoice->fetch_thirdparty();
484 $newlang = $invoice->thirdparty->default_lang;
486 if (!empty($newlang)) {
488 $outputlangs->setDefaultLang($newlang);
495 $ret = $invoice->fetch($facid);
497 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
499 dol_syslog(get_class($this).
'::create Regenerate end result='.$result, LOG_DEBUG);
502 $this->error = $invoice->error;
503 $this->errors = $invoice->errors;
508 $this->error = $this->db->lasterror();
512 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
516 dol_syslog(get_class($this).
'::create Now we call the triggers if no error (error = '.$error.
')', LOG_DEBUG);
520 $result = $this->
call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
527 $this->error = $this->db->lasterror();
534 $this->multicurrency_amount = $mtotal;
538 $this->db->rollback();
654 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'', $addbankurl =
'')
661 if (isModEnabled(
"banque")) {
662 if ($accountid <= 0) {
663 $this->error =
'Bad value for parameter accountid='.$accountid;
664 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
668 $this->fk_account = $accountid;
670 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
672 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
674 $result = $acc->fetch($this->fk_account);
676 $this->error = $acc->error;
677 $this->errors = $acc->errors;
684 $totalamount = $this->amount;
685 $totalamount_main_currency =
null;
686 if (empty($totalamount)) {
691 if (isModEnabled(
'multicurrency') && $conf->currency != $acc->currency_code) {
692 $totalamount = $this->multicurrency_amount;
693 $totalamount_main_currency = $this->amount;
696 if ($mode ==
'payment_supplier') {
697 $totalamount = -$totalamount;
698 if (isset($totalamount_main_currency)) {
699 $totalamount_main_currency = -$totalamount_main_currency;
704 $bank_line_id = $acc->addline(
706 $this->paiementcode ? $this->paiementcode : $this->paiementid,
717 $totalamount_main_currency
722 if ($bank_line_id > 0) {
732 if ($mode ==
'payment') {
733 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
735 if ($mode ==
'payment_supplier') {
736 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
739 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
749 $linkaddedforthirdparty = array();
750 foreach ($this->amounts as $key => $value) {
751 if ($mode ==
'payment') {
754 $fac->fetch_thirdparty();
755 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
756 $result = $acc->add_url_line(
758 $fac->thirdparty->id,
759 DOL_URL_ROOT.
'/comm/card.php?socid=',
760 $fac->thirdparty->name,
764 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
766 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
769 if ($mode ==
'payment_supplier') {
772 $fac->fetch_thirdparty();
773 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
774 $result = $acc->add_url_line(
776 $fac->thirdparty->id,
777 DOL_URL_ROOT.
'/fourn/card.php?socid=',
778 $fac->thirdparty->name,
782 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
784 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
791 if (!$error && $addbankurl && in_array($addbankurl, array(
'direct-debit',
'credit-transfer'))) {
792 $result = $acc->add_url_line(
794 $this->id_prelevement,
795 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
802 if (!$error && $label ==
'(InvoiceRefused)') {
803 $result=$acc->add_url_line(
805 $this->id_prelevement,
806 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
807 $this->num_prelevement,
812 if (!$error && !$notrigger) {
814 $result = $this->
call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
821 $this->error = $acc->error;
822 $this->errors = $acc->errors;
829 $this->db->rollback();
834 return $bank_line_id;
1241 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
1243 global $conf, $langs, $hookmanager;
1245 if (!empty($conf->dol_no_mouse_hover)) {
1251 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
1252 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$this->ref;
1253 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1254 if ($dateofpayment) {
1255 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> ';
1257 if ($tmparray[
'seconds'] == 0 && $tmparray[
'minutes'] == 0 && ($tmparray[
'hours'] == 0 || $tmparray[
'hours'] == 12)) {
1263 if ($this->amount) {
1264 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
1266 if ($mode ==
'withlistofinvoices') {
1268 if (is_array($arraybill) && count($arraybill) > 0) {
1269 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1270 $facturestatic =
new Facture($this->db);
1271 foreach ($arraybill as $billid) {
1272 $facturestatic->fetch($billid);
1273 $label .=
'<br> '.$facturestatic->getNomUrl(1,
'', 0, 0,
'', 1).
' '.$facturestatic->getLibStatut(2, -1);
1279 if (empty($notooltip)) {
1281 $label = $langs->trans(
"Payment");
1282 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1284 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1285 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1287 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1290 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id='.$this->id;
1292 $linkstart =
'<a href="'.$url.
'"';
1293 $linkstart .= $linkclose.
'>';
1296 $result .= $linkstart;
1298 $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);
1300 if ($withpicto && $withpicto != 2) {
1301 $result .= ($this->
ref ? $this->
ref : $this->id);
1303 $result .= $linkend;
1305 $hookmanager->initHooks(array($this->element .
'dao'));
1306 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1307 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1309 $result = $hookmanager->resPrint;
1311 $result .= $hookmanager->resPrint;