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