dolibarr  13.0.2
paymentloan.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2014-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
3  * Copyright (C) 2015-2018 Frederic France <frederic.france@netlogic.fr>
4  * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
26 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27 
28 
33 {
37  public $element = 'payment_loan';
38 
42  public $table_element = 'payment_loan';
43 
47  public $picto = 'money-bill-alt';
48 
52  public $fk_loan;
53 
57  public $datec = '';
58 
59  public $tms = '';
60 
64  public $datep = '';
65 
66  public $amounts = array(); // Array of amounts
67 
68  public $amount_capital; // Total amount of payment
69 
70  public $amount_insurance;
71 
72  public $amount_interest;
73 
77  public $fk_typepayment;
78 
82  public $num_payment;
83 
87  public $fk_bank;
88 
92  public $fk_user_creat;
93 
97  public $fk_user_modif;
98 
99  public $type_code;
100  public $type_label;
101 
102 
108  public function __construct($db)
109  {
110  $this->db = $db;
111  }
112 
120  public function create($user)
121  {
122  global $conf, $langs;
123 
124  $error = 0;
125 
126  $now = dol_now();
127 
128  // Validate parameters
129  if (!$this->datep)
130  {
131  $this->error = 'ErrorBadValueForParameter';
132  return -1;
133  }
134 
135  // Clean parameters
136  if (isset($this->fk_loan)) $this->fk_loan = (int) $this->fk_loan;
137  if (isset($this->amount_capital)) $this->amount_capital = price2num($this->amount_capital ? $this->amount_capital : 0);
138  if (isset($this->amount_insurance)) $this->amount_insurance = price2num($this->amount_insurance ? $this->amount_insurance : 0);
139  if (isset($this->amount_interest)) $this->amount_interest = price2num($this->amount_interest ? $this->amount_interest : 0);
140  if (isset($this->fk_typepayment)) $this->fk_typepayment = (int) $this->fk_typepayment;
141  if (isset($this->num_payment)) $this->num_payment = (int) $this->num_payment;
142  if (isset($this->note_private)) $this->note_private = trim($this->note_private);
143  if (isset($this->note_public)) $this->note_public = trim($this->note_public);
144  if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
145  if (isset($this->fk_user_creat)) $this->fk_user_creat = (int) $this->fk_user_creat;
146  if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
147 
148  $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
149  $totalamount = price2num($totalamount);
150 
151  // Check parameters
152  if ($totalamount == 0) return -1; // Negative amounts are accepted for reject prelevement but not null
153 
154 
155  $this->db->begin();
156 
157  if ($totalamount != 0)
158  {
159  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
160  $sql .= " fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
161  $sql .= " VALUES (".$this->chid.", '".$this->db->idate($now)."',";
162  $sql .= " '".$this->db->idate($this->datep)."',";
163  $sql .= " ".$this->amount_capital.",";
164  $sql .= " ".$this->amount_insurance.",";
165  $sql .= " ".$this->amount_interest.",";
166  $sql .= " ".$this->paymenttype.", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_private)."', '".$this->db->escape($this->note_public)."', ".$user->id.",";
167  $sql .= " 0)";
168 
169  dol_syslog(get_class($this)."::create", LOG_DEBUG);
170  $resql = $this->db->query($sql);
171  if ($resql)
172  {
173  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_loan");
174  } else {
175  $this->error = $this->db->lasterror();
176  $error++;
177  }
178  }
179 
180  if ($totalamount != 0 && !$error)
181  {
182  $this->amount_capital = $totalamount;
183  $this->db->commit();
184  return $this->id;
185  } else {
186  $this->error = $this->db->lasterror();
187  $this->db->rollback();
188  return -1;
189  }
190  }
191 
198  public function fetch($id)
199  {
200  global $langs;
201  $sql = "SELECT";
202  $sql .= " t.rowid,";
203  $sql .= " t.fk_loan,";
204  $sql .= " t.datec,";
205  $sql .= " t.tms,";
206  $sql .= " t.datep,";
207  $sql .= " t.amount_capital,";
208  $sql .= " t.amount_insurance,";
209  $sql .= " t.amount_interest,";
210  $sql .= " t.fk_typepayment,";
211  $sql .= " t.num_payment,";
212  $sql .= " t.note_private,";
213  $sql .= " t.note_public,";
214  $sql .= " t.fk_bank,";
215  $sql .= " t.fk_user_creat,";
216  $sql .= " t.fk_user_modif,";
217  $sql .= " pt.code as type_code, pt.libelle as type_label,";
218  $sql .= ' b.fk_account';
219  $sql .= " FROM ".MAIN_DB_PREFIX."payment_loan as t";
220  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
221  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
222  $sql .= " WHERE t.rowid = ".$id;
223 
224  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
225  $resql = $this->db->query($sql);
226  if ($resql)
227  {
228  if ($this->db->num_rows($resql))
229  {
230  $obj = $this->db->fetch_object($resql);
231 
232  $this->id = $obj->rowid;
233  $this->ref = $obj->rowid;
234 
235  $this->fk_loan = $obj->fk_loan;
236  $this->datec = $this->db->jdate($obj->datec);
237  $this->tms = $this->db->jdate($obj->tms);
238  $this->datep = $this->db->jdate($obj->datep);
239  $this->amount_capital = $obj->amount_capital;
240  $this->amount_insurance = $obj->amount_insurance;
241  $this->amount_interest = $obj->amount_interest;
242  $this->fk_typepayment = $obj->fk_typepayment;
243  $this->num_payment = $obj->num_payment;
244  $this->note_private = $obj->note_private;
245  $this->note_public = $obj->note_public;
246  $this->fk_bank = $obj->fk_bank;
247  $this->fk_user_creat = $obj->fk_user_creat;
248  $this->fk_user_modif = $obj->fk_user_modif;
249 
250  $this->type_code = $obj->type_code;
251  $this->type_label = $obj->type_label;
252 
253  $this->bank_account = $obj->fk_account;
254  $this->bank_line = $obj->fk_bank;
255  }
256  $this->db->free($resql);
257 
258  return 1;
259  } else {
260  $this->error = "Error ".$this->db->lasterror();
261  return -1;
262  }
263  }
264 
265 
273  public function update($user = 0, $notrigger = 0)
274  {
275  global $conf, $langs;
276  $error = 0;
277 
278  // Clean parameters
279  if (isset($this->fk_loan)) $this->fk_loan = (int) $this->fk_loan;
280  if (isset($this->amount_capital)) $this->amount_capital = trim($this->amount_capital);
281  if (isset($this->amount_insurance)) $this->amount_insurance = trim($this->amount_insurance);
282  if (isset($this->amount_interest)) $this->amount_interest = trim($this->amount_interest);
283  if (isset($this->fk_typepayment)) $this->fk_typepayment = (int) $this->fk_typepayment;
284  if (isset($this->num_payment)) $this->num_payment = (int) $this->num_payment;
285  if (isset($this->note_private)) $this->note = trim($this->note_private);
286  if (isset($this->note_public)) $this->note = trim($this->note_public);
287  if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
288  if (isset($this->fk_user_creat)) $this->fk_user_creat = (int) $this->fk_user_creat;
289  if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
290 
291  // Check parameters
292 
293  // Update request
294  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET";
295 
296  $sql .= " fk_loan=".(isset($this->fk_loan) ? $this->fk_loan : "null").",";
297  $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
298  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
299  $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
300  $sql .= " amount_capital=".(isset($this->amount_capital) ? $this->amount_capital : "null").",";
301  $sql .= " amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance : "null").",";
302  $sql .= " amount_interest=".(isset($this->amount_interest) ? $this->amount_interest : "null").",";
303  $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
304  $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
305  $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
306  $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
307  $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
308  $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
309  $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null")."";
310 
311  $sql .= " WHERE rowid=".$this->id;
312 
313  $this->db->begin();
314 
315  dol_syslog(get_class($this)."::update", LOG_DEBUG);
316  $resql = $this->db->query($sql);
317  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
318 
319  // Commit or rollback
320  if ($error)
321  {
322  foreach ($this->errors as $errmsg)
323  {
324  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
325  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
326  }
327  $this->db->rollback();
328  return -1 * $error;
329  } else {
330  $this->db->commit();
331  return 1;
332  }
333  }
334 
335 
343  public function delete($user, $notrigger = 0)
344  {
345  global $conf, $langs;
346  $error = 0;
347 
348  $this->db->begin();
349 
350  if (!$error)
351  {
352  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
353  $sql .= " WHERE type='payment_loan' AND url_id=".$this->id;
354 
355  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
356  $resql = $this->db->query($sql);
357  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
358  }
359 
360  if (!$error)
361  {
362  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan";
363  $sql .= " WHERE rowid=".$this->id;
364 
365  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
366  $resql = $this->db->query($sql);
367  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
368  }
369 
370  // Set loan unpaid if loan has no other payment
371  if (!$error)
372  {
373  require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
374  $loan = new Loan($this->db);
375  $loan->fetch($this->fk_loan);
376  $sum_payment = $loan->getSumPayment();
377  if ($sum_payment == 0)
378  {
379  dol_syslog(get_class($this)."::delete : set loan to unpaid", LOG_DEBUG);
380  if ($loan->set_unpaid($user) < 1)
381  {
382  $error++;
383  dol_print_error($this->db);
384  }
385  }
386  }
387 
388  //if (! $error)
389  //{
390  // if (! $notrigger)
391  // {
392  // Uncomment this and change MYOBJECT to your own tag if you
393  // want this action call a trigger.
394 
396  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
397  //$interface=new Interfaces($this->db);
398  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
399  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
401  // }
402  //}
403 
404  // Commit or rollback
405  if ($error)
406  {
407  foreach ($this->errors as $errmsg)
408  {
409  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
410  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
411  }
412  $this->db->rollback();
413  return -1 * $error;
414  } else {
415  $this->db->commit();
416  return 1;
417  }
418  }
419 
426  public function getLibStatut($mode = 0)
427  {
428  return $this->LibStatut($this->statut, $mode);
429  }
430 
431  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
439  public function LibStatut($status, $mode = 0)
440  {
441  // phpcs:enable
442  return '';
443  }
444 
458  public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
459  {
460  global $conf;
461 
462  $error = 0;
463  $this->db->begin();
464 
465  if (!empty($conf->banque->enabled))
466  {
467  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
468 
469  $acc = new Account($this->db);
470  $acc->fetch($accountid);
471 
472  $total = $this->amount_capital;
473  if ($mode == 'payment_loan') $total = -$total;
474 
475  // Insert payment into llx_bank
476  $bank_line_id = $acc->addline(
477  $this->datep,
478  $this->paymenttype, // Payment mode ID or code ("CHQ or VIR for example")
479  $label,
480  $total,
481  $this->num_payment,
482  '',
483  $user,
484  $emetteur_nom,
485  $emetteur_banque
486  );
487 
488  // Update fk_bank into llx_paiement.
489  // We know the payment who generated the account write
490  if ($bank_line_id > 0)
491  {
492  $result = $this->update_fk_bank($bank_line_id);
493  if ($result <= 0)
494  {
495  $error++;
496  dol_print_error($this->db);
497  }
498 
499  // Add link 'payment_loan' in bank_url between payment and bank transaction
500  $url = '';
501  if ($mode == 'payment_loan') $url = DOL_URL_ROOT.'/loan/payment/card.php?id=';
502  if ($url)
503  {
504  $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(payment)', $mode);
505  if ($result <= 0)
506  {
507  $error++;
508  dol_print_error($this->db);
509  }
510  }
511 
512 
513  // Add link 'loan' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
514  if ($mode == 'payment_loan')
515  {
516  $result = $acc->add_url_line($bank_line_id, $fk_loan, DOL_URL_ROOT.'/loan/card.php?id=', ($this->label ? $this->label : ''), 'loan');
517  if ($result <= 0) dol_print_error($this->db);
518  }
519  } else {
520  $this->error = $acc->error;
521  $error++;
522  }
523  }
524 
525 
526  // Set loan payment started if no set
527  if (!$error)
528  {
529  require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
530  $loan = new Loan($this->db);
531  $loan->fetch($fk_loan);
532  if ($loan->paid == $loan::STATUS_UNPAID)
533  {
534  dol_syslog(get_class($this)."::addPaymentToBank : set loan payment to started", LOG_DEBUG);
535  if ($loan->set_started($user) < 1)
536  {
537  $error++;
538  dol_print_error($this->db);
539  }
540  }
541  }
542 
543  if (!$error)
544  {
545  $this->db->commit();
546  return 1;
547  }
548  else {
549  $this->db->rollback();
550  return -1;
551  }
552  }
553 
554 
555  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
562  public function update_fk_bank($id_bank)
563  {
564  // phpcs:enable
565  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_loan SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id;
566 
567  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
568  $result = $this->db->query($sql);
569  if ($result)
570  {
571  $this->fk_bank = $id_bank;
572  return 1;
573  } else {
574  $this->error = $this->db->error();
575  return 0;
576  }
577  }
578 
589  public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
590  {
591  global $langs, $conf;
592 
593  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
594 
595  $result = '';
596  $label = '<u>'.$langs->trans("Loan").'</u>';
597  if (!empty($this->id)) {
598  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
599  }
600  if ($moretitle) $label .= ' - '.$moretitle;
601 
602  $url = DOL_URL_ROOT.'/loan/payment/card.php?id='.$this->id;
603 
604  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
605  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
606  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
607 
608  $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
609  $linkend = '</a>';
610 
611  $result .= $linkstart;
612  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
613  if ($withpicto != 2) $result .= $this->ref;
614  $result .= $linkend;
615 
616  return $result;
617  }
618 }
addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
Add record into bank for payment with links between this bank record and invoices of payment...
Loan.
Definition: loan.class.php:30
__construct($db)
Constructor.
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
Definition: list.php:632
update_fk_bank($id_bank)
Update link between loan&#39;s payment and the line generate in llx_bank.
create($user)
Create payment of loan into database.
dol_now($mode= 'auto')
Return date for now.
getNomUrl($withpicto=0, $maxlen=0, $notooltip=0, $moretitle= '', $save_lastsearch_value=-1)
Return clicable name (with eventually a picto)
fetch($id)
Load object in memory from database.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage bank accounts.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Class to manage payments of loans.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
update($user=0, $notrigger=0)
Update database.
print $_SERVER["PHP_SELF"]
Edit parameters.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un statut donne.
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;une facture (brouillon, validee, abandonnee, payee) ...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
if(!empty($search_group)) natural_search(array("g.nom"g note
Definition: list.php:122
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...