dolibarr 21.0.0-alpha
paymentloan.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
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 public $bank_line;
135
136
142 public function __construct($db)
143 {
144 $this->db = $db;
145 }
146
154 public function create($user)
155 {
156 global $conf, $langs;
157
158 $error = 0;
159
160 $now = dol_now();
161
162 // Validate parameters
163 if (!$this->datep) {
164 $this->error = 'ErrorBadValueForParameter';
165 return -1;
166 }
167
168 // Clean parameters
169 if (isset($this->fk_loan)) {
170 $this->fk_loan = (int) $this->fk_loan;
171 }
172 if (isset($this->amount_capital)) {
173 $this->amount_capital = (float) price2num($this->amount_capital ? $this->amount_capital : 0);
174 }
175 if (isset($this->amount_insurance)) {
176 $this->amount_insurance = (float) price2num($this->amount_insurance ? $this->amount_insurance : 0);
177 }
178 if (isset($this->amount_interest)) {
179 $this->amount_interest = (float) price2num($this->amount_interest ? $this->amount_interest : 0);
180 }
181 if (isset($this->fk_typepayment)) {
182 $this->fk_typepayment = (int) $this->fk_typepayment;
183 }
184 if (isset($this->num_payment)) {
185 $this->num_payment = trim($this->num_payment);
186 }
187 if (isset($this->note_private)) {
188 $this->note_private = trim($this->note_private);
189 }
190 if (isset($this->note_public)) {
191 $this->note_public = trim($this->note_public);
192 }
193 if (isset($this->fk_bank)) {
194 $this->fk_bank = (int) $this->fk_bank;
195 }
196 if (isset($this->fk_user_creat)) {
197 $this->fk_user_creat = (int) $this->fk_user_creat;
198 }
199 if (isset($this->fk_user_modif)) {
200 $this->fk_user_modif = (int) $this->fk_user_modif;
201 }
202
203 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
204 $totalamount = (float) price2num($totalamount);
205
206 // Check parameters
207 if ($totalamount == 0) {
208 return -1; // Negative amounts are accepted for reject prelevement but not null
209 }
210
211
212 $this->db->begin();
213
214 if ($totalamount != 0) {
215 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
216 $sql .= " fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
217 $sql .= " VALUES (".$this->chid.", '".$this->db->idate($now)."',";
218 $sql .= " '".$this->db->idate($this->datep)."',";
219 $sql .= " ".price2num($this->amount_capital).",";
220 $sql .= " ".price2num($this->amount_insurance).",";
221 $sql .= " ".price2num($this->amount_interest).",";
222 $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_private)."', '".$this->db->escape($this->note_public)."', ".$user->id.",";
223 $sql .= " 0)";
224
225 dol_syslog(get_class($this)."::create", LOG_DEBUG);
226 $resql = $this->db->query($sql);
227 if ($resql) {
228 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_loan");
229 } else {
230 $this->error = $this->db->lasterror();
231 $error++;
232 }
233 }
234
235 if ($totalamount != 0 && !$error) {
236 $this->amount_capital = $totalamount;
237 $this->db->commit();
238 return $this->id;
239 } else {
240 $this->error = $this->db->lasterror();
241 $this->db->rollback();
242 return -1;
243 }
244 }
245
252 public function fetch($id)
253 {
254 global $langs;
255 $sql = "SELECT";
256 $sql .= " t.rowid,";
257 $sql .= " t.fk_loan,";
258 $sql .= " t.datec,";
259 $sql .= " t.tms,";
260 $sql .= " t.datep,";
261 $sql .= " t.amount_capital,";
262 $sql .= " t.amount_insurance,";
263 $sql .= " t.amount_interest,";
264 $sql .= " t.fk_typepayment,";
265 $sql .= " t.num_payment,";
266 $sql .= " t.note_private,";
267 $sql .= " t.note_public,";
268 $sql .= " t.fk_bank,";
269 $sql .= " t.fk_user_creat,";
270 $sql .= " t.fk_user_modif,";
271 $sql .= " pt.code as type_code, pt.libelle as type_label,";
272 $sql .= ' b.fk_account';
273 $sql .= " FROM ".MAIN_DB_PREFIX."payment_loan as t";
274 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
275 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
276 $sql .= " WHERE t.rowid = ".((int) $id);
277
278 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
279 $resql = $this->db->query($sql);
280 if ($resql) {
281 if ($this->db->num_rows($resql)) {
282 $obj = $this->db->fetch_object($resql);
283
284 $this->id = $obj->rowid;
285 $this->ref = $obj->rowid;
286
287 $this->fk_loan = $obj->fk_loan;
288 $this->datec = $this->db->jdate($obj->datec);
289 $this->tms = $this->db->jdate($obj->tms);
290 $this->datep = $this->db->jdate($obj->datep);
291 $this->amount_capital = $obj->amount_capital;
292 $this->amount_insurance = $obj->amount_insurance;
293 $this->amount_interest = $obj->amount_interest;
294 $this->fk_typepayment = $obj->fk_typepayment;
295 $this->num_payment = $obj->num_payment;
296 $this->note_private = $obj->note_private;
297 $this->note_public = $obj->note_public;
298 $this->fk_bank = $obj->fk_bank;
299 $this->fk_user_creat = $obj->fk_user_creat;
300 $this->fk_user_modif = $obj->fk_user_modif;
301
302 $this->type_code = $obj->type_code;
303 $this->type_label = $obj->type_label;
304
305 $this->bank_account = $obj->fk_account;
306 $this->bank_line = $obj->fk_bank;
307 }
308 $this->db->free($resql);
309
310 return 1;
311 } else {
312 $this->error = "Error ".$this->db->lasterror();
313 return -1;
314 }
315 }
316
317
325 public function update($user = null, $notrigger = 0)
326 {
327 global $conf, $langs;
328 $error = 0;
329
330 // Clean parameters
331 if (isset($this->fk_loan)) {
332 $this->fk_loan = (int) $this->fk_loan;
333 }
334 if (isset($this->amount_capital)) {
335 $this->amount_capital = (float) $this->amount_capital;
336 }
337 if (isset($this->amount_insurance)) {
338 $this->amount_insurance = (float) $this->amount_insurance;
339 }
340 if (isset($this->amount_interest)) {
341 $this->amount_interest = (float) $this->amount_interest;
342 }
343 if (isset($this->fk_typepayment)) {
344 $this->fk_typepayment = (int) $this->fk_typepayment;
345 }
346 if (isset($this->num_payment)) {
347 $this->num_payment = trim($this->num_payment);
348 }
349 if (isset($this->note_private)) {
350 $this->note = trim($this->note_private);
351 }
352 if (isset($this->note_public)) {
353 $this->note = trim($this->note_public);
354 }
355 if (isset($this->fk_bank)) {
356 $this->fk_bank = (int) $this->fk_bank;
357 }
358 if (isset($this->fk_user_creat)) {
359 $this->fk_user_creat = (int) $this->fk_user_creat;
360 }
361 if (isset($this->fk_user_modif)) {
362 $this->fk_user_modif = (int) $this->fk_user_modif;
363 }
364
365 // Check parameters
366
367 // Update request
368 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET";
369 $sql .= " fk_loan=".(isset($this->fk_loan) ? $this->fk_loan : "null").",";
370 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
371 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
372 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
373 $sql .= " amount_capital=".(isset($this->amount_capital) ? $this->amount_capital : "null").",";
374 $sql .= " amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance : "null").",";
375 $sql .= " amount_interest=".(isset($this->amount_interest) ? $this->amount_interest : "null").",";
376 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
377 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
378 $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
379 $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
380 $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
381 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
382 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
383 $sql .= " WHERE rowid=".((int) $this->id);
384
385 $this->db->begin();
386
387 dol_syslog(get_class($this)."::update", LOG_DEBUG);
388 $resql = $this->db->query($sql);
389 if (!$resql) {
390 $error++;
391 $this->errors[] = "Error ".$this->db->lasterror();
392 }
393
394 // Commit or rollback
395 if ($error) {
396 foreach ($this->errors as $errmsg) {
397 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
398 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
399 }
400 $this->db->rollback();
401 return -1 * $error;
402 } else {
403 $this->db->commit();
404 return 1;
405 }
406 }
407
408
416 public function delete($user, $notrigger = 0)
417 {
418 global $conf, $langs;
419 $error = 0;
420
421 $this->db->begin();
422
423 if (!$error) {
424 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
425 $sql .= " WHERE type='payment_loan' AND url_id=".((int) $this->id);
426
427 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
428 $resql = $this->db->query($sql);
429 if (!$resql) {
430 $error++;
431 $this->errors[] = "Error ".$this->db->lasterror();
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 //if (! $error)
463 //{
464 // if (! $notrigger)
465 // {
466 // Uncomment this and change MYOBJECT to your own tag if you
467 // want this action call a trigger.
468
470 //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
471 //$interface=new Interfaces($this->db);
472 //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
473 //if ($result < 0) { $error++; $this->errors=$interface->errors; }
475 // }
476 //}
477
478 // Commit or rollback
479 if ($error) {
480 foreach ($this->errors as $errmsg) {
481 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
482 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
483 }
484 $this->db->rollback();
485 return -1 * $error;
486 } else {
487 $this->db->commit();
488 return 1;
489 }
490 }
491
498 public function getLibStatut($mode = 0)
499 {
500 return $this->LibStatut($this->statut, $mode);
501 }
502
503 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
511 public function LibStatut($status, $mode = 0)
512 {
513 // phpcs:enable
514 return '';
515 }
516
530 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
531 {
532 global $conf;
533
534 $error = 0;
535 $this->db->begin();
536
537 if (isModEnabled("bank")) {
538 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
539
540 $acc = new Account($this->db);
541 $acc->fetch($accountid);
542
543 $total = $this->amount_capital;
544 if ($mode == 'payment_loan') {
545 $total = -$total;
546 }
547
548 // Insert payment into llx_bank
549 $bank_line_id = $acc->addline(
550 $this->datep,
551 $this->paymenttype, // Payment mode ID or code ("CHQ or VIR for example") it's integer in db
552 $label,
553 $total,
554 $this->num_payment,
555 '',
556 $user,
557 $emetteur_nom,
558 $emetteur_banque
559 );
560
561 // Update fk_bank into llx_paiement.
562 // We know the payment who generated the account write
563 if ($bank_line_id > 0) {
564 $result = $this->update_fk_bank($bank_line_id);
565 if ($result <= 0) {
566 $error++;
567 dol_print_error($this->db);
568 }
569
570 // Add link 'payment_loan' in bank_url between payment and bank transaction
571 $url = '';
572 if ($mode == 'payment_loan') {
573 $url = DOL_URL_ROOT.'/loan/payment/card.php?id=';
574 }
575 if ($url) {
576 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(payment)', $mode);
577 if ($result <= 0) {
578 $error++;
579 dol_print_error($this->db);
580 }
581 }
582
583
584 // Add link 'loan' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
585 if ($mode == 'payment_loan') {
586 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.'/loan/card.php?id=', ($this->label ? $this->label : ''), 'loan');
587 if ($result <= 0) {
588 dol_print_error($this->db);
589 }
590 }
591 } else {
592 $this->error = $acc->error;
593 $error++;
594 }
595 }
596
597
598 // Set loan payment started if no set
599 if (!$error) {
600 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
601 $loan = new Loan($this->db);
602 $loan->fetch($fk_loan);
603 if ($loan->paid == $loan::STATUS_UNPAID) {
604 dol_syslog(get_class($this)."::addPaymentToBank : set loan payment to started", LOG_DEBUG);
605 if ($loan->setStarted($user) < 1) {
606 $error++;
607 dol_print_error($this->db);
608 }
609 }
610 }
611
612 if (!$error) {
613 $this->db->commit();
614 return 1;
615 } else {
616 $this->db->rollback();
617 return -1;
618 }
619 }
620
621
622 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
629 public function update_fk_bank($id_bank)
630 {
631 // phpcs:enable
632 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
633
634 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
635 $result = $this->db->query($sql);
636 if ($result) {
637 $this->fk_bank = ((int) $id_bank);
638 return 1;
639 } else {
640 $this->error = $this->db->error();
641 return 0;
642 }
643 }
644
655 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
656 {
657 global $langs, $conf, $hookmanager;
658
659 if (!empty($conf->dol_no_mouse_hover)) {
660 $notooltip = 1; // Force disable tooltips
661 }
662
663 $result = '';
664 $label = '<u>'.$langs->trans("Loan").'</u>';
665 if (!empty($this->id)) {
666 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
667 }
668 if ($moretitle) {
669 $label .= ' - '.$moretitle;
670 }
671
672 $url = DOL_URL_ROOT.'/loan/payment/card.php?id='.$this->id;
673
674 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
675 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
676 $add_save_lastsearch_values = 1;
677 }
678 if ($add_save_lastsearch_values) {
679 $url .= '&save_lastsearch_values=1';
680 }
681
682 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
683 $linkend = '</a>';
684
685 $result .= $linkstart;
686 if ($withpicto) {
687 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
688 }
689 if ($withpicto != 2) {
690 $result .= $this->ref;
691 }
692 $result .= $linkend;
693
694 global $action;
695 $hookmanager->initHooks(array($this->element . 'dao'));
696 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
697 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
698 if ($reshook > 0) {
699 $result = $hookmanager->resPrint;
700 } else {
701 $result .= $hookmanager->resPrint;
702 }
703 return $result;
704 }
705}
$object ref
Definition info.php:79
Class to manage bank accounts.
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.
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...