diff --git a/app/Livewire/Studio/Dashboard/Analysis.php b/app/Livewire/Studio/Dashboard/Analysis.php index a5eb955..696b563 100644 --- a/app/Livewire/Studio/Dashboard/Analysis.php +++ b/app/Livewire/Studio/Dashboard/Analysis.php @@ -23,6 +23,8 @@ #[Title('Analisa')] class Analysis extends Component { + public string $period = 'all'; + public array $outlets = []; public array $selectedOutletIds = []; @@ -46,6 +48,11 @@ public function mount() $this->loadData(); } + public function updatedPeriod(string $period) + { + $this->loadData(); + } + public function updatedSelectedOutletIds() { $this->loadData(); @@ -54,108 +61,185 @@ public function updatedSelectedOutletIds() protected function loadData() { $outletIds = $this->selectedOutletIds; + $period = $this->period; + + $dateQuery = match ($period) { + '7' => now()->subDays(1), + '30' => now()->subDays(30), + '90' => now()->subDays(90), + '365' => now()->subDays(365), + default => null, + }; $this->countingData = [ [ 'title' => 'Outlet', - 'value' => Outlet::count(), + 'value' => Outlet::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), ], [ 'title' => 'Pegawai', - 'value' => User::whereHas('employee')->when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds)))->count(), + '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)) + ->count(), ], [ 'title' => 'Tier', - 'value' => Tier::count(), + 'value' => Tier::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), ], [ 'title' => 'Voucher Aktif', - 'value' => Voucher::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds)))->active()->count(), + '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)) + ->active() + ->count(), ], [ 'title' => 'Member', - 'value' => User::whereHas('customer')->when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds)))->count(), + '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)) + ->count(), ], [ 'title' => 'Customer', - 'value' => Customer::whereDoesntHave('user')->count(), + 'value' => Customer::whereDoesntHave('user') + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->count(), ], [ 'title' => 'Kategori', - 'value' => Category::count(), + 'value' => Category::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), ], [ 'title' => 'Merek', - 'value' => Brand::count(), + 'value' => Brand::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), ], [ 'title' => 'Parfum', - 'value' => Perfume::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds)))->count(), + '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)) + ->count(), ], [ 'title' => 'Produk', - 'value' => Product::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds)))->count(), + '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)) + ->count(), ], [ 'title' => 'Botol', - 'value' => Bottle::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds)))->count(), + '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)) + ->count(), ], ]; $this->orders = [ [ 'title' => 'Total Order', - 'value' => currency(Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds))->count()), + 'value' => currency( + Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->count() + ), ], [ 'title' => 'Pendapatan', - 'value' => currency(Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds))->sum('total'), 'Rp'), + 'value' => currency( + Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->sum('total'), + 'Rp' + ), ], [ 'title' => 'HPP', - 'value' => currency(Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds))->sum('cogs'), 'Rp'), + 'value' => currency( + Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->sum('cogs'), + 'Rp' + ), ], [ 'title' => 'Diskon', - 'value' => currency(Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds))->sum('discount'), 'Rp'), + 'value' => currency( + Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->sum('discount'), + 'Rp' + ), ], [ 'title' => 'Voucher Terpakai', - 'value' => currency(Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds))->whereNotNull('voucher_id')->count()), + 'value' => currency( + Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->whereNotNull('voucher_id')->count() + ), ], [ '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)))->sum('quantity')).' ml', + '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))) + ->sum('quantity') + ).' ml', ], [ '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)))->sum('quantity')).' pcs', + '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))) + ->sum('quantity') + ).' pcs', ], [ '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)))->sum('quantity')).' pcs', + '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))) + ->sum('quantity') + ).' pcs', ], ]; $this->expenses = [ [ 'title' => 'Beban Toko', - 'value' => currency(Expense::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds))->sum('amount'), 'Rp'), + 'value' => currency( + Expense::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->sum('amount'), + 'Rp' + ), ], [ 'title' => 'Belanja Barang', - 'value' => currency(Purchase::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds))->sum('total'), 'Rp'), + 'value' => currency( + Purchase::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) + ->sum('total'), + 'Rp' + ), ], [ 'title' => 'Penggajian', - 'value' => currency(Payroll::when(! empty($outletIds), fn ($q) => $q->whereHas('user.outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds)))->sum('total_salary'), 'Rp'), + '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)) + ->sum('total_salary'), + 'Rp' + ), ], ]; - $this->topPerfumes = Perfume::with(['items' => function ($query) use ($outletIds) { + $this->topPerfumes = Perfume::with(['items' => function ($query) use ($outletIds, $dateQuery) { $query->whereNotNull('order_id') - ->whereHas('order', function ($q) use ($outletIds) { - $q->when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)); + ->whereHas('order', function ($q) use ($outletIds, $dateQuery) { + $q->when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)); }); }]) ->select('id', 'name', 'sale_price') @@ -171,10 +255,11 @@ protected function loadData() ->take(5) ->toArray(); - $this->topProducts = Product::with(['items' => function ($query) use ($outletIds) { + $this->topProducts = Product::with(['items' => function ($query) use ($outletIds, $dateQuery) { $query->whereNotNull('order_id') - ->whereHas('order', function ($q) use ($outletIds) { - $q->when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)); + ->whereHas('order', function ($q) use ($outletIds, $dateQuery) { + $q->when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)); }); }]) ->select('id', 'name', 'sale_price') @@ -190,10 +275,11 @@ protected function loadData() ->take(5) ->toArray(); - $this->topBottles = Bottle::with(['items' => function ($query) use ($outletIds) { + $this->topBottles = Bottle::with(['items' => function ($query) use ($outletIds, $dateQuery) { $query->whereNotNull('order_id') - ->whereHas('order', function ($q) use ($outletIds) { - $q->when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)); + ->whereHas('order', function ($q) use ($outletIds, $dateQuery) { + $q->when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) + ->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)); }); }]) ->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 af181d3..4aabb0c 100644 --- a/resources/views/livewire/studio/dashboard/analysis.blade.php +++ b/resources/views/livewire/studio/dashboard/analysis.blade.php @@ -3,12 +3,25 @@
-
- - @foreach ($outlets as $key => $value) - - @endforeach - +
+
+
+ + + + + + + +
+ + + + @foreach ($outlets as $key => $value) + + @endforeach + +
Counting Data