Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app/Http/Controllers/Api/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 15 additions & 3 deletions app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
Loading