dolibarr 18.0.6
paymentloan.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014-2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
3 * Copyright (C) 2015-2018 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
105 public $bank_line;
106
107
113 public function __construct($db)
114 {
115 $this->db = $db;
116 }
117
125 public function create($user)
126 {
127 global $conf, $langs;
128
129 $error = 0;
130
131 $now = dol_now();
132
133 // Validate parameters
134 if (!$this->datep) {
135 $this->error = 'ErrorBadValueForParameter';
136 return -1;
137 }
138
139 // Clean parameters
140 if (isset($this->fk_loan)) {
141 $this->fk_loan = (int) $this->fk_loan;
142 }
143 if (isset($this->amount_capital)) {
144 $this->amount_capital = price2num($this->amount_capital ? $this->amount_capital : 0);
145 }
146 if (isset($this->amount_insurance)) {
147 $this->amount_insurance = price2num($this->amount_insurance ? $this->amount_insurance : 0);
148 }
149 if (isset($this->amount_interest)) {
150 $this->amount_interest = price2num($this->amount_interest ? $this->amount_interest : 0);
151 }
152 if (isset($this->fk_typepayment)) {
153 $this->fk_typepayment = (int) $this->fk_typepayment;
154 }
155 if (isset($this->num_payment)) {
156 $this->num_payment = (int) $this->num_payment;
157 }
158 if (isset($this->note_private)) {
159 $this->note_private = trim($this->note_private);
160 }
161 if (isset($this->note_public)) {
162 $this->note_public = trim($this->note_public);
163 }
164 if (isset($this->fk_bank)) {
165 $this->fk_bank = (int) $this->fk_bank;
166 }
167 if (isset($this->fk_user_creat)) {
168 $this->fk_user_creat = (int) $this->fk_user_creat;
169 }
170 if (isset($this->fk_user_modif)) {
171 $this->fk_user_modif = (int) $this->fk_user_modif;
172 }
173
174 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
175 $totalamount = price2num($totalamount);
176
177 // Check parameters
178 if ($totalamount == 0) {
179 return -1; // Negative amounts are accepted for reject prelevement but not null
180 }
181
182
183 $this->db->begin();
184
185 if ($totalamount != 0) {
186 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
187 $sql .= " fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
188 $sql .= " VALUES (".$this->chid.", '".$this->db->idate($now)."',";
189 $sql .= " '".$this->db->idate($this->datep)."',";
190 $sql .= " ".price2num($this->amount_capital).",";
191 $sql .= " ".price2num($this->amount_insurance).",";
192 $sql .= " ".price2num($this->amount_interest).",";
193 $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_private)."', '".$this->db->escape($this->note_public)."', ".$user->id.",";
194 $sql .= " 0)";
195
196 dol_syslog(get_class($this)."::create", LOG_DEBUG);
197 $resql = $this->db->query($sql);
198 if ($resql) {
199 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_loan");
200 } else {
201 $this->error = $this->db->lasterror();
202 $error++;
203 }
204 }
205
206 if ($totalamount != 0 && !$error) {
207 $this->amount_capital = $totalamount;
208 $this->db->commit();
209 return $this->id;
210 } else {
211 $this->error = $this->db->lasterror();
212 $this->db->rollback();
213 return -1;
214 }
215 }
216
223 public function fetch($id)
224 {
225 global $langs;
226 $sql = "SELECT";
227 $sql .= " t.rowid,";
228 $sql .= " t.fk_loan,";
229 $sql .= " t.datec,";
230 $sql .= " t.tms,";
231 $sql .= " t.datep,";
232 $sql .= " t.amount_capital,";
233 $sql .= " t.amount_insurance,";
234 $sql .= " t.amount_interest,";
235 $sql .= " t.fk_typepayment,";
236 $sql .= " t.num_payment,";
237 $sql .= " t.note_private,";
238 $sql .= " t.note_public,";
239 $sql .= " t.fk_bank,";
240 $sql .= " t.fk_user_creat,";
241 $sql .= " t.fk_user_modif,";
242 $sql .= " pt.code as type_code, pt.libelle as type_label,";
243 $sql .= ' b.fk_account';
244 $sql .= " FROM ".MAIN_DB_PREFIX."payment_loan as t";
245 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
246 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
247 $sql .= " WHERE t.rowid = ".((int) $id);
248
249 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
250 $resql = $this->db->query($sql);
251 if ($resql) {
252 if ($this->db->num_rows($resql)) {
253 $obj = $this->db->fetch_object($resql);
254
255 $this->id = $obj->rowid;
256 $this->ref = $obj->rowid;
257
258 $this->fk_loan = $obj->fk_loan;
259 $this->datec = $this->db->jdate($obj->datec);
260 $this->tms = $this->db->jdate($obj->tms);
261 $this->datep = $this->db->jdate($obj->datep);
262 $this->amount_capital = $obj->amount_capital;
263 $this->amount_insurance = $obj->amount_insurance;
264 $this->amount_interest = $obj->amount_interest;
265 $this->fk_typepayment = $obj->fk_typepayment;
266 $this->num_payment = $obj->num_payment;
267 $this->note_private = $obj->note_private;
268 $this->note_public = $obj->note_public;
269 $this->fk_bank = $obj->fk_bank;
270 $this->fk_user_creat = $obj->fk_user_creat;
271 $this->fk_user_modif = $obj->fk_user_modif;
272
273 $this->type_code = $obj->type_code;
274 $this->type_label = $obj->type_label;
275
276 $this->bank_account = $obj->fk_account;
277 $this->bank_line = $obj->fk_bank;
278 }
279 $this->db->free($resql);
280
281 return 1;
282 } else {
283 $this->error = "Error ".$this->db->lasterror();
284 return -1;
285 }
286 }
287
288
296 public function update($user = 0, $notrigger = 0)
297 {
298 global $conf, $langs;
299 $error = 0;
300
301 // Clean parameters
302 if (isset($this->fk_loan)) {
303 $this->fk_loan = (int) $this->fk_loan;
304 }
305 if (isset($this->amount_capital)) {
306 $this->amount_capital = trim($this->amount_capital);
307 }
308 if (isset($this->amount_insurance)) {
309 $this->amount_insurance = trim($this->amount_insurance);
310 }
311 if (isset($this->amount_interest)) {
312 $this->amount_interest = trim($this->amount_interest);
313 }
314 if (isset($this->fk_typepayment)) {
315 $this->fk_typepayment = (int) $this->fk_typepayment;
316 }
317 if (isset($this->num_payment)) {
318 $this->num_payment = (int) $this->num_payment;
319 }
320 if (isset($this->note_private)) {
321 $this->note = trim($this->note_private);
322 }
323 if (isset($this->note_public)) {
324 $this->note = trim($this->note_public);
325 }
326 if (isset($this->fk_bank)) {
327 $this->fk_bank = (int) $this->fk_bank;
328 }
329 if (isset($this->fk_user_creat)) {
330 $this->fk_user_creat = (int) $this->fk_user_creat;
331 }
332 if (isset($this->fk_user_modif)) {
333 $this->fk_user_modif = (int) $this->fk_user_modif;
334 }
335
336 // Check parameters
337
338 // Update request
339 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET";
340 $sql .= " fk_loan=".(isset($this->fk_loan) ? $this->fk_loan : "null").",";
341 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
342 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
343 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
344 $sql .= " amount_capital=".(isset($this->amount_capital) ? $this->amount_capital : "null").",";
345 $sql .= " amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance : "null").",";
346 $sql .= " amount_interest=".(isset($this->amount_interest) ? $this->amount_interest : "null").",";
347 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
348 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
349 $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
350 $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
351 $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
352 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
353 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ?((int) $this->fk_user_modif) : "null");
354 $sql .= " WHERE rowid=".((int) $this->id);
355
356 $this->db->begin();
357
358 dol_syslog(get_class($this)."::update", LOG_DEBUG);
359 $resql = $this->db->query($sql);
360 if (!$resql) {
361 $error++; $this->errors[] = "Error ".$this->db->lasterror();
362 }
363
364 // Commit or rollback
365 if ($error) {
366 foreach ($this->errors as $errmsg) {
367 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
368 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
369 }
370 $this->db->rollback();
371 return -1 * $error;
372 } else {
373 $this->db->commit();
374 return 1;
375 }
376 }
377
378
386 public function delete($user, $notrigger = 0)
387 {
388 $error = 0;
389
390 $this->db->begin();
391
392 if ($this->bank_line > 0) {
393 $accline = new AccountLine($this->db);
394 $accline->fetch($this->bank_line);
395 $result = $accline->delete($user);
396 if ($result < 0) {
397 $this->errors[] = $accline->error;
398 $error++;
399 }
400 }
401
402 if (!$error) {
403 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan";
404 $sql .= " WHERE rowid=".((int) $this->id);
405
406 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
407 $resql = $this->db->query($sql);
408 if (!$resql) {
409 $error++; $this->errors[] = "Error ".$this->db->lasterror();
410 }
411 }
412
413 // Set loan unpaid if loan has no other payment
414 if (!$error) {
415 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
416 $loan = new Loan($this->db);
417 $loan->fetch($this->fk_loan);
418 $sum_payment = $loan->getSumPayment();
419 if ($sum_payment == 0) {
420 dol_syslog(get_class($this)."::delete : set loan to unpaid", LOG_DEBUG);
421 if ($loan->setUnpaid($user) < 1) {
422 $error++;
423 dol_print_error($this->db);
424 }
425 }
426 }
427
428 // Commit or rollback
429 if ($error) {
430 foreach ($this->errors as $errmsg) {
431 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
432 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
433 }
434 $this->db->rollback();
435 return -1 * $error;
436 } else {
437 $this->db->commit();
438 return 1;
439 }
440 }
441
448 public function getLibStatut($mode = 0)
449 {
450 return $this->LibStatut($this->statut, $mode);
451 }
452
453 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
461 public function LibStatut($status, $mode = 0)
462 {
463 // phpcs:enable
464 return '';
465 }
466
480 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
481 {
482 global $conf;
483
484 $error = 0;
485 $this->db->begin();
486
487 if (isModEnabled("banque")) {
488 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
489
490 $acc = new Account($this->db);
491 $acc->fetch($accountid);
492
493 $total = $this->amount_capital;
494 if ($mode == 'payment_loan') {
495 $total = -$total;
496 }
497
498 // Insert payment into llx_bank
499 $bank_line_id = $acc->addline(
500 $this->datep,
501 $this->paymenttype, // Payment mode ID or code ("CHQ or VIR for example")
502 $label,
503 $total,
504 $this->num_payment,
505 '',
506 $user,
507 $emetteur_nom,
508 $emetteur_banque
509 );
510
511 // Update fk_bank into llx_paiement.
512 // We know the payment who generated the account write
513 if ($bank_line_id > 0) {
514 $result = $this->update_fk_bank($bank_line_id);
515 if ($result <= 0) {
516 $error++;
517 dol_print_error($this->db);
518 }
519
520 // Add link 'payment_loan' in bank_url between payment and bank transaction
521 $url = '';
522 if ($mode == 'payment_loan') {
523 $url = DOL_URL_ROOT.'/loan/payment/card.php?id=';
524 }
525 if ($url) {
526 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(payment)', $mode);
527 if ($result <= 0) {
528 $error++;
529 dol_print_error($this->db);
530 }
531 }
532
533
534 // Add link 'loan' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
535 if ($mode == 'payment_loan') {
536 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.'/loan/card.php?id=', ($this->label ? $this->label : ''), 'loan');
537 if ($result <= 0) {
538 dol_print_error($this->db);
539 }
540 }
541 } else {
542 $this->error = $acc->error;
543 $error++;
544 }
545 }
546
547
548 // Set loan payment started if no set
549 if (!$error) {
550 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
551 $loan = new Loan($this->db);
552 $loan->fetch($fk_loan);
553 if ($loan->paid == $loan::STATUS_UNPAID) {
554 dol_syslog(get_class($this)."::addPaymentToBank : set loan payment to started", LOG_DEBUG);
555 if ($loan->setStarted($user) < 1) {
556 $error++;
557 dol_print_error($this->db);
558 }
559 }
560 }
561
562 if (!$error) {
563 $this->db->commit();
564 return 1;
565 } else {
566 $this->db->rollback();
567 return -1;
568 }
569 }
570
571
572 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
579 public function update_fk_bank($id_bank)
580 {
581 // phpcs:enable
582 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
583
584 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
585 $result = $this->db->query($sql);
586 if ($result) {
587 $this->fk_bank = ((int) $id_bank);
588 return 1;
589 } else {
590 $this->error = $this->db->error();
591 return 0;
592 }
593 }
594
605 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
606 {
607 global $langs, $conf, $hookmanager;
608
609 if (!empty($conf->dol_no_mouse_hover)) {
610 $notooltip = 1; // Force disable tooltips
611 }
612
613 $result = '';
614 $label = '<u>'.$langs->trans("Loan").'</u>';
615 if (!empty($this->id)) {
616 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
617 }
618 if ($moretitle) {
619 $label .= ' - '.$moretitle;
620 }
621
622 $url = DOL_URL_ROOT.'/loan/payment/card.php?id='.$this->id;
623
624 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
625 if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
626 $add_save_lastsearch_values = 1;
627 }
628 if ($add_save_lastsearch_values) {
629 $url .= '&save_lastsearch_values=1';
630 }
631
632 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
633 $linkend = '</a>';
634
635 $result .= $linkstart;
636 if ($withpicto) {
637 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
638 }
639 if ($withpicto != 2) {
640 $result .= $this->ref;
641 }
642 $result .= $linkend;
643
644 global $action;
645 $hookmanager->initHooks(array($this->element . 'dao'));
646 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
647 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
648 if ($reshook > 0) {
649 $result = $hookmanager->resPrint;
650 } else {
651 $result .= $hookmanager->resPrint;
652 }
653 return $result;
654 }
655}
$object ref
Definition info.php:78
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.
update($user=0, $notrigger=0)
Update database.
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.
print $langs trans("Ref").' m m m statut
Definition index.php:152
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...