feat(analysis): membuat filter period

This commit is contained in:
Yoga Pangestu 2025-11-11 23:18:58 +07:00
parent 9cfb5b75a7
commit 00341e13d0
2 changed files with 136 additions and 37 deletions

View File

@ -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')

View File

@ -3,12 +3,25 @@
<flux:separator />
<div class="mt-6">
<div class="mb-6 grid grid-cols-1 lg:grid-cols-2 gap-4">
<flux:checkbox.group wire:model.live="selectedOutletIds" variant="buttons">
@foreach ($outlets as $key => $value)
<flux:checkbox value="{{ $key }}" label="{{ $value }}" />
@endforeach
</flux:checkbox.group>
<div class="flex justify-between items-center mb-6">
<div class="flex items-center gap-2">
<div class="flex items-center gap-2">
<flux:select wire:model.live="period">
<option value="all">Semua</option>
<option value="7">7 Hari Terakhir</option>
<option value="30">30 Hari Terakhir</option>
<option value="90">90 Hari Terakhir</option>
<option value="365">1 Tahun Terakhir</option>
</flux:select>
</div>
<flux:separator vertical class="max-lg:hidden mx-2 my-2" />
<flux:checkbox.group wire:model.live="selectedOutletIds" variant="buttons">
@foreach ($outlets as $key => $value)
<flux:checkbox value="{{ $key }}" label="{{ $value }}" />
@endforeach
</flux:checkbox.group>
</div>
</div>
<flux:heading>Counting Data</flux:heading>