From 21251b462308f2d783b83be1a331a0faf0813d16 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 9 Jul 2014 21:12:58 -0400 Subject: [PATCH] Fixed #190, where depreciation on licenses would not be saved --- app/controllers/admin/LicensesController.php | 3 +- app/models/License.php | 60 +++++++++++++++++++- app/views/backend/licenses/view.blade.php | 17 ++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/LicensesController.php b/app/controllers/admin/LicensesController.php index 4778cd20ea93..6a0fbc6163d5 100644 --- a/app/controllers/admin/LicensesController.php +++ b/app/controllers/admin/LicensesController.php @@ -78,7 +78,7 @@ public function postCreate() $license->seats = e(Input::get('seats')); $license->purchase_date = e(Input::get('purchase_date')); $license->purchase_cost = e(Input::get('purchase_cost')); - $license->depreciate = e(Input::get('depreciate')); + $license->depreciation_id = e(Input::get('depreciation_id')); $license->user_id = Sentry::getId(); if (($license->purchase_date == "") || ($license->purchase_date == "0000-00-00")) { @@ -181,6 +181,7 @@ public function postEdit($licenseId = null) $license->license_name = e(Input::get('license_name')); $license->notes = e(Input::get('notes')); $license->order_number = e(Input::get('order_number')); + $license->depreciation_id = e(Input::get('depreciation_id')); // Update the asset data if ( e(Input::get('purchase_date')) == '') { diff --git a/app/models/License.php b/app/models/License.php index 1ee2ac71618a..b457ad6e8673 100644 --- a/app/models/License.php +++ b/app/models/License.php @@ -118,7 +118,65 @@ public function licenseseats() */ public function depreciation() { - return $this->belongsTo('Depreciation','id'); + 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() + { + $depreciation_id = License::find($this->license_id)->depreciation_id; + if ($depreciation_id) { + $depreciation_term = Depreciation::find($depreciation_id)->months; + if($depreciation_term>0) { + + $purchase_date = strtotime($this->purchase_date); + + $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)) * $this->purchase_cost,2); + + if ($current_value < 0) { + $current_value = 0; + } + return $current_value; + } else { + return $this->purchase_cost; + } + } else { + return $this->purchase_cost; + } + } diff --git a/app/views/backend/licenses/view.blade.php b/app/views/backend/licenses/view.blade.php index 6262b4967a06..2097eb250031 100644 --- a/app/views/backend/licenses/view.blade.php +++ b/app/views/backend/licenses/view.blade.php @@ -39,6 +39,23 @@
@lang('admin/licenses/form.notes'): {{ $license->notes }}
@endif + @if ($license->depreciation) +
@lang('admin/hardware/form.depreciation'): + {{ $license->depreciation->name }} + ({{{ $license->depreciation->months }}} + @lang('admin/hardware/form.months') + )
+
@lang('admin/hardware/form.depreciates_on'): + {{{ $license->depreciated_date() }}}
+
@lang('admin/hardware/form.fully_depreciated'): + {{{ $license->months_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