Skip to content

A free open source IT asset/license management system

Added maintenance counts

snipe50b8e34bdd93
diff --git a/app/Http/Controllers/Api/MaintenancesController.php b/app/Http/Controllers/Api/MaintenancesController.php
index deec1e250d..da2496d21a 100644
--- a/app/Http/Controllers/Api/MaintenancesController.php
+++ b/app/Http/Controllers/Api/MaintenancesController.php
@@ -14,6 +14,7 @@ use App\Models\Asset;
use App\Models\Company;
use App\Models\Maintenance;
use App\Models\Setting;
+use App\Models\User;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -54,6 +55,18 @@ class MaintenancesController extends Controller
$maintenances->where('asset_id', '=', $request->input('asset_id'));
}
+ // Polymorphic filter — used by the user detail Maintenances tab to
+ // pull every maintenance where the underlying asset was checked out
+ // to a specific user. Defaults to type=User when only the id is
+ // supplied; pass `checked_out_to_type=<fqcn>` to target locations
+ // or assets if/when those tabs land.
+ if ($request->filled('checked_out_to_id')) {
+ $type = $request->input('checked_out_to_type', User::class);
+ $maintenances
+ ->where('maintenances.checked_out_to_id', $request->input('checked_out_to_id'))
+ ->where('maintenances.checked_out_to_type', $type);
+ }
+
if ($request->filled('supplier_id')) {
$maintenances->where('maintenances.supplier_id', '=', $request->input('supplier_id'));
}
diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php
index a2395beda6..37daca06f7 100644
--- a/app/Http/Controllers/Api/UsersController.php
+++ b/app/Http/Controllers/Api/UsersController.php
@@ -101,6 +101,10 @@ class UsersController extends Controller
'consumables as consumables_count',
'managesUsers as manages_users_count',
'managedLocations as manages_locations_count',
+ // Count of maintenances whose polymorphic checked_out_to points
+ // at this user. Used by the users index sort + filter and by
+ // the user detail Maintenances tab badge.
+ 'assignedMaintenances as assigned_maintenances_count',
]);
$allowed_columns =
@@ -125,6 +129,7 @@ class UsersController extends Controller
'accessories_count',
'manages_users_count',
'manages_locations_count',
+ 'assigned_maintenances_count',
'phone',
'mobile',
'address',
@@ -306,6 +311,10 @@ class UsersController extends Controller
$users->has('accessories', '=', $request->input('accessories_count'));
}
+ if ($request->filled('assigned_maintenances_count')) {
+ $users->has('assignedMaintenances', '=', $request->input('assigned_maintenances_count'));
+ }
+
if ($request->filled('manages_users_count')) {
$users->has('managesUsers', '=', $request->input('manages_users_count'));
}
@@ -532,7 +541,7 @@ class UsersController extends Controller
{
$this->authorize('view', User::class);
- if ($user = User::withCount('assets as assets_count', 'licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count', 'managesUsers as manages_users_count', 'managedLocations as manages_locations_count')->find($id)) {
+ if ($user = User::withCount('assets as assets_count', 'licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count', 'managesUsers as manages_users_count', 'managedLocations as manages_locations_count', 'assignedMaintenances as assigned_maintenances_count')->find($id)) {
$this->authorize('view', $user);
return (new UsersTransformer)->transformUser($user);
diff --git a/app/Http/Transformers/UsersTransformer.php b/app/Http/Transformers/UsersTransformer.php
index 41c9c0c5b4..67e055a878 100644
--- a/app/Http/Transformers/UsersTransformer.php
+++ b/app/Http/Transformers/UsersTransformer.php
@@ -82,6 +82,7 @@ class UsersTransformer
'consumables_count' => (int) $user->consumables_count,
'manages_users_count' => (int) $user->manages_users_count,
'manages_locations_count' => (int) $user->manages_locations_count,
+ 'assigned_maintenances_count' => (int) $user->assigned_maintenances_count,
// Legacy field — kept for backward API compatibility; use `companies` for multi-company support.
'company' => $user->companies->isNotEmpty() ? [
'id' => (int) $user->companies->first()->id,
diff --git a/app/Models/User.php b/app/Models/User.php
index 3f3f357fe2..de60fa798c 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -838,6 +838,20 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
return $this->hasMany(Maintenance::class, 'user_id')->withTrashed();
}
+ /**
+ * Maintenances whose underlying asset was checked out to this user at the
+ * time the maintenance was opened — i.e. the polymorphic checked_out_to_*
+ * pair on the maintenances row points at this user.
+ *
+ * Distinct from maintenances() (which tracks who *created* the record)
+ * and from responsibleParty() (whoever is responsible for completion).
+ * Used by the user detail view's Maintenances tab and badge count.
+ */
+ public function assignedMaintenances()
+ {
+ return $this->morphMany(Maintenance::class, 'checked_out_to')->withTrashed();
+ }
+
/**
* Establishes the user -> accessories relationship
*
@@ -1522,6 +1536,7 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
$asset_cost = 0;
$license_cost = 0;
$accessory_cost = 0;
+ $maintenance_cost = 0;
foreach ($this->assets as $asset) {
$asset_cost += $asset->purchase_cost;
$this->asset_cost = $asset_cost;
@@ -1534,8 +1549,15 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
$accessory_cost += $accessory->purchase_cost;
$this->accessory_cost = $accessory_cost;
}
+ // Maintenances tied to this user as the polymorphic checked_out_to
+ // target. Summed across open + completed records — the user
+ // "caused" both.
+ foreach ($this->assignedMaintenances as $maintenance) {
+ $maintenance_cost += $maintenance->cost;
+ }
+ $this->maintenance_cost = $maintenance_cost;
- $this->total_user_cost = ($asset_cost + $accessory_cost + $license_cost);
+ $this->total_user_cost = ($asset_cost + $accessory_cost + $license_cost + $maintenance_cost);
return $this;
}
diff --git a/app/Presenters/UserPresenter.php b/app/Presenters/UserPresenter.php
index 4cb5a3dd0f..28f0ac91d9 100644
--- a/app/Presenters/UserPresenter.php
+++ b/app/Presenters/UserPresenter.php
@@ -307,6 +307,16 @@ class UserPresenter extends Presenter
'visible' => true,
'formatter' => 'linkNumberToUserManagedLocationsFormatter',
],
+ [
+ 'field' => 'assigned_maintenances_count',
+ 'searchable' => false,
+ 'sortable' => true,
+ 'switchable' => true,
+ 'class' => 'css-maintenances',
+ 'title' => trans('general.maintenances'),
+ 'visible' => false,
+ 'formatter' => 'linkNumberToUserAssignedMaintenancesFormatter',
+ ],
[
'field' => 'notes',
'searchable' => true,