ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Depreciation.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
9 {
35  public static function DB($cost, $salvage, $life, $period, $month = 12)
36  {
37  $cost = Functions::flattenSingleValue($cost);
38  $salvage = Functions::flattenSingleValue($salvage);
39  $life = Functions::flattenSingleValue($life);
40  $period = Functions::flattenSingleValue($period);
41  $month = Functions::flattenSingleValue($month);
42 
43  try {
44  $cost = self::validateCost($cost);
45  $salvage = self::validateSalvage($salvage);
46  $life = self::validateLife($life);
47  $period = self::validatePeriod($period);
48  $month = self::validateMonth($month);
49  } catch (Exception $e) {
50  return $e->getMessage();
51  }
52 
53  if ($cost === 0.0) {
54  return 0.0;
55  }
56 
57  // Set Fixed Depreciation Rate
58  $fixedDepreciationRate = 1 - ($salvage / $cost) ** (1 / $life);
59  $fixedDepreciationRate = round($fixedDepreciationRate, 3);
60 
61  // Loop through each period calculating the depreciation
62  // TODO Handle period value between 0 and 1 (e.g. 0.5)
63  $previousDepreciation = 0;
64  $depreciation = 0;
65  for ($per = 1; $per <= $period; ++$per) {
66  if ($per == 1) {
67  $depreciation = $cost * $fixedDepreciationRate * $month / 12;
68  } elseif ($per == ($life + 1)) {
69  $depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate * (12 - $month) / 12;
70  } else {
71  $depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate;
72  }
73  $previousDepreciation += $depreciation;
74  }
75 
76  return $depreciation;
77  }
78 
101  public static function DDB($cost, $salvage, $life, $period, $factor = 2.0)
102  {
103  $cost = Functions::flattenSingleValue($cost);
104  $salvage = Functions::flattenSingleValue($salvage);
105  $life = Functions::flattenSingleValue($life);
106  $period = Functions::flattenSingleValue($period);
107  $factor = Functions::flattenSingleValue($factor);
108 
109  try {
110  $cost = self::validateCost($cost);
111  $salvage = self::validateSalvage($salvage);
112  $life = self::validateLife($life);
113  $period = self::validatePeriod($period);
114  $factor = self::validateFactor($factor);
115  } catch (Exception $e) {
116  return $e->getMessage();
117  }
118 
119  if ($period > $life) {
120  return Functions::NAN();
121  }
122 
123  // Loop through each period calculating the depreciation
124  // TODO Handling for fractional $period values
125  $previousDepreciation = 0;
126  $depreciation = 0;
127  for ($per = 1; $per <= $period; ++$per) {
128  $depreciation = min(
129  ($cost - $previousDepreciation) * ($factor / $life),
130  ($cost - $salvage - $previousDepreciation)
131  );
132  $previousDepreciation += $depreciation;
133  }
134 
135  return $depreciation;
136  }
137 
149  public static function SLN($cost, $salvage, $life)
150  {
151  $cost = Functions::flattenSingleValue($cost);
152  $salvage = Functions::flattenSingleValue($salvage);
153  $life = Functions::flattenSingleValue($life);
154 
155  try {
156  $cost = self::validateCost($cost, true);
157  $salvage = self::validateSalvage($salvage, true);
158  $life = self::validateLife($life, true);
159  } catch (Exception $e) {
160  return $e->getMessage();
161  }
162 
163  if ($life === 0.0) {
164  return Functions::DIV0();
165  }
166 
167  return ($cost - $salvage) / $life;
168  }
169 
182  public static function SYD($cost, $salvage, $life, $period)
183  {
184  $cost = Functions::flattenSingleValue($cost);
185  $salvage = Functions::flattenSingleValue($salvage);
186  $life = Functions::flattenSingleValue($life);
187  $period = Functions::flattenSingleValue($period);
188 
189  try {
190  $cost = self::validateCost($cost, true);
191  $salvage = self::validateSalvage($salvage);
192  $life = self::validateLife($life);
193  $period = self::validatePeriod($period);
194  } catch (Exception $e) {
195  return $e->getMessage();
196  }
197 
198  if ($period > $life) {
199  return Functions::NAN();
200  }
201 
202  $syd = (($cost - $salvage) * ($life - $period + 1) * 2) / ($life * ($life + 1));
203 
204  return $syd;
205  }
206 
207  private static function validateCost($cost, bool $negativeValueAllowed = false): float
208  {
210  if ($cost < 0.0 && $negativeValueAllowed === false) {
211  throw new Exception(Functions::NAN());
212  }
213 
214  return $cost;
215  }
216 
217  private static function validateSalvage($salvage, bool $negativeValueAllowed = false): float
218  {
219  $salvage = FinancialValidations::validateFloat($salvage);
220  if ($salvage < 0.0 && $negativeValueAllowed === false) {
221  throw new Exception(Functions::NAN());
222  }
223 
224  return $salvage;
225  }
226 
227  private static function validateLife($life, bool $negativeValueAllowed = false): float
228  {
230  if ($life < 0.0 && $negativeValueAllowed === false) {
231  throw new Exception(Functions::NAN());
232  }
233 
234  return $life;
235  }
236 
237  private static function validatePeriod($period, bool $negativeValueAllowed = false): float
238  {
239  $period = FinancialValidations::validateFloat($period);
240  if ($period <= 0.0 && $negativeValueAllowed === false) {
241  throw new Exception(Functions::NAN());
242  }
243 
244  return $period;
245  }
246 
247  private static function validateMonth($month): int
248  {
249  $month = FinancialValidations::validateInt($month);
250  if ($month < 1) {
251  throw new Exception(Functions::NAN());
252  }
253 
254  return $month;
255  }
256 
257  private static function validateFactor($factor): float
258  {
259  $factor = FinancialValidations::validateFloat($factor);
260  if ($factor <= 0.0) {
261  throw new Exception(Functions::NAN());
262  }
263 
264  return $factor;
265  }
266 }
static validatePeriod($period, bool $negativeValueAllowed=false)
static SYD($cost, $salvage, $life, $period)
SYD.
static DB($cost, $salvage, $life, $period, $month=12)
DB.
static validateSalvage($salvage, bool $negativeValueAllowed=false)
static DDB($cost, $salvage, $life, $period, $factor=2.0)
DDB.
static validateCost($cost, bool $negativeValueAllowed=false)
static validateLife($life, bool $negativeValueAllowed=false)
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649