where('admin_site_id', $siteId) ->where('status', 'open') ->where('period_start', $start) ->where('period_end', $end) ->orderByDesc('id') ->first(); if ($existingSameRange !== null) { throw ValidationException::withMessages([ 'period_start' => ['period_already_open'], ]); } $otherOpen = DB::table('settlement_periods') ->where('admin_site_id', $siteId) ->where('status', 'open') ->orderByDesc('id') ->first(); if ($otherOpen !== null) { throw ValidationException::withMessages([ 'period_start' => ['period_site_has_open'], ]); } $id = (int) DB::table('settlement_periods')->insertGetId([ 'admin_site_id' => $siteId, 'period_start' => $start, 'period_end' => $end, 'status' => 'open', 'created_at' => now(), 'updated_at' => now(), ]); $row = DB::table('settlement_periods')->where('id', $id)->first(); if ($row === null) { throw new \RuntimeException('period_insert_failed'); } return $row; } }