dolibarr 21.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-2020 Frederic France <frederic.france@netlogic.fr>
4 * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
5 * Copyright (C) 2024 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
103function loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm)
104{
105 global $conf, $db;
106 require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
107 $object = new LoanSchedule($db);
108 $output = array();
109
110 // Sanitize data in case of
111 $mens = price2num($mens);
112 $capital = price2num($capital);
113 $rate = price2num($rate);
114 $numactualloadterm = ((int) $numactualloadterm);
115 $nbterm = ((int) $nbterm);
116
117 // If mensuality is 0 we don't pay interests and remaining capital not modified
118 if ($mens == 0) {
119 $int = 0;
120 $cap_rest = $capital;
121 } else {
122 $int = ((float) $capital * ((float) $rate / 12));
123 $int = round($int, 2, PHP_ROUND_HALF_UP);
124 $cap_rest = round((float) $capital - ((float) $mens - $int), 2, PHP_ROUND_HALF_UP);
125 }
126 $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);
127
128 $numactualloadterm++;
129 $capital = $cap_rest;
130 while ($numactualloadterm <= $nbterm) {
131 $mens = round($object->calcMonthlyPayments($capital, $rate, $nbterm - $numactualloadterm + 1), 2, PHP_ROUND_HALF_UP);
132
133 $int = ($capital * ((float) $rate / 12));
134 $int = round($int, 2, PHP_ROUND_HALF_UP);
135 $cap_rest = round($capital - ($mens - $int), 2, PHP_ROUND_HALF_UP);
136
137 $output[$numactualloadterm] = array(
138 'cap_rest' => $cap_rest,
139 'cap_rest_str' => price($cap_rest, 0, '', 1, -1, -1, $conf->currency),
140 'interet' => $int,
141 'interet_str' => price($int, 0, '', 1, -1, -1, $conf->currency),
142 'mens' => $mens,
143 );
144
145 $capital = $cap_rest;
146 $numactualloadterm++;
147 }
148
149 return $output;
150}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage Schedule of loans.
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:63
price2num($amount, $rounding='', $option=0)
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.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
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)
Calculate remaining loan mensuality and interests.
Definition loan.lib.php:103