Skip to content

Commit a272bdc

Browse files
authored
Merge pull request #18251 from uberbrady/improve_component_asset_counts
Optimize queries for Components listing
2 parents b3996f1 + 416b32c commit a272bdc

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

app/Http/Controllers/Api/ComponentsController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public function index(Request $request) : JsonResponse | array
5858
];
5959

6060
$components = Component::select('components.*')
61-
->with('company', 'location', 'category', 'assets', 'supplier', 'adminuser', 'manufacturer', 'uncontrainedAssets')
62-
->withSum('uncontrainedAssets', 'components_assets.assigned_qty');
61+
->with('company', 'location', 'category', 'supplier', 'adminuser', 'manufacturer')
62+
->withSum('uncontrainedAssets as sum_unconstrained_assets', 'components_assets.assigned_qty');
6363

6464
$filter = [];
6565

@@ -112,7 +112,8 @@ public function index(Request $request) : JsonResponse | array
112112
}
113113

114114
// Make sure the offset and limit are actually integers and do not exceed system limits
115-
$offset = ($request->input('offset') > $components->count()) ? $components->count() : app('api_offset_value');
115+
$components_count = $components->count();
116+
$offset = ($request->input('offset') > $components_count) ? $components_count : app('api_offset_value');
116117
$limit = app('api_limit_value');
117118

118119
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
@@ -143,7 +144,7 @@ public function index(Request $request) : JsonResponse | array
143144
break;
144145
}
145146

146-
$total = $components->count();
147+
$total = $components_count;
147148
$components = $components->skip($offset)->take($limit)->get();
148149

149150
return (new ComponentsTransformer)->transformComponents($components, $total);

app/Models/Component.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,26 @@ public function assetlog()
238238
* @since [v5.0]
239239
* @return int
240240
*/
241-
public function numCheckedOut()
241+
public function numCheckedOut(bool $recalculate = false)
242242
{
243-
$checkedout = 0;
243+
/**
244+
*
245+
* WARNING: This method caches the result, so if you're doing something
246+
* that is going to change the number of checked-out items, make sure to pass
247+
* 'true' as the first parameter to force this to recalculate the number of checked-out
248+
* items!!!!!
249+
*
250+
*/
244251

245252
// In case there are elements checked out to assets that belong to a different company
246253
// than this asset and full multiple company support is on we'll remove the global scope,
247254
// so they are included in the count.
248-
return $this->uncontrainedAssets->sum('pivot.assigned_qty');
255+
if (is_null($this->sum_unconstrained_assets) || $recalculate) {
256+
// This, in a components-listing context, is mostly important for when it sets a 'zero' which
257+
// is *not* null - so we don't have to keep recalculating for un-checked-out components
258+
$this->sum_unconstrained_assets = $this->uncontrainedAssets()->sum('assigned_qty') ?? 0;
259+
}
260+
return $this->sum_unconstrained_assets;
249261
}
250262

251263

0 commit comments

Comments
 (0)