diff --git a/app/Livewire/Studio/Loyalty/Tier.php b/app/Livewire/Studio/Loyalty/Tier.php index 3a1f28a..36ca0e2 100644 --- a/app/Livewire/Studio/Loyalty/Tier.php +++ b/app/Livewire/Studio/Loyalty/Tier.php @@ -3,6 +3,9 @@ namespace App\Livewire\Studio\Loyalty; use App\Livewire\Forms\Studio\Loyalty\TierForm; +use App\Models\Bottle; +use App\Models\Perfume; +use App\Models\Product; use App\Models\Tier as TierModel; use App\Traits\WithAuthorization; use App\Traits\WithCloseModal; @@ -29,13 +32,13 @@ class Tier extends Component public function mount() { - $this->tiers = TierModel::with(['memberships.user.customer.orders.items', 'memberships.user.customer.orders.payments']) + $this->tiers = TierModel::with(['memberships.user.customer.orders.items']) ->withCount('memberships') ->orderBy('min_points', 'asc') ->get() ->map(function ($tier) { $topMembership = $tier->memberships() - ->with(['user.customer.orders.items', 'user.customer.orders.payments']) + ->with(['user.customer.orders.items']) ->orderByDesc('tier_points') ->first(); @@ -44,15 +47,31 @@ public function mount() $orders = $topMembership->user->customer->orders; $totalOrders = $orders->count(); - $totalItems = $orders->flatMap(fn ($order) => $order->items)->sum('quantity'); - $totalAmount = $orders->flatMap(fn ($order) => $order->payments)->sum('amount'); + + // Sum items + $totalPerfume = $orders->flatMap(fn ($order) => $order->items->where('orderable_type', Perfume::class))->sum('quantity'); + $totalProduct = $orders->flatMap(fn ($order) => $order->items->where('orderable_type', Product::class))->sum('quantity'); + $totalBottle = $orders->flatMap(fn ($order) => $order->items->where('orderable_type', Bottle::class))->sum('quantity'); + + // Sum total + $subtotal = $orders->sum('subtotal'); + $totalDiscount = $orders->sum('discount'); + $grandTotal = $orders->sum('total'); $topMemberStats = [ 'name' => $topMembership->user->customer->name, 'points' => currency($topMembership->tier_points), 'total_orders' => currency($totalOrders), - 'total_items' => currency($totalItems), - 'total_amount' => currency($totalAmount), + 'items' => [ + 'total_perfume' => currency($totalPerfume), + 'total_product' => currency($totalProduct), + 'total_bottle' => currency($totalBottle), + ], + 'total' => [ + 'subtotal' => currency($subtotal, 'Rp'), + 'discount' => currency($totalDiscount, 'Rp'), + 'grand_total' => currency($grandTotal, 'Rp'), + ], ]; } diff --git a/resources/views/livewire/studio/loyalty/tiers.blade.php b/resources/views/livewire/studio/loyalty/tiers.blade.php index 2b32b5c..ee9cec7 100644 --- a/resources/views/livewire/studio/loyalty/tiers.blade.php +++ b/resources/views/livewire/studio/loyalty/tiers.blade.php @@ -57,16 +57,32 @@ class="mt-1 flex items-center justify-between bg-gradient-to-r from-emerald-400 class="mt-2 bg-white dark:bg-gray-800 rounded-lg shadow p-4 text-gray-700 dark:text-gray-300">