158 public function create($user, $notrigger = 0)
160 global
$conf, $langs;
168 $this->error =
'ErrorBadValueForParameter';
173 if (isset($this->fk_loan)) {
174 $this->fk_loan = (int) $this->fk_loan;
176 if (isset($this->amount_capital)) {
177 $this->amount_capital = trim($this->amount_capital ? $this->amount_capital : 0);
179 if (isset($this->amount_insurance)) {
180 $this->amount_insurance = trim($this->amount_insurance ? $this->amount_insurance : 0);
182 if (isset($this->amount_interest)) {
183 $this->amount_interest = trim($this->amount_interest ? $this->amount_interest : 0);
185 if (isset($this->fk_typepayment)) {
186 $this->fk_typepayment = (int) $this->fk_typepayment;
188 if (isset($this->fk_bank)) {
189 $this->fk_bank = (int) $this->fk_bank;
191 if (isset($this->fk_user_creat)) {
192 $this->fk_user_creat = (int) $this->fk_user_creat;
194 if (isset($this->fk_user_modif)) {
195 $this->fk_user_modif = (int) $this->fk_user_modif;
198 $totalamount = (float) $this->amount_capital + (
float) $this->amount_insurance + (float) $this->amount_interest;
202 if ($totalamount == 0) {
203 $this->errors[] =
'Amount must not be "0".';
210 if ($totalamount != 0) {
211 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
212 $sql .=
" fk_typepayment, fk_user_creat, fk_bank)";
213 $sql .=
" VALUES (".((int) $this->fk_loan).
", '".$this->db->idate($now).
"',";
214 $sql .=
" '".$this->db->idate($this->datep).
"',";
215 $sql .=
" ".price2num($this->amount_capital).
",";
216 $sql .=
" ".price2num($this->amount_insurance).
",";
217 $sql .=
" ".price2num($this->amount_interest).
",";
218 $sql .=
" ".price2num($this->fk_typepayment).
", ";
219 $sql .=
" ".((int) $user->id).
",";
220 $sql .=
" ".((int) $this->fk_bank).
")";
222 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
223 $resql = $this->db->query($sql);
225 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"loan_schedule");
227 $this->error = $this->db->lasterror();
232 if ($totalamount != 0 && !$error && !$notrigger) {
234 $result = $this->call_trigger(
'LOANSCHEDULE_CREATE', $user);
241 if ($totalamount != 0 && !$error) {
242 $this->amount_capital = $totalamount;
246 $this->errors[] = $this->db->lasterror();
247 $this->db->rollback();
263 $sql .=
" t.fk_loan,";
267 $sql .=
" t.amount_capital,";
268 $sql .=
" t.amount_insurance,";
269 $sql .=
" t.amount_interest,";
270 $sql .=
" t.fk_typepayment,";
271 $sql .=
" t.num_payment,";
272 $sql .=
" t.note_private,";
273 $sql .=
" t.note_public,";
274 $sql .=
" t.fk_bank,";
275 $sql .=
" t.fk_payment_loan,";
276 $sql .=
" t.fk_user_creat,";
277 $sql .=
" t.fk_user_modif,";
278 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
279 $sql .=
' b.fk_account';
280 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
281 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
282 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
283 $sql .=
" WHERE t.rowid = ".((int) $id);
285 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
286 $resql = $this->db->query($sql);
288 if ($this->db->num_rows($resql)) {
289 $obj = $this->db->fetch_object($resql);
291 $this->
id = $obj->rowid;
292 $this->
ref = $obj->rowid;
294 $this->fk_loan = $obj->fk_loan;
295 $this->datec = $this->db->jdate($obj->datec);
296 $this->tms = $this->db->jdate($obj->tms);
297 $this->datep = $this->db->jdate($obj->datep);
298 $this->amount_capital = $obj->amount_capital;
299 $this->amount_insurance = $obj->amount_insurance;
300 $this->amount_interest = $obj->amount_interest;
301 $this->fk_typepayment = $obj->fk_typepayment;
302 $this->num_payment = $obj->num_payment;
303 $this->note_private = $obj->note_private;
304 $this->note_public = $obj->note_public;
305 $this->fk_bank = $obj->fk_bank;
306 $this->fk_payment_loan = $obj->fk_payment_loan;
307 $this->fk_user_creat = $obj->fk_user_creat;
308 $this->fk_user_modif = $obj->fk_user_modif;
310 $this->type_code = $obj->type_code;
311 $this->type_label = $obj->type_label;
313 $this->bank_account = $obj->fk_account;
314 $this->bank_line = $obj->fk_bank;
316 $this->db->free($resql);
320 $this->error =
"Error ".$this->db->lasterror();
333 public function update($user =
null, $notrigger = 0)
335 global
$conf, $langs;
339 if (isset($this->amount_capital)) {
340 $this->amount_capital = trim($this->amount_capital);
342 if (isset($this->amount_insurance)) {
343 $this->amount_insurance = trim($this->amount_insurance);
345 if (isset($this->amount_interest)) {
346 $this->amount_interest = trim($this->amount_interest);
348 if (isset($this->num_payment)) {
349 $this->num_payment = trim($this->num_payment);
351 if (isset($this->note_private)) {
352 $this->note_private = trim($this->note_private);
354 if (isset($this->note_public)) {
355 $this->note_public = trim($this->note_public);
357 if (isset($this->fk_bank)) {
358 $this->fk_bank = (int) $this->fk_bank;
360 if (isset($this->fk_payment_loan)) {
361 $this->fk_payment_loan = (int) $this->fk_payment_loan;
368 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
370 $sql .=
" fk_loan=".(isset($this->fk_loan) ? ((int) $this->fk_loan) :
"null").
",";
371 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
372 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
373 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
374 $sql .=
" amount_capital=".(isset($this->amount_capital) ? ((float) $this->amount_capital) :
"null").
",";
375 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? ((float) $this->amount_insurance) :
"null").
",";
376 $sql .=
" amount_interest=".(isset($this->amount_interest) ? ((float) $this->amount_interest) :
"null").
",";
377 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? ((int) $this->fk_typepayment) :
"null").
",";
378 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
379 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
380 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
381 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
382 $sql .=
" fk_payment_loan=".(isset($this->fk_payment_loan) ? ((int) $this->fk_payment_loan) :
"null").
",";
383 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
384 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
386 $sql .=
" WHERE rowid=".((int) $this->
id);
390 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
391 $resql = $this->db->query($sql);
394 $this->errors[] =
"Error ".$this->db->lasterror();
397 if (!$error && $user && !$notrigger) {
399 $result = $this->call_trigger(
'LOANSCHEDULE_MODIFY', $user);
408 $this->db->rollback();
424 public function delete($user, $notrigger = 0)
426 global
$conf, $langs;
432 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
433 $sql .=
" WHERE rowid=".((int) $this->
id);
435 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
436 $resql = $this->db->query($sql);
439 $this->errors[] =
"Error ".$this->db->lasterror();
443 if (!$error && !$notrigger) {
445 $result = $this->call_trigger(
'LOANSCHEDULE_DELETE', $user);
454 foreach ($this->errors as $errmsg) {
455 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
456 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
458 $this->db->rollback();
478 if (!empty($capital) && !empty($nbterm)) {
480 $result = ($capital * ($rate / 12)) / (1 - pow((1 + ($rate / 12)), ($nbterm * -1)));
482 $result = $capital / $nbterm;
500 $sql .=
" t.fk_loan,";
504 $sql .=
" t.amount_capital,";
505 $sql .=
" t.amount_insurance,";
506 $sql .=
" t.amount_interest,";
507 $sql .=
" t.fk_typepayment,";
508 $sql .=
" t.num_payment,";
509 $sql .=
" t.note_private,";
510 $sql .=
" t.note_public,";
511 $sql .=
" t.fk_bank,";
512 $sql .=
" t.fk_payment_loan,";
513 $sql .=
" t.fk_user_creat,";
514 $sql .=
" t.fk_user_modif";
515 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
516 $sql .=
" WHERE t.fk_loan = ".((int) $loanid);
518 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
519 $resql = $this->db->query($sql);
522 while ($obj = $this->db->fetch_object($resql)) {
524 $line->id = $obj->rowid;
525 $line->ref = $obj->rowid;
527 $line->fk_loan = $obj->fk_loan;
528 $line->datec = $this->db->jdate($obj->datec);
529 $line->tms = $this->db->jdate($obj->tms);
530 $line->datep = $this->db->jdate($obj->datep);
531 $line->amount_capital = $obj->amount_capital;
532 $line->amount_insurance = $obj->amount_insurance;
533 $line->amount_interest = $obj->amount_interest;
534 $line->fk_typepayment = $obj->fk_typepayment;
535 $line->num_payment = $obj->num_payment;
536 $line->note_private = $obj->note_private;
537 $line->note_public = $obj->note_public;
538 $line->fk_bank = $obj->fk_bank;
539 $line->fk_payment_loan = $obj->fk_payment_loan;
540 $line->fk_user_creat = $obj->fk_user_creat;
541 $line->fk_user_modif = $obj->fk_user_modif;
543 $this->lines[] = $line;
545 $this->db->free($resql);
548 $this->error =
"Error ".$this->db->lasterror();
560 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
561 require_once DOL_DOCUMENT_ROOT.
'/core/lib/loan.lib.php';
562 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
566 $sql =
"SELECT l.rowid";
567 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
568 $sql .=
" WHERE l.paid = 0";
569 $resql = $this->db->query($sql);
572 while ($obj = $this->db->fetch_object($resql)) {
575 if (count($toinsert) > 0) {
576 foreach ($toinsert as $echid) {
578 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan ";
579 $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) ";
580 $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";
581 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan_schedule WHERE rowid =".((int) $echid);
582 $res = $this->db->query($sql);
586 $this->db->rollback();