131 global $conf, $langs;
139 $this->error =
'ErrorBadValueForParameter';
144 if (isset($this->fk_loan)) {
145 $this->fk_loan = (int) $this->fk_loan;
147 if (isset($this->amount_capital)) {
148 $this->amount_capital = trim($this->amount_capital ? $this->amount_capital : 0);
150 if (isset($this->amount_insurance)) {
151 $this->amount_insurance = trim($this->amount_insurance ? $this->amount_insurance : 0);
153 if (isset($this->amount_interest)) {
154 $this->amount_interest = trim($this->amount_interest ? $this->amount_interest : 0);
156 if (isset($this->fk_typepayment)) {
157 $this->fk_typepayment = (int) $this->fk_typepayment;
159 if (isset($this->fk_bank)) {
160 $this->fk_bank = (int) $this->fk_bank;
162 if (isset($this->fk_user_creat)) {
163 $this->fk_user_creat = (int) $this->fk_user_creat;
165 if (isset($this->fk_user_modif)) {
166 $this->fk_user_modif = (int) $this->fk_user_modif;
169 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
173 if ($totalamount == 0) {
174 $this->errors[] =
'Amount must not be "0".';
181 if ($totalamount != 0) {
182 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
183 $sql .=
" fk_typepayment, fk_user_creat, fk_bank)";
184 $sql .=
" VALUES (".$this->fk_loan.
", '".$this->db->idate($now).
"',";
185 $sql .=
" '".$this->db->idate($this->datep).
"',";
186 $sql .=
" ".price2num($this->amount_capital).
",";
187 $sql .=
" ".price2num($this->amount_insurance).
",";
188 $sql .=
" ".price2num($this->amount_interest).
",";
189 $sql .=
" ".price2num($this->fk_typepayment).
", ";
190 $sql .=
" ".((int) $user->id).
",";
191 $sql .=
" ".((int) $this->fk_bank).
")";
193 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
194 $resql = $this->db->query($sql);
196 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
198 $this->error = $this->db->lasterror();
203 if ($totalamount != 0 && !$error) {
204 $this->amount_capital = $totalamount;
208 $this->errors[] = $this->db->lasterror();
209 $this->db->rollback();
225 $sql .=
" t.fk_loan,";
229 $sql .=
" t.amount_capital,";
230 $sql .=
" t.amount_insurance,";
231 $sql .=
" t.amount_interest,";
232 $sql .=
" t.fk_typepayment,";
233 $sql .=
" t.num_payment,";
234 $sql .=
" t.note_private,";
235 $sql .=
" t.note_public,";
236 $sql .=
" t.fk_bank,";
237 $sql .=
" t.fk_payment_loan,";
238 $sql .=
" t.fk_user_creat,";
239 $sql .=
" t.fk_user_modif,";
240 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
241 $sql .=
' b.fk_account';
242 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
243 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
244 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
245 $sql .=
" WHERE t.rowid = ".((int) $id);
247 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
248 $resql = $this->db->query($sql);
250 if ($this->db->num_rows($resql)) {
251 $obj = $this->db->fetch_object($resql);
253 $this->
id = $obj->rowid;
254 $this->
ref = $obj->rowid;
256 $this->fk_loan = $obj->fk_loan;
257 $this->datec = $this->db->jdate($obj->datec);
258 $this->tms = $this->db->jdate($obj->tms);
259 $this->datep = $this->db->jdate($obj->datep);
260 $this->amount_capital = $obj->amount_capital;
261 $this->amount_insurance = $obj->amount_insurance;
262 $this->amount_interest = $obj->amount_interest;
263 $this->fk_typepayment = $obj->fk_typepayment;
264 $this->num_payment = $obj->num_payment;
265 $this->note_private = $obj->note_private;
266 $this->note_public = $obj->note_public;
267 $this->fk_bank = $obj->fk_bank;
268 $this->fk_payment_loan = $obj->fk_payment_loan;
269 $this->fk_user_creat = $obj->fk_user_creat;
270 $this->fk_user_modif = $obj->fk_user_modif;
272 $this->type_code = $obj->type_code;
273 $this->type_label = $obj->type_label;
275 $this->bank_account = $obj->fk_account;
276 $this->bank_line = $obj->fk_bank;
278 $this->db->free($resql);
282 $this->error =
"Error ".$this->db->lasterror();
295 public function update($user = 0, $notrigger = 0)
297 global $conf, $langs;
301 if (isset($this->amount_capital)) {
302 $this->amount_capital = trim($this->amount_capital);
304 if (isset($this->amount_insurance)) {
305 $this->amount_insurance = trim($this->amount_insurance);
307 if (isset($this->amount_interest)) {
308 $this->amount_interest = trim($this->amount_interest);
310 if (isset($this->num_payment)) {
311 $this->num_payment = trim($this->num_payment);
313 if (isset($this->note_private)) {
314 $this->note_private = trim($this->note_private);
316 if (isset($this->note_public)) {
317 $this->note_public = trim($this->note_public);
319 if (isset($this->fk_bank)) {
320 $this->fk_bank = trim($this->fk_bank);
322 if (isset($this->fk_payment_loan)) {
323 $this->fk_payment_loan = (int) $this->fk_payment_loan;
330 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
332 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
333 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
334 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
335 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
336 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
337 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
338 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
339 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
340 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
341 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
342 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
343 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
344 $sql .=
" fk_payment_loan=".(isset($this->fk_payment_loan) ? ((int) $this->fk_payment_loan) :
"null").
",";
345 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
346 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
348 $sql .=
" WHERE rowid=".((int) $this->
id);
352 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
353 $resql = $this->db->query($sql);
356 $this->errors[] =
"Error ".$this->db->lasterror();
361 $this->db->rollback();
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();