169 $this->error =
'ErrorBadValueForParameter';
174 if (isset($this->fk_loan)) {
175 $this->fk_loan = (int) $this->fk_loan;
177 if (isset($this->amount_capital)) {
178 $this->amount_capital = (float)
price2num($this->amount_capital ? $this->amount_capital : 0);
180 if (isset($this->amount_insurance)) {
181 $this->amount_insurance = (float)
price2num($this->amount_insurance ? $this->amount_insurance : 0);
183 if (isset($this->amount_interest)) {
184 $this->amount_interest = (float)
price2num($this->amount_interest ? $this->amount_interest : 0);
186 if (isset($this->fk_typepayment)) {
187 $this->fk_typepayment = (int) $this->fk_typepayment;
189 if (isset($this->num_payment)) {
190 $this->num_payment = trim($this->num_payment);
192 if (isset($this->note_private)) {
193 $this->note_private = trim($this->note_private);
195 if (isset($this->note_public)) {
196 $this->note_public = trim($this->note_public);
198 if (isset($this->fk_bank)) {
199 $this->fk_bank = (int) $this->fk_bank;
201 if (isset($this->fk_user_creat)) {
202 $this->fk_user_creat = (int) $this->fk_user_creat;
204 if (isset($this->fk_user_modif)) {
205 $this->fk_user_modif = (int) $this->fk_user_modif;
208 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
209 $totalamount = (float)
price2num($totalamount);
212 if ($totalamount == 0) {
219 if ($totalamount != 0) {
220 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
221 $sql .=
" fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
222 $sql .=
" VALUES (".$this->chid.
", '".$this->db->idate($now).
"',";
223 $sql .=
" '".$this->db->idate($this->datep).
"',";
224 $sql .=
" ".price2num($this->amount_capital).
",";
225 $sql .=
" ".price2num($this->amount_insurance).
",";
226 $sql .=
" ".price2num($this->amount_interest).
",";
227 $sql .=
" ".((int) $this->paymenttype).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', '".$this->db->escape($this->note_public).
"', ".$user->id.
",";
230 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
231 $resql = $this->db->query($sql);
233 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
235 $this->error = $this->db->lasterror();
240 if ($totalamount != 0 && !$error) {
241 $this->amount_capital = $totalamount;
245 $this->error = $this->db->lasterror();
246 $this->db->rollback();
261 $sql .=
" t.fk_loan,";
265 $sql .=
" t.amount_capital,";
266 $sql .=
" t.amount_insurance,";
267 $sql .=
" t.amount_interest,";
268 $sql .=
" t.fk_typepayment,";
269 $sql .=
" t.num_payment,";
270 $sql .=
" t.note_private,";
271 $sql .=
" t.note_public,";
272 $sql .=
" t.fk_bank,";
273 $sql .=
" t.fk_user_creat,";
274 $sql .=
" t.fk_user_modif,";
275 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
276 $sql .=
' b.fk_account';
277 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan as t";
278 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
279 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
280 $sql .=
" WHERE t.rowid = ".((int) $id);
282 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
283 $resql = $this->db->query($sql);
285 if ($this->db->num_rows($resql)) {
286 $obj = $this->db->fetch_object($resql);
288 $this->
id = $obj->rowid;
289 $this->
ref = $obj->rowid;
291 $this->fk_loan = $obj->fk_loan;
292 $this->datec = $this->db->jdate($obj->datec);
293 $this->tms = $this->db->jdate($obj->tms);
294 $this->datep = $this->db->jdate($obj->datep);
295 $this->amount_capital = $obj->amount_capital;
296 $this->amount_insurance = $obj->amount_insurance;
297 $this->amount_interest = $obj->amount_interest;
298 $this->fk_typepayment = $obj->fk_typepayment;
299 $this->num_payment = $obj->num_payment;
300 $this->note_private = $obj->note_private;
301 $this->note_public = $obj->note_public;
302 $this->fk_bank = $obj->fk_bank;
303 $this->fk_user_creat = $obj->fk_user_creat;
304 $this->fk_user_modif = $obj->fk_user_modif;
306 $this->type_code = $obj->type_code;
307 $this->type_label = $obj->type_label;
309 $this->bank_account = $obj->fk_account;
310 $this->bank_line = $obj->fk_bank;
312 $this->db->free($resql);
316 $this->error =
"Error ".$this->db->lasterror();
329 public function update($user =
null, $notrigger = 0)
334 if (isset($this->fk_loan)) {
335 $this->fk_loan = (int) $this->fk_loan;
337 if (isset($this->amount_capital)) {
338 $this->amount_capital = (float) $this->amount_capital;
340 if (isset($this->amount_insurance)) {
341 $this->amount_insurance = (float) $this->amount_insurance;
343 if (isset($this->amount_interest)) {
344 $this->amount_interest = (float) $this->amount_interest;
346 if (isset($this->fk_typepayment)) {
347 $this->fk_typepayment = (int) $this->fk_typepayment;
349 if (isset($this->num_payment)) {
350 $this->num_payment = trim($this->num_payment);
352 if (isset($this->note_private)) {
353 $this->note = trim($this->note_private);
355 if (isset($this->note_public)) {
356 $this->note = trim($this->note_public);
358 if (isset($this->fk_bank)) {
359 $this->fk_bank = (int) $this->fk_bank;
361 if (isset($this->fk_user_creat)) {
362 $this->fk_user_creat = (int) $this->fk_user_creat;
364 if (isset($this->fk_user_modif)) {
365 $this->fk_user_modif = (int) $this->fk_user_modif;
371 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_loan SET";
372 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
373 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
374 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
375 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
376 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
377 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
378 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
379 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
380 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
381 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
382 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
383 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
384 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
385 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
386 $sql .=
" WHERE rowid=".((int) $this->
id);
390 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
391 $resql = $this->db->query($sql);
394 $this->errors[] =
"Error ".$this->db->lasterror();
399 foreach ($this->errors as $errmsg) {
400 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
401 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
403 $this->db->rollback();
419 public function delete($user, $notrigger = 0)
425 if ($this->bank_line > 0) {
427 $accline->fetch($this->bank_line);
428 $result = $accline->delete($user);
430 $this->errors[] = $accline->error;
436 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan";
437 $sql .=
" WHERE rowid=".((int) $this->
id);
439 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
440 $resql = $this->db->query($sql);
443 $this->errors[] =
"Error ".$this->db->lasterror();
449 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
450 $loan =
new Loan($this->db);
451 $loan->fetch($this->fk_loan);
452 $sum_payment = $loan->getSumPayment();
453 if ($sum_payment == 0) {
454 dol_syslog(get_class($this).
"::delete : set loan to unpaid", LOG_DEBUG);
455 if ($loan->setUnpaid($user) < 1) {
464 foreach ($this->errors as $errmsg) {
465 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
466 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
468 $this->db->rollback();
514 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
519 if (isModEnabled(
"bank")) {
520 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
523 $acc->fetch($accountid);
525 $total = $this->amount_capital;
526 if ($mode ==
'payment_loan') {
531 $bank_line_id = $acc->addline(
545 if ($bank_line_id > 0) {
554 if ($mode ==
'payment_loan') {
555 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id=';
558 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(payment)', $mode);
567 if ($mode ==
'payment_loan') {
568 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.
'/loan/card.php?id=', ($this->label ? $this->label :
''),
'loan');
574 $this->error = $acc->error;
582 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
583 $loan =
new Loan($this->db);
584 $loan->fetch($fk_loan);
585 if ($loan->paid == $loan::STATUS_UNPAID) {
586 dol_syslog(get_class($this).
"::addPaymentToBank : set loan payment to started", LOG_DEBUG);
587 if ($loan->setStarted($user) < 1) {
598 $this->db->rollback();
637 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle =
'', $save_lastsearch_value = -1)
639 global $langs,
$conf, $hookmanager;
641 if (!empty(
$conf->dol_no_mouse_hover)) {
646 $label =
'<u>'.$langs->trans(
"Loan").
'</u>';
647 if (!empty($this->
id)) {
648 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->id;
651 $label .=
' - '.$moretitle;
654 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id='.$this->id;
656 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
657 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
658 $add_save_lastsearch_values = 1;
660 if ($add_save_lastsearch_values) {
661 $url .=
'&save_lastsearch_values=1';
664 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
667 $result .= $linkstart;
669 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
671 if ($withpicto != 2) {
672 $result .= $this->ref;
677 $hookmanager->initHooks(array($this->element .
'dao'));
678 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
679 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
681 $result = $hookmanager->resPrint;
683 $result .= $hookmanager->resPrint;