Merge remote-tracking branch 'origin/develop'
Some checks are pending
CodeQL Security Scan / CodeQL Security Scan (javascript) (push) Waiting to run
Codacy Security Scan / Codacy Security Scan (push) Waiting to run
Docker images (Alpine) / docker (push) Waiting to run
Docker images / docker (push) Waiting to run
Tests in MySQL / PHP ${{ matrix.php-version }} (8.1) (push) Waiting to run
Tests in MySQL / PHP ${{ matrix.php-version }} (8.2) (push) Waiting to run
Tests in MySQL / PHP ${{ matrix.php-version }} (8.3) (push) Waiting to run
Tests in SQLite / PHP ${{ matrix.php-version }} (8.1.1) (push) Waiting to run

This commit is contained in:
snipe 2024-08-12 10:10:59 +01:00
commit d5f66bc0dd
8 changed files with 136 additions and 10 deletions

View file

@ -20,9 +20,9 @@ class DepreciationsController extends Controller
public function index(Request $request) : JsonResponse | array
{
$this->authorize('view', Depreciation::class);
$allowed_columns = ['id','name','months','depreciation_min','created_at'];
$allowed_columns = ['id','name','months','depreciation_min', 'depreciation_type','created_at'];
$depreciations = Depreciation::select('id','name','months','depreciation_min','user_id','created_at','updated_at');
$depreciations = Depreciation::select('id','name','months','depreciation_min','depreciation_type','user_id','created_at','updated_at');
if ($request->filled('search')) {
$depreciations = $depreciations->TextSearch($request->input('search'));

View file

@ -62,6 +62,20 @@ class DepreciationsController extends Controller
$depreciation->name = $request->input('name');
$depreciation->months = $request->input('months');
$depreciation->user_id = Auth::id();
$request->validate([
'depreciation_min' => [
'required',
'numeric',
function ($attribute, $value, $fail) use ($request) {
if ($request->input('depreciation_type') == 'percent' && ($value < 0 || $value > 100)) {
$fail(trans('validation.percent'));
}
},
],
'depreciation_type' => 'required|in:amount,percent',
]);
$depreciation->depreciation_type = $request->input('depreciation_type');
$depreciation->depreciation_min = $request->input('depreciation_min');
// Was the asset created?
@ -116,6 +130,20 @@ class DepreciationsController extends Controller
// Depreciation data
$depreciation->name = $request->input('name');
$depreciation->months = $request->input('months');
$request->validate([
'depreciation_min' => [
'required',
'numeric',
function ($attribute, $value, $fail) use ($request) {
if ($request->input('depreciation_type') == 'percent' && ($value < 0 || $value > 100)) {
$fail(trans('validation.percent'));
}
},
],
'depreciation_type' => 'required|in:amount,percent',
]);
$depreciation->depreciation_type = $request->input('depreciation_type');
$depreciation->depreciation_min = $request->input('depreciation_min');
// Was the asset created?

View file

@ -7,6 +7,7 @@ use App\Models\Depreciable;
use App\Models\Depreciation;
use Illuminate\Support\Facades\Gate;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;
class DepreciationsTransformer
{
@ -26,7 +27,7 @@ class DepreciationsTransformer
'id' => (int) $depreciation->id,
'name' => e($depreciation->name),
'months' => $depreciation->months.' '.trans('general.months'),
'depreciation_min' => $depreciation->depreciation_min,
'depreciation_min' => $depreciation->depreciation_type === 'percent' ? $depreciation->depreciation_min.'%' : $depreciation->depreciation_min,
'created_at' => Helper::getFormattedDateObject($depreciation->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($depreciation->updated_at, 'datetime')
];

View file

@ -76,9 +76,9 @@ class Depreciable extends SnipeModel
if ($months_passed >= $this->get_depreciation()->months){
//if there is a floor use it
if(!$this->get_depreciation()->depreciation_min == null) {
if($this->get_depreciation()->depreciation_min) {
$current_value = $this->get_depreciation()->depreciation_min;
$current_value = $this->calculateDepreciation();
}else{
$current_value = 0;
@ -86,7 +86,7 @@ class Depreciable extends SnipeModel
}
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->calculateDepreciation())) * ($months_passed / $this->get_depreciation()->months)), 2);
}
@ -95,7 +95,7 @@ class Depreciable extends SnipeModel
public function getMonthlyDepreciation(){
return ($this->purchase_cost-$this->get_depreciation()->depreciation_min)/$this->get_depreciation()->months;
return ($this->purchase_cost-$this->calculateDepreciation())/$this->get_depreciation()->months;
}
@ -191,4 +191,16 @@ class Depreciable extends SnipeModel
{
return new \DateTime($time);
}
private function calculateDepreciation()
{
if($this->get_depreciation()->depreciation_type === 'percent') {
$depreciation_percent= $this->get_depreciation()->depreciation_min / 100;
$depreciation_min= $this->purchase_cost * $depreciation_percent;
return $depreciation_min;
}
$depreciation_min = $this->get_depreciation()->depreciation_min;
return $depreciation_min;
}
}

View file

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('depreciations', function (Blueprint $table) {
$table->string('depreciation_type')->after('depreciation_min')->default('amount');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('depreciations', function (Blueprint $table) {
$table->dropColumn('depreciation_type');
});
}
};

View file

@ -125,6 +125,8 @@ return [
'symbols' => 'The :attribute field must contain at least one symbol.',
'uncompromised' => 'The given :attribute has appeared in a data leak. Please choose a different :attribute.',
],
'percent' => 'The depreciation minimum must be between 0 and 100 when depreciation type is percentage.',
'present' => 'The :attribute field must be present.',
'present_if' => 'The :attribute field must be present when :other is :value.',
'present_unless' => 'The :attribute field must be present unless :other is :value.',

View file

@ -28,9 +28,13 @@
<label for="depreciation_min" class="col-md-3 control-label">
{{ trans('admin/depreciations/general.depreciation_min') }}
</label>
<div class="col-md-2" style="padding-left:0px">
<input class="form-control" name="depreciation_min" id="depreciation_min" value="{{ old('depreciation_min', $item->depreciation_min) }}" style="width: 80px; margin-left:15px" />
<div class="col-md-2" style="display: flex;">
<input class="form-control" name="depreciation_min" id="depreciation_min" value="{{ old('depreciation_min', $item->depreciation_min) }}" style="width: 80px; margin-right: 15px; display: inline-block;" />
<select class="form-control select2" name="depreciation_type" id="depreciation_type" data-minimum-results-for-search="Infinity" style="width: 150px; display: inline-block;">
<option value="amount" {{ old('depreciation_type', $item->depreciation_type) == 'amount' ? 'selected' : '' }}>Amount</option>
<option value="percent" {{ old('depreciation_type', $item->depreciation_type) == 'percent' ? 'selected' : '' }}>Percentage</option>
</select>
</div>
{!! $errors->first('depreciation_min', '<span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span>') !!}
{!! $errors->first('depreciation_min', '<span class="col-md-7 col-md-offset-3 alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span>') !!}
</div>
@stop

View file

@ -1,10 +1,13 @@
<?php
namespace Tests\Unit;
use App\Models\Asset;
use App\Models\Depreciable;
use App\Models\Depreciation;
use App\Models\Category;
use App\Models\License;
use App\Models\AssetModel;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;
class DepreciationTest extends TestCase
@ -25,6 +28,54 @@ class DepreciationTest extends TestCase
$this->assertEquals(5, $depreciation->models->count());
}
public function testDepreciationAmount()
{
$depreciation = Depreciation::factory()->create([
'depreciation_type' => 'amount',
'depreciation_min' => 1000,
'months'=> 36,
]);
$asset = Asset::factory()
->laptopMbp()
->create(
[
'category_id' => Category::factory()->assetLaptopCategory()->create(),
'purchase_date' => now()->subDecade(),
'purchase_cost' => 4000,
]);
$asset->model->update([
'depreciation_id' => $depreciation->id,
]);
$asset->getLinearDepreciatedValue();
$this->assertEquals($depreciation->depreciation_min, $asset->getLinearDepreciatedValue());
}
public function testDepreciationPercentage()
{
$depreciation = Depreciation::factory()->create([
'depreciation_type' => 'percent',
'depreciation_min' => 50,
'months'=> 36,
]);
$asset = Asset::factory()
->laptopMbp()
->create(
[
'category_id' => Category::factory()->assetLaptopCategory()->create(),
'purchase_date' => now()->subDecade(),
'purchase_cost' => 4000,
]);
$asset->model->update([
'depreciation_id' => $depreciation->id,
]);
$asset->getLinearDepreciatedValue();
$this->assertEquals(2000, $asset->getLinearDepreciatedValue());
}
public function testADepreciationHasLicenses()
{