129 global $conf, $langs;
137 $this->error =
'ErrorBadValueForParameter';
142 if (isset($this->fk_loan)) {
143 $this->fk_loan = (int) $this->fk_loan;
145 if (isset($this->amount_capital)) {
146 $this->amount_capital = trim($this->amount_capital ? $this->amount_capital : 0);
148 if (isset($this->amount_insurance)) {
149 $this->amount_insurance = trim($this->amount_insurance ? $this->amount_insurance : 0);
151 if (isset($this->amount_interest)) {
152 $this->amount_interest = trim($this->amount_interest ? $this->amount_interest : 0);
154 if (isset($this->fk_typepayment)) {
155 $this->fk_typepayment = (int) $this->fk_typepayment;
157 if (isset($this->fk_bank)) {
158 $this->fk_bank = (int) $this->fk_bank;
160 if (isset($this->fk_user_creat)) {
161 $this->fk_user_creat = (int) $this->fk_user_creat;
163 if (isset($this->fk_user_modif)) {
164 $this->fk_user_modif = (int) $this->fk_user_modif;
167 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
171 if ($totalamount == 0) {
172 $this->errors[] =
'step1';
179 if ($totalamount != 0) {
180 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
181 $sql .=
" fk_typepayment, fk_user_creat, fk_bank)";
182 $sql .=
" VALUES (".$this->fk_loan.
", '".$this->db->idate($now).
"',";
183 $sql .=
" '".$this->db->idate($this->datep).
"',";
184 $sql .=
" ".price2num($this->amount_capital).
",";
185 $sql .=
" ".price2num($this->amount_insurance).
",";
186 $sql .=
" ".price2num($this->amount_interest).
",";
187 $sql .=
" ".price2num($this->fk_typepayment).
", ";
188 $sql .=
" ".((int) $user->id).
",";
189 $sql .=
" ".((int) $this->fk_bank).
")";
191 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
192 $resql = $this->db->query($sql);
194 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
196 $this->error = $this->db->lasterror();
201 if ($totalamount != 0 && !$error) {
202 $this->amount_capital = $totalamount;
206 $this->errors[] = $this->db->lasterror();
207 $this->db->rollback();
223 $sql .=
" t.fk_loan,";
227 $sql .=
" t.amount_capital,";
228 $sql .=
" t.amount_insurance,";
229 $sql .=
" t.amount_interest,";
230 $sql .=
" t.fk_typepayment,";
231 $sql .=
" t.num_payment,";
232 $sql .=
" t.note_private,";
233 $sql .=
" t.note_public,";
234 $sql .=
" t.fk_bank,";
235 $sql .=
" t.fk_payment_loan,";
236 $sql .=
" t.fk_user_creat,";
237 $sql .=
" t.fk_user_modif,";
238 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
239 $sql .=
' b.fk_account';
240 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
241 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
242 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
243 $sql .=
" WHERE t.rowid = ".((int) $id);
245 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
246 $resql = $this->db->query($sql);
248 if ($this->db->num_rows($resql)) {
249 $obj = $this->db->fetch_object($resql);
251 $this->
id = $obj->rowid;
252 $this->
ref = $obj->rowid;
254 $this->fk_loan = $obj->fk_loan;
255 $this->datec = $this->db->jdate($obj->datec);
256 $this->tms = $this->db->jdate($obj->tms);
257 $this->datep = $this->db->jdate($obj->datep);
258 $this->amount_capital = $obj->amount_capital;
259 $this->amount_insurance = $obj->amount_insurance;
260 $this->amount_interest = $obj->amount_interest;
261 $this->fk_typepayment = $obj->fk_typepayment;
262 $this->num_payment = $obj->num_payment;
263 $this->note_private = $obj->note_private;
264 $this->note_public = $obj->note_public;
265 $this->fk_bank = $obj->fk_bank;
266 $this->fk_payment_loan = $obj->fk_payment_loan;
267 $this->fk_user_creat = $obj->fk_user_creat;
268 $this->fk_user_modif = $obj->fk_user_modif;
270 $this->type_code = $obj->type_code;
271 $this->type_label = $obj->type_label;
273 $this->bank_account = $obj->fk_account;
274 $this->bank_line = $obj->fk_bank;
276 $this->db->free($resql);
280 $this->error =
"Error ".$this->db->lasterror();
293 public function update($user = 0, $notrigger = 0)
295 global $conf, $langs;
299 if (isset($this->amount_capital)) {
300 $this->amount_capital = trim($this->amount_capital);
302 if (isset($this->amount_insurance)) {
303 $this->amount_insurance = trim($this->amount_insurance);
305 if (isset($this->amount_interest)) {
306 $this->amount_interest = trim($this->amount_interest);
308 if (isset($this->num_payment)) {
309 $this->num_payment = trim($this->num_payment);
311 if (isset($this->note_private)) {
312 $this->note_private = trim($this->note_private);
314 if (isset($this->note_public)) {
315 $this->note_public = trim($this->note_public);
317 if (isset($this->fk_bank)) {
318 $this->fk_bank = trim($this->fk_bank);
320 if (isset($this->fk_payment_loan)) {
321 $this->fk_payment_loan = (int) $this->fk_payment_loan;
328 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
330 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
331 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
332 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
333 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
334 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
335 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
336 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
337 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
338 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
339 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
340 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
341 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
342 $sql .=
" fk_payment_loan=".(isset($this->fk_payment_loan) ? ((int) $this->fk_payment_loan) :
"null").
",";
343 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
344 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
346 $sql .=
" WHERE rowid=".((int) $this->
id);
350 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
351 $resql = $this->db->query($sql);
353 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
358 $this->db->rollback();
438 $sql .=
" t.fk_loan,";
442 $sql .=
" t.amount_capital,";
443 $sql .=
" t.amount_insurance,";
444 $sql .=
" t.amount_interest,";
445 $sql .=
" t.fk_typepayment,";
446 $sql .=
" t.num_payment,";
447 $sql .=
" t.note_private,";
448 $sql .=
" t.note_public,";
449 $sql .=
" t.fk_bank,";
450 $sql .=
" t.fk_payment_loan,";
451 $sql .=
" t.fk_user_creat,";
452 $sql .=
" t.fk_user_modif";
453 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
454 $sql .=
" WHERE t.fk_loan = ".((int) $loanid);
456 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
457 $resql = $this->db->query($sql);
460 while ($obj = $this->db->fetch_object($resql)) {
462 $line->id = $obj->rowid;
463 $line->ref = $obj->rowid;
465 $line->fk_loan = $obj->fk_loan;
466 $line->datec = $this->db->jdate($obj->datec);
467 $line->tms = $this->db->jdate($obj->tms);
468 $line->datep = $this->db->jdate($obj->datep);
469 $line->amount_capital = $obj->amount_capital;
470 $line->amount_insurance = $obj->amount_insurance;
471 $line->amount_interest = $obj->amount_interest;
472 $line->fk_typepayment = $obj->fk_typepayment;
473 $line->num_payment = $obj->num_payment;
474 $line->note_private = $obj->note_private;
475 $line->note_public = $obj->note_public;
476 $line->fk_bank = $obj->fk_bank;
477 $line->fk_payment_loan = $obj->fk_payment_loan;
478 $line->fk_user_creat = $obj->fk_user_creat;
479 $line->fk_user_modif = $obj->fk_user_modif;
481 $this->lines[] = $line;
483 $this->db->free($resql);
486 $this->error =
"Error ".$this->db->lasterror();
498 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
499 require_once DOL_DOCUMENT_ROOT.
'/core/lib/loan.lib.php';
500 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
504 $sql =
"SELECT l.rowid";
505 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan as l";
506 $sql .=
" WHERE l.paid = 0";
507 $resql = $this->db->query($sql);
510 while ($obj = $this->db->fetch_object($resql)) {
513 if (count($toinsert) > 0) {
514 foreach ($toinsert as $echid) {
516 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan ";
517 $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) ";
518 $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";
519 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan_schedule WHERE rowid =".((int) $echid);
520 $res = $this->db->query($sql);
524 $this->db->rollback();