24require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
35 public $element =
'loan';
37 public $table =
'loan';
42 public $table_element =
'loan';
47 public $picto =
'money-bill-alt';
66 public $account_capital;
67 public $account_insurance;
68 public $account_interest;
69 public $accountancy_account_capital;
70 public $accountancy_account_insurance;
71 public $accountancy_account_interest;
73 public $insurance_amount;
83 public $fk_user_creat;
88 public $fk_user_modif;
100 const STATUS_UNPAID = 0;
101 const STATUS_PAID = 1;
102 const STATUS_STARTED = 2;
123 $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,";
124 $sql .=
" l.paid, l.fk_bank, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
125 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
126 $sql .=
" WHERE l.rowid = ".((int) $id);
128 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
129 $resql = $this->db->query($sql);
131 if ($this->db->num_rows($resql)) {
132 $obj = $this->db->fetch_object($resql);
134 $this->
id = $obj->rowid;
135 $this->
ref = $obj->rowid;
136 $this->datestart = $this->db->jdate($obj->datestart);
137 $this->dateend = $this->db->jdate($obj->dateend);
138 $this->label = $obj->label;
139 $this->capital = $obj->capital;
140 $this->nbterm = $obj->nbterm;
141 $this->rate = $obj->rate;
142 $this->note_private = $obj->note_private;
143 $this->note_public = $obj->note_public;
144 $this->insurance_amount = $obj->insurance_amount;
145 $this->paid = $obj->paid;
146 $this->fk_bank = $obj->fk_bank;
148 $this->account_capital = $obj->accountancy_account_capital;
149 $this->account_insurance = $obj->accountancy_account_insurance;
150 $this->account_interest = $obj->accountancy_account_interest;
151 $this->fk_project = $obj->fk_project;
153 $this->db->free($resql);
156 $this->db->free($resql);
160 $this->error = $this->db->lasterror();
174 global $conf, $langs;
181 $newcapital =
price2num($this->capital,
'MT');
182 if (empty($this->insurance_amount)) {
183 $this->insurance_amount = 0;
185 $newinsuranceamount =
price2num($this->insurance_amount,
'MT');
186 if (isset($this->note_private)) {
187 $this->note_private = trim($this->note_private);
189 if (isset($this->note_public)) {
190 $this->note_public = trim($this->note_public);
192 if (isset($this->account_capital)) {
193 $this->account_capital = trim($this->account_capital);
195 if (isset($this->account_insurance)) {
196 $this->account_insurance = trim($this->account_insurance);
198 if (isset($this->account_interest)) {
199 $this->account_interest = trim($this->account_interest);
201 if (isset($this->fk_bank)) {
202 $this->fk_bank = (int) $this->fk_bank;
204 if (isset($this->fk_user_creat)) {
205 $this->fk_user_creat = (int) $this->fk_user_creat;
207 if (isset($this->fk_user_modif)) {
208 $this->fk_user_modif = (int) $this->fk_user_modif;
210 if (isset($this->fk_project)) {
211 $this->fk_project = (int) $this->fk_project;
215 if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
216 $this->error =
"ErrorBadParameter";
219 if (isModEnabled(
'accounting') && empty($this->account_capital)) {
220 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyCapitalCode"));
223 if (isModEnabled(
'accounting') && empty($this->account_insurance)) {
224 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInsuranceCode"));
227 if (isModEnabled(
'accounting') && empty($this->account_interest)) {
228 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInterestCode"));
234 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
235 $sql .=
" accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
236 $sql .=
" datec, fk_projet, fk_user_author, insurance_amount)";
237 $sql .=
" VALUES ('".$this->db->escape($this->label).
"',";
238 $sql .=
" '".$this->db->escape($this->fk_bank).
"',";
239 $sql .=
" '".price2num($newcapital).
"',";
240 $sql .=
" '".$this->db->idate($this->datestart).
"',";
241 $sql .=
" '".$this->db->idate($this->dateend).
"',";
242 $sql .=
" '".$this->db->escape($this->nbterm).
"',";
243 $sql .=
" '".$this->db->escape($this->rate).
"',";
244 $sql .=
" '".$this->db->escape($this->note_private).
"',";
245 $sql .=
" '".$this->db->escape($this->note_public).
"',";
246 $sql .=
" '".$this->db->escape($this->account_capital).
"',";
247 $sql .=
" '".$this->db->escape($this->account_insurance).
"',";
248 $sql .=
" '".$this->db->escape($this->account_interest).
"',";
249 $sql .=
" ".$conf->entity.
",";
250 $sql .=
" '".$this->db->idate($now).
"',";
251 $sql .=
" ".(empty($this->fk_project) ?
'NULL' : $this->fk_project).
",";
252 $sql .=
" ".$user->id.
",";
253 $sql .=
" '".price2num($newinsuranceamount).
"'";
256 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
257 $resql = $this->db->query($sql);
259 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"loan");
265 $this->error = $this->db->error();
266 $this->db->rollback();
278 public function delete($user)
285 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
286 $account =
new Account($this->db);
287 $lines_url = $account->get_url(
'', $this->
id,
'loan');
290 foreach ($lines_url as $line_url) {
293 $accountline->fetch($line_url[
'fk_bank']);
294 $result = $accountline->delete_urls($user);
303 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan where fk_loan=".((int) $this->
id);
304 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
305 $resql = $this->db->query($sql);
308 $this->error = $this->db->lasterror();
313 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"loan where rowid=".((int) $this->
id);
314 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
315 $resql = $this->db->query($sql);
318 $this->error = $this->db->lasterror();
326 $this->db->rollback();
342 if (!is_numeric($this->nbterm)) {
343 $this->error =
'BadValueForParameterForNbTerm';
347 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan";
348 $sql .=
" SET label='".$this->db->escape($this->label).
"',";
349 $sql .=
" capital='".price2num($this->db->escape($this->capital)).
"',";
350 $sql .=
" datestart='".$this->db->idate($this->datestart).
"',";
351 $sql .=
" dateend='".$this->db->idate($this->dateend).
"',";
352 $sql .=
" nbterm=".((float) $this->nbterm).
",";
353 $sql .=
" rate=".((float) $this->rate).
",";
354 $sql .=
" accountancy_account_capital = '".$this->db->escape($this->account_capital).
"',";
355 $sql .=
" accountancy_account_insurance = '".$this->db->escape($this->account_insurance).
"',";
356 $sql .=
" accountancy_account_interest = '".$this->db->escape($this->account_interest).
"',";
357 $sql .=
" fk_projet=".(empty($this->fk_project) ?
'NULL' : ((int) $this->fk_project)).
",";
358 $sql .=
" fk_user_modif = ".$user->id.
",";
359 $sql .=
" insurance_amount = '".price2num($this->db->escape($this->insurance_amount)).
"'";
360 $sql .=
" WHERE rowid=".((int) $this->
id);
362 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
363 $resql = $this->db->query($sql);
368 $this->error = $this->db->error();
369 $this->db->rollback();
382 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
383 $sql .=
" paid = ".$this::STATUS_PAID;
384 $sql .=
" WHERE rowid = ".((int) $this->
id);
386 $return = $this->db->query($sql);
389 $this->paid = $this::STATUS_PAID;
392 $this->error = $this->db->lasterror();
409 dol_syslog(get_class($this).
"::set_started is deprecated, use setStarted instead", LOG_NOTICE);
421 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
422 $sql .=
" paid = ".$this::STATUS_STARTED;
423 $sql .=
" WHERE rowid = ".((int) $this->
id);
425 $return = $this->db->query($sql);
428 $this->paid = $this::STATUS_STARTED;
431 $this->error = $this->db->lasterror();
444 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
445 $sql .=
" paid = ".$this::STATUS_UNPAID;
446 $sql .=
" WHERE rowid = ".((int) $this->
id);
448 $return = $this->db->query($sql);
451 $this->paid = $this::STATUS_UNPAID;
454 $this->error = $this->db->lasterror();
468 return $this->
LibStatut($this->paid, $mode, $alreadypaid);
480 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
486 $langs->loadLangs(array(
"customers",
"bills"));
488 unset($this->labelStatus);
492 $this->labelStatus = array();
493 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
494 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
495 $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
496 if ($status == 0 && $alreadypaid > 0) {
497 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
499 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
500 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
501 $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
502 if ($status == 0 && $alreadypaid > 0) {
503 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
507 $statusType =
'status1';
508 if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
509 $statusType =
'status3';
512 $statusType =
'status6';
515 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
530 public function getNomUrl($withpicto = 0, $maxlen = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
532 global $conf, $langs, $hookmanager;
536 $label =
'<u>'.$langs->trans(
"ShowLoan").
'</u>';
537 if (!empty($this->
ref)) {
538 $label .=
'<br><strong>'.$langs->trans(
'Ref').
':</strong> '.$this->ref;
540 if (!empty($this->label)) {
541 $label .=
'<br><strong>'.$langs->trans(
'Label').
':</strong> '.$this->label;
544 $url = DOL_URL_ROOT.
'/loan/card.php?id='.$this->id;
546 if ($option !=
'nolink') {
548 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
549 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
550 $add_save_lastsearch_values = 1;
552 if ($add_save_lastsearch_values) {
553 $url .=
'&save_lastsearch_values=1';
558 if (empty($notooltip)) {
560 $label = $langs->trans(
"ShowMyObject");
561 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
563 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
564 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
566 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
569 $linkstart =
'<a href="'.$url.
'"';
570 $linkstart .= $linkclose.
'>';
573 $result .= $linkstart;
575 $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);
577 if ($withpicto != 2) {
578 $result .= ($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref);
583 $hookmanager->initHooks(array($this->element .
'dao'));
584 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
585 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
587 $result = $hookmanager->resPrint;
589 $result .= $hookmanager->resPrint;
603 global $user, $langs, $conf;
610 $this->label =
'SPECIMEN';
612 $this->account_capital = 16;
613 $this->account_insurance = 616;
614 $this->account_interest = 518;
615 $this->datestart = $now;
616 $this->dateend = $now + (3600 * 24 * 365);
617 $this->note_public =
'SPECIMEN';
618 $this->capital = 20000;
632 $table =
'payment_loan';
635 $sql =
'SELECT sum(amount_capital) as amount';
636 $sql .=
' FROM '.MAIN_DB_PREFIX.$table;
637 $sql .=
" WHERE ".$field.
" = ".((int) $this->
id);
639 dol_syslog(get_class($this).
"::getSumPayment", LOG_DEBUG);
640 $resql = $this->db->query($sql);
644 $obj = $this->db->fetch_object($resql);
646 $amount = $obj->amount ? $obj->amount : 0;
649 $this->db->free($resql);
652 $this->error = $this->db->lasterror();
665 $sql =
'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
666 $sql .=
' l.tms as datem';
667 $sql .=
' WHERE l.rowid = '.((int) $id);
669 dol_syslog(get_class($this).
'::info', LOG_DEBUG);
670 $result = $this->db->query($sql);
673 if ($this->db->num_rows($result)) {
674 $obj = $this->db->fetch_object($result);
676 $this->
id = $obj->rowid;
678 $this->user_creation_id = $obj->fk_user_author;
679 $this->user_modification_id = $obj->fk_user_modif;
680 $this->date_creation = $this->db->jdate($obj->datec);
681 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
683 $this->db->free($result);
686 $this->db->free($result);
690 $this->error = $this->db->lasterror();
706 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
708 $return =
'<div class="box-flex-item box-flex-grow-zero">';
709 $return .=
'<div class="info-box info-box-sm">';
710 $return .=
'<span class="info-box-icon bg-infobox-action">';
712 $return .=
'</span>';
713 $return .=
'<div class="info-box-content">';
714 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl(1) : $this->ref).
'</span>';
715 if ($selected >= 0) {
716 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
718 if (property_exists($this,
'capital')) {
719 $return .=
' | <span class="opacitymedium">'.$langs->trans(
"Amount").
'</span> : <span class="info-box-label amount">'.
price($this->capital).
'</span>';
721 if (property_exists($this,
'datestart')) {
722 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateStart").
'</span> : <span class="info-box-label">'.
dol_print_date($this->db->jdate($this->datestart),
'day').
'</span>';
724 if (property_exists($this,
'dateend')) {
725 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateEnd").
'</span> : <span class="info-box-label">'.
dol_print_date($this->db->jdate($this->dateend),
'day').
'</span>';
728 if (method_exists($this,
'LibStatut')) {
729 $return .=
'<br><div class="info-box-status">'.$this->getLibStatut(3, $this->alreadypaid).
'</div>';
Class to manage bank accounts.
Class to manage bank transaction lines.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch($id)
Load object in memory from database.
setUnpaid($user)
Tag loan as payment as unpaid.
initAsSpecimen()
Initialise an instance with random values.
setPaid($user)
Tag loan as paid completely.
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
setStarted($user)
Tag loan as payment started.
info($id)
Information on record.
LibStatut($status, $mode=0, $alreadypaid=-1)
Return label for given status.
__construct($db)
Constructor.
getNomUrl($withpicto=0, $maxlen=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return clicable name (with eventually the picto)
update($user)
Update loan.
getSumPayment()
Return amount of payments already done.
create($user)
Create a loan into database.
set_started($user)
Tag loan as payment started.
getLibStatut($mode=0, $alreadypaid=-1)
Return label of loan status (unpaid, paid)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.