dolibarr  19.0.0-dev
loanschedule.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 Florian HENRY <florian.henry@atm-consulting.fr>
3  * Copyright (C) 2018-2023 Frédéric France <frederic.france@netlogic.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26 
27 
32 {
36  public $element = 'loan_schedule';
37 
41  public $table_element = 'loan_schedule';
42 
46  public $fk_loan;
47 
51  public $datec;
52  public $tms;
53 
57  public $datep;
58 
59  public $amounts = array(); // Array of amounts
60  public $amount_capital; // Total amount of payment
61  public $amount_insurance;
62  public $amount_interest;
63 
67  public $fk_typepayment;
68 
72  public $num_payment;
73 
77  public $fk_bank;
78 
82  public $fk_payment_loan;
83 
87  public $fk_user_creat;
88 
92  public $fk_user_modif;
93 
98  public $lines = array();
99 
104  public $total;
105 
106  public $type_code;
107  public $type_label;
108 
109 
115  public function __construct($db)
116  {
117  $this->db = $db;
118  }
119 
127  public function create($user)
128  {
129  global $conf, $langs;
130 
131  $error = 0;
132 
133  $now = dol_now();
134 
135  // Validate parameters
136  if (!$this->datep) {
137  $this->error = 'ErrorBadValueForParameter';
138  return -1;
139  }
140 
141  // Clean parameters
142  if (isset($this->fk_loan)) {
143  $this->fk_loan = (int) $this->fk_loan;
144  }
145  if (isset($this->amount_capital)) {
146  $this->amount_capital = trim($this->amount_capital ? $this->amount_capital : 0);
147  }
148  if (isset($this->amount_insurance)) {
149  $this->amount_insurance = trim($this->amount_insurance ? $this->amount_insurance : 0);
150  }
151  if (isset($this->amount_interest)) {
152  $this->amount_interest = trim($this->amount_interest ? $this->amount_interest : 0);
153  }
154  if (isset($this->fk_typepayment)) {
155  $this->fk_typepayment = (int) $this->fk_typepayment;
156  }
157  if (isset($this->fk_bank)) {
158  $this->fk_bank = (int) $this->fk_bank;
159  }
160  if (isset($this->fk_user_creat)) {
161  $this->fk_user_creat = (int) $this->fk_user_creat;
162  }
163  if (isset($this->fk_user_modif)) {
164  $this->fk_user_modif = (int) $this->fk_user_modif;
165  }
166 
167  $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
168  $totalamount = price2num($totalamount);
169 
170  // Check parameters
171  if ($totalamount == 0) {
172  $this->errors[] = 'step1';
173  return -1; // Negative amounts are accepted for reject prelevement but not null
174  }
175 
176 
177  $this->db->begin();
178 
179  if ($totalamount != 0) {
180  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
181  $sql .= " fk_typepayment, fk_user_creat, fk_bank)";
182  $sql .= " VALUES (".$this->fk_loan.", '".$this->db->idate($now)."',";
183  $sql .= " '".$this->db->idate($this->datep)."',";
184  $sql .= " ".price2num($this->amount_capital).",";
185  $sql .= " ".price2num($this->amount_insurance).",";
186  $sql .= " ".price2num($this->amount_interest).",";
187  $sql .= " ".price2num($this->fk_typepayment).", ";
188  $sql .= " ".((int) $user->id).",";
189  $sql .= " ".((int) $this->fk_bank).")";
190 
191  dol_syslog(get_class($this)."::create", LOG_DEBUG);
192  $resql = $this->db->query($sql);
193  if ($resql) {
194  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_loan");
195  } else {
196  $this->error = $this->db->lasterror();
197  $error++;
198  }
199  }
200 
201  if ($totalamount != 0 && !$error) {
202  $this->amount_capital = $totalamount;
203  $this->db->commit();
204  return $this->id;
205  } else {
206  $this->errors[] = $this->db->lasterror();
207  $this->db->rollback();
208  return -1;
209  }
210  }
211 
218  public function fetch($id)
219  {
220  global $langs;
221  $sql = "SELECT";
222  $sql .= " t.rowid,";
223  $sql .= " t.fk_loan,";
224  $sql .= " t.datec,";
225  $sql .= " t.tms,";
226  $sql .= " t.datep,";
227  $sql .= " t.amount_capital,";
228  $sql .= " t.amount_insurance,";
229  $sql .= " t.amount_interest,";
230  $sql .= " t.fk_typepayment,";
231  $sql .= " t.num_payment,";
232  $sql .= " t.note_private,";
233  $sql .= " t.note_public,";
234  $sql .= " t.fk_bank,";
235  $sql .= " t.fk_payment_loan,";
236  $sql .= " t.fk_user_creat,";
237  $sql .= " t.fk_user_modif,";
238  $sql .= " pt.code as type_code, pt.libelle as type_label,";
239  $sql .= ' b.fk_account';
240  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
241  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
242  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
243  $sql .= " WHERE t.rowid = ".((int) $id);
244 
245  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
246  $resql = $this->db->query($sql);
247  if ($resql) {
248  if ($this->db->num_rows($resql)) {
249  $obj = $this->db->fetch_object($resql);
250 
251  $this->id = $obj->rowid;
252  $this->ref = $obj->rowid;
253 
254  $this->fk_loan = $obj->fk_loan;
255  $this->datec = $this->db->jdate($obj->datec);
256  $this->tms = $this->db->jdate($obj->tms);
257  $this->datep = $this->db->jdate($obj->datep);
258  $this->amount_capital = $obj->amount_capital;
259  $this->amount_insurance = $obj->amount_insurance;
260  $this->amount_interest = $obj->amount_interest;
261  $this->fk_typepayment = $obj->fk_typepayment;
262  $this->num_payment = $obj->num_payment;
263  $this->note_private = $obj->note_private;
264  $this->note_public = $obj->note_public;
265  $this->fk_bank = $obj->fk_bank;
266  $this->fk_payment_loan = $obj->fk_payment_loan;
267  $this->fk_user_creat = $obj->fk_user_creat;
268  $this->fk_user_modif = $obj->fk_user_modif;
269 
270  $this->type_code = $obj->type_code;
271  $this->type_label = $obj->type_label;
272 
273  $this->bank_account = $obj->fk_account;
274  $this->bank_line = $obj->fk_bank;
275  }
276  $this->db->free($resql);
277 
278  return 1;
279  } else {
280  $this->error = "Error ".$this->db->lasterror();
281  return -1;
282  }
283  }
284 
285 
293  public function update($user = 0, $notrigger = 0)
294  {
295  global $conf, $langs;
296  $error = 0;
297 
298  // Clean parameters
299  if (isset($this->amount_capital)) {
300  $this->amount_capital = trim($this->amount_capital);
301  }
302  if (isset($this->amount_insurance)) {
303  $this->amount_insurance = trim($this->amount_insurance);
304  }
305  if (isset($this->amount_interest)) {
306  $this->amount_interest = trim($this->amount_interest);
307  }
308  if (isset($this->num_payment)) {
309  $this->num_payment = trim($this->num_payment);
310  }
311  if (isset($this->note_private)) {
312  $this->note_private = trim($this->note_private);
313  }
314  if (isset($this->note_public)) {
315  $this->note_public = trim($this->note_public);
316  }
317  if (isset($this->fk_bank)) {
318  $this->fk_bank = trim($this->fk_bank);
319  }
320  if (isset($this->fk_payment_loan)) {
321  $this->fk_payment_loan = (int) $this->fk_payment_loan;
322  }
323 
324  // Check parameters
325  // Put here code to add control on parameters values
326 
327  // Update request
328  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
329 
330  $sql .= " fk_loan=".(isset($this->fk_loan) ? $this->fk_loan : "null").",";
331  $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
332  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
333  $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
334  $sql .= " amount_capital=".(isset($this->amount_capital) ? $this->amount_capital : "null").",";
335  $sql .= " amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance : "null").",";
336  $sql .= " amount_interest=".(isset($this->amount_interest) ? $this->amount_interest : "null").",";
337  $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
338  $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
339  $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
340  $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
341  $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
342  $sql .= " fk_payment_loan=".(isset($this->fk_payment_loan) ? ((int) $this->fk_payment_loan) : "null").",";
343  $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null").",";
344  $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
345 
346  $sql .= " WHERE rowid=".((int) $this->id);
347 
348  $this->db->begin();
349 
350  dol_syslog(get_class($this)."::update", LOG_DEBUG);
351  $resql = $this->db->query($sql);
352  if (!$resql) {
353  $error++; $this->errors[] = "Error ".$this->db->lasterror();
354  }
355 
356  // Commit or rollback
357  if ($error) {
358  $this->db->rollback();
359  return -1 * $error;
360  } else {
361  $this->db->commit();
362  return 1;
363  }
364  }
365 
366 
374  public function delete($user, $notrigger = 0)
375  {
376  global $conf, $langs;
377  $error = 0;
378 
379  $this->db->begin();
380 
381  if (!$error) {
382  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
383  $sql .= " WHERE rowid=".((int) $this->id);
384 
385  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
386  $resql = $this->db->query($sql);
387  if (!$resql) {
388  $error++; $this->errors[] = "Error ".$this->db->lasterror();
389  }
390  }
391 
392  // Commit or rollback
393  if ($error) {
394  foreach ($this->errors as $errmsg) {
395  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
396  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
397  }
398  $this->db->rollback();
399  return -1 * $error;
400  } else {
401  $this->db->commit();
402  return 1;
403  }
404  }
405 
414  public function calcMonthlyPayments($capital, $rate, $nbterm)
415  {
416  $result = '';
417 
418  if (!empty($capital) && !empty($rate) && !empty($nbterm)) {
419  $result = ($capital * ($rate / 12)) / (1 - pow((1 + ($rate / 12)), ($nbterm * -1)));
420  }
421 
422  return $result;
423  }
424 
425 
432  public function fetchAll($loanid)
433  {
434  global $langs;
435 
436  $sql = "SELECT";
437  $sql .= " t.rowid,";
438  $sql .= " t.fk_loan,";
439  $sql .= " t.datec,";
440  $sql .= " t.tms,";
441  $sql .= " t.datep,";
442  $sql .= " t.amount_capital,";
443  $sql .= " t.amount_insurance,";
444  $sql .= " t.amount_interest,";
445  $sql .= " t.fk_typepayment,";
446  $sql .= " t.num_payment,";
447  $sql .= " t.note_private,";
448  $sql .= " t.note_public,";
449  $sql .= " t.fk_bank,";
450  $sql .= " t.fk_payment_loan,";
451  $sql .= " t.fk_user_creat,";
452  $sql .= " t.fk_user_modif";
453  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
454  $sql .= " WHERE t.fk_loan = ".((int) $loanid);
455 
456  dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
457  $resql = $this->db->query($sql);
458 
459  if ($resql) {
460  while ($obj = $this->db->fetch_object($resql)) {
461  $line = new LoanSchedule($this->db);
462  $line->id = $obj->rowid;
463  $line->ref = $obj->rowid;
464 
465  $line->fk_loan = $obj->fk_loan;
466  $line->datec = $this->db->jdate($obj->datec);
467  $line->tms = $this->db->jdate($obj->tms);
468  $line->datep = $this->db->jdate($obj->datep);
469  $line->amount_capital = $obj->amount_capital;
470  $line->amount_insurance = $obj->amount_insurance;
471  $line->amount_interest = $obj->amount_interest;
472  $line->fk_typepayment = $obj->fk_typepayment;
473  $line->num_payment = $obj->num_payment;
474  $line->note_private = $obj->note_private;
475  $line->note_public = $obj->note_public;
476  $line->fk_bank = $obj->fk_bank;
477  $line->fk_payment_loan = $obj->fk_payment_loan;
478  $line->fk_user_creat = $obj->fk_user_creat;
479  $line->fk_user_modif = $obj->fk_user_modif;
480 
481  $this->lines[] = $line;
482  }
483  $this->db->free($resql);
484  return 1;
485  } else {
486  $this->error = "Error ".$this->db->lasterror();
487  return -1;
488  }
489  }
490 
496  private function transPayment()
497  {
498  require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
499  require_once DOL_DOCUMENT_ROOT.'/core/lib/loan.lib.php';
500  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
501 
502  $toinsert = array();
503 
504  $sql = "SELECT l.rowid";
505  $sql .= " FROM ".MAIN_DB_PREFIX."loan as l";
506  $sql .= " WHERE l.paid = 0";
507  $resql = $this->db->query($sql);
508 
509  if ($resql) {
510  while ($obj = $this->db->fetch_object($resql)) {
511  $lastrecorded = $this->lastPayment($obj->rowid);
512  $toinsert = $this->paimenttorecord($obj->rowid, $lastrecorded);
513  if (count($toinsert) > 0) {
514  foreach ($toinsert as $echid) {
515  $this->db->begin();
516  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_loan ";
517  $sql .= "(fk_loan,datec,tms,datep,amount_capital,amount_insurance,amount_interest,fk_typepayment,num_payment,note_private,note_public,fk_bank,fk_user_creat,fk_user_modif) ";
518  $sql .= "SELECT fk_loan,datec,tms,datep,amount_capital,amount_insurance,amount_interest,fk_typepayment,num_payment,note_private,note_public,fk_bank,fk_user_creat,fk_user_modif";
519  $sql .= " FROM ".MAIN_DB_PREFIX."loan_schedule WHERE rowid =".((int) $echid);
520  $res = $this->db->query($sql);
521  if ($res) {
522  $this->db->commit();
523  } else {
524  $this->db->rollback();
525  }
526  }
527  }
528  }
529  }
530  }
531 
532 
539  private function lastPayment($loanid)
540  {
541  $sql = "SELECT p.datep";
542  $sql .= " FROM ".MAIN_DB_PREFIX."payment_loan as p ";
543  $sql .= " WHERE p.fk_loan = ".((int) $loanid);
544  $sql .= " ORDER BY p.datep DESC ";
545  $sql .= " LIMIT 1 ";
546 
547  $resql = $this->db->query($sql);
548 
549  if ($resql) {
550  $obj = $this->db->fetch_object($resql);
551  return $this->db->jdate($obj->datep);
552  } else {
553  return -1;
554  }
555  }
556 
564  public function paimenttorecord($loanid, $datemax)
565  {
566 
567  $result = array();
568 
569  $sql = "SELECT p.rowid";
570  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as p ";
571  $sql .= " WHERE p.fk_loan = ".((int) $loanid);
572  if (!empty($datemax)) {
573  $sql .= " AND p.datep > '".$this->db->idate($datemax)."'";
574  }
575  $sql .= " AND p.datep <= '".$this->db->idate(dol_now())."'";
576 
577  $resql = $this->db->query($sql);
578 
579  if ($resql) {
580  while ($obj = $this->db->fetch_object($resql)) {
581  $result[] = $obj->rowid;
582  }
583  }
584 
585  return $result;
586  }
587 }
$object ref
Definition: info.php:78
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage Schedule of loans.
__construct($db)
Constructor.
fetchAll($loanid)
Load all object in memory from database.
calcMonthlyPayments($capital, $rate, $nbterm)
Calculate Monthly Payments.
create($user)
Create payment of loan into database.
fetch($id)
Load object in memory from database.
paimenttorecord($loanid, $datemax)
paimenttorecord
lastPayment($loanid)
lastpayment
update($user=0, $notrigger=0)
Update database.
transPayment()
transPayment
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
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_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.