dolibarr  17.0.4
loan.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-2018 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 
24 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
25 
26 
30 class Loan extends CommonObject
31 {
35  public $element = 'loan';
36 
37  public $table = 'loan';
38 
42  public $table_element = 'loan';
43 
47  public $picto = 'money-bill-alt';
48 
52  public $rowid;
53 
54  public $datestart;
55  public $dateend;
56 
60  public $label;
61 
62  public $capital;
63  public $nbterm;
64  public $rate;
65  public $paid;
66  public $account_capital;
67  public $account_insurance;
68  public $account_interest;
69 
73  public $date_creation;
74 
78  public $date_modification;
79 
83  public $date_validation;
84 
85  public $insurance_amount;
86 
90  public $fk_bank;
91 
95  public $fk_user_creat;
96 
100  public $fk_user_modif;
101 
105  public $fk_project;
106 
110  public $totalpaid;
111 
112  const STATUS_UNPAID = 0;
113  const STATUS_PAID = 1;
114  const STATUS_STARTED = 2;
115 
116 
122  public function __construct($db)
123  {
124  $this->db = $db;
125  }
126 
133  public function fetch($id)
134  {
135  $sql = "SELECT l.rowid, l.label, l.capital, l.datestart, l.dateend, l.nbterm, l.rate, l.note_private, l.note_public, l.insurance_amount,";
136  $sql .= " l.paid, l.fk_bank, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
137  $sql .= " FROM ".MAIN_DB_PREFIX."loan as l";
138  $sql .= " WHERE l.rowid = ".((int) $id);
139 
140  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
141  $resql = $this->db->query($sql);
142  if ($resql) {
143  if ($this->db->num_rows($resql)) {
144  $obj = $this->db->fetch_object($resql);
145 
146  $this->id = $obj->rowid;
147  $this->ref = $obj->rowid;
148  $this->datestart = $this->db->jdate($obj->datestart);
149  $this->dateend = $this->db->jdate($obj->dateend);
150  $this->label = $obj->label;
151  $this->capital = $obj->capital;
152  $this->nbterm = $obj->nbterm;
153  $this->rate = $obj->rate;
154  $this->note_private = $obj->note_private;
155  $this->note_public = $obj->note_public;
156  $this->insurance_amount = $obj->insurance_amount;
157  $this->paid = $obj->paid;
158  $this->fk_bank = $obj->fk_bank;
159 
160  $this->account_capital = $obj->accountancy_account_capital;
161  $this->account_insurance = $obj->accountancy_account_insurance;
162  $this->account_interest = $obj->accountancy_account_interest;
163  $this->fk_project = $obj->fk_project;
164 
165  $this->db->free($resql);
166  return 1;
167  } else {
168  $this->db->free($resql);
169  return 0;
170  }
171  } else {
172  $this->error = $this->db->lasterror();
173  return -1;
174  }
175  }
176 
177 
184  public function create($user)
185  {
186  global $conf, $langs;
187 
188  $error = 0;
189 
190  $now = dol_now();
191 
192  // clean parameters
193  $newcapital = price2num($this->capital, 'MT');
194  if (empty($this->insurance_amount)) {
195  $this->insurance_amount = 0;
196  }
197  $newinsuranceamount = price2num($this->insurance_amount, 'MT');
198  if (isset($this->note_private)) {
199  $this->note_private = trim($this->note_private);
200  }
201  if (isset($this->note_public)) {
202  $this->note_public = trim($this->note_public);
203  }
204  if (isset($this->account_capital)) {
205  $this->account_capital = trim($this->account_capital);
206  }
207  if (isset($this->account_insurance)) {
208  $this->account_insurance = trim($this->account_insurance);
209  }
210  if (isset($this->account_interest)) {
211  $this->account_interest = trim($this->account_interest);
212  }
213  if (isset($this->fk_bank)) {
214  $this->fk_bank = (int) $this->fk_bank;
215  }
216  if (isset($this->fk_user_creat)) {
217  $this->fk_user_creat = (int) $this->fk_user_creat;
218  }
219  if (isset($this->fk_user_modif)) {
220  $this->fk_user_modif = (int) $this->fk_user_modif;
221  }
222  if (isset($this->fk_project)) {
223  $this->fk_project = (int) $this->fk_project;
224  }
225 
226  // Check parameters
227  if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
228  $this->error = "ErrorBadParameter";
229  return -2;
230  }
231  if (isModEnabled('accounting') && empty($this->account_capital)) {
232  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyCapitalCode"));
233  return -2;
234  }
235  if (isModEnabled('accounting') && empty($this->account_insurance)) {
236  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInsuranceCode"));
237  return -2;
238  }
239  if (isModEnabled('accounting') && empty($this->account_interest)) {
240  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInterestCode"));
241  return -2;
242  }
243 
244  $this->db->begin();
245 
246  $sql = "INSERT INTO ".MAIN_DB_PREFIX."loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
247  $sql .= " accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
248  $sql .= " datec, fk_projet, fk_user_author, insurance_amount)";
249  $sql .= " VALUES ('".$this->db->escape($this->label)."',";
250  $sql .= " '".$this->db->escape($this->fk_bank)."',";
251  $sql .= " '".price2num($newcapital)."',";
252  $sql .= " '".$this->db->idate($this->datestart)."',";
253  $sql .= " '".$this->db->idate($this->dateend)."',";
254  $sql .= " '".$this->db->escape($this->nbterm)."',";
255  $sql .= " '".$this->db->escape($this->rate)."',";
256  $sql .= " '".$this->db->escape($this->note_private)."',";
257  $sql .= " '".$this->db->escape($this->note_public)."',";
258  $sql .= " '".$this->db->escape($this->account_capital)."',";
259  $sql .= " '".$this->db->escape($this->account_insurance)."',";
260  $sql .= " '".$this->db->escape($this->account_interest)."',";
261  $sql .= " ".$conf->entity.",";
262  $sql .= " '".$this->db->idate($now)."',";
263  $sql .= " ".(empty($this->fk_project) ? 'NULL' : $this->fk_project).",";
264  $sql .= " ".$user->id.",";
265  $sql .= " '".price2num($newinsuranceamount)."'";
266  $sql .= ")";
267 
268  dol_syslog(get_class($this)."::create", LOG_DEBUG);
269  $resql = $this->db->query($sql);
270  if ($resql) {
271  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."loan");
272 
273  //dol_syslog("Loans::create this->id=".$this->id);
274  $this->db->commit();
275  return $this->id;
276  } else {
277  $this->error = $this->db->error();
278  $this->db->rollback();
279  return -1;
280  }
281  }
282 
283 
290  public function delete($user)
291  {
292  $error = 0;
293 
294  $this->db->begin();
295 
296  // Get bank transaction lines for this loan
297  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
298  $account = new Account($this->db);
299  $lines_url = $account->get_url('', $this->id, 'loan');
300 
301  // Delete bank urls
302  foreach ($lines_url as $line_url) {
303  if (!$error) {
304  $accountline = new AccountLine($this->db);
305  $accountline->fetch($line_url['fk_bank']);
306  $result = $accountline->delete_urls($user);
307  if ($result < 0) {
308  $error++;
309  }
310  }
311  }
312 
313  // Delete payments
314  if (!$error) {
315  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan where fk_loan=".((int) $this->id);
316  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
317  $resql = $this->db->query($sql);
318  if (!$resql) {
319  $error++;
320  $this->error = $this->db->lasterror();
321  }
322  }
323 
324  if (!$error) {
325  $sql = "DELETE FROM ".MAIN_DB_PREFIX."loan where rowid=".((int) $this->id);
326  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
327  $resql = $this->db->query($sql);
328  if (!$resql) {
329  $error++;
330  $this->error = $this->db->lasterror();
331  }
332  }
333 
334  if (!$error) {
335  $this->db->commit();
336  return 1;
337  } else {
338  $this->db->rollback();
339  return -1;
340  }
341  }
342 
343 
350  public function update($user)
351  {
352  $this->db->begin();
353 
354  if (!is_numeric($this->nbterm)) {
355  $this->error = 'BadValueForParameterForNbTerm';
356  return -1;
357  }
358 
359  $sql = "UPDATE ".MAIN_DB_PREFIX."loan";
360  $sql .= " SET label='".$this->db->escape($this->label)."',";
361  $sql .= " capital='".price2num($this->db->escape($this->capital))."',";
362  $sql .= " datestart='".$this->db->idate($this->datestart)."',";
363  $sql .= " dateend='".$this->db->idate($this->dateend)."',";
364  $sql .= " nbterm=".((float) $this->nbterm).",";
365  $sql .= " rate=".((float) $this->rate).",";
366  $sql .= " accountancy_account_capital = '".$this->db->escape($this->account_capital)."',";
367  $sql .= " accountancy_account_insurance = '".$this->db->escape($this->account_insurance)."',";
368  $sql .= " accountancy_account_interest = '".$this->db->escape($this->account_interest)."',";
369  $sql .= " fk_projet=".(empty($this->fk_project) ? 'NULL' : ((int) $this->fk_project)).",";
370  $sql .= " fk_user_modif = ".$user->id.",";
371  $sql .= " insurance_amount = '".price2num($this->db->escape($this->insurance_amount))."'";
372  $sql .= " WHERE rowid=".((int) $this->id);
373 
374  dol_syslog(get_class($this)."::update", LOG_DEBUG);
375  $resql = $this->db->query($sql);
376  if ($resql) {
377  $this->db->commit();
378  return 1;
379  } else {
380  $this->error = $this->db->error();
381  $this->db->rollback();
382  return -1;
383  }
384  }
385 
386  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
395  public function set_paid($user)
396  {
397  // phpcs:enable
398  dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
399  return $this->setPaid($user);
400  }
401 
408  public function setPaid($user)
409  {
410  $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
411  $sql .= " paid = ".$this::STATUS_PAID;
412  $sql .= " WHERE rowid = ".((int) $this->id);
413  $return = $this->db->query($sql);
414  if ($return) {
415  return 1;
416  } else {
417  $this->error = $this->db->lasterror();
418  return -1;
419  }
420  }
421 
422  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
431  public function set_started($user)
432  {
433  // phpcs:enable
434  dol_syslog(get_class($this)."::set_started is deprecated, use setStarted instead", LOG_NOTICE);
435  return $this->setStarted($user);
436  }
437 
444  public function setStarted($user)
445  {
446  $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
447  $sql .= " paid = ".$this::STATUS_STARTED;
448  $sql .= " WHERE rowid = ".((int) $this->id);
449  $return = $this->db->query($sql);
450  if ($return) {
451  return 1;
452  } else {
453  $this->error = $this->db->lasterror();
454  return -1;
455  }
456  }
457 
458  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
466  public function set_unpaid($user)
467  {
468  // phpcs:enable
469  dol_syslog(get_class($this)."::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
470  return $this->setUnpaid($user);
471  }
472 
479  public function setUnpaid($user)
480  {
481  $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
482  $sql .= " paid = ".$this::STATUS_UNPAID;
483  $sql .= " WHERE rowid = ".((int) $this->id);
484  $return = $this->db->query($sql);
485  if ($return) {
486  return 1;
487  } else {
488  $this->error = $this->db->lasterror();
489  return -1;
490  }
491  }
492 
500  public function getLibStatut($mode = 0, $alreadypaid = -1)
501  {
502  return $this->LibStatut($this->paid, $mode, $alreadypaid);
503  }
504 
505  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
514  public function LibStatut($status, $mode = 0, $alreadypaid = -1)
515  {
516  // phpcs:enable
517  global $langs;
518 
519  // Load translation files required by the page
520  $langs->loadLangs(array("customers", "bills"));
521 
522  unset($this->labelStatus); // Force to reset the array of status label, because label can change depending on parameters
523  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
524  global $langs;
525  $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('Unpaid');
526  $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('Paid');
527  $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv("BillStatusStarted");
528  if ($status == 0 && $alreadypaid > 0) {
529  $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
530  }
531  $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('Unpaid');
532  $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('Enabled');
533  $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv("BillStatusStarted");
534  if ($status == 0 && $alreadypaid > 0) {
535  $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
536  }
537  }
538 
539  $statusType = 'status1';
540  if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
541  $statusType = 'status3';
542  }
543  if ($status == 1) {
544  $statusType = 'status6';
545  }
546 
547  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
548  }
549 
550 
562  public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
563  {
564  global $conf, $langs, $hookmanager;
565 
566  $result = '';
567 
568  $label = '<u>'.$langs->trans("ShowLoan").'</u>';
569  if (!empty($this->ref)) {
570  $label .= '<br><strong>'.$langs->trans('Ref').':</strong> '.$this->ref;
571  }
572  if (!empty($this->label)) {
573  $label .= '<br><strong>'.$langs->trans('Label').':</strong> '.$this->label;
574  }
575 
576  $url = DOL_URL_ROOT.'/loan/card.php?id='.$this->id;
577 
578  if ($option != 'nolink') {
579  // Add param to save lastsearch_values or not
580  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
581  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
582  $add_save_lastsearch_values = 1;
583  }
584  if ($add_save_lastsearch_values) {
585  $url .= '&save_lastsearch_values=1';
586  }
587  }
588 
589  $linkclose = '';
590  if (empty($notooltip)) {
591  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
592  $label = $langs->trans("ShowMyObject");
593  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
594  }
595  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
596  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
597  } else {
598  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
599  }
600 
601  $linkstart = '<a href="'.$url.'"';
602  $linkstart .= $linkclose.'>';
603  $linkend = '</a>';
604 
605  $result .= $linkstart;
606  if ($withpicto) {
607  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
608  }
609  if ($withpicto != 2) {
610  $result .= ($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref);
611  }
612  $result .= $linkend;
613 
614  global $action;
615  $hookmanager->initHooks(array($this->element . 'dao'));
616  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
617  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
618  if ($reshook > 0) {
619  $result = $hookmanager->resPrint;
620  } else {
621  $result .= $hookmanager->resPrint;
622  }
623  return $result;
624  }
625 
633  public function initAsSpecimen()
634  {
635  global $user, $langs, $conf;
636 
637  $now = dol_now();
638 
639  // Initialise parameters
640  $this->id = 0;
641  $this->fk_bank = 1;
642  $this->label = 'SPECIMEN';
643  $this->specimen = 1;
644  $this->socid = 1;
645  $this->account_capital = 16;
646  $this->account_insurance = 616;
647  $this->account_interest = 518;
648  $this->datestart = $now;
649  $this->dateend = $now + (3600 * 24 * 365);
650  $this->note_public = 'SPECIMEN';
651  $this->capital = 20000;
652  $this->nbterm = 48;
653  $this->rate = 4.3;
654  }
655 
661  public function getSumPayment()
662  {
663  $table = 'payment_loan';
664  $field = 'fk_loan';
665 
666  $sql = 'SELECT sum(amount_capital) as amount';
667  $sql .= ' FROM '.MAIN_DB_PREFIX.$table;
668  $sql .= " WHERE ".$field." = ".((int) $this->id);
669 
670  dol_syslog(get_class($this)."::getSumPayment", LOG_DEBUG);
671  $resql = $this->db->query($sql);
672  if ($resql) {
673  $amount = 0;
674 
675  $obj = $this->db->fetch_object($resql);
676  if ($obj) {
677  $amount = $obj->amount ? $obj->amount : 0;
678  }
679 
680  $this->db->free($resql);
681  return $amount;
682  } else {
683  $this->error = $this->db->lasterror();
684  return -1;
685  }
686  }
687 
694  public function info($id)
695  {
696  $sql = 'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
697  $sql .= ' l.tms as datem';
698  $sql .= ' WHERE l.rowid = '.((int) $id);
699 
700  dol_syslog(get_class($this).'::info', LOG_DEBUG);
701  $result = $this->db->query($sql);
702 
703  if ($result) {
704  if ($this->db->num_rows($result)) {
705  $obj = $this->db->fetch_object($result);
706  $this->id = $obj->rowid;
707 
708  $this->user_creation_id = $obj->fk_user_author;
709  $this->user_modification_id = $obj->fk_user_modif;
710  $this->date_creation = $this->db->jdate($obj->datec);
711  $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
712 
713  $this->db->free($result);
714  return 1;
715  } else {
716  $this->db->free($result);
717  return 0;
718  }
719  } else {
720  $this->error = $this->db->lasterror();
721  return -1;
722  }
723  }
724 }
$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.
Definition: loan.class.php:31
fetch($id)
Load object in memory from database.
Definition: loan.class.php:133
setUnpaid($user)
Tag loan as payment as unpaid.
Definition: loan.class.php:479
initAsSpecimen()
Initialise an instance with random values.
Definition: loan.class.php:633
setPaid($user)
Tag loan as paid completely.
Definition: loan.class.php:408
setStarted($user)
Tag loan as payment started.
Definition: loan.class.php:444
set_unpaid($user)
Tag loan as payment as unpaid.
Definition: loan.class.php:466
info($id)
Information on record.
Definition: loan.class.php:694
LibStatut($status, $mode=0, $alreadypaid=-1)
Return label for given status.
Definition: loan.class.php:514
__construct($db)
Constructor.
Definition: loan.class.php:122
set_paid($user)
Tag loan as paid completely.
Definition: loan.class.php:395
getNomUrl($withpicto=0, $maxlen=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return clicable name (with eventually the picto)
Definition: loan.class.php:562
update($user)
Update loan.
Definition: loan.class.php:350
getSumPayment()
Return amount of payments already done.
Definition: loan.class.php:661
create($user)
Create a loan into database.
Definition: loan.class.php:184
set_started($user)
Tag loan as payment started.
Definition: loan.class.php:431
getLibStatut($mode=0, $alreadypaid=-1)
Return label of loan status (unpaid, paid)
Definition: loan.class.php:500
if(isModEnabled('facture') &&!empty($user->rights->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') &&!empty($user->rights->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)) $resql
Social contributions to pay.
Definition: index.php:745
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41