36 global $db, $langs,
$conf;
41 $head[$tab][0] = DOL_URL_ROOT.
'/loan/card.php?id='.
$object->id;
42 $head[$tab][1] = $langs->trans(
'Card');
43 $head[$tab][2] =
'card';
46 $head[$tab][0] = DOL_URL_ROOT.
'/loan/schedule.php?loanid='.
$object->id;
47 $head[$tab][1] = $langs->trans(
'FinancialCommitment');
48 $head[$tab][2] =
'FinancialCommitment';
57 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
58 require_once DOL_DOCUMENT_ROOT.
'/core/class/link.class.php';
60 $nbFiles = count(
dol_dir_list($upload_dir,
'files', 0,
'',
'(\.meta|_preview.*\.png)$'));
62 $head[$tab][0] = DOL_URL_ROOT.
'/loan/document.php?id='.
$object->id;
63 $head[$tab][1] = $langs->trans(
"Documents");
64 if (($nbFiles + $nbLinks) > 0) {
65 $head[$tab][1] .=
'<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).
'</span>';
67 $head[$tab][2] =
'documents';
71 $nbNote = (empty(
$object->note_private) ? 0 : 1) + (empty(
$object->note_public) ? 0 : 1);
72 $head[$tab][0] = DOL_URL_ROOT.
"/loan/note.php?id=".
$object->id;
73 $head[$tab][1] = $langs->trans(
"Notes");
75 $head[$tab][1] .=
'<span class="badge marginleftonlyshort">'.$nbNote.
'</span>';
77 $head[$tab][2] =
'note';
81 $head[$tab][0] = DOL_URL_ROOT.
'/loan/info.php?id='.
$object->id;
82 $head[$tab][1] = $langs->trans(
"Info");
83 $head[$tab][2] =
'info';
111function loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm, $amort =
null, $source =
'mens', $grace_period = 0, $int =
null, $frequency = 12, $interest_basis = 0, $datestart = 0, $balloon = 0)
114 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loanschedule.class.php';
122 $numactualloadterm = ((int) $numactualloadterm);
123 $nbterm = ((int) $nbterm);
124 $grace_period = ((int) $grace_period);
125 $amort = ($amort !==
null && $amort !==
'') ? (
float)
price2num($amort) :
null;
126 $int = ($int !==
null && $int !==
'') ? (
float)
price2num($int) :
null;
127 $frequency = ((int) $frequency > 0 ? (
int) $frequency : 12);
128 $interest_basis = (int) $interest_basis;
129 $datestart = (int) $datestart;
131 $periodrate =
function (
int $term) use ($rate, $frequency, $interest_basis, $datestart):
float {
132 return loanPeriodRate((
float) $rate, $frequency, $interest_basis, ($interest_basis && $datestart ?
loanPeriodDays($datestart, $term, $frequency) : 0));
135 if ($grace_period > 0 && $numactualloadterm <= $grace_period) {
137 $int = ($int !==
null && $int > 0) ? $int : round((
float) $capital * $periodrate($numactualloadterm), 2, PHP_ROUND_HALF_UP);
139 $cap_rest = $capital;
140 } elseif ($source ===
'amort') {
141 $amort = ($amort !==
null) ? $amort : 0.0;
142 $int = ($int !==
null && $int > 0) ? $int : round((
float) $capital * $periodrate($numactualloadterm), 2, PHP_ROUND_HALF_UP);
143 $mens = round((
float) $amort + (
float) $int, 2, PHP_ROUND_HALF_UP);
144 $cap_rest = round((
float) $capital - (
float) $amort, 2, PHP_ROUND_HALF_UP);
145 } elseif ($source ===
'interet') {
146 $int = ($int !==
null) ? $int : round((
float) $capital * $periodrate($numactualloadterm), 2, PHP_ROUND_HALF_UP);
147 $amort = ($amort !==
null) ? $amort : 0.0;
148 $mens = round((
float) $amort + (
float) $int, 2, PHP_ROUND_HALF_UP);
149 $cap_rest = round((
float) $capital - (
float) $amort, 2, PHP_ROUND_HALF_UP);
152 if ($amort !==
null && $amort == 0) {
156 $cap_rest = $capital;
158 $int = ($int !==
null && $int > 0) ? $int : round((
float) $capital * $periodrate($numactualloadterm), 2, PHP_ROUND_HALF_UP);
159 $amort = round((
float) $mens - (
float) $int, 2, PHP_ROUND_HALF_UP);
163 $cap_rest = $capital;
165 $cap_rest = round((
float) $capital - (
float) $amort, 2, PHP_ROUND_HALF_UP);
170 $output[$numactualloadterm] = array(
171 'cap_rest' => $cap_rest,
172 'cap_rest_str' =>
price($cap_rest, 0,
'', 1, -1, -1,
$conf->currency),
174 'interet_str' =>
price($int, 0,
'', 1, -1, -1,
$conf->currency),
175 'amort' =>
price($amort),
176 'amort_num' => (
float) $amort,
177 'mens' =>
price($mens),
178 'mens_num' => (
float) $mens,
181 $numactualloadterm++;
182 $capital = $cap_rest;
183 while ($numactualloadterm <= $nbterm) {
184 if ($grace_period > 0 && $numactualloadterm <= $grace_period) {
186 $int = ((float) $capital * $periodrate($numactualloadterm));
187 $int = round($int, 2, PHP_ROUND_HALF_UP);
189 $cap_rest = $capital;
191 $mens = round(
$object->calcMonthlyPayments($capital, (
float) $rate, $nbterm - $numactualloadterm + 1, $frequency, $interest_basis, (
float) $balloon), 2, PHP_ROUND_HALF_UP);
193 $int = ($capital * $periodrate($numactualloadterm));
194 $int = round($int, 2, PHP_ROUND_HALF_UP);
195 $amort = round($mens - $int, 2, PHP_ROUND_HALF_UP);
196 $cap_rest = round($capital - $amort, 2, PHP_ROUND_HALF_UP);
201 if ($numactualloadterm == $nbterm && ($interest_basis || (
float) $balloon > 0 || abs($cap_rest) <= 0.05) && $capital > 0) {
204 $mens = round($amort + $int, 2, PHP_ROUND_HALF_UP);
208 $output[$numactualloadterm] = array(
209 'cap_rest' => $cap_rest,
210 'cap_rest_str' =>
price($cap_rest, 0,
'', 1, -1, -1,
$conf->currency),
212 'interet_str' =>
price($int, 0,
'', 1, -1, -1,
$conf->currency),
213 'amort' =>
price($amort),
214 'amort_num' => (
float) $amort,
215 'mens' =>
price($mens),
216 'mens_num' => (
float) $mens,
218 $capital = $cap_rest;
219 $numactualloadterm++;
234 52 =>
'LoanFrequencyWeekly',
235 26 =>
'LoanFrequencyFortnightly',
236 12 =>
'LoanFrequencyMonthly',
237 4 =>
'LoanFrequencyQuarterly',
238 2 =>
'LoanFrequencyHalfYearly',
239 1 =>
'LoanFrequencyYearly',
253 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
255 $frequency = (int) $frequency;
256 if ($frequency == 52) {
259 if ($frequency == 26) {
262 $months = (($frequency == 4 || $frequency == 2 || $frequency == 1) ? (
int) (12 / $frequency) : 1) * $index;
282 return (
int) round((
loanTermDate($datestart, $term - 1, $frequency) -
loanTermDate($datestart, $term - 2, $frequency)) / 86400);
296 $frequency = ((int) $frequency > 0 ? (
int) $frequency : 12);
297 if ($interest_basis) {
299 $days = ($frequency == 52 ? 7 : ($frequency == 26 ? 14 : 365 / $frequency));
301 return $rate * $days / 365;
303 return $rate / $frequency;
316 1 =>
'LoanChargeAccountFee',
317 2 =>
'LoanChargeOtherFee',
331 return $outputlangs->trans($types[(
int) $charge_type] ?? $types[0]);
345 return array((
float)
price2num($amount,
'MT'), 0.0);
347 if ((
float) $nbterm <= 0) {
348 return array(0.0, 0.0);
350 $each = (float)
price2num((
float) $amount / $nbterm,
'MT');
351 return array($each, (
float)
price2num((
float) $amount - ($each * $nbterm)));
373 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loanschedule.class.php';
375 $lines = array_values($lines);
376 $total = count($lines);
377 $res = array(
'error' =>
'',
'payment_old' => 0.0,
'payment_new' => 0.0,
'nbterm_old' => $total,
'nbterm_new' => $total);
378 if ($from < 0 || $from >= $total) {
379 $res[
'error'] =
'LoanRecalcNothingToRecalculate';
382 for ($i = $from; $i < $total; $i++) {
383 if (!empty($lines[$i]->fk_bank)) {
384 $res[
'error'] =
'LoanRecalcLaterPaymentPaid';
389 $frequency = ((int) $loan->frequency > 0 ? (
int) $loan->frequency : 12);
390 $interest_basis = (int) $loan->interest_basis;
391 $balloon = (float) $loan->balloon_amount;
392 $datestart = (int) $loan->datestart;
393 $lastdate = (int) $lines[$total - 1]->datep;
396 $rateof =
function (
int $index) use ($rate, $history, $loan, $lines, $total, $lastdate, $frequency):
float {
397 if ($rate !==
null) {
398 return (
float) $rate;
400 $date = ($index < $total ? (int) $lines[$index]->datep :
loanTermDate($lastdate, $index - ($total - 1), $frequency));
401 return loanRateAt($history, (
float) $loan->rate, $date);
404 $periodrate =
function (
int $index) use ($rateof, $frequency, $interest_basis, $datestart):
float {
405 return loanPeriodRate($rateof($index), $frequency, $interest_basis, ($interest_basis ?
loanPeriodDays($datestart, $index + 1, $frequency) : 0)) / 100;
408 $res[
'payment_old'] = (float)
price2num((
float) $lines[$from]->amount_capital + (float) $lines[$from]->amount_interest,
'MT');
411 $cap = (float) $capital;
413 if ($keep ===
'payment') {
416 $regular = (float)
price2num((
float) $lines[max(0, $total - 2)]->amount_capital + (float) $lines[max(0, $total - 2)]->amount_interest,
'MT');
418 while ($cap > 0.005) {
420 $pmt = (float)
price2num($payment,
'MT');
421 } elseif ($i < $total - 1) {
422 $pmt = (float)
price2num((
float) $lines[$i]->amount_capital + (float) $lines[$i]->amount_interest,
'MT');
427 $res[
'payment_new'] = $pmt;
429 if ($i - $from >= 1200) {
430 $res[
'error'] =
'LoanRecalcPaymentTooSmall';
433 $int = (float)
price2num($cap * $periodrate($i),
'MT');
434 if ($pmt - $int <= 0) {
435 $res[
'error'] =
'LoanRecalcPaymentTooSmall';
438 $amort = (float)
price2num($pmt - $int,
'MT');
439 if ($cap - $amort <= $balloon + 0.005) {
442 $new[$i] = array($amort, $int);
443 $cap = (float)
price2num($cap - $amort,
'MT');
448 for ($k = 0; $k < $n; $k++) {
450 $mens = (float)
price2num($calc->calcMonthlyPayments($cap, $rateof($i) / 100, $n - $k, $frequency, $interest_basis, $balloon),
'MT');
451 $int = (float)
price2num($cap * $periodrate($i),
'MT');
452 $amort = (float)
price2num($mens - $int,
'MT');
456 $new[$i] = array($amort, $int);
457 $cap = (float)
price2num($cap - $amort,
'MT');
459 $res[
'payment_new'] = $mens;
465 list($charge, $chargeregul) =
loanChargePerPayment($loan->insurance_amount, $loan->charge_per_payment, $loan->nbterm);
466 $enddate = $lastdate;
467 foreach ($new as $i => $amounts) {
472 $line->fk_loan = $loan->id;
474 $line->datep =
loanTermDate($lastdate, $i - ($total - 1), $frequency);
475 $line->amount_insurance = $charge;
476 $line->fk_typepayment = (int) $lines[$total - 1]->fk_typepayment;
478 $line->fk_user_creat = $user->id;
480 $line->amount_capital = $amounts[0];
481 $line->amount_interest = $amounts[1];
483 $line->fk_user_modif = $user->id;
484 $result = ($i < $total ? $line->update($user, 0) : $line->create($user));
486 $res[
'error'] = ($line->error ? $line->error :
'Error');
489 $enddate = (int) $line->datep;
491 $nbnew = $from + count($new);
492 for ($i = $nbnew; $i < $total; $i++) {
493 if ($lines[$i]->
delete($user) < 0) {
494 $res[
'error'] = ($lines[$i]->error ? $lines[$i]->error :
'Error');
499 if ($rate !==
null) {
500 $loan->rate = (float) $rate;
502 $loan->nbterm = $nbnew;
503 $loan->dateend = $enddate;
504 if ($loan->update($user) < 0) {
505 $res[
'error'] = ($loan->error ? $loan->error :
'Error');
508 $res[
'nbterm_new'] = $nbnew;
531function loanRecordChange($db, $user, $loanid, $reason, $datechange, $rate_old, $rate_new, $keep, $payment_old, $payment_new, $nbterm_old, $nbterm_new, $fk_payment = 0)
535 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"loan_change (entity, fk_loan, datec, date_change, reason, rate_old, rate_new, keep_mode, payment_old, payment_new, nbterm_old, nbterm_new, fk_payment_loan, fk_user_author)";
536 $sql .=
" VALUES (".((int)
$conf->entity).
", ".((int) $loanid).
", '".$db->idate(
dol_now()).
"', '".$db->idate($datechange).
"',";
537 $sql .=
" '".$db->escape($reason).
"', ".((float) $rate_old).
", ".((float) $rate_new).
", '".$db->escape($keep).
"',";
538 $sql .=
" ".((float)
price2num($payment_old,
'MT')).
", ".((
float)
price2num($payment_new,
'MT')).
", ".((
float) $nbterm_old).
", ".((
float) $nbterm_new).
",";
539 $sql .=
" ".($fk_payment > 0 ? ((int) $fk_payment) :
"NULL").
", ".((
int) $user->id).
")";
540 if (!$db->query($sql)) {
543 return $db->last_insert_id(MAIN_DB_PREFIX.
"loan_change");
556 $sql =
"SELECT rowid, datec, date_change, reason, rate_old, rate_new, keep_mode, payment_old, payment_new, nbterm_old, nbterm_new, fk_payment_loan, fk_user_author";
557 $sql .=
" FROM ".MAIN_DB_PREFIX.
"loan_change WHERE fk_loan = ".((int) $loanid).
" ORDER BY datec DESC, rowid DESC";
558 $resql = $db->query($sql);
559 while ($resql && ($obj = $db->fetch_object($resql))) {
560 $obj->datec = $db->jdate($obj->datec);
561 $obj->date_change = $db->jdate($obj->date_change);
581 foreach ($history as
$c) {
582 if (
$c->reason !=
'rate') {
586 if ($found ===
null && (
int)
$c->date_change <= (
int) $date) {
590 if ($found !==
null) {
591 return (
float) $found->rate_new;
593 return ($first !==
null ? (
float) $first->rate_old : (float) $current);
608 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loanschedule.class.php';
610 $frequency = ((int) $loan->frequency > 0 ? (
int) $loan->frequency : 12);
611 $datestart = (int) $loan->datestart;
612 $nbterm = (int) $loan->nbterm;
617 $sql =
"SELECT amount_capital, datep FROM ".MAIN_DB_PREFIX.
"payment_loan WHERE fk_loan = ".((int) $loan->id);
618 $resql = $db->query($sql);
619 while ($resql && ($obj = $db->fetch_object($resql))) {
620 $paidcapital += (float) $obj->amount_capital;
621 $lastdate = max($lastdate, (
int) $db->jdate($obj->datep));
623 $capital = (float)
price2num((
float) $loan->capital - $paidcapital,
'MT');
624 if ($capital <= 0 || empty($datestart) || $nbterm <= 0) {
631 while ($index < $nbterm - 1 &&
loanTermDate($datestart, $index, $frequency) <= $lastdate) {
638 $days = ((int) $loan->interest_basis ?
loanPeriodDays($datestart, $index + 1, $frequency) : 0);
639 $interest = (float)
price2num($capital *
loanPeriodRate($rate, $frequency, (
int) $loan->interest_basis, $days) / 100,
'MT');
641 $left = $nbterm - $index;
646 $mens = (float)
price2num($calc->calcMonthlyPayments($capital, $rate / 100, $left, $frequency, (
int) $loan->interest_basis, (
float) $loan->balloon_amount),
'MT');
647 $amort = (float)
price2num($mens - $interest,
'MT');
650 list($charge, $chargeregul) =
loanChargePerPayment($loan->insurance_amount, $loan->charge_per_payment, $nbterm);
652 $charge = (float)
price2num($charge + $chargeregul,
'MT');
655 return array(
'date' => $date,
'term' => $index + 1,
'capital' => $amort,
'interest' => $interest,
'charge' => $charge);
if(! $sortfield) if(! $sortorder) $object
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
static count($dbs, $objecttype, $objectid)
Return nb of links.
Class to manage Schedule of loans.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0, $tz='')
Add a delay to a date.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
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=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add', $filterorigmodule='')
Complete or removed entries into a head array (used to build tabs).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
loanFetchChanges($db, $loanid)
History of the changes of a loan, most recent first.
loanNextPayment($db, $loan)
Next payment of a loan that has no schedule, worked out from the loan and the payments already made: ...
loan_prepare_head($object)
Prepare array with list of tabs.
loanChargeLabel($charge_type, $outputlangs)
Label of the charge of a loan (Insurance, Account-keeping fee, Other fee).
loanChargePerPayment($amount, $per_payment, $nbterm)
Charge of each payment of a loan.
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.
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 ...
loanPeriodRate($rate, $frequency=12, $interest_basis=0, $days=0)
Interest rate of one period of a loan.
loanFrequencies()
Payment frequencies of a loan.
loanTermDate($datestart, $index, $frequency=12)
Date of a payment of a loan.
loanChargeTypes()
Kinds of charge the "insurance" amount of a loan can be.
loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm, $amort=null, $source='mens', $grace_period=0, $int=null, $frequency=12, $interest_basis=0, $datestart=0, $balloon=0)
Calculate remaining loan mensuality and interests.
loanPeriodDays($datestart, $term, $frequency=12)
Number of days of the period that ends with a payment (used by the daily interest basis).
loanRateAt($history, $current, $date)
Annual rate of a loan in force on a date, from the history of its rate changes: the rate of the most ...