Skip to content

Commit 21251b4

Browse files
committed
Fixed #190, where depreciation on licenses would not be saved
1 parent 6f2b78a commit 21251b4

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

app/controllers/admin/LicensesController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function postCreate()
7878
$license->seats = e(Input::get('seats'));
7979
$license->purchase_date = e(Input::get('purchase_date'));
8080
$license->purchase_cost = e(Input::get('purchase_cost'));
81-
$license->depreciate = e(Input::get('depreciate'));
81+
$license->depreciation_id = e(Input::get('depreciation_id'));
8282
$license->user_id = Sentry::getId();
8383

8484
if (($license->purchase_date == "") || ($license->purchase_date == "0000-00-00")) {
@@ -181,6 +181,7 @@ public function postEdit($licenseId = null)
181181
$license->license_name = e(Input::get('license_name'));
182182
$license->notes = e(Input::get('notes'));
183183
$license->order_number = e(Input::get('order_number'));
184+
$license->depreciation_id = e(Input::get('depreciation_id'));
184185

185186
// Update the asset data
186187
if ( e(Input::get('purchase_date')) == '') {

app/models/License.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,65 @@ public function licenseseats()
118118
*/
119119
public function depreciation()
120120
{
121-
return $this->belongsTo('Depreciation','id');
121+
return $this->belongsTo('Depreciation','depreciation_id');
122+
}
123+
124+
125+
public function months_until_depreciated()
126+
{
127+
128+
$today = date("Y-m-d");
129+
130+
// @link http://www.php.net/manual/en/class.datetime.php
131+
$d1 = new DateTime($today);
132+
$d2 = new DateTime($this->depreciated_date());
133+
134+
// @link http://www.php.net/manual/en/class.dateinterval.php
135+
$interval = $d1->diff($d2);
136+
return $interval;
137+
138+
}
139+
140+
141+
public function depreciated_date()
142+
{
143+
$date = date_create($this->purchase_date);
144+
date_add($date, date_interval_create_from_date_string($this->depreciation->months.' months'));
145+
return date_format($date, 'Y-m-d');
146+
}
147+
148+
149+
150+
/**
151+
* Handle depreciation
152+
*/
153+
public function depreciate()
154+
{
155+
$depreciation_id = License::find($this->license_id)->depreciation_id;
156+
if ($depreciation_id) {
157+
$depreciation_term = Depreciation::find($depreciation_id)->months;
158+
if($depreciation_term>0) {
159+
160+
$purchase_date = strtotime($this->purchase_date);
161+
162+
$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
163+
$purchasemonthnumber=date("Y",$purchase_date)*12+(date("m",$purchase_date)-1); //purchase date calculated similarly
164+
$diff_months=$todaymonthnumber-$purchasemonthnumber;
165+
166+
// fraction of value left
167+
$current_value = round((( $depreciation_term - $diff_months) / ($depreciation_term)) * $this->purchase_cost,2);
168+
169+
if ($current_value < 0) {
170+
$current_value = 0;
171+
}
172+
return $current_value;
173+
} else {
174+
return $this->purchase_cost;
175+
}
176+
} else {
177+
return $this->purchase_cost;
178+
}
179+
122180
}
123181

124182

app/views/backend/licenses/view.blade.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@
3939
<div class="col-md-6"><strong>@lang('admin/licenses/form.notes'): </strong>{{ $license->notes }}</div>
4040
@endif
4141

42+
@if ($license->depreciation)
43+
<div class="col-md-6"><strong>@lang('admin/hardware/form.depreciation'): </strong>
44+
{{ $license->depreciation->name }}
45+
({{{ $license->depreciation->months }}}
46+
@lang('admin/hardware/form.months')
47+
)</div>
48+
<div class="col-md-6"><strong>@lang('admin/hardware/form.depreciates_on'): </strong>
49+
{{{ $license->depreciated_date() }}} </div>
50+
<div class="col-md-6"><strong>@lang('admin/hardware/form.fully_depreciated'): </strong>
51+
{{{ $license->months_until_depreciated()->m }}}
52+
@lang('admin/hardware/form.months')
53+
@if ($license->months_until_depreciated()->y > 0)
54+
, {{{ $license->months_until_depreciated()->y }}}
55+
@lang('admin/hardware/form.years')
56+
@endif
57+
</div>
58+
@endif
4259

4360
<br><br><br>
4461
</div>

0 commit comments

Comments
 (0)