From 1e4071b51dcb1e884608c88f74bbc87d24bbbfa4 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Thu, 12 Feb 2015 20:37:43 -0800 Subject: [PATCH 1/5] First stab at cleaning up Depreciation Conflicts: app/models/Asset.php It looks like the same time I pulled depreciation out of the Asset class, uploads were being added in. It doesn't look like a conflict to me, but git couldn't figure it out. Oh Well. --- app/models/Asset.php | 46 ++--------------------- app/models/Depreciation.php | 5 +++ app/models/Elegant.php | 32 ---------------- app/models/License.php | 44 +--------------------- app/views/backend/hardware/view.blade.php | 2 +- 5 files changed, 12 insertions(+), 117 deletions(-) diff --git a/app/models/Asset.php b/app/models/Asset.php index 9020d97d7d..4ecb301313 100755 --- a/app/models/Asset.php +++ b/app/models/Asset.php @@ -1,6 +1,6 @@ 'integer' ); - /** - * Handle depreciation - */ - public function depreciate() - { - return $this->getCurrentValue( - Model::find($this->model_id)->depreciation_id, - $this->purchase_cost, - $this->purchase_date - ); - } /** * Get uploads for this asset @@ -129,33 +118,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 +126,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 +169,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/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..2ec061404e 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..ad5ec38a8d 100755 --- a/app/views/backend/hardware/view.blade.php +++ b/app/views/backend/hardware/view.blade.php @@ -125,7 +125,7 @@ , {{{ $asset->months_until_depreciated()->y }}} @lang('admin/hardware/form.years') @endif - ({{{ $asset->depreciated_date() }}}) + ({{{ $asset->depreciation->depreciated_date() }}}) @endif From 899e2bc9b656a73ef4c0d78f5eb4a599a018a2d7 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Mon, 16 Feb 2015 20:35:24 -0800 Subject: [PATCH 2/5] Not convinved this filename is right or anything, but the shared depreciation code is here --- app/models/Depreciatable.php | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app/models/Depreciatable.php diff --git a/app/models/Depreciatable.php b/app/models/Depreciatable.php new file mode 100644 index 0000000000..8742456c3a --- /dev/null +++ b/app/models/Depreciatable.php @@ -0,0 +1,56 @@ +model->belongsTo('Depreciation','depreciation_id'); + } + + /** + * @param $purchase_cost + * @param $purchase_date1 + * @return float|int + */ + + protected function getCurrentValue() + { + if (!$this->depreciation) { // will never happen + return $this->purchase_cost; + } + + if ($this->depreciation->months <= 0) { + return $this->purchase_cost; + } + + // fraction of value left + $current_value = round(($this->months_until_depreciated() / ($this->depreciation->months)) * $this->purchase_cost, 2); + + if ($current_value < 0) { + $current_value = 0; + } + return $current_value; + } + + public function months_until_depreciated() + { + // @link http://www.php.net/manual/en/class.datetime.php + $d1 = new DateTime(); + $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; //date_format($date, 'Y-m-d'); //don't bake-in format, for internationalization + } +} \ No newline at end of file From 88dc0700f0ee6b8f336ac9f51721da35f3e372cd Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Tue, 17 Feb 2015 17:25:30 -0800 Subject: [PATCH 3/5] Make date functions work; create negative intervals for depreciation, Also renamed "months_until_depreciation" to "time_until_depreciation" And renamed Depreciatable to Depreciable (which is an actual word in English.) Also a cleanup/consistency-enforcement for interval display. --- app/models/Asset.php | 2 +- .../{Depreciatable.php => Depreciable.php} | 19 +++++++++++++------ app/models/License.php | 2 +- app/views/backend/hardware/view.blade.php | 8 ++++---- app/views/backend/licenses/view.blade.php | 12 ++++++------ 5 files changed, 25 insertions(+), 18 deletions(-) rename app/models/{Depreciatable.php => Depreciable.php} (67%) diff --git a/app/models/Asset.php b/app/models/Asset.php index 4ecb301313..071098c441 100755 --- a/app/models/Asset.php +++ b/app/models/Asset.php @@ -1,6 +1,6 @@ model->belongsTo('Depreciation','depreciation_id'); + return $this->belongsTo('Depreciation','depreciation_id'); } /** @@ -28,7 +31,7 @@ class Depreciatable extends Elegant } // fraction of value left - $current_value = round(($this->months_until_depreciated() / ($this->depreciation->months)) * $this->purchase_cost, 2); + $current_value = round(($this->time_until_depreciated() / ($this->depreciation->months)) * $this->purchase_cost, 2); if ($current_value < 0) { $current_value = 0; @@ -36,15 +39,19 @@ class Depreciatable extends Elegant return $current_value; } - public function months_until_depreciated() + public function time_until_depreciated() { // @link http://www.php.net/manual/en/class.datetime.php $d1 = new DateTime(); - $d2 = new DateTime($this->depreciated_date()); + $d2 = $this->depreciated_date(); // @link http://www.php.net/manual/en/class.dateinterval.php $interval = $d1->diff($d2); - return $interval; + if(!$interval->invert) { + return $interval; + } else { + return new DateInterval("PT0S"); //null interval (zero seconds from now) + } } public function depreciated_date() diff --git a/app/models/License.php b/app/models/License.php index 2ec061404e..b977aae9d5 100755 --- a/app/models/License.php +++ b/app/models/License.php @@ -1,6 +1,6 @@
@lang('admin/hardware/form.fully_depreciated'): - {{{ $asset->months_until_depreciated()->m }}} + {{{ $asset->time_until_depreciated()->m }}} @lang('admin/hardware/form.months') - @if ($asset->months_until_depreciated()->y > 0) - , {{{ $asset->months_until_depreciated()->y }}} + @if ($asset->time_until_depreciated()->y > 0) + , {{{ $asset->time_until_depreciated()->y }}} @lang('admin/hardware/form.years') @endif - ({{{ $asset->depreciation->depreciated_date() }}}) + ({{{ $asset->depreciated_date()->format('Y-m-d') }}})
@endif diff --git a/app/views/backend/licenses/view.blade.php b/app/views/backend/licenses/view.blade.php index e126b80bf8..19536e7287 100755 --- a/app/views/backend/licenses/view.blade.php +++ b/app/views/backend/licenses/view.blade.php @@ -73,18 +73,18 @@
@lang('admin/hardware/form.depreciates_on'): - {{{ $license->depreciated_date() }}} + {{{ $license->depreciated_date()->format("Y-m-d") }}}
@lang('admin/hardware/form.fully_depreciated'): - {{{ $license->months_until_depreciated()->m }}} + @if ($license->time_until_depreciated()->y > 0) + {{{ $license->time_until_depreciated()->y }}} + @lang('admin/hardware/form.years'), + @endif + {{{ $license->time_until_depreciated()->m }}} @lang('admin/hardware/form.months') - @if ($license->months_until_depreciated()->y > 0) - , {{{ $license->months_until_depreciated()->y }}} - @lang('admin/hardware/form.years') - @endif
@endif From 3ba02b65fb0c6d5a91039f303ec76059b559a022 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Tue, 17 Feb 2015 17:56:59 -0800 Subject: [PATCH 4/5] Oops - Assets get their Depreciation from their *model* - wheras Licenses have a Depreciation directly associated with them. So I pulled the Depreciation relationship out of Depreciable so that both Assets and Licenses could pull their depreciations in their associated ways. I could've done this with an inheritance call, but that seems a bit much (and unnecessarily confusing). --- app/models/Asset.php | 4 ++++ app/models/Depreciable.php | 7 +------ app/models/License.php | 5 +++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/models/Asset.php b/app/models/Asset.php index 071098c441..a245fd7ea9 100755 --- a/app/models/Asset.php +++ b/app/models/Asset.php @@ -20,6 +20,10 @@ class Asset extends Depreciable 'status' => 'integer' ); + public function depreciation() + { + return $this->model->belongsTo('Depreciation','depreciation_id'); + } /** * Get uploads for this asset diff --git a/app/models/Depreciable.php b/app/models/Depreciable.php index 4aa588b9df..a798fc620c 100644 --- a/app/models/Depreciable.php +++ b/app/models/Depreciable.php @@ -9,12 +9,7 @@ class Depreciable extends Elegant //REQUIRES a purchase_date field // and a purchase_cost field - public function depreciation() - { - return $this->belongsTo('Depreciation','depreciation_id'); - } - - /** + /** * @param $purchase_cost * @param $purchase_date1 * @return float|int diff --git a/app/models/License.php b/app/models/License.php index b977aae9d5..5487192f90 100755 --- a/app/models/License.php +++ b/app/models/License.php @@ -19,6 +19,11 @@ class License extends Depreciable 'notes' => 'alpha_space|min:0', ); + public function depreciation() + { + return $this->belongsTo('Depreciation','depreciation_id'); + } + /** * Get the assigned user */ From 063e5c407c8f817c16026887be16eeb42598417f Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Tue, 17 Feb 2015 18:51:13 -0800 Subject: [PATCH 5/5] Fixed the depreciation report, fixes to calculate current depreciated value (which were only used there). Additional fixes and refactoring around Depreciable, in terms of how assets get depreciation via models, and licenses get depreciation directly. --- app/models/Asset.php | 5 +++ app/models/Depreciable.php | 33 ++++++++++++++++--- app/models/License.php | 5 --- app/views/backend/hardware/view.blade.php | 10 +++--- .../backend/reports/depreciation.blade.php | 4 +-- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/app/models/Asset.php b/app/models/Asset.php index a245fd7ea9..7b4c6b398f 100755 --- a/app/models/Asset.php +++ b/app/models/Asset.php @@ -24,6 +24,11 @@ class Asset extends Depreciable { return $this->model->belongsTo('Depreciation','depreciation_id'); } + + public function get_depreciation() + { + return $this->model->depreciation; + } /** * Get uploads for this asset diff --git a/app/models/Depreciable.php b/app/models/Depreciable.php index a798fc620c..52386e853b 100644 --- a/app/models/Depreciable.php +++ b/app/models/Depreciable.php @@ -9,24 +9,47 @@ class Depreciable extends Elegant //REQUIRES a purchase_date field // and a purchase_cost field + //REQUIRES a get_depreciation method, + //which will return the deprecation. + //this is needed because assets get + //their depreciation from a model, + //whereas licenses have deprecations + //directly associated with them. + + //assets will override the following + //two methods in order to inherit from + //their model instead of directly (like + //here) + + public function depreciation() + { + return $this->belongsTo('Depreciation','depreciation_id'); + } + + public function get_depreciation() + { + return $this->depreciation; + } + /** * @param $purchase_cost * @param $purchase_date1 * @return float|int */ - protected function getCurrentValue() + public function getDepreciatedValue() { - if (!$this->depreciation) { // will never happen + if (!$this->get_depreciation()) { // will never happen return $this->purchase_cost; } - if ($this->depreciation->months <= 0) { + if ($this->get_depreciation()->months <= 0) { return $this->purchase_cost; } // fraction of value left - $current_value = round(($this->time_until_depreciated() / ($this->depreciation->months)) * $this->purchase_cost, 2); + $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; @@ -52,7 +75,7 @@ class Depreciable extends Elegant public function depreciated_date() { $date = date_create($this->purchase_date); - date_add($date, date_interval_create_from_date_string($this->depreciation->months . ' months')); + 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/License.php b/app/models/License.php index 5487192f90..b977aae9d5 100755 --- a/app/models/License.php +++ b/app/models/License.php @@ -19,11 +19,6 @@ class License extends Depreciable 'notes' => 'alpha_space|min:0', ); - public function depreciation() - { - return $this->belongsTo('Depreciation','depreciation_id'); - } - /** * Get the assigned user */ diff --git a/app/views/backend/hardware/view.blade.php b/app/views/backend/hardware/view.blade.php index fc2e3e689d..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') )
@lang('admin/hardware/form.fully_depreciated'): - {{{ $asset->time_until_depreciated()->m }}} - @lang('admin/hardware/form.months') @if ($asset->time_until_depreciated()->y > 0) - , {{{ $asset->time_until_depreciated()->y }}} - @lang('admin/hardware/form.years') + {{{ $asset->time_until_depreciated()->y }}} + @lang('admin/hardware/form.years'), @endif - ({{{ $asset->depreciated_date()->format('Y-m-d') }}}) + {{{ $asset->time_until_depreciated()->m }}} + @lang('admin/hardware/form.months') + ({{{ $asset->depreciated_date()->format('Y-m-d') }}})
@endif diff --git a/app/views/backend/reports/depreciation.blade.php b/app/views/backend/reports/depreciation.blade.php index 134321f3eb..394bae98a0 100644 --- a/app/views/backend/reports/depreciation.blade.php +++ b/app/views/backend/reports/depreciation.blade.php @@ -89,9 +89,9 @@ @lang('general.currency') {{{ number_format($asset->purchase_cost) }}} @lang('general.currency') - {{{ number_format($asset->depreciate()) }}} + {{{ number_format($asset->getDepreciatedValue()) }}} @lang('general.currency') - -{{{ number_format(($asset->purchase_cost - $asset->depreciate())) }}} + -{{{ number_format(($asset->purchase_cost - $asset->getDepreciatedValue())) }}} @else