['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())], 'outlet_ids' => ['required', 'array', 'min:1'], 'outlet_ids.*' => Rule::exists('outlets', 'id'), ]; } 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) { $voucher->load('outlets'); $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; $this->outlet_ids = $this->voucher->outlets->pluck('id')->toArray(); } public function store() { $this->validate(); $data = $this->prepareSavedData(); DB::transaction(function () use ($data) { $voucher = Voucher::create($data); $voucher->outlets()->attach($this->outlet_ids); }); } public function update() { $this->validate(); $data = $this->prepareSavedData(); DB::transaction(function () use ($data) { $this->voucher->update($data); $this->voucher->outlets()->sync($this->outlet_ids); }); } 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, ]; } }