127 global $conf, $langs;
135 $this->error =
'ErrorBadValueForParameter';
140 if (isset($this->fk_loan)) {
141 $this->fk_loan = (int) $this->fk_loan;
143 if (isset($this->amount_capital)) {
144 $this->amount_capital =
price2num($this->amount_capital ? $this->amount_capital : 0);
146 if (isset($this->amount_insurance)) {
147 $this->amount_insurance =
price2num($this->amount_insurance ? $this->amount_insurance : 0);
149 if (isset($this->amount_interest)) {
150 $this->amount_interest =
price2num($this->amount_interest ? $this->amount_interest : 0);
152 if (isset($this->fk_typepayment)) {
153 $this->fk_typepayment = (int) $this->fk_typepayment;
155 if (isset($this->num_payment)) {
156 $this->num_payment = (int) $this->num_payment;
158 if (isset($this->note_private)) {
159 $this->note_private = trim($this->note_private);
161 if (isset($this->note_public)) {
162 $this->note_public = trim($this->note_public);
164 if (isset($this->fk_bank)) {
165 $this->fk_bank = (int) $this->fk_bank;
167 if (isset($this->fk_user_creat)) {
168 $this->fk_user_creat = (int) $this->fk_user_creat;
170 if (isset($this->fk_user_modif)) {
171 $this->fk_user_modif = (int) $this->fk_user_modif;
174 $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
178 if ($totalamount == 0) {
185 if ($totalamount != 0) {
186 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
187 $sql .=
" fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
188 $sql .=
" VALUES (".$this->chid.
", '".$this->db->idate($now).
"',";
189 $sql .=
" '".$this->db->idate($this->datep).
"',";
190 $sql .=
" ".price2num($this->amount_capital).
",";
191 $sql .=
" ".price2num($this->amount_insurance).
",";
192 $sql .=
" ".price2num($this->amount_interest).
",";
193 $sql .=
" ".((int) $this->paymenttype).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', '".$this->db->escape($this->note_public).
"', ".$user->id.
",";
196 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
197 $resql = $this->db->query($sql);
199 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"payment_loan");
201 $this->error = $this->db->lasterror();
206 if ($totalamount != 0 && !$error) {
207 $this->amount_capital = $totalamount;
211 $this->error = $this->db->lasterror();
212 $this->db->rollback();
228 $sql .=
" t.fk_loan,";
232 $sql .=
" t.amount_capital,";
233 $sql .=
" t.amount_insurance,";
234 $sql .=
" t.amount_interest,";
235 $sql .=
" t.fk_typepayment,";
236 $sql .=
" t.num_payment,";
237 $sql .=
" t.note_private,";
238 $sql .=
" t.note_public,";
239 $sql .=
" t.fk_bank,";
240 $sql .=
" t.fk_user_creat,";
241 $sql .=
" t.fk_user_modif,";
242 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
243 $sql .=
' b.fk_account';
244 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan as t";
245 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
246 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
247 $sql .=
" WHERE t.rowid = ".((int) $id);
249 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
250 $resql = $this->db->query($sql);
252 if ($this->db->num_rows($resql)) {
253 $obj = $this->db->fetch_object($resql);
255 $this->
id = $obj->rowid;
256 $this->
ref = $obj->rowid;
258 $this->fk_loan = $obj->fk_loan;
259 $this->datec = $this->db->jdate($obj->datec);
260 $this->tms = $this->db->jdate($obj->tms);
261 $this->datep = $this->db->jdate($obj->datep);
262 $this->amount_capital = $obj->amount_capital;
263 $this->amount_insurance = $obj->amount_insurance;
264 $this->amount_interest = $obj->amount_interest;
265 $this->fk_typepayment = $obj->fk_typepayment;
266 $this->num_payment = $obj->num_payment;
267 $this->note_private = $obj->note_private;
268 $this->note_public = $obj->note_public;
269 $this->fk_bank = $obj->fk_bank;
270 $this->fk_user_creat = $obj->fk_user_creat;
271 $this->fk_user_modif = $obj->fk_user_modif;
273 $this->type_code = $obj->type_code;
274 $this->type_label = $obj->type_label;
276 $this->bank_account = $obj->fk_account;
277 $this->bank_line = $obj->fk_bank;
279 $this->db->free($resql);
283 $this->error =
"Error ".$this->db->lasterror();
296 public function update($user = 0, $notrigger = 0)
298 global $conf, $langs;
302 if (isset($this->fk_loan)) {
303 $this->fk_loan = (int) $this->fk_loan;
305 if (isset($this->amount_capital)) {
306 $this->amount_capital = trim($this->amount_capital);
308 if (isset($this->amount_insurance)) {
309 $this->amount_insurance = trim($this->amount_insurance);
311 if (isset($this->amount_interest)) {
312 $this->amount_interest = trim($this->amount_interest);
314 if (isset($this->fk_typepayment)) {
315 $this->fk_typepayment = (int) $this->fk_typepayment;
317 if (isset($this->num_payment)) {
318 $this->num_payment = (int) $this->num_payment;
320 if (isset($this->note_private)) {
321 $this->note = trim($this->note_private);
323 if (isset($this->note_public)) {
324 $this->note = trim($this->note_public);
326 if (isset($this->fk_bank)) {
327 $this->fk_bank = (int) $this->fk_bank;
329 if (isset($this->fk_user_creat)) {
330 $this->fk_user_creat = (int) $this->fk_user_creat;
332 if (isset($this->fk_user_modif)) {
333 $this->fk_user_modif = (int) $this->fk_user_modif;
339 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_loan SET";
340 $sql .=
" fk_loan=".(isset($this->fk_loan) ? $this->fk_loan :
"null").
",";
341 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
342 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
343 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
344 $sql .=
" amount_capital=".(isset($this->amount_capital) ? $this->amount_capital :
"null").
",";
345 $sql .=
" amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance :
"null").
",";
346 $sql .=
" amount_interest=".(isset($this->amount_interest) ? $this->amount_interest :
"null").
",";
347 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
348 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
349 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
350 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
351 $sql .=
" fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) :
"null").
",";
352 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) :
"null").
",";
353 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ?((int) $this->fk_user_modif) :
"null");
354 $sql .=
" WHERE rowid=".((int) $this->
id);
358 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
359 $resql = $this->db->query($sql);
361 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
366 foreach ($this->errors as $errmsg) {
367 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
368 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
370 $this->db->rollback();
386 public function delete($user, $notrigger = 0)
392 if ($this->bank_line > 0) {
394 $accline->fetch($this->bank_line);
395 $result = $accline->delete($user);
397 $this->errors[] = $accline->error;
403 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan";
404 $sql .=
" WHERE rowid=".((int) $this->
id);
406 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
407 $resql = $this->db->query($sql);
409 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
415 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
416 $loan =
new Loan($this->db);
417 $loan->fetch($this->fk_loan);
418 $sum_payment = $loan->getSumPayment();
419 if ($sum_payment == 0) {
420 dol_syslog(get_class($this).
"::delete : set loan to unpaid", LOG_DEBUG);
421 if ($loan->setUnpaid($user) < 1) {
430 foreach ($this->errors as $errmsg) {
431 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
432 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
434 $this->db->rollback();
480 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
487 if (isModEnabled(
"banque")) {
488 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
491 $acc->fetch($accountid);
493 $total = $this->amount_capital;
494 if ($mode ==
'payment_loan') {
499 $bank_line_id = $acc->addline(
513 if ($bank_line_id > 0) {
522 if ($mode ==
'payment_loan') {
523 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id=';
526 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(payment)', $mode);
535 if ($mode ==
'payment_loan') {
536 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.
'/loan/card.php?id=', ($this->label ? $this->label :
''),
'loan');
542 $this->error = $acc->error;
550 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
551 $loan =
new Loan($this->db);
552 $loan->fetch($fk_loan);
553 if ($loan->paid == $loan::STATUS_UNPAID) {
554 dol_syslog(get_class($this).
"::addPaymentToBank : set loan payment to started", LOG_DEBUG);
555 if ($loan->setStarted($user) < 1) {
566 $this->db->rollback();
605 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle =
'', $save_lastsearch_value = -1)
607 global $langs, $conf, $hookmanager;
609 if (!empty($conf->dol_no_mouse_hover)) {
614 $label =
'<u>'.$langs->trans(
"Loan").
'</u>';
615 if (!empty($this->
id)) {
616 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->id;
619 $label .=
' - '.$moretitle;
622 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id='.$this->id;
624 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
625 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
626 $add_save_lastsearch_values = 1;
628 if ($add_save_lastsearch_values) {
629 $url .=
'&save_lastsearch_values=1';
632 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
635 $result .= $linkstart;
637 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
639 if ($withpicto != 2) {
640 $result .= $this->ref;
645 $hookmanager->initHooks(array($this->element .
'dao'));
646 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
647 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
649 $result = $hookmanager->resPrint;
651 $result .= $hookmanager->resPrint;