dolibarr  13.0.2
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 
107 
108  const STATUS_UNPAID = 0;
109  const STATUS_PAID = 1;
110  const STATUS_STARTED = 2;
111 
112 
118  public function __construct($db)
119  {
120  $this->db = $db;
121  }
122 
129  public function fetch($id)
130  {
131  $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,";
132  $sql .= " l.paid, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
133  $sql .= " FROM ".MAIN_DB_PREFIX."loan as l";
134  $sql .= " WHERE l.rowid = ".$id;
135 
136  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
137  $resql = $this->db->query($sql);
138  if ($resql)
139  {
140  if ($this->db->num_rows($resql))
141  {
142  $obj = $this->db->fetch_object($resql);
143 
144  $this->id = $obj->rowid;
145  $this->ref = $obj->rowid;
146  $this->datestart = $this->db->jdate($obj->datestart);
147  $this->dateend = $this->db->jdate($obj->dateend);
148  $this->label = $obj->label;
149  $this->capital = $obj->capital;
150  $this->nbterm = $obj->nbterm;
151  $this->rate = $obj->rate;
152  $this->note_private = $obj->note_private;
153  $this->note_public = $obj->note_public;
154  $this->insurance_amount = $obj->insurance_amount;
155  $this->paid = $obj->paid;
156 
157  $this->account_capital = $obj->accountancy_account_capital;
158  $this->account_insurance = $obj->accountancy_account_insurance;
159  $this->account_interest = $obj->accountancy_account_interest;
160  $this->fk_project = $obj->fk_project;
161 
162  $this->db->free($resql);
163  return 1;
164  } else {
165  $this->db->free($resql);
166  return 0;
167  }
168  } else {
169  $this->error = $this->db->lasterror();
170  return -1;
171  }
172  }
173 
174 
181  public function create($user)
182  {
183  global $conf, $langs;
184 
185  $error = 0;
186 
187  $now = dol_now();
188 
189  // clean parameters
190  $newcapital = price2num($this->capital, 'MT');
191  if (empty($this->insurance_amount)) $this->insurance_amount = 0;
192  $newinsuranceamount = price2num($this->insurance_amount, 'MT');
193  if (isset($this->note_private)) $this->note_private = trim($this->note_private);
194  if (isset($this->note_public)) $this->note_public = trim($this->note_public);
195  if (isset($this->account_capital)) $this->account_capital = trim($this->account_capital);
196  if (isset($this->account_insurance)) $this->account_insurance = trim($this->account_insurance);
197  if (isset($this->account_interest)) $this->account_interest = trim($this->account_interest);
198  if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
199  if (isset($this->fk_user_creat)) $this->fk_user_creat = (int) $this->fk_user_creat;
200  if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
201  if (isset($this->fk_project)) $this->fk_project = (int) $this->fk_project;
202 
203  // Check parameters
204  if (!$newcapital > 0 || empty($this->datestart) || empty($this->dateend))
205  {
206  $this->error = "ErrorBadParameter";
207  return -2;
208  }
209  if (($conf->accounting->enabled) && empty($this->account_capital))
210  {
211  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyCapitalCode"));
212  return -2;
213  }
214  if (($conf->accounting->enabled) && empty($this->account_insurance))
215  {
216  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInsuranceCode"));
217  return -2;
218  }
219  if (($conf->accounting->enabled) && empty($this->account_interest))
220  {
221  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInterestCode"));
222  return -2;
223  }
224 
225  $this->db->begin();
226 
227  $sql = "INSERT INTO ".MAIN_DB_PREFIX."loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
228  $sql .= " accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
229  $sql .= " datec, fk_projet, fk_user_author, insurance_amount)";
230  $sql .= " VALUES ('".$this->db->escape($this->label)."',";
231  $sql .= " '".$this->db->escape($this->fk_bank)."',";
232  $sql .= " '".price2num($newcapital)."',";
233  $sql .= " '".$this->db->idate($this->datestart)."',";
234  $sql .= " '".$this->db->idate($this->dateend)."',";
235  $sql .= " '".$this->db->escape($this->nbterm)."',";
236  $sql .= " '".$this->db->escape($this->rate)."',";
237  $sql .= " '".$this->db->escape($this->note_private)."',";
238  $sql .= " '".$this->db->escape($this->note_public)."',";
239  $sql .= " '".$this->db->escape($this->account_capital)."',";
240  $sql .= " '".$this->db->escape($this->account_insurance)."',";
241  $sql .= " '".$this->db->escape($this->account_interest)."',";
242  $sql .= " ".$conf->entity.",";
243  $sql .= " '".$this->db->idate($now)."',";
244  $sql .= " ".(empty($this->fk_project) ? 'NULL' : $this->fk_project).",";
245  $sql .= " ".$user->id.",";
246  $sql .= " '".price2num($newinsuranceamount)."'";
247  $sql .= ")";
248 
249  dol_syslog(get_class($this)."::create", LOG_DEBUG);
250  $resql = $this->db->query($sql);
251  if ($resql)
252  {
253  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."loan");
254 
255  //dol_syslog("Loans::create this->id=".$this->id);
256  $this->db->commit();
257  return $this->id;
258  } else {
259  $this->error = $this->db->error();
260  $this->db->rollback();
261  return -1;
262  }
263  }
264 
265 
272  public function delete($user)
273  {
274  $error = 0;
275 
276  $this->db->begin();
277 
278  // Get bank transaction lines for this loan
279  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
280  $account = new Account($this->db);
281  $lines_url = $account->get_url('', $this->id, 'loan');
282 
283  // Delete bank urls
284  foreach ($lines_url as $line_url)
285  {
286  if (!$error)
287  {
288  $accountline = new AccountLine($this->db);
289  $accountline->fetch($line_url['fk_bank']);
290  $result = $accountline->delete_urls($user);
291  if ($result < 0)
292  {
293  $error++;
294  }
295  }
296  }
297 
298  // Delete payments
299  if (!$error)
300  {
301  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan where fk_loan=".$this->id;
302  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
303  $resql = $this->db->query($sql);
304  if (!$resql)
305  {
306  $error++;
307  $this->error = $this->db->lasterror();
308  }
309  }
310 
311  if (!$error)
312  {
313  $sql = "DELETE FROM ".MAIN_DB_PREFIX."loan where rowid=".$this->id;
314  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
315  $resql = $this->db->query($sql);
316  if (!$resql)
317  {
318  $error++;
319  $this->error = $this->db->lasterror();
320  }
321  }
322 
323  if (!$error)
324  {
325  $this->db->commit();
326  return 1;
327  } else {
328  $this->db->rollback();
329  return -1;
330  }
331  }
332 
333 
340  public function update($user)
341  {
342  $this->db->begin();
343 
344  if (!is_numeric($this->nbterm))
345  {
346  $this->error = 'BadValueForParameterForNbTerm';
347  return -1;
348  }
349 
350  $sql = "UPDATE ".MAIN_DB_PREFIX."loan";
351  $sql .= " SET label='".$this->db->escape($this->label)."',";
352  $sql .= " capital='".price2num($this->db->escape($this->capital))."',";
353  $sql .= " datestart='".$this->db->idate($this->datestart)."',";
354  $sql .= " dateend='".$this->db->idate($this->dateend)."',";
355  $sql .= " nbterm=".$this->nbterm.",";
356  $sql .= " rate=".$this->db->escape($this->rate).",";
357  $sql .= " accountancy_account_capital = '".$this->db->escape($this->account_capital)."',";
358  $sql .= " accountancy_account_insurance = '".$this->db->escape($this->account_insurance)."',";
359  $sql .= " accountancy_account_interest = '".$this->db->escape($this->account_interest)."',";
360  $sql .= " fk_projet=".(empty($this->fk_project) ? 'NULL' : $this->fk_project).",";
361  $sql .= " fk_user_modif = ".$user->id.",";
362  $sql .= " insurance_amount = '".price2num($this->db->escape($this->insurance_amount))."'";
363  $sql .= " WHERE rowid=".$this->id;
364 
365  dol_syslog(get_class($this)."::update", LOG_DEBUG);
366  $resql = $this->db->query($sql);
367  if ($resql)
368  {
369  $this->db->commit();
370  return 1;
371  } else {
372  $this->error = $this->db->error();
373  $this->db->rollback();
374  return -1;
375  }
376  }
377 
378  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
385  public function set_paid($user)
386  {
387  // phpcs:enable
388  $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
389  $sql .= " paid = ".$this::STATUS_PAID;
390  $sql .= " WHERE rowid = ".$this->id;
391  $return = $this->db->query($sql);
392  if ($return) {
393  return 1;
394  } else {
395  $this->error = $this->db->lasterror();
396  return -1;
397  }
398  }
399 
400  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
407  public function set_started($user)
408  {
409  // phpcs:enable
410  $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
411  $sql .= " paid = ".$this::STATUS_STARTED;
412  $sql .= " WHERE rowid = ".$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
429  public function set_unpaid($user)
430  {
431  // phpcs:enable
432  $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
433  $sql .= " paid = ".$this::STATUS_UNPAID;
434  $sql .= " WHERE rowid = ".$this->id;
435  $return = $this->db->query($sql);
436  if ($return) {
437  return 1;
438  } else {
439  $this->error = $this->db->lasterror();
440  return -1;
441  }
442  }
443 
451  public function getLibStatut($mode = 0, $alreadypaid = -1)
452  {
453  return $this->LibStatut($this->paid, $mode, $alreadypaid);
454  }
455 
456  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
465  public function LibStatut($status, $mode = 0, $alreadypaid = -1)
466  {
467  // phpcs:enable
468  global $langs;
469 
470  // Load translation files required by the page
471  $langs->loadLangs(array("customers", "bills"));
472 
473  unset($this->labelStatus); // Force to reset the array of status label, because label can change depending on parameters
474  if (empty($this->labelStatus) || empty($this->labelStatusShort))
475  {
476  global $langs;
477  $this->labelStatus[self::STATUS_UNPAID] = $langs->trans('Unpaid');
478  $this->labelStatus[self::STATUS_PAID] = $langs->trans('Paid');
479  $this->labelStatus[self::STATUS_STARTED] = $langs->trans("BillStatusStarted");
480  if ($status == 0 && $alreadypaid > 0) $this->labelStatus[self::STATUS_UNPAID] = $langs->trans("BillStatusStarted");
481  $this->labelStatusShort[self::STATUS_UNPAID] = $langs->trans('Unpaid');
482  $this->labelStatusShort[self::STATUS_PAID] = $langs->trans('Enabled');
483  $this->labelStatusShort[self::STATUS_STARTED] = $langs->trans("BillStatusStarted");
484  if ($status == 0 && $alreadypaid > 0) $this->labelStatusShort[self::STATUS_UNPAID] = $langs->trans("BillStatusStarted");
485  }
486 
487  $statusType = 'status1';
488  if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) $statusType = 'status3';
489  if ($status == 1) $statusType = 'status6';
490 
491  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
492  }
493 
494 
506  public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
507  {
508  global $conf, $langs;
509 
510  $result = '';
511 
512  $label = '<u>'.$langs->trans("ShowLoan").'</u>';
513  if (!empty($this->ref)) {
514  $label .= '<br><strong>'.$langs->trans('Ref').':</strong> '.$this->ref;
515  }
516  if (!empty($this->label)) {
517  $label .= '<br><strong>'.$langs->trans('Label').':</strong> '.$this->label;
518  }
519 
520  $url = DOL_URL_ROOT.'/loan/card.php?id='.$this->id;
521 
522  if ($option != 'nolink')
523  {
524  // Add param to save lastsearch_values or not
525  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
526  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
527  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
528  }
529 
530  $linkclose = '';
531  if (empty($notooltip))
532  {
533  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
534  {
535  $label = $langs->trans("ShowMyObject");
536  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
537  }
538  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
539  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
540  } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
541 
542  $linkstart = '<a href="'.$url.'"';
543  $linkstart .= $linkclose.'>';
544  $linkend = '</a>';
545 
546  $result .= $linkstart;
547  if ($withpicto) $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);
548  if ($withpicto != 2) $result .= ($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref);
549  $result .= $linkend;
550 
551  return $result;
552  }
553 
561  public function initAsSpecimen()
562  {
563  global $user, $langs, $conf;
564 
565  $now = dol_now();
566 
567  // Initialise parameters
568  $this->id = 0;
569  $this->fk_bank = 1;
570  $this->label = 'SPECIMEN';
571  $this->specimen = 1;
572  $this->socid = 1;
573  $this->account_capital = 16;
574  $this->account_insurance = 616;
575  $this->account_interest = 518;
576  $this->datestart = $now;
577  $this->dateend = $now + (3600 * 24 * 365);
578  $this->note_public = 'SPECIMEN';
579  $this->capital = 20000;
580  $this->nbterm = 48;
581  $this->rate = 4.3;
582  }
583 
589  public function getSumPayment()
590  {
591  $table = 'payment_loan';
592  $field = 'fk_loan';
593 
594  $sql = 'SELECT sum(amount_capital) as amount';
595  $sql .= ' FROM '.MAIN_DB_PREFIX.$table;
596  $sql .= ' WHERE '.$field.' = '.$this->id;
597 
598  dol_syslog(get_class($this)."::getSumPayment", LOG_DEBUG);
599  $resql = $this->db->query($sql);
600  if ($resql)
601  {
602  $amount = 0;
603 
604  $obj = $this->db->fetch_object($resql);
605  if ($obj) $amount = $obj->amount ? $obj->amount : 0;
606 
607  $this->db->free($resql);
608  return $amount;
609  } else {
610  $this->error = $this->db->lasterror();
611  return -1;
612  }
613  }
614 
621  public function info($id)
622  {
623  $sql = 'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
624  $sql .= ' l.tms';
625  $sql .= ' WHERE l.rowid = '.$id;
626 
627  dol_syslog(get_class($this).'::info', LOG_DEBUG);
628  $result = $this->db->query($sql);
629 
630  if ($result)
631  {
632  if ($this->db->num_rows($result))
633  {
634  $obj = $this->db->fetch_object($result);
635  $this->id = $obj->rowid;
636  if ($obj->fk_user_author)
637  {
638  $cuser = new User($this->db);
639  $cuser->fetch($obj->fk_user_author);
640  $this->user_creation = $cuser;
641  }
642  if ($obj->fk_user_modif)
643  {
644  $muser = new User($this->db);
645  $muser->fetch($obj->fk_user_modif);
646  $this->user_modification = $muser;
647  }
648  $this->date_creation = $this->db->jdate($obj->datec);
649  if (empty($obj->fk_user_modif)) $obj->tms = "";
650  $this->date_modification = $this->db->jdate($obj->tms);
651 
652  $this->db->free($result);
653  return 1;
654  } else {
655  $this->db->free($result);
656  return 0;
657  }
658  } else {
659  $this->error = $this->db->lasterror();
660  return -1;
661  }
662  }
663 }
Loan.
Definition: loan.class.php:30
LibStatut($status, $mode=0, $alreadypaid=-1)
Return label for given status.
Definition: loan.class.php:465
__construct($db)
Constructor.
Definition: loan.class.php:118
getNomUrl($withpicto=0, $maxlen=0, $option= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return clicable name (with eventually the picto)
Definition: loan.class.php:506
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
getLibStatut($mode=0, $alreadypaid=-1)
Return label of loan status (unpaid, paid)
Definition: loan.class.php:451
Class to manage bank transaction lines.
getSumPayment()
Return amount of payments already done.
Definition: loan.class.php:589
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage bank accounts.
create($user)
Create a loan into database.
Definition: loan.class.php:181
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
info($id)
Information on record.
Definition: loan.class.php:621
set_started($user)
Tag loan as payement started.
Definition: loan.class.php:407
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
initAsSpecimen()
Initialise an instance with random values.
Definition: loan.class.php:561
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
set_paid($user)
Tag loan as paid completely.
Definition: loan.class.php:385
print $_SERVER["PHP_SELF"]
Edit parameters.
update($user)
Update loan.
Definition: loan.class.php:340
set_unpaid($user)
Tag loan as payement as unpaid.
Definition: loan.class.php:429
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
fetch($id)
Load object in memory from database.
Definition: loan.class.php:129
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)