dolibarr 25.0.0-alpha
loan.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014-2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
3 * Copyright (C) 2015-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
5 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
35{
36 global $db, $langs, $conf;
37
38 $tab = 0;
39 $head = array();
40
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';
44 $tab++;
45
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';
49 $tab++;
50
51 // Show more tabs from modules
52 // Entries must be declared in modules descriptor with line
53 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
54 // $this->tabs = array('entity:-tabname); to remove a tab
55 complete_head_from_modules($conf, $langs, $object, $head, $tab, 'loan', 'add', 'core');
56
57 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
58 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
59 $upload_dir = $conf->loan->dir_output."/".dol_sanitizeFileName($object->ref);
60 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
61 $nbLinks = Link::count($db, $object->element, $object->id);
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>';
66 }
67 $head[$tab][2] = 'documents';
68 $tab++;
69
70 if (!getDolGlobalString('MAIN_DISABLE_NOTES_TAB')) {
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");
74 if ($nbNote > 0) {
75 $head[$tab][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
76 }
77 $head[$tab][2] = 'note';
78 $tab++;
79 }
80
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';
84 $tab++;
85
86 complete_head_from_modules($conf, $langs, $object, $head, $tab, 'loan', 'add', 'external');
87
88 complete_head_from_modules($conf, $langs, $object, $head, $tab, 'loan', 'remove');
89
90 return $head;
91}
92
107function loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm, $amort = null, $source = 'mens', $grace_period = 0, $int = null)
108{
109 global $conf, $db;
110 require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
111 $object = new LoanSchedule($db);
112 $output = array();
113
114 // Sanitize data in case of
115 $mens = (float) price2num($mens);
116 $capital = (float) price2num($capital);
117 $rate = (float) price2num($rate);
118 $numactualloadterm = ((int) $numactualloadterm);
119 $nbterm = ((int) $nbterm);
120 $grace_period = ((int) $grace_period);
121 $amort = ($amort !== null && $amort !== '') ? (float) price2num($amort) : null;
122 $int = ($int !== null && $int !== '') ? (float) price2num($int) : null;
123
124 if ($grace_period > 0 && $numactualloadterm <= $grace_period) {
125 $amort = 0.0;
126 $int = ($int !== null && $int > 0) ? $int : round((float) $capital * ((float) $rate / 12), 2, PHP_ROUND_HALF_UP);
127 $mens = $int;
128 $cap_rest = $capital;
129 } elseif ($source === 'amort') {
130 $amort = ($amort !== null) ? $amort : 0.0;
131 $int = ($int !== null && $int > 0) ? $int : round((float) $capital * ((float) $rate / 12), 2, PHP_ROUND_HALF_UP);
132 $mens = round((float) $amort + (float) $int, 2, PHP_ROUND_HALF_UP);
133 $cap_rest = round((float) $capital - (float) $amort, 2, PHP_ROUND_HALF_UP);
134 } elseif ($source === 'interet') {
135 $int = ($int !== null) ? $int : round((float) $capital * ((float) $rate / 12), 2, PHP_ROUND_HALF_UP);
136 $amort = ($amort !== null) ? $amort : 0.0;
137 $mens = round((float) $amort + (float) $int, 2, PHP_ROUND_HALF_UP);
138 $cap_rest = round((float) $capital - (float) $amort, 2, PHP_ROUND_HALF_UP);
139 } else {
140 // source === 'mens'
141 if ($amort !== null && $amort == 0) {
142 // Zero capital amortization: payment is interest/fees and capital stays intact
143 $amort = 0.0;
144 $int = $mens;
145 $cap_rest = $capital;
146 } else {
147 $int = ($int !== null && $int > 0) ? $int : round((float) $capital * ((float) $rate / 12), 2, PHP_ROUND_HALF_UP);
148 $amort = round((float) $mens - (float) $int, 2, PHP_ROUND_HALF_UP);
149 if ($amort < 0) {
150 $amort = 0.0;
151 $int = $mens;
152 $cap_rest = $capital;
153 } else {
154 $cap_rest = round((float) $capital - (float) $amort, 2, PHP_ROUND_HALF_UP);
155 }
156 }
157 }
158
159 $output[$numactualloadterm] = array(
160 'cap_rest' => $cap_rest,
161 'cap_rest_str' => price($cap_rest, 0, '', 1, -1, -1, $conf->currency),
162 'interet' => $int,
163 'interet_str' => price($int, 0, '', 1, -1, -1, $conf->currency),
164 'amort' => price($amort),
165 'amort_num' => (float) $amort,
166 'mens' => price($mens),
167 'mens_num' => (float) $mens,
168 );
169
170 $numactualloadterm++;
171 $capital = $cap_rest;
172 while ($numactualloadterm <= $nbterm) {
173 if ($grace_period > 0 && $numactualloadterm <= $grace_period) {
174 $amort = 0.0;
175 $int = ((float) $capital * ((float) $rate / 12));
176 $int = round($int, 2, PHP_ROUND_HALF_UP);
177 $mens = $int;
178 $cap_rest = $capital;
179 } else {
180 $mens = round($object->calcMonthlyPayments($capital, (float) $rate, $nbterm - $numactualloadterm + 1), 2, PHP_ROUND_HALF_UP);
181
182 $int = ($capital * ((float) $rate / 12));
183 $int = round($int, 2, PHP_ROUND_HALF_UP);
184 $amort = round($mens - $int, 2, PHP_ROUND_HALF_UP);
185 $cap_rest = round($capital - $amort, 2, PHP_ROUND_HALF_UP);
186
187 // Adjust rounding difference on the last installment if small remainder
188 if ($numactualloadterm == $nbterm && abs($cap_rest) <= 0.05 && $capital > 0) {
189 $amort = $capital;
190 $cap_rest = 0.0;
191 $mens = round($amort + $int, 2, PHP_ROUND_HALF_UP);
192 }
193 }
194
195 $output[$numactualloadterm] = array(
196 'cap_rest' => $cap_rest,
197 'cap_rest_str' => price($cap_rest, 0, '', 1, -1, -1, $conf->currency),
198 'interet' => $int,
199 'interet_str' => price($int, 0, '', 1, -1, -1, $conf->currency),
200 'amort' => price($amort),
201 'amort_num' => (float) $amort,
202 'mens' => price($mens),
203 'mens_num' => (float) $mens,
204 );
205 $capital = $cap_rest;
206 $numactualloadterm++;
207 }
208
209 return $output;
210}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage Schedule of loans.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.
Definition files.lib.php:65
price2num($amount, $rounding='', $option=0)
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.
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.
loan_prepare_head($object)
Prepare array with list of tabs.
Definition loan.lib.php:34
loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm, $amort=null, $source='mens', $grace_period=0, $int=null)
Calculate remaining loan mensuality and interests.
Definition loan.lib.php:107