dolibarr 25.0.0-alpha
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-2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
4 * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
5 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2026 Lenin Rivas <lenin.rivas777@gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29// Load Dolibarr environment
30require '../main.inc.php';
38require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
39require_once DOL_DOCUMENT_ROOT.'/core/lib/loan.lib.php';
40require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
41require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
42require_once DOL_DOCUMENT_ROOT.'/loan/class/paymentloan.class.php';
43require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
44if (isModEnabled('project')) {
45 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
46}
47
48$loanid = GETPOSTINT('loanid');
49$action = GETPOST('action', 'aZ09');
50
51// Security check
52$socid = 0;
53if (GETPOSTISSET('socid')) {
54 $socid = GETPOSTINT('socid');
55}
56if ($user->socid) {
57 $socid = $user->socid;
58}
59if (!$user->hasRight('loan', 'calc')) {
61}
62
63// Load translation files required by the page
64$langs->loadLangs(array("compta", "bills", "loan"));
65
66$object = new Loan($db);
67$object->fetch($loanid);
68
69$echeances = new LoanSchedule($db);
70$echeances->fetchAll($object->id);
71
72if ($object->paid > 0 && count($echeances->lines) == 0) {
73 $pay_without_schedule = 1;
74} else {
75 $pay_without_schedule = 0;
76}
77
78$permissiontoadd = $user->hasRight('loan', 'write');
79
80
81/*
82 * Actions
83 */
84
85if ($action == 'createecheancier' && empty($pay_without_schedule) && $permissiontoadd) {
86 $result = -1;
87 $db->begin();
88 $i = 1;
89 while ($i < $object->nbterm + 1) {
90 $date = GETPOSTINT('hi_date'.$i);
91 $mens = price2num(GETPOST('mens'.$i));
92 if (GETPOSTISSET('interets'.$i)) {
93 $int = price2num(GETPOST('interets'.$i));
94 } else {
95 $int = price2num(GETPOST('hi_interets'.$i));
96 }
97 $insurance = price2num(GETPOST('hi_insurance'.$i));
98 if (GETPOSTISSET('amort'.$i)) {
99 $amort = price2num(GETPOST('amort'.$i));
100 } else {
101 $amort = (float) $mens - (float) $int;
102 }
103
104 $new_echeance = new LoanSchedule($db);
105
106 $new_echeance->fk_loan = $object->id;
107 $new_echeance->datec = dol_now();
108 $new_echeance->tms = dol_now();
109 $new_echeance->datep = $date;
110 $new_echeance->amount_capital = $amort;
111 $new_echeance->amount_insurance = $insurance;
112 $new_echeance->amount_interest = $int;
113 $new_echeance->fk_typepayment = 3;
114 $new_echeance->fk_bank = 0;
115 $new_echeance->fk_user_creat = $user->id;
116 $new_echeance->fk_user_modif = $user->id;
117 $result = $new_echeance->create($user);
118 if ($result < 0) {
119 setEventMessages($new_echeance->error, $new_echeance->errors, 'errors');
120 $db->rollback();
121 $echeances->lines = [];
122 break;
123 }
124 $echeances->lines[] = $new_echeance;
125 $i++;
126 }
127 if ($result > 0) {
128 $db->commit();
129 }
130}
131
132if ($action == 'updateecheancier' && empty($pay_without_schedule) && $permissiontoadd) {
133 $result = -1;
134 $db->begin();
135 $i = 1;
136 while ($i < $object->nbterm + 1) {
137 $mens = price2num(GETPOST('mens'.$i));
138 if (GETPOSTISSET('interets'.$i)) {
139 $int = price2num(GETPOST('interets'.$i));
140 } else {
141 $int = price2num(GETPOST('hi_interets'.$i));
142 }
143 $id = GETPOSTINT('hi_rowid'.$i);
144 $insurance = price2num(GETPOST('hi_insurance'.$i));
145 if (GETPOSTISSET('amort'.$i)) {
146 $amort = price2num(GETPOST('amort'.$i));
147 } else {
148 $amort = (float) $mens - (float) $int;
149 }
150
151 $new_echeance = new LoanSchedule($db);
152 $new_echeance->fetch($id);
153 $new_echeance->tms = dol_now();
154 $new_echeance->amount_capital = $amort;
155 $new_echeance->amount_insurance = $insurance;
156 $new_echeance->amount_interest = $int;
157 $new_echeance->fk_user_modif = $user->id;
158 $result = $new_echeance->update($user, 0);
159 if ($result < 0) {
160 setEventMessages(null, $new_echeance->errors, 'errors');
161 $db->rollback();
162 $echeances->fetchAll($object->id);
163 break;
164 }
165
166 $echeances->lines[$i - 1] = $new_echeance;
167 $i++;
168 }
169 if ($result > 0) {
170 $db->commit();
171 }
172}
173
174if ($action == 'recalculate' && $permissiontoadd) {
175 $newrate = GETPOSTFLOAT('newrate');
176 $keep = (GETPOST('keep', 'aZ09') === 'payment' ? 'payment' : 'term');
177 $datefrom = dol_mktime(0, 0, 0, GETPOSTINT('recalcfrommonth'), GETPOSTINT('recalcfromday'), GETPOSTINT('recalcfromyear'));
178 $oldrate = (float) $object->rate;
179 if (GETPOST('newrate') === '' || $newrate < 0 || empty($datefrom)) {
180 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanNewRate")), null, 'errors');
181 } else {
182 $db->begin();
183 $error = 0;
184 if (count($echeances->lines) == 0) {
185 // No schedule: only the rate of the loan changes
186 $object->rate = $newrate;
187 if ($object->update($user) < 0) {
188 $error++;
189 setEventMessages($object->error, $object->errors, 'errors');
190 } elseif (loanRecordChange($db, $user, $object->id, 'rate', $datefrom, $oldrate, $newrate, '', 0, 0, $object->nbterm, $object->nbterm) < 0) {
191 $error++;
192 setEventMessages($db->lasterror(), null, 'errors');
193 }
194 } else {
195 // First unpaid payment on or after the date; capital left before it
196 $from = -1;
197 $capital = (float) $object->capital;
198 foreach ($echeances->lines as $k => $l) {
199 if (empty($l->fk_bank) && $l->datep >= $datefrom) {
200 $from = (int) $k;
201 break;
202 }
203 $capital -= (float) $l->amount_capital;
204 }
205 $nbtermold = (float) $object->nbterm;
206 $res = loanRecalculateSchedule($db, $user, $object, $echeances->lines, $from, $capital, $newrate, $keep);
207 if ($res['error']) {
208 $error++;
209 setEventMessages($langs->trans($res['error']), null, 'errors');
210 } elseif (loanRecordChange($db, $user, $object->id, 'rate', (int) $echeances->lines[$from]->datep, $oldrate, $newrate, $keep, $res['payment_old'], $res['payment_new'], $nbtermold, $res['nbterm_new']) < 0) {
211 $error++;
212 setEventMessages($db->lasterror(), null, 'errors');
213 }
214 }
215 if ($error) {
216 $db->rollback();
217 } else {
218 $db->commit();
219 setEventMessages($langs->trans("LoanRecalcDone"), null);
220 }
221 $object->fetch($object->id);
222 $echeances = new LoanSchedule($db);
223 $echeances->fetchAll($object->id);
224 }
225}
226
227
228/*
229 * View
230 */
231
232$form = new Form($db);
233$formproject = new FormProjets($db);
234
235$title = $langs->trans("Loan").' - '.$langs->trans("FinancialCommitment");
236$help_url = 'EN:Module_Loan|FR:Module_Emprunt';
237
238llxHeader("", $title, $help_url, '', 0, 0, '', '', '', 'mod-loan page-card_schedule');
239
240$head = loan_prepare_head($object);
241print dol_get_fiche_head($head, 'FinancialCommitment', $langs->trans("Loan"), -1, 'money-bill-alt');
242
243$linkback = '<a href="'.DOL_URL_ROOT.'/loan/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
244
245$morehtmlref = '<div class="refidno">';
246// Ref loan
247$morehtmlref .= $form->editfieldkey("Label", 'label', $object->label, $object, 0, 'string', '', 0, 1);
248$morehtmlref .= $form->editfieldval("Label", 'label', $object->label, $object, 0, 'string', '', null, null, '', 1);
249// Project
250if (isModEnabled('project')) {
251 $langs->loadLangs(array("projects"));
252 $morehtmlref .= '<br>'.$langs->trans('Project').' : ';
253 if ($user->hasRight('loan', 'write')) {
254 if ($action != 'classify') {
255 //$morehtmlref .= '<a class="editfielda" href="'.dolBuildUrl($_SERVER['PHP_SELF'], ['action' => 'classify', 'id' => $object->id], true).'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> : ';
256 if ($action == 'classify') {
257 //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
258 $morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
259 $morehtmlref .= '<input type="hidden" name="action" value="classin">';
260 $morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
261 $morehtmlref .= $formproject->select_projects(-1, (string) $object->fk_project, 'projectid', 16, 0, 1, 0, 1, 0, 0, '', 1);
262 $morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
263 $morehtmlref .= '</form>';
264 } else {
265 $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, -1, (string) $object->fk_project, 'none', 0, 0, 0, 1, '', 'maxwidth300');
266 }
267 }
268 } else {
269 if (!empty($object->fk_project)) {
270 $proj = new Project($db);
271 $proj->fetch((int) $object->fk_project);
272 $morehtmlref .= ' : '.$proj->getNomUrl(1);
273 if ($proj->title) {
274 $morehtmlref .= ' - '.$proj->title;
275 }
276 } else {
277 $morehtmlref .= '';
278 }
279 }
280}
281$morehtmlref .= '</div>';
282
283$morehtmlstatus = '';
284
285dol_banner_tab($object, 'loanid', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlstatus);
286
287?>
288<script type="text/javascript">
289$(document).ready(function() {
290 var timeout = null;
291 var delay = 750; // 0.75 seconds
292
293 function updateTotals() {
294 var totInsu = 0;
295 var totInt = 0;
296 var totAmort = 0;
297 var totMens = 0;
298
299 $('[name^="hi_insurance"]').each(function() {
300 totInsu += (price2numjs($(this).val()) || 0);
301 });
302 $('[name^="hi_interets"]').each(function() {
303 totInt += (price2numjs($(this).val()) || 0);
304 });
305 $('[id^="hi_amort"]').each(function() {
306 totAmort += (price2numjs($(this).val()) || 0);
307 });
308 $('[id^="hi_mens"]').each(function() {
309 totMens += (price2numjs($(this).val()) || 0);
310 });
311
312 $('#total_insurance').text(pricejs(totInsu, 'MT', '<?php echo $conf->currency; ?>'));
313 $('#total_interest').text(pricejs(totInt, 'MT', '<?php echo $conf->currency; ?>'));
314 $('#total_amort').text(pricejs(totAmort, 'MT', '<?php echo $conf->currency; ?>'));
315 $('#total_mens').text(pricejs(totMens, 'MT', '<?php echo $conf->currency; ?>'));
316 }
317
318 $('[name^="mens"]').on('keyup change', function() {
319 clearTimeout(timeout);
320 var $this = $(this);
321 timeout = setTimeout(() => {
322 var echeance = $this.attr('ech');
323 var mens = $this.val();
324 var amort = $('#amort' + echeance).val();
325 var interet = $('#interets' + echeance).val();
326 calculateMens(echeance, mens, amort, 'mens', 0, interet);
327 }, delay);
328 });
329
330 $('[name^="amort"]').on('keyup change', function() {
331 clearTimeout(timeout);
332 var $this = $(this);
333 timeout = setTimeout(() => {
334 var echeance = $this.attr('ech');
335 var amort = $this.val();
336 var mens = $('#mens' + echeance).val();
337 var interet = $('#interets' + echeance).val();
338 calculateMens(echeance, mens, amort, 'amort', 0, interet);
339 }, delay);
340 });
341
342 $('[name^="interets"]').on('keyup change', function() {
343 clearTimeout(timeout);
344 var $this = $(this);
345 timeout = setTimeout(() => {
346 var echeance = $this.attr('ech');
347 var interet = $this.val();
348 var amort = $('#amort' + echeance).val();
349 var mens = $('#mens' + echeance).val();
350 calculateMens(echeance, mens, amort, 'interet', 0, interet);
351 }, delay);
352 });
353
354 $('#btn_apply_grace_period').on('click', function(e) {
355 e.preventDefault();
356 var months = parseInt($('#grace_period_months').val(), 10);
357 if (isNaN(months) || months < 1) return;
358 var nbterm = <?php echo (int) $object->nbterm; ?>;
359 if (months >= nbterm) months = nbterm - 1;
360
361 calculateMens(1, 0, 0, 'amort', months, 0);
362 });
363
364 function calculateMens(echeance, mens, amort, source, grace_period, interet) {
365 var idcap = echeance - 1;
366 idcap = '#hi_capital' + idcap;
367 var capital = price2numjs($(idcap).val());
368 console.log("calculateMens echeance=" + echeance + " idcap=" + idcap + " capital=" + capital + " source=" + source + " grace=" + grace_period);
369
370 $.ajax({
371 method: "GET",
372 dataType: 'json',
373 url: 'calcmens.php',
374 data: {
375 echeance: echeance,
376 mens: price2numjs(mens),
377 amort: price2numjs(amort),
378 interet: price2numjs(interet),
379 source: source,
380 grace_period: grace_period,
381 capital: capital,
382 rate: <?php echo $object->rate / 100; ?>,
383 nbterm: <?php echo $object->nbterm; ?>,
384 frequency: <?php echo (int) $object->frequency; ?>,
385 interest_basis: <?php echo (int) $object->interest_basis; ?>,
386 datestart: <?php echo (int) $object->datestart; ?>,
387 balloon: <?php echo (float) $object->balloon_amount; ?>,
388 token: '<?php echo currentToken(); ?>'
389 },
390 success: function(data) {
391 $.each(data, function(index, element) {
392 $('#hi_capital' + index).val(element.cap_rest);
393 $('#capital' + index).text(element.cap_rest_str);
394
395 if ($('#interets' + index).is('input')) {
396 $('#interets' + index).val(element.interet);
397 } else {
398 $('#interets' + index).text(element.interet_str);
399 }
400 $('#hi_interets' + index).val(element.interet);
401
402 if ($('#amort' + index).is('input')) {
403 $('#amort' + index).val(element.amort);
404 } else {
405 $('#amort' + index).text(element.amort);
406 }
407 $('#hi_amort' + index).val(element.amort_num);
408
409 if ($('#mens' + index).is('input')) {
410 $('#mens' + index).val(element.mens);
411 } else {
412 $('#mens' + index).text(element.mens);
413 }
414 $('#hi_mens' + index).val(element.mens_num);
415 });
416 updateTotals();
417 }
418 });
419 }
420
421 updateTotals();
422});
423</script>
424<?php
425
426if ($pay_without_schedule == 1) {
427 print '<div class="warning">'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'</div>'."\n";
428}
429
430print '<form name="createecheancier" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
431print '<input type="hidden" name="token" value="'.newToken().'">';
432print '<input type="hidden" name="loanid" value="'.$loanid.'">';
433if (count($echeances->lines) > 0) {
434 print '<input type="hidden" name="action" value="updateecheancier">';
435} else {
436 print '<input type="hidden" name="action" value="createecheancier">';
437}
438
439if (empty($pay_without_schedule) && $permissiontoadd) {
440 print '<div class="marginbottomonly inline-block valignmiddle">';
441 print '<span class="opacitymedium">'.$langs->trans((int) $object->frequency == 12 ? "GracePeriodMonths" : "GracePeriodTerms").': </span>';
442 print '<input type="number" id="grace_period_months" min="1" max="'.max(1, $object->nbterm - 1).'" value="1" class="width50 right"> ';
443 print '<input type="button" id="btn_apply_grace_period" class="button valignmiddle" value="'.$langs->trans("Apply").'">';
444 print '</div>';
445 print '<br><br>';
446}
447
448print '<div class="div-table-responsive-no-min">';
449print '<table class="border centpercent">';
450
451$colspan = 7;
452if (count($echeances->lines) > 0) {
453 $colspan++;
454}
455
456print '<tr class="liste_titre">';
457print '<th class="center">'.$langs->trans("Term").'</th>';
458print '<th class="center">'.$langs->trans("Date").'</th>';
459print '<th class="center">'.loanChargeLabel($object->charge_type, $langs).'</th>';
460print '<th class="center">'.$langs->trans("InterestAmount").'</th>';
461print '<th class="center">'.$langs->trans("CapitalAmortization").'</th>';
462print '<th class="center">'.$langs->trans("Amount").'</th>';
463print '<th class="center">'.$langs->trans("CapitalRemain");
464print '<br>('.price($object->capital, 0, '', 1, -1, -1, $conf->currency).')';
465print '<input type="hidden" name="hi_capital0" id ="hi_capital0" value="'.$object->capital.'">';
466print '</th>';
467if (count($echeances->lines) > 0) {
468 print '<th class="center">'.$langs->trans('DoPayment').'</th>';
469}
470print '</tr>'."\n";
471
472$cap_rest = 0.0;
473$total_insurance = 0.0;
474$total_interest = 0.0;
475$total_amort = 0.0;
476$total_mens = 0.0;
477
478if ($object->nbterm > 0 && count($echeances->lines) == 0) {
479 $i = 1;
480 $capital = $object->capital;
481 $cap_rest = (float) $capital;
482 list($insurance, $regulInsurance) = loanChargePerPayment($object->insurance_amount, $object->charge_per_payment, $object->nbterm);
483
484 while ($i < $object->nbterm + 1) {
485 $mens = price2num($echeances->calcMonthlyPayments($capital, $object->rate / 100, $object->nbterm - $i + 1, $object->frequency, $object->interest_basis, $object->balloon_amount), 'MT');
486 $int = ($capital * loanPeriodRate($object->rate, $object->frequency, $object->interest_basis, ($object->interest_basis ? loanPeriodDays($object->datestart, $i, $object->frequency) : 0))) / 100;
487 $int = price2num($int, 'MT');
488 $amort = price2num((float) $mens - (float) $int, 'MT');
489 $insu = ((float) $insurance + (($i == 1) ? (float) $regulInsurance : 0));
490 $cap_rest = (float) price2num((float) $capital - (float) $amort, 'MT');
491
492 // Adjust rounding difference on last term (with daily interest, periods are not all the same
493 // length, so the last term always settles what is left)
494 // A balloon is paid with the last term, which then repays all the capital left.
495 if ($i == $object->nbterm && ($object->interest_basis || (float) $object->balloon_amount > 0 || abs($cap_rest) <= 0.05) && $capital > 0) {
496 $amort = $capital;
497 $cap_rest = 0.0;
498 $mens = price2num((float) $amort + (float) $int, 'MT');
499 }
500
501 $total_insurance += $insu;
502 $total_interest += $int;
503 $total_amort += $amort;
504 $total_mens += $mens;
505
506 print '<tr>';
507 print '<td class="center" id="n'.$i.'">'.$i.'</td>';
508 print '<td class="center" id ="date'.$i.'"><input type="hidden" name="hi_date'.$i.'" id ="hi_date'.$i.'" value="'.loanTermDate($object->datestart, $i - 1, $object->frequency).'">'.dol_print_date(loanTermDate($object->datestart, $i - 1, $object->frequency), 'day').'</td>';
509 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.'">';
510 print '<td class="center"><input class="width75 right" name="interets'.$i.'" id="interets'.$i.'" value="'.price($int).'" ech="'.$i.'"><input type="hidden" name="hi_interets'.$i.'" id="hi_interets'.$i.'" value="'.$int.'"></td>';
511 print '<td class="center"><input class="width75 right" name="amort'.$i.'" id="amort'.$i.'" value="'.price($amort).'" ech="'.$i.'"><input type="hidden" name="hi_amort'.$i.'" id="hi_amort'.$i.'" value="'.$amort.'"></td>';
512 print '<td class="center"><input class="width75 right" name="mens'.$i.'" id="mens'.$i.'" value="'.price($mens).'" ech="'.$i.'"><input type="hidden" name="hi_mens'.$i.'" id="hi_mens'.$i.'" value="'.$mens.'"></td>';
513 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.'">';
514 print '</tr>'."\n";
515 $i++;
516 $capital = $cap_rest;
517 }
518} elseif (count($echeances->lines) > 0) {
519 $i = 1;
520 $capital = $object->capital;
521 $cap_rest = (float) $capital;
522 list($insurance, $regulInsurance) = loanChargePerPayment($object->insurance_amount, $object->charge_per_payment, $object->nbterm);
523 $printed = false;
524
525 foreach ($echeances->lines as $line) {
526 $mens = $line->amount_capital + $line->amount_interest;
527 $int = $line->amount_interest;
528 $amort = $line->amount_capital;
529 $insu = ((float) $insurance + (($i == 1) ? (float) $regulInsurance : 0));
530 $cap_rest = price2num($capital - $amort, 'MT');
531
532 $total_insurance += $insu;
533 $total_interest += $int;
534 $total_amort += $amort;
535 $total_mens += $mens;
536
537 print '<tr>';
538 print '<td class="center" id="n'.$i.'"><input type="hidden" name="hi_rowid'.$i.'" id ="hi_rowid'.$i.'" value="'.$line->id.'">'.$i.'</td>';
539 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>';
540 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.'">';
541
542 if (empty($line->fk_bank)) {
543 print '<td class="center"><input class="right width75" name="interets'.$i.'" id="interets'.$i.'" value="'.price($int).'" ech="'.$i.'"><input type="hidden" name="hi_interets'.$i.'" id="hi_interets'.$i.'" value="'.$int.'"></td>';
544 print '<td class="center"><input class="right width75" name="amort'.$i.'" id="amort'.$i.'" value="'.price($amort).'" ech="'.$i.'"><input type="hidden" name="hi_amort'.$i.'" id="hi_amort'.$i.'" value="'.$amort.'"></td>';
545 print '<td class="center"><input class="right width75" name="mens'.$i.'" id="mens'.$i.'" value="'.price($mens).'" ech="'.$i.'"><input type="hidden" name="hi_mens'.$i.'" id="hi_mens'.$i.'" value="'.$mens.'"></td>';
546 } else {
547 print '<td class="center amount" id="interets'.$i.'">'.price($int, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="interets'.$i.'" id="interets'.$i.'" value="'.$int.'"><input type="hidden" name="hi_interets'.$i.'" id="hi_interets'.$i.'" value="'.$int.'">';
548 print '<td class="center amount" id="amort'.$i.'">'.price($amort, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="amort'.$i.'" id ="amort'.$i.'" value="'.$amort.'"><input type="hidden" name="hi_amort'.$i.'" id="hi_amort'.$i.'" value="'.$amort.'">';
549 print '<td class="center amount" id="mens'.$i.'">'.price($mens, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="mens'.$i.'" id ="mens'.$i.'" value="'.$mens.'"><input type="hidden" name="hi_mens'.$i.'" id="hi_mens'.$i.'" value="'.$mens.'">';
550 }
551
552 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.'">';
553 print '<td class="center">';
554 if (!empty($line->fk_bank)) {
555 print $langs->trans('Paid');
556 if (!empty($line->fk_payment_loan)) {
557 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>';
558 }
559 } elseif (!$printed) {
560 print '<a class="butAction smallpaddingimp" href="'.DOL_URL_ROOT.'/loan/payment/payment.php?id='.$object->id.'&action=create">'.$langs->trans('DoPayment').'</a>';
561 $printed = true;
562 }
563 print '</td>';
564 print '</tr>'."\n";
565 $i++;
566 $capital = $cap_rest;
567 }
568}
569
570print '<tr class="liste_total">';
571print '<td class="center">'.$langs->trans("Total").'</td>';
572print '<td></td>';
573print '<td class="center amount" id="total_insurance">'.price($total_insurance, 0, '', 1, -1, -1, $conf->currency).'</td>';
574print '<td class="center amount" id="total_interest">'.price($total_interest, 0, '', 1, -1, -1, $conf->currency).'</td>';
575print '<td class="center amount" id="total_amort">'.price($total_amort, 0, '', 1, -1, -1, $conf->currency).'</td>';
576print '<td class="center amount" id="total_mens">'.price($total_mens, 0, '', 1, -1, -1, $conf->currency).'</td>';
577print '<td class="center amount" id="total_capital">'.price(0, 0, '', 1, -1, -1, $conf->currency).'</td>';
578if (count($echeances->lines) > 0) {
579 print '<td></td>';
580}
581print '</tr>'."\n";
582
583print '</table>';
584print '</div>';
585
586if ((float) $object->balloon_amount > 0) {
587 print '<div class="opacitymedium margintoponly">'.$langs->trans("LoanBalloonInLastPayment", price($object->balloon_amount, 0, '', 1, -1, -1, $conf->currency)).'</div>';
588}
589
590print '<br>';
591
592if (count($echeances->lines) == 0) {
593 $label = $langs->trans("Create");
594} else {
595 $label = $langs->trans("Save");
596}
597print '<div class="center"><input type="submit" class="button button-add" value="'.$label.'" '.(($pay_without_schedule == 1) ? 'disabled title="'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'"' : '').'title=""></div>';
598print '</form>';
599
600// Recalculate the unpaid payments from a date (rate change, or keep the repayment / the term)
601if ($permissiontoadd && $object->paid != Loan::STATUS_PAID) {
602 $firstunpaid = 0;
603 foreach ($echeances->lines as $l) {
604 if (empty($l->fk_bank)) {
605 $firstunpaid = (int) $l->datep;
606 break;
607 }
608 }
609 print '<br>';
610 print load_fiche_titre($langs->trans("LoanRecalculate"), '', '');
611 print '<form name="recalculate" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
612 print '<input type="hidden" name="token" value="'.newToken().'">';
613 print '<input type="hidden" name="loanid" value="'.$loanid.'">';
614 print '<input type="hidden" name="action" value="recalculate">';
615 print '<table class="border centpercent">';
616 print '<tr><td class="titlefield">'.$langs->trans("LoanRecalcFrom").'</td><td>'.$form->selectDate($firstunpaid ? $firstunpaid : dol_now(), 'recalcfrom', 0, 0, 0, 'recalculate', 1, 0).'</td></tr>';
617 print '<tr><td>'.$langs->trans("LoanNewRate").'</td><td><input name="newrate" size="5" value="'.dol_escape_htmltag((string) $object->rate).'"> %</td></tr>';
618 if (count($echeances->lines) > 0) {
619 print '<tr><td>'.$langs->trans("LoanRecalcKeep").'</td><td>'.$form->selectarray('keep', array('term' => $langs->trans("LoanRecalcKeepTerm"), 'payment' => $langs->trans("LoanRecalcKeepPayment")), 'term').'</td></tr>';
620 }
621 print '</table>';
622 print '<div class="opacitymedium margintoponly">'.$langs->trans(count($echeances->lines) > 0 ? "LoanRecalcHelp" : "LoanRecalcHelpNoSchedule").'</div>';
623 print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("LoanRecalculateButton").'"></div>';
624 print '</form>';
625}
626
627// History of the changes
628$changes = loanFetchChanges($db, $object->id);
629if (count($changes)) {
630 print '<br>';
631 print load_fiche_titre($langs->trans("LoanChangesHistory"), '', '');
632 print '<div class="div-table-responsive-no-min">';
633 print '<table class="noborder centpercent">';
634 print '<tr class="liste_titre"><th>'.$langs->trans("Date").'</th><th>'.$langs->trans("LoanChangeReason").'</th><th>'.$langs->trans("LoanChangeFrom").'</th>';
635 print '<th class="right">'.$langs->trans("Rate").'</th><th>'.$langs->trans("LoanRecalcKeep").'</th><th class="right">'.$langs->trans("Amount").'</th><th class="right">'.$langs->trans("Nbterms").'</th><th>'.$langs->trans("User").'</th></tr>';
636 foreach ($changes as $c) {
637 $u = new User($db);
638 $u->fetch((int) $c->fk_user_author);
639 print '<tr class="oddeven">';
640 print '<td class="nowraponall">'.dol_print_date($c->datec, 'dayhour').'</td>';
641 print '<td>'.$langs->trans($c->reason == 'payment' ? 'LoanChangeReasonPayment' : 'LoanChangeReasonRate');
642 if (!empty($c->fk_payment_loan)) {
643 print ' <a href="'.DOL_URL_ROOT.'/loan/payment/card.php?id='.((int) $c->fk_payment_loan).'">'.img_object($langs->trans("Payment"), "payment").' '.((int) $c->fk_payment_loan).'</a>';
644 }
645 print '</td>';
646 print '<td class="nowraponall">'.dol_print_date($c->date_change, 'day').'</td>';
647 print '<td class="right nowraponall">'.price($c->rate_old).'% &rarr; '.price($c->rate_new).'%</td>';
648 print '<td>'.($c->keep_mode == 'payment' ? $langs->trans("LoanRecalcKeepPayment") : ($c->keep_mode == 'term' ? $langs->trans("LoanRecalcKeepTerm") : '')).'</td>';
649 print '<td class="right nowraponall">'.($c->keep_mode ? price($c->payment_old, 0, $langs, 1, -1, -1, $conf->currency).' &rarr; '.price($c->payment_new, 0, $langs, 1, -1, -1, $conf->currency) : '').'</td>';
650 print '<td class="right nowraponall">'.((float) $c->nbterm_old != (float) $c->nbterm_new ? ((float) $c->nbterm_old).' &rarr; '.((float) $c->nbterm_new) : ((float) $c->nbterm_new)).'</td>';
651 print '<td>'.($u->id > 0 ? $u->getNomUrl(-1) : '').'</td>';
652 print '</tr>';
653 }
654 print '</table>';
655 print '</div>';
656}
657
658// End of page
659llxFooter();
660$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition propal.php:281
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
$c
Definition line.php:353
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage generation of HTML components Only common components must be here.
Class to manage building of HTML components.
Loan.
Class to manage Schedule of loans.
Class to manage projects.
Class to manage Dolibarr users.
dol_now($mode='gmt')
Return date for now.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTFLOAT($paramname, $rounding='', $option=2)
Return the value of a $_GET or $_POST supervariable, converted into float.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
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.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
Definition html.lib.php:542
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:183
loanFetchChanges($db, $loanid)
History of the changes of a loan, most recent first.
Definition loan.lib.php:553
loan_prepare_head($object)
Prepare array with list of tabs.
Definition loan.lib.php:34
loanChargePerPayment($amount, $per_payment, $nbterm)
Charge of each payment of a loan.
Definition loan.lib.php:342
loanRecordChange($db, $user, $loanid, $reason, $datechange, $rate_old, $rate_new, $keep, $payment_old, $payment_new, $nbterm_old, $nbterm_new, $fk_payment=0)
Record a change of a loan (rate change, or a payment that differs from the schedule) in its history.
Definition loan.lib.php:531
loanRecalculateSchedule($db, $user, $loan, $lines, $from, $capital, $rate, $keep, $payment=0.0)
Recalculate the unpaid payments of a loan schedule, from one payment to the end, after a rate change ...
Definition loan.lib.php:370
loanPeriodRate($rate, $frequency=12, $interest_basis=0, $days=0)
Interest rate of one period of a loan.
Definition loan.lib.php:294
loanTermDate($datestart, $index, $frequency=12)
Date of a payment of a loan.
Definition loan.lib.php:251
loanPeriodDays($datestart, $term, $frequency=12)
Number of days of the period that ends with a payment (used by the daily interest basis).
Definition loan.lib.php:280
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.