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 =
null, $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);
362 $this->errors[] =
"Error ".$this->db->lasterror();
367 foreach ($this->errors as $errmsg) {
368 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
369 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
371 $this->db->rollback();
387 public function delete($user, $notrigger = 0)
389 global $conf, $langs;
395 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"bank_url";
396 $sql .=
" WHERE type='payment_loan' AND url_id=".((int) $this->
id);
398 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
399 $resql = $this->db->query($sql);
402 $this->errors[] =
"Error ".$this->db->lasterror();
407 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_loan";
408 $sql .=
" WHERE rowid=".((int) $this->
id);
410 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
411 $resql = $this->db->query($sql);
414 $this->errors[] =
"Error ".$this->db->lasterror();
420 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
421 $loan =
new Loan($this->db);
422 $loan->fetch($this->fk_loan);
423 $sum_payment = $loan->getSumPayment();
424 if ($sum_payment == 0) {
425 dol_syslog(get_class($this).
"::delete : set loan to unpaid", LOG_DEBUG);
426 if ($loan->setUnpaid($user) < 1) {
451 foreach ($this->errors as $errmsg) {
452 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
453 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
455 $this->db->rollback();
501 public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
508 if (isModEnabled(
"banque")) {
509 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
512 $acc->fetch($accountid);
514 $total = $this->amount_capital;
515 if ($mode ==
'payment_loan') {
520 $bank_line_id = $acc->addline(
534 if ($bank_line_id > 0) {
543 if ($mode ==
'payment_loan') {
544 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id=';
547 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(payment)', $mode);
556 if ($mode ==
'payment_loan') {
557 $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.
'/loan/card.php?id=', ($this->label ? $this->label :
''),
'loan');
563 $this->error = $acc->error;
571 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
572 $loan =
new Loan($this->db);
573 $loan->fetch($fk_loan);
574 if ($loan->paid == $loan::STATUS_UNPAID) {
575 dol_syslog(get_class($this).
"::addPaymentToBank : set loan payment to started", LOG_DEBUG);
576 if ($loan->setStarted($user) < 1) {
587 $this->db->rollback();
626 public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle =
'', $save_lastsearch_value = -1)
628 global $langs, $conf, $hookmanager;
630 if (!empty($conf->dol_no_mouse_hover)) {
635 $label =
'<u>'.$langs->trans(
"Loan").
'</u>';
636 if (!empty($this->
id)) {
637 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->id;
640 $label .=
' - '.$moretitle;
643 $url = DOL_URL_ROOT.
'/loan/payment/card.php?id='.$this->id;
645 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
646 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
647 $add_save_lastsearch_values = 1;
649 if ($add_save_lastsearch_values) {
650 $url .=
'&save_lastsearch_values=1';
653 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
656 $result .= $linkstart;
658 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
660 if ($withpicto != 2) {
661 $result .= $this->ref;
666 $hookmanager->initHooks(array($this->element .
'dao'));
667 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
668 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
670 $result = $hookmanager->resPrint;
672 $result .= $hookmanager->resPrint;