@@ -12,20 +12,33 @@ class GlobalCalendarController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request): Response
|
||||
{
|
||||
$filters = $request->validate(['month' => ['nullable', 'date_format:Y-m']]);
|
||||
$filters = $request->validate([
|
||||
'month' => ['nullable', 'date_format:Y-m'],
|
||||
'status' => ['nullable', 'string', 'in:pending,confirmed,closed'],
|
||||
]);
|
||||
$month = isset($filters['month'])
|
||||
? CarbonImmutable::createFromFormat('!Y-m', $filters['month'])
|
||||
: now()->startOfMonth();
|
||||
|
||||
$query = Reservation::query()
|
||||
->with(['requester:id,name,department', 'items.asset:id,name,asset_tag'])
|
||||
->where('starts_at', '<', $month->addMonth())
|
||||
->where('ends_at', '>', $month);
|
||||
|
||||
if (! empty($filters['status'])) {
|
||||
$statusMap = [
|
||||
'pending' => ['pending_approval', 'awaiting_information'],
|
||||
'confirmed' => ['approved', 'ready', 'delivered', 'in_use'],
|
||||
'closed' => ['rejected', 'cancelled', 'expired', 'no_show', 'completed'],
|
||||
];
|
||||
$query->whereIn('status', $statusMap[$filters['status']]);
|
||||
}
|
||||
|
||||
return Inertia::render('reservations/calendar', [
|
||||
'scope' => 'global',
|
||||
'month' => $month->format('Y-m'),
|
||||
'reservations' => Reservation::query()
|
||||
->with(['requester:id,name,department', 'items.asset:id,name,asset_tag'])
|
||||
->where('starts_at', '<', $month->addMonth())
|
||||
->where('ends_at', '>', $month)
|
||||
->orderBy('starts_at')
|
||||
->get(),
|
||||
'reservations' => $query->orderBy('starts_at')->get(),
|
||||
'status' => $filters['status'] ?? '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,14 @@ use App\Domain\Inventory\Models\SnipeAsset;
|
||||
use App\Domain\Reservations\Models\Reservation;
|
||||
use App\Domain\Reservations\Services\AvailabilityService;
|
||||
use App\Domain\Reservations\Services\ReservationService;
|
||||
use App\Http\Requests\StoreRecurringReservationRequest;
|
||||
use App\Http\Requests\StoreReservationRequest;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
@@ -18,11 +22,20 @@ class ReservationController extends Controller
|
||||
{
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$filters = $request->validate([
|
||||
'status' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
$query = $request->user()->reservations()
|
||||
->with('items.asset:id,name,asset_tag');
|
||||
|
||||
if (! empty($filters['status'])) {
|
||||
$query->where('status', $filters['status']);
|
||||
}
|
||||
|
||||
return Inertia::render('reservations/index', [
|
||||
'reservations' => $request->user()->reservations()
|
||||
->with('items.asset:id,name,asset_tag')
|
||||
->latest()
|
||||
->paginate(15),
|
||||
'reservations' => $query->latest()->paginate(15),
|
||||
'status' => $filters['status'] ?? '',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -30,20 +43,31 @@ class ReservationController extends Controller
|
||||
{
|
||||
$filters = $request->validate([
|
||||
'month' => ['nullable', 'date_format:Y-m'],
|
||||
'status' => ['nullable', 'string', 'in:pending,confirmed,closed'],
|
||||
]);
|
||||
$month = isset($filters['month'])
|
||||
? CarbonImmutable::createFromFormat('!Y-m', $filters['month'])
|
||||
: now()->startOfMonth();
|
||||
$windowEnd = $month->addMonth();
|
||||
|
||||
$query = $request->user()->reservations()
|
||||
->with('items.asset:id,name,asset_tag')
|
||||
->where('starts_at', '<', $windowEnd)
|
||||
->where('ends_at', '>', $month);
|
||||
|
||||
if (! empty($filters['status'])) {
|
||||
$statusMap = [
|
||||
'pending' => ['pending_approval', 'awaiting_information'],
|
||||
'confirmed' => ['approved', 'ready', 'delivered', 'in_use'],
|
||||
'closed' => ['rejected', 'cancelled', 'expired', 'no_show', 'completed'],
|
||||
];
|
||||
$query->whereIn('status', $statusMap[$filters['status']]);
|
||||
}
|
||||
|
||||
return Inertia::render('reservations/calendar', [
|
||||
'month' => $month->format('Y-m'),
|
||||
'reservations' => $request->user()->reservations()
|
||||
->with('items.asset:id,name,asset_tag')
|
||||
->where('starts_at', '<', $windowEnd)
|
||||
->where('ends_at', '>', $month)
|
||||
->orderBy('starts_at')
|
||||
->get(),
|
||||
'reservations' => $query->orderBy('starts_at')->get(),
|
||||
'status' => $filters['status'] ?? '',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -92,6 +116,225 @@ class ReservationController extends Controller
|
||||
return to_route('dashboard')->with('success', "Requisição {$reservation->reference} submetida para aprovação.");
|
||||
}
|
||||
|
||||
public function checkRecurrence(Request $request, ReservationService $service): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'weekly_slots' => ['required', 'array', 'min:1', 'max:5'],
|
||||
'weekly_slots.*.day_of_week' => ['required', 'integer', 'min:1', 'max:5'],
|
||||
'weekly_slots.*.start_time' => ['required', 'date_format:H:i'],
|
||||
'weekly_slots.*.end_time' => ['required', 'date_format:H:i'],
|
||||
'repeat_until' => ['nullable', 'date', 'after:now'],
|
||||
'weeks_count' => ['nullable', 'integer', 'min:1', 'max:26'],
|
||||
'asset_ids' => ['required', 'array', 'min:1'],
|
||||
'asset_ids.*' => ['required', 'integer', 'exists:snipe_assets,id'],
|
||||
]);
|
||||
|
||||
// Custom validation: end_time must be after start_time for each slot
|
||||
foreach ($data['weekly_slots'] as $index => $slot) {
|
||||
if ($slot['start_time'] >= $slot['end_time']) {
|
||||
throw ValidationException::withMessages([
|
||||
"weekly_slots.{$index}.end_time" => 'A hora de fim deve ser posterior à hora de início.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/** @var array{weekly_slots: list<array{day_of_week: int, start_time: string, end_time: string}>, repeat_until: string|null, weeks_count: int|null, asset_ids: list<int>} $data */
|
||||
$repeatUntil = ! empty($data['repeat_until'])
|
||||
? CarbonImmutable::parse($data['repeat_until'])->setTime(23, 59, 59)
|
||||
: now()->addWeeks(($data['weeks_count'] ?? 4) - 1)->endOfDay();
|
||||
|
||||
$result = $service->checkFlexibleRecurringSeries(
|
||||
$data['asset_ids'],
|
||||
$data['weekly_slots'],
|
||||
$repeatUntil,
|
||||
);
|
||||
|
||||
return response()->json($result);
|
||||
}
|
||||
|
||||
public function checkWeeklyAvailability(Request $request, AvailabilityService $availability): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'weekly_slots' => ['required', 'array', 'min:1', 'max:5'],
|
||||
'weekly_slots.*.day_of_week' => ['required', 'integer', 'min:1', 'max:5'],
|
||||
'weekly_slots.*.start_time' => ['required', 'date_format:H:i'],
|
||||
'weekly_slots.*.end_time' => ['required', 'date_format:H:i'],
|
||||
'week_offset' => ['nullable', 'integer', 'min:0'],
|
||||
]);
|
||||
|
||||
// Custom validation: end_time must be after start_time for each slot
|
||||
foreach ($data['weekly_slots'] as $index => $slot) {
|
||||
if ($slot['start_time'] >= $slot['end_time']) {
|
||||
throw ValidationException::withMessages([
|
||||
"weekly_slots.{$index}.end_time" => 'A hora de fim deve ser posterior à hora de início.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$weekOffset = $data['week_offset'] ?? 0;
|
||||
$now = now();
|
||||
$startOfWeek = $now->copy()->addWeeks($weekOffset)->startOfWeek(CarbonImmutable::MONDAY);
|
||||
|
||||
// Calculate specific dates for each slot
|
||||
$slotPeriods = [];
|
||||
foreach ($data['weekly_slots'] as $slot) {
|
||||
$slotDate = $startOfWeek->copy()->addDays($slot['day_of_week'] - 1);
|
||||
$startsAt = $slotDate->setTimeFromTimeString($slot['start_time']);
|
||||
$endsAt = $slotDate->setTimeFromTimeString($slot['end_time']);
|
||||
$slotPeriods[] = ['starts_at' => $startsAt, 'ends_at' => $endsAt];
|
||||
}
|
||||
|
||||
// Find assets available for ALL slots (intersection)
|
||||
$availableAssets = null;
|
||||
foreach ($slotPeriods as $period) {
|
||||
$assetsForSlot = $availability->availableAssets($period['starts_at'], $period['ends_at'])
|
||||
->orderBy('name')
|
||||
->get(['id', 'name', 'asset_tag', 'model_name', 'category_name', 'location_name'])
|
||||
->keyBy('id');
|
||||
|
||||
if ($availableAssets === null) {
|
||||
$availableAssets = $assetsForSlot;
|
||||
} else {
|
||||
// Intersect: keep only assets available in both sets
|
||||
$availableAssets = $availableAssets->intersectByKeys($assetsForSlot);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'assets' => $availableAssets ? $availableAssets->values() : collect(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function checkSlotAvailability(Request $request, AvailabilityService $availability): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'weekly_slots' => ['required', 'array'],
|
||||
'weekly_slots.*.day_of_week' => ['required', 'integer', 'min:1', 'max:5'],
|
||||
'weekly_slots.*.start_time' => ['required', 'date_format:H:i'],
|
||||
'weekly_slots.*.end_time' => ['required', 'date_format:H:i'],
|
||||
'week_offset' => ['nullable', 'integer', 'min:0'],
|
||||
'asset_ids' => ['nullable', 'array'],
|
||||
'asset_ids.*' => ['nullable', 'integer', 'exists:snipe_assets,id'],
|
||||
]);
|
||||
|
||||
// Custom validation: end_time must be after start_time for each slot
|
||||
foreach ($data['weekly_slots'] as $index => $slot) {
|
||||
if ($slot['start_time'] >= $slot['end_time']) {
|
||||
throw ValidationException::withMessages([
|
||||
"weekly_slots.{$index}.end_time" => 'A hora de fim deve ser posterior à hora de início.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$now = now();
|
||||
$weekOffset = $data['week_offset'] ?? 0;
|
||||
$startOfWeek = $now->copy()->addWeeks($weekOffset)->startOfWeek(CarbonImmutable::MONDAY);
|
||||
$availabilityMap = [];
|
||||
|
||||
foreach ($data['weekly_slots'] as $slot) {
|
||||
// Calculate the date based on week offset
|
||||
$slotDate = $startOfWeek->copy()->addDays($slot['day_of_week'] - 1);
|
||||
$startsAt = $slotDate->setTimeFromTimeString($slot['start_time']);
|
||||
$endsAt = $slotDate->setTimeFromTimeString($slot['end_time']);
|
||||
|
||||
$key = "{$slot['day_of_week']}-{$slot['start_time']}-{$slot['end_time']}";
|
||||
|
||||
// Check availability for this specific slot
|
||||
$availableAssets = $availability->availableAssets($startsAt, $endsAt);
|
||||
|
||||
// If specific asset_ids are provided, check if any of them are available
|
||||
if (! empty($data['asset_ids'])) {
|
||||
$availableIds = $availableAssets->pluck('id')->toArray();
|
||||
$hasAvailable = false;
|
||||
foreach ($data['asset_ids'] as $assetId) {
|
||||
if (in_array($assetId, $availableIds)) {
|
||||
$hasAvailable = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$availabilityMap[$key] = $hasAvailable;
|
||||
} else {
|
||||
// If no specific assets, check if any asset is available
|
||||
$availabilityMap[$key] = $availableAssets->count() > 0;
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'availability' => $availabilityMap,
|
||||
]);
|
||||
}
|
||||
|
||||
public function storeRecurring(StoreRecurringReservationRequest $request, ReservationService $service): RedirectResponse
|
||||
{
|
||||
/** @var array{purpose: string, usage_location: string} $baseData */
|
||||
$baseData = $request->safe()->only(['purpose', 'usage_location']);
|
||||
/** @var list<array{starts_at: string, ends_at: string, asset_ids: list<int>}> $occurrences */
|
||||
$occurrences = $request->validated('occurrences');
|
||||
|
||||
$reservations = $service->createRecurringPending($request->user(), $baseData, $occurrences);
|
||||
$count = $reservations->count();
|
||||
|
||||
return to_route('dashboard')->with('success', "Série recorrente submetida com {$count} requisições para aprovação.");
|
||||
}
|
||||
|
||||
public function storeMultiple(Request $request, ReservationService $service): RedirectResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'weekly_slots' => ['required', 'array', 'min:1'],
|
||||
'weekly_slots.*.day_of_week' => ['required', 'integer', 'min:1', 'max:5'],
|
||||
'weekly_slots.*.start_time' => ['required', 'date_format:H:i'],
|
||||
'weekly_slots.*.end_time' => ['required', 'date_format:H:i'],
|
||||
'week_offset' => ['nullable', 'integer', 'min:0'],
|
||||
'purpose' => ['required', 'string', 'min:10', 'max:2000'],
|
||||
'usage_location' => ['required', 'string', 'max:255'],
|
||||
'asset_ids' => ['required', 'array', 'min:1'],
|
||||
'asset_ids.*' => ['required', 'integer', 'distinct', 'exists:snipe_assets,id'],
|
||||
'terms_accepted' => ['accepted'],
|
||||
]);
|
||||
|
||||
// Custom validation: end_time must be after start_time for each slot
|
||||
foreach ($data['weekly_slots'] as $index => $slot) {
|
||||
if ($slot['start_time'] >= $slot['end_time']) {
|
||||
throw ValidationException::withMessages([
|
||||
"weekly_slots.{$index}.end_time" => 'A hora de fim deve ser posterior à hora de início.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$now = now();
|
||||
$weekOffset = $data['week_offset'] ?? 0;
|
||||
$startOfWeek = $now->copy()->addWeeks($weekOffset)->startOfWeek(CarbonImmutable::MONDAY);
|
||||
$baseData = [
|
||||
'purpose' => $data['purpose'],
|
||||
'usage_location' => $data['usage_location'],
|
||||
'asset_ids' => $data['asset_ids'],
|
||||
];
|
||||
|
||||
$reservations = collect();
|
||||
|
||||
DB::transaction(function () use ($now, $weekOffset, $startOfWeek, $data, $baseData, $service, $request, &$reservations) {
|
||||
foreach ($data['weekly_slots'] as $slot) {
|
||||
// Calculate the date based on week offset
|
||||
$slotDate = $startOfWeek->copy()->addDays($slot['day_of_week'] - 1);
|
||||
$startsAt = $slotDate->setTimeFromTimeString($slot['start_time']);
|
||||
$endsAt = $slotDate->setTimeFromTimeString($slot['end_time']);
|
||||
|
||||
$slotData = array_merge($baseData, [
|
||||
'starts_at' => $startsAt->toIso8601String(),
|
||||
'ends_at' => $endsAt->toIso8601String(),
|
||||
]);
|
||||
|
||||
$reservation = $service->createPending($request->user(), $slotData);
|
||||
$reservations->push($reservation);
|
||||
}
|
||||
});
|
||||
|
||||
$count = $reservations->count();
|
||||
$references = $reservations->pluck('reference')->join(', ');
|
||||
|
||||
return to_route('dashboard')->with('success', "Criadas {$count} requisições ({$references}) para aprovação.");
|
||||
}
|
||||
|
||||
public function cancel(Request $request, Reservation $reservation, ReservationService $service): RedirectResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
|
||||
Reference in New Issue
Block a user