dolibarr 23.0.3
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-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
5 * Copyright (C) 2024 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
164 public function create($user)
165 {
166 $error = 0;
167
168 $now = dol_now();
169
170 // Validate parameters
171 if (!$this->datep) {
172 $this->error = 'ErrorBadValueForParameter';
173 return -1;
174 }
175
176 // Clean parameters
177 if (isset($this->fk_loan)) {
178 $this->fk_loan = (int) $this->fk_loan;
179 }
180 if (isset($this->amount_capital)) {
181 $this->amount_capital = (float) price2num($this->amount_capital ? $this->amount_capital : 0);
182 }
183 if (isset($this->amount_insurance)) {
184 $this->amount_insurance = (float) price2num($this->amount_insurance ? $this->amount_insurance : 0);
185 }
186 if (isset($this->amount_interest)) {
187 $this->amount_interest = (float) price2num($this->amount_interest ? $this->amount_interest : 0);
188 }
189 if (isset($this->fk_typepayment)) {
190 $this->fk_typepayment = (int) $this->fk_typepayment;
191 }
192 if (isset($this->num_payment)) {
193 $this->num_payment = trim($this->num_payment);
194 }
195 if (isset($this->note_private)) {
196 $this->note_private = trim($this->note_private);
197 }
198 if (isset($this->note_public)) {
199 $this->note_public = trim($this->note_public);
200 }
201 if (isset($this->fk_bank)) {
202 $this->fk_bank = (int) $this->fk_bank;
203 }
204 if (isset($this->fk_user_creat)) {
205 $this->fk_user_creat = (int) $this->fk_user_creat;
206 }
207 if (isset($this->fk_user_modif)) {
208 $this->fk_user_modif = (int) $this->fk_user_modif;
209 }
210
211 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
212 $totalamount = (float) price2num($totalamount);
213
214 // Check parameters
215 if ($totalamount == 0) {
216 return -1; // Negative amounts are accepted for reject prelevement but not null
217 }
218
219
220 $this->db->begin();
221
222 if ($totalamount != 0) {
223 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
224 $sql .= " fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
225 $sql .= " VALUES (".$this->chid.", '".$this->db->idate($now)."',";
226 $sql .= " '".$this->db->idate($this->datep)."',";
227 $sql .= " ".price2num($this->amount_capital).",";
228 $sql .= " ".price2num($this->amount_insurance).",";
229 $sql .= " ".price2num($this->amount_interest).",";
230 $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_private)."', '".$this->db->escape($this->note_public)."', ".$user->id.",";
231 $sql .= " 0)";
232
233 dol_syslog(get_class($this)."::create", LOG_DEBUG);
234 $resql = $this->db->query($sql);
235 if ($resql) {
236 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_loan");
237 } else {
238 $this->error = $this->db->lasterror();
239 $error++;
240 }
241 }
242
243 if ($totalamount != 0 && !$error) {
244 $this->amount_capital = $totalamount;
245 $this->db->commit();
246 return $this->id;
247 } else {
248 $this->error = $this->db->lasterror();
249 $this->db->rollback();
250 return -1;
251 }
252 }
253
260 public function fetch($id)
261 {
262 $sql = "SELECT";
263 $sql .= " t.rowid,";
264 $sql .= " t.fk_loan,";
265 $sql .= " t.datec,";
266 $sql .= " t.tms,";
267 $sql .= " t.datep,";
268 $sql .= " t.amount_capital,";
269 $sql .= " t.amount_insurance,";
270 $sql .= " t.amount_interest,";
271 $sql .= " t.fk_typepayment,";
272 $sql .= " t.num_payment,";
273 $sql .= " t.note_private,";
274 $sql .= " t.note_public,";
275 $sql .= " t.fk_bank,";
276 $sql .= " t.fk_user_creat,";
277 $sql .= " t.fk_user_modif,";
278 $sql .= " pt.code as type_code, pt.libelle as type_label,";
279 $sql .= ' b.fk_account';
280 $sql .= " FROM ".MAIN_DB_PREFIX."payment_loan as t";
281 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
282 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
283 $sql .= " WHERE t.rowid = ".((int) $id);
284
285 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
286 $resql = $this->db->query($sql);
287 if ($resql) {
288 if ($this->db->num_rows($resql)) {
289 $obj = $this->db->fetch_object($resql);
290
291 $this->id = $obj->rowid;
292 $this->ref = $obj->rowid;
293
294 $this->fk_loan = $obj->fk_loan;
295 $this->datec = $this->db->jdate($obj->datec);
296 $this->tms = $this->db->jdate($obj->tms);
297 $this->datep = $this->db->jdate($obj->datep);
298 $this->amount_capital = $obj->amount_capital;
299 $this->amount_insurance = $obj->amount_insurance;
300 $this->amount_interest = $obj->amount_interest;
301 $this->fk_typepayment = $obj->fk_typepayment;
302 $this->num_payment = $obj->num_payment;
303 $this->note_private = $obj->note_private;
304 $this->note_public = $obj->note_public;
305 $this->fk_bank = $obj->fk_bank;
306 $this->fk_user_creat = $obj->fk_user_creat;
307 $this->fk_user_modif = $obj->fk_user_modif;
308
309 $this->type_code = $obj->type_code;
310 $this->type_label = $obj->type_label;
311
312 $this->bank_account = $obj->fk_account;
313 $this->bank_line = $obj->fk_bank;
314 }
315 $this->db->free($resql);
316
317 return 1;
318 } else {
319 $this->error = "Error ".$this->db->lasterror();
320 return -1;
321 }
322 }
323
324
332 public function update($user = null, $notrigger = 0)
333 {
334 $error = 0;
335
336 // Clean parameters
337 if (isset($this->fk_loan)) {
338 $this->fk_loan = (int) $this->fk_loan;
339 }
340 if (isset($this->amount_capital)) {
341 $this->amount_capital = (float) $this->amount_capital;
342 }
343 if (isset($this->amount_insurance)) {
344 $this->amount_insurance = (float) $this->amount_insurance;
345 }
346 if (isset($this->amount_interest)) {
347 $this->amount_interest = (float) $this->amount_interest;
348 }
349 if (isset($this->fk_typepayment)) {
350 $this->fk_typepayment = (int) $this->fk_typepayment;
351 }
352 if (isset($this->num_payment)) {
353 $this->num_payment = trim($this->num_payment);
354 }
355 if (isset($this->note_private)) {
356 $this->note = trim($this->note_private);
357 }
358 if (isset($this->note_public)) {
359 $this->note = trim($this->note_public);
360 }
361 if (isset($this->fk_bank)) {
362 $this->fk_bank = (int) $this->fk_bank;
363 }
364 if (isset($this->fk_user_creat)) {
365 $this->fk_user_creat = (int) $this->fk_user_creat;
366 }
367 if (isset($this->fk_user_modif)) {
368 $this->fk_user_modif = (int) $this->fk_user_modif;
369 }
370
371 // Check parameters
372
373 // Update request
374 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET";
375 $sql .= " fk_loan=".(isset($this->fk_loan) ? $this->fk_loan : "null").",";
376 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
377 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
378 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
379 $sql .= " amount_capital=".(isset($this->amount_capital) ? $this->amount_capital : "null").",";
380 $sql .= " amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance : "null").",";
381 $sql .= " amount_interest=".(isset($this->amount_interest) ? $this->amount_interest : "null").",";
382 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
383 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
384 $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
385 $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
386 $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
387 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
388 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
389 $sql .= " WHERE rowid=".((int) $this->id);
390
391 $this->db->begin();
392
393 dol_syslog(get_class($this)."::update", LOG_DEBUG);
394 $resql = $this->db->query($sql);
395 if (!$resql) {
396 $error++;
397 $this->errors[] = "Error ".$this->db->lasterror();
398 }
399
400 // Commit or rollback
401 if ($error) {
402 foreach ($this->errors as $errmsg) {
403 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
404 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
405 }
406 $this->db->rollback();
407 return -1 * $error;
408 } else {
409 $this->db->commit();
410 return 1;
411 }
412 }
413
414
422 public function delete($user, $notrigger = 0)
423 {
424 $error = 0;
425
426 $this->db->begin();
427
428 if ($this->bank_line > 0) {
429 $accline = new AccountLine($this->db);
430 $accline->fetch($this->bank_line);
431 $result = $accline->delete($user);
432 if ($result < 0) {
433 $this->errors[] = $accline->error;
434 $error++;
435 }
436 }
437
438 if (!$error) {
439 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan";
440 $sql .= " WHERE rowid=".((int) $this->id);
441
442 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
443 $resql = $this->db->query($sql);
444 if (!$resql) {
445 $error++;
446 $this->errors[] = "Error ".$this->db->lasterror();
447 }
448 }
449
450 // Set loan unpaid if loan has no other payment
451 if (!$error) {
452 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
453 $loan = new Loan($this->db);
454 $loan->fetch($this->fk_loan);
455 $sum_payment = $loan->getSumPayment();
456 if ($sum_payment == 0) {
457 dol_syslog(get_class($this)."::delete : set loan to unpaid", LOG_DEBUG);
458 if ($loan->setUnpaid($user) < 1) {
459 $error++;
460 dol_print_error($this->db);
461 }
462 }
463 }
464
465 // Commit or rollback
466 if ($error) {
467 foreach ($this->errors as $errmsg) {
468 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
469 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
470 }
471 $this->db->rollback();
472 return -1 * $error;
473 } else {
474 $this->db->commit();
475 return 1;
476 }
477 }
478
485 public function getLibStatut($mode = 0)
486 {
487 return $this->LibStatut($this->statut, $mode);
488 }
489
490 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
498 public function LibStatut($status, $mode = 0)
499 {
500 // phpcs:enable
501 return '';
502 }
503
517 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
518 {
519 $error = 0;
520 $this->db->begin();
521
522 if (isModEnabled("bank")) {
523 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
524
525 $acc = new Account($this->db);
526 $acc->fetch($accountid);
527
528 $total = (float) $this->amount_capital;
529 if ($mode == 'payment_loan') {
530 $total = -$total;
531 }
532
533 // Insert payment into llx_bank
534 $bank_line_id = $acc->addline(
535 $this->datep,
536 $this->paymenttype, // Payment mode ID or code ("CHQ or VIR for example") it's integer in db
537 $label,
538 $total,
539 $this->num_payment,
540 0,
541 $user,
542 $emetteur_nom,
543 $emetteur_banque
544 );
545
546 // Update fk_bank into llx_paiement.
547 // We know the payment who generated the account write
548 if ($bank_line_id > 0) {
549 $result = $this->update_fk_bank($bank_line_id);
550 if ($result <= 0) {
551 $error++;
552 dol_print_error($this->db);
553 }
554
555 // Add link 'payment_loan' in bank_url between payment and bank transaction
556 $url = '';
557 if ($mode == 'payment_loan') {
558 $url = DOL_URL_ROOT.'/loan/payment/card.php?id=';
559 }
560 if ($url) {
561 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(payment)', $mode);
562 if ($result <= 0) {
563 $error++;
564 dol_print_error($this->db);
565 }
566 }
567
568
569 // Add link 'loan' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
570 if ($mode == 'payment_loan') {
571 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.'/loan/card.php?id=', ($this->label ? $this->label : ''), 'loan');
572 if ($result <= 0) {
573 dol_print_error($this->db);
574 }
575 }
576 } else {
577 $this->error = $acc->error;
578 $error++;
579 }
580 }
581
582
583 // Set loan payment started if no set
584 if (!$error) {
585 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
586 $loan = new Loan($this->db);
587 $loan->fetch($fk_loan);
588 if ($loan->paid == $loan::STATUS_UNPAID) {
589 dol_syslog(get_class($this)."::addPaymentToBank : set loan payment to started", LOG_DEBUG);
590 if ($loan->setStarted($user) < 1) {
591 $error++;
592 dol_print_error($this->db);
593 }
594 }
595 }
596
597 if (!$error) {
598 $this->db->commit();
599 return 1;
600 } else {
601 $this->db->rollback();
602 return -1;
603 }
604 }
605
606
607 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
614 public function update_fk_bank($id_bank)
615 {
616 // phpcs:enable
617 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
618
619 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
620 $result = $this->db->query($sql);
621 if ($result) {
622 $this->fk_bank = ((int) $id_bank);
623 return 1;
624 } else {
625 $this->error = $this->db->error();
626 return 0;
627 }
628 }
629
640 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
641 {
642 global $langs, $conf, $hookmanager;
643
644 if (!empty($conf->dol_no_mouse_hover)) {
645 $notooltip = 1; // Force disable tooltips
646 }
647
648 $result = '';
649 $label = '<u>'.$langs->trans("Loan").'</u>';
650 if (!empty($this->id)) {
651 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
652 }
653 if ($moretitle) {
654 $label .= ' - '.$moretitle;
655 }
656
657 $baseurl = DOL_URL_ROOT.'/loan/payment/card.php';
658 $query = ['id' => $this->id];
659
660 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
661 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
662 $add_save_lastsearch_values = 1;
663 }
664 if ($add_save_lastsearch_values) {
665 $query += ['save_lastsearch_values' => 1];
666 }
667 $url = dolBuildUrl($baseurl, $query);
668
669 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
670 $linkend = '</a>';
671
672 $result .= $linkstart;
673 if ($withpicto) {
674 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
675 }
676 if ($withpicto != 2) {
677 $result .= $this->ref;
678 }
679 $result .= $linkend;
680
681 global $action;
682 $hookmanager->initHooks(array($this->element . 'dao'));
683 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
684 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
685 if ($reshook > 0) {
686 $result = $hookmanager->resPrint;
687 } else {
688 $result .= $hookmanager->resPrint;
689 }
690 return $result;
691 }
692}
$object ref
Definition info.php:90
Class to manage bank accounts.
Class to manage bank transaction lines.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Loan.
Class to manage payments of loans.
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
__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.
create($user)
Create payment of loan into database.
update($user=null, $notrigger=0)
Update database.
dol_now($mode='gmt')
Return date for now.
dolBuildUrl($url, $params=[], $addtoken=false)
Return path of url.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...