Skip to content

Commit

Permalink
Fixed #190, where depreciation on licenses would not be saved
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Jul 10, 2014
1 parent 6f2b78a commit 21251b4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/controllers/admin/LicensesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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')) == '') {
Expand Down
60 changes: 59 additions & 1 deletion app/models/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}


Expand Down
17 changes: 17 additions & 0 deletions app/views/backend/licenses/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@
<div class="col-md-6"><strong>@lang('admin/licenses/form.notes'): </strong>{{ $license->notes }}</div>
@endif

@if ($license->depreciation)
<div class="col-md-6"><strong>@lang('admin/hardware/form.depreciation'): </strong>
{{ $license->depreciation->name }}
({{{ $license->depreciation->months }}}
@lang('admin/hardware/form.months')
)</div>
<div class="col-md-6"><strong>@lang('admin/hardware/form.depreciates_on'): </strong>
{{{ $license->depreciated_date() }}} </div>
<div class="col-md-6"><strong>@lang('admin/hardware/form.fully_depreciated'): </strong>
{{{ $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
</div>
@endif

<br><br><br>
</div>
Expand Down

0 comments on commit 21251b4

Please sign in to comment.