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;
158 const STATUS_UNPAID = 0;
163 const STATUS_PAID = 1;
168 const STATUS_STARTED = 2;
189 $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,";
190 $sql .=
" l.paid, l.fk_bank, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
191 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
192 $sql .=
" WHERE l.rowid = ".((int) $id);
194 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
195 $resql = $this->db->query($sql);
197 if ($this->db->num_rows($resql)) {
198 $obj = $this->db->fetch_object($resql);
200 $this->
id = $obj->rowid;
201 $this->
ref = $obj->rowid;
202 $this->datestart = $this->db->jdate($obj->datestart);
203 $this->dateend = $this->db->jdate($obj->dateend);
204 $this->label = $obj->label;
205 $this->capital = $obj->capital;
206 $this->nbterm = $obj->nbterm;
207 $this->rate = $obj->rate;
208 $this->note_private = $obj->note_private;
209 $this->note_public = $obj->note_public;
210 $this->insurance_amount = $obj->insurance_amount;
211 $this->paid = $obj->paid;
212 $this->fk_bank = $obj->fk_bank;
214 $this->account_capital = $obj->accountancy_account_capital;
215 $this->account_insurance = $obj->accountancy_account_insurance;
216 $this->account_interest = $obj->accountancy_account_interest;
217 $this->fk_project = $obj->fk_project;
219 $this->db->free($resql);
222 $this->db->free($resql);
226 $this->error = $this->db->lasterror();
240 global
$conf, $langs;
247 $newcapital =
price2num($this->capital,
'MT');
248 if (empty($this->insurance_amount)) {
249 $this->insurance_amount = 0;
251 $newinsuranceamount =
price2num($this->insurance_amount,
'MT');
252 if (isset($this->note_private)) {
253 $this->note_private = trim($this->note_private);
255 if (isset($this->note_public)) {
256 $this->note_public = trim($this->note_public);
258 if (isset($this->account_capital)) {
259 $this->account_capital = trim($this->account_capital);
261 if (isset($this->account_insurance)) {
262 $this->account_insurance = trim($this->account_insurance);
264 if (isset($this->account_interest)) {
265 $this->account_interest = trim($this->account_interest);
267 if (isset($this->fk_bank)) {
268 $this->fk_bank = (int) $this->fk_bank;
270 if (isset($this->fk_user_creat)) {
271 $this->fk_user_creat = (int) $this->fk_user_creat;
273 if (isset($this->fk_user_modif)) {
274 $this->fk_user_modif = (int) $this->fk_user_modif;
276 if (isset($this->fk_project)) {
277 $this->fk_project = (int) $this->fk_project;
281 if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
282 $this->error =
"ErrorBadParameter";
285 if (isModEnabled(
'accounting') && empty($this->account_capital)) {
286 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyCapitalCode"));
289 if (isModEnabled(
'accounting') && empty($this->account_insurance)) {
290 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInsuranceCode"));
293 if (isModEnabled(
'accounting') && empty($this->account_interest)) {
294 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInterestCode"));
300 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
301 $sql .=
" accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
302 $sql .=
" datec, fk_projet, fk_user_author, insurance_amount)";
303 $sql .=
" VALUES ('".$this->db->escape($this->label).
"',";
304 $sql .=
" '".$this->db->escape($this->fk_bank).
"',";
305 $sql .=
" '".price2num($newcapital).
"',";
306 $sql .=
" '".$this->db->idate($this->datestart).
"',";
307 $sql .=
" '".$this->db->idate($this->dateend).
"',";
308 $sql .=
" '".$this->db->escape($this->nbterm).
"',";
309 $sql .=
" '".$this->db->escape($this->rate).
"',";
310 $sql .=
" '".$this->db->escape($this->note_private).
"',";
311 $sql .=
" '".$this->db->escape($this->note_public).
"',";
312 $sql .=
" '".$this->db->escape($this->account_capital).
"',";
313 $sql .=
" '".$this->db->escape($this->account_insurance).
"',";
314 $sql .=
" '".$this->db->escape($this->account_interest).
"',";
315 $sql .=
" ".((int)
$conf->entity).
",";
316 $sql .=
" '".$this->db->idate($now).
"',";
317 $sql .=
" ".(empty($this->fk_project) ?
'NULL' : $this->fk_project).
",";
318 $sql .=
" ".((int) $user->id).
",";
319 $sql .=
" '".price2num($newinsuranceamount).
"'";
322 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
323 $resql = $this->db->query($sql);
325 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"loan");
331 $this->error = $this->db->error();
332 $this->db->rollback();
344 public function delete($user)
351 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
352 $account =
new Account($this->db);
353 $lines_url = $account->get_url(0, $this->
id,
'loan');
356 foreach ($lines_url as $line_url) {
359 $accountline->fetch($line_url[
'fk_bank']);
360 $result = $accountline->delete_urls($user);
369 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan where fk_loan=".((int) $this->
id);
370 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
371 $resql = $this->db->query($sql);
374 $this->error = $this->db->lasterror();
379 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"loan where rowid=".((int) $this->
id);
380 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
381 $resql = $this->db->query($sql);
384 $this->error = $this->db->lasterror();
392 $this->db->rollback();
408 if (!is_numeric($this->nbterm)) {
409 $this->error =
'BadValueForParameterForNbTerm';
413 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan";
414 $sql .=
" SET label='".$this->db->escape($this->label).
"',";
415 $sql .=
" capital='".price2num($this->db->escape($this->capital)).
"',";
416 $sql .=
" datestart='".$this->db->idate($this->datestart).
"',";
417 $sql .=
" dateend='".$this->db->idate($this->dateend).
"',";
418 $sql .=
" nbterm=".((float) $this->nbterm).
",";
419 $sql .=
" rate=".((float) $this->rate).
",";
420 $sql .=
" accountancy_account_capital = '".$this->db->escape($this->account_capital).
"',";
421 $sql .=
" accountancy_account_insurance = '".$this->db->escape($this->account_insurance).
"',";
422 $sql .=
" accountancy_account_interest = '".$this->db->escape($this->account_interest).
"',";
423 $sql .=
" fk_projet=".(empty($this->fk_project) ?
'NULL' : ((int) $this->fk_project)).
",";
424 $sql .=
" fk_user_modif = ".((int) $user->id).
",";
425 $sql .=
" insurance_amount = '".price2num($this->db->escape($this->insurance_amount)).
"'";
426 $sql .=
" WHERE rowid=".((int) $this->
id);
428 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
429 $resql = $this->db->query($sql);
434 $this->error = $this->db->error();
435 $this->db->rollback();
448 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
449 $sql .=
" paid = ".((int) $this::STATUS_PAID);
450 $sql .=
" WHERE rowid = ".((int) $this->
id);
452 $return = $this->db->query($sql);
455 $this->paid = $this::STATUS_PAID;
458 $this->error = $this->db->lasterror();
475 dol_syslog(get_class($this).
"::set_started is deprecated, use setStarted instead", LOG_NOTICE);
487 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
488 $sql .=
" paid = ".((int) $this::STATUS_STARTED);
489 $sql .=
" WHERE rowid = ".((int) $this->
id);
491 $return = $this->db->query($sql);
494 $this->paid = $this::STATUS_STARTED;
497 $this->error = $this->db->lasterror();
510 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
511 $sql .=
" paid = ".((int) $this::STATUS_UNPAID);
512 $sql .=
" WHERE rowid = ".((int) $this->
id);
514 $return = $this->db->query($sql);
517 $this->paid = $this::STATUS_UNPAID;
520 $this->error = $this->db->lasterror();
534 return $this->
LibStatut($this->paid, $mode, $alreadypaid);
546 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
552 $langs->loadLangs(array(
"customers",
"bills"));
554 unset($this->labelStatus);
558 $this->labelStatus = array();
559 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
560 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
561 $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
562 if ($status == 0 && $alreadypaid > 0) {
563 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
565 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
566 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
567 $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
568 if ($status == 0 && $alreadypaid > 0) {
569 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
573 $statusType =
'status1';
574 if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
575 $statusType =
'status3';
578 $statusType =
'status6';
581 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
596 public function getNomUrl($withpicto = 0, $maxlen = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
598 global
$conf, $langs, $hookmanager;
602 $label =
'<u>'.$langs->trans(
"ShowLoan").
'</u>';
603 if (!empty($this->
ref)) {
604 $label .=
'<br><strong>'.$langs->trans(
'Ref').
':</strong> '.$this->ref;
606 if (!empty($this->label)) {
607 $label .=
'<br><strong>'.$langs->trans(
'Label').
':</strong> '.$this->label;
610 $label .=
'<br><strong>'.$langs->trans(
"DateStart").
':</strong> '.
dol_print_date($this->datestart,
'day');
613 $label .=
'<br><strong>'.$langs->trans(
"DateEnd").
':</strong> '.
dol_print_date($this->dateend,
'day');
616 $url = DOL_URL_ROOT.
'/loan/card.php?id='.$this->id;
618 if ($option !=
'nolink') {
620 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
621 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
622 $add_save_lastsearch_values = 1;
624 if ($add_save_lastsearch_values) {
625 $url .=
'&save_lastsearch_values=1';
630 if (empty($notooltip)) {
632 $label = $langs->trans(
"ShowMyObject");
633 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
635 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
636 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
638 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
641 $linkstart =
'<a href="'.$url.
'"';
642 $linkstart .= $linkclose.
'>';
645 $result .= $linkstart;
647 $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);
649 if ($withpicto != 2) {
650 $result .= ($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref);
655 $hookmanager->initHooks(array($this->element .
'dao'));
656 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
657 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
659 $result = $hookmanager->resPrint;
661 $result .= $hookmanager->resPrint;
680 $this->label =
'SPECIMEN';
682 $this->account_capital =
'16';
683 $this->account_insurance =
'616';
684 $this->account_interest =
'518';
685 $this->datestart = $now;
686 $this->dateend = $now + (3600 * 24 * 365);
687 $this->note_public =
'SPECIMEN';
688 $this->capital = 20000.80;
702 $table =
'payment_loan';
705 $sql =
'SELECT sum(amount_capital) as amount';
706 $sql .=
' FROM '.MAIN_DB_PREFIX.$table;
707 $sql .=
" WHERE ".$field.
" = ".((int) $this->
id);
709 dol_syslog(get_class($this).
"::getSumPayment", LOG_DEBUG);
710 $resql = $this->db->query($sql);
714 $obj = $this->db->fetch_object($resql);
716 $amount = $obj->amount ? $obj->amount : 0;
719 $this->db->free($resql);
722 $this->error = $this->db->lasterror();
735 $sql =
'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
736 $sql .=
' l.tms as datem';
737 $sql .=
' WHERE l.rowid = '.((int) $id);
739 dol_syslog(get_class($this).
'::info', LOG_DEBUG);
740 $result = $this->db->query($sql);
743 if ($this->db->num_rows($result)) {
744 $obj = $this->db->fetch_object($result);
746 $this->
id = $obj->rowid;
748 $this->user_creation_id = $obj->fk_user_author;
749 $this->user_modification_id = $obj->fk_user_modif;
750 $this->date_creation = $this->db->jdate($obj->datec);
751 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
753 $this->db->free($result);
756 $this->db->free($result);
760 $this->error = $this->db->lasterror();
776 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
778 $return =
'<div class="box-flex-item box-flex-grow-zero">';
779 $return .=
'<div class="info-box info-box-sm">';
780 $return .=
'<span class="info-box-icon bg-infobox-action">';
782 $return .=
'</span>';
783 $return .=
'<div class="info-box-content">';
784 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.$this->getNomUrl(1).
'</span>';
785 if ($selected >= 0) {
786 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
788 if (!empty($this->capital)) {
789 $return .=
' | <span class="opacitymedium">'.$langs->trans(
"Amount").
'</span> : <span class="info-box-label amount">'.
price($this->capital).
'</span>';
792 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateStart").
'</span> : <span class="info-box-label">'.
dol_print_date($this->datestart,
'day').
'</span>';
795 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateEnd").
'</span> : <span class="info-box-label">'.
dol_print_date($this->dateend,
'day').
'</span>';
798 $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 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)
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 '.
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_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).
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...