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(
'', $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 $url = DOL_URL_ROOT.
'/loan/card.php?id='.$this->id;
612 if ($option !=
'nolink') {
614 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
615 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
616 $add_save_lastsearch_values = 1;
618 if ($add_save_lastsearch_values) {
619 $url .=
'&save_lastsearch_values=1';
624 if (empty($notooltip)) {
626 $label = $langs->trans(
"ShowMyObject");
627 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
629 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
630 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
632 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
635 $linkstart =
'<a href="'.$url.
'"';
636 $linkstart .= $linkclose.
'>';
639 $result .= $linkstart;
641 $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);
643 if ($withpicto != 2) {
644 $result .= ($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref);
649 $hookmanager->initHooks(array($this->element .
'dao'));
650 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
651 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
653 $result = $hookmanager->resPrint;
655 $result .= $hookmanager->resPrint;
674 $this->label =
'SPECIMEN';
676 $this->account_capital =
'16';
677 $this->account_insurance =
'616';
678 $this->account_interest =
'518';
679 $this->datestart = $now;
680 $this->dateend = $now + (3600 * 24 * 365);
681 $this->note_public =
'SPECIMEN';
682 $this->capital = 20000.80;
696 $table =
'payment_loan';
699 $sql =
'SELECT sum(amount_capital) as amount';
700 $sql .=
' FROM '.MAIN_DB_PREFIX.$table;
701 $sql .=
" WHERE ".$field.
" = ".((int) $this->
id);
703 dol_syslog(get_class($this).
"::getSumPayment", LOG_DEBUG);
704 $resql = $this->db->query($sql);
708 $obj = $this->db->fetch_object($resql);
710 $amount = $obj->amount ? $obj->amount : 0;
713 $this->db->free($resql);
716 $this->error = $this->db->lasterror();
729 $sql =
'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
730 $sql .=
' l.tms as datem';
731 $sql .=
' WHERE l.rowid = '.((int) $id);
733 dol_syslog(get_class($this).
'::info', LOG_DEBUG);
734 $result = $this->db->query($sql);
737 if ($this->db->num_rows($result)) {
738 $obj = $this->db->fetch_object($result);
740 $this->
id = $obj->rowid;
742 $this->user_creation_id = $obj->fk_user_author;
743 $this->user_modification_id = $obj->fk_user_modif;
744 $this->date_creation = $this->db->jdate($obj->datec);
745 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
747 $this->db->free($result);
750 $this->db->free($result);
754 $this->error = $this->db->lasterror();
770 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
772 $return =
'<div class="box-flex-item box-flex-grow-zero">';
773 $return .=
'<div class="info-box info-box-sm">';
774 $return .=
'<span class="info-box-icon bg-infobox-action">';
776 $return .=
'</span>';
777 $return .=
'<div class="info-box-content">';
778 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl(1) : $this->ref).
'</span>';
779 if ($selected >= 0) {
780 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
782 if (property_exists($this,
'capital')) {
783 $return .=
' | <span class="opacitymedium">'.$langs->trans(
"Amount").
'</span> : <span class="info-box-label amount">'.
price($this->capital).
'</span>';
785 if (property_exists($this,
'datestart')) {
786 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateStart").
'</span> : <span class="info-box-label">'.
dol_print_date($this->db->jdate($this->datestart),
'day').
'</span>';
788 if (property_exists($this,
'dateend')) {
789 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateEnd").
'</span> : <span class="info-box-label">'.
dol_print_date($this->db->jdate($this->dateend),
'day').
'</span>';
792 if (method_exists($this,
'LibStatut')) {
793 $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 '.
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 a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.