dolibarr 19.0.3
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-2023 Frederic France <frederic.france@netlogic.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 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 = null, $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++;
362 $this->errors[] = "Error ".$this->db->lasterror();
363 }
364
365 // Commit or rollback
366 if ($error) {
367 foreach ($this->errors as $errmsg) {
368 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
369 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
370 }
371 $this->db->rollback();
372 return -1 * $error;
373 } else {
374 $this->db->commit();
375 return 1;
376 }
377 }
378
379
387 public function delete($user, $notrigger = 0)
388 {
389 global $conf, $langs;
390 $error = 0;
391
392 $this->db->begin();
393
394 if (!$error) {
395 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
396 $sql .= " WHERE type='payment_loan' AND url_id=".((int) $this->id);
397
398 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
399 $resql = $this->db->query($sql);
400 if (!$resql) {
401 $error++;
402 $this->errors[] = "Error ".$this->db->lasterror();
403 }
404 }
405
406 if (!$error) {
407 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan";
408 $sql .= " WHERE rowid=".((int) $this->id);
409
410 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
411 $resql = $this->db->query($sql);
412 if (!$resql) {
413 $error++;
414 $this->errors[] = "Error ".$this->db->lasterror();
415 }
416 }
417
418 // Set loan unpaid if loan has no other payment
419 if (!$error) {
420 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
421 $loan = new Loan($this->db);
422 $loan->fetch($this->fk_loan);
423 $sum_payment = $loan->getSumPayment();
424 if ($sum_payment == 0) {
425 dol_syslog(get_class($this)."::delete : set loan to unpaid", LOG_DEBUG);
426 if ($loan->setUnpaid($user) < 1) {
427 $error++;
428 dol_print_error($this->db);
429 }
430 }
431 }
432
433 //if (! $error)
434 //{
435 // if (! $notrigger)
436 // {
437 // Uncomment this and change MYOBJECT to your own tag if you
438 // want this action call a trigger.
439
441 //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
442 //$interface=new Interfaces($this->db);
443 //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
444 //if ($result < 0) { $error++; $this->errors=$interface->errors; }
446 // }
447 //}
448
449 // Commit or rollback
450 if ($error) {
451 foreach ($this->errors as $errmsg) {
452 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
453 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
454 }
455 $this->db->rollback();
456 return -1 * $error;
457 } else {
458 $this->db->commit();
459 return 1;
460 }
461 }
462
469 public function getLibStatut($mode = 0)
470 {
471 return $this->LibStatut($this->statut, $mode);
472 }
473
474 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
482 public function LibStatut($status, $mode = 0)
483 {
484 // phpcs:enable
485 return '';
486 }
487
501 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
502 {
503 global $conf;
504
505 $error = 0;
506 $this->db->begin();
507
508 if (isModEnabled("banque")) {
509 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
510
511 $acc = new Account($this->db);
512 $acc->fetch($accountid);
513
514 $total = $this->amount_capital;
515 if ($mode == 'payment_loan') {
516 $total = -$total;
517 }
518
519 // Insert payment into llx_bank
520 $bank_line_id = $acc->addline(
521 $this->datep,
522 $this->paymenttype, // Payment mode ID or code ("CHQ or VIR for example")
523 $label,
524 $total,
525 $this->num_payment,
526 '',
527 $user,
528 $emetteur_nom,
529 $emetteur_banque
530 );
531
532 // Update fk_bank into llx_paiement.
533 // We know the payment who generated the account write
534 if ($bank_line_id > 0) {
535 $result = $this->update_fk_bank($bank_line_id);
536 if ($result <= 0) {
537 $error++;
538 dol_print_error($this->db);
539 }
540
541 // Add link 'payment_loan' in bank_url between payment and bank transaction
542 $url = '';
543 if ($mode == 'payment_loan') {
544 $url = DOL_URL_ROOT.'/loan/payment/card.php?id=';
545 }
546 if ($url) {
547 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(payment)', $mode);
548 if ($result <= 0) {
549 $error++;
550 dol_print_error($this->db);
551 }
552 }
553
554
555 // Add link 'loan' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
556 if ($mode == 'payment_loan') {
557 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.'/loan/card.php?id=', ($this->label ? $this->label : ''), 'loan');
558 if ($result <= 0) {
559 dol_print_error($this->db);
560 }
561 }
562 } else {
563 $this->error = $acc->error;
564 $error++;
565 }
566 }
567
568
569 // Set loan payment started if no set
570 if (!$error) {
571 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
572 $loan = new Loan($this->db);
573 $loan->fetch($fk_loan);
574 if ($loan->paid == $loan::STATUS_UNPAID) {
575 dol_syslog(get_class($this)."::addPaymentToBank : set loan payment to started", LOG_DEBUG);
576 if ($loan->setStarted($user) < 1) {
577 $error++;
578 dol_print_error($this->db);
579 }
580 }
581 }
582
583 if (!$error) {
584 $this->db->commit();
585 return 1;
586 } else {
587 $this->db->rollback();
588 return -1;
589 }
590 }
591
592
593 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
600 public function update_fk_bank($id_bank)
601 {
602 // phpcs:enable
603 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
604
605 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
606 $result = $this->db->query($sql);
607 if ($result) {
608 $this->fk_bank = ((int) $id_bank);
609 return 1;
610 } else {
611 $this->error = $this->db->error();
612 return 0;
613 }
614 }
615
626 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
627 {
628 global $langs, $conf, $hookmanager;
629
630 if (!empty($conf->dol_no_mouse_hover)) {
631 $notooltip = 1; // Force disable tooltips
632 }
633
634 $result = '';
635 $label = '<u>'.$langs->trans("Loan").'</u>';
636 if (!empty($this->id)) {
637 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
638 }
639 if ($moretitle) {
640 $label .= ' - '.$moretitle;
641 }
642
643 $url = DOL_URL_ROOT.'/loan/payment/card.php?id='.$this->id;
644
645 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
646 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
647 $add_save_lastsearch_values = 1;
648 }
649 if ($add_save_lastsearch_values) {
650 $url .= '&save_lastsearch_values=1';
651 }
652
653 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
654 $linkend = '</a>';
655
656 $result .= $linkstart;
657 if ($withpicto) {
658 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
659 }
660 if ($withpicto != 2) {
661 $result .= $this->ref;
662 }
663 $result .= $linkend;
664
665 global $action;
666 $hookmanager->initHooks(array($this->element . 'dao'));
667 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
668 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
669 if ($reshook > 0) {
670 $result = $hookmanager->resPrint;
671 } else {
672 $result .= $hookmanager->resPrint;
673 }
674 return $result;
675 }
676}
$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 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