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();
235 public function create($user, $notrigger = 0)
237 global
$conf, $langs;
244 $newcapital =
price2num($this->capital,
'MT');
245 if (empty($this->insurance_amount)) {
246 $this->insurance_amount = 0;
248 $newinsuranceamount =
price2num($this->insurance_amount,
'MT');
249 if (isset($this->note_private)) {
250 $this->note_private = trim($this->note_private);
252 if (isset($this->note_public)) {
253 $this->note_public = trim($this->note_public);
255 if (isset($this->account_capital)) {
256 $this->account_capital = trim($this->account_capital);
258 if (isset($this->account_insurance)) {
259 $this->account_insurance = trim($this->account_insurance);
261 if (isset($this->account_interest)) {
262 $this->account_interest = trim($this->account_interest);
264 if (isset($this->fk_bank)) {
265 $this->fk_bank = (int) $this->fk_bank;
267 if (isset($this->fk_user_creat)) {
268 $this->fk_user_creat = (int) $this->fk_user_creat;
270 if (isset($this->fk_user_modif)) {
271 $this->fk_user_modif = (int) $this->fk_user_modif;
273 if (isset($this->fk_project)) {
274 $this->fk_project = (int) $this->fk_project;
278 if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
279 $this->error =
"ErrorBadParameter";
282 if (
isModEnabled(
'accounting') && empty($this->account_capital)) {
283 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyCapitalCode"));
286 if (
isModEnabled(
'accounting') && empty($this->account_insurance)) {
287 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInsuranceCode"));
290 if (
isModEnabled(
'accounting') && empty($this->account_interest)) {
291 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInterestCode"));
297 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
298 $sql .=
" accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
299 $sql .=
" datec, fk_projet, fk_user_author, insurance_amount)";
300 $sql .=
" VALUES ('".$this->db->escape($this->label).
"',";
301 $sql .=
" '".$this->db->escape((
string) $this->fk_bank).
"',";
302 $sql .=
" '".price2num($newcapital).
"',";
303 $sql .=
" '".$this->db->idate($this->datestart).
"',";
304 $sql .=
" '".$this->db->idate($this->dateend).
"',";
305 $sql .=
" '".$this->db->escape((
string) $this->nbterm).
"',";
306 $sql .=
" '".$this->db->escape((
string) $this->rate).
"',";
307 $sql .=
" '".$this->db->escape($this->note_private).
"',";
308 $sql .=
" '".$this->db->escape($this->note_public).
"',";
309 $sql .=
" '".$this->db->escape($this->account_capital).
"',";
310 $sql .=
" '".$this->db->escape($this->account_insurance).
"',";
311 $sql .=
" '".$this->db->escape($this->account_interest).
"',";
312 $sql .=
" ".((int)
$conf->entity).
",";
313 $sql .=
" '".$this->db->idate($now).
"',";
314 $sql .=
" ".(empty($this->fk_project) ?
'NULL' : ((int) $this->fk_project)).
",";
315 $sql .=
" ".((int) $user->id).
",";
316 $sql .=
" '".price2num($newinsuranceamount).
"'";
319 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
320 $resql = $this->db->query($sql);
322 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"loan");
326 $result = $this->call_trigger(
'LOAN_CREATE', $user);
334 $this->db->rollback();
342 $this->error = $this->db->error();
343 $this->db->rollback();
356 public function delete($user, $notrigger = 0)
363 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
364 $account =
new Account($this->db);
365 $lines_url = $account->get_url(0, $this->
id,
'loan');
368 foreach ($lines_url as $line_url) {
371 $accountline->fetch($line_url[
'fk_bank']);
372 $result = $accountline->delete_urls($user);
374 $this->errors = array_merge($this->errors, [$accountline->error], $accountline->errors);
382 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan where fk_loan=".((int) $this->
id);
383 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
384 $resql = $this->db->query($sql);
387 $this->error = $this->db->lasterror();
392 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"loan where rowid=".((int) $this->
id);
393 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
394 $resql = $this->db->query($sql);
397 $this->error = $this->db->lasterror();
401 if (!$error && !$notrigger) {
403 $result = $this->call_trigger(
'LOAN_DELETE', $user);
414 $this->db->rollback();
427 public function update($user, $notrigger = 0)
431 if (!is_numeric($this->nbterm)) {
432 $this->error =
'BadValueForParameterForNbTerm';
436 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan";
437 $sql .=
" SET label='".$this->db->escape($this->label).
"',";
438 $sql .=
" capital='".$this->db->escape(
price2num($this->capital)).
"',";
439 $sql .=
" datestart='".$this->db->idate($this->datestart).
"',";
440 $sql .=
" dateend='".$this->db->idate($this->dateend).
"',";
441 $sql .=
" nbterm=".((float) $this->nbterm).
",";
442 $sql .=
" rate=".((float) $this->rate).
",";
443 $sql .=
" accountancy_account_capital = '".$this->db->escape($this->account_capital).
"',";
444 $sql .=
" accountancy_account_insurance = '".$this->db->escape($this->account_insurance).
"',";
445 $sql .=
" accountancy_account_interest = '".$this->db->escape($this->account_interest).
"',";
446 $sql .=
" fk_projet=".(empty($this->fk_project) ?
'NULL' : ((int) $this->fk_project)).
",";
447 $sql .=
" fk_user_modif = ".((int) $user->id).
",";
448 $sql .=
" insurance_amount = '".price2num($this->db->escape((
string) $this->insurance_amount)).
"'";
449 $sql .=
" WHERE rowid=".((int) $this->
id);
451 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
452 $resql = $this->db->query($sql);
456 $result = $this->call_trigger(
'LOAN_MODIFY', $user);
458 $this->db->rollback();
467 $this->error = $this->db->error();
468 $this->db->rollback();
481 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
482 $sql .=
" paid = ".((int) $this::STATUS_PAID);
483 $sql .=
" WHERE rowid = ".((int) $this->
id);
485 $return = $this->db->query($sql);
488 $this->paid = $this::STATUS_PAID;
491 $this->error = $this->db->lasterror();
508 dol_syslog(get_class($this).
"::set_started is deprecated, use setStarted instead", LOG_NOTICE);
520 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
521 $sql .=
" paid = ".((int) $this::STATUS_STARTED);
522 $sql .=
" WHERE rowid = ".((int) $this->
id);
524 $return = $this->db->query($sql);
527 $this->paid = $this::STATUS_STARTED;
530 $this->error = $this->db->lasterror();
543 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
544 $sql .=
" paid = ".((int) $this::STATUS_UNPAID);
545 $sql .=
" WHERE rowid = ".((int) $this->
id);
547 $return = $this->db->query($sql);
550 $this->paid = $this::STATUS_UNPAID;
553 $this->error = $this->db->lasterror();
567 return $this->
LibStatut($this->paid, $mode, $alreadypaid);
579 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
585 $langs->loadLangs(array(
"customers",
"bills"));
587 unset($this->labelStatus);
591 $this->labelStatus = array();
592 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
593 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
594 $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
595 if ($status == 0 && $alreadypaid > 0) {
596 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
598 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
599 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
600 $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
601 if ($status == 0 && $alreadypaid > 0) {
602 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
606 $statusType =
'status1';
607 if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
608 $statusType =
'status3';
611 $statusType =
'status6';
614 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
629 public function getNomUrl($withpicto = 0, $maxlen = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
631 global
$conf, $langs, $hookmanager;
635 $label =
'<u>'.$langs->trans(
"ShowLoan").
'</u>';
636 if (!empty($this->
ref)) {
637 $label .=
'<br><strong>'.$langs->trans(
'Ref').
':</strong> '.$this->ref;
639 if (!empty($this->label)) {
640 $label .=
'<br><strong>'.$langs->trans(
'Label').
':</strong> '.$this->label;
643 $label .=
'<br><strong>'.$langs->trans(
"DateStart").
':</strong> '.
dol_print_date($this->datestart,
'day');
646 $label .=
'<br><strong>'.$langs->trans(
"DateEnd").
':</strong> '.
dol_print_date($this->dateend,
'day');
649 $baseurl = DOL_URL_ROOT.
'/loan/card.php';
650 $query = [
'id' => $this->id];
651 if ($option !=
'nolink') {
653 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
654 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
655 $add_save_lastsearch_values = 1;
657 if ($add_save_lastsearch_values) {
658 $query += [
'save_lastsearch_values' => 1];
664 if (empty($notooltip)) {
666 $label = $langs->trans(
"ShowLoan");
667 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
669 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
670 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
672 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
675 $linkstart =
'<a href="'.$url.
'"';
676 $linkstart .= $linkclose.
'>';
679 $result .= $linkstart;
681 $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);
683 if ($withpicto != 2) {
684 $result .= ($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref);
689 $hookmanager->initHooks(array($this->element .
'dao'));
690 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
691 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
693 $result = $hookmanager->resPrint;
695 $result .= $hookmanager->resPrint;
714 $this->label =
'SPECIMEN';
716 $this->account_capital =
'16';
717 $this->account_insurance =
'616';
718 $this->account_interest =
'518';
719 $this->datestart = $now;
720 $this->dateend = $now + (3600 * 24 * 365);
721 $this->note_public =
'SPECIMEN';
722 $this->capital = 20000.80;
736 $sql =
"SELECT sum(amount_capital) as amount";
737 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan";
738 $sql .=
" WHERE fk_loan = ".((int) $this->
id);
740 dol_syslog(get_class($this).
"::getSumPayment", LOG_DEBUG);
742 $resql = $this->db->query($sql);
746 $obj = $this->db->fetch_object($resql);
748 $amount = $obj->amount ? $obj->amount : 0;
751 $this->db->free($resql);
754 $this->error = $this->db->lasterror();
767 $sql =
'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
768 $sql .=
' l.tms as datem';
769 $sql .=
' WHERE l.rowid = '.((int) $id);
771 dol_syslog(get_class($this).
'::info', LOG_DEBUG);
772 $result = $this->db->query($sql);
775 if ($this->db->num_rows($result)) {
776 $obj = $this->db->fetch_object($result);
778 $this->
id = $obj->rowid;
780 $this->user_creation_id = $obj->fk_user_author;
781 $this->user_modification_id = $obj->fk_user_modif;
782 $this->date_creation = $this->db->jdate($obj->datec);
783 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
785 $this->db->free($result);
788 $this->db->free($result);
792 $this->error = $this->db->lasterror();
808 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
810 $return =
'<div class="box-flex-item box-flex-grow-zero">';
811 $return .=
'<div class="info-box info-box-sm">';
812 $return .=
'<span class="info-box-icon bg-infobox-action">';
813 $return .= img_picto(
'', $this->picto);
814 $return .=
'</span>';
815 $return .=
'<div class="info-box-content">';
816 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.$this->getNomUrl(1).
'</span>';
817 if ($selected >= 0) {
818 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
820 if (!empty($this->capital)) {
821 $return .=
' | <span class="opacitymedium">'.$langs->trans(
"Amount").
'</span> : <span class="info-box-label amount">'.
price($this->capital).
'</span>';
824 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateStart").
'</span> : <span class="info-box-label">'.
dol_print_date($this->datestart,
'day').
'</span>';
827 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateEnd").
'</span> : <span class="info-box-label">'.
dol_print_date($this->dateend,
'day').
'</span>';
830 $return .=
'<br><div class="info-box-status">'.$this->getLibStatut(3, (
float) $this->totalpaid).
'</div>';
Class to manage bank accounts.
Class to manage bank transaction lines.
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)
getSumPayment()
Return amount of payments already done.
update($user, $notrigger=0)
Update loan.
set_started($user)
Tag loan as payment started.
create($user, $notrigger=0)
Create a loan into database.
getLibStatut($mode=0, $alreadypaid=-1)
Return label of loan status (unpaid, paid)
dol_now($mode='gmt')
Return date for now.
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.
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.