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;
112 const STATUS_UNPAID = 0;
113 const STATUS_PAID = 1;
114 const STATUS_STARTED = 2;
135 $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,";
136 $sql .=
" l.paid, l.fk_bank, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
137 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
138 $sql .=
" WHERE l.rowid = ".((int) $id);
140 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
144 $obj = $this->
db->fetch_object(
$resql);
146 $this->
id = $obj->rowid;
147 $this->
ref = $obj->rowid;
148 $this->datestart = $this->
db->jdate($obj->datestart);
149 $this->dateend = $this->
db->jdate($obj->dateend);
150 $this->label = $obj->label;
151 $this->capital = $obj->capital;
152 $this->nbterm = $obj->nbterm;
153 $this->rate = $obj->rate;
154 $this->note_private = $obj->note_private;
155 $this->note_public = $obj->note_public;
156 $this->insurance_amount = $obj->insurance_amount;
157 $this->paid = $obj->paid;
158 $this->fk_bank = $obj->fk_bank;
160 $this->account_capital = $obj->accountancy_account_capital;
161 $this->account_insurance = $obj->accountancy_account_insurance;
162 $this->account_interest = $obj->accountancy_account_interest;
163 $this->fk_project = $obj->fk_project;
172 $this->error = $this->
db->lasterror();
186 global $conf, $langs;
193 $newcapital =
price2num($this->capital,
'MT');
194 if (empty($this->insurance_amount)) {
195 $this->insurance_amount = 0;
197 $newinsuranceamount =
price2num($this->insurance_amount,
'MT');
198 if (isset($this->note_private)) {
199 $this->note_private = trim($this->note_private);
201 if (isset($this->note_public)) {
202 $this->note_public = trim($this->note_public);
204 if (isset($this->account_capital)) {
205 $this->account_capital = trim($this->account_capital);
207 if (isset($this->account_insurance)) {
208 $this->account_insurance = trim($this->account_insurance);
210 if (isset($this->account_interest)) {
211 $this->account_interest = trim($this->account_interest);
213 if (isset($this->fk_bank)) {
214 $this->fk_bank = (int) $this->fk_bank;
216 if (isset($this->fk_user_creat)) {
217 $this->fk_user_creat = (int) $this->fk_user_creat;
219 if (isset($this->fk_user_modif)) {
220 $this->fk_user_modif = (int) $this->fk_user_modif;
222 if (isset($this->fk_project)) {
223 $this->fk_project = (int) $this->fk_project;
227 if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
228 $this->error =
"ErrorBadParameter";
231 if (
isModEnabled(
'accounting') && empty($this->account_capital)) {
232 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyCapitalCode"));
235 if (
isModEnabled(
'accounting') && empty($this->account_insurance)) {
236 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInsuranceCode"));
239 if (
isModEnabled(
'accounting') && empty($this->account_interest)) {
240 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInterestCode"));
246 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
247 $sql .=
" accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
248 $sql .=
" datec, fk_projet, fk_user_author, insurance_amount)";
249 $sql .=
" VALUES ('".$this->db->escape($this->label).
"',";
250 $sql .=
" '".$this->db->escape($this->fk_bank).
"',";
251 $sql .=
" '".price2num($newcapital).
"',";
252 $sql .=
" '".$this->db->idate($this->datestart).
"',";
253 $sql .=
" '".$this->db->idate($this->dateend).
"',";
254 $sql .=
" '".$this->db->escape($this->nbterm).
"',";
255 $sql .=
" '".$this->db->escape($this->rate).
"',";
256 $sql .=
" '".$this->db->escape($this->note_private).
"',";
257 $sql .=
" '".$this->db->escape($this->note_public).
"',";
258 $sql .=
" '".$this->db->escape($this->account_capital).
"',";
259 $sql .=
" '".$this->db->escape($this->account_insurance).
"',";
260 $sql .=
" '".$this->db->escape($this->account_interest).
"',";
261 $sql .=
" ".$conf->entity.
",";
262 $sql .=
" '".$this->db->idate($now).
"',";
263 $sql .=
" ".(empty($this->fk_project) ?
'NULL' : $this->fk_project).
",";
264 $sql .=
" ".$user->id.
",";
265 $sql .=
" '".price2num($newinsuranceamount).
"'";
268 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
271 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"loan");
277 $this->error = $this->
db->error();
278 $this->
db->rollback();
290 public function delete($user)
297 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
299 $lines_url = $account->get_url(
'', $this->
id,
'loan');
302 foreach ($lines_url as $line_url) {
305 $accountline->fetch($line_url[
'fk_bank']);
306 $result = $accountline->delete_urls($user);
315 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan where fk_loan=".((int) $this->
id);
316 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
320 $this->error = $this->
db->lasterror();
325 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"loan where rowid=".((int) $this->
id);
326 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
330 $this->error = $this->
db->lasterror();
338 $this->
db->rollback();
354 if (!is_numeric($this->nbterm)) {
355 $this->error =
'BadValueForParameterForNbTerm';
359 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan";
360 $sql .=
" SET label='".$this->db->escape($this->label).
"',";
361 $sql .=
" capital='".price2num($this->
db->escape($this->capital)).
"',";
362 $sql .=
" datestart='".$this->db->idate($this->datestart).
"',";
363 $sql .=
" dateend='".$this->db->idate($this->dateend).
"',";
364 $sql .=
" nbterm=".((float) $this->nbterm).
",";
365 $sql .=
" rate=".((float) $this->rate).
",";
366 $sql .=
" accountancy_account_capital = '".$this->db->escape($this->account_capital).
"',";
367 $sql .=
" accountancy_account_insurance = '".$this->db->escape($this->account_insurance).
"',";
368 $sql .=
" accountancy_account_interest = '".$this->db->escape($this->account_interest).
"',";
369 $sql .=
" fk_projet=".(empty($this->fk_project) ?
'NULL' : ((int) $this->fk_project)).
",";
370 $sql .=
" fk_user_modif = ".$user->id.
",";
371 $sql .=
" insurance_amount = '".price2num($this->
db->escape($this->insurance_amount)).
"'";
372 $sql .=
" WHERE rowid=".((int) $this->
id);
374 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
380 $this->error = $this->
db->error();
381 $this->
db->rollback();
398 dol_syslog(get_class($this).
"::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
410 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
411 $sql .=
" paid = ".$this::STATUS_PAID;
412 $sql .=
" WHERE rowid = ".((int) $this->
id);
413 $return = $this->
db->query($sql);
417 $this->error = $this->
db->lasterror();
434 dol_syslog(get_class($this).
"::set_started is deprecated, use setStarted instead", LOG_NOTICE);
446 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
447 $sql .=
" paid = ".$this::STATUS_STARTED;
448 $sql .=
" WHERE rowid = ".((int) $this->
id);
449 $return = $this->
db->query($sql);
453 $this->error = $this->
db->lasterror();
469 dol_syslog(get_class($this).
"::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
481 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
482 $sql .=
" paid = ".$this::STATUS_UNPAID;
483 $sql .=
" WHERE rowid = ".((int) $this->
id);
484 $return = $this->
db->query($sql);
488 $this->error = $this->
db->lasterror();
502 return $this->
LibStatut($this->paid, $mode, $alreadypaid);
514 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
520 $langs->loadLangs(array(
"customers",
"bills"));
522 unset($this->labelStatus);
523 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
525 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
526 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Paid');
527 $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
528 if ($status == 0 && $alreadypaid > 0) {
529 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
531 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
'Unpaid');
532 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv(
'Enabled');
533 $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
534 if ($status == 0 && $alreadypaid > 0) {
535 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv(
"BillStatusStarted");
539 $statusType =
'status1';
540 if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
541 $statusType =
'status3';
544 $statusType =
'status6';
547 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
562 public function getNomUrl($withpicto = 0, $maxlen = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
564 global $conf, $langs, $hookmanager;
568 $label =
'<u>'.$langs->trans(
"ShowLoan").
'</u>';
569 if (!empty($this->
ref)) {
570 $label .=
'<br><strong>'.$langs->trans(
'Ref').
':</strong> '.$this->ref;
572 if (!empty($this->label)) {
573 $label .=
'<br><strong>'.$langs->trans(
'Label').
':</strong> '.$this->label;
576 $url = DOL_URL_ROOT.
'/loan/card.php?id='.$this->id;
578 if ($option !=
'nolink') {
580 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
581 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
582 $add_save_lastsearch_values = 1;
584 if ($add_save_lastsearch_values) {
585 $url .=
'&save_lastsearch_values=1';
590 if (empty($notooltip)) {
591 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
592 $label = $langs->trans(
"ShowMyObject");
593 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
595 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
596 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
598 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
601 $linkstart =
'<a href="'.$url.
'"';
602 $linkstart .= $linkclose.
'>';
605 $result .= $linkstart;
607 $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);
609 if ($withpicto != 2) {
610 $result .= ($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref);
615 $hookmanager->initHooks(array($this->element .
'dao'));
616 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
617 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
619 $result = $hookmanager->resPrint;
621 $result .= $hookmanager->resPrint;
635 global $user, $langs, $conf;
642 $this->label =
'SPECIMEN';
645 $this->account_capital = 16;
646 $this->account_insurance = 616;
647 $this->account_interest = 518;
648 $this->datestart = $now;
649 $this->dateend = $now + (3600 * 24 * 365);
650 $this->note_public =
'SPECIMEN';
651 $this->capital = 20000;
663 $table =
'payment_loan';
666 $sql =
'SELECT sum(amount_capital) as amount';
667 $sql .=
' FROM '.MAIN_DB_PREFIX.$table;
668 $sql .=
" WHERE ".$field.
" = ".((int) $this->
id);
670 dol_syslog(get_class($this).
"::getSumPayment", LOG_DEBUG);
675 $obj = $this->
db->fetch_object(
$resql);
677 $amount = $obj->amount ? $obj->amount : 0;
683 $this->error = $this->
db->lasterror();
696 $sql =
'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
697 $sql .=
' l.tms as datem';
698 $sql .=
' WHERE l.rowid = '.((int) $id);
700 dol_syslog(get_class($this).
'::info', LOG_DEBUG);
701 $result = $this->
db->query($sql);
704 if ($this->
db->num_rows($result)) {
705 $obj = $this->
db->fetch_object($result);
706 $this->
id = $obj->rowid;
708 $this->user_creation_id = $obj->fk_user_author;
709 $this->user_modification_id = $obj->fk_user_modif;
710 $this->date_creation = $this->
db->jdate($obj->datec);
711 $this->date_modification = empty($obj->datem) ?
'' : $this->
db->jdate($obj->datem);
713 $this->
db->free($result);
716 $this->
db->free($result);
720 $this->error = $this->
db->lasterror();
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 payement as unpaid.
initAsSpecimen()
Initialise an instance with random values.
setPaid($user)
Tag loan as paid completely.
setStarted($user)
Tag loan as payment started.
set_unpaid($user)
Tag loan as payement as unpaid.
info($id)
Information on record.
LibStatut($status, $mode=0, $alreadypaid=-1)
Return label for given status.
__construct($db)
Constructor.
set_paid($user)
Tag loan as paid completely.
getNomUrl($withpicto=0, $maxlen=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return clicable 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)
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='auto')
Return date for now.
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.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.