A free open source IT asset/license management system
Refactor the percent logic out of the blade
snipe7eb6ebb60dec
diff --git a/app/Models/Asset.php b/app/Models/Asset.phpindex 1ae46a8d34..88af0ed058 100644--- a/app/Models/Asset.php+++ b/app/Models/Asset.php@@ -1267,6 +1267,39 @@ class Asset extends Depreciablereturn (float) $this->components->sum('calculated_purchase_cost');}+ /**+ * Return EOL progress percentage (0-100), based on elapsed months since+ * purchase date over the configured EOL window.+ */+ public function eolProgressPercent(): float+ {+ if (! $this->purchase_date || ! $this->asset_eol_date) {+ return 0.0;+ }++ return $this->calculateProgressPercent(+ start: Carbon::parse($this->purchase_date),+ end: Carbon::parse($this->asset_eol_date),+ );+ }++ /**+ * Return warranty progress percentage (0-100), based on elapsed months+ * since purchase date over the warranty window.+ */+ public function warrantyProgressPercent(): float+ {+ if (! $this->purchase_date || ! $this->warranty_expires) {+ return 0.0;+ }++ return $this->calculateProgressPercent(+ start: Carbon::parse($this->purchase_date),+ end: $this->warranty_expires,+ );+ }++public function getAccessoryCost(){return (float) $this->accessories()->sum('purchase_cost');diff --git a/app/Models/Depreciable.php b/app/Models/Depreciable.phpindex f9e6754c64..4b1ca6d8fa 100644--- a/app/Models/Depreciable.php+++ b/app/Models/Depreciable.php@@ -2,6 +2,8 @@namespace App\Models;+use Carbon\Carbon;+class Depreciable extends SnipeModel{/**@@ -187,6 +189,39 @@ class Depreciable extends SnipeModel}+ /**+ * Return depreciation progress percentage (0-100), based on elapsed months+ * since purchase date over the depreciation window.+ */+ public function depreciationProgressPercent(): float+ {+ if (! $this->purchase_date || ! $this->depreciated_date()) {+ return 0.0;+ }++ return $this->calculateProgressPercent(+ start: Carbon::parse($this->purchase_date),+ end: Carbon::instance($this->depreciated_date()),+ );+ }++ /**+ * Calculate elapsed/total month percentage and clamp to 0-100.+ */+ protected function calculateProgressPercent(Carbon $start, Carbon $end): float+ {+ $totalMonths = (float) $start->diffInMonths($end);++ if ($totalMonths <= 0) {+ return 0.0;+ }++ $elapsedMonths = (float) $start->diffInMonths(Carbon::now());+ $rawPercent = ($elapsedMonths / $totalMonths) * 100;++ return (float) min(100, max(0, $rawPercent));+ }+// it's necessary for unit testsprotected function getDateTime($time = null){