25require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
36 public $element =
'loan';
41 public $table_element =
'loan';
46 public $picto =
'money-bill-alt';
86 public $frequency = 12;
91 public $interest_basis = 0;
101 public $account_capital;
106 public $account_insurance;
111 public $account_interest;
116 public $accountancy_account_capital;
121 public $accountancy_account_insurance;
126 public $accountancy_account_interest;
131 public $insurance_amount;
136 public $balloon_amount = 0;
141 public $charge_type = 0;
146 public $charge_per_payment = 0;
156 public $fk_user_creat;
161 public $fk_user_modif;
171 const STATUS_UNPAID = 0;
176 const STATUS_PAID = 1;
181 const STATUS_STARTED = 2;
202 $sql =
"SELECT l.rowid, l.entity, l.label, l.capital, l.datestart, l.dateend, l.nbterm, l.rate, l.frequency, l.interest_basis, l.note_private, l.note_public, l.insurance_amount, l.balloon_amount, l.charge_type, l.charge_per_payment,";
203 $sql .=
" l.paid, l.fk_bank, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
204 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
205 $sql .=
" WHERE l.rowid = ".((int) $id);
207 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
208 $resql = $this->db->query($sql);
210 if ($this->db->num_rows($resql)) {
211 $obj = $this->db->fetch_object($resql);
213 $this->
id = $obj->rowid;
214 $this->entity = $obj->entity;
215 $this->
ref = $obj->rowid;
216 $this->datestart = $this->db->jdate($obj->datestart);
217 $this->dateend = $this->db->jdate($obj->dateend);
218 $this->label = $obj->label;
219 $this->capital = $obj->capital;
220 $this->nbterm = $obj->nbterm;
221 $this->rate = $obj->rate;
222 $this->frequency = ((int) $obj->frequency > 0 ? (
int) $obj->frequency : 12);
223 $this->interest_basis = (int) $obj->interest_basis;
224 $this->note_private = $obj->note_private;
225 $this->note_public = $obj->note_public;
226 $this->insurance_amount = $obj->insurance_amount;
227 $this->balloon_amount = (float) $obj->balloon_amount;
228 $this->charge_type = (int) $obj->charge_type;
229 $this->charge_per_payment = (int) $obj->charge_per_payment;
230 $this->paid = $obj->paid;
231 $this->fk_bank = $obj->fk_bank;
233 $this->account_capital = $obj->accountancy_account_capital;
234 $this->account_insurance = $obj->accountancy_account_insurance;
235 $this->account_interest = $obj->accountancy_account_interest;
236 $this->fk_project = $obj->fk_project;
238 $this->db->free($resql);
241 $this->db->free($resql);
245 $this->error = $this->db->lasterror();
258 public function create($user, $notrigger = 0)
260 global
$conf, $langs;
267 $newcapital =
price2num($this->capital,
'MT');
268 if (empty($this->insurance_amount)) {
269 $this->insurance_amount = 0;
271 $newinsuranceamount =
price2num($this->insurance_amount,
'MT');
272 if (isset($this->note_private)) {
273 $this->note_private = trim($this->note_private);
275 if (isset($this->note_public)) {
276 $this->note_public = trim($this->note_public);
278 if (isset($this->account_capital)) {
279 $this->account_capital = trim($this->account_capital);
281 if (isset($this->account_insurance)) {
282 $this->account_insurance = trim($this->account_insurance);
284 if (isset($this->account_interest)) {
285 $this->account_interest = trim($this->account_interest);
287 if (isset($this->fk_bank)) {
288 $this->fk_bank = (int) $this->fk_bank;
290 if (isset($this->fk_user_creat)) {
291 $this->fk_user_creat = (int) $this->fk_user_creat;
293 if (isset($this->fk_user_modif)) {
294 $this->fk_user_modif = (int) $this->fk_user_modif;
296 if (isset($this->fk_project)) {
297 $this->fk_project = (int) $this->fk_project;
301 if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
302 $this->error =
"ErrorBadParameter";
305 if (
isModEnabled(
'accounting') && empty($this->account_capital)) {
306 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyCapitalCode"));
309 if (
isModEnabled(
'accounting') && empty($this->account_insurance)) {
310 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInsuranceCode"));
313 if (
isModEnabled(
'accounting') && empty($this->account_interest)) {
314 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInterestCode"));
320 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, frequency, interest_basis, note_private, note_public,";
321 $sql .=
" accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
322 $sql .=
" datec, fk_projet, fk_user_author, insurance_amount, balloon_amount, charge_type, charge_per_payment)";
323 $sql .=
" VALUES ('".$this->db->escape($this->label).
"',";
324 $sql .=
" '".$this->db->escape((
string) $this->fk_bank).
"',";
325 $sql .=
" '".price2num($newcapital).
"',";
326 $sql .=
" '".$this->db->idate($this->datestart).
"',";
327 $sql .=
" '".$this->db->idate($this->dateend).
"',";
328 $sql .=
" '".$this->db->escape((
string) $this->nbterm).
"',";
329 $sql .=
" '".$this->db->escape((
string) $this->rate).
"',";
330 $sql .=
" ".((int) $this->frequency > 0 ? (
int) $this->frequency : 12).
",";
331 $sql .=
" ".((int) $this->interest_basis ? 1 : 0).
",";
332 $sql .=
" '".$this->db->escape($this->note_private).
"',";
333 $sql .=
" '".$this->db->escape($this->note_public).
"',";
334 $sql .=
" '".$this->db->escape($this->account_capital).
"',";
335 $sql .=
" '".$this->db->escape($this->account_insurance).
"',";
336 $sql .=
" '".$this->db->escape($this->account_interest).
"',";
337 $sql .=
" ".((int)
$conf->entity).
",";
338 $sql .=
" '".$this->db->idate($now).
"',";
339 $sql .=
" ".(empty($this->fk_project) ?
'NULL' : ((int) $this->fk_project)).
",";
340 $sql .=
" ".((int) $user->id).
",";
341 $sql .=
" '".price2num($newinsuranceamount).
"',";
342 $sql .=
" ".((float)
price2num((
float) $this->balloon_amount,
'MT')).
",";
343 $sql .=
" ".((int) $this->charge_type).
",";
344 $sql .=
" ".((int) $this->charge_per_payment ? 1 : 0);
347 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
348 $resql = $this->db->query($sql);
350 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"loan");
354 $result = $this->call_trigger(
'LOAN_CREATE', $user);
362 $this->db->rollback();
370 $this->error = $this->db->error();
371 $this->db->rollback();
384 public function delete($user, $notrigger = 0)
391 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
392 $account =
new Account($this->db);
393 $lines_url = $account->get_url(0, $this->
id,
'loan');
396 foreach ($lines_url as $line_url) {
399 $accountline->fetch($line_url[
'fk_bank']);
400 $result = $accountline->delete_urls($user);
402 $this->errors = array_merge($this->errors, [$accountline->error], $accountline->errors);
410 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"loan_change where fk_loan=".((int) $this->
id);
411 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
412 if (!$this->db->query($sql)) {
414 $this->error = $this->db->lasterror();
418 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan where fk_loan=".((int) $this->
id);
419 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
420 $resql = $this->db->query($sql);
423 $this->error = $this->db->lasterror();
428 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"loan where rowid=".((int) $this->
id);
429 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
430 $resql = $this->db->query($sql);
433 $this->error = $this->db->lasterror();
437 if (!$error && !$notrigger) {
439 $result = $this->call_trigger(
'LOAN_DELETE', $user);
450 $this->db->rollback();
463 public function update($user, $notrigger = 0)
467 if (!is_numeric($this->nbterm)) {
468 $this->error =
'BadValueForParameterForNbTerm';
472 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan";
473 $sql .=
" SET label='".$this->db->escape($this->label).
"',";
474 $sql .=
" capital='".$this->db->escape(
price2num($this->capital)).
"',";
475 $sql .=
" datestart='".$this->db->idate($this->datestart).
"',";
476 $sql .=
" dateend='".$this->db->idate($this->dateend).
"',";
477 $sql .=
" nbterm=".((float) $this->nbterm).
",";
478 $sql .=
" rate=".((float) $this->rate).
",";
479 $sql .=
" frequency=".((int) $this->frequency > 0 ? (
int) $this->frequency : 12).
",";
480 $sql .=
" interest_basis=".((int) $this->interest_basis ? 1 : 0).
",";
481 $sql .=
" accountancy_account_capital = '".$this->db->escape($this->account_capital).
"',";
482 $sql .=
" accountancy_account_insurance = '".$this->db->escape($this->account_insurance).
"',";
483 $sql .=
" accountancy_account_interest = '".$this->db->escape($this->account_interest).
"',";
484 $sql .=
" fk_projet=".(empty($this->fk_project) ?
'NULL' : ((int) $this->fk_project)).
",";
485 $sql .=
" fk_user_modif = ".((int) $user->id).
",";
486 $sql .=
" insurance_amount = '".price2num($this->db->escape((
string) $this->insurance_amount)).
"',";
487 $sql .=
" balloon_amount = ".((float)
price2num((
float) $this->balloon_amount,
'MT')).
",";
488 $sql .=
" charge_type = ".((int) $this->charge_type).
",";
489 $sql .=
" charge_per_payment = ".((int) $this->charge_per_payment ? 1 : 0);
490 $sql .=
" WHERE rowid=".((int) $this->
id);
492 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
493 $resql = $this->db->query($sql);
497 $result = $this->call_trigger(
'LOAN_MODIFY', $user);
499 $this->db->rollback();
508 $this->error = $this->db->error();
509 $this->db->rollback();
522 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
523 $sql .=
" paid = ".((int) $this::STATUS_PAID);
524 $sql .=
" WHERE rowid = ".((int) $this->
id);
526 $return = $this->db->query($sql);
529 $this->paid = $this::STATUS_PAID;
532 $this->error = $this->db->lasterror();
549 dol_syslog(get_class($this).
"::set_started is deprecated, use setStarted instead", LOG_NOTICE);
561 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
562 $sql .=
" paid = ".((int) $this::STATUS_STARTED);
563 $sql .=
" WHERE rowid = ".((int) $this->
id);
565 $return = $this->db->query($sql);
568 $this->paid = $this::STATUS_STARTED;
571 $this->error = $this->db->lasterror();
584 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
585 $sql .=
" paid = ".((int) $this::STATUS_UNPAID);
586 $sql .=
" WHERE rowid = ".((int) $this->
id);
588 $return = $this->db->query($sql);
591 $this->paid = $this::STATUS_UNPAID;
594 $this->error = $this->db->lasterror();
608 return $this->
LibStatut($this->paid, $mode, $alreadypaid);
620 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
626 $langs->loadLangs(array(
"customers",
"bills"));
628 unset($this->labelStatus);
632 $this->labelStatus = array();
633 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
634 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
635 $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
636 if ($status == 0 && $alreadypaid > 0) {
637 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
639 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
640 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
641 $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
642 if ($status == 0 && $alreadypaid > 0) {
643 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
647 $statusType =
'status1';
648 if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
649 $statusType =
'status3';
652 $statusType =
'status6';
655 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
670 public function getNomUrl($withpicto = 0, $maxlen = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
672 global
$conf, $langs, $hookmanager;
676 $label =
'<u>'.$langs->trans(
"ShowLoan").
'</u>';
677 if (!empty($this->
ref)) {
678 $label .=
'<br><strong>'.$langs->trans(
'Ref').
':</strong> '.$this->ref;
680 if (!empty($this->label)) {
681 $label .=
'<br><strong>'.$langs->trans(
'Label').
':</strong> '.$this->label;
684 $label .=
'<br><strong>'.$langs->trans(
"DateStart").
':</strong> '.
dol_print_date($this->datestart,
'day');
687 $label .=
'<br><strong>'.$langs->trans(
"DateEnd").
':</strong> '.
dol_print_date($this->dateend,
'day');
690 $baseurl = DOL_URL_ROOT.
'/loan/card.php';
691 $query = [
'id' => $this->id];
692 if ($option !=
'nolink') {
694 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
695 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
696 $add_save_lastsearch_values = 1;
698 if ($add_save_lastsearch_values) {
699 $query += [
'save_lastsearch_values' => 1];
705 if (empty($notooltip)) {
707 $label = $langs->trans(
"ShowLoan");
708 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
710 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
711 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
713 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
716 $linkstart =
'<a href="'.$url.
'"';
717 $linkstart .= $linkclose.
'>';
720 $result .= $linkstart;
722 $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);
724 if ($withpicto != 2) {
725 $result .= ($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref);
730 $hookmanager->initHooks(array($this->element .
'dao'));
731 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
732 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
734 $result = $hookmanager->resPrint;
736 $result .= $hookmanager->resPrint;
755 $this->label =
'SPECIMEN';
757 $this->account_capital =
'16';
758 $this->account_insurance =
'616';
759 $this->account_interest =
'518';
760 $this->datestart = $now;
761 $this->dateend = $now + (3600 * 24 * 365);
762 $this->note_public =
'SPECIMEN';
763 $this->capital = 20000.80;
777 $sql =
"SELECT sum(amount_capital) as amount";
778 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan";
779 $sql .=
" WHERE fk_loan = ".((int) $this->
id);
781 dol_syslog(get_class($this).
"::getSumPayment", LOG_DEBUG);
783 $resql = $this->db->query($sql);
787 $obj = $this->db->fetch_object($resql);
789 $amount = $obj->amount ? $obj->amount : 0;
792 $this->db->free($resql);
795 $this->error = $this->db->lasterror();
808 $sql =
'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
809 $sql .=
' l.tms as datem';
810 $sql .=
' WHERE l.rowid = '.((int) $id);
812 dol_syslog(get_class($this).
'::info', LOG_DEBUG);
813 $result = $this->db->query($sql);
816 if ($this->db->num_rows($result)) {
817 $obj = $this->db->fetch_object($result);
819 $this->
id = $obj->rowid;
821 $this->user_creation_id = $obj->fk_user_author;
822 $this->user_modification_id = $obj->fk_user_modif;
823 $this->date_creation = $this->db->jdate($obj->datec);
824 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
826 $this->db->free($result);
829 $this->db->free($result);
833 $this->error = $this->db->lasterror();
849 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
851 $return =
'<div class="box-flex-item box-flex-grow-zero">';
852 $return .=
'<div class="info-box info-box-sm">';
853 $return .=
'<span class="info-box-icon bg-infobox-action">';
854 $return .= img_picto(
'', $this->picto);
855 $return .=
'</span>';
856 $return .=
'<div class="info-box-content">';
857 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.$this->getNomUrl(1).
'</span>';
858 if ($selected >= 0) {
859 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
861 if (!empty($this->capital)) {
862 $return .=
' | <span class="opacitymedium">'.$langs->trans(
"Amount").
'</span> : <span class="info-box-label amount">'.
price($this->capital).
'</span>';
865 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateStart").
'</span> : <span class="info-box-label">'.
dol_print_date($this->datestart,
'day').
'</span>';
868 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateEnd").
'</span> : <span class="info-box-label">'.
dol_print_date($this->dateend,
'day').
'</span>';
871 $return .=
'<br><div class="info-box-status">'.$this->getLibStatut(3, (
float) $this->totalpaid).
'</div>';
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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, $currencycode='')
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.