Merge pull request #532 from uberbrady/depreciation_refactor

Depreciation refactor
This commit is contained in:
snipe
2015-02-17 18:55:52 -08:00
8 changed files with 113 additions and 128 deletions
+11 -40
View File
@@ -1,6 +1,6 @@
<?php
class Asset extends Elegant
class Asset extends Depreciable
{
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
@@ -20,16 +20,14 @@ class Asset extends Elegant
'status' => '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();
+81
View File
@@ -0,0 +1,81 @@
<?php
class Depreciable extends Elegant
{
/**
* Depreciation Relation, and associated helper methods
*/
//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
*/
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
}
}
+5
View File
@@ -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();
}
}
-32
View File
@@ -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;
}
}
+2 -42
View File
@@ -1,11 +1,11 @@
<?php
class License extends Elegant
class License extends Depreciable
{
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
public $timestamps = true;
public $timestamps = true;
protected $guarded = 'id';
protected $table = 'licenses';
@@ -155,44 +155,4 @@ class License extends Elegant
{
return $this->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
);
}
}
+6 -6
View File
@@ -119,13 +119,13 @@
@lang('admin/hardware/form.months')
)</div>
<div class="col-md-12" style="padding-bottom: 5px;"><strong>@lang('admin/hardware/form.fully_depreciated'): </strong>
{{{ $asset->months_until_depreciated()->m }}}
@lang('admin/hardware/form.months')
@if ($asset->months_until_depreciated()->y > 0)
, {{{ $asset->months_until_depreciated()->y }}}
@lang('admin/hardware/form.years')
@if ($asset->time_until_depreciated()->y > 0)
{{{ $asset->time_until_depreciated()->y }}}
@lang('admin/hardware/form.years'),
@endif
({{{ $asset->depreciated_date() }}})
{{{ $asset->time_until_depreciated()->m }}}
@lang('admin/hardware/form.months')
({{{ $asset->depreciated_date()->format('Y-m-d') }}})
</div>
@endif
+6 -6
View File
@@ -73,18 +73,18 @@
<div class="col-md-6" style="padding-bottom: 5px">
<strong>@lang('admin/hardware/form.depreciates_on'): </strong>
{{{ $license->depreciated_date() }}}
{{{ $license->depreciated_date()->format("Y-m-d") }}}
</div>
<div class="col-md-6" style="padding-bottom: 5px">
<strong>@lang('admin/hardware/form.fully_depreciated'): </strong>
{{{ $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
</div>
@endif
@@ -89,9 +89,9 @@
<td class="align-right">@lang('general.currency')
{{{ number_format($asset->purchase_cost) }}}</td>
<td class="align-right">@lang('general.currency')
{{{ number_format($asset->depreciate()) }}}</td>
{{{ number_format($asset->getDepreciatedValue()) }}}</td>
<td class="align-right">@lang('general.currency')
-{{{ number_format(($asset->purchase_cost - $asset->depreciate())) }}}</td>
-{{{ number_format(($asset->purchase_cost - $asset->getDepreciatedValue())) }}}</td>
@else
<td></td>
<td></td>