dolibarr 25.0.0-alpha
paymentloan.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014-2025 Alexandre Spangaro <alexandre@inovea-conseil.com>
3 * Copyright (C) 2015-2026 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
5 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
28
29
34{
38 public $element = 'payment_loan';
39
43 public $table_element = 'payment_loan';
44
48 public $picto = 'money-bill-alt';
49
53 public $fk_loan;
54
58 public $datec = '';
59
63 public $datep = '';
64
68 public $amounts = array();
69
73 public $amount_capital;
74
78 public $amount_insurance;
79
83 public $amount_interest;
84
88 public $fk_typepayment;
89
94 public $num_payment;
95
99 public $fk_bank;
100
104 public $fk_user_creat;
105
109 public $fk_user_modif;
110
114 public $type_code;
115
119 public $type_label;
120
124 public $chid;
125
129 public $label;
130
134 public $paymenttype;
135
139 public $bank_account;
140
144 public $bank_line;
145
146
152 public function __construct($db)
153 {
154 $this->db = $db;
155 }
156
165 public function create($user, $notrigger = 0)
166 {
167 $error = 0;
168
169 $now = dol_now();
170
171 // Validate parameters
172 if (!$this->datep) {
173 $this->error = 'ErrorBadValueForParameter';
174 return -1;
175 }
176
177 // Clean parameters
178 if (isset($this->fk_loan)) {
179 $this->fk_loan = (int) $this->fk_loan;
180 }
181 if (isset($this->amount_capital)) {
182 $this->amount_capital = (float) price2num($this->amount_capital ? $this->amount_capital : 0);
183 }
184 if (isset($this->amount_insurance)) {
185 $this->amount_insurance = (float) price2num($this->amount_insurance ? $this->amount_insurance : 0);
186 }
187 if (isset($this->amount_interest)) {
188 $this->amount_interest = (float) price2num($this->amount_interest ? $this->amount_interest : 0);
189 }
190 if (isset($this->fk_typepayment)) {
191 $this->fk_typepayment = (int) $this->fk_typepayment;
192 }
193 if (isset($this->num_payment)) {
194 $this->num_payment = trim($this->num_payment);
195 }
196 if (isset($this->note_private)) {
197 $this->note_private = trim($this->note_private);
198 }
199 if (isset($this->note_public)) {
200 $this->note_public = trim($this->note_public);
201 }
202 if (isset($this->fk_bank)) {
203 $this->fk_bank = (int) $this->fk_bank;
204 }
205 if (isset($this->fk_user_creat)) {
206 $this->fk_user_creat = (int) $this->fk_user_creat;
207 }
208 if (isset($this->fk_user_modif)) {
209 $this->fk_user_modif = (int) $this->fk_user_modif;
210 }
211
212 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
213 $totalamount = (float) price2num($totalamount);
214
215 // Check parameters
216 if ($totalamount == 0) {
217 return -1; // Negative amounts are accepted for reject prelevement but not null
218 }
219
220
221 $this->db->begin();
222
223 if ($totalamount != 0) {
224 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
225 $sql .= " fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
226 $sql .= " VALUES (".((int) $this->chid).", '".$this->db->idate($now)."',";
227 $sql .= " '".$this->db->idate($this->datep)."',";
228 $sql .= " ".price2num($this->amount_capital).",";
229 $sql .= " ".price2num($this->amount_insurance).",";
230 $sql .= " ".price2num($this->amount_interest).",";
231 $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_private)."', '".$this->db->escape($this->note_public)."', ".((int) $user->id).",";
232 $sql .= " 0)";
233
234 dol_syslog(get_class($this)."::create", LOG_DEBUG);
235 $resql = $this->db->query($sql);
236 if ($resql) {
237 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_loan");
238 } else {
239 $this->error = $this->db->lasterror();
240 $error++;
241 }
242 }
243
244 if ($totalamount != 0 && !$error && !$notrigger) {
245 // Call trigger
246 $result = $this->call_trigger('PAYMENTLOAN_CREATE', $user);
247 if ($result < 0) {
248 $error++;
249 }
250 // End call triggers
251 }
252
253 if ($totalamount != 0 && !$error) {
254 $this->amount_capital = $totalamount;
255 $this->db->commit();
256 return $this->id;
257 } else {
258 $this->error = $this->db->lasterror();
259 $this->db->rollback();
260 return -1;
261 }
262 }
263
270 public function fetch($id)
271 {
272 $sql = "SELECT";
273 $sql .= " t.rowid,";
274 $sql .= " t.fk_loan,";
275 $sql .= " t.datec,";
276 $sql .= " t.tms,";
277 $sql .= " t.datep,";
278 $sql .= " t.amount_capital,";
279 $sql .= " t.amount_insurance,";
280 $sql .= " t.amount_interest,";
281 $sql .= " t.fk_typepayment,";
282 $sql .= " t.num_payment,";
283 $sql .= " t.note_private,";
284 $sql .= " t.note_public,";
285 $sql .= " t.fk_bank,";
286 $sql .= " t.fk_user_creat,";
287 $sql .= " t.fk_user_modif,";
288 $sql .= " pt.code as type_code, pt.libelle as type_label,";
289 $sql .= ' b.fk_account';
290 $sql .= " FROM ".MAIN_DB_PREFIX."payment_loan as t";
291 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
292 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
293 $sql .= " WHERE t.rowid = ".((int) $id);
294
295 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
296 $resql = $this->db->query($sql);
297 if ($resql) {
298 if ($this->db->num_rows($resql)) {
299 $obj = $this->db->fetch_object($resql);
300
301 $this->id = $obj->rowid;
302 $this->ref = $obj->rowid;
303
304 $this->fk_loan = $obj->fk_loan;
305 $this->datec = $this->db->jdate($obj->datec);
306 $this->tms = $this->db->jdate($obj->tms);
307 $this->datep = $this->db->jdate($obj->datep);
308 $this->amount_capital = $obj->amount_capital;
309 $this->amount_insurance = $obj->amount_insurance;
310 $this->amount_interest = $obj->amount_interest;
311 $this->fk_typepayment = $obj->fk_typepayment;
312 $this->num_payment = $obj->num_payment;
313 $this->note_private = $obj->note_private;
314 $this->note_public = $obj->note_public;
315 $this->fk_bank = $obj->fk_bank;
316 $this->fk_user_creat = $obj->fk_user_creat;
317 $this->fk_user_modif = $obj->fk_user_modif;
318
319 $this->type_code = $obj->type_code;
320 $this->type_label = $obj->type_label;
321
322 $this->bank_account = $obj->fk_account;
323 $this->bank_line = $obj->fk_bank;
324 }
325 $this->db->free($resql);
326
327 return 1;
328 } else {
329 $this->error = "Error ".$this->db->lasterror();
330 return -1;
331 }
332 }
333
334
342 public function update($user = null, $notrigger = 0)
343 {
344 $error = 0;
345
346 // Clean parameters
347 if (isset($this->fk_loan)) {
348 $this->fk_loan = (int) $this->fk_loan;
349 }
350 if (isset($this->amount_capital)) {
351 $this->amount_capital = (float) $this->amount_capital;
352 }
353 if (isset($this->amount_insurance)) {
354 $this->amount_insurance = (float) $this->amount_insurance;
355 }
356 if (isset($this->amount_interest)) {
357 $this->amount_interest = (float) $this->amount_interest;
358 }
359 if (isset($this->fk_typepayment)) {
360 $this->fk_typepayment = (int) $this->fk_typepayment;
361 }
362 if (isset($this->num_payment)) {
363 $this->num_payment = trim($this->num_payment);
364 }
365 if (isset($this->note_private)) {
366 $this->note_private = trim($this->note_private);
367 }
368 if (isset($this->note_public)) {
369 $this->note_public = trim($this->note_public);
370 }
371 if (isset($this->fk_bank)) {
372 $this->fk_bank = (int) $this->fk_bank;
373 }
374 if (isset($this->fk_user_creat)) {
375 $this->fk_user_creat = (int) $this->fk_user_creat;
376 }
377 if (isset($this->fk_user_modif)) {
378 $this->fk_user_modif = (int) $this->fk_user_modif;
379 }
380
381 // Check parameters
382
383 // Update request
384 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET";
385 $sql .= " fk_loan=".(isset($this->fk_loan) ? ((int) $this->fk_loan) : "null").",";
386 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
387 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
388 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
389 $sql .= " amount_capital=".(isset($this->amount_capital) ? ((float) $this->amount_capital) : "null").",";
390 $sql .= " amount_insurance=".(isset($this->amount_insurance) ? ((float) $this->amount_insurance) : "null").",";
391 $sql .= " amount_interest=".(isset($this->amount_interest) ? ((float) $this->amount_interest) : "null").",";
392 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? ((int) $this->fk_typepayment) : "null").",";
393 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
394 $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
395 $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
396 $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
397 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
398 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
399 $sql .= " WHERE rowid=".((int) $this->id);
400
401 $this->db->begin();
402
403 dol_syslog(get_class($this)."::update", LOG_DEBUG);
404 $resql = $this->db->query($sql);
405 if (!$resql) {
406 $error++;
407 $this->errors[] = "Error ".$this->db->lasterror();
408 }
409
410 if (!$error && $user && !$notrigger) {
411 // Call trigger
412 $result = $this->call_trigger('PAYMENTLOAN_MODIFY', $user);
413 if ($result < 0) {
414 $error++;
415 }
416 // End call triggers
417 }
418
419 // Commit or rollback
420 if ($error) {
421 foreach ($this->errors as $errmsg) {
422 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
423 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
424 }
425 $this->db->rollback();
426 return -1 * $error;
427 } else {
428 $this->db->commit();
429 return 1;
430 }
431 }
432
433
441 public function delete($user, $notrigger = 0)
442 {
443 $error = 0;
444
445 $this->db->begin();
446
447 // Schedule lines paid by this payment are no longer paid
448 if ($this->fk_bank > 0) {
449 $sql = "UPDATE ".MAIN_DB_PREFIX."loan_schedule SET fk_bank = 0 WHERE fk_bank = ".((int) $this->fk_bank);
450 if (!$this->db->query($sql)) {
451 $error++;
452 $this->errors[] = "Error ".$this->db->lasterror();
453 }
454 }
455
456 if (!$error && $this->bank_line > 0) {
457 $accline = new AccountLine($this->db);
458 $accline->fetch($this->bank_line);
459 $result = $accline->delete($user);
460 if ($result < 0) {
461 $this->errors[] = $accline->error;
462 $error++;
463 }
464 }
465
466 if (!$error) {
467 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan";
468 $sql .= " WHERE rowid=".((int) $this->id);
469
470 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
471 $resql = $this->db->query($sql);
472 if (!$resql) {
473 $error++;
474 $this->errors[] = "Error ".$this->db->lasterror();
475 }
476 }
477
478 // Set loan unpaid if loan has no other payment
479 if (!$error) {
480 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
481 $loan = new Loan($this->db);
482 $loan->fetch($this->fk_loan);
483 $sum_payment = $loan->getSumPayment();
484 if ($sum_payment == 0) {
485 dol_syslog(get_class($this)."::delete : set loan to unpaid", LOG_DEBUG);
486 if ($loan->setUnpaid($user) < 1) {
487 $error++;
488 dol_print_error($this->db);
489 }
490 }
491 }
492
493 if (!$error && !$notrigger) {
494 // Call trigger
495 $result = $this->call_trigger('PAYMENTLOAN_DELETE', $user);
496 if ($result < 0) {
497 $error++;
498 }
499 // End call triggers
500 }
501
502 // Commit or rollback
503 if ($error) {
504 foreach ($this->errors as $errmsg) {
505 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
506 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
507 }
508 $this->db->rollback();
509 return -1 * $error;
510 } else {
511 $this->db->commit();
512 return 1;
513 }
514 }
515
522 public function getLibStatut($mode = 0)
523 {
524 return $this->LibStatut($this->status, $mode);
525 }
526
527 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
535 public function LibStatut($status, $mode = 0)
536 {
537 // phpcs:enable
538 return '';
539 }
540
554 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
555 {
556 $error = 0;
557 $this->db->begin();
558
559 if (isModEnabled("bank")) {
560 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
561
562 $acc = new Account($this->db);
563 $acc->fetch($accountid);
564
565 $total = (float) $this->amount_capital;
566 if ($mode == 'payment_loan') {
567 $total = -$total;
568 }
569
570 // Insert payment into llx_bank
571 $bank_line_id = $acc->addline(
572 $this->datep,
573 $this->paymenttype, // Payment mode ID or code ("CHQ or VIR for example") it's integer in db
574 $label,
575 $total,
576 $this->num_payment,
577 0,
578 $user,
579 $emetteur_nom,
580 $emetteur_banque
581 );
582
583 // Update fk_bank into llx_paiement.
584 // We know the payment who generated the account write
585 if ($bank_line_id > 0) {
586 $result = $this->update_fk_bank($bank_line_id);
587 if ($result <= 0) {
588 $error++;
589 dol_print_error($this->db);
590 }
591
592 // Add link 'payment_loan' in bank_url between payment and bank transaction
593 $url = '';
594 if ($mode == 'payment_loan') {
595 $url = DOL_URL_ROOT.'/loan/payment/card.php?id=';
596 }
597 if ($url) {
598 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(payment)', $mode);
599 if ($result <= 0) {
600 $error++;
601 dol_print_error($this->db);
602 }
603 }
604
605
606 // Add link 'loan' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
607 if ($mode == 'payment_loan') {
608 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.'/loan/card.php?id=', ($this->label ? $this->label : ''), 'loan');
609 if ($result <= 0) {
610 dol_print_error($this->db);
611 }
612 }
613 } else {
614 $this->error = $acc->error;
615 $error++;
616 }
617 }
618
619
620 // Set loan payment started if no set
621 if (!$error) {
622 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
623 $loan = new Loan($this->db);
624 $loan->fetch($fk_loan);
625 if ($loan->paid == $loan::STATUS_UNPAID) {
626 dol_syslog(get_class($this)."::addPaymentToBank : set loan payment to started", LOG_DEBUG);
627 if ($loan->setStarted($user) < 1) {
628 $error++;
629 dol_print_error($this->db);
630 }
631 }
632 }
633
634 if (!$error) {
635 $this->db->commit();
636 return 1;
637 } else {
638 $this->db->rollback();
639 return -1;
640 }
641 }
642
643
644 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
651 public function update_fk_bank($id_bank)
652 {
653 // phpcs:enable
654 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
655
656 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
657 $result = $this->db->query($sql);
658 if ($result) {
659 $this->fk_bank = ((int) $id_bank);
660 return 1;
661 } else {
662 $this->error = $this->db->error();
663 return 0;
664 }
665 }
666
677 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
678 {
679 global $langs, $conf, $hookmanager;
680
681 if (!empty($conf->dol_no_mouse_hover)) {
682 $notooltip = 1; // Force disable tooltips
683 }
684
685 $result = '';
686 $label = '<u>'.$langs->trans("Loan").'</u>';
687 if (!empty($this->id)) {
688 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
689 }
690 if ($moretitle) {
691 $label .= ' - '.$moretitle;
692 }
693
694 $baseurl = DOL_URL_ROOT.'/loan/payment/card.php';
695 $query = ['id' => $this->id];
696
697 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
698 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
699 $add_save_lastsearch_values = 1;
700 }
701 if ($add_save_lastsearch_values) {
702 $query += ['save_lastsearch_values' => 1];
703 }
704 $url = dolBuildUrl($baseurl, $query);
705
706 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
707 $linkend = '</a>';
708
709 $result .= $linkstart;
710 if ($withpicto) {
711 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
712 }
713 if ($withpicto != 2) {
714 $result .= $this->ref;
715 }
716 $result .= $linkend;
717
718 global $action;
719 $hookmanager->initHooks(array($this->element . 'dao'));
720 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
721 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
722 if ($reshook > 0) {
723 $result = $hookmanager->resPrint;
724 } else {
725 $result .= $hookmanager->resPrint;
726 }
727 return $result;
728 }
729}
$object ref
Definition info.php:90
Class to manage bank accounts.
Class to manage bank transaction lines.
Loan.
Class to manage payments of loans.
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
create($user, $notrigger=0)
Create payment of loan into database.
__construct($db)
Constructor.
addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
Add record into bank for payment with links between this bank record and invoices of payment.
getLibStatut($mode=0)
Return the label of the status.
getNomUrl($withpicto=0, $maxlen=0, $notooltip=0, $moretitle='', $save_lastsearch_value=-1)
Return clickable name (with eventually a picto)
fetch($id)
Load object in memory from database.
update_fk_bank($id_bank)
Update link between loan's payment and the line generate in llx_bank.
update($user=null, $notrigger=0)
Update database.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:181