dolibarr  9.0.0
loan.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2014-2018 Alexandre Spangaro <aspangaro@zendsi.com>
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 <http://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 
44  public $picto = 'bill';
45 
49  public $rowid;
50 
51  public $datestart;
52  public $dateend;
53 
57  public $label;
58 
59  public $capital;
60  public $nbterm;
61  public $rate;
62  public $paid;
63  public $account_capital;
64  public $account_insurance;
65  public $account_interest;
66  public $date_creation;
67  public $date_modification;
68  public $date_validation;
69 
73  public $fk_bank;
74 
78  public $fk_user_creat;
79 
83  public $fk_user_modif;
84 
88  public $fk_project;
89 
90 
96  function __construct($db)
97  {
98  $this->db = $db;
99  }
100 
107  function fetch($id)
108  {
109  $sql = "SELECT l.rowid, l.label, l.capital, l.datestart, l.dateend, l.nbterm, l.rate, l.note_private, l.note_public,";
110  $sql.= " l.paid, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
111  $sql.= " FROM ".MAIN_DB_PREFIX."loan as l";
112  $sql.= " WHERE l.rowid = ".$id;
113 
114  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
115  $resql=$this->db->query($sql);
116  if ($resql)
117  {
118  if ($this->db->num_rows($resql))
119  {
120  $obj = $this->db->fetch_object($resql);
121 
122  $this->id = $obj->rowid;
123  $this->ref = $obj->rowid;
124  $this->datestart = $this->db->jdate($obj->datestart);
125  $this->dateend = $this->db->jdate($obj->dateend);
126  $this->label = $obj->label;
127  $this->capital = $obj->capital;
128  $this->nbterm = $obj->nbterm;
129  $this->rate = $obj->rate;
130  $this->note_private = $obj->note_private;
131  $this->note_public = $obj->note_public;
132  $this->paid = $obj->paid;
133 
134  $this->account_capital = $obj->accountancy_account_capital;
135  $this->account_insurance = $obj->accountancy_account_insurance;
136  $this->account_interest = $obj->accountancy_account_interest;
137  $this->fk_project = $obj->fk_project;
138 
139  $this->db->free($resql);
140  return 1;
141  }
142  else
143  {
144  $this->db->free($resql);
145  return 0;
146  }
147  }
148  else
149  {
150  $this->error=$this->db->lasterror();
151  return -1;
152  }
153  }
154 
155 
162  function create($user)
163  {
164  global $conf, $langs;
165 
166  $error=0;
167 
168  $now=dol_now();
169 
170  // clean parameters
171  $newcapital=price2num($this->capital,'MT');
172  if (isset($this->note_private)) $this->note_private = trim($this->note_private);
173  if (isset($this->note_public)) $this->note_public = trim($this->note_public);
174  if (isset($this->account_capital)) $this->account_capital = trim($this->account_capital);
175  if (isset($this->account_insurance)) $this->account_insurance = trim($this->account_insurance);
176  if (isset($this->account_interest)) $this->account_interest = trim($this->account_interest);
177  if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
178  if (isset($this->fk_user_creat)) $this->fk_user_creat = (int) $this->fk_user_creat;
179  if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
180  if (isset($this->fk_project)) $this->fk_project = (int) $this->fk_project;
181 
182  // Check parameters
183  if (! $newcapital > 0 || empty($this->datestart) || empty($this->dateend))
184  {
185  $this->error="ErrorBadParameter";
186  return -2;
187  }
188  if (($conf->accounting->enabled) && empty($this->account_capital))
189  {
190  $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyCapitalCode"));
191  return -2;
192  }
193  if (($conf->accounting->enabled) && empty($this->account_insurance))
194  {
195  $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInsuranceCode"));
196  return -2;
197  }
198  if (($conf->accounting->enabled) && empty($this->account_interest))
199  {
200  $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInterestCode"));
201  return -2;
202  }
203 
204  $this->db->begin();
205 
206  $sql = "INSERT INTO ".MAIN_DB_PREFIX."loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
207  $sql.= " accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
208  $sql.= " datec, fk_projet, fk_user_author)";
209  $sql.= " VALUES ('".$this->db->escape($this->label)."',";
210  $sql.= " '".$this->db->escape($this->fk_bank)."',";
211  $sql.= " '".price2num($newcapital)."',";
212  $sql.= " '".$this->db->idate($this->datestart)."',";
213  $sql.= " '".$this->db->idate($this->dateend)."',";
214  $sql.= " '".$this->db->escape($this->nbterm)."',";
215  $sql.= " '".$this->db->escape($this->rate)."',";
216  $sql.= " '".$this->db->escape($this->note_private)."',";
217  $sql.= " '".$this->db->escape($this->note_public)."',";
218  $sql.= " '".$this->db->escape($this->account_capital)."',";
219  $sql.= " '".$this->db->escape($this->account_insurance)."',";
220  $sql.= " '".$this->db->escape($this->account_interest)."',";
221  $sql.= " ".$conf->entity.",";
222  $sql.= " '".$this->db->idate($now)."',";
223  $sql.= " ".(empty($this->fk_project)?'NULL':$this->fk_project).",";
224  $sql.= " ".$user->id;
225  $sql.= ")";
226 
227  dol_syslog(get_class($this)."::create", LOG_DEBUG);
228  $resql=$this->db->query($sql);
229  if ($resql)
230  {
231  $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."loan");
232 
233  //dol_syslog("Loans::create this->id=".$this->id);
234  $this->db->commit();
235  return $this->id;
236  }
237  else
238  {
239  $this->error=$this->db->error();
240  $this->db->rollback();
241  return -1;
242  }
243  }
244 
245 
252  function delete($user)
253  {
254  $error=0;
255 
256  $this->db->begin();
257 
258  // Get bank transaction lines for this loan
259  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
260  $account=new Account($this->db);
261  $lines_url=$account->get_url('',$this->id,'loan');
262 
263  // Delete bank urls
264  foreach ($lines_url as $line_url)
265  {
266  if (! $error)
267  {
268  $accountline=new AccountLine($this->db);
269  $accountline->fetch($line_url['fk_bank']);
270  $result=$accountline->delete_urls($user);
271  if ($result < 0)
272  {
273  $error++;
274  }
275  }
276  }
277 
278  // Delete payments
279  if (! $error)
280  {
281  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan where fk_loan=".$this->id;
282  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
283  $resql=$this->db->query($sql);
284  if (! $resql)
285  {
286  $error++;
287  $this->error=$this->db->lasterror();
288  }
289  }
290 
291  if (! $error)
292  {
293  $sql = "DELETE FROM ".MAIN_DB_PREFIX."loan where rowid=".$this->id;
294  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
295  $resql=$this->db->query($sql);
296  if (! $resql)
297  {
298  $error++;
299  $this->error=$this->db->lasterror();
300  }
301  }
302 
303  if (! $error)
304  {
305  $this->db->commit();
306  return 1;
307  }
308  else
309  {
310  $this->db->rollback();
311  return -1;
312  }
313  }
314 
315 
322  function update($user)
323  {
324  $this->db->begin();
325 
326  if (! is_numeric($this->nbterm))
327  {
328  $this->error='BadValueForParameterForNbTerm';
329  return -1;
330  }
331 
332  $sql = "UPDATE ".MAIN_DB_PREFIX."loan";
333  $sql.= " SET label='".$this->db->escape($this->label)."',";
334  $sql.= " capital='".price2num($this->db->escape($this->capital))."',";
335  $sql.= " datestart='".$this->db->idate($this->datestart)."',";
336  $sql.= " dateend='".$this->db->idate($this->dateend)."',";
337  $sql.= " nbterm=".$this->nbterm.",";
338  $sql.= " accountancy_account_capital = '".$this->db->escape($this->account_capital)."',";
339  $sql.= " accountancy_account_insurance = '".$this->db->escape($this->account_insurance)."',";
340  $sql.= " accountancy_account_interest = '".$this->db->escape($this->account_interest)."',";
341  $sql.= " fk_projet=".(empty($this->fk_project)?'NULL':$this->fk_project).",";
342  $sql.= " fk_user_modif = ".$user->id;
343  $sql.= " WHERE rowid=".$this->id;
344 
345  dol_syslog(get_class($this)."::update", LOG_DEBUG);
346  $resql=$this->db->query($sql);
347  if ($resql)
348  {
349  $this->db->commit();
350  return 1;
351  }
352  else
353  {
354  $this->error=$this->db->error();
355  $this->db->rollback();
356  return -1;
357  }
358  }
359 
360  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
367  function set_paid($user)
368  {
369  // phpcs:enable
370  $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
371  $sql.= " paid = 1";
372  $sql.= " WHERE rowid = ".$this->id;
373  $return = $this->db->query($sql);
374  if ($return) {
375  return 1;
376  } else {
377  $this->error=$this->db->lasterror();
378  return -1;
379  }
380  }
381 
389  function getLibStatut($mode=0,$alreadypaid=-1)
390  {
391  return $this->LibStatut($this->paid,$mode,$alreadypaid);
392  }
393 
394  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
403  function LibStatut($statut,$mode=0,$alreadypaid=-1)
404  {
405  // phpcs:enable
406  global $langs;
407  $langs->loadLangs(array("customers","bills"));
408 
409  if ($mode == 0 || $mode == 1)
410  {
411  if ($statut == 0) return $langs->trans("Unpaid");
412  elseif ($statut == 1) return $langs->trans("Paid");
413  }
414  elseif ($mode == 2)
415  {
416  if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
417  elseif ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
418  elseif ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
419  }
420  elseif ($mode == 3)
421  {
422  if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1');
423  elseif ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3');
424  elseif ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6');
425  }
426  elseif ($mode == 4)
427  {
428  if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
429  elseif ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
430  elseif ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
431  }
432  elseif ($mode == 5)
433  {
434  if ($statut == 0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
435  elseif ($statut == 0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
436  elseif ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
437  }
438  elseif ($mode == 6)
439  {
440  if ($statut == 0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
441  elseif ($statut == 0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
442  elseif ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
443  }
444 
445  else return "Error, mode/status not found";
446  }
447 
448 
456  function getNomUrl($withpicto=0,$maxlen=0)
457  {
458  global $langs;
459 
460  $result='';
461 
462  $tooltip = '<u>' . $langs->trans("ShowLoan") . '</u>';
463  if (! empty($this->ref))
464  $tooltip .= '<br><strong>' . $langs->trans('Ref') . ':</strong> ' . $this->ref;
465  if (! empty($this->label))
466  $tooltip .= '<br><strong>' . $langs->trans('Label') . ':</strong> ' . $this->label;
467 
468  $linkstart = '<a href="'.DOL_URL_ROOT.'/loan/card.php?id='.$this->id.'" title="'.str_replace('\n', '', dol_escape_htmltag($tooltip, 1)).'" class="classfortooltip">';
469  $linkend = '</a>';
470 
471  $result .= $linkstart;
472  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);
473  if ($withpicto != 2) $result.= ($maxlen?dol_trunc($this->ref,$maxlen):$this->ref);
474  $result .= $linkend;
475 
476  return $result;
477  }
478 
486  function initAsSpecimen()
487  {
488  global $user, $langs, $conf;
489 
490  $now=dol_now();
491 
492  // Initialise parameters
493  $this->id = 0;
494  $this->fk_bank = 1;
495  $this->label = 'SPECIMEN';
496  $this->specimen = 1;
497  $this->socid = 1;
498  $this->account_capital = 16;
499  $this->account_insurance = 616;
500  $this->account_interest = 518;
501  $this->datestart = $now;
502  $this->dateend = $now + (3600 * 24 * 365);
503  $this->note_public = 'SPECIMEN';
504  $this->capital = 20000;
505  $this->nbterm = 48;
506  $this->rate = 4.3;
507  }
508 
514  function getSumPayment()
515  {
516  $table='payment_loan';
517  $field='fk_loan';
518 
519  $sql = 'SELECT sum(amount) as amount';
520  $sql.= ' FROM '.MAIN_DB_PREFIX.$table;
521  $sql.= ' WHERE '.$field.' = '.$this->id;
522 
523  dol_syslog(get_class($this)."::getSumPayment", LOG_DEBUG);
524  $resql=$this->db->query($sql);
525  if ($resql)
526  {
527  $amount=0;
528 
529  $obj = $this->db->fetch_object($resql);
530  if ($obj) $amount=$obj->amount?$obj->amount:0;
531 
532  $this->db->free($resql);
533  return $amount;
534  }
535  else
536  {
537  $this->error=$this->db->lasterror();
538  return -1;
539  }
540  }
541 
548  function info($id)
549  {
550  $sql = 'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
551  $sql.= ' l.tms';
552  $sql.= ' FROM '.MAIN_DB_PREFIX.'loan as l';
553  $sql.= ' WHERE l.rowid = '.$id;
554 
555  dol_syslog(get_class($this).'::info', LOG_DEBUG);
556  $result = $this->db->query($sql);
557 
558  if ($result)
559  {
560  if ($this->db->num_rows($result))
561  {
562  $obj = $this->db->fetch_object($result);
563  $this->id = $obj->rowid;
564  if ($obj->fk_user_author)
565  {
566  $cuser = new User($this->db);
567  $cuser->fetch($obj->fk_user_author);
568  $this->user_creation = $cuser;
569  }
570  if ($obj->fk_user_modif)
571  {
572  $muser = new User($this->db);
573  $muser->fetch($obj->fk_user_modif);
574  $this->user_modification = $muser;
575  }
576  $this->date_creation = $this->db->jdate($obj->datec);
577  if (empty($obj->fk_user_modif)) $obj->tms = "";
578  $this->date_modification = $this->db->jdate($obj->tms);
579 
580  $this->db->free($result);
581  return 1;
582  }
583  else
584  {
585  $this->db->free($result);
586  return 0;
587  }
588  }
589  else
590  {
591  $this->error=$this->db->lasterror();
592  return -1;
593  }
594  }
595 }
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
getNomUrl($withpicto=0, $maxlen=0)
Return clicable name (with eventually the picto)
Definition: loan.class.php:456
Loan.
Definition: loan.class.php:30
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.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
__construct($db)
Constructor.
Definition: loan.class.php:96
Class to manage Dolibarr users.
Definition: user.class.php:41
getLibStatut($mode=0, $alreadypaid=-1)
Return label of loan status (unpaid, paid)
Definition: loan.class.php:389
Class to manage bank transaction lines.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
getSumPayment()
Return amount of payments already done.
Definition: loan.class.php:514
Class to manage bank accounts.
LibStatut($statut, $mode=0, $alreadypaid=-1)
Return label for given status.
Definition: loan.class.php:403
create($user)
Create a loan into database.
Definition: loan.class.php:162
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
info($id)
Information on record.
Definition: loan.class.php:548
initAsSpecimen()
Initialise an instance with random values.
Definition: loan.class.php:486
set_paid($user)
Tag loan as payed completely.
Definition: loan.class.php:367
dol_now($mode='gmt')
Return date for now.
update($user)
Update loan.
Definition: loan.class.php:322
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
fetch($id)
Load object in memory from database.
Definition: loan.class.php:107
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)