refactor loan schedule

This commit is contained in:
Frédéric France 2024-11-23 18:24:15 +01:00
parent 2b264ae5d0
commit 0df412b6be
No known key found for this signature in database
GPG key ID: CE25B0B7B53B9177
2 changed files with 45 additions and 28 deletions

View file

@ -1,8 +1,8 @@
<?php
/* Copyright (C) 2014-2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2015-2020 Frederic France <frederic.france@netlogic.fr>
* Copyright (C) 2015-2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -98,7 +98,7 @@ function loan_prepare_head($object)
* @param float $rate Loan rate
* @param int $numactualloadterm Actual loan term
* @param int $nbterm Total number of term for this loan
* @return array<array{cap_rest:float,cap_rest_str:string,interet:float,interet_str:string,mens:float}> Array with remaining capital, interest, and mensuality for each remaining terms
* @return array<array{cap_rest:float,cap_rest_str:string,interet:float,interet_str:string,mens:string}> Array with remaining capital, interest, and mensuality for each remaining terms
*/
function loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm)
{
@ -123,7 +123,13 @@ function loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbt
$int = round($int, 2, PHP_ROUND_HALF_UP);
$cap_rest = round((float) $capital - ((float) $mens - $int), 2, PHP_ROUND_HALF_UP);
}
$output[$numactualloadterm] = array('cap_rest' => $cap_rest, 'cap_rest_str' => price($cap_rest, 0, '', 1, -1, -1, $conf->currency), 'interet' => $int, 'interet_str' => price($int, 0, '', 1, -1, -1, $conf->currency), 'mens' => $mens);
$output[$numactualloadterm] = array(
'cap_rest' => $cap_rest,
'cap_rest_str' => price($cap_rest, 0, '', 1, -1, -1, $conf->currency),
'interet' => $int,
'interet_str' => price($int, 0, '', 1, -1, -1, $conf->currency),
'mens' => price($mens),
);
$numactualloadterm++;
$capital = $cap_rest;
@ -139,7 +145,7 @@ function loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbt
'cap_rest_str' => price($cap_rest, 0, '', 1, -1, -1, $conf->currency),
'interet' => $int,
'interet_str' => price($int, 0, '', 1, -1, -1, $conf->currency),
'mens' => $mens,
'mens' => price($mens),
);
$capital = $cap_rest;
$numactualloadterm++;

View file

@ -214,34 +214,45 @@ dol_banner_tab($object, 'loanid', $linkback, 1, 'rowid', 'ref', $morehtmlref, ''
?>
<script type="text/javascript">
$(document).ready(function() {
$('[name^="mens"]').focusout(function() {
var echeance=$(this).attr('ech');
var mens=price2numjs($(this).val());
var idcap=echeance-1;
var timeout = null;
var delay = 750; // 0.75 seconds
$('[name^="mens"]').keyup(function() {
clearTimeout(timeout);
timeout = setTimeout(() => {
var echeance = $(this).attr('ech');
var mens = $(this).val();
calculateMens(echeance, mens);
}, delay);
});
function calculateMens(echeance, mens) {
var table = $('[name^="mens"]');
var idcap = echeance-1;
idcap = '#hi_capital'+idcap;
var capital=price2numjs($(idcap).val());
var capital = price2numjs($(idcap).val());
console.log("Change monthly amount echeance="+echeance+" idcap="+idcap+" capital="+capital);
$.ajax({
method: "GET",
dataType: 'json',
url: 'calcmens.php',
data: { echeance: echeance, mens: mens, capital:capital, rate:<?php echo $object->rate / 100; ?>, nbterm: <?php echo $object->nbterm; ?>, token: '<?php echo currentToken(); ?>' },
success: function(data) {
method: "GET",
dataType: 'json',
url: 'calcmens.php',
data: {
echeance: echeance,
mens: price2numjs(mens),
capital: capital,
rate: <?php echo $object->rate / 100; ?>,
nbterm: <?php echo $object->nbterm; ?>,
token: '<?php echo currentToken(); ?>'
},
success: function(data) {
$.each(data, function(index, element) {
var idcap_res='#hi_capital'+index;
var idcap_res_srt='#capital'+index;
var interet_res='#hi_interets'+index;
var interet_res_str='#interets'+index;
var men_res='#mens'+index;
$(idcap_res).val(element.cap_rest);
$(idcap_res_srt).text(element.cap_rest_str);
$(interet_res).val(element.interet);
$(interet_res_str).text(element.interet_str);
$(men_res).val(element.mens);
$('#hi_capital'+index).val(element.cap_rest);
$('#capital'+index).text(element.cap_rest_str);
$('#hi_interets'+index).val(element.interet);
$('#interets'+index).text(element.interet_str);
$('#mens'+index).val(element.mens);
});
}
});
});
}
});
</script>
<?php
@ -302,7 +313,7 @@ if ($object->nbterm > 0 && count($echeances->lines) == 0) {
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>';
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.'">';
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.'">';
print '<td class="center"><input class="width75 right" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
print '<td class="center"><input class="width75 right" name="mens'.$i.'" id="mens'.$i.'" value="'.price($mens).'" ech="'.$i.'"></td>';
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.'">';
print '</tr>'."\n";
$i++;
@ -327,7 +338,7 @@ if ($object->nbterm > 0 && count($echeances->lines) == 0) {
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.'">';
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.'">';
if (empty($line->fk_bank)) {
print '<td class="center"><input class="right width75" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
print '<td class="center"><input class="right width75" name="mens'.$i.'" id="mens'.$i.'" value="'.price($mens).'" ech="'.$i.'"></td>';
} else {
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.'">';
}