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