From 9cfb5b75a7f16c5d999e55d303c8c1b1c36d0bca Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 11 Nov 2025 15:58:49 +0700 Subject: [PATCH] refactor(chart): prop outlets dikirim dari parent --- app/Livewire/Chart/ChannelRevenue.php | 10 ++++------ app/Livewire/Chart/OutletExpense.php | 7 +++---- app/Livewire/Chart/OutletRevenue.php | 11 +++++------ app/Livewire/Chart/OutletRevenueExpense.php | 6 ++---- .../livewire/studio/dashboard/analysis.blade.php | 8 ++++---- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/app/Livewire/Chart/ChannelRevenue.php b/app/Livewire/Chart/ChannelRevenue.php index f722f0f..0ad70b2 100644 --- a/app/Livewire/Chart/ChannelRevenue.php +++ b/app/Livewire/Chart/ChannelRevenue.php @@ -16,19 +16,15 @@ class ChannelRevenue extends Component public array $borderColors = []; - public function mount() + public function mount(array $outlets) { - $outlets = auth()->user()->outlets()->pluck('name', 'outlets.id'); - $orders = Order::select('channel', 'total') - ->whereIn('outlet_id', $outlets->keys()) + ->whereIn('outlet_id', array_keys($outlets)) ->get() ->groupBy('channel'); $data = []; - $count = count(OrderChannel::cases()); - foreach (OrderChannel::cases() as $channel) { $channelOrders = $orders[$channel->value] ?? collect(); $total = $channelOrders->sum('total'); @@ -39,6 +35,8 @@ public function mount() $this->revenues = $data; + // random colors + $count = count(OrderChannel::cases()); $this->backgroundColors = randomColors($count, 0.2); $this->borderColors = randomColors($count, 1.0); } diff --git a/app/Livewire/Chart/OutletExpense.php b/app/Livewire/Chart/OutletExpense.php index af22ad0..be269a1 100644 --- a/app/Livewire/Chart/OutletExpense.php +++ b/app/Livewire/Chart/OutletExpense.php @@ -17,10 +17,8 @@ class OutletExpense extends Component public array $borderColors = []; - public function mount() + public function mount(array $outlets) { - $outlets = auth()->user()->outlets()->pluck('name', 'outlets.id'); - $expensesData = []; foreach ($outlets as $id => $name) { @@ -35,10 +33,11 @@ public function mount() $expensesData[$name] = $total; } - $this->outlets = array_keys($expensesData); + $this->outlets = array_values($outlets); $this->expenses = array_values($expensesData); + // random colors $count = count($this->outlets); $this->backgroundColors = randomColors($count, 0.2); $this->borderColors = randomColors($count, 1.0); diff --git a/app/Livewire/Chart/OutletRevenue.php b/app/Livewire/Chart/OutletRevenue.php index 41226a9..bcf28da 100644 --- a/app/Livewire/Chart/OutletRevenue.php +++ b/app/Livewire/Chart/OutletRevenue.php @@ -15,20 +15,19 @@ class OutletRevenue extends Component public array $borderColors = []; - public function mount() + public function mount(array $outlets) { - $outlets = auth()->user()->outlets()->pluck('name', 'outlets.id'); - $revenues = DB::table('orders') ->select('outlet_id', DB::raw('SUM(total) as total_revenue')) - ->whereIn('outlet_id', $outlets->keys()) + ->whereIn('outlet_id', array_keys($outlets)) ->groupBy('outlet_id') ->pluck('total_revenue', 'outlet_id'); - $this->outlets = $outlets->values()->toArray(); + $this->outlets = array_values($outlets); - $this->revenues = $outlets->map(fn ($name, $id) => $revenues[$id] ?? 0)->values()->toArray(); + $this->revenues = collect($outlets)->map(fn ($name, $id) => $revenues[$id] ?? 0)->values()->toArray(); + // random colors $count = count($this->outlets); $this->backgroundColors = randomColors($count, 0.2); $this->borderColors = randomColors($count, 1.0); diff --git a/app/Livewire/Chart/OutletRevenueExpense.php b/app/Livewire/Chart/OutletRevenueExpense.php index 87a0052..7d5db4b 100644 --- a/app/Livewire/Chart/OutletRevenueExpense.php +++ b/app/Livewire/Chart/OutletRevenueExpense.php @@ -16,10 +16,8 @@ class OutletRevenueExpense extends Component public array $expenses = []; - public function mount() + public function mount(array $outlets) { - $outlets = auth()->user()->outlets()->pluck('name', 'outlets.id'); - $revenueData = []; $expenseData = []; @@ -34,7 +32,7 @@ public function mount() $expenseData[$name] = $expenseTotal + $purchaseTotal + $payrollTotal; } - $this->outlets = $outlets->values()->toArray(); + $this->outlets = array_values($outlets); $this->revenues = array_values($revenueData); diff --git a/resources/views/livewire/studio/dashboard/analysis.blade.php b/resources/views/livewire/studio/dashboard/analysis.blade.php index 74b4f88..af181d3 100644 --- a/resources/views/livewire/studio/dashboard/analysis.blade.php +++ b/resources/views/livewire/studio/dashboard/analysis.blade.php @@ -127,10 +127,10 @@
- - - + + +
- +