158 global
$conf, $langs;
166 $this->error =
'ErrorBadValueForParameter';
171 if (isset($this->fk_loan)) {
172 $this->fk_loan = (int) $this->fk_loan;
174 if (isset($this->amount_capital)) {
175 $this->amount_capital = trim($this->amount_capital ? $this->amount_capital : 0);
177 if (isset($this->amount_insurance)) {
178 $this->amount_insurance = trim($this->amount_insurance ? $this->amount_insurance : 0);
180 if (isset($this->amount_interest)) {
181 $this->amount_interest = trim($this->amount_interest ? $this->amount_interest : 0);
183 if (isset($this->fk_typepayment)) {
184 $this->fk_typepayment = (int) $this->fk_typepayment;
186 if (isset($this->fk_bank)) {
187 $this->fk_bank = (int) $this->fk_bank;
189 if (isset($this->fk_user_creat)) {
190 $this->fk_user_creat = (int) $this->fk_user_creat;
192 if (isset($this->fk_user_modif)) {
193 $this->fk_user_modif = (int) $this->fk_user_modif;
196 $totalamount = (float) $this->amount_capital + (
float) $this->amount_insurance + (float) $this->amount_interest;
200 if ($totalamount == 0) {
201 $this->errors[] =
'Amount must not be "0".';
208 if ($totalamount != 0) {
209 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
210 $sql .=
" fk_typepayment, fk_user_creat, fk_bank)";
211 $sql .=
" VALUES (".$this->fk_loan.
", '".$this->db->idate($now).
"',";
212 $sql .=
" '".$this->db->idate($this->datep).
"',";
213 $sql .=
" ".price2num($this->amount_capital).
",";
214 $sql .=
" ".price2num($this->amount_insurance).
",";
215 $sql .=
" ".price2num($this->amount_interest).
",";
216 $sql .=
" ".price2num($this->fk_typepayment).
", ";
217 $sql .=
" ".((int) $user->id).
",";
218 $sql .=
" ".((int) $this->fk_bank).
")";
220 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
221 $resql = $this->db->query($sql);
223 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
225 $this->error = $this->db->lasterror();
230 if ($totalamount != 0 && !$error) {
231 $this->amount_capital = $totalamount;
235 $this->errors[] = $this->db->lasterror();
236 $this->db->rollback();
252 $sql .=
" t.fk_loan,";
256 $sql .=
" t.amount_capital,";
257 $sql .=
" t.amount_insurance,";
258 $sql .=
" t.amount_interest,";
259 $sql .=
" t.fk_typepayment,";
260 $sql .=
" t.num_payment,";
261 $sql .=
" t.note_private,";
262 $sql .=
" t.note_public,";
263 $sql .=
" t.fk_bank,";
264 $sql .=
" t.fk_payment_loan,";
265 $sql .=
" t.fk_user_creat,";
266 $sql .=
" t.fk_user_modif,";
267 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
268 $sql .=
' b.fk_account';
269 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
270 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
271 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
272 $sql .=
" WHERE t.rowid = ".((int) $id);
274 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
275 $resql = $this->db->query($sql);
277 if ($this->db->num_rows($resql)) {
278 $obj = $this->db->fetch_object($resql);
280 $this->
id = $obj->rowid;
281 $this->
ref = $obj->rowid;
283 $this->fk_loan = $obj->fk_loan;
284 $this->datec = $this->db->jdate($obj->datec);
285 $this->tms = $this->db->jdate($obj->tms);
286 $this->datep = $this->db->jdate($obj->datep);
287 $this->amount_capital = $obj->amount_capital;
288 $this->amount_insurance = $obj->amount_insurance;
289 $this->amount_interest = $obj->amount_interest;
290 $this->fk_typepayment = $obj->fk_typepayment;
291 $this->num_payment = $obj->num_payment;
292 $this->note_private = $obj->note_private;
293 $this->note_public = $obj->note_public;
294 $this->fk_bank = $obj->fk_bank;
295 $this->fk_payment_loan = $obj->fk_payment_loan;
296 $this->fk_user_creat = $obj->fk_user_creat;
297 $this->fk_user_modif = $obj->fk_user_modif;
299 $this->type_code = $obj->type_code;
300 $this->type_label = $obj->type_label;
302 $this->bank_account = $obj->fk_account;
303 $this->bank_line = $obj->fk_bank;
305 $this->db->free($resql);
309 $this->error =
"Error ".$this->db->lasterror();
322 public function update($user =
null, $notrigger = 0)
324 global
$conf, $langs;
328 if (isset($this->amount_capital)) {
329 $this->amount_capital = trim($this->amount_capital);
331 if (isset($this->amount_insurance)) {
332 $this->amount_insurance = trim($this->amount_insurance);
334 if (isset($this->amount_interest)) {
335 $this->amount_interest = trim($this->amount_interest);
337 if (isset($this->num_payment)) {
338 $this->num_payment = trim($this->num_payment);
340 if (isset($this->note_private)) {
341 $this->note_private = trim($this->note_private);
343 if (isset($this->note_public)) {
344 $this->note_public = trim($this->note_public);
346 if (isset($this->fk_bank)) {
347 $this->fk_bank = (int) $this->fk_bank;
349 if (isset($this->fk_payment_loan)) {
350 $this->fk_payment_loan = (int) $this->fk_payment_loan;
357 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
359 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
360 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
361 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
362 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
363 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
364 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
365 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
366 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
367 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
368 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
369 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
370 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
371 $sql .=
" fk_payment_loan=".(isset($this->fk_payment_loan) ? ((int) $this->fk_payment_loan) :
"null").
",";
372 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
373 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
375 $sql .=
" WHERE rowid=".((int) $this->
id);
379 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
380 $resql = $this->db->query($sql);
383 $this->errors[] =
"Error ".$this->db->lasterror();
388 $this->db->rollback();
449 if (!empty($capital) && !empty($nbterm)) {
451 $result = ($capital * ($rate / 12)) / (1 - pow((1 + ($rate / 12)), ($nbterm * -1)));
453 $result = $capital / $nbterm;
471 $sql .=
" t.fk_loan,";
475 $sql .=
" t.amount_capital,";
476 $sql .=
" t.amount_insurance,";
477 $sql .=
" t.amount_interest,";
478 $sql .=
" t.fk_typepayment,";
479 $sql .=
" t.num_payment,";
480 $sql .=
" t.note_private,";
481 $sql .=
" t.note_public,";
482 $sql .=
" t.fk_bank,";
483 $sql .=
" t.fk_payment_loan,";
484 $sql .=
" t.fk_user_creat,";
485 $sql .=
" t.fk_user_modif";
486 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
487 $sql .=
" WHERE t.fk_loan = ".((int) $loanid);
489 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
490 $resql = $this->db->query($sql);
493 while ($obj = $this->db->fetch_object($resql)) {
495 $line->id = $obj->rowid;
496 $line->ref = $obj->rowid;
498 $line->fk_loan = $obj->fk_loan;
499 $line->datec = $this->db->jdate($obj->datec);
500 $line->tms = $this->db->jdate($obj->tms);
501 $line->datep = $this->db->jdate($obj->datep);
502 $line->amount_capital = $obj->amount_capital;
503 $line->amount_insurance = $obj->amount_insurance;
504 $line->amount_interest = $obj->amount_interest;
505 $line->fk_typepayment = $obj->fk_typepayment;
506 $line->num_payment = $obj->num_payment;
507 $line->note_private = $obj->note_private;
508 $line->note_public = $obj->note_public;
509 $line->fk_bank = $obj->fk_bank;
510 $line->fk_payment_loan = $obj->fk_payment_loan;
511 $line->fk_user_creat = $obj->fk_user_creat;
512 $line->fk_user_modif = $obj->fk_user_modif;
514 $this->lines[] = $line;
516 $this->db->free($resql);
519 $this->error =
"Error ".$this->db->lasterror();
531 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
532 require_once DOL_DOCUMENT_ROOT.
'/core/lib/loan.lib.php';
533 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
537 $sql =
"SELECT l.rowid";
538 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
539 $sql .=
" WHERE l.paid = 0";
540 $resql = $this->db->query($sql);
543 while ($obj = $this->db->fetch_object($resql)) {
546 if (count($toinsert) > 0) {
547 foreach ($toinsert as $echid) {
549 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan ";
550 $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) ";
551 $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";
552 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan_schedule WHERE rowid =".((int) $echid);
553 $res = $this->db->query($sql);
557 $this->db->rollback();