162 global
$conf, $langs;
170 $this->error =
'ErrorBadValueForParameter';
175 if (isset($this->fk_loan)) {
176 $this->fk_loan = (int) $this->fk_loan;
178 if (isset($this->amount_capital)) {
179 $this->amount_capital = (float)
price2num($this->amount_capital ? $this->amount_capital : 0);
181 if (isset($this->amount_insurance)) {
182 $this->amount_insurance = (float)
price2num($this->amount_insurance ? $this->amount_insurance : 0);
184 if (isset($this->amount_interest)) {
185 $this->amount_interest = (float)
price2num($this->amount_interest ? $this->amount_interest : 0);
187 if (isset($this->fk_typepayment)) {
188 $this->fk_typepayment = (int) $this->fk_typepayment;
190 if (isset($this->num_payment)) {
191 $this->num_payment = trim($this->num_payment);
193 if (isset($this->note_private)) {
194 $this->note_private = trim($this->note_private);
196 if (isset($this->note_public)) {
197 $this->note_public = trim($this->note_public);
199 if (isset($this->fk_bank)) {
200 $this->fk_bank = (int) $this->fk_bank;
202 if (isset($this->fk_user_creat)) {
203 $this->fk_user_creat = (int) $this->fk_user_creat;
205 if (isset($this->fk_user_modif)) {
206 $this->fk_user_modif = (int) $this->fk_user_modif;
209 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
210 $totalamount = (float)
price2num($totalamount);
213 if ($totalamount == 0) {
220 if ($totalamount != 0) {
221 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
222 $sql .=
" fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
223 $sql .=
" VALUES (".$this->chid.
", '".$this->db->idate($now).
"',";
224 $sql .=
" '".$this->db->idate($this->datep).
"',";
225 $sql .=
" ".price2num($this->amount_capital).
",";
226 $sql .=
" ".price2num($this->amount_insurance).
",";
227 $sql .=
" ".price2num($this->amount_interest).
",";
228 $sql .=
" ".((int) $this->paymenttype).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', '".$this->db->escape($this->note_public).
"', ".$user->id.
",";
231 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
232 $resql = $this->db->query($sql);
234 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
236 $this->error = $this->db->lasterror();
241 if ($totalamount != 0 && !$error) {
242 $this->amount_capital = $totalamount;
246 $this->error = $this->db->lasterror();
247 $this->db->rollback();
263 $sql .=
" t.fk_loan,";
267 $sql .=
" t.amount_capital,";
268 $sql .=
" t.amount_insurance,";
269 $sql .=
" t.amount_interest,";
270 $sql .=
" t.fk_typepayment,";
271 $sql .=
" t.num_payment,";
272 $sql .=
" t.note_private,";
273 $sql .=
" t.note_public,";
274 $sql .=
" t.fk_bank,";
275 $sql .=
" t.fk_user_creat,";
276 $sql .=
" t.fk_user_modif,";
277 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
278 $sql .=
' b.fk_account';
279 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan as t";
280 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
281 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
282 $sql .=
" WHERE t.rowid = ".((int) $id);
284 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
285 $resql = $this->db->query($sql);
287 if ($this->db->num_rows($resql)) {
288 $obj = $this->db->fetch_object($resql);
290 $this->
id = $obj->rowid;
291 $this->
ref = $obj->rowid;
293 $this->fk_loan = $obj->fk_loan;
294 $this->datec = $this->db->jdate($obj->datec);
295 $this->tms = $this->db->jdate($obj->tms);
296 $this->datep = $this->db->jdate($obj->datep);
297 $this->amount_capital = $obj->amount_capital;
298 $this->amount_insurance = $obj->amount_insurance;
299 $this->amount_interest = $obj->amount_interest;
300 $this->fk_typepayment = $obj->fk_typepayment;
301 $this->num_payment = $obj->num_payment;
302 $this->note_private = $obj->note_private;
303 $this->note_public = $obj->note_public;
304 $this->fk_bank = $obj->fk_bank;
305 $this->fk_user_creat = $obj->fk_user_creat;
306 $this->fk_user_modif = $obj->fk_user_modif;
308 $this->type_code = $obj->type_code;
309 $this->type_label = $obj->type_label;
311 $this->bank_account = $obj->fk_account;
312 $this->bank_line = $obj->fk_bank;
314 $this->db->free($resql);
318 $this->error =
"Error ".$this->db->lasterror();
331 public function update($user =
null, $notrigger = 0)
333 global
$conf, $langs;
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)
424 global
$conf, $langs;
430 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"bank_url";
431 $sql .=
" WHERE type='payment_loan' AND url_id=".((int) $this->
id);
433 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
434 $resql = $this->db->query($sql);
437 $this->errors[] =
"Error ".$this->db->lasterror();
442 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan";
443 $sql .=
" WHERE rowid=".((int) $this->
id);
445 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
446 $resql = $this->db->query($sql);
449 $this->errors[] =
"Error ".$this->db->lasterror();
455 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
456 $loan =
new Loan($this->db);
457 $loan->fetch($this->fk_loan);
458 $sum_payment = $loan->getSumPayment();
459 if ($sum_payment == 0) {
460 dol_syslog(get_class($this).
"::delete : set loan to unpaid", LOG_DEBUG);
461 if ($loan->setUnpaid($user) < 1) {
486 foreach ($this->errors as $errmsg) {
487 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
488 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
490 $this->db->rollback();
536 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
543 if (isModEnabled(
"bank")) {
544 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
547 $acc->fetch($accountid);
549 $total = $this->amount_capital;
550 if ($mode ==
'payment_loan') {
555 $bank_line_id = $acc->addline(
569 if ($bank_line_id > 0) {
578 if ($mode ==
'payment_loan') {
579 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id=';
582 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(payment)', $mode);
591 if ($mode ==
'payment_loan') {
592 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.
'/loan/card.php?id=', ($this->label ? $this->label :
''),
'loan');
598 $this->error = $acc->error;
606 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
607 $loan =
new Loan($this->db);
608 $loan->fetch($fk_loan);
609 if ($loan->paid == $loan::STATUS_UNPAID) {
610 dol_syslog(get_class($this).
"::addPaymentToBank : set loan payment to started", LOG_DEBUG);
611 if ($loan->setStarted($user) < 1) {
622 $this->db->rollback();
661 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle =
'', $save_lastsearch_value = -1)
663 global $langs,
$conf, $hookmanager;
665 if (!empty(
$conf->dol_no_mouse_hover)) {
670 $label =
'<u>'.$langs->trans(
"Loan").
'</u>';
671 if (!empty($this->
id)) {
672 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->id;
675 $label .=
' - '.$moretitle;
678 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id='.$this->id;
680 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
681 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
682 $add_save_lastsearch_values = 1;
684 if ($add_save_lastsearch_values) {
685 $url .=
'&save_lastsearch_values=1';
688 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
691 $result .= $linkstart;
693 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
695 if ($withpicto != 2) {
696 $result .= $this->ref;
701 $hookmanager->initHooks(array($this->element .
'dao'));
702 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
703 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
705 $result = $hookmanager->resPrint;
707 $result .= $hookmanager->resPrint;