86 lines
2.6 KiB
PHP
86 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\RoomType;
|
|
use App\Models\RoomAvailability;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class RoomAvailabilityController extends Controller
|
|
{
|
|
public function index(Request $request, $roomTypeId)
|
|
{
|
|
$roomType = RoomType::findOrFail($roomTypeId);
|
|
|
|
$from = $request->query('from');
|
|
$to = $request->query('to');
|
|
|
|
if (!$from || !$to) {
|
|
throw ValidationException::withMessages([
|
|
'from' => ['The from date is required.'],
|
|
'to' => ['The to date is required.'],
|
|
]);
|
|
}
|
|
|
|
$fromDate = \DateTime::createFromFormat('Y-m-d', $from);
|
|
$toDate = \DateTime::createFromFormat('Y-m-d', $to);
|
|
|
|
if (!$fromDate || !$toDate) {
|
|
throw ValidationException::withMessages([
|
|
'from' => ['Invalid from date format. Use YYYY-MM-DD.'],
|
|
'to' => ['Invalid to date format. Use YYYY-MM-DD.'],
|
|
]);
|
|
}
|
|
|
|
if ($fromDate > $toDate) {
|
|
throw ValidationException::withMessages([
|
|
'from' => ['From date must be before to date.'],
|
|
]);
|
|
}
|
|
|
|
$availabilities = RoomAvailability::where('room_type_id', $roomTypeId)
|
|
->whereBetween('date', [$from, $to])
|
|
->orderBy('date')
|
|
->get();
|
|
|
|
return response()->json($availabilities);
|
|
}
|
|
|
|
public function bulkUpdate(Request $request, $roomTypeId)
|
|
{
|
|
$roomType = RoomType::findOrFail($roomTypeId);
|
|
|
|
$validated = $request->validate([
|
|
'data' => 'required|array',
|
|
'data.*.date' => 'required|date_format:Y-m-d|after_or_equal:today',
|
|
'data.*.is_available' => 'required|boolean',
|
|
'data.*.price_override' => 'nullable|numeric|min:0',
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
foreach ($validated['data'] as $item) {
|
|
RoomAvailability::updateOrCreate(
|
|
[
|
|
'room_type_id' => $roomTypeId,
|
|
'date' => $item['date'],
|
|
],
|
|
[
|
|
'is_available' => $item['is_available'],
|
|
'price_override' => $item['price_override'] ?? null,
|
|
]
|
|
);
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return response()->json(['message' => 'Availability updated successfully']);
|
|
} catch (\Exception $e) {
|
|
DB::rollback();
|
|
throw $e;
|
|
}
|
|
}
|
|
} |