172 $this->error =
'ErrorBadValueForParameter';
177 if (isset($this->fk_loan)) {
178 $this->fk_loan = (int) $this->fk_loan;
180 if (isset($this->amount_capital)) {
181 $this->amount_capital = (float)
price2num($this->amount_capital ? $this->amount_capital : 0);
183 if (isset($this->amount_insurance)) {
184 $this->amount_insurance = (float)
price2num($this->amount_insurance ? $this->amount_insurance : 0);
186 if (isset($this->amount_interest)) {
187 $this->amount_interest = (float)
price2num($this->amount_interest ? $this->amount_interest : 0);
189 if (isset($this->fk_typepayment)) {
190 $this->fk_typepayment = (int) $this->fk_typepayment;
192 if (isset($this->num_payment)) {
193 $this->num_payment = trim($this->num_payment);
195 if (isset($this->note_private)) {
196 $this->note_private = trim($this->note_private);
198 if (isset($this->note_public)) {
199 $this->note_public = trim($this->note_public);
201 if (isset($this->fk_bank)) {
202 $this->fk_bank = (int) $this->fk_bank;
204 if (isset($this->fk_user_creat)) {
205 $this->fk_user_creat = (int) $this->fk_user_creat;
207 if (isset($this->fk_user_modif)) {
208 $this->fk_user_modif = (int) $this->fk_user_modif;
211 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
212 $totalamount = (float)
price2num($totalamount);
215 if ($totalamount == 0) {
222 if ($totalamount != 0) {
223 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
224 $sql .=
" fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
225 $sql .=
" VALUES (".$this->chid.
", '".$this->db->idate($now).
"',";
226 $sql .=
" '".$this->db->idate($this->datep).
"',";
227 $sql .=
" ".price2num($this->amount_capital).
",";
228 $sql .=
" ".price2num($this->amount_insurance).
",";
229 $sql .=
" ".price2num($this->amount_interest).
",";
230 $sql .=
" ".((int) $this->paymenttype).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', '".$this->db->escape($this->note_public).
"', ".$user->id.
",";
233 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
234 $resql = $this->db->query($sql);
236 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
238 $this->error = $this->db->lasterror();
243 if ($totalamount != 0 && !$error) {
244 $this->amount_capital = $totalamount;
248 $this->error = $this->db->lasterror();
249 $this->db->rollback();
264 $sql .=
" t.fk_loan,";
268 $sql .=
" t.amount_capital,";
269 $sql .=
" t.amount_insurance,";
270 $sql .=
" t.amount_interest,";
271 $sql .=
" t.fk_typepayment,";
272 $sql .=
" t.num_payment,";
273 $sql .=
" t.note_private,";
274 $sql .=
" t.note_public,";
275 $sql .=
" t.fk_bank,";
276 $sql .=
" t.fk_user_creat,";
277 $sql .=
" t.fk_user_modif,";
278 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
279 $sql .=
' b.fk_account';
280 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan as t";
281 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
282 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
283 $sql .=
" WHERE t.rowid = ".((int) $id);
285 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
286 $resql = $this->db->query($sql);
288 if ($this->db->num_rows($resql)) {
289 $obj = $this->db->fetch_object($resql);
291 $this->
id = $obj->rowid;
292 $this->
ref = $obj->rowid;
294 $this->fk_loan = $obj->fk_loan;
295 $this->datec = $this->db->jdate($obj->datec);
296 $this->tms = $this->db->jdate($obj->tms);
297 $this->datep = $this->db->jdate($obj->datep);
298 $this->amount_capital = $obj->amount_capital;
299 $this->amount_insurance = $obj->amount_insurance;
300 $this->amount_interest = $obj->amount_interest;
301 $this->fk_typepayment = $obj->fk_typepayment;
302 $this->num_payment = $obj->num_payment;
303 $this->note_private = $obj->note_private;
304 $this->note_public = $obj->note_public;
305 $this->fk_bank = $obj->fk_bank;
306 $this->fk_user_creat = $obj->fk_user_creat;
307 $this->fk_user_modif = $obj->fk_user_modif;
309 $this->type_code = $obj->type_code;
310 $this->type_label = $obj->type_label;
312 $this->bank_account = $obj->fk_account;
313 $this->bank_line = $obj->fk_bank;
315 $this->db->free($resql);
319 $this->error =
"Error ".$this->db->lasterror();
332 public function update($user =
null, $notrigger = 0)
337 if (isset($this->fk_loan)) {
338 $this->fk_loan = (int) $this->fk_loan;
340 if (isset($this->amount_capital)) {
341 $this->amount_capital = (float) $this->amount_capital;
343 if (isset($this->amount_insurance)) {
344 $this->amount_insurance = (float) $this->amount_insurance;
346 if (isset($this->amount_interest)) {
347 $this->amount_interest = (float) $this->amount_interest;
349 if (isset($this->fk_typepayment)) {
350 $this->fk_typepayment = (int) $this->fk_typepayment;
352 if (isset($this->num_payment)) {
353 $this->num_payment = trim($this->num_payment);
355 if (isset($this->note_private)) {
356 $this->note = trim($this->note_private);
358 if (isset($this->note_public)) {
359 $this->note = trim($this->note_public);
361 if (isset($this->fk_bank)) {
362 $this->fk_bank = (int) $this->fk_bank;
364 if (isset($this->fk_user_creat)) {
365 $this->fk_user_creat = (int) $this->fk_user_creat;
367 if (isset($this->fk_user_modif)) {
368 $this->fk_user_modif = (int) $this->fk_user_modif;
374 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_loan SET";
375 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
376 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
377 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
378 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
379 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
380 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
381 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
382 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
383 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
384 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
385 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
386 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
387 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
388 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
389 $sql .=
" WHERE rowid=".((int) $this->
id);
393 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
394 $resql = $this->db->query($sql);
397 $this->errors[] =
"Error ".$this->db->lasterror();
402 foreach ($this->errors as $errmsg) {
403 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
404 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
406 $this->db->rollback();
422 public function delete($user, $notrigger = 0)
428 if ($this->bank_line > 0) {
430 $accline->fetch($this->bank_line);
431 $result = $accline->delete($user);
433 $this->errors[] = $accline->error;
439 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan";
440 $sql .=
" WHERE rowid=".((int) $this->
id);
442 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
443 $resql = $this->db->query($sql);
446 $this->errors[] =
"Error ".$this->db->lasterror();
452 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
453 $loan =
new Loan($this->db);
454 $loan->fetch($this->fk_loan);
455 $sum_payment = $loan->getSumPayment();
456 if ($sum_payment == 0) {
457 dol_syslog(get_class($this).
"::delete : set loan to unpaid", LOG_DEBUG);
458 if ($loan->setUnpaid($user) < 1) {
467 foreach ($this->errors as $errmsg) {
468 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
469 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
471 $this->db->rollback();
517 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
523 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
526 $acc->fetch($accountid);
528 $total = (float) $this->amount_capital;
529 if ($mode ==
'payment_loan') {
534 $bank_line_id = $acc->addline(
548 if ($bank_line_id > 0) {
557 if ($mode ==
'payment_loan') {
558 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id=';
561 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(payment)', $mode);
570 if ($mode ==
'payment_loan') {
571 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.
'/loan/card.php?id=', ($this->label ? $this->label :
''),
'loan');
577 $this->error = $acc->error;
585 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
586 $loan =
new Loan($this->db);
587 $loan->fetch($fk_loan);
588 if ($loan->paid == $loan::STATUS_UNPAID) {
589 dol_syslog(get_class($this).
"::addPaymentToBank : set loan payment to started", LOG_DEBUG);
590 if ($loan->setStarted($user) < 1) {
601 $this->db->rollback();
640 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle =
'', $save_lastsearch_value = -1)
642 global $langs, $conf, $hookmanager;
644 if (!empty($conf->dol_no_mouse_hover)) {
649 $label =
'<u>'.$langs->trans(
"Loan").
'</u>';
650 if (!empty($this->
id)) {
651 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->id;
654 $label .=
' - '.$moretitle;
657 $baseurl = DOL_URL_ROOT.
'/loan/payment/card.php';
658 $query = [
'id' => $this->id];
660 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
661 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
662 $add_save_lastsearch_values = 1;
664 if ($add_save_lastsearch_values) {
665 $query += [
'save_lastsearch_values' => 1];
669 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
672 $result .= $linkstart;
674 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
676 if ($withpicto != 2) {
677 $result .= $this->ref;
682 $hookmanager->initHooks(array($this->element .
'dao'));
683 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
684 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
686 $result = $hookmanager->resPrint;
688 $result .= $hookmanager->resPrint;