Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions app/Http/Controllers/Api/DepreciationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ public function index(Request $request) : JsonResponse | array
$allowed_columns = [
'id',
'name',
'months',
'term_length',
'term_type',
'depreciation_min',
'depreciation_type',
'created_at',
'assets_count',
'models_count',
'licenses_count',
];
];

$depreciations = Depreciation::select('id','name','months','depreciation_min','depreciation_type','created_at','updated_at', 'created_by')
$depreciations = Depreciation::select('id','name','term_length','term_type','depreciation_min','depreciation_type','created_at','updated_at', 'created_by')
->with('adminuser')
->withCount('assets as assets_count')
->withCount('models as models_count')
->withCount('licenses as licenses_count');

if ($request->filled('search')) {
$depreciations = $depreciations->TextSearch($request->input('search'));
}
Expand Down
7 changes: 4 additions & 3 deletions app/Http/Controllers/DepreciationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function store(Request $request) : RedirectResponse
$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->created_by = auth()->id();

$request->validate([
Expand Down Expand Up @@ -129,8 +130,8 @@ public function update(Request $request, $depreciationId = null) : RedirectRespo

// 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');
$request->validate([
'depreciation_min' => [
'required',
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 @@ -63,11 +75,11 @@ public function transformAsset(Asset $asset)
*/
if (($asset->model) && ($asset->model->depreciation)) {
$depreciated_value = Helper::formatCurrencyOutput($asset->getDepreciatedValue());
$monthly_depreciation =Helper::formatCurrencyOutput($asset->purchase_cost / $asset->model->depreciation->months);
$rate_of_depreciation = Helper::formatCurrencyOutput($asset->purchase_cost / $asset->model->depreciation->term_length);
$diff = Helper::formatCurrencyOutput(($asset->purchase_cost - $asset->getDepreciatedValue()));
}
else if($asset->model->eol !== null) {
$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));
}

if ($asset->assigned) {
Expand Down Expand Up @@ -100,10 +112,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
6 changes: 3 additions & 3 deletions app/Http/Transformers/DepreciationsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Models\Depreciation;
use Illuminate\Support\Facades\Gate;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;

class DepreciationsTransformer
{
Expand All @@ -26,8 +25,9 @@ public function transformDepreciation(Depreciation $depreciation)
$array = [
'id' => (int) $depreciation->id,
'name' => e($depreciation->name),
'months' => $depreciation->months.' '.trans('general.months'),
'depreciation_min' => $depreciation->depreciation_type === 'percent' ? $depreciation->depreciation_min.'%' : $depreciation->depreciation_min,
'term_length' => $depreciation->term_length,
'term_type'=> $depreciation->term_type,
'depreciation_min' =>$depreciation->depreciation_type === 'percent' ? $depreciation->depreciation_min.'%' : $depreciation->depreciation_min,
'assets_count' => $depreciation->assets_count,
'models_count' => $depreciation->models_count,
'licenses_count' => $depreciation->licenses_count,
Expand Down
68 changes: 59 additions & 9 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,60 @@ 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()) && ($this->purchase_date)) {

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) {

Expand All @@ -86,7 +128,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->calculateDepreciation())) * ($months_passed / $this->get_depreciation()->months)), 2);
$current_value = round(($this->purchase_cost-($this->purchase_cost - ($this->calculateDepreciation())) * ($months_passed / $this->get_depreciation()->term_length)), 2);

}

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

public function getMonthlyDepreciation(){

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

}

Expand All @@ -111,7 +153,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 @@ -179,13 +221,21 @@ public function depreciated_date()
if (($this->purchase_date) && ($this->get_depreciation())) {
$date = date_create($this->purchase_date);

return date_add($date, date_interval_create_from_date_string($this->get_depreciation()->months.' months'));//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 null;
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
}
}
return null;
}



// it's necessary for unit tests
protected function getDateTime($time = null)
{
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 @@ -32,13 +32,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 @@ -68,12 +68,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 @@ -145,12 +139,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->unique()->catchPhrase(),
'created_by' => User::factory()->superuser(),
'months' => 36,
'term_length' => 365,
'term_type' => 'days',
];
}

Expand All @@ -34,7 +35,8 @@ public function computer()
return $this->state(function () {
return [
'name' => 'Computer Depreciation',
'months' => 36,
'term_length' => 36,
'term_type' => 'months',
];
});
}
Expand All @@ -44,7 +46,8 @@ public function display()
return $this->state(function () {
return [
'name' => 'Display Depreciation',
'months' => 12,
'term_length' => 36,
'term_type' => 'months',
];
});
}
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' => 'days',
];
});
}
Expand Down
Loading