mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-09-14 22:37:03 -04:00
fix(items): add support for sorting by quantity across all locations
- Implement conditional logic to calculate and sort by the sum of quantities across all locations when `stock_location_id` is invalid. - Adjust query joins and select clauses to handle the new sorting behavior. - Maintain existing sorting logic for other cases to ensure backward compatibility. Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
This commit is contained in:
1 parent
09115461b1
commit
d01ea38b69
1 file changed
+17
-2
+17
-2
@@ -257,7 +257,12 @@ class Item extends Model
|
||||
}
|
||||
|
||||
// Order by name of item by default
|
||||
$idBuilder->orderBy($sort, $order);
|
||||
if ($sort === 'quantity' && $filters['stock_location_id'] <= -1) {
|
||||
$idBuilder->join('item_quantities AS item_quantities', 'item_quantities.item_id = items.item_id', 'left');
|
||||
$idBuilder->orderBy('SUM(item_quantities.quantity)', $order);
|
||||
} else {
|
||||
$idBuilder->orderBy($sort, $order);
|
||||
}
|
||||
|
||||
if ($rows > 0) {
|
||||
$idBuilder->limit($rows, $limitFrom);
|
||||
@@ -302,10 +307,14 @@ class Item extends Model
|
||||
$builder->select('MAX(inventory.trans_location) AS trans_location');
|
||||
$builder->select('MAX(inventory.trans_inventory) AS trans_inventory');
|
||||
|
||||
$sortByQuantityAllLocations = $sort === 'quantity' && $filters['stock_location_id'] <= -1;
|
||||
|
||||
if ($filters['stock_location_id'] > -1) {
|
||||
$builder->select('MAX(item_quantities.item_id) AS qty_item_id');
|
||||
$builder->select('MAX(item_quantities.location_id) AS location_id');
|
||||
$builder->select('MAX(item_quantities.quantity) AS quantity');
|
||||
} elseif ($sortByQuantityAllLocations) {
|
||||
$builder->select('SUM(item_quantities.quantity) AS quantity');
|
||||
}
|
||||
|
||||
$builder->join('suppliers AS suppliers', 'suppliers.person_id = items.supplier_id', 'left');
|
||||
@@ -314,6 +323,8 @@ class Item extends Model
|
||||
if ($filters['stock_location_id'] > -1) {
|
||||
$builder->join('item_quantities AS item_quantities', 'item_quantities.item_id = items.item_id');
|
||||
$builder->where('location_id', $filters['stock_location_id']);
|
||||
} elseif ($sortByQuantityAllLocations) {
|
||||
$builder->join('item_quantities AS item_quantities', 'item_quantities.item_id = items.item_id', 'left');
|
||||
}
|
||||
|
||||
$applyTransDateRange($builder);
|
||||
@@ -332,7 +343,11 @@ class Item extends Model
|
||||
$builder->groupBy('items.item_id');
|
||||
|
||||
// Re-apply order: WHERE...IN + GROUP BY do not preserve Phase A's row order
|
||||
$builder->orderBy($sort, $order);
|
||||
if ($sortByQuantityAllLocations) {
|
||||
$builder->orderBy('SUM(item_quantities.quantity)', $order);
|
||||
} else {
|
||||
$builder->orderBy($sort, $order);
|
||||
}
|
||||
|
||||
return $builder->get();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user