ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Amortization.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
11 {
42  public static function AMORDEGRC(
43  $cost,
44  $purchased,
45  $firstPeriod,
46  $salvage,
47  $period,
48  $rate,
49  $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
50  ) {
51  $cost = Functions::flattenSingleValue($cost);
52  $purchased = Functions::flattenSingleValue($purchased);
53  $firstPeriod = Functions::flattenSingleValue($firstPeriod);
54  $salvage = Functions::flattenSingleValue($salvage);
55  $period = Functions::flattenSingleValue($period);
56  $rate = Functions::flattenSingleValue($rate);
57  $basis = ($basis === null)
58  ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
60 
61  try {
63  $purchased = FinancialValidations::validateDate($purchased);
64  $firstPeriod = FinancialValidations::validateDate($firstPeriod);
65  $salvage = FinancialValidations::validateFloat($salvage);
66  $period = FinancialValidations::validateInt($period);
68  $basis = FinancialValidations::validateBasis($basis);
69  } catch (Exception $e) {
70  return $e->getMessage();
71  }
72 
73  $yearFrac = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis);
74  if (is_string($yearFrac)) {
75  return $yearFrac;
76  }
77 
78  $amortiseCoeff = self::getAmortizationCoefficient($rate);
79 
80  $rate *= $amortiseCoeff;
81  $fNRate = round($yearFrac * $rate * $cost, 0);
82  $cost -= $fNRate;
83  $fRest = $cost - $salvage;
84 
85  for ($n = 0; $n < $period; ++$n) {
86  $fNRate = round($rate * $cost, 0);
87  $fRest -= $fNRate;
88 
89  if ($fRest < 0.0) {
90  switch ($period - $n) {
91  case 0:
92  case 1:
93  return round($cost * 0.5, 0);
94  default:
95  return 0.0;
96  }
97  }
98  $cost -= $fNRate;
99  }
100 
101  return $fNRate;
102  }
103 
129  public static function AMORLINC(
130  $cost,
131  $purchased,
132  $firstPeriod,
133  $salvage,
134  $period,
135  $rate,
136  $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
137  ) {
138  $cost = Functions::flattenSingleValue($cost);
139  $purchased = Functions::flattenSingleValue($purchased);
140  $firstPeriod = Functions::flattenSingleValue($firstPeriod);
141  $salvage = Functions::flattenSingleValue($salvage);
142  $period = Functions::flattenSingleValue($period);
143  $rate = Functions::flattenSingleValue($rate);
144  $basis = ($basis === null)
145  ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
147 
148  try {
150  $purchased = FinancialValidations::validateDate($purchased);
151  $firstPeriod = FinancialValidations::validateDate($firstPeriod);
152  $salvage = FinancialValidations::validateFloat($salvage);
153  $period = FinancialValidations::validateFloat($period);
155  $basis = FinancialValidations::validateBasis($basis);
156  } catch (Exception $e) {
157  return $e->getMessage();
158  }
159 
160  $fOneRate = $cost * $rate;
161  $fCostDelta = $cost - $salvage;
162  // Note, quirky variation for leap years on the YEARFRAC for this function
163  $purchasedYear = DateTimeExcel\DateParts::year($purchased);
164  $yearFrac = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis);
165  if (is_string($yearFrac)) {
166  return $yearFrac;
167  }
168 
169  if (
170  ($basis == FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL) &&
171  ($yearFrac < 1) && (DateTimeExcel\Helpers::isLeapYear($purchasedYear))
172  ) {
173  $yearFrac *= 365 / 366;
174  }
175 
176  $f0Rate = $yearFrac * $rate * $cost;
177  $nNumOfFullPeriods = (int) (($cost - $salvage - $f0Rate) / $fOneRate);
178 
179  if ($period == 0) {
180  return $f0Rate;
181  } elseif ($period <= $nNumOfFullPeriods) {
182  return $fOneRate;
183  } elseif ($period == ($nNumOfFullPeriods + 1)) {
184  return $fCostDelta - $fOneRate * $nNumOfFullPeriods - $f0Rate;
185  }
186 
187  return 0.0;
188  }
189 
190  private static function getAmortizationCoefficient(float $rate): float
191  {
192  // The depreciation coefficients are:
193  // Life of assets (1/rate) Depreciation coefficient
194  // Less than 3 years 1
195  // Between 3 and 4 years 1.5
196  // Between 5 and 6 years 2
197  // More than 6 years 2.5
198  $fUsePer = 1.0 / $rate;
199 
200  if ($fUsePer < 3.0) {
201  return 1.0;
202  } elseif ($fUsePer < 4.0) {
203  return 1.5;
204  } elseif ($fUsePer <= 6.0) {
205  return 2.0;
206  }
207 
208  return 2.5;
209  }
210 }
static AMORLINC( $cost, $purchased, $firstPeriod, $salvage, $period, $rate, $basis=FinancialConstants::BASIS_DAYS_PER_YEAR_NASD)
AMORLINC.
static fraction($startDate, $endDate, $method=0)
YEARFRAC.
Definition: YearFrac.php:37
$n
Definition: RandomTest.php:85
static isLeapYear($year)
Identify if a year is a leap year or not.
Definition: Helpers.php:19
static AMORDEGRC( $cost, $purchased, $firstPeriod, $salvage, $period, $rate, $basis=FinancialConstants::BASIS_DAYS_PER_YEAR_NASD)
AMORDEGRC.
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649