diff --git a/app/Http/Controllers/DepreciationsController.php b/app/Http/Controllers/DepreciationsController.php index 57ad35fea..77a173581 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.php index 721135873..f5a29d790 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 it if(!$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 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; } @@ -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.php new file mode 100644 index 000000000..1a5355f73 --- /dev/null +++ b/database/migrations/2024_07_16_184145_add_deprecitation_type_to_depreciations_table.php @@ -0,0 +1,28 @@ +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.php index a5376ea69..fed28918e 100755 --- a/resources/views/depreciations/edit.blade.php +++ b/resources/views/depreciations/edit.blade.php @@ -28,8 +28,12 @@ -