Skip to content

A free open source IT asset/license management system

Add half-year convention in depreciation for Models/Depreciable.php

Minaev Dmitriy49ecc083984b
diff --git a/app/Models/Depreciable.php b/app/Models/Depreciable.php
index ee12a6506a..b26215bc8f 100644
--- a/app/Models/Depreciable.php
+++ b/app/Models/Depreciable.php
@@ -60,6 +60,64 @@ class Depreciable extends SnipeModel
return $current_value;
}
+ /**
+ * @param onlyHalfFirstYear Boolean always applied only second half of the first year
+ * @return float|int
+ */
+ public function getHalfYearDepreciatedValue($onlyHalfFirstYear = false)
+ {
+ // @link http://www.php.net/manual/en/class.dateinterval.php
+ $current_date = $this->getDateTime();
+ $purchase_date = date_create($this->purchase_date);
+ $currentYear = $this->get_fiscal_year( $current_date );
+ $purchaseYear = $this->get_fiscal_year( $purchase_date );
+ $yearsPast = $currentYear - $purchaseYear;
+ $deprecationYears = ceil($this->get_depreciation()->months / 12);
+ if( $onlyHalfFirstYear ) {
+ $yearsPast -= 0.5;
+ }
+ else {
+ if( !$this->is_first_half_of_year($purchase_date) ) {
+ $yearsPast -= 0.5;
+ }
+ }
+ if( !$this->is_first_half_of_year($current_date) ) {
+ $yearsPast += 0.5;
+ }
+
+ if($yearsPast >= $deprecationYears) {
+ $yearsPast = $deprecationYears;
+ }
+ else if($yearsPast < 0) {
+ $yearsPast = 0;
+ }
+ return $yearsPast / $deprecationYears * $this->purchase_cost;
+ }
+
+ /**
+ * @param \DateTime $date
+ * @return int
+ */
+ protected function get_fiscal_year($date) {
+ $year = intval($date->format('Y'));
+ // also, maybe it'll have to set fiscal year date
+ if($date->format('nj') === '1231') {
+ return $year;
+ }
+ else {
+ return $year - 1;
+ }
+ }
+
+ /**
+ * @param \DateTime $date
+ * @return bool
+ */
+ protected function is_first_half_of_year($date) {
+ $date0m0d = intval($date->format('md'));
+ return ($date0m0d < 601) || ($date0m0d >= 1231);
+ }
+
public function time_until_depreciated()
{
// @link http://www.php.net/manual/en/class.datetime.php
@@ -81,4 +139,10 @@ class Depreciable extends SnipeModel
date_add($date, date_interval_create_from_date_string($this->get_depreciation()->months . ' months'));
return $date; //date_format($date, 'Y-m-d'); //don't bake-in format, for internationalization
}
+
+ // it's necessary for unit tests
+ protected function getDateTime($time = null)
+ {
+ return new \DateTime($time);
+ }
}