133 global $conf, $langs;
141 $this->error =
'ErrorBadValueForParameter';
146 if (isset($this->fk_loan)) {
147 $this->fk_loan = (int) $this->fk_loan;
149 if (isset($this->amount_capital)) {
150 $this->amount_capital = trim($this->amount_capital ? $this->amount_capital : 0);
152 if (isset($this->amount_insurance)) {
153 $this->amount_insurance = trim($this->amount_insurance ? $this->amount_insurance : 0);
155 if (isset($this->amount_interest)) {
156 $this->amount_interest = trim($this->amount_interest ? $this->amount_interest : 0);
158 if (isset($this->fk_typepayment)) {
159 $this->fk_typepayment = (int) $this->fk_typepayment;
161 if (isset($this->fk_bank)) {
162 $this->fk_bank = (int) $this->fk_bank;
164 if (isset($this->fk_user_creat)) {
165 $this->fk_user_creat = (int) $this->fk_user_creat;
167 if (isset($this->fk_user_modif)) {
168 $this->fk_user_modif = (int) $this->fk_user_modif;
171 $totalamount = (float) $this->amount_capital + (
float) $this->amount_insurance + (float) $this->amount_interest;
175 if ($totalamount == 0) {
176 $this->errors[] =
'Amount must not be "0".';
183 if ($totalamount != 0) {
184 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
185 $sql .=
" fk_typepayment, fk_user_creat, fk_bank)";
186 $sql .=
" VALUES (".$this->fk_loan.
", '".$this->db->idate($now).
"',";
187 $sql .=
" '".$this->db->idate($this->datep).
"',";
188 $sql .=
" ".price2num($this->amount_capital).
",";
189 $sql .=
" ".price2num($this->amount_insurance).
",";
190 $sql .=
" ".price2num($this->amount_interest).
",";
191 $sql .=
" ".price2num($this->fk_typepayment).
", ";
192 $sql .=
" ".((int) $user->id).
",";
193 $sql .=
" ".((int) $this->fk_bank).
")";
195 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
196 $resql = $this->db->query($sql);
198 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
200 $this->error = $this->db->lasterror();
205 if ($totalamount != 0 && !$error) {
206 $this->amount_capital = $totalamount;
210 $this->errors[] = $this->db->lasterror();
211 $this->db->rollback();
227 $sql .=
" t.fk_loan,";
231 $sql .=
" t.amount_capital,";
232 $sql .=
" t.amount_insurance,";
233 $sql .=
" t.amount_interest,";
234 $sql .=
" t.fk_typepayment,";
235 $sql .=
" t.num_payment,";
236 $sql .=
" t.note_private,";
237 $sql .=
" t.note_public,";
238 $sql .=
" t.fk_bank,";
239 $sql .=
" t.fk_payment_loan,";
240 $sql .=
" t.fk_user_creat,";
241 $sql .=
" t.fk_user_modif,";
242 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
243 $sql .=
' b.fk_account';
244 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
245 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
246 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
247 $sql .=
" WHERE t.rowid = ".((int) $id);
249 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
250 $resql = $this->db->query($sql);
252 if ($this->db->num_rows($resql)) {
253 $obj = $this->db->fetch_object($resql);
255 $this->
id = $obj->rowid;
256 $this->
ref = $obj->rowid;
258 $this->fk_loan = $obj->fk_loan;
259 $this->datec = $this->db->jdate($obj->datec);
260 $this->tms = $this->db->jdate($obj->tms);
261 $this->datep = $this->db->jdate($obj->datep);
262 $this->amount_capital = $obj->amount_capital;
263 $this->amount_insurance = $obj->amount_insurance;
264 $this->amount_interest = $obj->amount_interest;
265 $this->fk_typepayment = $obj->fk_typepayment;
266 $this->num_payment = $obj->num_payment;
267 $this->note_private = $obj->note_private;
268 $this->note_public = $obj->note_public;
269 $this->fk_bank = $obj->fk_bank;
270 $this->fk_payment_loan = $obj->fk_payment_loan;
271 $this->fk_user_creat = $obj->fk_user_creat;
272 $this->fk_user_modif = $obj->fk_user_modif;
274 $this->type_code = $obj->type_code;
275 $this->type_label = $obj->type_label;
277 $this->bank_account = $obj->fk_account;
278 $this->bank_line = $obj->fk_bank;
280 $this->db->free($resql);
284 $this->error =
"Error ".$this->db->lasterror();
297 public function update($user =
null, $notrigger = 0)
299 global $conf, $langs;
303 if (isset($this->amount_capital)) {
304 $this->amount_capital = trim($this->amount_capital);
306 if (isset($this->amount_insurance)) {
307 $this->amount_insurance = trim($this->amount_insurance);
309 if (isset($this->amount_interest)) {
310 $this->amount_interest = trim($this->amount_interest);
312 if (isset($this->num_payment)) {
313 $this->num_payment = trim($this->num_payment);
315 if (isset($this->note_private)) {
316 $this->note_private = trim($this->note_private);
318 if (isset($this->note_public)) {
319 $this->note_public = trim($this->note_public);
321 if (isset($this->fk_bank)) {
322 $this->fk_bank = (int) $this->fk_bank;
324 if (isset($this->fk_payment_loan)) {
325 $this->fk_payment_loan = (int) $this->fk_payment_loan;
332 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
334 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
335 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
336 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
337 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
338 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
339 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
340 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
341 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
342 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
343 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
344 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
345 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
346 $sql .=
" fk_payment_loan=".(isset($this->fk_payment_loan) ? ((int) $this->fk_payment_loan) :
"null").
",";
347 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
348 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
350 $sql .=
" WHERE rowid=".((int) $this->
id);
354 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
355 $resql = $this->db->query($sql);
358 $this->errors[] =
"Error ".$this->db->lasterror();
363 $this->db->rollback();
424 if (!empty($capital) && !empty($nbterm)) {
426 $result = ($capital * ($rate / 12)) / (1 - pow((1 + ($rate / 12)), ($nbterm * -1)));
428 $result = $capital / $nbterm;
446 $sql .=
" t.fk_loan,";
450 $sql .=
" t.amount_capital,";
451 $sql .=
" t.amount_insurance,";
452 $sql .=
" t.amount_interest,";
453 $sql .=
" t.fk_typepayment,";
454 $sql .=
" t.num_payment,";
455 $sql .=
" t.note_private,";
456 $sql .=
" t.note_public,";
457 $sql .=
" t.fk_bank,";
458 $sql .=
" t.fk_payment_loan,";
459 $sql .=
" t.fk_user_creat,";
460 $sql .=
" t.fk_user_modif";
461 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
462 $sql .=
" WHERE t.fk_loan = ".((int) $loanid);
464 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
465 $resql = $this->db->query($sql);
468 while ($obj = $this->db->fetch_object($resql)) {
470 $line->id = $obj->rowid;
471 $line->ref = $obj->rowid;
473 $line->fk_loan = $obj->fk_loan;
474 $line->datec = $this->db->jdate($obj->datec);
475 $line->tms = $this->db->jdate($obj->tms);
476 $line->datep = $this->db->jdate($obj->datep);
477 $line->amount_capital = $obj->amount_capital;
478 $line->amount_insurance = $obj->amount_insurance;
479 $line->amount_interest = $obj->amount_interest;
480 $line->fk_typepayment = $obj->fk_typepayment;
481 $line->num_payment = $obj->num_payment;
482 $line->note_private = $obj->note_private;
483 $line->note_public = $obj->note_public;
484 $line->fk_bank = $obj->fk_bank;
485 $line->fk_payment_loan = $obj->fk_payment_loan;
486 $line->fk_user_creat = $obj->fk_user_creat;
487 $line->fk_user_modif = $obj->fk_user_modif;
489 $this->lines[] = $line;
491 $this->db->free($resql);
494 $this->error =
"Error ".$this->db->lasterror();
506 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
507 require_once DOL_DOCUMENT_ROOT.
'/core/lib/loan.lib.php';
508 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
512 $sql =
"SELECT l.rowid";
513 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
514 $sql .=
" WHERE l.paid = 0";
515 $resql = $this->db->query($sql);
518 while ($obj = $this->db->fetch_object($resql)) {
521 if (count($toinsert) > 0) {
522 foreach ($toinsert as $echid) {
524 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan ";
525 $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) ";
526 $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";
527 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan_schedule WHERE rowid =".((int) $echid);
528 $res = $this->db->query($sql);
532 $this->db->rollback();