165 public function create($user, $notrigger = 0)
173 $this->error =
'ErrorBadValueForParameter';
178 if (isset($this->fk_loan)) {
179 $this->fk_loan = (int) $this->fk_loan;
181 if (isset($this->amount_capital)) {
182 $this->amount_capital = (float)
price2num($this->amount_capital ? $this->amount_capital : 0);
184 if (isset($this->amount_insurance)) {
185 $this->amount_insurance = (float)
price2num($this->amount_insurance ? $this->amount_insurance : 0);
187 if (isset($this->amount_interest)) {
188 $this->amount_interest = (float)
price2num($this->amount_interest ? $this->amount_interest : 0);
190 if (isset($this->fk_typepayment)) {
191 $this->fk_typepayment = (int) $this->fk_typepayment;
193 if (isset($this->num_payment)) {
194 $this->num_payment = trim($this->num_payment);
196 if (isset($this->note_private)) {
197 $this->note_private = trim($this->note_private);
199 if (isset($this->note_public)) {
200 $this->note_public = trim($this->note_public);
202 if (isset($this->fk_bank)) {
203 $this->fk_bank = (int) $this->fk_bank;
205 if (isset($this->fk_user_creat)) {
206 $this->fk_user_creat = (int) $this->fk_user_creat;
208 if (isset($this->fk_user_modif)) {
209 $this->fk_user_modif = (int) $this->fk_user_modif;
212 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
213 $totalamount = (float)
price2num($totalamount);
216 if ($totalamount == 0) {
223 if ($totalamount != 0) {
224 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
225 $sql .=
" fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
226 $sql .=
" VALUES (".((int) $this->chid).
", '".$this->db->idate($now).
"',";
227 $sql .=
" '".$this->db->idate($this->datep).
"',";
228 $sql .=
" ".price2num($this->amount_capital).
",";
229 $sql .=
" ".price2num($this->amount_insurance).
",";
230 $sql .=
" ".price2num($this->amount_interest).
",";
231 $sql .=
" ".((int) $this->paymenttype).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', '".$this->db->escape($this->note_public).
"', ".((int) $user->id).
",";
234 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
235 $resql = $this->db->query($sql);
237 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
239 $this->error = $this->db->lasterror();
244 if ($totalamount != 0 && !$error && !$notrigger) {
246 $result = $this->call_trigger(
'PAYMENTLOAN_CREATE', $user);
253 if ($totalamount != 0 && !$error) {
254 $this->amount_capital = $totalamount;
258 $this->error = $this->db->lasterror();
259 $this->db->rollback();
274 $sql .=
" t.fk_loan,";
278 $sql .=
" t.amount_capital,";
279 $sql .=
" t.amount_insurance,";
280 $sql .=
" t.amount_interest,";
281 $sql .=
" t.fk_typepayment,";
282 $sql .=
" t.num_payment,";
283 $sql .=
" t.note_private,";
284 $sql .=
" t.note_public,";
285 $sql .=
" t.fk_bank,";
286 $sql .=
" t.fk_user_creat,";
287 $sql .=
" t.fk_user_modif,";
288 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
289 $sql .=
' b.fk_account';
290 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan as t";
291 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
292 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
293 $sql .=
" WHERE t.rowid = ".((int) $id);
295 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
296 $resql = $this->db->query($sql);
298 if ($this->db->num_rows($resql)) {
299 $obj = $this->db->fetch_object($resql);
301 $this->
id = $obj->rowid;
302 $this->
ref = $obj->rowid;
304 $this->fk_loan = $obj->fk_loan;
305 $this->datec = $this->db->jdate($obj->datec);
306 $this->tms = $this->db->jdate($obj->tms);
307 $this->datep = $this->db->jdate($obj->datep);
308 $this->amount_capital = $obj->amount_capital;
309 $this->amount_insurance = $obj->amount_insurance;
310 $this->amount_interest = $obj->amount_interest;
311 $this->fk_typepayment = $obj->fk_typepayment;
312 $this->num_payment = $obj->num_payment;
313 $this->note_private = $obj->note_private;
314 $this->note_public = $obj->note_public;
315 $this->fk_bank = $obj->fk_bank;
316 $this->fk_user_creat = $obj->fk_user_creat;
317 $this->fk_user_modif = $obj->fk_user_modif;
319 $this->type_code = $obj->type_code;
320 $this->type_label = $obj->type_label;
322 $this->bank_account = $obj->fk_account;
323 $this->bank_line = $obj->fk_bank;
325 $this->db->free($resql);
329 $this->error =
"Error ".$this->db->lasterror();
342 public function update($user =
null, $notrigger = 0)
347 if (isset($this->fk_loan)) {
348 $this->fk_loan = (int) $this->fk_loan;
350 if (isset($this->amount_capital)) {
351 $this->amount_capital = (float) $this->amount_capital;
353 if (isset($this->amount_insurance)) {
354 $this->amount_insurance = (float) $this->amount_insurance;
356 if (isset($this->amount_interest)) {
357 $this->amount_interest = (float) $this->amount_interest;
359 if (isset($this->fk_typepayment)) {
360 $this->fk_typepayment = (int) $this->fk_typepayment;
362 if (isset($this->num_payment)) {
363 $this->num_payment = trim($this->num_payment);
365 if (isset($this->note_private)) {
366 $this->note_private = trim($this->note_private);
368 if (isset($this->note_public)) {
369 $this->note_public = trim($this->note_public);
371 if (isset($this->fk_bank)) {
372 $this->fk_bank = (int) $this->fk_bank;
374 if (isset($this->fk_user_creat)) {
375 $this->fk_user_creat = (int) $this->fk_user_creat;
377 if (isset($this->fk_user_modif)) {
378 $this->fk_user_modif = (int) $this->fk_user_modif;
384 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_loan SET";
385 $sql .=
" fk_loan=".(isset($this->fk_loan) ? ((int) $this->fk_loan) :
"null").
",";
386 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
387 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
388 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
389 $sql .=
" amount_capital=".(isset($this->amount_capital) ? ((float) $this->amount_capital) :
"null").
",";
390 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? ((float) $this->amount_insurance) :
"null").
",";
391 $sql .=
" amount_interest=".(isset($this->amount_interest) ? ((float) $this->amount_interest) :
"null").
",";
392 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? ((int) $this->fk_typepayment) :
"null").
",";
393 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
394 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
395 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
396 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
397 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
398 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
399 $sql .=
" WHERE rowid=".((int) $this->
id);
403 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
404 $resql = $this->db->query($sql);
407 $this->errors[] =
"Error ".$this->db->lasterror();
410 if (!$error && $user && !$notrigger) {
412 $result = $this->call_trigger(
'PAYMENTLOAN_MODIFY', $user);
421 foreach ($this->errors as $errmsg) {
422 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
423 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
425 $this->db->rollback();
441 public function delete($user, $notrigger = 0)
448 if ($this->fk_bank > 0) {
449 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"loan_schedule SET fk_bank = 0 WHERE fk_bank = ".((int) $this->fk_bank);
450 if (!$this->db->query($sql)) {
452 $this->errors[] =
"Error ".$this->db->lasterror();
456 if (!$error && $this->bank_line > 0) {
458 $accline->fetch($this->bank_line);
459 $result = $accline->delete($user);
461 $this->errors[] = $accline->error;
467 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan";
468 $sql .=
" WHERE rowid=".((int) $this->
id);
470 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
471 $resql = $this->db->query($sql);
474 $this->errors[] =
"Error ".$this->db->lasterror();
480 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
481 $loan =
new Loan($this->db);
482 $loan->fetch($this->fk_loan);
483 $sum_payment = $loan->getSumPayment();
484 if ($sum_payment == 0) {
485 dol_syslog(get_class($this).
"::delete : set loan to unpaid", LOG_DEBUG);
486 if ($loan->setUnpaid($user) < 1) {
488 dol_print_error($this->db);
493 if (!$error && !$notrigger) {
495 $result = $this->call_trigger(
'PAYMENTLOAN_DELETE', $user);
504 foreach ($this->errors as $errmsg) {
505 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
506 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
508 $this->db->rollback();
554 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
560 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
563 $acc->fetch($accountid);
565 $total = (float) $this->amount_capital;
566 if ($mode ==
'payment_loan') {
571 $bank_line_id = $acc->addline(
585 if ($bank_line_id > 0) {
589 dol_print_error($this->db);
594 if ($mode ==
'payment_loan') {
595 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id=';
598 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(payment)', $mode);
601 dol_print_error($this->db);
607 if ($mode ==
'payment_loan') {
608 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.
'/loan/card.php?id=', ($this->label ? $this->label :
''),
'loan');
610 dol_print_error($this->db);
614 $this->error = $acc->error;
622 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
623 $loan =
new Loan($this->db);
624 $loan->fetch($fk_loan);
625 if ($loan->paid == $loan::STATUS_UNPAID) {
626 dol_syslog(get_class($this).
"::addPaymentToBank : set loan payment to started", LOG_DEBUG);
627 if ($loan->setStarted($user) < 1) {
629 dol_print_error($this->db);
638 $this->db->rollback();
677 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle =
'', $save_lastsearch_value = -1)
679 global $langs,
$conf, $hookmanager;
681 if (!empty(
$conf->dol_no_mouse_hover)) {
686 $label =
'<u>'.$langs->trans(
"Loan").
'</u>';
687 if (!empty($this->
id)) {
688 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->id;
691 $label .=
' - '.$moretitle;
694 $baseurl = DOL_URL_ROOT.
'/loan/payment/card.php';
695 $query = [
'id' => $this->id];
697 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
698 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
699 $add_save_lastsearch_values = 1;
701 if ($add_save_lastsearch_values) {
702 $query += [
'save_lastsearch_values' => 1];
706 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
709 $result .= $linkstart;
711 $result .= img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
713 if ($withpicto != 2) {
714 $result .= $this->ref;
719 $hookmanager->initHooks(array($this->element .
'dao'));
720 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
721 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
723 $result = $hookmanager->resPrint;
725 $result .= $hookmanager->resPrint;