From 95779ff4c806e18517afe587de2c41b53ac3b9fd Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 11 Nov 2025 23:33:17 +0700 Subject: [PATCH] feat(analysis): membuat filter by date --- app/Livewire/Studio/Dashboard/Analysis.php | 89 ++++++++++++------- .../studio/dashboard/analysis.blade.php | 10 +++ 2 files changed, 68 insertions(+), 31 deletions(-) diff --git a/app/Livewire/Studio/Dashboard/Analysis.php b/app/Livewire/Studio/Dashboard/Analysis.php index 696b563..4074543 100644 --- a/app/Livewire/Studio/Dashboard/Analysis.php +++ b/app/Livewire/Studio/Dashboard/Analysis.php @@ -17,6 +17,7 @@ use App\Models\Tier; use App\Models\User; use App\Models\Voucher; +use Carbon\Carbon; use Livewire\Attributes\Title; use Livewire\Component; @@ -27,6 +28,11 @@ class Analysis extends Component public array $outlets = []; + public array $range = [ + 'start' => null, + 'end' => null, + ]; + public array $selectedOutletIds = []; public array $countingData = []; @@ -58,10 +64,16 @@ public function updatedSelectedOutletIds() $this->loadData(); } + public function updatedRange($range) + { + $this->loadData(); + } + protected function loadData() { $outletIds = $this->selectedOutletIds; $period = $this->period; + $range = $this->range; $dateQuery = match ($period) { '7' => now()->subDays(1), @@ -71,26 +83,41 @@ protected function loadData() default => null, }; + $startDate = ! empty($range['start']) ? Carbon::parse($range['start'])->startOfDay() : null; + $endDate = ! empty($range['end']) ? Carbon::parse($range['end'])->endOfDay() : null; + + if ($startDate && $endDate) { + $dateQuery = null; + } + + $filterDate = function ($q) use ($dateQuery, $startDate, $endDate) { + if ($startDate && $endDate) { + $q->whereBetween('created_at', [$startDate, $endDate]); + } elseif ($dateQuery) { + $q->where('created_at', '>=', $dateQuery); + } + }; + $this->countingData = [ [ 'title' => 'Outlet', - 'value' => Outlet::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), + 'value' => Outlet::when($dateQuery || ($startDate && $endDate), $filterDate)->count(), ], [ 'title' => 'Pegawai', 'value' => User::whereHas('employee') ->when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->count(), ], [ 'title' => 'Tier', - 'value' => Tier::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), + 'value' => Tier::when($dateQuery || ($startDate && $endDate), $filterDate)->count(), ], [ 'title' => 'Voucher Aktif', 'value' => Voucher::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->active() ->count(), ], @@ -98,39 +125,39 @@ protected function loadData() 'title' => 'Member', 'value' => User::whereHas('customer') ->when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->count(), ], [ 'title' => 'Customer', 'value' => Customer::whereDoesntHave('user') - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->count(), ], [ 'title' => 'Kategori', - 'value' => Category::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), + 'value' => Category::when($dateQuery || ($startDate && $endDate), $filterDate)->count(), ], [ 'title' => 'Merek', - 'value' => Brand::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), + 'value' => Brand::when($dateQuery || ($startDate && $endDate), $filterDate)->count(), ], [ 'title' => 'Parfum', 'value' => Perfume::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->count(), ], [ 'title' => 'Produk', 'value' => Product::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->count(), ], [ 'title' => 'Botol', 'value' => Bottle::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->count(), ], ]; @@ -140,7 +167,7 @@ protected function loadData() 'title' => 'Total Order', 'value' => currency( Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->count() ), ], @@ -148,7 +175,7 @@ protected function loadData() 'title' => 'Pendapatan', 'value' => currency( Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->sum('total'), 'Rp' ), @@ -157,7 +184,7 @@ protected function loadData() 'title' => 'HPP', 'value' => currency( Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->sum('cogs'), 'Rp' ), @@ -166,7 +193,7 @@ protected function loadData() 'title' => 'Diskon', 'value' => currency( Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->sum('discount'), 'Rp' ), @@ -175,7 +202,7 @@ protected function loadData() 'title' => 'Voucher Terpakai', 'value' => currency( Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->whereNotNull('voucher_id')->count() ), ], @@ -183,7 +210,7 @@ protected function loadData() 'title' => 'Parfum Terjual', 'value' => currency( OrderItem::where('orderable_type', Perfume::class) - ->whereHas('order', fn ($q) => $q->when(! empty($outletIds), fn ($q2) => $q2->whereIn('outlet_id', $outletIds))->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))) + ->whereHas('order', fn ($q) => $q->when(! empty($outletIds), fn ($q2) => $q2->whereIn('outlet_id', $outletIds))->when($dateQuery || ($startDate && $endDate), $filterDate)) ->sum('quantity') ).' ml', ], @@ -191,7 +218,7 @@ protected function loadData() 'title' => 'Produk Terjual', 'value' => currency( OrderItem::where('orderable_type', Product::class) - ->whereHas('order', fn ($q) => $q->when(! empty($outletIds), fn ($q2) => $q2->whereIn('outlet_id', $outletIds))->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))) + ->whereHas('order', fn ($q) => $q->when(! empty($outletIds), fn ($q2) => $q2->whereIn('outlet_id', $outletIds))->when($dateQuery || ($startDate && $endDate), $filterDate)) ->sum('quantity') ).' pcs', ], @@ -199,7 +226,7 @@ protected function loadData() 'title' => 'Botol Terjual', 'value' => currency( OrderItem::where('orderable_type', Bottle::class) - ->whereHas('order', fn ($q) => $q->when(! empty($outletIds), fn ($q2) => $q2->whereIn('outlet_id', $outletIds))->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))) + ->whereHas('order', fn ($q) => $q->when(! empty($outletIds), fn ($q2) => $q2->whereIn('outlet_id', $outletIds))->when($dateQuery || ($startDate && $endDate), $filterDate)) ->sum('quantity') ).' pcs', ], @@ -210,7 +237,7 @@ protected function loadData() 'title' => 'Beban Toko', 'value' => currency( Expense::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->sum('amount'), 'Rp' ), @@ -219,7 +246,7 @@ protected function loadData() 'title' => 'Belanja Barang', 'value' => currency( Purchase::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->sum('total'), 'Rp' ), @@ -228,18 +255,18 @@ protected function loadData() 'title' => 'Penggajian', 'value' => currency( Payroll::when(! empty($outletIds), fn ($q) => $q->whereHas('user.outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->when($dateQuery || ($startDate && $endDate), $filterDate) ->sum('total_salary'), 'Rp' ), ], ]; - $this->topPerfumes = Perfume::with(['items' => function ($query) use ($outletIds, $dateQuery) { + $this->topPerfumes = Perfume::with(['items' => function ($query) use ($outletIds, $dateQuery, $startDate, $endDate, $filterDate) { $query->whereNotNull('order_id') - ->whereHas('order', function ($q) use ($outletIds, $dateQuery) { + ->whereHas('order', function ($q) use ($outletIds, $dateQuery, $startDate, $endDate, $filterDate) { $q->when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)); + ->when($dateQuery || ($startDate && $endDate), $filterDate); }); }]) ->select('id', 'name', 'sale_price') @@ -255,11 +282,11 @@ protected function loadData() ->take(5) ->toArray(); - $this->topProducts = Product::with(['items' => function ($query) use ($outletIds, $dateQuery) { + $this->topProducts = Product::with(['items' => function ($query) use ($outletIds, $dateQuery, $startDate, $endDate, $filterDate) { $query->whereNotNull('order_id') - ->whereHas('order', function ($q) use ($outletIds, $dateQuery) { + ->whereHas('order', function ($q) use ($outletIds, $dateQuery, $startDate, $endDate, $filterDate) { $q->when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)); + ->when($dateQuery || ($startDate && $endDate), $filterDate); }); }]) ->select('id', 'name', 'sale_price') @@ -275,11 +302,11 @@ protected function loadData() ->take(5) ->toArray(); - $this->topBottles = Bottle::with(['items' => function ($query) use ($outletIds, $dateQuery) { + $this->topBottles = Bottle::with(['items' => function ($query) use ($outletIds, $dateQuery, $startDate, $endDate, $filterDate) { $query->whereNotNull('order_id') - ->whereHas('order', function ($q) use ($outletIds, $dateQuery) { + ->whereHas('order', function ($q) use ($outletIds, $dateQuery, $startDate, $endDate, $filterDate) { $q->when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) - ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)); + ->when($dateQuery || ($startDate && $endDate), $filterDate); }); }]) ->select('id', 'name', 'sale_price') diff --git a/resources/views/livewire/studio/dashboard/analysis.blade.php b/resources/views/livewire/studio/dashboard/analysis.blade.php index 4aabb0c..e77ebb0 100644 --- a/resources/views/livewire/studio/dashboard/analysis.blade.php +++ b/resources/views/livewire/studio/dashboard/analysis.blade.php @@ -21,6 +21,16 @@ @endforeach + + + + +
+ + +
+
+