PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Calculation / Financial / Depreciation.php

Depreciation.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/Financial/Depreciation.php

286 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Financial;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception;
6 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions;
7 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8
9 class Depreciation
10 {
11 private static float $zeroPointZero = 0.0;
12
13 /**
14 * DB.
15 *
16 * Returns the depreciation of an asset for a specified period using the
17 * fixed-declining balance method.
18 * This form of depreciation is used if you want to get a higher depreciation value
19 * at the beginning of the depreciation (as opposed to linear depreciation). The
20 * depreciation value is reduced with every depreciation period by the depreciation
21 * already deducted from the initial cost.
22 *
23 * Excel Function:
24 * DB(cost,salvage,life,period[,month])
25 *
26 * @param mixed $cost Initial cost of the asset
27 * @param mixed $salvage Value at the end of the depreciation.
28 * (Sometimes called the salvage value of the asset)
29 * @param mixed $life Number of periods over which the asset is depreciated.
30 * (Sometimes called the useful life of the asset)
31 * @param mixed $period The period for which you want to calculate the
32 * depreciation. Period must use the same units as life.
33 * @param mixed $month Number of months in the first year. If month is omitted,
34 * it defaults to 12.
35 * @return string|float|int
36 */
37 public static function DB($cost, $salvage, $life, $period, $month = 12)
38 {
39 $cost = Functions::flattenSingleValue($cost);
40 $salvage = Functions::flattenSingleValue($salvage);
41 $life = Functions::flattenSingleValue($life);
42 $period = Functions::flattenSingleValue($period);
43 $month = Functions::flattenSingleValue($month);
44
45 try {
46 $cost = self::validateCost($cost);
47 $salvage = self::validateSalvage($salvage);
48 $life = self::validateLife($life);
49 $period = self::validatePeriod($period);
50 $month = self::validateMonth($month);
51 } catch (Exception $e) {
52 return $e->getMessage();
53 }
54
55 if ($cost === self::$zeroPointZero) {
56 return 0.0;
57 }
58
59 // Set Fixed Depreciation Rate
60 $fixedDepreciationRate = 1 - ($salvage / $cost) ** (1 / $life);
61 $fixedDepreciationRate = round($fixedDepreciationRate, 3);
62
63 // Loop through each period calculating the depreciation
64 // TODO Handle period value between 0 and 1 (e.g. 0.5)
65 $previousDepreciation = 0;
66 $depreciation = 0;
67 for ($per = 1; $per <= $period; ++$per) {
68 if ($per == 1) {
69 $depreciation = $cost * $fixedDepreciationRate * $month / 12;
70 } elseif ($per == ($life + 1)) {
71 $depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate * (12 - $month) / 12;
72 } else {
73 $depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate;
74 }
75 $previousDepreciation += $depreciation;
76 }
77
78 return $depreciation;
79 }
80
81 /**
82 * DDB.
83 *
84 * Returns the depreciation of an asset for a specified period using the
85 * double-declining balance method or some other method you specify.
86 *
87 * Excel Function:
88 * DDB(cost,salvage,life,period[,factor])
89 *
90 * @param mixed $cost Initial cost of the asset
91 * @param mixed $salvage Value at the end of the depreciation.
92 * (Sometimes called the salvage value of the asset)
93 * @param mixed $life Number of periods over which the asset is depreciated.
94 * (Sometimes called the useful life of the asset)
95 * @param mixed $period The period for which you want to calculate the
96 * depreciation. Period must use the same units as life.
97 * @param mixed $factor The rate at which the balance declines.
98 * If factor is omitted, it is assumed to be 2 (the
99 * double-declining balance method).
100 * @return float|string
101 */
102 public static function DDB($cost, $salvage, $life, $period, $factor = 2.0)
103 {
104 $cost = Functions::flattenSingleValue($cost);
105 $salvage = Functions::flattenSingleValue($salvage);
106 $life = Functions::flattenSingleValue($life);
107 $period = Functions::flattenSingleValue($period);
108 $factor = Functions::flattenSingleValue($factor);
109
110 try {
111 $cost = self::validateCost($cost);
112 $salvage = self::validateSalvage($salvage);
113 $life = self::validateLife($life);
114 $period = self::validatePeriod($period);
115 $factor = self::validateFactor($factor);
116 } catch (Exception $e) {
117 return $e->getMessage();
118 }
119
120 if ($period > $life) {
121 return ExcelError::NAN();
122 }
123
124 // Loop through each period calculating the depreciation
125 // TODO Handling for fractional $period values
126 $previousDepreciation = 0;
127 $depreciation = 0;
128 for ($per = 1; $per <= $period; ++$per) {
129 $depreciation = min(
130 ($cost - $previousDepreciation) * ($factor / $life),
131 ($cost - $salvage - $previousDepreciation)
132 );
133 $previousDepreciation += $depreciation;
134 }
135
136 return $depreciation;
137 }
138
139 /**
140 * SLN.
141 *
142 * Returns the straight-line depreciation of an asset for one period
143 *
144 * @param mixed $cost Initial cost of the asset
145 * @param mixed $salvage Value at the end of the depreciation
146 * @param mixed $life Number of periods over which the asset is depreciated
147 *
148 * @return float|string Result, or a string containing an error
149 */
150 public static function SLN($cost, $salvage, $life)
151 {
152 $cost = Functions::flattenSingleValue($cost);
153 $salvage = Functions::flattenSingleValue($salvage);
154 $life = Functions::flattenSingleValue($life);
155
156 try {
157 $cost = self::validateCost($cost, true);
158 $salvage = self::validateSalvage($salvage, true);
159 $life = self::validateLife($life, true);
160 } catch (Exception $e) {
161 return $e->getMessage();
162 }
163
164 if ($life === self::$zeroPointZero) {
165 return ExcelError::DIV0();
166 }
167
168 return ($cost - $salvage) / $life;
169 }
170
171 /**
172 * SYD.
173 *
174 * Returns the sum-of-years' digits depreciation of an asset for a specified period.
175 *
176 * @param mixed $cost Initial cost of the asset
177 * @param mixed $salvage Value at the end of the depreciation
178 * @param mixed $life Number of periods over which the asset is depreciated
179 * @param mixed $period Period
180 *
181 * @return float|string Result, or a string containing an error
182 */
183 public static function SYD($cost, $salvage, $life, $period)
184 {
185 $cost = Functions::flattenSingleValue($cost);
186 $salvage = Functions::flattenSingleValue($salvage);
187 $life = Functions::flattenSingleValue($life);
188 $period = Functions::flattenSingleValue($period);
189
190 try {
191 $cost = self::validateCost($cost, true);
192 $salvage = self::validateSalvage($salvage);
193 $life = self::validateLife($life);
194 $period = self::validatePeriod($period);
195 } catch (Exception $e) {
196 return $e->getMessage();
197 }
198
199 if ($period > $life) {
200 return ExcelError::NAN();
201 }
202
203 $syd = (($cost - $salvage) * ($life - $period + 1) * 2) / ($life * ($life + 1));
204
205 return $syd;
206 }
207
208 /**
209 * @param mixed $cost
210 */
211 private static function validateCost($cost, bool $negativeValueAllowed = false): float
212 {
213 $cost = FinancialValidations::validateFloat($cost);
214 if ($cost < 0.0 && $negativeValueAllowed === false) {
215 throw new Exception(ExcelError::NAN());
216 }
217
218 return $cost;
219 }
220
221 /**
222 * @param mixed $salvage
223 */
224 private static function validateSalvage($salvage, bool $negativeValueAllowed = false): float
225 {
226 $salvage = FinancialValidations::validateFloat($salvage);
227 if ($salvage < 0.0 && $negativeValueAllowed === false) {
228 throw new Exception(ExcelError::NAN());
229 }
230
231 return $salvage;
232 }
233
234 /**
235 * @param mixed $life
236 */
237 private static function validateLife($life, bool $negativeValueAllowed = false): float
238 {
239 $life = FinancialValidations::validateFloat($life);
240 if ($life < 0.0 && $negativeValueAllowed === false) {
241 throw new Exception(ExcelError::NAN());
242 }
243
244 return $life;
245 }
246
247 /**
248 * @param mixed $period
249 */
250 private static function validatePeriod($period, bool $negativeValueAllowed = false): float
251 {
252 $period = FinancialValidations::validateFloat($period);
253 if ($period <= 0.0 && $negativeValueAllowed === false) {
254 throw new Exception(ExcelError::NAN());
255 }
256
257 return $period;
258 }
259
260 /**
261 * @param mixed $month
262 */
263 private static function validateMonth($month): int
264 {
265 $month = FinancialValidations::validateInt($month);
266 if ($month < 1) {
267 throw new Exception(ExcelError::NAN());
268 }
269
270 return $month;
271 }
272
273 /**
274 * @param mixed $factor
275 */
276 private static function validateFactor($factor): float
277 {
278 $factor = FinancialValidations::validateFloat($factor);
279 if ($factor <= 0.0) {
280 throw new Exception(ExcelError::NAN());
281 }
282
283 return $factor;
284 }
285 }
286