131 global $conf, $langs;
139 $this->error =
'ErrorBadValueForParameter';
144 if (isset($this->fk_loan)) {
145 $this->fk_loan = (int) $this->fk_loan;
147 if (isset($this->amount_capital)) {
148 $this->amount_capital =
price2num($this->amount_capital ? $this->amount_capital : 0);
150 if (isset($this->amount_insurance)) {
151 $this->amount_insurance =
price2num($this->amount_insurance ? $this->amount_insurance : 0);
153 if (isset($this->amount_interest)) {
154 $this->amount_interest =
price2num($this->amount_interest ? $this->amount_interest : 0);
156 if (isset($this->fk_typepayment)) {
157 $this->fk_typepayment = (int) $this->fk_typepayment;
159 if (isset($this->num_payment)) {
160 $this->num_payment = (int) $this->num_payment;
162 if (isset($this->note_private)) {
163 $this->note_private = trim($this->note_private);
165 if (isset($this->note_public)) {
166 $this->note_public = trim($this->note_public);
168 if (isset($this->fk_bank)) {
169 $this->fk_bank = (int) $this->fk_bank;
171 if (isset($this->fk_user_creat)) {
172 $this->fk_user_creat = (int) $this->fk_user_creat;
174 if (isset($this->fk_user_modif)) {
175 $this->fk_user_modif = (int) $this->fk_user_modif;
178 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
182 if ($totalamount == 0) {
189 if ($totalamount != 0) {
190 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
191 $sql .=
" fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
192 $sql .=
" VALUES (".$this->chid.
", '".$this->db->idate($now).
"',";
193 $sql .=
" '".$this->db->idate($this->datep).
"',";
194 $sql .=
" ".price2num($this->amount_capital).
",";
195 $sql .=
" ".price2num($this->amount_insurance).
",";
196 $sql .=
" ".price2num($this->amount_interest).
",";
197 $sql .=
" ".((int) $this->paymenttype).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', '".$this->db->escape($this->note_public).
"', ".$user->id.
",";
200 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
201 $resql = $this->db->query($sql);
203 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
205 $this->error = $this->db->lasterror();
210 if ($totalamount != 0 && !$error) {
211 $this->amount_capital = $totalamount;
215 $this->error = $this->db->lasterror();
216 $this->db->rollback();
232 $sql .=
" t.fk_loan,";
236 $sql .=
" t.amount_capital,";
237 $sql .=
" t.amount_insurance,";
238 $sql .=
" t.amount_interest,";
239 $sql .=
" t.fk_typepayment,";
240 $sql .=
" t.num_payment,";
241 $sql .=
" t.note_private,";
242 $sql .=
" t.note_public,";
243 $sql .=
" t.fk_bank,";
244 $sql .=
" t.fk_user_creat,";
245 $sql .=
" t.fk_user_modif,";
246 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
247 $sql .=
' b.fk_account';
248 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan as t";
249 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
250 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
251 $sql .=
" WHERE t.rowid = ".((int) $id);
253 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
254 $resql = $this->db->query($sql);
256 if ($this->db->num_rows($resql)) {
257 $obj = $this->db->fetch_object($resql);
259 $this->
id = $obj->rowid;
260 $this->
ref = $obj->rowid;
262 $this->fk_loan = $obj->fk_loan;
263 $this->datec = $this->db->jdate($obj->datec);
264 $this->tms = $this->db->jdate($obj->tms);
265 $this->datep = $this->db->jdate($obj->datep);
266 $this->amount_capital = $obj->amount_capital;
267 $this->amount_insurance = $obj->amount_insurance;
268 $this->amount_interest = $obj->amount_interest;
269 $this->fk_typepayment = $obj->fk_typepayment;
270 $this->num_payment = $obj->num_payment;
271 $this->note_private = $obj->note_private;
272 $this->note_public = $obj->note_public;
273 $this->fk_bank = $obj->fk_bank;
274 $this->fk_user_creat = $obj->fk_user_creat;
275 $this->fk_user_modif = $obj->fk_user_modif;
277 $this->type_code = $obj->type_code;
278 $this->type_label = $obj->type_label;
280 $this->bank_account = $obj->fk_account;
281 $this->bank_line = $obj->fk_bank;
283 $this->db->free($resql);
287 $this->error =
"Error ".$this->db->lasterror();
300 public function update($user =
null, $notrigger = 0)
302 global $conf, $langs;
306 if (isset($this->fk_loan)) {
307 $this->fk_loan = (int) $this->fk_loan;
309 if (isset($this->amount_capital)) {
310 $this->amount_capital = trim($this->amount_capital);
312 if (isset($this->amount_insurance)) {
313 $this->amount_insurance = trim($this->amount_insurance);
315 if (isset($this->amount_interest)) {
316 $this->amount_interest = trim($this->amount_interest);
318 if (isset($this->fk_typepayment)) {
319 $this->fk_typepayment = (int) $this->fk_typepayment;
321 if (isset($this->num_payment)) {
322 $this->num_payment = (int) $this->num_payment;
324 if (isset($this->note_private)) {
325 $this->note = trim($this->note_private);
327 if (isset($this->note_public)) {
328 $this->note = trim($this->note_public);
330 if (isset($this->fk_bank)) {
331 $this->fk_bank = (int) $this->fk_bank;
333 if (isset($this->fk_user_creat)) {
334 $this->fk_user_creat = (int) $this->fk_user_creat;
336 if (isset($this->fk_user_modif)) {
337 $this->fk_user_modif = (int) $this->fk_user_modif;
343 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_loan SET";
344 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
345 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
346 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
347 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
348 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
349 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
350 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
351 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
352 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
353 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
354 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
355 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
356 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
357 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) :
"null");
358 $sql .=
" WHERE rowid=".((int) $this->
id);
362 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
363 $resql = $this->db->query($sql);
366 $this->errors[] =
"Error ".$this->db->lasterror();
371 foreach ($this->errors as $errmsg) {
372 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
373 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
375 $this->db->rollback();
391 public function delete($user, $notrigger = 0)
397 if ($this->bank_line > 0) {
399 $accline->fetch($this->bank_line);
400 $result = $accline->delete($user);
402 $this->errors[] = $accline->error;
408 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan";
409 $sql .=
" WHERE rowid=".((int) $this->
id);
411 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
412 $resql = $this->db->query($sql);
415 $this->errors[] =
"Error ".$this->db->lasterror();
421 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
422 $loan =
new Loan($this->db);
423 $loan->fetch($this->fk_loan);
424 $sum_payment = $loan->getSumPayment();
425 if ($sum_payment == 0) {
426 dol_syslog(get_class($this).
"::delete : set loan to unpaid", LOG_DEBUG);
427 if ($loan->setUnpaid($user) < 1) {
436 foreach ($this->errors as $errmsg) {
437 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
438 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
440 $this->db->rollback();
486 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
493 if (isModEnabled(
"banque")) {
494 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
497 $acc->fetch($accountid);
499 $total = $this->amount_capital;
500 if ($mode ==
'payment_loan') {
505 $bank_line_id = $acc->addline(
519 if ($bank_line_id > 0) {
528 if ($mode ==
'payment_loan') {
529 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id=';
532 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(payment)', $mode);
541 if ($mode ==
'payment_loan') {
542 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.
'/loan/card.php?id=', ($this->label ? $this->label :
''),
'loan');
548 $this->error = $acc->error;
556 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
557 $loan =
new Loan($this->db);
558 $loan->fetch($fk_loan);
559 if ($loan->paid == $loan::STATUS_UNPAID) {
560 dol_syslog(get_class($this).
"::addPaymentToBank : set loan payment to started", LOG_DEBUG);
561 if ($loan->setStarted($user) < 1) {
572 $this->db->rollback();
611 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle =
'', $save_lastsearch_value = -1)
613 global $langs, $conf, $hookmanager;
615 if (!empty($conf->dol_no_mouse_hover)) {
620 $label =
'<u>'.$langs->trans(
"Loan").
'</u>';
621 if (!empty($this->
id)) {
622 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->id;
625 $label .=
' - '.$moretitle;
628 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id='.$this->id;
630 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
631 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
632 $add_save_lastsearch_values = 1;
634 if ($add_save_lastsearch_values) {
635 $url .=
'&save_lastsearch_values=1';
638 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
641 $result .= $linkstart;
643 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
645 if ($withpicto != 2) {
646 $result .= $this->ref;
651 $hookmanager->initHooks(array($this->element .
'dao'));
652 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
653 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
655 $result = $hookmanager->resPrint;
657 $result .= $hookmanager->resPrint;