25require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
36 public $element =
'loan';
43 public $table =
'loan';
48 public $table_element =
'loan';
53 public $picto =
'money-bill-alt';
98 public $account_capital;
103 public $account_insurance;
108 public $account_interest;
113 public $accountancy_account_capital;
118 public $accountancy_account_insurance;
123 public $accountancy_account_interest;
128 public $insurance_amount;
138 public $fk_user_creat;
143 public $fk_user_modif;
153 const STATUS_UNPAID = 0;
158 const STATUS_PAID = 1;
163 const STATUS_STARTED = 2;
184 $sql =
"SELECT l.rowid, l.entity, l.label, l.capital, l.datestart, l.dateend, l.nbterm, l.rate, l.note_private, l.note_public, l.insurance_amount,";
185 $sql .=
" l.paid, l.fk_bank, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
186 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
187 $sql .=
" WHERE l.rowid = ".((int) $id);
189 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
190 $resql = $this->db->query($sql);
192 if ($this->db->num_rows($resql)) {
193 $obj = $this->db->fetch_object($resql);
195 $this->
id = $obj->rowid;
196 $this->entity = $obj->entity;
197 $this->
ref = $obj->rowid;
198 $this->datestart = $this->db->jdate($obj->datestart);
199 $this->dateend = $this->db->jdate($obj->dateend);
200 $this->label = $obj->label;
201 $this->capital = $obj->capital;
202 $this->nbterm = $obj->nbterm;
203 $this->rate = $obj->rate;
204 $this->note_private = $obj->note_private;
205 $this->note_public = $obj->note_public;
206 $this->insurance_amount = $obj->insurance_amount;
207 $this->paid = $obj->paid;
208 $this->fk_bank = $obj->fk_bank;
210 $this->account_capital = $obj->accountancy_account_capital;
211 $this->account_insurance = $obj->accountancy_account_insurance;
212 $this->account_interest = $obj->accountancy_account_interest;
213 $this->fk_project = $obj->fk_project;
215 $this->db->free($resql);
218 $this->db->free($resql);
222 $this->error = $this->db->lasterror();
236 global
$conf, $langs;
243 $newcapital =
price2num($this->capital,
'MT');
244 if (empty($this->insurance_amount)) {
245 $this->insurance_amount = 0;
247 $newinsuranceamount =
price2num($this->insurance_amount,
'MT');
248 if (isset($this->note_private)) {
249 $this->note_private = trim($this->note_private);
251 if (isset($this->note_public)) {
252 $this->note_public = trim($this->note_public);
254 if (isset($this->account_capital)) {
255 $this->account_capital = trim($this->account_capital);
257 if (isset($this->account_insurance)) {
258 $this->account_insurance = trim($this->account_insurance);
260 if (isset($this->account_interest)) {
261 $this->account_interest = trim($this->account_interest);
263 if (isset($this->fk_bank)) {
264 $this->fk_bank = (int) $this->fk_bank;
266 if (isset($this->fk_user_creat)) {
267 $this->fk_user_creat = (int) $this->fk_user_creat;
269 if (isset($this->fk_user_modif)) {
270 $this->fk_user_modif = (int) $this->fk_user_modif;
272 if (isset($this->fk_project)) {
273 $this->fk_project = (int) $this->fk_project;
277 if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
278 $this->error =
"ErrorBadParameter";
281 if (
isModEnabled(
'accounting') && empty($this->account_capital)) {
282 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyCapitalCode"));
285 if (
isModEnabled(
'accounting') && empty($this->account_insurance)) {
286 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInsuranceCode"));
289 if (
isModEnabled(
'accounting') && empty($this->account_interest)) {
290 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInterestCode"));
296 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
297 $sql .=
" accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
298 $sql .=
" datec, fk_projet, fk_user_author, insurance_amount)";
299 $sql .=
" VALUES ('".$this->db->escape($this->label).
"',";
300 $sql .=
" '".$this->db->escape((
string) $this->fk_bank).
"',";
301 $sql .=
" '".price2num($newcapital).
"',";
302 $sql .=
" '".$this->db->idate($this->datestart).
"',";
303 $sql .=
" '".$this->db->idate($this->dateend).
"',";
304 $sql .=
" '".$this->db->escape((
string) $this->nbterm).
"',";
305 $sql .=
" '".$this->db->escape((
string) $this->rate).
"',";
306 $sql .=
" '".$this->db->escape($this->note_private).
"',";
307 $sql .=
" '".$this->db->escape($this->note_public).
"',";
308 $sql .=
" '".$this->db->escape($this->account_capital).
"',";
309 $sql .=
" '".$this->db->escape($this->account_insurance).
"',";
310 $sql .=
" '".$this->db->escape($this->account_interest).
"',";
311 $sql .=
" ".((int)
$conf->entity).
",";
312 $sql .=
" '".$this->db->idate($now).
"',";
313 $sql .=
" ".(empty($this->fk_project) ?
'NULL' : $this->fk_project).
",";
314 $sql .=
" ".((int) $user->id).
",";
315 $sql .=
" '".price2num($newinsuranceamount).
"'";
318 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
319 $resql = $this->db->query($sql);
321 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"loan");
327 $this->error = $this->db->error();
328 $this->db->rollback();
340 public function delete($user)
347 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
348 $account =
new Account($this->db);
349 $lines_url = $account->get_url(0, $this->
id,
'loan');
352 foreach ($lines_url as $line_url) {
355 $accountline->fetch($line_url[
'fk_bank']);
356 $result = $accountline->delete_urls($user);
358 $this->errors = array_merge($this->errors, [$accountline->error], $accountline->errors);
366 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan where fk_loan=".((int) $this->
id);
367 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
368 $resql = $this->db->query($sql);
371 $this->error = $this->db->lasterror();
376 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"loan where rowid=".((int) $this->
id);
377 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
378 $resql = $this->db->query($sql);
381 $this->error = $this->db->lasterror();
389 $this->db->rollback();
405 if (!is_numeric($this->nbterm)) {
406 $this->error =
'BadValueForParameterForNbTerm';
410 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan";
411 $sql .=
" SET label='".$this->db->escape($this->label).
"',";
412 $sql .=
" capital='".$this->db->escape(
price2num($this->capital)).
"',";
413 $sql .=
" datestart='".$this->db->idate($this->datestart).
"',";
414 $sql .=
" dateend='".$this->db->idate($this->dateend).
"',";
415 $sql .=
" nbterm=".((float) $this->nbterm).
",";
416 $sql .=
" rate=".((float) $this->rate).
",";
417 $sql .=
" accountancy_account_capital = '".$this->db->escape($this->account_capital).
"',";
418 $sql .=
" accountancy_account_insurance = '".$this->db->escape($this->account_insurance).
"',";
419 $sql .=
" accountancy_account_interest = '".$this->db->escape($this->account_interest).
"',";
420 $sql .=
" fk_projet=".(empty($this->fk_project) ?
'NULL' : ((int) $this->fk_project)).
",";
421 $sql .=
" fk_user_modif = ".((int) $user->id).
",";
422 $sql .=
" insurance_amount = '".price2num($this->db->escape((
string) $this->insurance_amount)).
"'";
423 $sql .=
" WHERE rowid=".((int) $this->
id);
425 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
426 $resql = $this->db->query($sql);
431 $this->error = $this->db->error();
432 $this->db->rollback();
445 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
446 $sql .=
" paid = ".((int) $this::STATUS_PAID);
447 $sql .=
" WHERE rowid = ".((int) $this->
id);
449 $return = $this->db->query($sql);
452 $this->paid = $this::STATUS_PAID;
455 $this->error = $this->db->lasterror();
472 dol_syslog(get_class($this).
"::set_started is deprecated, use setStarted instead", LOG_NOTICE);
484 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
485 $sql .=
" paid = ".((int) $this::STATUS_STARTED);
486 $sql .=
" WHERE rowid = ".((int) $this->
id);
488 $return = $this->db->query($sql);
491 $this->paid = $this::STATUS_STARTED;
494 $this->error = $this->db->lasterror();
507 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
508 $sql .=
" paid = ".((int) $this::STATUS_UNPAID);
509 $sql .=
" WHERE rowid = ".((int) $this->
id);
511 $return = $this->db->query($sql);
514 $this->paid = $this::STATUS_UNPAID;
517 $this->error = $this->db->lasterror();
531 return $this->
LibStatut($this->paid, $mode, $alreadypaid);
543 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
549 $langs->loadLangs(array(
"customers",
"bills"));
551 unset($this->labelStatus);
555 $this->labelStatus = array();
556 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
557 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
558 $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
559 if ($status == 0 && $alreadypaid > 0) {
560 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
562 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
563 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
564 $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
565 if ($status == 0 && $alreadypaid > 0) {
566 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
570 $statusType =
'status1';
571 if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
572 $statusType =
'status3';
575 $statusType =
'status6';
578 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
593 public function getNomUrl($withpicto = 0, $maxlen = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
595 global
$conf, $langs, $hookmanager;
599 $label =
'<u>'.$langs->trans(
"ShowLoan").
'</u>';
600 if (!empty($this->
ref)) {
601 $label .=
'<br><strong>'.$langs->trans(
'Ref').
':</strong> '.$this->ref;
603 if (!empty($this->label)) {
604 $label .=
'<br><strong>'.$langs->trans(
'Label').
':</strong> '.$this->label;
607 $label .=
'<br><strong>'.$langs->trans(
"DateStart").
':</strong> '.
dol_print_date($this->datestart,
'day');
610 $label .=
'<br><strong>'.$langs->trans(
"DateEnd").
':</strong> '.
dol_print_date($this->dateend,
'day');
613 $baseurl = DOL_URL_ROOT.
'/loan/card.php';
614 $query = [
'id' => $this->id];
615 if ($option !=
'nolink') {
617 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
618 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
619 $add_save_lastsearch_values = 1;
621 if ($add_save_lastsearch_values) {
622 $query += [
'save_lastsearch_values' => 1];
628 if (empty($notooltip)) {
630 $label = $langs->trans(
"ShowLoan");
631 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
633 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
634 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
636 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
639 $linkstart =
'<a href="'.$url.
'"';
640 $linkstart .= $linkclose.
'>';
643 $result .= $linkstart;
645 $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);
647 if ($withpicto != 2) {
648 $result .= ($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref);
653 $hookmanager->initHooks(array($this->element .
'dao'));
654 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
655 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
657 $result = $hookmanager->resPrint;
659 $result .= $hookmanager->resPrint;
678 $this->label =
'SPECIMEN';
680 $this->account_capital =
'16';
681 $this->account_insurance =
'616';
682 $this->account_interest =
'518';
683 $this->datestart = $now;
684 $this->dateend = $now + (3600 * 24 * 365);
685 $this->note_public =
'SPECIMEN';
686 $this->capital = 20000.80;
700 $sql =
"SELECT sum(amount_capital) as amount";
701 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan";
702 $sql .=
" WHERE fk_loan = ".((int) $this->
id);
704 dol_syslog(get_class($this).
"::getSumPayment", LOG_DEBUG);
706 $resql = $this->db->query($sql);
710 $obj = $this->db->fetch_object($resql);
712 $amount = $obj->amount ? $obj->amount : 0;
715 $this->db->free($resql);
718 $this->error = $this->db->lasterror();
731 $sql =
'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
732 $sql .=
' l.tms as datem';
733 $sql .=
' WHERE l.rowid = '.((int) $id);
735 dol_syslog(get_class($this).
'::info', LOG_DEBUG);
736 $result = $this->db->query($sql);
739 if ($this->db->num_rows($result)) {
740 $obj = $this->db->fetch_object($result);
742 $this->
id = $obj->rowid;
744 $this->user_creation_id = $obj->fk_user_author;
745 $this->user_modification_id = $obj->fk_user_modif;
746 $this->date_creation = $this->db->jdate($obj->datec);
747 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
749 $this->db->free($result);
752 $this->db->free($result);
756 $this->error = $this->db->lasterror();
772 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
774 $return =
'<div class="box-flex-item box-flex-grow-zero">';
775 $return .=
'<div class="info-box info-box-sm">';
776 $return .=
'<span class="info-box-icon bg-infobox-action">';
778 $return .=
'</span>';
779 $return .=
'<div class="info-box-content">';
780 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.$this->getNomUrl(1).
'</span>';
781 if ($selected >= 0) {
782 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
784 if (!empty($this->capital)) {
785 $return .=
' | <span class="opacitymedium">'.$langs->trans(
"Amount").
'</span> : <span class="info-box-label amount">'.
price($this->capital).
'</span>';
788 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateStart").
'</span> : <span class="info-box-label">'.
dol_print_date($this->datestart,
'day').
'</span>';
791 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateEnd").
'</span> : <span class="info-box-label">'.
dol_print_date($this->dateend,
'day').
'</span>';
794 $return .=
'<br><div class="info-box-status">'.$this->getLibStatut(3, (
float) $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 clickable 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 clickable 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)
dol_now($mode='gmt')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
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 '.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
isDolTms($timestamp)
isDolTms check if a timestamp is valid.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
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_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
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 a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.