156 global $conf, $langs;
164 $this->error =
'ErrorBadValueForParameter';
169 if (isset($this->fk_loan)) {
170 $this->fk_loan = (int) $this->fk_loan;
172 if (isset($this->amount_capital)) {
173 $this->amount_capital = (float)
price2num($this->amount_capital ? $this->amount_capital : 0);
175 if (isset($this->amount_insurance)) {
176 $this->amount_insurance = (float)
price2num($this->amount_insurance ? $this->amount_insurance : 0);
178 if (isset($this->amount_interest)) {
179 $this->amount_interest = (float)
price2num($this->amount_interest ? $this->amount_interest : 0);
181 if (isset($this->fk_typepayment)) {
182 $this->fk_typepayment = (int) $this->fk_typepayment;
184 if (isset($this->num_payment)) {
185 $this->num_payment = trim($this->num_payment);
187 if (isset($this->note_private)) {
188 $this->note_private = trim($this->note_private);
190 if (isset($this->note_public)) {
191 $this->note_public = trim($this->note_public);
193 if (isset($this->fk_bank)) {
194 $this->fk_bank = (int) $this->fk_bank;
196 if (isset($this->fk_user_creat)) {
197 $this->fk_user_creat = (int) $this->fk_user_creat;
199 if (isset($this->fk_user_modif)) {
200 $this->fk_user_modif = (int) $this->fk_user_modif;
203 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
204 $totalamount = (float)
price2num($totalamount);
207 if ($totalamount == 0) {
214 if ($totalamount != 0) {
215 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
216 $sql .=
" fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
217 $sql .=
" VALUES (".$this->chid.
", '".$this->db->idate($now).
"',";
218 $sql .=
" '".$this->db->idate($this->datep).
"',";
219 $sql .=
" ".price2num($this->amount_capital).
",";
220 $sql .=
" ".price2num($this->amount_insurance).
",";
221 $sql .=
" ".price2num($this->amount_interest).
",";
222 $sql .=
" ".((int) $this->paymenttype).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', '".$this->db->escape($this->note_public).
"', ".$user->id.
",";
225 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
226 $resql = $this->db->query($sql);
228 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
230 $this->error = $this->db->lasterror();
235 if ($totalamount != 0 && !$error) {
236 $this->amount_capital = $totalamount;
240 $this->error = $this->db->lasterror();
241 $this->db->rollback();
257 $sql .=
" t.fk_loan,";
261 $sql .=
" t.amount_capital,";
262 $sql .=
" t.amount_insurance,";
263 $sql .=
" t.amount_interest,";
264 $sql .=
" t.fk_typepayment,";
265 $sql .=
" t.num_payment,";
266 $sql .=
" t.note_private,";
267 $sql .=
" t.note_public,";
268 $sql .=
" t.fk_bank,";
269 $sql .=
" t.fk_user_creat,";
270 $sql .=
" t.fk_user_modif,";
271 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
272 $sql .=
' b.fk_account';
273 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan as t";
274 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
275 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
276 $sql .=
" WHERE t.rowid = ".((int) $id);
278 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
279 $resql = $this->db->query($sql);
281 if ($this->db->num_rows($resql)) {
282 $obj = $this->db->fetch_object($resql);
284 $this->
id = $obj->rowid;
285 $this->
ref = $obj->rowid;
287 $this->fk_loan = $obj->fk_loan;
288 $this->datec = $this->db->jdate($obj->datec);
289 $this->tms = $this->db->jdate($obj->tms);
290 $this->datep = $this->db->jdate($obj->datep);
291 $this->amount_capital = $obj->amount_capital;
292 $this->amount_insurance = $obj->amount_insurance;
293 $this->amount_interest = $obj->amount_interest;
294 $this->fk_typepayment = $obj->fk_typepayment;
295 $this->num_payment = $obj->num_payment;
296 $this->note_private = $obj->note_private;
297 $this->note_public = $obj->note_public;
298 $this->fk_bank = $obj->fk_bank;
299 $this->fk_user_creat = $obj->fk_user_creat;
300 $this->fk_user_modif = $obj->fk_user_modif;
302 $this->type_code = $obj->type_code;
303 $this->type_label = $obj->type_label;
305 $this->bank_account = $obj->fk_account;
306 $this->bank_line = $obj->fk_bank;
308 $this->db->free($resql);
312 $this->error =
"Error ".$this->db->lasterror();
325 public function update($user =
null, $notrigger = 0)
327 global $conf, $langs;
331 if (isset($this->fk_loan)) {
332 $this->fk_loan = (int) $this->fk_loan;
334 if (isset($this->amount_capital)) {
335 $this->amount_capital = (float) $this->amount_capital;
337 if (isset($this->amount_insurance)) {
338 $this->amount_insurance = (float) $this->amount_insurance;
340 if (isset($this->amount_interest)) {
341 $this->amount_interest = (float) $this->amount_interest;
343 if (isset($this->fk_typepayment)) {
344 $this->fk_typepayment = (int) $this->fk_typepayment;
346 if (isset($this->num_payment)) {
347 $this->num_payment = trim($this->num_payment);
349 if (isset($this->note_private)) {
350 $this->note = trim($this->note_private);
352 if (isset($this->note_public)) {
353 $this->note = trim($this->note_public);
355 if (isset($this->fk_bank)) {
356 $this->fk_bank = (int) $this->fk_bank;
358 if (isset($this->fk_user_creat)) {
359 $this->fk_user_creat = (int) $this->fk_user_creat;
361 if (isset($this->fk_user_modif)) {
362 $this->fk_user_modif = (int) $this->fk_user_modif;
368 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_loan SET";
369 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
370 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
371 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
372 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
373 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
374 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
375 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
376 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
377 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
378 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
379 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
380 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
381 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
382 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
383 $sql .=
" WHERE rowid=".((int) $this->
id);
387 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
388 $resql = $this->db->query($sql);
391 $this->errors[] =
"Error ".$this->db->lasterror();
396 foreach ($this->errors as $errmsg) {
397 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
398 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
400 $this->db->rollback();
416 public function delete($user, $notrigger = 0)
418 global $conf, $langs;
424 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"bank_url";
425 $sql .=
" WHERE type='payment_loan' AND url_id=".((int) $this->
id);
427 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
428 $resql = $this->db->query($sql);
431 $this->errors[] =
"Error ".$this->db->lasterror();
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) {
480 foreach ($this->errors as $errmsg) {
481 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
482 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
484 $this->db->rollback();
530 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
537 if (isModEnabled(
"bank")) {
538 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
541 $acc->fetch($accountid);
543 $total = $this->amount_capital;
544 if ($mode ==
'payment_loan') {
549 $bank_line_id = $acc->addline(
563 if ($bank_line_id > 0) {
572 if ($mode ==
'payment_loan') {
573 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id=';
576 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(payment)', $mode);
585 if ($mode ==
'payment_loan') {
586 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.
'/loan/card.php?id=', ($this->label ? $this->label :
''),
'loan');
592 $this->error = $acc->error;
600 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
601 $loan =
new Loan($this->db);
602 $loan->fetch($fk_loan);
603 if ($loan->paid == $loan::STATUS_UNPAID) {
604 dol_syslog(get_class($this).
"::addPaymentToBank : set loan payment to started", LOG_DEBUG);
605 if ($loan->setStarted($user) < 1) {
616 $this->db->rollback();
655 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle =
'', $save_lastsearch_value = -1)
657 global $langs, $conf, $hookmanager;
659 if (!empty($conf->dol_no_mouse_hover)) {
664 $label =
'<u>'.$langs->trans(
"Loan").
'</u>';
665 if (!empty($this->
id)) {
666 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->id;
669 $label .=
' - '.$moretitle;
672 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id='.$this->id;
674 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
675 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
676 $add_save_lastsearch_values = 1;
678 if ($add_save_lastsearch_values) {
679 $url .=
'&save_lastsearch_values=1';
682 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
685 $result .= $linkstart;
687 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
689 if ($withpicto != 2) {
690 $result .= $this->ref;
695 $hookmanager->initHooks(array($this->element .
'dao'));
696 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
697 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
699 $result = $hookmanager->resPrint;
701 $result .= $hookmanager->resPrint;