160 global $conf, $langs;
168 $this->error =
'ErrorBadValueForParameter';
173 if (isset($this->fk_loan)) {
174 $this->fk_loan = (int) $this->fk_loan;
176 if (isset($this->amount_capital)) {
177 $this->amount_capital = (float)
price2num($this->amount_capital ? $this->amount_capital : 0);
179 if (isset($this->amount_insurance)) {
180 $this->amount_insurance = (float)
price2num($this->amount_insurance ? $this->amount_insurance : 0);
182 if (isset($this->amount_interest)) {
183 $this->amount_interest = (float)
price2num($this->amount_interest ? $this->amount_interest : 0);
185 if (isset($this->fk_typepayment)) {
186 $this->fk_typepayment = (int) $this->fk_typepayment;
188 if (isset($this->num_payment)) {
189 $this->num_payment = trim($this->num_payment);
191 if (isset($this->note_private)) {
192 $this->note_private = trim($this->note_private);
194 if (isset($this->note_public)) {
195 $this->note_public = trim($this->note_public);
197 if (isset($this->fk_bank)) {
198 $this->fk_bank = (int) $this->fk_bank;
200 if (isset($this->fk_user_creat)) {
201 $this->fk_user_creat = (int) $this->fk_user_creat;
203 if (isset($this->fk_user_modif)) {
204 $this->fk_user_modif = (int) $this->fk_user_modif;
207 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
208 $totalamount = (float)
price2num($totalamount);
211 if ($totalamount == 0) {
218 if ($totalamount != 0) {
219 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
220 $sql .=
" fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
221 $sql .=
" VALUES (".$this->chid.
", '".$this->db->idate($now).
"',";
222 $sql .=
" '".$this->db->idate($this->datep).
"',";
223 $sql .=
" ".price2num($this->amount_capital).
",";
224 $sql .=
" ".price2num($this->amount_insurance).
",";
225 $sql .=
" ".price2num($this->amount_interest).
",";
226 $sql .=
" ".((int) $this->paymenttype).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', '".$this->db->escape($this->note_public).
"', ".$user->id.
",";
229 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
230 $resql = $this->db->query($sql);
232 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
234 $this->error = $this->db->lasterror();
239 if ($totalamount != 0 && !$error) {
240 $this->amount_capital = $totalamount;
244 $this->error = $this->db->lasterror();
245 $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)
331 global $conf, $langs;
335 if (isset($this->fk_loan)) {
336 $this->fk_loan = (int) $this->fk_loan;
338 if (isset($this->amount_capital)) {
339 $this->amount_capital = (float) $this->amount_capital;
341 if (isset($this->amount_insurance)) {
342 $this->amount_insurance = (float) $this->amount_insurance;
344 if (isset($this->amount_interest)) {
345 $this->amount_interest = (float) $this->amount_interest;
347 if (isset($this->fk_typepayment)) {
348 $this->fk_typepayment = (int) $this->fk_typepayment;
350 if (isset($this->num_payment)) {
351 $this->num_payment = trim($this->num_payment);
353 if (isset($this->note_private)) {
354 $this->note = trim($this->note_private);
356 if (isset($this->note_public)) {
357 $this->note = trim($this->note_public);
359 if (isset($this->fk_bank)) {
360 $this->fk_bank = (int) $this->fk_bank;
362 if (isset($this->fk_user_creat)) {
363 $this->fk_user_creat = (int) $this->fk_user_creat;
365 if (isset($this->fk_user_modif)) {
366 $this->fk_user_modif = (int) $this->fk_user_modif;
372 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_loan SET";
373 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
374 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
375 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
376 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
377 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
378 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
379 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
380 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
381 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
382 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
383 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
384 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
385 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
386 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
387 $sql .=
" WHERE rowid=".((int) $this->
id);
391 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
392 $resql = $this->db->query($sql);
395 $this->errors[] =
"Error ".$this->db->lasterror();
400 foreach ($this->errors as $errmsg) {
401 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
402 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
404 $this->db->rollback();
420 public function delete($user, $notrigger = 0)
426 if ($this->bank_line > 0) {
428 $accline->fetch($this->bank_line);
429 $result = $accline->delete($user);
431 $this->errors[] = $accline->error;
437 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan";
438 $sql .=
" WHERE rowid=".((int) $this->
id);
440 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
441 $resql = $this->db->query($sql);
444 $this->errors[] =
"Error ".$this->db->lasterror();
450 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
451 $loan =
new Loan($this->db);
452 $loan->fetch($this->fk_loan);
453 $sum_payment = $loan->getSumPayment();
454 if ($sum_payment == 0) {
455 dol_syslog(get_class($this).
"::delete : set loan to unpaid", LOG_DEBUG);
456 if ($loan->setUnpaid($user) < 1) {
465 foreach ($this->errors as $errmsg) {
466 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
467 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
469 $this->db->rollback();
515 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
522 if (isModEnabled(
"bank")) {
523 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
526 $acc->fetch($accountid);
528 $total = $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 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id='.$this->id;
659 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
660 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
661 $add_save_lastsearch_values = 1;
663 if ($add_save_lastsearch_values) {
664 $url .=
'&save_lastsearch_values=1';
667 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
670 $result .= $linkstart;
672 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
674 if ($withpicto != 2) {
675 $result .= $this->ref;
680 $hookmanager->initHooks(array($this->element .
'dao'));
681 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
682 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
684 $result = $hookmanager->resPrint;
686 $result .= $hookmanager->resPrint;