dolibarr  19.0.0-dev
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 } else {
61  $pay_without_schedule = 0;
62 }
63 
64 /*
65  * Actions
66  */
67 
68 if ($action == 'createecheancier' && empty($pay_without_schedule)) {
69  $db->begin();
70  $i = 1;
71  while ($i < $object->nbterm + 1) {
72  $date = GETPOST('hi_date'.$i, 'int');
73  $mens = price2num(GETPOST('mens'.$i));
74  $int = price2num(GETPOST('hi_interets'.$i));
75  $insurance = price2num(GETPOST('hi_insurance'.$i));
76 
77  $new_echeance = new LoanSchedule($db);
78 
79  $new_echeance->fk_loan = $object->id;
80  $new_echeance->datec = dol_now();
81  $new_echeance->tms = dol_now();
82  $new_echeance->datep = $date;
83  $new_echeance->amount_capital = $mens - $int;
84  $new_echeance->amount_insurance = $insurance;
85  $new_echeance->amount_interest = $int;
86  $new_echeance->fk_typepayment = 3;
87  $new_echeance->fk_bank = 0;
88  $new_echeance->fk_user_creat = $user->id;
89  $new_echeance->fk_user_modif = $user->id;
90  $result = $new_echeance->create($user);
91  if ($result < 0) {
92  setEventMessages($new_echeance->error, $echeance->errors, 'errors');
93  $db->rollback();
94  unset($echeances->lines);
95  break;
96  }
97  $echeances->lines[] = $new_echeance;
98  $i++;
99  }
100  if ($result > 0) {
101  $db->commit();
102  }
103 }
104 
105 if ($action == 'updateecheancier' && empty($pay_without_schedule)) {
106  $db->begin();
107  $i = 1;
108  while ($i < $object->nbterm + 1) {
109  $mens = price2num(GETPOST('mens'.$i));
110  $int = price2num(GETPOST('hi_interets'.$i));
111  $id = GETPOST('hi_rowid'.$i);
112  $insurance = price2num(GETPOST('hi_insurance'.$i));
113 
114  $new_echeance = new LoanSchedule($db);
115  $new_echeance->fetch($id);
116  $new_echeance->tms = dol_now();
117  $new_echeance->amount_capital = $mens - $int;
118  $new_echeance->amount_insurance = $insurance;
119  $new_echeance->amount_interest = $int;
120  $new_echeance->fk_user_modif = $user->id;
121  $result = $new_echeance->update($user, 0);
122  if ($result < 0) {
123  setEventMessages(null, $new_echeance->errors, 'errors');
124  $db->rollback();
125  $echeances->fetchAll($object->id);
126  break;
127  }
128 
129  $echeances->lines[$i - 1] = $new_echeance;
130  $i++;
131  }
132  if ($result > 0) {
133  $db->commit();
134  }
135 }
136 
137 /*
138  * View
139  */
140 
141 $title = $langs->trans("Loan").' - '.$langs->trans("Card");
142 $help_url = 'EN:Module_Loan|FR:Module_Emprunt';
143 llxHeader("", $title, $help_url);
144 
145 $head = loan_prepare_head($object);
146 print dol_get_fiche_head($head, 'FinancialCommitment', $langs->trans("Loan"), -1, 'bill');
147 
148 $linkback = '<a href="'.DOL_URL_ROOT.'/loan/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
149 
150 $morehtmlref = '<div class="refidno">';
151 // Ref loan
152 $morehtmlref .= $form->editfieldkey("Label", 'label', $object->label, $object, 0, 'string', '', 0, 1);
153 $morehtmlref .= $form->editfieldval("Label", 'label', $object->label, $object, 0, 'string', '', null, null, '', 1);
154 // Project
155 if (isModEnabled('project')) {
156  $langs->loadLangs(array("projects"));
157  $morehtmlref .= '<br>'.$langs->trans('Project').' : ';
158  if ($user->rights->loan->write) {
159  if ($action != 'classify') {
160  //$morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> : ';
161  if ($action == 'classify') {
162  //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
163  $morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
164  $morehtmlref .= '<input type="hidden" name="action" value="classin">';
165  $morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
166  $morehtmlref .= $formproject->select_projects(-1, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
167  $morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
168  $morehtmlref .= '</form>';
169  } else {
170  $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, -1, $object->fk_project, 'none', 0, 0, 0, 1, '', 'maxwidth300');
171  }
172  }
173  } else {
174  if (!empty($object->fk_project)) {
175  $proj = new Project($db);
176  $proj->fetch($object->fk_project);
177  $morehtmlref .= ' : '.$proj->getNomUrl(1);
178  if ($proj->title) {
179  $morehtmlref .= ' - '.$proj->title;
180  }
181  } else {
182  $morehtmlref .= '';
183  }
184  }
185 }
186 $morehtmlref .= '</div>';
187 
188 $morehtmlright = '';
189 
190 dol_banner_tab($object, 'loanid', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
191 
192 ?>
193 <script type="text/javascript">
194 $(document).ready(function() {
195  $('[name^="mens"]').focusout(function() {
196  var echeance=$(this).attr('ech');
197  var mens=price2numjs($(this).val());
198  var idcap=echeance-1;
199  idcap = '#hi_capital'+idcap;
200  var capital=price2numjs($(idcap).val());
201  console.log("Change montly amount echeance="+echeance+" idcap="+idcap+" capital="+capital);
202  $.ajax({
203  method: "GET",
204  dataType: 'json',
205  url: 'calcmens.php',
206  data: { echeance: echeance, mens: mens, capital:capital, rate:<?php echo $object->rate / 100; ?>, nbterm: <?php echo $object->nbterm; ?>, token: '<?php echo currentToken(); ?>' },
207  success: function(data) {
208  $.each(data, function(index, element) {
209  var idcap_res='#hi_capital'+index;
210  var idcap_res_srt='#capital'+index;
211  var interet_res='#hi_interets'+index;
212  var interet_res_str='#interets'+index;
213  var men_res='#mens'+index;
214  $(idcap_res).val(element.cap_rest);
215  $(idcap_res_srt).text(element.cap_rest_str);
216  $(interet_res).val(element.interet);
217  $(interet_res_str).text(element.interet_str);
218  $(men_res).val(element.mens);
219  });
220  }
221  });
222  });
223 });
224 </script>
225 <?php
226 
227 if ($pay_without_schedule == 1) {
228  print '<div class="warning">'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'</div>'."\n";
229 }
230 
231 print '<form name="createecheancier" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
232 print '<input type="hidden" name="token" value="'.newToken().'">';
233 print '<input type="hidden" name="loanid" value="'.$loanid.'">';
234 if (count($echeances->lines) > 0) {
235  print '<input type="hidden" name="action" value="updateecheancier">';
236 } else {
237  print '<input type="hidden" name="action" value="createecheancier">';
238 }
239 
240 //print_fiche_titre($langs->trans("FinancialCommitment"));
241 print '<br>';
242 
243 print '<div class="div-table-responsive-no-min">';
244 print '<table class="border centpercent">';
245 
246 $colspan = 6;
247 if (count($echeances->lines) > 0) {
248  $colspan++;
249 }
250 
251 print '<tr class="liste_titre">';
252 print '<th class="center">'.$langs->trans("Term").'</th>';
253 print '<th class="center">'.$langs->trans("Date").'</th>';
254 print '<th class="center">'.$langs->trans("Insurance");
255 print '<th class="center">'.$langs->trans("InterestAmount").'</th>';
256 print '<th class="center">'.$langs->trans("Amount").'</th>';
257 print '<th class="center">'.$langs->trans("CapitalRemain");
258 print '<br>('.price($object->capital, 0, '', 1, -1, -1, $conf->currency).')';
259 print '<input type="hidden" name="hi_capital0" id ="hi_capital0" value="'.$object->capital.'">';
260 print '</th>';
261 if (count($echeances->lines) > 0) {
262  print '<th class="center">'.$langs->trans('DoPayment').'</th>';
263 }
264 print '</tr>'."\n";
265 
266 if ($object->nbterm > 0 && count($echeances->lines) == 0) {
267  $i = 1;
268  $capital = $object->capital;
269  $insurance = $object->insurance_amount / $object->nbterm;
270  $insurance = price2num($insurance, 'MT');
271  $regulInsurance = price2num($object->insurance_amount - ($insurance * $object->nbterm));
272  while ($i < $object->nbterm + 1) {
273  $mens = price2num($echeances->calcMonthlyPayments($capital, $object->rate / 100, $object->nbterm - $i + 1), 'MT');
274  $int = ($capital * ($object->rate / 12)) / 100;
275  $int = price2num($int, 'MT');
276  $insu = ($insurance + (($i == 1) ? $regulInsurance : 0));
277  $cap_rest = price2num($capital - ($mens - $int), 'MT');
278  print '<tr>';
279  print '<td class="center" id="n'.$i.'">'.$i.'</td>';
280  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>';
281  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)).'">';
282  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.'">';
283  print '<td class="center"><input class="width75 right" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
284  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.'">';
285  print '</tr>'."\n";
286  $i++;
287  $capital = $cap_rest;
288  }
289 } elseif (count($echeances->lines) > 0) {
290  $i = 1;
291  $capital = $object->capital;
292  $insurance = $object->insurance_amount / $object->nbterm;
293  $insurance = price2num($insurance, 'MT');
294  $regulInsurance = price2num($object->insurance_amount - ($insurance * $object->nbterm));
295  $printed = false;
296  foreach ($echeances->lines as $line) {
297  $mens = $line->amount_capital + $line->amount_interest;
298  $int = $line->amount_interest;
299  $insu = ($insurance + (($i == 1) ? $regulInsurance : 0));
300  $cap_rest = price2num($capital - ($mens - $int), 'MT');
301 
302  print '<tr>';
303  print '<td class="center" id="n'.$i.'"><input type="hidden" name="hi_rowid'.$i.'" id ="hi_rowid'.$i.'" value="'.$line->id.'">'.$i.'</td>';
304  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>';
305  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.'">';
306  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.'">';
307  if (empty($line->fk_bank)) {
308  print '<td class="center"><input class="right width75" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
309  } else {
310  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.'">';
311  }
312 
313  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.'">';
314  print '<td class="center">';
315  if (!empty($line->fk_bank)) {
316  print $langs->trans('Paid');
317  if (!empty($line->fk_payment_loan)) {
318  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>';
319  }
320  } elseif (!$printed) {
321  print '<a class="butAction smallpaddingimp" href="'.DOL_URL_ROOT.'/loan/payment/payment.php?id='.$object->id.'&action=create">'.$langs->trans('DoPayment').'</a>';
322  $printed = true;
323  }
324  print '</td>';
325  print '</tr>'."\n";
326  $i++;
327  $capital = $cap_rest;
328  }
329 }
330 
331 print '</table>';
332 print '</div>';
333 
334 print '</br>';
335 
336 if (count($echeances->lines) == 0) {
337  $label = $langs->trans("Create");
338 } else {
339  $label = $langs->trans("Save");
340 }
341 print '<div class="center"><input type="submit" class="button button-add" value="'.$label.'" '.(($pay_without_schedule == 1) ? 'disabled title="'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'"' : '').'title=""></div>';
342 print '</form>';
343 
344 // End of page
345 llxFooter();
346 $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:122
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='', $dragdropfile=0)
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)
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
isModEnabled($module)
Is Dolibarr module enabled.
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:120
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.