Failed Conditions
Push — master ( bf4629...7712d5 )
by Adrien
27:59 queued 18:08
created

Amortization   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Test Coverage

Coverage 97.67%

Importance

Changes 0
Metric Value
wmc 20
eloc 86
c 0
b 0
f 0
dl 0
loc 201
ccs 84
cts 86
cp 0.9767
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAmortizationCoefficient() 0 19 4
B AMORLINC() 0 62 10
B AMORDEGRC() 0 59 6
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
6
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
7
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
8
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
9
10
class Amortization
11
{
12
    /**
13
     * AMORDEGRC.
14
     *
15
     * Returns the depreciation for each accounting period.
16
     * This function is provided for the French accounting system. If an asset is purchased in
17
     * the middle of the accounting period, the prorated depreciation is taken into account.
18
     * The function is similar to AMORLINC, except that a depreciation coefficient is applied in
19
     * the calculation depending on the life of the assets.
20
     * This function will return the depreciation until the last period of the life of the assets
21
     * or until the cumulated value of depreciation is greater than the cost of the assets minus
22
     * the salvage value.
23
     *
24
     * Excel Function:
25
     *        AMORDEGRC(cost,purchased,firstPeriod,salvage,period,rate[,basis])
26
     *
27
     * @param mixed $cost The float cost of the asset
28
     * @param mixed $purchased Date of the purchase of the asset
29
     * @param mixed $firstPeriod Date of the end of the first period
30
     * @param mixed $salvage The salvage value at the end of the life of the asset
31
     * @param mixed $period the period (float)
32
     * @param mixed $rate rate of depreciation (float)
33
     * @param mixed $basis The type of day count to use (int).
34
     *                         0 or omitted    US (NASD) 30/360
35
     *                         1               Actual/actual
36
     *                         2               Actual/360
37
     *                         3               Actual/365
38
     *                         4               European 30/360
39
     *
40
     * @return float|string (string containing the error type if there is an error)
41
     */
42 25
    public static function AMORDEGRC(
43
        mixed $cost,
44
        mixed $purchased,
45
        mixed $firstPeriod,
46
        mixed $salvage,
47
        mixed $period,
48
        mixed $rate,
49
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
50
    ): string|float {
51 25
        $cost = Functions::flattenSingleValue($cost);
52 25
        $purchased = Functions::flattenSingleValue($purchased);
53 25
        $firstPeriod = Functions::flattenSingleValue($firstPeriod);
54 25
        $salvage = Functions::flattenSingleValue($salvage);
55 25
        $period = Functions::flattenSingleValue($period);
56 25
        $rate = Functions::flattenSingleValue($rate);
57 25
        $basis = ($basis === null)
58 1
            ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
59 24
            : Functions::flattenSingleValue($basis);
60
61
        try {
62 25
            $cost = FinancialValidations::validateFloat($cost);
63 24
            $purchased = FinancialValidations::validateDate($purchased);
64 23
            $firstPeriod = FinancialValidations::validateDate($firstPeriod);
65 22
            $salvage = FinancialValidations::validateFloat($salvage);
66 21
            $period = FinancialValidations::validateInt($period);
67 20
            $rate = FinancialValidations::validateFloat($rate);
68 19
            $basis = FinancialValidations::validateBasis($basis);
69 8
        } catch (Exception $e) {
70 8
            return $e->getMessage();
71
        }
72
73 17
        $yearFracx = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis);
74 17
        if (is_string($yearFracx)) {
75
            return $yearFracx;
76
        }
77
        /** @var float */
78 17
        $yearFrac = $yearFracx;
79
80 17
        $amortiseCoeff = self::getAmortizationCoefficient($rate);
81
82 17
        $rate *= $amortiseCoeff;
83 17
        $fNRate = round($yearFrac * $rate * $cost, 0);
84 17
        $cost -= $fNRate;
85 17
        $fRest = $cost - $salvage;
86
87 17
        for ($n = 0; $n < $period; ++$n) {
88 17
            $fNRate = round($rate * $cost, 0);
89 17
            $fRest -= $fNRate;
90
91 17
            if ($fRest < 0.0) {
92 1
                return match ($period - $n) {
93 1
                    1 => round($cost * 0.5, 0),
94 1
                    default => 0.0,
95 1
                };
96
            }
97 16
            $cost -= $fNRate;
98
        }
99
100 16
        return $fNRate;
101
    }
102
103
    /**
104
     * AMORLINC.
105
     *
106
     * Returns the depreciation for each accounting period.
107
     * This function is provided for the French accounting system. If an asset is purchased in
108
     * the middle of the accounting period, the prorated depreciation is taken into account.
109
     *
110
     * Excel Function:
111
     *        AMORLINC(cost,purchased,firstPeriod,salvage,period,rate[,basis])
112
     *
113
     * @param mixed $cost The cost of the asset as a float
114
     * @param mixed $purchased Date of the purchase of the asset
115
     * @param mixed $firstPeriod Date of the end of the first period
116
     * @param mixed $salvage The salvage value at the end of the life of the asset
117
     * @param mixed $period The period as a float
118
     * @param mixed $rate Rate of depreciation as  float
119
     * @param mixed $basis Integer indicating the type of day count to use.
120
     *                             0 or omitted    US (NASD) 30/360
121
     *                             1               Actual/actual
122
     *                             2               Actual/360
123
     *                             3               Actual/365
124
     *                             4               European 30/360
125
     *
126
     * @return float|string (string containing the error type if there is an error)
127
     */
128 18
    public static function AMORLINC(
129
        mixed $cost,
130
        mixed $purchased,
131
        mixed $firstPeriod,
132
        mixed $salvage,
133
        mixed $period,
134
        mixed $rate,
135
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
136
    ): string|float {
137 18
        $cost = Functions::flattenSingleValue($cost);
138 18
        $purchased = Functions::flattenSingleValue($purchased);
139 18
        $firstPeriod = Functions::flattenSingleValue($firstPeriod);
140 18
        $salvage = Functions::flattenSingleValue($salvage);
141 18
        $period = Functions::flattenSingleValue($period);
142 18
        $rate = Functions::flattenSingleValue($rate);
143 18
        $basis = ($basis === null)
144 1
            ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
145 17
            : Functions::flattenSingleValue($basis);
146
147
        try {
148 18
            $cost = FinancialValidations::validateFloat($cost);
149 17
            $purchased = FinancialValidations::validateDate($purchased);
150 16
            $firstPeriod = FinancialValidations::validateDate($firstPeriod);
151 15
            $salvage = FinancialValidations::validateFloat($salvage);
152 14
            $period = FinancialValidations::validateFloat($period);
153 13
            $rate = FinancialValidations::validateFloat($rate);
154 12
            $basis = FinancialValidations::validateBasis($basis);
155 8
        } catch (Exception $e) {
156 8
            return $e->getMessage();
157
        }
158
159 10
        $fOneRate = $cost * $rate;
160 10
        $fCostDelta = $cost - $salvage;
161
        //    Note, quirky variation for leap years on the YEARFRAC for this function
162 10
        $purchasedYear = DateTimeExcel\DateParts::year($purchased);
163 10
        $yearFracx = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis);
164 10
        if (is_string($yearFracx)) {
165
            return $yearFracx;
166
        }
167
        /** @var float */
168 10
        $yearFrac = $yearFracx;
169
170
        if (
171 10
            $basis == FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL
172 10
            && $yearFrac < 1
173 10
            && DateTimeExcel\Helpers::isLeapYear(Functions::scalar($purchasedYear))
174
        ) {
175 3
            $yearFrac *= 365 / 366;
176
        }
177
178 10
        $f0Rate = $yearFrac * $rate * $cost;
179 10
        $nNumOfFullPeriods = (int) (($cost - $salvage - $f0Rate) / $fOneRate);
180
181 10
        if ($period == 0) {
182 1
            return $f0Rate;
183 9
        } elseif ($period <= $nNumOfFullPeriods) {
184 7
            return $fOneRate;
185 2
        } elseif ($period == ($nNumOfFullPeriods + 1)) {
186 1
            return $fCostDelta - $fOneRate * $nNumOfFullPeriods - $f0Rate;
187
        }
188
189 1
        return 0.0;
190
    }
191
192 17
    private static function getAmortizationCoefficient(float $rate): float
193
    {
194
        //    The depreciation coefficients are:
195
        //    Life of assets (1/rate)        Depreciation coefficient
196
        //    Less than 3 years            1
197
        //    Between 3 and 4 years        1.5
198
        //    Between 5 and 6 years        2
199
        //    More than 6 years            2.5
200 17
        $fUsePer = 1.0 / $rate;
201
202 17
        if ($fUsePer < 3.0) {
203 1
            return 1.0;
204 16
        } elseif ($fUsePer < 4.0) {
205 3
            return 1.5;
206 13
        } elseif ($fUsePer <= 6.0) {
207 8
            return 2.0;
208
        }
209
210 5
        return 2.5;
211
    }
212
}
213