Skip to content

A free open source IT asset/license management system

Introduce and user unconstrainedUsers on Consumable model

Marcus Moore565b3fd21a02
diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php
index 785a8c4175..185a4ffcb0 100644
--- a/app/Http/Controllers/Api/ConsumablesController.php
+++ b/app/Http/Controllers/Api/ConsumablesController.php
@@ -47,7 +47,7 @@ class ConsumablesController extends Controller
// distributed, unchanged by mark-consumed). outstanding_assignments_count feeds
// isDeletable() and the archive guard so neither fires a per-row count query.
$consumables = Consumable::with('company', 'location', 'category', 'defaultSupplier', 'manufacturer', 'orderItems.order.supplier')
- ->withCount('users as consumables_users_count')
+ ->withCount('unconstrainedUsers as consumables_users_count')
->withCount('outstandingAssignments as outstanding_assignments_count');
// This array is what determines which fields should be allowed to be sorted on ON the table itself.
@@ -616,7 +616,7 @@ class ConsumablesController extends Controller
public function requestable(Request $request): array
{
$query = Consumable::with('category', 'location', 'company', 'manufacturer', 'requests')
- ->withCount('users as consumables_users_count')
+ ->withCount('unconstrainedUsers as consumables_users_count')
->withCount('outstandingAssignments as outstanding_assignments_count')
->Requestable();
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index d08d51fd61..299cde8d51 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.php
@@ -389,6 +389,17 @@ class Consumable extends SnipeModel
return $this->belongsToMany(User::class, 'consumables_users', 'consumable_id', 'assigned_to')->withPivot('created_by')->withTrashed()->withTimestamps();
}
+ /**
+ * Same as users() but without FMCS scoping applied so counts
+ * reflect every unit checked out regardless of the viewer's company.
+ */
+ public function unconstrainedUsers(): BelongsToMany
+ {
+ return $this->belongsToMany(User::class, 'consumables_users', 'consumable_id', 'assigned_to')
+ ->withTrashed()
+ ->withoutGlobalScope(new CompanyableScope);
+ }
+
/**
* Establishes the item -> supplier relationship
*
@@ -450,7 +461,7 @@ class Consumable extends SnipeModel
*/
public function numCheckedOut()
{
- return $this->consumables_users_count ?? $this->users()->count();
+ return $this->consumables_users_count ?? $this->unconstrainedUsers()->count();
}
/**