dolibarr 19.0.4
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-2023 Frederic France <frederic.france@free.fr>
4 * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27
28
33{
37 public $element = 'payment_loan';
38
42 public $table_element = 'payment_loan';
43
47 public $picto = 'money-bill-alt';
48
52 public $fk_loan;
53
57 public $datec = '';
58
59 public $tms = '';
60
64 public $datep = '';
65
66 public $amounts = array(); // Array of amounts
67
68 public $amount_capital; // Total amount of payment
69
70 public $amount_insurance;
71
72 public $amount_interest;
73
77 public $fk_typepayment;
78
82 public $num_payment;
83
87 public $fk_bank;
88
92 public $fk_user_creat;
93
97 public $fk_user_modif;
98
99 public $type_code;
100 public $type_label;
101 public $chid;
102 public $label;
103 public $paymenttype;
104 public $bank_account;
105
109 public $bank_line;
110
111
117 public function __construct($db)
118 {
119 $this->db = $db;
120 }
121
129 public function create($user)
130 {
131 global $conf, $langs;
132
133 $error = 0;
134
135 $now = dol_now();
136
137 // Validate parameters
138 if (!$this->datep) {
139 $this->error = 'ErrorBadValueForParameter';
140 return -1;
141 }
142
143 // Clean parameters
144 if (isset($this->fk_loan)) {
145 $this->fk_loan = (int) $this->fk_loan;
146 }
147 if (isset($this->amount_capital)) {
148 $this->amount_capital = price2num($this->amount_capital ? $this->amount_capital : 0);
149 }
150 if (isset($this->amount_insurance)) {
151 $this->amount_insurance = price2num($this->amount_insurance ? $this->amount_insurance : 0);
152 }
153 if (isset($this->amount_interest)) {
154 $this->amount_interest = price2num($this->amount_interest ? $this->amount_interest : 0);
155 }
156 if (isset($this->fk_typepayment)) {
157 $this->fk_typepayment = (int) $this->fk_typepayment;
158 }
159 if (isset($this->num_payment)) {
160 $this->num_payment = (int) $this->num_payment;
161 }
162 if (isset($this->note_private)) {
163 $this->note_private = trim($this->note_private);
164 }
165 if (isset($this->note_public)) {
166 $this->note_public = trim($this->note_public);
167 }
168 if (isset($this->fk_bank)) {
169 $this->fk_bank = (int) $this->fk_bank;
170 }
171 if (isset($this->fk_user_creat)) {
172 $this->fk_user_creat = (int) $this->fk_user_creat;
173 }
174 if (isset($this->fk_user_modif)) {
175 $this->fk_user_modif = (int) $this->fk_user_modif;
176 }
177
178 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
179 $totalamount = price2num($totalamount);
180
181 // Check parameters
182 if ($totalamount == 0) {
183 return -1; // Negative amounts are accepted for reject prelevement but not null
184 }
185
186
187 $this->db->begin();
188
189 if ($totalamount != 0) {
190 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
191 $sql .= " fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
192 $sql .= " VALUES (".$this->chid.", '".$this->db->idate($now)."',";
193 $sql .= " '".$this->db->idate($this->datep)."',";
194 $sql .= " ".price2num($this->amount_capital).",";
195 $sql .= " ".price2num($this->amount_insurance).",";
196 $sql .= " ".price2num($this->amount_interest).",";
197 $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_private)."', '".$this->db->escape($this->note_public)."', ".$user->id.",";
198 $sql .= " 0)";
199
200 dol_syslog(get_class($this)."::create", LOG_DEBUG);
201 $resql = $this->db->query($sql);
202 if ($resql) {
203 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_loan");
204 } else {
205 $this->error = $this->db->lasterror();
206 $error++;
207 }
208 }
209
210 if ($totalamount != 0 && !$error) {
211 $this->amount_capital = $totalamount;
212 $this->db->commit();
213 return $this->id;
214 } else {
215 $this->error = $this->db->lasterror();
216 $this->db->rollback();
217 return -1;
218 }
219 }
220
227 public function fetch($id)
228 {
229 global $langs;
230 $sql = "SELECT";
231 $sql .= " t.rowid,";
232 $sql .= " t.fk_loan,";
233 $sql .= " t.datec,";
234 $sql .= " t.tms,";
235 $sql .= " t.datep,";
236 $sql .= " t.amount_capital,";
237 $sql .= " t.amount_insurance,";
238 $sql .= " t.amount_interest,";
239 $sql .= " t.fk_typepayment,";
240 $sql .= " t.num_payment,";
241 $sql .= " t.note_private,";
242 $sql .= " t.note_public,";
243 $sql .= " t.fk_bank,";
244 $sql .= " t.fk_user_creat,";
245 $sql .= " t.fk_user_modif,";
246 $sql .= " pt.code as type_code, pt.libelle as type_label,";
247 $sql .= ' b.fk_account';
248 $sql .= " FROM ".MAIN_DB_PREFIX."payment_loan as t";
249 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
250 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
251 $sql .= " WHERE t.rowid = ".((int) $id);
252
253 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
254 $resql = $this->db->query($sql);
255 if ($resql) {
256 if ($this->db->num_rows($resql)) {
257 $obj = $this->db->fetch_object($resql);
258
259 $this->id = $obj->rowid;
260 $this->ref = $obj->rowid;
261
262 $this->fk_loan = $obj->fk_loan;
263 $this->datec = $this->db->jdate($obj->datec);
264 $this->tms = $this->db->jdate($obj->tms);
265 $this->datep = $this->db->jdate($obj->datep);
266 $this->amount_capital = $obj->amount_capital;
267 $this->amount_insurance = $obj->amount_insurance;
268 $this->amount_interest = $obj->amount_interest;
269 $this->fk_typepayment = $obj->fk_typepayment;
270 $this->num_payment = $obj->num_payment;
271 $this->note_private = $obj->note_private;
272 $this->note_public = $obj->note_public;
273 $this->fk_bank = $obj->fk_bank;
274 $this->fk_user_creat = $obj->fk_user_creat;
275 $this->fk_user_modif = $obj->fk_user_modif;
276
277 $this->type_code = $obj->type_code;
278 $this->type_label = $obj->type_label;
279
280 $this->bank_account = $obj->fk_account;
281 $this->bank_line = $obj->fk_bank;
282 }
283 $this->db->free($resql);
284
285 return 1;
286 } else {
287 $this->error = "Error ".$this->db->lasterror();
288 return -1;
289 }
290 }
291
292
300 public function update($user = null, $notrigger = 0)
301 {
302 global $conf, $langs;
303 $error = 0;
304
305 // Clean parameters
306 if (isset($this->fk_loan)) {
307 $this->fk_loan = (int) $this->fk_loan;
308 }
309 if (isset($this->amount_capital)) {
310 $this->amount_capital = trim($this->amount_capital);
311 }
312 if (isset($this->amount_insurance)) {
313 $this->amount_insurance = trim($this->amount_insurance);
314 }
315 if (isset($this->amount_interest)) {
316 $this->amount_interest = trim($this->amount_interest);
317 }
318 if (isset($this->fk_typepayment)) {
319 $this->fk_typepayment = (int) $this->fk_typepayment;
320 }
321 if (isset($this->num_payment)) {
322 $this->num_payment = (int) $this->num_payment;
323 }
324 if (isset($this->note_private)) {
325 $this->note = trim($this->note_private);
326 }
327 if (isset($this->note_public)) {
328 $this->note = trim($this->note_public);
329 }
330 if (isset($this->fk_bank)) {
331 $this->fk_bank = (int) $this->fk_bank;
332 }
333 if (isset($this->fk_user_creat)) {
334 $this->fk_user_creat = (int) $this->fk_user_creat;
335 }
336 if (isset($this->fk_user_modif)) {
337 $this->fk_user_modif = (int) $this->fk_user_modif;
338 }
339
340 // Check parameters
341
342 // Update request
343 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET";
344 $sql .= " fk_loan=".(isset($this->fk_loan) ? $this->fk_loan : "null").",";
345 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
346 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
347 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
348 $sql .= " amount_capital=".(isset($this->amount_capital) ? $this->amount_capital : "null").",";
349 $sql .= " amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance : "null").",";
350 $sql .= " amount_interest=".(isset($this->amount_interest) ? $this->amount_interest : "null").",";
351 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
352 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
353 $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
354 $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
355 $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
356 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
357 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
358 $sql .= " WHERE rowid=".((int) $this->id);
359
360 $this->db->begin();
361
362 dol_syslog(get_class($this)."::update", LOG_DEBUG);
363 $resql = $this->db->query($sql);
364 if (!$resql) {
365 $error++;
366 $this->errors[] = "Error ".$this->db->lasterror();
367 }
368
369 // Commit or rollback
370 if ($error) {
371 foreach ($this->errors as $errmsg) {
372 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
373 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
374 }
375 $this->db->rollback();
376 return -1 * $error;
377 } else {
378 $this->db->commit();
379 return 1;
380 }
381 }
382
383
391 public function delete($user, $notrigger = 0)
392 {
393 $error = 0;
394
395 $this->db->begin();
396
397 if ($this->bank_line > 0) {
398 $accline = new AccountLine($this->db);
399 $accline->fetch($this->bank_line);
400 $result = $accline->delete($user);
401 if ($result < 0) {
402 $this->errors[] = $accline->error;
403 $error++;
404 }
405 }
406
407 if (!$error) {
408 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan";
409 $sql .= " WHERE rowid=".((int) $this->id);
410
411 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
412 $resql = $this->db->query($sql);
413 if (!$resql) {
414 $error++;
415 $this->errors[] = "Error ".$this->db->lasterror();
416 }
417 }
418
419 // Set loan unpaid if loan has no other payment
420 if (!$error) {
421 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
422 $loan = new Loan($this->db);
423 $loan->fetch($this->fk_loan);
424 $sum_payment = $loan->getSumPayment();
425 if ($sum_payment == 0) {
426 dol_syslog(get_class($this)."::delete : set loan to unpaid", LOG_DEBUG);
427 if ($loan->setUnpaid($user) < 1) {
428 $error++;
429 dol_print_error($this->db);
430 }
431 }
432 }
433
434 // Commit or rollback
435 if ($error) {
436 foreach ($this->errors as $errmsg) {
437 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
438 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
439 }
440 $this->db->rollback();
441 return -1 * $error;
442 } else {
443 $this->db->commit();
444 return 1;
445 }
446 }
447
454 public function getLibStatut($mode = 0)
455 {
456 return $this->LibStatut($this->statut, $mode);
457 }
458
459 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
467 public function LibStatut($status, $mode = 0)
468 {
469 // phpcs:enable
470 return '';
471 }
472
486 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
487 {
488 global $conf;
489
490 $error = 0;
491 $this->db->begin();
492
493 if (isModEnabled("banque")) {
494 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
495
496 $acc = new Account($this->db);
497 $acc->fetch($accountid);
498
499 $total = $this->amount_capital;
500 if ($mode == 'payment_loan') {
501 $total = -$total;
502 }
503
504 // Insert payment into llx_bank
505 $bank_line_id = $acc->addline(
506 $this->datep,
507 $this->paymenttype, // Payment mode ID or code ("CHQ or VIR for example")
508 $label,
509 $total,
510 $this->num_payment,
511 '',
512 $user,
513 $emetteur_nom,
514 $emetteur_banque
515 );
516
517 // Update fk_bank into llx_paiement.
518 // We know the payment who generated the account write
519 if ($bank_line_id > 0) {
520 $result = $this->update_fk_bank($bank_line_id);
521 if ($result <= 0) {
522 $error++;
523 dol_print_error($this->db);
524 }
525
526 // Add link 'payment_loan' in bank_url between payment and bank transaction
527 $url = '';
528 if ($mode == 'payment_loan') {
529 $url = DOL_URL_ROOT.'/loan/payment/card.php?id=';
530 }
531 if ($url) {
532 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(payment)', $mode);
533 if ($result <= 0) {
534 $error++;
535 dol_print_error($this->db);
536 }
537 }
538
539
540 // Add link 'loan' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
541 if ($mode == 'payment_loan') {
542 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.'/loan/card.php?id=', ($this->label ? $this->label : ''), 'loan');
543 if ($result <= 0) {
544 dol_print_error($this->db);
545 }
546 }
547 } else {
548 $this->error = $acc->error;
549 $error++;
550 }
551 }
552
553
554 // Set loan payment started if no set
555 if (!$error) {
556 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
557 $loan = new Loan($this->db);
558 $loan->fetch($fk_loan);
559 if ($loan->paid == $loan::STATUS_UNPAID) {
560 dol_syslog(get_class($this)."::addPaymentToBank : set loan payment to started", LOG_DEBUG);
561 if ($loan->setStarted($user) < 1) {
562 $error++;
563 dol_print_error($this->db);
564 }
565 }
566 }
567
568 if (!$error) {
569 $this->db->commit();
570 return 1;
571 } else {
572 $this->db->rollback();
573 return -1;
574 }
575 }
576
577
578 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
585 public function update_fk_bank($id_bank)
586 {
587 // phpcs:enable
588 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
589
590 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
591 $result = $this->db->query($sql);
592 if ($result) {
593 $this->fk_bank = ((int) $id_bank);
594 return 1;
595 } else {
596 $this->error = $this->db->error();
597 return 0;
598 }
599 }
600
611 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
612 {
613 global $langs, $conf, $hookmanager;
614
615 if (!empty($conf->dol_no_mouse_hover)) {
616 $notooltip = 1; // Force disable tooltips
617 }
618
619 $result = '';
620 $label = '<u>'.$langs->trans("Loan").'</u>';
621 if (!empty($this->id)) {
622 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
623 }
624 if ($moretitle) {
625 $label .= ' - '.$moretitle;
626 }
627
628 $url = DOL_URL_ROOT.'/loan/payment/card.php?id='.$this->id;
629
630 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
631 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
632 $add_save_lastsearch_values = 1;
633 }
634 if ($add_save_lastsearch_values) {
635 $url .= '&save_lastsearch_values=1';
636 }
637
638 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
639 $linkend = '</a>';
640
641 $result .= $linkstart;
642 if ($withpicto) {
643 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
644 }
645 if ($withpicto != 2) {
646 $result .= $this->ref;
647 }
648 $result .= $linkend;
649
650 global $action;
651 $hookmanager->initHooks(array($this->element . 'dao'));
652 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
653 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
654 if ($reshook > 0) {
655 $result = $hookmanager->resPrint;
656 } else {
657 $result .= $hookmanager->resPrint;
658 }
659 return $result;
660 }
661}
$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.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='auto')
Return date for now.
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 right right takeposterminal SELECT e e e e e statut
Definition invoice.php:1926