dolibarr  17.0.4
schedule.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 Franck Moreau <franck.moreau@theobald.com>
3  * Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.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 // Load Dolibarr environment
27 require '../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/loan.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/loan/class/paymentloan.class.php';
33 
34 $loanid = GETPOST('loanid', 'int');
35 $action = GETPOST('action', 'aZ09');
36 
37 // Security check
38 $socid = 0;
39 if (GETPOSTISSET('socid')) {
40  $socid = GETPOST('socid', 'int');
41 }
42 if ($user->socid) {
43  $socid = $user->socid;
44 }
45 if (empty($user->rights->loan->calc)) {
47 }
48 
49 // Load translation files required by the page
50 $langs->loadLangs(array("compta", "bills", "loan"));
51 
52 $object = new Loan($db);
53 $object->fetch($loanid);
54 
55 $echeances = new LoanSchedule($db);
56 $echeances->fetchAll($object->id);
57 
58 if ($object->paid > 0 && count($echeances->lines) == 0) {
59  $pay_without_schedule = 1;
60 }
61 
62 /*
63  * Actions
64  */
65 
66 if ($action == 'createecheancier' && empty($pay_without_schedule)) {
67  $db->begin();
68  $i = 1;
69  while ($i < $object->nbterm + 1) {
70  $date = GETPOST('hi_date'.$i, 'int');
71  $mens = price2num(GETPOST('mens'.$i));
72  $int = price2num(GETPOST('hi_interets'.$i));
73  $insurance = price2num(GETPOST('hi_insurance'.$i));
74 
75  $new_echeance = new LoanSchedule($db);
76 
77  $new_echeance->fk_loan = $object->id;
78  $new_echeance->datec = dol_now();
79  $new_echeance->tms = dol_now();
80  $new_echeance->datep = $date;
81  $new_echeance->amount_capital = $mens - $int;
82  $new_echeance->amount_insurance = $insurance;
83  $new_echeance->amount_interest = $int;
84  $new_echeance->fk_typepayment = 3;
85  $new_echeance->fk_bank = 0;
86  $new_echeance->fk_user_creat = $user->id;
87  $new_echeance->fk_user_modif = $user->id;
88  $result = $new_echeance->create($user);
89  if ($result < 0) {
90  setEventMessages($new_echeance->error, $echeance->errors, 'errors');
91  $db->rollback();
92  unset($echeances->lines);
93  break;
94  }
95  $echeances->lines[] = $new_echeance;
96  $i++;
97  }
98  if ($result > 0) {
99  $db->commit();
100  }
101 }
102 
103 if ($action == 'updateecheancier' && empty($pay_without_schedule)) {
104  $db->begin();
105  $i = 1;
106  while ($i < $object->nbterm + 1) {
107  $mens = price2num(GETPOST('mens'.$i));
108  $int = price2num(GETPOST('hi_interets'.$i));
109  $id = GETPOST('hi_rowid'.$i);
110  $insurance = price2num(GETPOST('hi_insurance'.$i));
111 
112  $new_echeance = new LoanSchedule($db);
113  $new_echeance->fetch($id);
114  $new_echeance->tms = dol_now();
115  $new_echeance->amount_capital = $mens - $int;
116  $new_echeance->amount_insurance = $insurance;
117  $new_echeance->amount_interest = $int;
118  $new_echeance->fk_user_modif = $user->id;
119  $result = $new_echeance->update($user, 0);
120  if ($result < 0) {
121  setEventMessages(null, $new_echeance->errors, 'errors');
122  $db->rollback();
123  $echeances->fetchAll($object->id);
124  break;
125  }
126 
127  $echeances->lines[$i - 1] = $new_echeance;
128  $i++;
129  }
130  if ($result > 0) {
131  $db->commit();
132  }
133 }
134 
135 /*
136  * View
137  */
138 
139 $title = $langs->trans("Loan").' - '.$langs->trans("Card");
140 $help_url = 'EN:Module_Loan|FR:Module_Emprunt';
141 llxHeader("", $title, $help_url);
142 
143 $head = loan_prepare_head($object);
144 print dol_get_fiche_head($head, 'FinancialCommitment', $langs->trans("Loan"), -1, 'bill');
145 
146 $linkback = '<a href="'.DOL_URL_ROOT.'/loan/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
147 
148 $morehtmlref = '<div class="refidno">';
149 // Ref loan
150 $morehtmlref .= $form->editfieldkey("Label", 'label', $object->label, $object, 0, 'string', '', 0, 1);
151 $morehtmlref .= $form->editfieldval("Label", 'label', $object->label, $object, 0, 'string', '', null, null, '', 1);
152 // Project
153 if (!empty($conf->project->enabled)) {
154  $langs->loadLangs(array("projects"));
155  $morehtmlref .= '<br>'.$langs->trans('Project').' : ';
156  if ($user->rights->loan->write) {
157  if ($action != 'classify') {
158  //$morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> : ';
159  if ($action == 'classify') {
160  //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
161  $morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
162  $morehtmlref .= '<input type="hidden" name="action" value="classin">';
163  $morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
164  $morehtmlref .= $formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
165  $morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
166  $morehtmlref .= '</form>';
167  } else {
168  $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1, '', 'maxwidth300');
169  }
170  }
171  } else {
172  if (!empty($object->fk_project)) {
173  $proj = new Project($db);
174  $proj->fetch($object->fk_project);
175  $morehtmlref .= ' : '.$proj->getNomUrl(1);
176  if ($proj->title) {
177  $morehtmlref .= ' - '.$proj->title;
178  }
179  } else {
180  $morehtmlref .= '';
181  }
182  }
183 }
184 $morehtmlref .= '</div>';
185 
186 $morehtmlright = '';
187 
188 dol_banner_tab($object, 'loanid', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
189 
190 ?>
191 <script type="text/javascript">
192 $(document).ready(function() {
193  $('[name^="mens"]').focusout(function() {
194  var echeance=$(this).attr('ech');
195  var mens=price2numjs($(this).val());
196  var idcap=echeance-1;
197  idcap = '#hi_capital'+idcap;
198  var capital=price2numjs($(idcap).val());
199  console.log("Change montly amount echeance="+echeance+" idcap="+idcap+" capital="+capital);
200  $.ajax({
201  method: "GET",
202  dataType: 'json',
203  url: 'calcmens.php',
204  data: { echeance: echeance, mens: mens, capital:capital, rate:<?php echo $object->rate / 100; ?>, nbterm: <?php echo $object->nbterm; ?>, token: '<?php echo currentToken(); ?>' },
205  success: function(data) {
206  $.each(data, function(index, element) {
207  var idcap_res='#hi_capital'+index;
208  var idcap_res_srt='#capital'+index;
209  var interet_res='#hi_interets'+index;
210  var interet_res_str='#interets'+index;
211  var men_res='#mens'+index;
212  $(idcap_res).val(element.cap_rest);
213  $(idcap_res_srt).text(element.cap_rest_str);
214  $(interet_res).val(element.interet);
215  $(interet_res_str).text(element.interet_str);
216  $(men_res).val(element.mens);
217  });
218  }
219  });
220  });
221 });
222 </script>
223 <?php
224 
225 if ($pay_without_schedule == 1) {
226  print '<div class="warning">'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'</div>'."\n";
227 }
228 
229 print '<form name="createecheancier" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
230 print '<input type="hidden" name="token" value="'.newToken().'">';
231 print '<input type="hidden" name="loanid" value="'.$loanid.'">';
232 if (count($echeances->lines) > 0) {
233  print '<input type="hidden" name="action" value="updateecheancier">';
234 } else {
235  print '<input type="hidden" name="action" value="createecheancier">';
236 }
237 
238 //print_fiche_titre($langs->trans("FinancialCommitment"));
239 print '<br>';
240 
241 print '<div class="div-table-responsive-no-min">';
242 print '<table class="border centpercent">';
243 
244 $colspan = 6;
245 if (count($echeances->lines) > 0) {
246  $colspan++;
247 }
248 
249 print '<tr class="liste_titre">';
250 print '<th class="center">'.$langs->trans("Term").'</th>';
251 print '<th class="center">'.$langs->trans("Date").'</th>';
252 print '<th class="center">'.$langs->trans("Insurance");
253 print '<th class="center">'.$langs->trans("InterestAmount").'</th>';
254 print '<th class="center">'.$langs->trans("Amount").'</th>';
255 print '<th class="center">'.$langs->trans("CapitalRemain");
256 print '<br>('.price($object->capital, 0, '', 1, -1, -1, $conf->currency).')';
257 print '<input type="hidden" name="hi_capital0" id ="hi_capital0" value="'.$object->capital.'">';
258 print '</th>';
259 if (count($echeances->lines) > 0) {
260  print '<th class="center">'.$langs->trans('DoPayment').'</th>';
261 }
262 print '</tr>'."\n";
263 
264 if ($object->nbterm > 0 && count($echeances->lines) == 0) {
265  $i = 1;
266  $capital = $object->capital;
267  $insurance = $object->insurance_amount / $object->nbterm;
268  $insurance = price2num($insurance, 'MT');
269  $regulInsurance = price2num($object->insurance_amount - ($insurance * $object->nbterm));
270  while ($i < $object->nbterm + 1) {
271  $mens = price2num($echeances->calcMonthlyPayments($capital, $object->rate / 100, $object->nbterm - $i + 1), 'MT');
272  $int = ($capital * ($object->rate / 12)) / 100;
273  $int = price2num($int, 'MT');
274  $insu = ($insurance + (($i == 1) ? $regulInsurance : 0));
275  $cap_rest = price2num($capital - ($mens - $int), 'MT');
276  print '<tr>';
277  print '<td class="center" id="n'.$i.'">'.$i.'</td>';
278  print '<td class="center" id ="date'.$i.'"><input type="hidden" name="hi_date'.$i.'" id ="hi_date'.$i.'" value="'.dol_time_plus_duree($object->datestart, $i - 1, 'm').'">'.dol_print_date(dol_time_plus_duree($object->datestart, $i - 1, 'm'), 'day').'</td>';
279  print '<td class="center amount" id="insurance'.$i.'">'.price($insurance + (($i == 1) ? $regulInsurance : 0), 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="hi_insurance'.$i.'" id ="hi_insurance'.$i.'" value="'.($insurance + (($i == 1) ? $regulInsurance : 0)).'">';
280  print '<td class="center amount" id="interets'.$i.'">'.price($int, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="hi_interets'.$i.'" id ="hi_interets'.$i.'" value="'.$int.'">';
281  print '<td class="center"><input class="width75 right" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
282  print '<td class="center amount" id="capital'.$i.'">'.price($cap_rest).'</td><input type="hidden" name="hi_capital'.$i.'" id ="hi_capital'.$i.'" value="'.$cap_rest.'">';
283  print '</tr>'."\n";
284  $i++;
285  $capital = $cap_rest;
286  }
287 } elseif (count($echeances->lines) > 0) {
288  $i = 1;
289  $capital = $object->capital;
290  $insurance = $object->insurance_amount / $object->nbterm;
291  $insurance = price2num($insurance, 'MT');
292  $regulInsurance = price2num($object->insurance_amount - ($insurance * $object->nbterm));
293  $printed = false;
294  foreach ($echeances->lines as $line) {
295  $mens = $line->amount_capital + $line->amount_interest;
296  $int = $line->amount_interest;
297  $insu = ($insurance + (($i == 1) ? $regulInsurance : 0));
298  $cap_rest = price2num($capital - ($mens - $int), 'MT');
299 
300  print '<tr>';
301  print '<td class="center" id="n'.$i.'"><input type="hidden" name="hi_rowid'.$i.'" id ="hi_rowid'.$i.'" value="'.$line->id.'">'.$i.'</td>';
302  print '<td class="center" id ="date'.$i.'"><input type="hidden" name="hi_date'.$i.'" id ="hi_date'.$i.'" value="'.$line->datep.'">'.dol_print_date($line->datep, 'day').'</td>';
303  print '<td class="center amount" id="insurance'.$i.'">'.price($insu, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="hi_insurance'.$i.'" id ="hi_insurance'.$i.'" value="'.$insu.'">';
304  print '<td class="center amount" id="interets'.$i.'">'.price($int, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="hi_interets'.$i.'" id ="hi_interets'.$i.'" value="'.$int.'">';
305  if (empty($line->fk_bank)) {
306  print '<td class="center"><input class="right width75" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
307  } else {
308  print '<td class="center amount">'.price($mens, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="mens'.$i.'" id ="mens'.$i.'" value="'.$mens.'">';
309  }
310 
311  print '<td class="center amount" id="capital'.$i.'">'.price($cap_rest, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="hi_capital'.$i.'" id ="hi_capital'.$i.'" value="'.$cap_rest.'">';
312  print '<td class="center">';
313  if (!empty($line->fk_bank)) {
314  print $langs->trans('Paid');
315  if (!empty($line->fk_payment_loan)) {
316  print '&nbsp;<a href="'.DOL_URL_ROOT.'/loan/payment/card.php?id='.$line->fk_payment_loan.'">('.img_object($langs->trans("Payment"), "payment").' '.$line->fk_payment_loan.')</a>';
317  }
318  } elseif (!$printed) {
319  print '<a class="butAction smallpaddingimp" href="'.DOL_URL_ROOT.'/loan/payment/payment.php?id='.$object->id.'&action=create">'.$langs->trans('DoPayment').'</a>';
320  $printed = true;
321  }
322  print '</td>';
323  print '</tr>'."\n";
324  $i++;
325  $capital = $cap_rest;
326  }
327 }
328 
329 print '</table>';
330 print '</div>';
331 
332 print '</br>';
333 
334 if (count($echeances->lines) == 0) {
335  $label = $langs->trans("Create");
336 } else {
337  $label = $langs->trans("Save");
338 }
339 print '<div class="center"><input type="submit" class="button button-add" value="'.$label.'" '.(($pay_without_schedule == 1) ? 'disabled title="'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'"' : '').'title=""></div>';
340 print '</form>';
341 
342 // End of page
343 llxFooter();
344 $db->close();
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $help_url
View.
Definition: agenda.php:118
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
Loan.
Definition: loan.class.php:31
Class to manage Schedule of loans.
Class to manage projects.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:121
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
price2numjs(amount)
Function similar to PHP price2num()
loan_prepare_head($object)
Prepare array with list of tabs.
Definition: loan.lib.php:33
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.