25 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
36 public $element =
'loan_schedule';
41 public $table_element =
'loan_schedule';
59 public $amounts = array();
60 public $amount_capital;
61 public $amount_insurance;
62 public $amount_interest;
67 public $fk_typepayment;
82 public $fk_payment_loan;
87 public $fk_user_creat;
92 public $fk_user_modif;
94 public $lines = array();
125 global $conf, $langs;
134 $this->error =
'ErrorBadValueForParameter';
139 if (isset($this->fk_loan)) $this->fk_loan = (int) $this->fk_loan;
140 if (isset($this->amount_capital)) $this->amount_capital = trim($this->amount_capital ? $this->amount_capital : 0);
141 if (isset($this->amount_insurance)) $this->amount_insurance = trim($this->amount_insurance ? $this->amount_insurance : 0);
142 if (isset($this->amount_interest)) $this->amount_interest = trim($this->amount_interest ? $this->amount_interest : 0);
143 if (isset($this->fk_typepayment)) $this->fk_typepayment = (
int) $this->fk_typepayment;
144 if (isset($this->fk_bank)) $this->fk_bank = (
int) $this->fk_bank;
145 if (isset($this->fk_user_creat)) $this->fk_user_creat = (
int) $this->fk_user_creat;
146 if (isset($this->fk_user_modif)) $this->fk_user_modif = (
int) $this->fk_user_modif;
148 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
152 if ($totalamount == 0) {
153 $this->errors[] =
'step1';
160 if ($totalamount != 0)
162 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
163 $sql .=
" fk_typepayment, fk_user_creat, fk_bank)";
164 $sql .=
" VALUES (".$this->fk_loan.
", '".$this->
db->idate($now).
"',";
165 $sql .=
" '".$this->db->idate($this->datep).
"',";
166 $sql .=
" ".$this->amount_capital.
",";
167 $sql .=
" ".$this->amount_insurance.
",";
168 $sql .=
" ".$this->amount_interest.
",";
169 $sql .=
" ".$this->fk_typepayment.
", ";
170 $sql .=
" ".$user->id.
",";
171 $sql .=
" ".$this->fk_bank.
")";
173 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
177 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
179 $this->error = $this->
db->lasterror();
184 if ($totalamount != 0 && !$error)
186 $this->amount_capital = $totalamount;
190 $this->errors[] = $this->
db->lasterror();
191 $this->
db->rollback();
207 $sql .=
" t.fk_loan,";
211 $sql .=
" t.amount_capital,";
212 $sql .=
" t.amount_insurance,";
213 $sql .=
" t.amount_interest,";
214 $sql .=
" t.fk_typepayment,";
215 $sql .=
" t.num_payment,";
216 $sql .=
" t.note_private,";
217 $sql .=
" t.note_public,";
218 $sql .=
" t.fk_bank,";
219 $sql .=
" t.fk_payment_loan,";
220 $sql .=
" t.fk_user_creat,";
221 $sql .=
" t.fk_user_modif,";
222 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
223 $sql .=
' b.fk_account';
224 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
225 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
226 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
227 $sql .=
" WHERE t.rowid = ".$id;
229 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
233 $obj = $this->
db->fetch_object(
$resql);
235 $this->
id = $obj->rowid;
236 $this->ref = $obj->rowid;
238 $this->fk_loan = $obj->fk_loan;
239 $this->datec = $this->
db->jdate($obj->datec);
240 $this->tms = $this->
db->jdate($obj->tms);
241 $this->datep = $this->
db->jdate($obj->datep);
242 $this->amount_capital = $obj->amount_capital;
243 $this->amount_insurance = $obj->amount_insurance;
244 $this->amount_interest = $obj->amount_interest;
245 $this->fk_typepayment = $obj->fk_typepayment;
246 $this->num_payment = $obj->num_payment;
247 $this->note_private = $obj->note_private;
248 $this->note_public = $obj->note_public;
249 $this->fk_bank = $obj->fk_bank;
250 $this->fk_payment_loan = $obj->fk_payment_loan;
251 $this->fk_user_creat = $obj->fk_user_creat;
252 $this->fk_user_modif = $obj->fk_user_modif;
254 $this->type_code = $obj->type_code;
255 $this->type_label = $obj->type_label;
257 $this->bank_account = $obj->fk_account;
258 $this->bank_line = $obj->fk_bank;
264 $this->error =
"Error ".$this->db->lasterror();
277 public function update($user = 0, $notrigger = 0)
279 global $conf, $langs;
283 if (isset($this->amount_capital)) $this->amount_capital = trim($this->amount_capital);
284 if (isset($this->amount_insurance)) $this->amount_insurance = trim($this->amount_insurance);
285 if (isset($this->amount_interest)) $this->amount_interest = trim($this->amount_interest);
286 if (isset($this->num_payment)) $this->num_payment = trim($this->num_payment);
287 if (isset($this->note_private)) $this->note_private = trim($this->note_private);
288 if (isset($this->note_public)) $this->note_public = trim($this->note_public);
289 if (isset($this->fk_bank)) $this->fk_bank = trim($this->fk_bank);
290 if (isset($this->fk_payment_loan)) $this->fk_payment_loan = (int) $this->fk_payment_loan;
296 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
298 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
299 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
300 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
301 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
302 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
303 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
304 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
305 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
306 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
307 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
308 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
309 $sql .=
" fk_bank=".(isset($this->fk_bank) ? $this->fk_bank :
"null").
",";
310 $sql .=
" fk_payment_loan=".(isset($this->fk_payment_loan) ? $this->fk_payment_loan :
"null").
",";
311 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat :
"null").
",";
312 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif :
"null").
"";
314 $sql .=
" WHERE rowid=".$this->id;
318 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
320 if (!
$resql) { $error++; $this->errors[] =
"Error ".$this->db->lasterror(); }
325 $this->
db->rollback();
341 public function delete($user, $notrigger = 0)
343 global $conf, $langs;
349 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
350 $sql .=
" WHERE rowid=".$this->id;
352 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
354 if (!
$resql) { $error++; $this->errors[] =
"Error ".$this->db->lasterror(); }
360 foreach ($this->errors as $errmsg)
362 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
363 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
365 $this->
db->rollback();
385 if (!empty($capital) && !empty($rate) && !empty($nbterm)) {
386 $result = ($capital * ($rate / 12)) / (1 - pow((1 + ($rate / 12)), ($nbterm * -1)));
405 $sql .=
" t.fk_loan,";
409 $sql .=
" t.amount_capital,";
410 $sql .=
" t.amount_insurance,";
411 $sql .=
" t.amount_interest,";
412 $sql .=
" t.fk_typepayment,";
413 $sql .=
" t.num_payment,";
414 $sql .=
" t.note_private,";
415 $sql .=
" t.note_public,";
416 $sql .=
" t.fk_bank,";
417 $sql .=
" t.fk_payment_loan,";
418 $sql .=
" t.fk_user_creat,";
419 $sql .=
" t.fk_user_modif";
420 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
421 $sql .=
" WHERE t.fk_loan = ".$loanid;
423 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
428 while ($obj = $this->
db->fetch_object(
$resql))
431 $line->id = $obj->rowid;
432 $line->ref = $obj->rowid;
434 $line->fk_loan = $obj->fk_loan;
435 $line->datec = $this->
db->jdate($obj->datec);
436 $line->tms = $this->
db->jdate($obj->tms);
437 $line->datep = $this->
db->jdate($obj->datep);
438 $line->amount_capital = $obj->amount_capital;
439 $line->amount_insurance = $obj->amount_insurance;
440 $line->amount_interest = $obj->amount_interest;
441 $line->fk_typepayment = $obj->fk_typepayment;
442 $line->num_payment = $obj->num_payment;
443 $line->note_private = $obj->note_private;
444 $line->note_public = $obj->note_public;
445 $line->fk_bank = $obj->fk_bank;
446 $line->fk_payment_loan = $obj->fk_payment_loan;
447 $line->fk_user_creat = $obj->fk_user_creat;
448 $line->fk_user_modif = $obj->fk_user_modif;
450 $this->lines[] = $line;
455 $this->error =
"Error ".$this->db->lasterror();
467 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
468 require_once DOL_DOCUMENT_ROOT.
'/core/lib/loan.lib.php';
469 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
473 $sql =
"SELECT l.rowid";
474 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l ";
475 $sql .=
" WHERE l.paid = 0";
479 while ($obj = $this->
db->fetch_object(
$resql)) {
482 if (count($toinsert) > 0) {
483 foreach ($toinsert as $echid) {
485 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan ";
486 $sql .=
"(fk_loan,datec,tms,datep,amount_capital,amount_insurance,amount_interest,fk_typepayment,num_payment,note_private,note_public,fk_bank,fk_user_creat,fk_user_modif) ";
487 $sql .=
"SELECT fk_loan,datec,tms,datep,amount_capital,amount_insurance,amount_interest,fk_typepayment,num_payment,note_private,note_public,fk_bank,fk_user_creat,fk_user_modif FROM ".MAIN_DB_PREFIX.
"loan_schedule WHERE rowid =".$echid;
488 $res = $this->
db->query($sql);
492 $this->
db->rollback();
509 $sql =
"SELECT p.datep";
510 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan as p ";
511 $sql .=
" WHERE p.fk_loan = ".$loanid;
512 $sql .=
" ORDER BY p.datep DESC ";
518 $obj = $this->
db->fetch_object(
$resql);
519 return $this->
db->jdate($obj->datep);
537 $sql =
"SELECT p.rowid";
538 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as p ";
539 $sql .=
" WHERE p.fk_loan = ".$loanid;
540 if (!empty($datemax)) { $sql .=
" AND p.datep > '".$this->db->idate($datemax).
"'"; }
541 $sql .=
" AND p.datep <= '".$this->db->idate(
dol_now()).
"'";
546 while ($obj = $this->
db->fetch_object(
$resql))
548 $result[] = $obj->rowid;
lastPayment($loanid)
lastpayment
__construct($db)
Constructor.
transPayment()
transPayment
dol_now($mode= 'auto')
Return date for now.
update($user=0, $notrigger=0)
Update database.
paimenttorecord($loanid, $datemax)
paimenttorecord
$conf db
API class for accounts.
Class to manage Schedule of loans.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
fetchAll($loanid)
Load all object in memory from database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
calcMonthlyPayments($capital, $rate, $nbterm)
Calculate Monthly Payments.
create($user)
Create payment of loan into database.
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.
fetch($id)
Load object in memory from database.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)