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 *
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
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', 'add', 'core');
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 (!getDolGlobalString('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', 'add', 'external');
86
87 complete_head_from_modules($conf, $langs, $object, $head, $tab, 'loan', 'remove');
88
89 return $head;
90}
91
102function loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm)
103{
104 global $conf, $db;
105 require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
106 $object = new LoanSchedule($db);
107 $output = array();
108
109 // Sanitize data in case of
110 $mens = price2num($mens);
111 $capital = price2num($capital);
112 $rate = price2num($rate);
113 $numactualloadterm = ((int) $numactualloadterm);
114 $nbterm = ((int) $nbterm);
115
116 // If mensuality is 0 we don't pay interests and remaining capital not modified
117 if ($mens == 0) {
118 $int = 0;
119 $cap_rest = $capital;
120 } else {
121 $int = ($capital * ($rate / 12));
122 $int = round($int, 2, PHP_ROUND_HALF_UP);
123 $cap_rest = round($capital - ($mens - $int), 2, PHP_ROUND_HALF_UP);
124 }
125 $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);
126
127 $numactualloadterm++;
128 $capital = $cap_rest;
129 while ($numactualloadterm <= $nbterm) {
130 $mens = round($object->calcMonthlyPayments($capital, $rate, $nbterm - $numactualloadterm + 1), 2, PHP_ROUND_HALF_UP);
131
132 $int = ($capital * ($rate / 12));
133 $int = round($int, 2, PHP_ROUND_HALF_UP);
134 $cap_rest = round($capital - ($mens - $int), 2, PHP_ROUND_HALF_UP);
135
136 $output[$numactualloadterm] = array(
137 'cap_rest' => $cap_rest,
138 'cap_rest_str' => price($cap_rest, 0, '', 1, -1, -1, $conf->currency),
139 'interet' => $int,
140 'interet_str' => price($int, 0, '', 1, -1, -1, $conf->currency),
141 'mens' => $mens,
142 );
143
144 $capital = $cap_rest;
145 $numactualloadterm++;
146 }
147
148 return $output;
149}
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 dolibarr global constant string value.
loan_prepare_head($object)
Prepare array with list of tabs.
Definition loan.lib.php:33
loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm)
Calculate remaining loan mensuality and interests.
Definition loan.lib.php:102