parfum/app/Livewire/Member/MyVouchers.php

75 lines
2.7 KiB
PHP

<?php
namespace App\Livewire\Member;
use App\Enums\VoucherType;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Voucher Saya')]
#[Layout('components.layouts.member')]
class MyVouchers extends Component
{
public array $myVouchers = [];
public function mount()
{
$this->myVouchers = auth()->user()->vouchers()
->withPivot('quantity', 'created_at')
->orderBy('user_voucher.created_at', 'desc')
->get()
->map(function ($voucher) {
// Status Logic
$now = now();
$isActive = $voucher->start_date <= $now && ($voucher->end_date === null || $voucher->end_date >= $now);
$isExpired = $voucher->end_date && $voucher->end_date < $now;
$isUpcoming = $voucher->start_date > $now;
$status = 'active';
if ($isExpired) {
$status = 'expired';
} elseif ($isUpcoming) {
$status = 'upcoming';
}
// Discount Display Logic
if ($voucher->type === VoucherType::PERCENTAGE) {
$discountDisplay = "Diskon {$voucher->discount_amount}%";
if ($voucher->max_discount) {
$discountDisplay .= ' (Max Rp'.formatCurrencyNumber($voucher->max_discount).')';
}
} else {
$discountDisplay = 'Potongan Rp'.formatCurrencyNumber($voucher->discount_amount);
}
// Date Display Logic
$dateDisplay = formatDateLocalized($voucher->start_date);
if ($voucher->end_date) {
$dateDisplay .= ' - '.formatDateLocalized($voucher->end_date);
} else {
$dateDisplay .= ' - Tidak ada batas waktu';
}
$voucher->claimed_at_display = formatDateTimeLocalized($voucher->pivot->created_at);
$voucher->date_display = $dateDisplay;
$voucher->min_purchase_display = $voucher->min_purchase ? 'Min. Belanja: Rp'.formatCurrencyNumber($voucher->min_purchase) : null;
$voucher->discount_display = $discountDisplay;
$voucher->status = $status;
$voucher->quantity = $voucher->pivot->quantity;
return $voucher;
})
->toArray();
}
public function render(): View
{
return view('livewire.member.my-vouchers', [
'pageTitle' => 'Voucher Saya',
'pageDescription' => 'Daftar voucher yang telah Anda klaim dan dapat digunakan ketika belanja di toko.',
]);
}
}