['required', 'array', 'min:1'], 'user_ids.*' => ['required', Rule::exists('users', 'id')], 'type' => ['required', Rule::in(SalaryAdjustmentType::values())], 'amount' => ['required', new UnsignedInteger], 'description' => ['required', 'string', 'max:100'], ]; } public function validationAttributes(): array { return [ 'user_ids' => 'pegawai', 'type' => 'tipe', 'description' => 'keterangan', 'amount' => 'jumlah', ]; } public function store() { $this->validate(); $amount = (int) replaceCurrency($this->amount); DB::transaction(function () use ($amount) { foreach ($this->user_ids as $user_id) { $periodMonth = Carbon::now()->format('Y-m'); $payroll = Payroll::where('user_id', $user_id) ->where('period_month', $periodMonth) ->first(); if (! $payroll) { throw new \Exception('Penggajian tidak ditemukan.'); } PayrollAdjustment::create([ 'payroll_id' => $payroll->id, 'type' => $this->type, 'description' => $this->description, 'amount' => $amount, ]); if ($this->type == SalaryAdjustmentType::BONUS->value) { $payroll->bonus += $amount; } elseif ($this->type == SalaryAdjustmentType::DEDUCTION->value) { $payroll->deduction += $amount; } $payroll->total_salary = $payroll->base_salary + $payroll->bonus - $payroll->deduction; $payroll->save(); } }); } public function update() { $this->validate(); } }