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.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 = ".$id;
136 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
142 $obj = $this->
db->fetch_object(
$resql);
144 $this->
id = $obj->rowid;
145 $this->ref = $obj->rowid;
146 $this->datestart = $this->
db->jdate($obj->datestart);
147 $this->dateend = $this->
db->jdate($obj->dateend);
148 $this->label = $obj->label;
149 $this->capital = $obj->capital;
150 $this->nbterm = $obj->nbterm;
151 $this->rate = $obj->rate;
152 $this->note_private = $obj->note_private;
153 $this->note_public = $obj->note_public;
154 $this->insurance_amount = $obj->insurance_amount;
155 $this->paid = $obj->paid;
157 $this->account_capital = $obj->accountancy_account_capital;
158 $this->account_insurance = $obj->accountancy_account_insurance;
159 $this->account_interest = $obj->accountancy_account_interest;
160 $this->fk_project = $obj->fk_project;
169 $this->error = $this->
db->lasterror();
183 global $conf, $langs;
190 $newcapital =
price2num($this->capital,
'MT');
191 if (empty($this->insurance_amount)) $this->insurance_amount = 0;
192 $newinsuranceamount =
price2num($this->insurance_amount,
'MT');
193 if (isset($this->note_private)) $this->note_private = trim($this->note_private);
194 if (isset($this->note_public)) $this->note_public = trim($this->note_public);
195 if (isset($this->account_capital)) $this->account_capital = trim($this->account_capital);
196 if (isset($this->account_insurance)) $this->account_insurance = trim($this->account_insurance);
197 if (isset($this->account_interest)) $this->account_interest = trim($this->account_interest);
198 if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
199 if (isset($this->fk_user_creat)) $this->fk_user_creat = (
int) $this->fk_user_creat;
200 if (isset($this->fk_user_modif)) $this->fk_user_modif = (
int) $this->fk_user_modif;
201 if (isset($this->fk_project)) $this->fk_project = (
int) $this->fk_project;
204 if (!$newcapital > 0 || empty($this->datestart) || empty($this->dateend))
206 $this->error =
"ErrorBadParameter";
209 if (($conf->accounting->enabled) && empty($this->account_capital))
211 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyCapitalCode"));
214 if (($conf->accounting->enabled) && empty($this->account_insurance))
216 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInsuranceCode"));
219 if (($conf->accounting->enabled) && empty($this->account_interest))
221 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"LoanAccountancyInterestCode"));
227 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
228 $sql .=
" accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
229 $sql .=
" datec, fk_projet, fk_user_author, insurance_amount)";
230 $sql .=
" VALUES ('".$this->db->escape($this->label).
"',";
231 $sql .=
" '".$this->db->escape($this->fk_bank).
"',";
232 $sql .=
" '".price2num($newcapital).
"',";
233 $sql .=
" '".$this->db->idate($this->datestart).
"',";
234 $sql .=
" '".$this->db->idate($this->dateend).
"',";
235 $sql .=
" '".$this->db->escape($this->nbterm).
"',";
236 $sql .=
" '".$this->db->escape($this->rate).
"',";
237 $sql .=
" '".$this->db->escape($this->note_private).
"',";
238 $sql .=
" '".$this->db->escape($this->note_public).
"',";
239 $sql .=
" '".$this->db->escape($this->account_capital).
"',";
240 $sql .=
" '".$this->db->escape($this->account_insurance).
"',";
241 $sql .=
" '".$this->db->escape($this->account_interest).
"',";
242 $sql .=
" ".$conf->entity.
",";
243 $sql .=
" '".$this->db->idate($now).
"',";
244 $sql .=
" ".(empty($this->fk_project) ?
'NULL' : $this->fk_project).
",";
245 $sql .=
" ".$user->id.
",";
246 $sql .=
" '".price2num($newinsuranceamount).
"'";
249 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
253 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"loan");
259 $this->error = $this->
db->error();
260 $this->
db->rollback();
272 public function delete($user)
279 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
281 $lines_url = $account->get_url(
'', $this->
id,
'loan');
284 foreach ($lines_url as $line_url)
289 $accountline->fetch($line_url[
'fk_bank']);
290 $result = $accountline->delete_urls($user);
301 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan where fk_loan=".$this->id;
302 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
307 $this->error = $this->
db->lasterror();
313 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"loan where rowid=".$this->id;
314 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
319 $this->error = $this->
db->lasterror();
328 $this->
db->rollback();
344 if (!is_numeric($this->nbterm))
346 $this->error =
'BadValueForParameterForNbTerm';
350 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan";
351 $sql .=
" SET label='".$this->db->escape($this->label).
"',";
352 $sql .=
" capital='".price2num($this->
db->escape($this->capital)).
"',";
353 $sql .=
" datestart='".$this->db->idate($this->datestart).
"',";
354 $sql .=
" dateend='".$this->db->idate($this->dateend).
"',";
355 $sql .=
" nbterm=".$this->nbterm.
",";
356 $sql .=
" rate=".$this->db->escape($this->rate).
",";
357 $sql .=
" accountancy_account_capital = '".$this->db->escape($this->account_capital).
"',";
358 $sql .=
" accountancy_account_insurance = '".$this->db->escape($this->account_insurance).
"',";
359 $sql .=
" accountancy_account_interest = '".$this->db->escape($this->account_interest).
"',";
360 $sql .=
" fk_projet=".(empty($this->fk_project) ?
'NULL' : $this->fk_project).
",";
361 $sql .=
" fk_user_modif = ".$user->id.
",";
362 $sql .=
" insurance_amount = '".price2num($this->
db->escape($this->insurance_amount)).
"'";
363 $sql .=
" WHERE rowid=".$this->id;
365 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
372 $this->error = $this->
db->error();
373 $this->
db->rollback();
388 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
389 $sql .=
" paid = ".$this::STATUS_PAID;
390 $sql .=
" WHERE rowid = ".$this->id;
391 $return = $this->
db->query($sql);
395 $this->error = $this->
db->lasterror();
410 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
411 $sql .=
" paid = ".$this::STATUS_STARTED;
412 $sql .=
" WHERE rowid = ".$this->id;
413 $return = $this->
db->query($sql);
417 $this->error = $this->
db->lasterror();
432 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan SET";
433 $sql .=
" paid = ".$this::STATUS_UNPAID;
434 $sql .=
" WHERE rowid = ".$this->id;
435 $return = $this->
db->query($sql);
439 $this->error = $this->
db->lasterror();
453 return $this->
LibStatut($this->paid, $mode, $alreadypaid);
465 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
471 $langs->loadLangs(array(
"customers",
"bills"));
473 unset($this->labelStatus);
474 if (empty($this->labelStatus) || empty($this->labelStatusShort))
477 $this->labelStatus[self::STATUS_UNPAID] = $langs->trans(
'Unpaid');
478 $this->labelStatus[self::STATUS_PAID] = $langs->trans(
'Paid');
479 $this->labelStatus[self::STATUS_STARTED] = $langs->trans(
"BillStatusStarted");
480 if ($status == 0 && $alreadypaid > 0) $this->labelStatus[self::STATUS_UNPAID] = $langs->trans(
"BillStatusStarted");
481 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->trans(
'Unpaid');
482 $this->labelStatusShort[self::STATUS_PAID] = $langs->trans(
'Enabled');
483 $this->labelStatusShort[self::STATUS_STARTED] = $langs->trans(
"BillStatusStarted");
484 if ($status == 0 && $alreadypaid > 0) $this->labelStatusShort[self::STATUS_UNPAID] = $langs->trans(
"BillStatusStarted");
487 $statusType =
'status1';
488 if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) $statusType =
'status3';
489 if ($status == 1) $statusType =
'status6';
491 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
506 public function getNomUrl($withpicto = 0, $maxlen = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
508 global $conf, $langs;
512 $label =
'<u>'.$langs->trans(
"ShowLoan").
'</u>';
513 if (!empty($this->ref)) {
514 $label .=
'<br><strong>'.$langs->trans(
'Ref').
':</strong> '.$this->ref;
516 if (!empty($this->label)) {
517 $label .=
'<br><strong>'.$langs->trans(
'Label').
':</strong> '.$this->label;
520 $url = DOL_URL_ROOT.
'/loan/card.php?id='.$this->id;
522 if ($option !=
'nolink')
525 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
526 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/',
$_SERVER[
"PHP_SELF"])) $add_save_lastsearch_values = 1;
527 if ($add_save_lastsearch_values) $url .=
'&save_lastsearch_values=1';
531 if (empty($notooltip))
533 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
535 $label = $langs->trans(
"ShowMyObject");
536 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
538 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
539 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
540 }
else $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
542 $linkstart =
'<a href="'.$url.
'"';
543 $linkstart .= $linkclose.
'>';
546 $result .= $linkstart;
547 if ($withpicto) $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);
548 if ($withpicto != 2) $result .= ($maxlen ?
dol_trunc($this->ref, $maxlen) : $this->ref);
563 global $user, $langs, $conf;
570 $this->label =
'SPECIMEN';
573 $this->account_capital = 16;
574 $this->account_insurance = 616;
575 $this->account_interest = 518;
576 $this->datestart = $now;
577 $this->dateend = $now + (3600 * 24 * 365);
578 $this->note_public =
'SPECIMEN';
579 $this->capital = 20000;
591 $table =
'payment_loan';
594 $sql =
'SELECT sum(amount_capital) as amount';
595 $sql .=
' FROM '.MAIN_DB_PREFIX.$table;
596 $sql .=
' WHERE '.$field.
' = '.$this->id;
598 dol_syslog(get_class($this).
"::getSumPayment", LOG_DEBUG);
604 $obj = $this->
db->fetch_object(
$resql);
605 if ($obj) $amount = $obj->amount ? $obj->amount : 0;
610 $this->error = $this->
db->lasterror();
623 $sql =
'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
625 $sql .=
' WHERE l.rowid = '.$id;
627 dol_syslog(get_class($this).
'::info', LOG_DEBUG);
628 $result = $this->
db->query($sql);
632 if ($this->
db->num_rows($result))
634 $obj = $this->
db->fetch_object($result);
635 $this->
id = $obj->rowid;
636 if ($obj->fk_user_author)
638 $cuser =
new User($this->
db);
639 $cuser->fetch($obj->fk_user_author);
640 $this->user_creation = $cuser;
642 if ($obj->fk_user_modif)
644 $muser =
new User($this->
db);
645 $muser->fetch($obj->fk_user_modif);
646 $this->user_modification = $muser;
648 $this->date_creation = $this->
db->jdate($obj->datec);
649 if (empty($obj->fk_user_modif)) $obj->tms =
"";
650 $this->date_modification = $this->
db->jdate($obj->tms);
652 $this->
db->free($result);
655 $this->
db->free($result);
659 $this->error = $this->
db->lasterror();
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 clicable name (with eventually the picto)
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
getLibStatut($mode=0, $alreadypaid=-1)
Return label of loan status (unpaid, paid)
Class to manage bank transaction lines.
getSumPayment()
Return amount of payments already done.
$conf db
API class for accounts.
Class to manage bank accounts.
create($user)
Create a loan into database.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
info($id)
Information on record.
set_started($user)
Tag loan as payement started.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
initAsSpecimen()
Initialise an instance with random values.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
set_paid($user)
Tag loan as paid completely.
print $_SERVER["PHP_SELF"]
Edit parameters.
update($user)
Update loan.
set_unpaid($user)
Tag loan as payement as unpaid.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
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.
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
fetch($id)
Load object in memory from database.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)