A free open source IT asset/license management system
battling with handling depreciation percentage and amount
Godfrey Maaa285833793
diff --git a/app/Http/Controllers/DepreciationsController.php b/app/Http/Controllers/DepreciationsController.phpindex 57ad35fea5..77a173581f 100755--- a/app/Http/Controllers/DepreciationsController.php+++ b/app/Http/Controllers/DepreciationsController.php@@ -63,6 +63,7 @@ class DepreciationsController extends Controller$depreciation->months = $request->input('months');$depreciation->user_id = Auth::id();$depreciation->depreciation_min = $request->input('depreciation_min');+ $depreciation->depreciation_type = $request->input('depreciation_type');// Was the asset created?if ($depreciation->save()) {@@ -116,6 +117,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('The depreciation minimum must be between 0 and 100 when depreciation type is percentage.');+ }+ },+ ],+ 'depreciation_type' => 'required|in:amount,percent',+ ]);+ $depreciation->depreciation_type = $request->input('depreciation_type');$depreciation->depreciation_min = $request->input('depreciation_min');// Was the asset created?diff --git a/app/Models/Depreciable.php b/app/Models/Depreciable.phpindex 7211358739..f5a29d790b 100644--- a/app/Models/Depreciable.php+++ b/app/Models/Depreciable.php@@ -67,7 +67,7 @@ class Depreciable extends SnipeModel* @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)) {$months_passed = ($this->purchase_date->diff(now())->m)+($this->purchase_date->diff(now())->y*12);} else {@@ -78,7 +78,7 @@ class Depreciable extends SnipeModel//if there is a floor use itif(!$this->get_depreciation()->depreciation_min == null) {- $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 SnipeModelpublic 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;}@@ -188,4 +188,16 @@ class Depreciable extends SnipeModel{return new \DateTime($time);}+ private function calculateDepreciation(){+ $depreciation_min = 0;+ if($this->get_depreciation()->depreciation_type === 'percentage') {+ $depreciation_percent= $this->get_depreciation()->depreciation_min / 100;+ $depreciation_min= $this->purchase_cost * $depreciation_percent;+ return $depreciation_min;+ }+ else{+ $depreciation_min = $this->get_depreciation()->depreciation_min;+ return $depreciation_min;+ }+ }}diff --git a/database/migrations/2024_07_16_184145_add_deprecitation_type_to_depreciations_table.php b/database/migrations/2024_07_16_184145_add_deprecitation_type_to_depreciations_table.phpnew file mode 100644index 0000000000..1a5355f739--- /dev/null+++ b/database/migrations/2024_07_16_184145_add_deprecitation_type_to_depreciations_table.php@@ -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');+ });+ }+};diff --git a/resources/views/depreciations/edit.blade.php b/resources/views/depreciations/edit.blade.phpindex a5376ea692..fed28918eb 100755--- a/resources/views/depreciations/edit.blade.php+++ b/resources/views/depreciations/edit.blade.php@@ -28,8 +28,12 @@<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>') !!}</div>