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