Skip to content

Commit fee557f

Browse files
committed
feat: Filter products by sales ID and optimize stock item loading by removing unnecessary eager loads.
1 parent eb7d2d7 commit fee557f

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

app/Http/Controllers/ProductController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ public function index(Request $request)
1414
{
1515
$query = Product::with(['category', 'recipe.item', 'files']);
1616

17-
if (!$request->has('all')) {
18-
$query->where('soldout', 0);
17+
if ($request->has('sales_id')) {
18+
$branch = Sales::find($request->sales_id)->branch_id;
19+
$query->whereHas('branches', function($bQuery) use ($branch) {
20+
$bQuery->where('branches.id', $branch)
21+
->where('branch_product.is_active', true);
22+
});
1923
}
2024

2125
if ($request->has('category')) {

app/Http/Controllers/StockController.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function index(Request $request)
2424
if ($request->has('storage')) {
2525
$query->where('storage', $request->storage);
2626
}
27-
$stocks = $query->with(['branch', 'ingredient', 'utility'])->get();
27+
$stocks = $query->with(['branch'])->get();
2828

2929
return response()->json([
3030
'err' => 0,
@@ -40,7 +40,7 @@ public function getStockMutation(Request $request) {
4040
$end = $request->end . ' 23:59:59';
4141

4242
// 1. Get the relevant stock items
43-
$query = Stock::with(['ingredient', 'utility']);
43+
$query = Stock::query();
4444

4545
if ($branch != 0) {
4646
$query->where('branch_id', $branch)->where('storage', $storage);
@@ -74,16 +74,12 @@ public function getStockMutation(Request $request) {
7474

7575
return [
7676
'item_code' => $first->item_code,
77-
'item_name' => $first->item_type === 'INGR'
78-
? $first->ingredient->name
79-
: $first->utility->name,
77+
'item_name' => $first->item_name,
8078
'opening' => (float)$opening,
8179
'qty_in' => (float)$in,
8280
'qty_out' => (float)$out,
8381
'closing' => (float)($opening + $in - $out),
84-
'unit' => $first->item_type === 'INGR'
85-
? $first->ingredient->unit
86-
: 'pcs'
82+
'unit' => $first->unit || 'pcs'
8783
];
8884
})->values();
8985

@@ -283,7 +279,7 @@ public function getStockCard(Request $request)
283279

284280
public function kitchenRequest(Request $request)
285281
{
286-
$request = KitchenRequest::with('from_branch', 'to_branch', 'items', 'items.ingredient', 'items.utility')->get();
282+
$request = KitchenRequest::with(['from_branch', 'to_branch', 'items'])->get();
287283
return response()->json([
288284
'err' => 0,
289285
'msg' => '',

0 commit comments

Comments
 (0)