whereKey($pool->id)->lockForUpdate()->firstOrFail(); $before = (int) $locked->current_amount; $after = $before + $amountDelta; if ($after < 0) { throw new \RuntimeException('adjustment_would_make_balance_negative'); } $adjustment = JackpotPoolAdjustment::query()->create([ 'adjustment_no' => $this->newAdjustmentNo(), 'jackpot_pool_id' => $locked->id, 'admin_user_id' => $admin->id, 'amount_delta' => $amountDelta, 'balance_before' => $before, 'balance_after' => $after, 'reason' => $reason, ]); $locked->forceFill(['current_amount' => $after])->save(); $snapshot = [ 'currency_code' => $locked->currency_code, 'amount_delta' => $amountDelta, 'balance_before' => $before, 'balance_after' => $after, 'reason' => $reason, 'adjustment_no' => $adjustment->adjustment_no, ]; if ($request !== null) { AuditLogger::recordFromRequest( $request, AuditLogger::OPERATOR_ADMIN, $admin->id, 'jackpot', 'adjust_balance', 'jackpot_pool', (string) $locked->id, ['current_amount' => $before], $snapshot, ); } else { AuditLogger::record( AuditLogger::OPERATOR_ADMIN, $admin->id, 'jackpot', 'adjust_balance', 'jackpot_pool', (string) $locked->id, ['current_amount' => $before], $snapshot, ); } return $adjustment->fresh(['adminUser']); }); } private function newAdjustmentNo(): string { return 'JA'.now()->format('YmdHis').Str::upper(Str::random(6)); } }