refactor: import enum classes and apply PSR-12 formatting to DashboardController
This commit is contained in:
parent
08cb1a5d78
commit
2b43213ee4
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Enums\OrderChannel;
|
||||||
|
use App\Enums\OrderStatus;
|
||||||
|
use App\Enums\PaymentMethod;
|
||||||
use App\Models\Expense;
|
use App\Models\Expense;
|
||||||
use App\Models\Order;
|
use App\Models\Order;
|
||||||
use App\Models\OrderItem;
|
use App\Models\OrderItem;
|
||||||
@ -36,9 +39,9 @@ public function __invoke()
|
|||||||
// Chart Data
|
// Chart Data
|
||||||
$stats['charts'] = [
|
$stats['charts'] = [
|
||||||
'jam_sibuk' => $this->getJamSibuk($today, $yesterday),
|
'jam_sibuk' => $this->getJamSibuk($today, $yesterday),
|
||||||
'order_by_payment' => $this->getOrderByEnum($today, 'payment_method', \App\Enums\PaymentMethod::class),
|
'order_by_payment' => $this->getOrderByEnum($today, 'payment_method', PaymentMethod::class),
|
||||||
'order_by_status' => $this->getOrderByEnum($today, 'order_status', \App\Enums\OrderStatus::class),
|
'order_by_status' => $this->getOrderByEnum($today, 'order_status', OrderStatus::class),
|
||||||
'order_by_channel' => $this->getOrderByEnum($today, 'order_channel', \App\Enums\OrderChannel::class),
|
'order_by_channel' => $this->getOrderByEnum($today, 'order_channel', OrderChannel::class),
|
||||||
'top_products' => $this->getTopProducts($today),
|
'top_products' => $this->getTopProducts($today),
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -80,9 +83,10 @@ private function getOrderByEnum($date, $column, $enumClass)
|
|||||||
->groupBy($column)
|
->groupBy($column)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
return $data->map(function($item) use ($column, $enumClass) {
|
return $data->map(function ($item) use ($column, $enumClass) {
|
||||||
$enumValue = $item->$column;
|
$enumValue = $item->$column;
|
||||||
$label = $enumValue instanceof $enumClass ? $enumValue->label() : $enumValue;
|
$label = $enumValue instanceof $enumClass ? $enumValue->label() : $enumValue;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'name' => $label,
|
'name' => $label,
|
||||||
'value' => $item->count,
|
'value' => $item->count,
|
||||||
@ -92,14 +96,14 @@ private function getOrderByEnum($date, $column, $enumClass)
|
|||||||
|
|
||||||
private function getTopProducts($date)
|
private function getTopProducts($date)
|
||||||
{
|
{
|
||||||
return OrderItem::whereHas('order', fn($q) => $q->whereDate('created_at', $date))
|
return OrderItem::whereHas('order', fn ($q) => $q->whereDate('created_at', $date))
|
||||||
->with('product:id,name')
|
->with('product:id,name')
|
||||||
->selectRaw('product_id, sum(qty) as total_qty')
|
->selectRaw('product_id, sum(qty) as total_qty')
|
||||||
->groupBy('product_id')
|
->groupBy('product_id')
|
||||||
->orderByDesc('total_qty')
|
->orderByDesc('total_qty')
|
||||||
->limit(5)
|
->limit(5)
|
||||||
->get()
|
->get()
|
||||||
->map(fn($item) => [
|
->map(fn ($item) => [
|
||||||
'name' => $item->product->name ?? 'Unknown',
|
'name' => $item->product->name ?? 'Unknown',
|
||||||
'qty' => (int) $item->total_qty,
|
'qty' => (int) $item->total_qty,
|
||||||
]);
|
]);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user