['required', 'string', 'max:50'], 'code' => [ 'required', 'string', 'max:20', 'alpha_num', Rule::unique('vouchers', 'code')->ignore($this->voucher), ], 'tags' => ['required', 'string', 'max:20'], 'type' => ['required', Rule::in(VoucherType::values())], 'discount_amount' => [ 'required', Rule::when( fn () => $this->type == VoucherType::PERCENTAGE->value, ['integer', 'between:0,100'], [new UnsignedInteger], ), ], 'min_purchase' => ['nullable', new UnsignedInteger], 'max_discount' => [ Rule::when( fn () => $this->type == VoucherType::PERCENTAGE->value, ['required', new UnsignedInteger], ['nullable'], ), ], 'quota' => ['nullable', new UnsignedInteger], 'limit_per_user' => ['nullable', new UnsignedInteger], 'summary' => ['required', 'string', 'max:200'], 'start_date' => ['required', 'date', 'after:yesterday'], 'end_date' => ['nullable', 'date', 'after_or_equal:start_date'], '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', 'summary' => 'summary', 'type' => 'Tipe', 'discount_amount' => 'Jumlah Diskon', 'start_date' => 'tanggal mulai', 'end_date' => 'tanggal selesai', 'outlet_ids' => 'outlet', 'outlet_ids.*' => 'outlet', ]; } public function setVoucher(Voucher $voucher) { $voucher->load('outlets'); $this->voucher = $voucher; $this->name = $this->voucher->name; $this->code = $this->voucher->code; $this->tags = $this->voucher->tags; $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->summary = $this->voucher->summary; $this->start_date = $this->voucher->start_date; $this->end_date = $this->voucher->end_date; $this->outlet_ids = $this->voucher->outlets->pluck('id')->toArray(); } public function store() { $this->validate(); $data = $this->prepareSavedData(); foreach (['min_purchase', 'max_discount', 'quota', 'limit_per_user'] as $field) { if ($this->$field === '') { $this->$field = null; } } 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) { unset($data['available_count']); $this->voucher->update($data); $this->voucher->outlets()->sync($this->outlet_ids); }); } private function prepareSavedData() { return [ 'name' => $this->name, 'code' => $this->code, 'tags' => $this->tags, '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, 'available_count' => $this->quota ?? 0, 'limit_per_user' => $this->limit_per_user, 'summary' => $this->summary, 'start_date' => $this->start_date, 'end_date' => $this->end_date, ]; } }