Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/DepreciationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class DepreciationsController extends Controller
public function index(Request $request)
{
$this->authorize('view', Depreciation::class);
$allowed_columns = ['id','name','months','depreciation_min','created_at'];
$allowed_columns = ['id','name','term_length','term_type','depreciation_min','created_at'];

$depreciations = Depreciation::select('id','name','months','depreciation_min','user_id','created_at','updated_at');
$depreciations = Depreciation::select('id','name','term_length','term_type','depreciation_min','user_id','created_at','updated_at');

if ($request->filled('search')) {
$depreciations = $depreciations->TextSearch($request->input('search'));
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/DepreciationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function store(Request $request)
$depreciation = new Depreciation();
// Depreciation data
$depreciation->name = $request->input('name');
$depreciation->months = $request->input('months');
$depreciation->term_length = $request->input('term_length');
$depreciation->term_type = $request->input('term_type');
$depreciation->user_id = Auth::id();
$depreciation->depreciation_min = $request->input('depreciation_min');

Expand Down Expand Up @@ -126,7 +127,8 @@ public function update(Request $request, $depreciationId = null)

// Depreciation data
$depreciation->name = $request->input('name');
$depreciation->months = $request->input('months');
$depreciation->term_length = $request->input('term_length');
$depreciation->term_type = $request->input('term_type');
$depreciation->depreciation_min = $request->input('depreciation_min');

// Was the asset created?
Expand Down
22 changes: 17 additions & 5 deletions app/Http/Transformers/DepreciationReportTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,21 @@ public function transformAsset(Asset $asset)
*/
$purchase_cost = null;
$depreciated_value = null;
$monthly_depreciation = null;
$rate_of_depreciation = null;
$diff = null;
$checkout_target = null;
$term_length = null;


/** Sets the term length based on the term_type months/days
*/
if (($asset->model) && ($asset->model->depreciation)) {
if ($asset->model->depreciation->term_type == "days") {
$term_length = e($asset->model->depreciation->term_length).' '.trans('admin/depreciations/table.days');
} else {
$term_length = e($asset->model->depreciation->term_length).' '.trans('admin/depreciations/table.months');
}
}

/**
* If there is a location set and a currency set, use that for display
Expand All @@ -64,10 +76,10 @@ public function transformAsset(Asset $asset)
if (($asset->model) && ($asset->model->depreciation)) {
$depreciated_value = Helper::formatCurrencyOutput($asset->getDepreciatedValue());
if($asset->model->eol==0 || $asset->model->eol==null ){
$monthly_depreciation = Helper::formatCurrencyOutput($asset->purchase_cost / $asset->model->depreciation->months);
$rate_of_depreciation = Helper::formatCurrencyOutput($asset->purchase_cost / $asset->model->depreciation->term_length);
}
else {
$monthly_depreciation = Helper::formatCurrencyOutput(($asset->model->eol > 0 ? ($asset->purchase_cost / $asset->model->eol) : 0));
$rate_of_depreciation = Helper::formatCurrencyOutput(($asset->model->eol > 0 ? ($asset->purchase_cost / $asset->model->eol) : 0));
}
$diff = Helper::formatCurrencyOutput(($asset->purchase_cost - $asset->getDepreciatedValue()));
}
Expand Down Expand Up @@ -103,10 +115,10 @@ public function transformAsset(Asset $asset)
'purchase_date' => Helper::getFormattedDateObject($asset->purchase_date, 'date'),
'purchase_cost' => Helper::formatCurrencyOutput($asset->purchase_cost),
'book_value' => Helper::formatCurrencyOutput($depreciated_value),
'monthly_depreciation' => $monthly_depreciation,
'rate_of_depreciation' => $rate_of_depreciation,
'checked_out_to' => ($checkout_target) ? e($checkout_target) : null,
'diff' => Helper::formatCurrencyOutput($diff),
'number_of_months' => ($asset->model && $asset->model->depreciation) ? e($asset->model->depreciation->months) : null,
'term_length' => $term_length,
'depreciation' => (($asset->model) && ($asset->model->depreciation)) ? e($asset->model->depreciation->name) : null,


Expand Down
3 changes: 2 additions & 1 deletion app/Http/Transformers/DepreciationsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function transformDepreciation(Depreciation $depreciation)
$array = [
'id' => (int) $depreciation->id,
'name' => e($depreciation->name),
'months' => $depreciation->months.' '.trans('general.months'),
'term_length' => $depreciation->term_length,
'term_type'=> $depreciation->term_type,
'depreciation_min' => $depreciation->depreciation_min,
'created_at' => Helper::getFormattedDateObject($depreciation->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($depreciation->updated_at, 'datetime')
Expand Down
62 changes: 55 additions & 7 deletions app/Models/Depreciable.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace App\Models;
use Carbon\Carbon;

class Depreciable extends SnipeModel
{
Expand Down Expand Up @@ -42,7 +43,7 @@ public function getDepreciatedValue()
return $this->purchase_cost;
}

if ($this->get_depreciation()->months <= 0) {
if ($this->get_depreciation()->term_length <= 0) {
return $this->purchase_cost;
}
$depreciation = 0;
Expand All @@ -62,19 +63,58 @@ public function getDepreciatedValue()

return $depreciation;
}
/**
* @return float|int
*/
public function getDailyDepreciationValue() {

if ($this->purchase_date) {
$purchased = new Carbon($this->purchase_date);
$now = Carbon::now();
$days_gone_by = $purchased->diffInDays($now);

} else {
return null;
}

$depreciation_per_day= $this->purchase_cost/$this->get_depreciation()->term_length;

if($days_gone_by >= $this->get_depreciation()->term_length) {

if (!$this->get_depreciation()->depreciation_min == null) {
$current_value = $this->get_depreciation()->depreciation_min;
}

else{
$current_value = 0;
}
}
else {
$current_value = $this->purchase_cost-($depreciation_per_day * $days_gone_by);
if($current_value < $this->get_depreciation()->depreciation_min)
{
$current_value = $this->get_depreciation()->depreciation_min;
}
}
return $current_value;
}

/**
* @return float|int
*/
public function getLinearDepreciatedValue() // TODO - for testing it might be nice to have an optional $relative_to param here, defaulted to 'now'
{
if ($this->get_depreciation()->term_type == 'days'){
return $this->getDailyDepreciationValue();
}

if ($this->purchase_date) {
$months_passed = ($this->purchase_date->diff(now())->m)+($this->purchase_date->diff(now())->y*12);
} else {
return null;
}

if ($months_passed >= $this->get_depreciation()->months){
if ($months_passed >= $this->get_depreciation()->term_length){
//if there is a floor use it
if(!$this->get_depreciation()->depreciation_min == null) {

Expand All @@ -86,7 +126,7 @@ public function getLinearDepreciatedValue() // TODO - for testing it might be ni
}
else {
// The equation here is (Purchase_Cost-Floor_min)*(Months_passed/Months_til_depreciated)
$current_value = round(($this->purchase_cost-($this->purchase_cost - ($this->get_depreciation()->depreciation_min)) * ($months_passed / $this->get_depreciation()->months)), 2);
$current_value = round(($this->purchase_cost-($this->purchase_cost - ($this->get_depreciation()->depreciation_min)) * ($months_passed / $this->get_depreciation()->term_length)), 2);

}

Expand All @@ -95,7 +135,7 @@ public function getLinearDepreciatedValue() // TODO - for testing it might be ni

public function getMonthlyDepreciation(){

return ($this->purchase_cost-$this->get_depreciation()->depreciation_min)/$this->get_depreciation()->months;
return ($this->purchase_cost-$this->get_depreciation()->depreciation_min)/$this->get_depreciation()->term_length;

}

Expand All @@ -111,7 +151,7 @@ public function getHalfYearDepreciatedValue($onlyHalfFirstYear = false)
$currentYear = $this->get_fiscal_year($current_date);
$purchaseYear = $this->get_fiscal_year($purchase_date);
$yearsPast = $currentYear - $purchaseYear;
$deprecationYears = ceil($this->get_depreciation()->months / 12);
$deprecationYears = ceil($this->get_depreciation()->term_length / 12);
if ($onlyHalfFirstYear) {
$yearsPast -= 0.5;
} elseif (! $this->is_first_half_of_year($purchase_date)) {
Expand Down Expand Up @@ -174,9 +214,17 @@ public function time_until_depreciated()
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
if($this->get_depreciation()->term_type == 'days') {
date_add($date, date_interval_create_from_date_string($this->get_depreciation()->term_length .'days'));

return $date; //date_format($date, 'Y-m-d'); //don't bake-in format, for internationalization
}
else
date_add($date, date_interval_create_from_date_string($this->get_depreciation()->term_length . ' months'));

return $date; //date_format($date, 'Y-m-d'); //don't bake-in format, for internationalization

}

// it's necessary for unit tests
Expand Down
7 changes: 4 additions & 3 deletions app/Models/Depreciation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Depreciation extends SnipeModel
// Declare the rules for the form validation
protected $rules = [
'name' => 'required|min:3|max:255|unique:depreciations,name',
'months' => 'required|max:3600|integer|gt:0',
'term_length' => 'required|max:3600|integer|gt:0',
'term_type' => 'required|string',
];

/**
Expand All @@ -34,7 +35,7 @@ class Depreciation extends SnipeModel
*
* @var array
*/
protected $fillable = ['name', 'months'];
protected $fillable = ['name', 'term_length', 'term_type'];

use Searchable;

Expand All @@ -43,7 +44,7 @@ class Depreciation extends SnipeModel
*
* @var array
*/
protected $searchableAttributes = ['name', 'months'];
protected $searchableAttributes = ['name', 'term_type', 'term_length'];

/**
* The relations and their attributes that should be included when searching the model.
Expand Down
10 changes: 9 additions & 1 deletion app/Presenters/DepreciationPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ public static function dataTableLayout()
],

[
'field' => 'months',
'field' => 'term_length',
'searchable' => true,
'sortable' => true,
'title' => trans('admin/depreciations/table.term'),
'visible' => true,
],

[
'field' => 'term_type',
'searchable' => true,
'sortable' => true,
'title' => trans('admin/depreciations/table.term_type'),
'visible' => true,
],

[
"field" => 'depreciation_min',
"searchable" => false,
Expand Down
18 changes: 9 additions & 9 deletions app/Presenters/DepreciationReportPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ public static function dataTableLayout()
"title" => trans('general.depreciation'),
"visible" => true,
], [
"field" => "number_of_months",
"searchable" => true,
"sortable" => true,
"title" => trans('admin/depreciations/general.number_of_months'),
"visible" => true,
], [
"field" => "status",
"searchable" => true,
"sortable" => true,
Expand Down Expand Up @@ -144,12 +138,18 @@ public static function dataTableLayout()
"title" => trans('admin/hardware/table.book_value'),
"footerFormatter" => 'sumFormatter',
"class" => "text-right",
], [
"field" => "monthly_depreciation",
], [
"field" => "term_length",
"searchable" => true,
"sortable" => true,
"title" => trans('admin/depreciations/general.term_length'),
"visible" => true,
],[
"field" => "rate_of_depreciation",
"searchable" => true,
"sortable" => true,
"visible" => true,
"title" => trans('admin/hardware/table.monthly_depreciation')
"title" => trans('admin/hardware/table.rate_of_depreciation')
],[
"field" => "diff",
"searchable" => false,
Expand Down
12 changes: 8 additions & 4 deletions database/factories/DepreciationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function definition()
return [
'name' => $this->faker->catchPhrase(),
'user_id' => User::factory()->superuser(),
'months' => 36,
'term_length' => 36,
'term_type' => 0,
];
}

Expand All @@ -34,7 +35,8 @@ public function computer()
return $this->state(function () {
return [
'name' => 'Computer Depreciation',
'months' => 36,
'term_length' => 36,
'term_type' => 0,
];
});
}
Expand All @@ -44,7 +46,8 @@ public function display()
return $this->state(function () {
return [
'name' => 'Display Depreciation',
'months' => 12,
'term_length' => 36,
'term_type' => 0,
];
});
}
Expand All @@ -54,7 +57,8 @@ public function mobilePhones()
return $this->state(function () {
return [
'name' => 'Mobile Phone Depreciation',
'months' => 24,
'term_length' => 365,
'term_type' => 1,
];
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddsTermTypeToDepreciationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('depreciations', function (Blueprint $table) {
$table->renameColumn('months', 'term_length');
$table->string('term_type')->default('months');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('depreciations', function (Blueprint $table) {
$table->renameColumn('term_length', 'months');
$table->dropColumn('term_type');
});
}
}
1 change: 1 addition & 0 deletions resources/lang/en/admin/depreciations/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'depreciation_name' => 'Depreciation Name',
'depreciation_min' => 'Floor Value of Depreciation',
'number_of_months' => 'Number of Months',
'term_length' => 'Length of Term',
'update' => 'Update Depreciation',
'depreciation_min' => 'Minimum Value after Depreciation',
'no_depreciations_warning' => '<strong>Warning: </strong>
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en/admin/depreciations/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

'id' => 'ID',
'months' => 'Months',
'days' => 'Days',
'term' => 'Term',
'title' => 'Name ',
'term_type' => 'Term type',
'depreciation_min' => 'Floor Value',

];
Loading