dolibarr 20.0.5
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;
119 public $chid;
123 public $label;
124
128 public $paymenttype;
129
133 public $bank_account;
134
138 public $bank_line;
139
140
146 public function __construct($db)
147 {
148 $this->db = $db;
149 }
150
158 public function create($user)
159 {
160 global $conf, $langs;
161
162 $error = 0;
163
164 $now = dol_now();
165
166 // Validate parameters
167 if (!$this->datep) {
168 $this->error = 'ErrorBadValueForParameter';
169 return -1;
170 }
171
172 // Clean parameters
173 if (isset($this->fk_loan)) {
174 $this->fk_loan = (int) $this->fk_loan;
175 }
176 if (isset($this->amount_capital)) {
177 $this->amount_capital = (float) price2num($this->amount_capital ? $this->amount_capital : 0);
178 }
179 if (isset($this->amount_insurance)) {
180 $this->amount_insurance = (float) price2num($this->amount_insurance ? $this->amount_insurance : 0);
181 }
182 if (isset($this->amount_interest)) {
183 $this->amount_interest = (float) price2num($this->amount_interest ? $this->amount_interest : 0);
184 }
185 if (isset($this->fk_typepayment)) {
186 $this->fk_typepayment = (int) $this->fk_typepayment;
187 }
188 if (isset($this->num_payment)) {
189 $this->num_payment = trim($this->num_payment);
190 }
191 if (isset($this->note_private)) {
192 $this->note_private = trim($this->note_private);
193 }
194 if (isset($this->note_public)) {
195 $this->note_public = trim($this->note_public);
196 }
197 if (isset($this->fk_bank)) {
198 $this->fk_bank = (int) $this->fk_bank;
199 }
200 if (isset($this->fk_user_creat)) {
201 $this->fk_user_creat = (int) $this->fk_user_creat;
202 }
203 if (isset($this->fk_user_modif)) {
204 $this->fk_user_modif = (int) $this->fk_user_modif;
205 }
206
207 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
208 $totalamount = (float) price2num($totalamount);
209
210 // Check parameters
211 if ($totalamount == 0) {
212 return -1; // Negative amounts are accepted for reject prelevement but not null
213 }
214
215
216 $this->db->begin();
217
218 if ($totalamount != 0) {
219 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
220 $sql .= " fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
221 $sql .= " VALUES (".$this->chid.", '".$this->db->idate($now)."',";
222 $sql .= " '".$this->db->idate($this->datep)."',";
223 $sql .= " ".price2num($this->amount_capital).",";
224 $sql .= " ".price2num($this->amount_insurance).",";
225 $sql .= " ".price2num($this->amount_interest).",";
226 $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_private)."', '".$this->db->escape($this->note_public)."', ".$user->id.",";
227 $sql .= " 0)";
228
229 dol_syslog(get_class($this)."::create", LOG_DEBUG);
230 $resql = $this->db->query($sql);
231 if ($resql) {
232 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_loan");
233 } else {
234 $this->error = $this->db->lasterror();
235 $error++;
236 }
237 }
238
239 if ($totalamount != 0 && !$error) {
240 $this->amount_capital = $totalamount;
241 $this->db->commit();
242 return $this->id;
243 } else {
244 $this->error = $this->db->lasterror();
245 $this->db->rollback();
246 return -1;
247 }
248 }
249
256 public function fetch($id)
257 {
258 global $langs;
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 global $conf, $langs;
332 $error = 0;
333
334 // Clean parameters
335 if (isset($this->fk_loan)) {
336 $this->fk_loan = (int) $this->fk_loan;
337 }
338 if (isset($this->amount_capital)) {
339 $this->amount_capital = (float) $this->amount_capital;
340 }
341 if (isset($this->amount_insurance)) {
342 $this->amount_insurance = (float) $this->amount_insurance;
343 }
344 if (isset($this->amount_interest)) {
345 $this->amount_interest = (float) $this->amount_interest;
346 }
347 if (isset($this->fk_typepayment)) {
348 $this->fk_typepayment = (int) $this->fk_typepayment;
349 }
350 if (isset($this->num_payment)) {
351 $this->num_payment = trim($this->num_payment);
352 }
353 if (isset($this->note_private)) {
354 $this->note = trim($this->note_private);
355 }
356 if (isset($this->note_public)) {
357 $this->note = trim($this->note_public);
358 }
359 if (isset($this->fk_bank)) {
360 $this->fk_bank = (int) $this->fk_bank;
361 }
362 if (isset($this->fk_user_creat)) {
363 $this->fk_user_creat = (int) $this->fk_user_creat;
364 }
365 if (isset($this->fk_user_modif)) {
366 $this->fk_user_modif = (int) $this->fk_user_modif;
367 }
368
369 // Check parameters
370
371 // Update request
372 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET";
373 $sql .= " fk_loan=".(isset($this->fk_loan) ? $this->fk_loan : "null").",";
374 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
375 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
376 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
377 $sql .= " amount_capital=".(isset($this->amount_capital) ? $this->amount_capital : "null").",";
378 $sql .= " amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance : "null").",";
379 $sql .= " amount_interest=".(isset($this->amount_interest) ? $this->amount_interest : "null").",";
380 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
381 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
382 $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
383 $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
384 $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
385 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
386 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
387 $sql .= " WHERE rowid=".((int) $this->id);
388
389 $this->db->begin();
390
391 dol_syslog(get_class($this)."::update", LOG_DEBUG);
392 $resql = $this->db->query($sql);
393 if (!$resql) {
394 $error++;
395 $this->errors[] = "Error ".$this->db->lasterror();
396 }
397
398 // Commit or rollback
399 if ($error) {
400 foreach ($this->errors as $errmsg) {
401 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
402 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
403 }
404 $this->db->rollback();
405 return -1 * $error;
406 } else {
407 $this->db->commit();
408 return 1;
409 }
410 }
411
412
420 public function delete($user, $notrigger = 0)
421 {
422 $error = 0;
423
424 $this->db->begin();
425
426 if ($this->bank_line > 0) {
427 $accline = new AccountLine($this->db);
428 $accline->fetch($this->bank_line);
429 $result = $accline->delete($user);
430 if ($result < 0) {
431 $this->errors[] = $accline->error;
432 $error++;
433 }
434 }
435
436 if (!$error) {
437 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan";
438 $sql .= " WHERE rowid=".((int) $this->id);
439
440 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
441 $resql = $this->db->query($sql);
442 if (!$resql) {
443 $error++;
444 $this->errors[] = "Error ".$this->db->lasterror();
445 }
446 }
447
448 // Set loan unpaid if loan has no other payment
449 if (!$error) {
450 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
451 $loan = new Loan($this->db);
452 $loan->fetch($this->fk_loan);
453 $sum_payment = $loan->getSumPayment();
454 if ($sum_payment == 0) {
455 dol_syslog(get_class($this)."::delete : set loan to unpaid", LOG_DEBUG);
456 if ($loan->setUnpaid($user) < 1) {
457 $error++;
458 dol_print_error($this->db);
459 }
460 }
461 }
462
463 // Commit or rollback
464 if ($error) {
465 foreach ($this->errors as $errmsg) {
466 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
467 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
468 }
469 $this->db->rollback();
470 return -1 * $error;
471 } else {
472 $this->db->commit();
473 return 1;
474 }
475 }
476
483 public function getLibStatut($mode = 0)
484 {
485 return $this->LibStatut($this->statut, $mode);
486 }
487
488 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
496 public function LibStatut($status, $mode = 0)
497 {
498 // phpcs:enable
499 return '';
500 }
501
515 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
516 {
517 global $conf;
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 = $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 '',
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 $url = DOL_URL_ROOT.'/loan/payment/card.php?id='.$this->id;
658
659 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
660 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
661 $add_save_lastsearch_values = 1;
662 }
663 if ($add_save_lastsearch_values) {
664 $url .= '&save_lastsearch_values=1';
665 }
666
667 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
668 $linkend = '</a>';
669
670 $result .= $linkstart;
671 if ($withpicto) {
672 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
673 }
674 if ($withpicto != 2) {
675 $result .= $this->ref;
676 }
677 $result .= $linkend;
678
679 global $action;
680 $hookmanager->initHooks(array($this->element . 'dao'));
681 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
682 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
683 if ($reshook > 0) {
684 $result = $hookmanager->resPrint;
685 } else {
686 $result .= $hookmanager->resPrint;
687 }
688 return $result;
689 }
690}
$object ref
Definition info.php:79
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 clicable 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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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...
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e e e e e statut
Definition invoice.php:2010