24 require_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;
73 public $date_creation;
78 public $date_modification;
83 public $date_validation;
85 public $insurance_amount;
95 public $fk_user_creat;
100 public $fk_user_modif;
108 const STATUS_UNPAID = 0;
109 const STATUS_PAID = 1;
110 const STATUS_STARTED = 2;
131 $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,";
132 $sql .=
" l.paid, l.fk_bank, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
133 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
134 $sql .=
" WHERE l.rowid = ".((int) $id);
136 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
140 $obj = $this->
db->fetch_object(
$resql);
142 $this->
id = $obj->rowid;
143 $this->
ref = $obj->rowid;
144 $this->datestart = $this->
db->jdate($obj->datestart);
145 $this->dateend = $this->
db->jdate($obj->dateend);
146 $this->label = $obj->label;
147 $this->capital = $obj->capital;
148 $this->nbterm = $obj->nbterm;
149 $this->rate = $obj->rate;
150 $this->note_private = $obj->note_private;
151 $this->note_public = $obj->note_public;
152 $this->insurance_amount = $obj->insurance_amount;
153 $this->paid = $obj->paid;
154 $this->fk_bank = $obj->fk_bank;
156 $this->account_capital = $obj->accountancy_account_capital;
157 $this->account_insurance = $obj->accountancy_account_insurance;
158 $this->account_interest = $obj->accountancy_account_interest;
159 $this->fk_project = $obj->fk_project;
168 $this->error = $this->
db->lasterror();
182 global $conf, $langs;
189 $newcapital =
price2num($this->capital,
'MT');
190 if (empty($this->insurance_amount)) {
191 $this->insurance_amount = 0;
193 $newinsuranceamount =
price2num($this->insurance_amount,
'MT');
194 if (isset($this->note_private)) {
195 $this->note_private = trim($this->note_private);
197 if (isset($this->note_public)) {
198 $this->note_public = trim($this->note_public);
200 if (isset($this->account_capital)) {
201 $this->account_capital = trim($this->account_capital);
203 if (isset($this->account_insurance)) {
204 $this->account_insurance = trim($this->account_insurance);
206 if (isset($this->account_interest)) {
207 $this->account_interest = trim($this->account_interest);
209 if (isset($this->fk_bank)) {
210 $this->fk_bank = (int) $this->fk_bank;
212 if (isset($this->fk_user_creat)) {
213 $this->fk_user_creat = (int) $this->fk_user_creat;
215 if (isset($this->fk_user_modif)) {
216 $this->fk_user_modif = (int) $this->fk_user_modif;
218 if (isset($this->fk_project)) {
219 $this->fk_project = (int) $this->fk_project;
223 if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
224 $this->error =
"ErrorBadParameter";
227 if (
isModEnabled(
'accounting') && empty($this->account_capital)) {
228 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyCapitalCode"));
231 if (
isModEnabled(
'accounting') && empty($this->account_insurance)) {
232 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInsuranceCode"));
235 if (
isModEnabled(
'accounting') && empty($this->account_interest)) {
236 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInterestCode"));
242 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
243 $sql .=
" accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
244 $sql .=
" datec, fk_projet, fk_user_author, insurance_amount)";
245 $sql .=
" VALUES ('".$this->db->escape($this->label).
"',";
246 $sql .=
" '".$this->db->escape($this->fk_bank).
"',";
247 $sql .=
" '".price2num($newcapital).
"',";
248 $sql .=
" '".$this->db->idate($this->datestart).
"',";
249 $sql .=
" '".$this->db->idate($this->dateend).
"',";
250 $sql .=
" '".$this->db->escape($this->nbterm).
"',";
251 $sql .=
" '".$this->db->escape($this->rate).
"',";
252 $sql .=
" '".$this->db->escape($this->note_private).
"',";
253 $sql .=
" '".$this->db->escape($this->note_public).
"',";
254 $sql .=
" '".$this->db->escape($this->account_capital).
"',";
255 $sql .=
" '".$this->db->escape($this->account_insurance).
"',";
256 $sql .=
" '".$this->db->escape($this->account_interest).
"',";
257 $sql .=
" ".$conf->entity.
",";
258 $sql .=
" '".$this->db->idate($now).
"',";
259 $sql .=
" ".(empty($this->fk_project) ?
'NULL' : $this->fk_project).
",";
260 $sql .=
" ".$user->id.
",";
261 $sql .=
" '".price2num($newinsuranceamount).
"'";
264 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
267 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"loan");
273 $this->error = $this->
db->error();
274 $this->
db->rollback();
286 public function delete($user)
293 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
295 $lines_url = $account->get_url(
'', $this->
id,
'loan');
298 foreach ($lines_url as $line_url) {
301 $accountline->fetch($line_url[
'fk_bank']);
302 $result = $accountline->delete_urls($user);
311 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan where fk_loan=".((int) $this->
id);
312 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
316 $this->error = $this->
db->lasterror();
321 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"loan where rowid=".((int) $this->
id);
322 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
326 $this->error = $this->
db->lasterror();
334 $this->
db->rollback();
350 if (!is_numeric($this->nbterm)) {
351 $this->error =
'BadValueForParameterForNbTerm';
355 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan";
356 $sql .=
" SET label='".$this->db->escape($this->label).
"',";
357 $sql .=
" capital='".price2num($this->
db->escape($this->capital)).
"',";
358 $sql .=
" datestart='".$this->db->idate($this->datestart).
"',";
359 $sql .=
" dateend='".$this->db->idate($this->dateend).
"',";
360 $sql .=
" nbterm=".((float) $this->nbterm).
",";
361 $sql .=
" rate=".((float) $this->rate).
",";
362 $sql .=
" accountancy_account_capital = '".$this->db->escape($this->account_capital).
"',";
363 $sql .=
" accountancy_account_insurance = '".$this->db->escape($this->account_insurance).
"',";
364 $sql .=
" accountancy_account_interest = '".$this->db->escape($this->account_interest).
"',";
365 $sql .=
" fk_projet=".(empty($this->fk_project) ?
'NULL' : ((int) $this->fk_project)).
",";
366 $sql .=
" fk_user_modif = ".$user->id.
",";
367 $sql .=
" insurance_amount = '".price2num($this->
db->escape($this->insurance_amount)).
"'";
368 $sql .=
" WHERE rowid=".((int) $this->
id);
370 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
376 $this->error = $this->
db->error();
377 $this->
db->rollback();
394 dol_syslog(get_class($this).
"::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
406 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
407 $sql .=
" paid = ".$this::STATUS_PAID;
408 $sql .=
" WHERE rowid = ".((int) $this->
id);
409 $return = $this->
db->query($sql);
413 $this->error = $this->
db->lasterror();
430 dol_syslog(get_class($this).
"::set_started is deprecated, use setStarted instead", LOG_NOTICE);
442 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
443 $sql .=
" paid = ".$this::STATUS_STARTED;
444 $sql .=
" WHERE rowid = ".((int) $this->
id);
445 $return = $this->
db->query($sql);
449 $this->error = $this->
db->lasterror();
465 dol_syslog(get_class($this).
"::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
477 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
478 $sql .=
" paid = ".$this::STATUS_UNPAID;
479 $sql .=
" WHERE rowid = ".((int) $this->
id);
480 $return = $this->
db->query($sql);
484 $this->error = $this->
db->lasterror();
498 return $this->
LibStatut($this->paid, $mode, $alreadypaid);
510 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
516 $langs->loadLangs(array(
"customers",
"bills"));
518 unset($this->labelStatus);
519 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
521 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
522 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
523 $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
524 if ($status == 0 && $alreadypaid > 0) {
525 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
527 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
528 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Enabled');
529 $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
530 if ($status == 0 && $alreadypaid > 0) {
531 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
535 $statusType =
'status1';
536 if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
537 $statusType =
'status3';
540 $statusType =
'status6';
543 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
558 public function getNomUrl($withpicto = 0, $maxlen = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
560 global $conf, $langs, $hookmanager;
564 $label =
'<u>'.$langs->trans(
"ShowLoan").
'</u>';
565 if (!empty($this->
ref)) {
566 $label .=
'<br><strong>'.$langs->trans(
'Ref').
':</strong> '.$this->ref;
568 if (!empty($this->label)) {
569 $label .=
'<br><strong>'.$langs->trans(
'Label').
':</strong> '.$this->label;
572 $url = DOL_URL_ROOT.
'/loan/card.php?id='.$this->id;
574 if ($option !=
'nolink') {
576 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
577 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
578 $add_save_lastsearch_values = 1;
580 if ($add_save_lastsearch_values) {
581 $url .=
'&save_lastsearch_values=1';
586 if (empty($notooltip)) {
587 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
588 $label = $langs->trans(
"ShowMyObject");
589 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
591 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
592 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
594 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
597 $linkstart =
'<a href="'.$url.
'"';
598 $linkstart .= $linkclose.
'>';
601 $result .= $linkstart;
603 $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);
605 if ($withpicto != 2) {
606 $result .= ($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref);
611 $hookmanager->initHooks(array($this->element .
'dao'));
612 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
613 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
615 $result = $hookmanager->resPrint;
617 $result .= $hookmanager->resPrint;
631 global $user, $langs, $conf;
638 $this->label =
'SPECIMEN';
641 $this->account_capital = 16;
642 $this->account_insurance = 616;
643 $this->account_interest = 518;
644 $this->datestart = $now;
645 $this->dateend = $now + (3600 * 24 * 365);
646 $this->note_public =
'SPECIMEN';
647 $this->capital = 20000;
659 $table =
'payment_loan';
662 $sql =
'SELECT sum(amount_capital) as amount';
663 $sql .=
' FROM '.MAIN_DB_PREFIX.$table;
664 $sql .=
" WHERE ".$field.
" = ".((int) $this->
id);
666 dol_syslog(get_class($this).
"::getSumPayment", LOG_DEBUG);
671 $obj = $this->
db->fetch_object(
$resql);
673 $amount = $obj->amount ? $obj->amount : 0;
679 $this->error = $this->
db->lasterror();
692 $sql =
'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
693 $sql .=
' l.tms as datem';
694 $sql .=
' WHERE l.rowid = '.((int) $id);
696 dol_syslog(get_class($this).
'::info', LOG_DEBUG);
697 $result = $this->
db->query($sql);
700 if ($this->
db->num_rows($result)) {
701 $obj = $this->
db->fetch_object($result);
702 $this->
id = $obj->rowid;
704 $this->user_creation_id = $obj->fk_user_author;
705 $this->user_modification_id = $obj->fk_user_modif;
706 $this->date_creation = $this->
db->jdate($obj->datec);
707 $this->date_modification = empty($obj->datem) ?
'' : $this->
db->jdate($obj->datem);
709 $this->
db->free($result);
712 $this->
db->free($result);
716 $this->error = $this->
db->lasterror();