dolibarr  16.0.5
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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
33 function loan_prepare_head($object)
34 {
35  global $db, $langs, $conf;
36 
37  $tab = 0;
38  $head = array();
39 
40  $head[$tab][0] = DOL_URL_ROOT.'/loan/card.php?id='.$object->id;
41  $head[$tab][1] = $langs->trans('Card');
42  $head[$tab][2] = 'card';
43  $tab++;
44 
45  $head[$tab][0] = DOL_URL_ROOT.'/loan/schedule.php?loanid='.$object->id;
46  $head[$tab][1] = $langs->trans('FinancialCommitment');
47  $head[$tab][2] = 'FinancialCommitment';
48  $tab++;
49 
50  // Show more tabs from modules
51  // Entries must be declared in modules descriptor with line
52  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
53  // $this->tabs = array('entity:-tabname); to remove a tab
54  complete_head_from_modules($conf, $langs, $object, $head, $tab, 'loan');
55 
56  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
57  require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
58  $upload_dir = $conf->loan->dir_output."/".dol_sanitizeFileName($object->ref);
59  $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
60  $nbLinks = Link::count($db, $object->element, $object->id);
61  $head[$tab][0] = DOL_URL_ROOT.'/loan/document.php?id='.$object->id;
62  $head[$tab][1] = $langs->trans("Documents");
63  if (($nbFiles + $nbLinks) > 0) {
64  $head[$tab][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
65  }
66  $head[$tab][2] = 'documents';
67  $tab++;
68 
69  if (empty($conf->global->MAIN_DISABLE_NOTES_TAB)) {
70  $nbNote = (empty($object->note_private) ? 0 : 1) + (empty($object->note_public) ? 0 : 1);
71  $head[$tab][0] = DOL_URL_ROOT."/loan/note.php?id=".$object->id;
72  $head[$tab][1] = $langs->trans("Notes");
73  if ($nbNote > 0) {
74  $head[$tab][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
75  }
76  $head[$tab][2] = 'note';
77  $tab++;
78  }
79 
80  $head[$tab][0] = DOL_URL_ROOT.'/loan/info.php?id='.$object->id;
81  $head[$tab][1] = $langs->trans("Info");
82  $head[$tab][2] = 'info';
83  $tab++;
84 
85  complete_head_from_modules($conf, $langs, $object, $head, $tab, 'loan', 'remove');
86 
87  return $head;
88 }
89 
100 function loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm)
101 {
102  global $conf, $db;
103  require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
104  $object = new LoanSchedule($db);
105  $output = array();
106 
107  // Sanitize data in case of
108  $mens = price2num($mens);
109  $capital = price2num($capital);
110  $rate = price2num($rate);
111  $numactualloadterm = ((int) $numactualloadterm);
112  $nbterm = ((int) $nbterm);
113 
114  // If mensuality is 0 we don't pay interests and remaining capital not modified
115  if ($mens == 0) {
116  $int = 0;
117  $cap_rest = $capital;
118  } else {
119  $int = ($capital * ($rate / 12));
120  $int = round($int, 2, PHP_ROUND_HALF_UP);
121  $cap_rest = round($capital - ($mens - $int), 2, PHP_ROUND_HALF_UP);
122  }
123  $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);
124 
125  $numactualloadterm++;
126  $capital = $cap_rest;
127  while ($numactualloadterm <= $nbterm) {
128  $mens = round($object->calcMonthlyPayments($capital, $rate, $nbterm - $numactualloadterm + 1), 2, PHP_ROUND_HALF_UP);
129 
130  $int = ($capital * ($rate / 12));
131  $int = round($int, 2, PHP_ROUND_HALF_UP);
132  $cap_rest = round($capital - ($mens - $int), 2, PHP_ROUND_HALF_UP);
133 
134  $output[$numactualloadterm] = array(
135  'cap_rest' => $cap_rest,
136  'cap_rest_str' => price($cap_rest, 0, '', 1, -1, -1, $conf->currency),
137  'interet' => $int,
138  'interet_str' => price($int, 0, '', 1, -1, -1, $conf->currency),
139  'mens' => $mens,
140  );
141 
142  $capital = $cap_rest;
143  $numactualloadterm++;
144  }
145 
146  return $output;
147 }
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
loanCalcMonthlyPayment
loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm)
Calculate remaining loan mensuality and interests.
Definition: loan.lib.php:100
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
complete_head_from_modules
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add')
Complete or removed entries into a head array (used to build tabs).
Definition: functions.lib.php:9038
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
loan_prepare_head
loan_prepare_head($object)
Prepare array with list of tabs.
Definition: loan.lib.php:33
LoanSchedule
Class to manage Schedule of loans.
Definition: loanschedule.class.php:31
price
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.
Definition: functions.lib.php:5541