parfum/app/Livewire/Forms/VoucherForm.php

149 lines
4.5 KiB
PHP

<?php
namespace App\Livewire\Forms;
use App\Enums\IsActive;
use App\Enums\VoucherType;
use App\Models\Voucher;
use App\Rules\UnsignedInteger;
use Illuminate\Validation\Rule;
use Livewire\Form;
class VoucherForm extends Form
{
public ?Voucher $voucher = null;
public string $name = '';
public string $code = '';
public string $type = '1';
public string $discount_amount = '';
public ?string $min_purchase = null;
public ?string $max_discount = null;
public ?string $quota = null;
public ?string $limit_per_user = null;
public string $start_date = '';
public ?string $end_date = null;
public ?string $terms_conditions = null;
public string $is_active = '1';
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:50'],
'code' => [
'required',
'string',
'max:20',
'alpha_num',
Rule::unique('vouchers', 'code')->ignore($this->voucher),
],
'type' => ['required', Rule::in(VoucherType::values())],
'discount_amount' => [
'required',
Rule::when(
fn () => $this->type == VoucherType::PERCENTAGE->value,
['integer', 'between:0,100'],
['numeric', new UnsignedInteger],
),
],
'min_purchase' => ['nullable', 'numeric', new UnsignedInteger],
'max_discount' => [
Rule::when(
fn () => $this->type == VoucherType::PERCENTAGE->value,
['required', 'numeric', new UnsignedInteger],
['nullable'],
),
],
'quota' => ['nullable', 'numeric', new UnsignedInteger],
'limit_per_user' => ['nullable', 'numeric', new UnsignedInteger],
'start_date' => ['required', 'date'],
'end_date' => ['nullable', 'date', 'after_or_equal:start_date'],
'terms_conditions' => ['nullable', 'string'],
'is_active' => ['required', Rule::in(IsActive::values())],
];
}
public function validationAttributes(): array
{
return [
'name' => 'nama',
'code' => 'kode',
'min_purchase' => 'min. belanja',
'max_discount' => 'maks. diskon',
'quota' => 'kuota',
'limit_per_user' => 'limit per user',
'terms_conditions' => 'syarat dan ketentuan',
'type' => 'Tipe',
'discount_amount' => 'Jumlah Diskon',
'start_date' => 'tanggal mulai',
'end_date' => 'tanggal selesai',
'is_active' => 'aktif?',
];
}
public function setVoucher(Voucher $voucher)
{
$this->voucher = $voucher;
$this->name = $this->voucher->name;
$this->code = $this->voucher->code;
$this->type = $this->voucher->type->value;
$this->discount_amount = replaceCurrency($this->voucher->discount_amount);
$this->min_purchase = replaceCurrency($this->voucher->min_purchase);
$this->max_discount = replaceCurrency($this->voucher->max_discount);
$this->quota = $this->voucher->quota;
$this->limit_per_user = $this->voucher->limit_per_user;
$this->terms_conditions = $this->voucher->terms_conditions;
$this->start_date = $this->voucher->start_date;
$this->end_date = $this->voucher->end_date;
$this->is_active = $this->voucher->is_active->value;
}
public function store()
{
$this->validate();
$data = $this->prepareSavedData();
Voucher::create($data);
}
public function update()
{
$this->validate();
$data = $this->prepareSavedData();
$this->voucher->update($data);
}
private function prepareSavedData()
{
return [
'name' => $this->name,
'code' => $this->code,
'type' => $this->type,
'discount_amount' => replaceCurrency($this->discount_amount),
'min_purchase' => replaceCurrency($this->min_purchase),
'max_discount' => $this->type == VoucherType::PERCENTAGE->value ? replaceCurrency($this->max_discount) : null,
'quota' => $this->quota,
'limit_per_user' => $this->limit_per_user,
'terms_conditions' => $this->terms_conditions,
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'is_active' => $this->is_active,
];
}
}