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