122 global $conf, $langs;
130 $this->error =
'ErrorBadValueForParameter';
135 if (isset($this->fk_loan)) {
136 $this->fk_loan = (int) $this->fk_loan;
138 if (isset($this->amount_capital)) {
139 $this->amount_capital =
price2num($this->amount_capital ? $this->amount_capital : 0);
141 if (isset($this->amount_insurance)) {
142 $this->amount_insurance =
price2num($this->amount_insurance ? $this->amount_insurance : 0);
144 if (isset($this->amount_interest)) {
145 $this->amount_interest =
price2num($this->amount_interest ? $this->amount_interest : 0);
147 if (isset($this->fk_typepayment)) {
148 $this->fk_typepayment = (int) $this->fk_typepayment;
150 if (isset($this->num_payment)) {
151 $this->num_payment = (int) $this->num_payment;
153 if (isset($this->note_private)) {
154 $this->note_private = trim($this->note_private);
156 if (isset($this->note_public)) {
157 $this->note_public = trim($this->note_public);
159 if (isset($this->fk_bank)) {
160 $this->fk_bank = (int) $this->fk_bank;
162 if (isset($this->fk_user_creat)) {
163 $this->fk_user_creat = (int) $this->fk_user_creat;
165 if (isset($this->fk_user_modif)) {
166 $this->fk_user_modif = (int) $this->fk_user_modif;
169 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
173 if ($totalamount == 0) {
180 if ($totalamount != 0) {
181 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
182 $sql .=
" fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
183 $sql .=
" VALUES (".$this->chid.
", '".$this->db->idate($now).
"',";
184 $sql .=
" '".$this->db->idate($this->datep).
"',";
185 $sql .=
" ".price2num($this->amount_capital).
",";
186 $sql .=
" ".price2num($this->amount_insurance).
",";
187 $sql .=
" ".price2num($this->amount_interest).
",";
188 $sql .=
" ".((int) $this->paymenttype).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', '".$this->db->escape($this->note_public).
"', ".$user->id.
",";
191 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
192 $resql = $this->db->query($sql);
194 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
196 $this->error = $this->db->lasterror();
201 if ($totalamount != 0 && !$error) {
202 $this->amount_capital = $totalamount;
206 $this->error = $this->db->lasterror();
207 $this->db->rollback();
223 $sql .=
" t.fk_loan,";
227 $sql .=
" t.amount_capital,";
228 $sql .=
" t.amount_insurance,";
229 $sql .=
" t.amount_interest,";
230 $sql .=
" t.fk_typepayment,";
231 $sql .=
" t.num_payment,";
232 $sql .=
" t.note_private,";
233 $sql .=
" t.note_public,";
234 $sql .=
" t.fk_bank,";
235 $sql .=
" t.fk_user_creat,";
236 $sql .=
" t.fk_user_modif,";
237 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
238 $sql .=
' b.fk_account';
239 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan as t";
240 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
241 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
242 $sql .=
" WHERE t.rowid = ".((int) $id);
244 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
245 $resql = $this->db->query($sql);
247 if ($this->db->num_rows($resql)) {
248 $obj = $this->db->fetch_object($resql);
250 $this->
id = $obj->rowid;
251 $this->
ref = $obj->rowid;
253 $this->fk_loan = $obj->fk_loan;
254 $this->datec = $this->db->jdate($obj->datec);
255 $this->tms = $this->db->jdate($obj->tms);
256 $this->datep = $this->db->jdate($obj->datep);
257 $this->amount_capital = $obj->amount_capital;
258 $this->amount_insurance = $obj->amount_insurance;
259 $this->amount_interest = $obj->amount_interest;
260 $this->fk_typepayment = $obj->fk_typepayment;
261 $this->num_payment = $obj->num_payment;
262 $this->note_private = $obj->note_private;
263 $this->note_public = $obj->note_public;
264 $this->fk_bank = $obj->fk_bank;
265 $this->fk_user_creat = $obj->fk_user_creat;
266 $this->fk_user_modif = $obj->fk_user_modif;
268 $this->type_code = $obj->type_code;
269 $this->type_label = $obj->type_label;
271 $this->bank_account = $obj->fk_account;
272 $this->bank_line = $obj->fk_bank;
274 $this->db->free($resql);
278 $this->error =
"Error ".$this->db->lasterror();
291 public function update($user = 0, $notrigger = 0)
293 global $conf, $langs;
297 if (isset($this->fk_loan)) {
298 $this->fk_loan = (int) $this->fk_loan;
300 if (isset($this->amount_capital)) {
301 $this->amount_capital = trim($this->amount_capital);
303 if (isset($this->amount_insurance)) {
304 $this->amount_insurance = trim($this->amount_insurance);
306 if (isset($this->amount_interest)) {
307 $this->amount_interest = trim($this->amount_interest);
309 if (isset($this->fk_typepayment)) {
310 $this->fk_typepayment = (int) $this->fk_typepayment;
312 if (isset($this->num_payment)) {
313 $this->num_payment = (int) $this->num_payment;
315 if (isset($this->note_private)) {
316 $this->note = trim($this->note_private);
318 if (isset($this->note_public)) {
319 $this->note = trim($this->note_public);
321 if (isset($this->fk_bank)) {
322 $this->fk_bank = (int) $this->fk_bank;
324 if (isset($this->fk_user_creat)) {
325 $this->fk_user_creat = (int) $this->fk_user_creat;
327 if (isset($this->fk_user_modif)) {
328 $this->fk_user_modif = (int) $this->fk_user_modif;
334 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_loan SET";
335 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
336 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
337 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
338 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
339 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
340 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
341 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
342 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
343 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
344 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
345 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
346 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
347 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
348 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ?((int) $this->fk_user_modif) :
"null");
349 $sql .=
" WHERE rowid=".((int) $this->
id);
353 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
354 $resql = $this->db->query($sql);
356 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
361 foreach ($this->errors as $errmsg) {
362 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
363 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
365 $this->db->rollback();
381 public function delete($user, $notrigger = 0)
383 global $conf, $langs;
389 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"bank_url";
390 $sql .=
" WHERE type='payment_loan' AND url_id=".((int) $this->
id);
392 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
393 $resql = $this->db->query($sql);
395 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
400 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan";
401 $sql .=
" WHERE rowid=".((int) $this->
id);
403 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
404 $resql = $this->db->query($sql);
406 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
412 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
413 $loan =
new Loan($this->db);
414 $loan->fetch($this->fk_loan);
415 $sum_payment = $loan->getSumPayment();
416 if ($sum_payment == 0) {
417 dol_syslog(get_class($this).
"::delete : set loan to unpaid", LOG_DEBUG);
418 if ($loan->setUnpaid($user) < 1) {
443 foreach ($this->errors as $errmsg) {
444 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
445 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
447 $this->db->rollback();
493 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
500 if (isModEnabled(
"banque")) {
501 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
504 $acc->fetch($accountid);
506 $total = $this->amount_capital;
507 if ($mode ==
'payment_loan') {
512 $bank_line_id = $acc->addline(
526 if ($bank_line_id > 0) {
535 if ($mode ==
'payment_loan') {
536 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id=';
539 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(payment)', $mode);
548 if ($mode ==
'payment_loan') {
549 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.
'/loan/card.php?id=', ($this->label ? $this->label :
''),
'loan');
555 $this->error = $acc->error;
563 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
564 $loan =
new Loan($this->db);
565 $loan->fetch($fk_loan);
566 if ($loan->paid == $loan::STATUS_UNPAID) {
567 dol_syslog(get_class($this).
"::addPaymentToBank : set loan payment to started", LOG_DEBUG);
568 if ($loan->setStarted($user) < 1) {
579 $this->db->rollback();
618 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle =
'', $save_lastsearch_value = -1)
620 global $langs, $conf, $hookmanager;
622 if (!empty($conf->dol_no_mouse_hover)) {
627 $label =
'<u>'.$langs->trans(
"Loan").
'</u>';
628 if (!empty($this->
id)) {
629 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->id;
632 $label .=
' - '.$moretitle;
635 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id='.$this->id;
637 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
638 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
639 $add_save_lastsearch_values = 1;
641 if ($add_save_lastsearch_values) {
642 $url .=
'&save_lastsearch_values=1';
645 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
648 $result .= $linkstart;
650 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
652 if ($withpicto != 2) {
653 $result .= $this->ref;
658 $hookmanager->initHooks(array($this->element .
'dao'));
659 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
660 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
662 $result = $hookmanager->resPrint;
664 $result .= $hookmanager->resPrint;