Passed
Branch develop (c36c8e)
by
unknown
39:14
created

loanCalcMonthlyPayment()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 4
nop 5
dl 0
loc 35
rs 9.568
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2014-2016	Alexandre Spangaro	<[email protected]>
3
 * Copyright (C) 2015		Frederic France		<[email protected]>
4
 * Copyright (C) 2020       Maxime DEMAREST     <[email protected]>
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
20
/**
21
 *      \file       htdocs/core/lib/loan.lib.php
22
 *      \ingroup    loan
23
 *      \brief      Library for loan module
24
 */
25
26
27
/**
28
 * Prepare array with list of tabs
29
 *
30
 * @param   Object	$object		Object related to tabs
31
 * @return  array				Array of tabs to show
32
 */
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) $head[$tab][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
64
	$head[$tab][2] = 'documents';
65
	$tab++;
66
67
	if (empty($conf->global->MAIN_DISABLE_NOTES_TAB))
68
	{
69
		$nbNote = (empty($object->note_private) ? 0 : 1) + (empty($object->note_public) ? 0 : 1);
70
		$head[$tab][0] = DOL_URL_ROOT."/loan/note.php?id=".$object->id;
71
		$head[$tab][1] = $langs->trans("Notes");
72
		if ($nbNote > 0) $head[$tab][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
73
		$head[$tab][2] = 'note';
74
		$tab++;
75
	}
76
77
    $head[$tab][0] = DOL_URL_ROOT.'/loan/info.php?id='.$object->id;
78
    $head[$tab][1] = $langs->trans("Info");
79
    $head[$tab][2] = 'info';
80
    $tab++;
81
82
    complete_head_from_modules($conf, $langs, $object, $head, $tab, 'loan', 'remove');
83
84
    return $head;
85
}
86
87
/**
88
 * Calculate remaining loan mensuality and interests
89
 *
90
 * @param   int     $mens		Value of this mensuality (interests include, set 0 if we don't paid interests for this mensuality)
91
 * @param   float   $capital    Remaining capital for this mensuality
92
 * @param   float   $rate		Loan rate
93
 * @param   int     $echance	Actual loan term
94
 * @param   int   	$nbterm  	Total number of term for this loan
95
 * @return  array				Array with remaining capital, interest, and mensuality for each remaining terms
96
 */
97
function loanCalcMonthlyPayment($mens, $capital, $rate, $echance, $nbterm)
98
{
99
    global $conf;
100
    require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
101
    $object = new LoanSchedule($db);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $db seems to be never defined.
Loading history...
102
103
    // If mensuality is 0 we don't pay interests and remaining capital not modified
104
    if ($mens == 0)
105
    {
106
        $int = 0;
107
        $cap_rest = $capital;
108
    }
109
    else {
110
        $int = ($capital * ($rate / 12));
111
        $int = round($int, 2, PHP_ROUND_HALF_UP);
112
        $cap_rest = round($capital - ($mens - $int), 2, PHP_ROUND_HALF_UP);
113
    }
114
    $output[$echance] = 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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$output was never initialized. Although not strictly required by PHP, it is generally a good practice to add $output = array(); before regardless.
Loading history...
115
116
    $echance++;
117
    $capital = $cap_rest;
118
    while ($echance <= $nbterm) {
119
        $mens = round($object->calcMonthlyPayments($capital, $rate, $nbterm - $echance + 1), 2, PHP_ROUND_HALF_UP);
120
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[$echance] = 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
        $capital = $cap_rest;
128
        $echance++;
129
    }
130
131
    return $output;
132
}
133