Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bdsumon4u committed Sep 1, 2024
2 parents 47e50f6 + ffc84d2 commit 18cc9c7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
20 changes: 15 additions & 5 deletions app/Http/Controllers/Admin/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,26 @@ public function index()
$orderQ->where('admin_id', request('staff_id'));
}

$productInOrders[] = [];

$products = (clone $orderQ)->get()
->whereIn('status', ['CONFIRMED', 'INVOICED', 'SHIPPING'])
->flatMap(fn ($order) => json_decode(json_encode($order->products, JSON_UNESCAPED_UNICODE), true))
->groupBy('id')->map(function ($item) {
return [
->flatMap(function ($order) use (&$productInOrders) {
$products = json_decode(json_encode($order->products, JSON_UNESCAPED_UNICODE), true);

foreach ($products as $product) {
$productInOrders[$product['name']][$order->id] = 1 + ($productInOrders[$product['name']][$order->id] ?? 0);
}

return $products;
})
->groupBy('id')->mapWithKeys(function ($item, $id) {
return [$id => [
'name' => $item->random()['name'],
'slug' => $item->random()['slug'],
'quantity' => $item->sum('quantity'),
'total' => $item->sum('total'),
];
]];
})->sortByDesc('quantity')->toArray();

$data = (clone $orderQ)
Expand Down Expand Up @@ -81,6 +91,6 @@ public function index()
$productsCount = Product::whereNull('parent_id')->count();
$inactiveProducts = Product::whereIsActive(0)->whereNull('parent_id')->get();
$lowStockProducts = Product::whereShouldTrack(1)->where('stock_count', '<', 10)->get();
return view('admin.dashboard', compact('staffs', 'products', 'productsCount', 'orders', 'amounts', 'inactiveProducts', 'lowStockProducts', 'start', 'end'));
return view('admin.dashboard', compact('staffs', 'products', 'productInOrders', 'productsCount', 'orders', 'amounts', 'inactiveProducts', 'lowStockProducts', 'start', 'end'));
}
}
35 changes: 24 additions & 11 deletions app/Http/Controllers/Admin/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,35 @@ public function filter(Request $request)
$amounts[$status] = $data->total_amount ?? 0;
}

$productInOrders[] = [];

$products = $orderQ
->when($request->status, fn($q) => $q->where('status', $request->status))->get()
->flatMap(function ($order) use (&$productInOrders) {
$products = json_decode(json_encode($order->products, JSON_UNESCAPED_UNICODE), true);

foreach ($products as $product) {
$productInOrders[$product['name']][$order->id] = 1 + ($productInOrders[$product['name']][$order->id] ?? 0);
}

return $products;
})
->groupBy('id')->map(function ($item) {
return [
'name' => $item->random()['name'],
'slug' => $item->random()['slug'],
'quantity' => $item->sum('quantity'),
'total' => $item->sum('total'),
];
})->sortByDesc('quantity')->toArray();

return view('admin.orders.filter', [
'start' => $start,
'end' => $end,
'orders' => $orders,
'amounts' => $amounts,
'products' => $orderQ
->when($request->status, fn ($q) => $q->where('status', $request->status))->get()
->flatMap(fn ($order) => json_decode(json_encode($order->products, JSON_UNESCAPED_UNICODE), true))
->groupBy('id')->map(function ($item) {
return [
'name' => $item->random()['name'],
'slug' => $item->random()['slug'],
'quantity' => $item->sum('quantity'),
'total' => $item->sum('total'),
];
})->sortByDesc('quantity')->toArray(),
'products' => $products,
'productInOrders' => $productInOrders,
]);
}

Expand Down
8 changes: 6 additions & 2 deletions resources/views/admin/reports/filtered.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@
<tr>
<th style="min-width: 50px;">SI</th>
<th style="min-width: 120px;">Name</th>
<th style="min-width: 100px;">Orders</th>
<th style="min-width: 100px;">Quantity</th>
<th style="min-width: 100px;">Price</th>
</tr>
</thead>
@php $total = 0; $amount = 0; @endphp
@php $total = 0; $amount = 0; $orders = 0; @endphp
<tbody>
@foreach ($products as $id => $product)
@php $total += $product['quantity']; $amount += $product['total']; @endphp
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $product['name'] }}</td>
<td>{{ count($productInOrders[$product['name']] ?? []) }}</td>
<td>{{ $product['quantity'] }}</td>
<td>{!!theMoney($product['total'])!!}</td>
</tr>
@php $orders += count($productInOrders[$product['name']] ?? []); @endphp
@endforeach
</tbody>
<tfoot>
<th colspan="2" class="text-right">Total</th>
<th class="text-right">Total</th>
<th>{{ $orders }}</th>
<th>{{ $total }}</th>
<th>{!!theMoney($amount)!!}</th>
</tfoot>
Expand Down

0 comments on commit 18cc9c7

Please sign in to comment.