diff --git a/app/models/Asset.php b/app/models/Asset.php index 9020d97d7d..7b4c6b398f 100755 --- a/app/models/Asset.php +++ b/app/models/Asset.php @@ -1,6 +1,6 @@ 'integer' ); - /** - * Handle depreciation - */ - public function depreciate() + public function depreciation() { - return $this->getCurrentValue( - Model::find($this->model_id)->depreciation_id, - $this->purchase_cost, - $this->purchase_date - ); + return $this->model->belongsTo('Depreciation','depreciation_id'); + } + + public function get_depreciation() + { + return $this->model->depreciation; } /** @@ -129,33 +127,6 @@ class Asset extends Elegant return date_format($date, 'Y-m-d'); } - public function months_until_depreciated() - { - $today = date("Y-m-d"); - - // @link http://www.php.net/manual/en/class.datetime.php - $d1 = new DateTime($today); - $d2 = new DateTime($this->depreciated_date()); - - // @link http://www.php.net/manual/en/class.dateinterval.php - $interval = $d1->diff($d2); - return $interval; - } - - - public function depreciated_date() - { - $date = date_create($this->purchase_date); - date_add($date, date_interval_create_from_date_string($this->depreciation->months.' months')); - return date_format($date, 'Y-m-d'); - } - - - public function depreciation() - { - return $this->model->belongsTo('Depreciation','depreciation_id'); - } - public function model() { return $this->belongsTo('Model','model_id'); @@ -164,13 +135,13 @@ class Asset extends Elegant /** * Get the license seat information **/ - public function licenses() + public function licenses() { return $this->belongsToMany('License', 'license_seats', 'asset_id', 'license_id'); } - public function licenseseats() + public function licenseseats() { return $this->hasMany('LicenseSeat', 'asset_id'); } @@ -207,7 +178,7 @@ class Asset extends Elegant /** * Get total assets */ - public static function autoincrement_asset() + public static function autoincrement_asset() { $settings = Setting::getSettings(); diff --git a/app/models/Depreciable.php b/app/models/Depreciable.php new file mode 100644 index 0000000000..52386e853b --- /dev/null +++ b/app/models/Depreciable.php @@ -0,0 +1,81 @@ +belongsTo('Depreciation','depreciation_id'); + } + + public function get_depreciation() + { + return $this->depreciation; + } + + /** + * @param $purchase_cost + * @param $purchase_date1 + * @return float|int + */ + + public function getDepreciatedValue() + { + if (!$this->get_depreciation()) { // will never happen + return $this->purchase_cost; + } + + if ($this->get_depreciation()->months <= 0) { + return $this->purchase_cost; + } + + // fraction of value left + $months_remaining = $this->time_until_depreciated()->m + $this->time_until_depreciated()->y; //UGlY + $current_value = round(($months_remaining/ $this->get_depreciation()->months) * $this->purchase_cost, 2); + + if ($current_value < 0) { + $current_value = 0; + } + return $current_value; + } + + public function time_until_depreciated() + { + // @link http://www.php.net/manual/en/class.datetime.php + $d1 = new DateTime(); + $d2 = $this->depreciated_date(); + + // @link http://www.php.net/manual/en/class.dateinterval.php + $interval = $d1->diff($d2); + if(!$interval->invert) { + return $interval; + } else { + return new DateInterval("PT0S"); //null interval (zero seconds from now) + } + } + + public function depreciated_date() + { + $date = date_create($this->purchase_date); + 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 + } +} \ No newline at end of file diff --git a/app/models/Depreciation.php b/app/models/Depreciation.php index 516f3c0cec..1b1506f9d0 100755 --- a/app/models/Depreciation.php +++ b/app/models/Depreciation.php @@ -12,4 +12,9 @@ class Depreciation extends Elegant { return $this->hasMany('Model', 'depreciation_id')->count(); } + + public function has_licenses() + { + return $this->hasMany('License','depreciation_id')->count(); + } } diff --git a/app/models/Elegant.php b/app/models/Elegant.php index aeec00023e..c2f0ddd77e 100755 --- a/app/models/Elegant.php +++ b/app/models/Elegant.php @@ -30,36 +30,4 @@ class Elegant extends Eloquent { return str_replace("{id}", $id, $this->rules); } - - /** - * @param $depreciation_id - * @param $purchase_cost - * @param $purchase_date1 - * @return float|int - */ - protected function getCurrentValue($depreciation_id, $purchase_cost, $purchase_date1) - { - if (!$depreciation_id) { - return $purchase_cost; - } - - $depreciation_term = Depreciation::find($depreciation_id)->months; - if ($depreciation_term <= 0) { - return $purchase_cost; - } - - $purchase_date = strtotime($purchase_date1); - - $todaymonthnumber = date("Y") * 12 + (date("m") - 1); //calculate the month number for today as YEAR*12 + (months-1) - number of months since January year 0 - $purchasemonthnumber = date("Y", $purchase_date) * 12 + (date("m", $purchase_date) - 1); //purchase date calculated similarly - $diff_months = $todaymonthnumber - $purchasemonthnumber; - - // fraction of value left - $current_value = round((($depreciation_term - $diff_months) / ($depreciation_term)) * $purchase_cost, 2); - - if ($current_value < 0) { - $current_value = 0; - } - return $current_value; - } } diff --git a/app/models/License.php b/app/models/License.php index 36fbee6d2f..b977aae9d5 100755 --- a/app/models/License.php +++ b/app/models/License.php @@ -1,11 +1,11 @@ belongsTo('Supplier','supplier_id'); } - - /** - * Get depreciation class - */ - public function depreciation() - { - return $this->belongsTo('Depreciation','depreciation_id'); - } - - public function months_until_depreciated() - { - $today = date("Y-m-d"); - - // @link http://www.php.net/manual/en/class.datetime.php - $d1 = new DateTime($today); - $d2 = new DateTime($this->depreciated_date()); - - // @link http://www.php.net/manual/en/class.dateinterval.php - $interval = $d1->diff($d2); - return $interval; - } - - public function depreciated_date() - { - $date = date_create($this->purchase_date); - date_add($date, date_interval_create_from_date_string($this->depreciation->months . ' months')); - return date_format($date, 'Y-m-d'); - } - - /** - * Handle depreciation - */ - public function depreciate() - { - return $this->getCurrentValue( - License::find($this->license_id)->depreciation_id, - $this->purchase_cost, - $this->purchase_date - ); - } } diff --git a/app/views/backend/hardware/view.blade.php b/app/views/backend/hardware/view.blade.php index 2a10c68e31..18e093101f 100755 --- a/app/views/backend/hardware/view.blade.php +++ b/app/views/backend/hardware/view.blade.php @@ -119,13 +119,13 @@ @lang('admin/hardware/form.months') )