feat(analysis): membuat filter by date

This commit is contained in:
Yoga Pangestu 2025-11-11 23:33:17 +07:00
parent 00341e13d0
commit 95779ff4c8
2 changed files with 68 additions and 31 deletions

View File

@ -17,6 +17,7 @@
use App\Models\Tier; use App\Models\Tier;
use App\Models\User; use App\Models\User;
use App\Models\Voucher; use App\Models\Voucher;
use Carbon\Carbon;
use Livewire\Attributes\Title; use Livewire\Attributes\Title;
use Livewire\Component; use Livewire\Component;
@ -27,6 +28,11 @@ class Analysis extends Component
public array $outlets = []; public array $outlets = [];
public array $range = [
'start' => null,
'end' => null,
];
public array $selectedOutletIds = []; public array $selectedOutletIds = [];
public array $countingData = []; public array $countingData = [];
@ -58,10 +64,16 @@ public function updatedSelectedOutletIds()
$this->loadData(); $this->loadData();
} }
public function updatedRange($range)
{
$this->loadData();
}
protected function loadData() protected function loadData()
{ {
$outletIds = $this->selectedOutletIds; $outletIds = $this->selectedOutletIds;
$period = $this->period; $period = $this->period;
$range = $this->range;
$dateQuery = match ($period) { $dateQuery = match ($period) {
'7' => now()->subDays(1), '7' => now()->subDays(1),
@ -71,26 +83,41 @@ protected function loadData()
default => null, 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 = [ $this->countingData = [
[ [
'title' => 'Outlet', 'title' => 'Outlet',
'value' => Outlet::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), 'value' => Outlet::when($dateQuery || ($startDate && $endDate), $filterDate)->count(),
], ],
[ [
'title' => 'Pegawai', 'title' => 'Pegawai',
'value' => User::whereHas('employee') 'value' => User::whereHas('employee')
->when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) ->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(), ->count(),
], ],
[ [
'title' => 'Tier', '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', 'title' => 'Voucher Aktif',
'value' => Voucher::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) '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() ->active()
->count(), ->count(),
], ],
@ -98,39 +125,39 @@ protected function loadData()
'title' => 'Member', 'title' => 'Member',
'value' => User::whereHas('customer') 'value' => User::whereHas('customer')
->when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) ->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(), ->count(),
], ],
[ [
'title' => 'Customer', 'title' => 'Customer',
'value' => Customer::whereDoesntHave('user') 'value' => Customer::whereDoesntHave('user')
->when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery)) ->when($dateQuery || ($startDate && $endDate), $filterDate)
->count(), ->count(),
], ],
[ [
'title' => 'Kategori', 'title' => 'Kategori',
'value' => Category::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), 'value' => Category::when($dateQuery || ($startDate && $endDate), $filterDate)->count(),
], ],
[ [
'title' => 'Merek', 'title' => 'Merek',
'value' => Brand::when($dateQuery, fn ($q) => $q->where('created_at', '>=', $dateQuery))->count(), 'value' => Brand::when($dateQuery || ($startDate && $endDate), $filterDate)->count(),
], ],
[ [
'title' => 'Parfum', 'title' => 'Parfum',
'value' => Perfume::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) '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(), ->count(),
], ],
[ [
'title' => 'Produk', 'title' => 'Produk',
'value' => Product::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) '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(), ->count(),
], ],
[ [
'title' => 'Botol', 'title' => 'Botol',
'value' => Bottle::when(! empty($outletIds), fn ($q) => $q->whereHas('outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) '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(), ->count(),
], ],
]; ];
@ -140,7 +167,7 @@ protected function loadData()
'title' => 'Total Order', 'title' => 'Total Order',
'value' => currency( 'value' => currency(
Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) 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() ->count()
), ),
], ],
@ -148,7 +175,7 @@ protected function loadData()
'title' => 'Pendapatan', 'title' => 'Pendapatan',
'value' => currency( 'value' => currency(
Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) 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'), ->sum('total'),
'Rp' 'Rp'
), ),
@ -157,7 +184,7 @@ protected function loadData()
'title' => 'HPP', 'title' => 'HPP',
'value' => currency( 'value' => currency(
Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) 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'), ->sum('cogs'),
'Rp' 'Rp'
), ),
@ -166,7 +193,7 @@ protected function loadData()
'title' => 'Diskon', 'title' => 'Diskon',
'value' => currency( 'value' => currency(
Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) 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'), ->sum('discount'),
'Rp' 'Rp'
), ),
@ -175,7 +202,7 @@ protected function loadData()
'title' => 'Voucher Terpakai', 'title' => 'Voucher Terpakai',
'value' => currency( 'value' => currency(
Order::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) 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() ->whereNotNull('voucher_id')->count()
), ),
], ],
@ -183,7 +210,7 @@ protected function loadData()
'title' => 'Parfum Terjual', 'title' => 'Parfum Terjual',
'value' => currency( 'value' => currency(
OrderItem::where('orderable_type', Perfume::class) 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') ->sum('quantity')
).' ml', ).' ml',
], ],
@ -191,7 +218,7 @@ protected function loadData()
'title' => 'Produk Terjual', 'title' => 'Produk Terjual',
'value' => currency( 'value' => currency(
OrderItem::where('orderable_type', Product::class) 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') ->sum('quantity')
).' pcs', ).' pcs',
], ],
@ -199,7 +226,7 @@ protected function loadData()
'title' => 'Botol Terjual', 'title' => 'Botol Terjual',
'value' => currency( 'value' => currency(
OrderItem::where('orderable_type', Bottle::class) 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') ->sum('quantity')
).' pcs', ).' pcs',
], ],
@ -210,7 +237,7 @@ protected function loadData()
'title' => 'Beban Toko', 'title' => 'Beban Toko',
'value' => currency( 'value' => currency(
Expense::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) 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'), ->sum('amount'),
'Rp' 'Rp'
), ),
@ -219,7 +246,7 @@ protected function loadData()
'title' => 'Belanja Barang', 'title' => 'Belanja Barang',
'value' => currency( 'value' => currency(
Purchase::when(! empty($outletIds), fn ($q) => $q->whereIn('outlet_id', $outletIds)) 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'), ->sum('total'),
'Rp' 'Rp'
), ),
@ -228,18 +255,18 @@ protected function loadData()
'title' => 'Penggajian', 'title' => 'Penggajian',
'value' => currency( 'value' => currency(
Payroll::when(! empty($outletIds), fn ($q) => $q->whereHas('user.outlets', fn ($q2) => $q2->whereIn('outlets.id', $outletIds))) 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'), ->sum('total_salary'),
'Rp' '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') $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)) $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') ->select('id', 'name', 'sale_price')
@ -255,11 +282,11 @@ protected function loadData()
->take(5) ->take(5)
->toArray(); ->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') $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)) $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') ->select('id', 'name', 'sale_price')
@ -275,11 +302,11 @@ protected function loadData()
->take(5) ->take(5)
->toArray(); ->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') $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)) $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') ->select('id', 'name', 'sale_price')

View File

@ -21,6 +21,16 @@
<flux:checkbox value="{{ $key }}" label="{{ $value }}" /> <flux:checkbox value="{{ $key }}" label="{{ $value }}" />
@endforeach @endforeach
</flux:checkbox.group> </flux:checkbox.group>
<flux:separator vertical class="max-lg:hidden mx-2 my-2" />
<flux:date-picker mode="range" wire:model.live="range">
<x-slot name="trigger">
<div class="flex flex-col sm:flex-row gap-6 sm:gap-4">
<flux:date-picker.input />
<flux:date-picker.input />
</div>
</x-slot>
</flux:date-picker>
</div> </div>
</div> </div>