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;
76 public $date_creation;
81 public $date_modification;
86 public $date_validation;
88 public $insurance_amount;
98 public $fk_user_creat;
103 public $fk_user_modif;
115 const STATUS_UNPAID = 0;
116 const STATUS_PAID = 1;
117 const STATUS_STARTED = 2;
138 $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,";
139 $sql .=
" l.paid, l.fk_bank, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
140 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
141 $sql .=
" WHERE l.rowid = ".((int) $id);
143 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
144 $resql = $this->db->query($sql);
146 if ($this->db->num_rows($resql)) {
147 $obj = $this->db->fetch_object($resql);
149 $this->
id = $obj->rowid;
150 $this->
ref = $obj->rowid;
151 $this->datestart = $this->db->jdate($obj->datestart);
152 $this->dateend = $this->db->jdate($obj->dateend);
153 $this->label = $obj->label;
154 $this->capital = $obj->capital;
155 $this->nbterm = $obj->nbterm;
156 $this->rate = $obj->rate;
157 $this->note_private = $obj->note_private;
158 $this->note_public = $obj->note_public;
159 $this->insurance_amount = $obj->insurance_amount;
160 $this->paid = $obj->paid;
161 $this->fk_bank = $obj->fk_bank;
163 $this->account_capital = $obj->accountancy_account_capital;
164 $this->account_insurance = $obj->accountancy_account_insurance;
165 $this->account_interest = $obj->accountancy_account_interest;
166 $this->fk_project = $obj->fk_project;
168 $this->db->free($resql);
171 $this->db->free($resql);
175 $this->error = $this->db->lasterror();
189 global $conf, $langs;
196 $newcapital =
price2num($this->capital,
'MT');
197 if (empty($this->insurance_amount)) {
198 $this->insurance_amount = 0;
200 $newinsuranceamount =
price2num($this->insurance_amount,
'MT');
201 if (isset($this->note_private)) {
202 $this->note_private = trim($this->note_private);
204 if (isset($this->note_public)) {
205 $this->note_public = trim($this->note_public);
207 if (isset($this->account_capital)) {
208 $this->account_capital = trim($this->account_capital);
210 if (isset($this->account_insurance)) {
211 $this->account_insurance = trim($this->account_insurance);
213 if (isset($this->account_interest)) {
214 $this->account_interest = trim($this->account_interest);
216 if (isset($this->fk_bank)) {
217 $this->fk_bank = (int) $this->fk_bank;
219 if (isset($this->fk_user_creat)) {
220 $this->fk_user_creat = (int) $this->fk_user_creat;
222 if (isset($this->fk_user_modif)) {
223 $this->fk_user_modif = (int) $this->fk_user_modif;
225 if (isset($this->fk_project)) {
226 $this->fk_project = (int) $this->fk_project;
230 if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
231 $this->error =
"ErrorBadParameter";
234 if (isModEnabled(
'accounting') && empty($this->account_capital)) {
235 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyCapitalCode"));
238 if (isModEnabled(
'accounting') && empty($this->account_insurance)) {
239 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInsuranceCode"));
242 if (isModEnabled(
'accounting') && empty($this->account_interest)) {
243 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInterestCode"));
249 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
250 $sql .=
" accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
251 $sql .=
" datec, fk_projet, fk_user_author, insurance_amount)";
252 $sql .=
" VALUES ('".$this->db->escape($this->label).
"',";
253 $sql .=
" '".$this->db->escape($this->fk_bank).
"',";
254 $sql .=
" '".price2num($newcapital).
"',";
255 $sql .=
" '".$this->db->idate($this->datestart).
"',";
256 $sql .=
" '".$this->db->idate($this->dateend).
"',";
257 $sql .=
" '".$this->db->escape($this->nbterm).
"',";
258 $sql .=
" '".$this->db->escape($this->rate).
"',";
259 $sql .=
" '".$this->db->escape($this->note_private).
"',";
260 $sql .=
" '".$this->db->escape($this->note_public).
"',";
261 $sql .=
" '".$this->db->escape($this->account_capital).
"',";
262 $sql .=
" '".$this->db->escape($this->account_insurance).
"',";
263 $sql .=
" '".$this->db->escape($this->account_interest).
"',";
264 $sql .=
" ".$conf->entity.
",";
265 $sql .=
" '".$this->db->idate($now).
"',";
266 $sql .=
" ".(empty($this->fk_project) ?
'NULL' : $this->fk_project).
",";
267 $sql .=
" ".$user->id.
",";
268 $sql .=
" '".price2num($newinsuranceamount).
"'";
271 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
272 $resql = $this->db->query($sql);
274 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"loan");
280 $this->error = $this->db->error();
281 $this->db->rollback();
293 public function delete($user)
300 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
301 $account =
new Account($this->db);
302 $lines_url = $account->get_url(
'', $this->
id,
'loan');
305 foreach ($lines_url as $line_url) {
308 $accountline->fetch($line_url[
'fk_bank']);
309 $result = $accountline->delete_urls($user);
318 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan where fk_loan=".((int) $this->
id);
319 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
320 $resql = $this->db->query($sql);
323 $this->error = $this->db->lasterror();
328 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"loan where rowid=".((int) $this->
id);
329 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
330 $resql = $this->db->query($sql);
333 $this->error = $this->db->lasterror();
341 $this->db->rollback();
357 if (!is_numeric($this->nbterm)) {
358 $this->error =
'BadValueForParameterForNbTerm';
362 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan";
363 $sql .=
" SET label='".$this->db->escape($this->label).
"',";
364 $sql .=
" capital='".price2num($this->db->escape($this->capital)).
"',";
365 $sql .=
" datestart='".$this->db->idate($this->datestart).
"',";
366 $sql .=
" dateend='".$this->db->idate($this->dateend).
"',";
367 $sql .=
" nbterm=".((float) $this->nbterm).
",";
368 $sql .=
" rate=".((float) $this->rate).
",";
369 $sql .=
" accountancy_account_capital = '".$this->db->escape($this->account_capital).
"',";
370 $sql .=
" accountancy_account_insurance = '".$this->db->escape($this->account_insurance).
"',";
371 $sql .=
" accountancy_account_interest = '".$this->db->escape($this->account_interest).
"',";
372 $sql .=
" fk_projet=".(empty($this->fk_project) ?
'NULL' : ((int) $this->fk_project)).
",";
373 $sql .=
" fk_user_modif = ".$user->id.
",";
374 $sql .=
" insurance_amount = '".price2num($this->db->escape($this->insurance_amount)).
"'";
375 $sql .=
" WHERE rowid=".((int) $this->
id);
377 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
378 $resql = $this->db->query($sql);
383 $this->error = $this->db->error();
384 $this->db->rollback();
397 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
398 $sql .=
" paid = ".$this::STATUS_PAID;
399 $sql .=
" WHERE rowid = ".((int) $this->
id);
401 $return = $this->db->query($sql);
404 $this->paid = $this::STATUS_PAID;
407 $this->error = $this->db->lasterror();
424 dol_syslog(get_class($this).
"::set_started is deprecated, use setStarted instead", LOG_NOTICE);
436 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
437 $sql .=
" paid = ".$this::STATUS_STARTED;
438 $sql .=
" WHERE rowid = ".((int) $this->
id);
440 $return = $this->db->query($sql);
443 $this->paid = $this::STATUS_STARTED;
446 $this->error = $this->db->lasterror();
459 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
460 $sql .=
" paid = ".$this::STATUS_UNPAID;
461 $sql .=
" WHERE rowid = ".((int) $this->
id);
463 $return = $this->db->query($sql);
466 $this->paid = $this::STATUS_UNPAID;
469 $this->error = $this->db->lasterror();
483 return $this->
LibStatut($this->paid, $mode, $alreadypaid);
495 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
501 $langs->loadLangs(array(
"customers",
"bills"));
503 unset($this->labelStatus);
507 $this->labelStatus = array();
508 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
509 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
510 $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
511 if ($status == 0 && $alreadypaid > 0) {
512 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
514 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
515 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
516 $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
517 if ($status == 0 && $alreadypaid > 0) {
518 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
522 $statusType =
'status1';
523 if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
524 $statusType =
'status3';
527 $statusType =
'status6';
530 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
545 public function getNomUrl($withpicto = 0, $maxlen = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
547 global $conf, $langs, $hookmanager;
551 $label =
'<u>'.$langs->trans(
"ShowLoan").
'</u>';
552 if (!empty($this->
ref)) {
553 $label .=
'<br><strong>'.$langs->trans(
'Ref').
':</strong> '.$this->ref;
555 if (!empty($this->label)) {
556 $label .=
'<br><strong>'.$langs->trans(
'Label').
':</strong> '.$this->label;
559 $url = DOL_URL_ROOT.
'/loan/card.php?id='.$this->id;
561 if ($option !=
'nolink') {
563 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
564 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
565 $add_save_lastsearch_values = 1;
567 if ($add_save_lastsearch_values) {
568 $url .=
'&save_lastsearch_values=1';
573 if (empty($notooltip)) {
575 $label = $langs->trans(
"ShowMyObject");
576 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
578 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
579 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
581 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
584 $linkstart =
'<a href="'.$url.
'"';
585 $linkstart .= $linkclose.
'>';
588 $result .= $linkstart;
590 $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);
592 if ($withpicto != 2) {
593 $result .= ($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref);
598 $hookmanager->initHooks(array($this->element .
'dao'));
599 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
600 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
602 $result = $hookmanager->resPrint;
604 $result .= $hookmanager->resPrint;
618 global $user, $langs, $conf;
625 $this->label =
'SPECIMEN';
627 $this->account_capital = 16;
628 $this->account_insurance = 616;
629 $this->account_interest = 518;
630 $this->datestart = $now;
631 $this->dateend = $now + (3600 * 24 * 365);
632 $this->note_public =
'SPECIMEN';
633 $this->capital = 20000;
647 $table =
'payment_loan';
650 $sql =
'SELECT sum(amount_capital) as amount';
651 $sql .=
' FROM '.MAIN_DB_PREFIX.$table;
652 $sql .=
" WHERE ".$field.
" = ".((int) $this->
id);
654 dol_syslog(get_class($this).
"::getSumPayment", LOG_DEBUG);
655 $resql = $this->db->query($sql);
659 $obj = $this->db->fetch_object($resql);
661 $amount = $obj->amount ? $obj->amount : 0;
664 $this->db->free($resql);
667 $this->error = $this->db->lasterror();
680 $sql =
'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
681 $sql .=
' l.tms as datem';
682 $sql .=
' WHERE l.rowid = '.((int) $id);
684 dol_syslog(get_class($this).
'::info', LOG_DEBUG);
685 $result = $this->db->query($sql);
688 if ($this->db->num_rows($result)) {
689 $obj = $this->db->fetch_object($result);
691 $this->
id = $obj->rowid;
693 $this->user_creation_id = $obj->fk_user_author;
694 $this->user_modification_id = $obj->fk_user_modif;
695 $this->date_creation = $this->db->jdate($obj->datec);
696 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
698 $this->db->free($result);
701 $this->db->free($result);
705 $this->error = $this->db->lasterror();
721 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
723 $return =
'<div class="box-flex-item box-flex-grow-zero">';
724 $return .=
'<div class="info-box info-box-sm">';
725 $return .=
'<span class="info-box-icon bg-infobox-action">';
727 $return .=
'</span>';
728 $return .=
'<div class="info-box-content">';
729 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl(1) : $this->ref).
'</span>';
730 if ($selected >= 0) {
731 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
733 if (property_exists($this,
'capital')) {
734 $return .=
' | <span class="opacitymedium">'.$langs->trans(
"Amount").
'</span> : <span class="info-box-label amount">'.
price($this->capital).
'</span>';
736 if (property_exists($this,
'datestart')) {
737 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateStart").
'</span> : <span class="info-box-label">'.
dol_print_date($this->db->jdate($this->datestart),
'day').
'</span>';
739 if (property_exists($this,
'dateend')) {
740 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateEnd").
'</span> : <span class="info-box-label">'.
dol_print_date($this->db->jdate($this->dateend),
'day').
'</span>';
743 if (method_exists($this,
'LibStatut')) {
744 $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.