diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php index 57bd6fe589dd..68695cf821d3 100644 --- a/app/Http/Controllers/Api/ComponentsController.php +++ b/app/Http/Controllers/Api/ComponentsController.php @@ -58,8 +58,8 @@ public function index(Request $request) : JsonResponse | array ]; $components = Component::select('components.*') - ->with('company', 'location', 'category', 'assets', 'supplier', 'adminuser', 'manufacturer', 'uncontrainedAssets') - ->withSum('uncontrainedAssets', 'components_assets.assigned_qty'); + ->with('company', 'location', 'category', 'supplier', 'adminuser', 'manufacturer') + ->withSum('uncontrainedAssets as sum_unconstrained_assets', 'components_assets.assigned_qty'); $filter = []; @@ -112,7 +112,8 @@ public function index(Request $request) : JsonResponse | array } // Make sure the offset and limit are actually integers and do not exceed system limits - $offset = ($request->input('offset') > $components->count()) ? $components->count() : app('api_offset_value'); + $components_count = $components->count(); + $offset = ($request->input('offset') > $components_count) ? $components_count : app('api_offset_value'); $limit = app('api_limit_value'); $order = $request->input('order') === 'asc' ? 'asc' : 'desc'; @@ -143,7 +144,7 @@ public function index(Request $request) : JsonResponse | array break; } - $total = $components->count(); + $total = $components_count; $components = $components->skip($offset)->take($limit)->get(); return (new ComponentsTransformer)->transformComponents($components, $total); diff --git a/app/Models/Component.php b/app/Models/Component.php index 14c4521fc4d1..7493d204c45c 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -238,14 +238,26 @@ public function assetlog() * @since [v5.0] * @return int */ - public function numCheckedOut() + public function numCheckedOut(bool $recalculate = false) { - $checkedout = 0; + /** + * + * WARNING: This method caches the result, so if you're doing something + * that is going to change the number of checked-out items, make sure to pass + * 'true' as the first parameter to force this to recalculate the number of checked-out + * items!!!!! + * + */ // In case there are elements checked out to assets that belong to a different company // than this asset and full multiple company support is on we'll remove the global scope, // so they are included in the count. - return $this->uncontrainedAssets->sum('pivot.assigned_qty'); + if (is_null($this->sum_unconstrained_assets) || $recalculate) { + // This, in a components-listing context, is mostly important for when it sets a 'zero' which + // is *not* null - so we don't have to keep recalculating for un-checked-out components + $this->sum_unconstrained_assets = $this->uncontrainedAssets()->sum('assigned_qty') ?? 0; + } + return $this->sum_unconstrained_assets; }