159 global $conf, $langs;
167 $this->error =
'ErrorBadValueForParameter';
172 if (isset($this->fk_loan)) {
173 $this->fk_loan = (int) $this->fk_loan;
175 if (isset($this->amount_capital)) {
176 $this->amount_capital = trim($this->amount_capital ? $this->amount_capital : 0);
178 if (isset($this->amount_insurance)) {
179 $this->amount_insurance = trim($this->amount_insurance ? $this->amount_insurance : 0);
181 if (isset($this->amount_interest)) {
182 $this->amount_interest = trim($this->amount_interest ? $this->amount_interest : 0);
184 if (isset($this->fk_typepayment)) {
185 $this->fk_typepayment = (int) $this->fk_typepayment;
187 if (isset($this->fk_bank)) {
188 $this->fk_bank = (int) $this->fk_bank;
190 if (isset($this->fk_user_creat)) {
191 $this->fk_user_creat = (int) $this->fk_user_creat;
193 if (isset($this->fk_user_modif)) {
194 $this->fk_user_modif = (int) $this->fk_user_modif;
197 $totalamount = (float) $this->amount_capital + (
float) $this->amount_insurance + (float) $this->amount_interest;
201 if ($totalamount == 0) {
202 $this->errors[] =
'Amount must not be "0".';
209 if ($totalamount != 0) {
210 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
211 $sql .=
" fk_typepayment, fk_user_creat, fk_bank)";
212 $sql .=
" VALUES (".$this->fk_loan.
", '".$this->db->idate($now).
"',";
213 $sql .=
" '".$this->db->idate($this->datep).
"',";
214 $sql .=
" ".price2num($this->amount_capital).
",";
215 $sql .=
" ".price2num($this->amount_insurance).
",";
216 $sql .=
" ".price2num($this->amount_interest).
",";
217 $sql .=
" ".price2num($this->fk_typepayment).
", ";
218 $sql .=
" ".((int) $user->id).
",";
219 $sql .=
" ".((int) $this->fk_bank).
")";
221 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
222 $resql = $this->db->query($sql);
224 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"loan_schedule");
226 $this->error = $this->db->lasterror();
231 if ($totalamount != 0 && !$error) {
232 $this->amount_capital = $totalamount;
236 $this->errors[] = $this->db->lasterror();
237 $this->db->rollback();
253 $sql .=
" t.fk_loan,";
257 $sql .=
" t.amount_capital,";
258 $sql .=
" t.amount_insurance,";
259 $sql .=
" t.amount_interest,";
260 $sql .=
" t.fk_typepayment,";
261 $sql .=
" t.num_payment,";
262 $sql .=
" t.note_private,";
263 $sql .=
" t.note_public,";
264 $sql .=
" t.fk_bank,";
265 $sql .=
" t.fk_payment_loan,";
266 $sql .=
" t.fk_user_creat,";
267 $sql .=
" t.fk_user_modif,";
268 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
269 $sql .=
' b.fk_account';
270 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
271 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
272 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
273 $sql .=
" WHERE t.rowid = ".((int) $id);
275 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
276 $resql = $this->db->query($sql);
278 if ($this->db->num_rows($resql)) {
279 $obj = $this->db->fetch_object($resql);
281 $this->
id = $obj->rowid;
282 $this->
ref = $obj->rowid;
284 $this->fk_loan = $obj->fk_loan;
285 $this->datec = $this->db->jdate($obj->datec);
286 $this->tms = $this->db->jdate($obj->tms);
287 $this->datep = $this->db->jdate($obj->datep);
288 $this->amount_capital = $obj->amount_capital;
289 $this->amount_insurance = $obj->amount_insurance;
290 $this->amount_interest = $obj->amount_interest;
291 $this->fk_typepayment = $obj->fk_typepayment;
292 $this->num_payment = $obj->num_payment;
293 $this->note_private = $obj->note_private;
294 $this->note_public = $obj->note_public;
295 $this->fk_bank = $obj->fk_bank;
296 $this->fk_payment_loan = $obj->fk_payment_loan;
297 $this->fk_user_creat = $obj->fk_user_creat;
298 $this->fk_user_modif = $obj->fk_user_modif;
300 $this->type_code = $obj->type_code;
301 $this->type_label = $obj->type_label;
303 $this->bank_account = $obj->fk_account;
304 $this->bank_line = $obj->fk_bank;
306 $this->db->free($resql);
310 $this->error =
"Error ".$this->db->lasterror();
323 public function update($user =
null, $notrigger = 0)
325 global $conf, $langs;
329 if (isset($this->amount_capital)) {
330 $this->amount_capital = trim($this->amount_capital);
332 if (isset($this->amount_insurance)) {
333 $this->amount_insurance = trim($this->amount_insurance);
335 if (isset($this->amount_interest)) {
336 $this->amount_interest = trim($this->amount_interest);
338 if (isset($this->num_payment)) {
339 $this->num_payment = trim($this->num_payment);
341 if (isset($this->note_private)) {
342 $this->note_private = trim($this->note_private);
344 if (isset($this->note_public)) {
345 $this->note_public = trim($this->note_public);
347 if (isset($this->fk_bank)) {
348 $this->fk_bank = (int) $this->fk_bank;
350 if (isset($this->fk_payment_loan)) {
351 $this->fk_payment_loan = (int) $this->fk_payment_loan;
358 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
360 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
361 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
362 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
363 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
364 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
365 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
366 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
367 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
368 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
369 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
370 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
371 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
372 $sql .=
" fk_payment_loan=".(isset($this->fk_payment_loan) ? ((int) $this->fk_payment_loan) :
"null").
",";
373 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
374 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
376 $sql .=
" WHERE rowid=".((int) $this->
id);
380 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
381 $resql = $this->db->query($sql);
384 $this->errors[] =
"Error ".$this->db->lasterror();
389 $this->db->rollback();
450 if (!empty($capital) && !empty($nbterm)) {
452 $result = ($capital * ($rate / 12)) / (1 - pow((1 + ($rate / 12)), ($nbterm * -1)));
454 $result = $capital / $nbterm;
472 $sql .=
" t.fk_loan,";
476 $sql .=
" t.amount_capital,";
477 $sql .=
" t.amount_insurance,";
478 $sql .=
" t.amount_interest,";
479 $sql .=
" t.fk_typepayment,";
480 $sql .=
" t.num_payment,";
481 $sql .=
" t.note_private,";
482 $sql .=
" t.note_public,";
483 $sql .=
" t.fk_bank,";
484 $sql .=
" t.fk_payment_loan,";
485 $sql .=
" t.fk_user_creat,";
486 $sql .=
" t.fk_user_modif";
487 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
488 $sql .=
" WHERE t.fk_loan = ".((int) $loanid);
490 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
491 $resql = $this->db->query($sql);
494 while ($obj = $this->db->fetch_object($resql)) {
496 $line->id = $obj->rowid;
497 $line->ref = $obj->rowid;
499 $line->fk_loan = $obj->fk_loan;
500 $line->datec = $this->db->jdate($obj->datec);
501 $line->tms = $this->db->jdate($obj->tms);
502 $line->datep = $this->db->jdate($obj->datep);
503 $line->amount_capital = $obj->amount_capital;
504 $line->amount_insurance = $obj->amount_insurance;
505 $line->amount_interest = $obj->amount_interest;
506 $line->fk_typepayment = $obj->fk_typepayment;
507 $line->num_payment = $obj->num_payment;
508 $line->note_private = $obj->note_private;
509 $line->note_public = $obj->note_public;
510 $line->fk_bank = $obj->fk_bank;
511 $line->fk_payment_loan = $obj->fk_payment_loan;
512 $line->fk_user_creat = $obj->fk_user_creat;
513 $line->fk_user_modif = $obj->fk_user_modif;
515 $this->lines[] = $line;
517 $this->db->free($resql);
520 $this->error =
"Error ".$this->db->lasterror();
532 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
533 require_once DOL_DOCUMENT_ROOT.
'/core/lib/loan.lib.php';
534 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
538 $sql =
"SELECT l.rowid";
539 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
540 $sql .=
" WHERE l.paid = 0";
541 $resql = $this->db->query($sql);
544 while ($obj = $this->db->fetch_object($resql)) {
547 if (count($toinsert) > 0) {
548 foreach ($toinsert as $echid) {
550 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan ";
551 $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) ";
552 $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";
553 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan_schedule WHERE rowid =".((int) $echid);
554 $res = $this->db->query($sql);
558 $this->db->rollback();