+ availability
This commit is contained in:
@@ -25,20 +25,14 @@ class AuthController extends Controller
|
||||
return redirect()->route('admin.hotels.index');
|
||||
}
|
||||
|
||||
return back()->withErrors([
|
||||
'email' => 'Неверные данные.',
|
||||
])->onlyInput('email');
|
||||
return back()->withErrors(['email' => 'Неверные данные.']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Выход из системы.
|
||||
*/
|
||||
public function logout(Request $request)
|
||||
{
|
||||
Auth::logout();
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
return view('admin.logout');
|
||||
Auth::logout();
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
return redirect()->route('admin.login.form')->with('success', 'Вы успешно вышли из системы.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,53 @@
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\RoomType;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AvailabilityController extends Controller
|
||||
{
|
||||
//
|
||||
public function calendar(RoomType $roomType)
|
||||
{
|
||||
$roomType->load('availabilities');
|
||||
|
||||
if (request()->ajax()) {
|
||||
return view('admin.availability._calendar', compact('roomType'))->render();
|
||||
}
|
||||
|
||||
return view('admin.availability.calendar', compact('roomType'));
|
||||
}
|
||||
|
||||
public function store(Request $request, RoomType $roomType)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'start_date' => 'required|date',
|
||||
'end_date' => 'required|date|after_or_equal:start_date',
|
||||
'is_available' => 'required|boolean',
|
||||
'price' => 'nullable|numeric|min=0',
|
||||
]);
|
||||
|
||||
$roomType->availabilities()
|
||||
->where('start_date', '<=', $validated['end_date'])
|
||||
->where('end_date', '>=', $validated['start_date'])
|
||||
->delete();
|
||||
|
||||
$roomType->availabilities()->create($validated);
|
||||
|
||||
if ($request->ajax()) {
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
return redirect()->back()->with('success', 'Период сохранён.');
|
||||
}
|
||||
|
||||
public function destroy(\App\Models\Availability $availability)
|
||||
{
|
||||
$availability->delete();
|
||||
|
||||
if (request()->ajax()) {
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
return redirect()->back()->with('success', 'Период удалён.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class BookingController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Hotel;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\RoomType;
|
||||
|
||||
class HotelController extends Controller
|
||||
{
|
||||
@@ -19,6 +20,18 @@ class HotelController extends Controller
|
||||
return view('admin.hotels.create');
|
||||
}
|
||||
|
||||
public function calendar(Request $request, $hotelId)
|
||||
{
|
||||
$hotel = \App\Models\Hotel::findOrFail($hotelId);
|
||||
$roomTypes = RoomType::where('hotel_id', $hotelId)->with('availabilities')->get();
|
||||
|
||||
if ($request->ajax()) {
|
||||
return view('admin.hotels._calendar', compact('hotel', 'roomTypes'))->render();
|
||||
}
|
||||
|
||||
return view('admin.hotels.calendar', compact('hotel', 'roomTypes'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
@@ -55,4 +68,4 @@ class HotelController extends Controller
|
||||
$hotel->delete();
|
||||
return redirect()->route('admin.hotels.index')->with('success', 'Отель удалён.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class RoomTypeController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Booking;
|
||||
use App\Models\RoomType;
|
||||
use App\Models\RoomAvailability;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class BookingController extends Controller
|
||||
{
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'room_type_id' => 'required|exists:room_types,id',
|
||||
'check_in' => 'required|date|after_or_equal:today',
|
||||
'check_out' => 'required|date|after:check_in',
|
||||
'guest_name' => 'required|string|max:255',
|
||||
'guest_email' => 'required|email',
|
||||
'guest_phone' => 'required|string',
|
||||
'confirmation_type' => 'required|in:auto,manual',
|
||||
]);
|
||||
|
||||
$roomType = RoomType::findOrFail($validated['room_type_id']);
|
||||
|
||||
$dates = [];
|
||||
$currentDate = new \DateTime($validated['check_in']);
|
||||
$endDate = new \DateTime($validated['check_out']);
|
||||
|
||||
while ($currentDate < $endDate) {
|
||||
$dates[] = $currentDate->format('Y-m-d');
|
||||
$currentDate->modify('+1 day');
|
||||
}
|
||||
|
||||
$unavailableDates = RoomAvailability::where('room_type_id', $roomType->id)
|
||||
->whereIn('date', $dates)
|
||||
->where('is_available', false)
|
||||
->pluck('date')
|
||||
->toArray();
|
||||
|
||||
if (!empty($unavailableDates)) {
|
||||
throw ValidationException::withMessages([
|
||||
'check_in' => [
|
||||
'The following dates are not available: ' . implode(', ', $unavailableDates)
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$booking = Booking::create([
|
||||
'room_type_id' => $validated['room_type_id'],
|
||||
'check_in' => $validated['check_in'],
|
||||
'check_out' => $validated['check_out'],
|
||||
'guest_name' => $validated['guest_name'],
|
||||
'guest_email' => $validated['guest_email'],
|
||||
'guest_phone' => $validated['guest_phone'],
|
||||
'status' => $validated['confirmation_type'] === 'auto' ? 'confirmed' : 'pending',
|
||||
'confirmed_at' => $validated['confirmation_type'] === 'auto' ? now() : null,
|
||||
'created_by_user_id' => $request->user()->id, // ← ID админа
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json($booking, 201);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollback();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = \App\Models\Booking::with(['roomType', 'roomType.hotel']);
|
||||
|
||||
// Фильтр по статусу
|
||||
if ($request->has('status')) {
|
||||
$query->where('status', $request->status);
|
||||
}
|
||||
|
||||
// Фильтр по отелю
|
||||
if ($request->has('hotel_id')) {
|
||||
$query->whereHas('roomType.hotel', function ($q) use ($request) {
|
||||
$q->where('id', $request->hotel_id);
|
||||
});
|
||||
}
|
||||
|
||||
// Фильтр по дате заезда (от)
|
||||
if ($request->has('from')) {
|
||||
$query->where('check_in', '>=', $request->from);
|
||||
}
|
||||
|
||||
$query->orderBy('created_at', 'desc');
|
||||
|
||||
return response()->json($query->paginate(10));
|
||||
}
|
||||
public function confirm(Request $request, $id)
|
||||
{
|
||||
$booking = Booking::findOrFail($id);
|
||||
|
||||
if ($booking->status !== 'pending') {
|
||||
return response()->json(['error' => 'Booking is not in pending status'], 400);
|
||||
}
|
||||
|
||||
$booking->update([
|
||||
'status' => 'confirmed',
|
||||
'confirmed_at' => now(),
|
||||
]);
|
||||
|
||||
return response()->json($booking);
|
||||
}
|
||||
|
||||
public function cancel(Request $request, $id)
|
||||
{
|
||||
$booking = Booking::findOrFail($id);
|
||||
|
||||
if ($booking->status === 'cancelled') {
|
||||
return response()->json(['error' => 'Booking is already cancelled'], 400);
|
||||
}
|
||||
|
||||
$booking->update([
|
||||
'status' => 'cancelled',
|
||||
]);
|
||||
|
||||
return response()->json($booking);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Hotel;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class HotelController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$hotels = Hotel::all();
|
||||
return view('admin.hotels.index', compact('hotels'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'address' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$hotel = Hotel::create($validated);
|
||||
|
||||
return response()->json($hotel, 201);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$hotel = Hotel::findOrFail($id);
|
||||
|
||||
return response()->json($hotel);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$hotel = Hotel::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'address' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$hotel->update($validated);
|
||||
|
||||
return response()->json($hotel);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$hotel = Hotel::findOrFail($id);
|
||||
|
||||
|
||||
//if ($hotel->bookings()->exists()) {
|
||||
// return response()->json(['error' => 'Cannot delete hotel with bookings'], 400);
|
||||
//}
|
||||
|
||||
$hotel->delete();
|
||||
|
||||
return response()->json(['message' => 'Hotel deleted successfully']);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class Orders extends Model
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
use Hamcrest\Number\OrderingComparison;
|
||||
use Illuminate\Container\Attributes\Auth;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Orders;
|
||||
|
||||
class OrdersController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return response()->json(Orders::all()->toJson);
|
||||
}
|
||||
public function create(Request $request)
|
||||
{
|
||||
$status = $request->get(key: 'status');
|
||||
$description = $request->get(key: 'description');
|
||||
|
||||
|
||||
|
||||
$order = new Order();
|
||||
$order ->status = $status;
|
||||
$order ->description = $description;
|
||||
|
||||
$order ->total = $request->get('total',0);
|
||||
$order ->save();
|
||||
|
||||
|
||||
|
||||
return response()->json($order->toJson());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Hotel;
|
||||
use App\Models\RoomType;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class RoomTypeController extends Controller
|
||||
{
|
||||
public function store(Request $request, $hotelId)
|
||||
{
|
||||
$hotel = Hotel::findOrFail($hotelId);
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'capacity' => 'required|integer|min:1',
|
||||
'base_price' => 'required|numeric|min:0',
|
||||
]);
|
||||
|
||||
$roomType = $hotel->roomTypes()->create($validated);
|
||||
|
||||
return response()->json($roomType, 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$roomType = RoomType::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'capacity' => 'required|integer|min:1',
|
||||
'base_price' => 'required|numeric|min:0',
|
||||
]);
|
||||
|
||||
$roomType->update($validated);
|
||||
|
||||
return response()->json($roomType);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$roomType = RoomType::findOrFail($id);
|
||||
|
||||
|
||||
if ($roomType->bookings()->exists()) {
|
||||
return response()->json(['error' => 'Cannot delete room type with active bookings'], 400);
|
||||
}
|
||||
|
||||
|
||||
if ($roomType->availabilities()->exists()) {
|
||||
return response()->json(['error' => 'Cannot delete room type with availability records'], 400);
|
||||
}
|
||||
|
||||
$roomType->delete();
|
||||
|
||||
return response()->json(['message' => 'Room type deleted successfully']);
|
||||
}
|
||||
|
||||
public function hotel()
|
||||
{
|
||||
return $this->belongsTo(Hotel::class);
|
||||
}
|
||||
|
||||
public function bookings()
|
||||
{
|
||||
return $this->hasMany(Booking::class);
|
||||
}
|
||||
|
||||
public function availabilities()
|
||||
{
|
||||
return $this->hasMany(RoomAvailability::class);
|
||||
}
|
||||
}
|
||||
31
app/Models/Availability.php
Normal file
31
app/Models/Availability.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Availability extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'room_type_id',
|
||||
'start_date',
|
||||
'end_date',
|
||||
'is_available',
|
||||
'price',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_available' => 'boolean',
|
||||
'start_date' => 'date',
|
||||
'end_date' => 'date',
|
||||
'price' => 'decimal:2',
|
||||
];
|
||||
|
||||
public function roomType()
|
||||
{
|
||||
return $this->belongsTo(RoomType::class);
|
||||
}
|
||||
}
|
||||
@@ -16,14 +16,15 @@ class Booking extends Model
|
||||
'guest_name',
|
||||
'guest_email',
|
||||
'guest_phone',
|
||||
'status',
|
||||
'confirmed_at',
|
||||
'is_confirmed',
|
||||
'total_price',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'check_in' => 'date',
|
||||
'check_out' => 'date',
|
||||
'confirmed_at' => 'datetime',
|
||||
'is_confirmed' => 'boolean',
|
||||
'total_price' => 'decimal:2',
|
||||
];
|
||||
|
||||
public function roomType()
|
||||
|
||||
@@ -16,7 +16,7 @@ class Hotel extends Model
|
||||
|
||||
public function roomTypes()
|
||||
{
|
||||
return $this->hasMany(RoomType::class);
|
||||
return $this->hasMany(\App\Models\RoomType::class);
|
||||
}
|
||||
|
||||
//public function bookings()
|
||||
|
||||
@@ -12,8 +12,8 @@ class RoomType extends Model
|
||||
protected $fillable = [
|
||||
'hotel_id',
|
||||
'name',
|
||||
'capacity',
|
||||
'base_price',
|
||||
'description',
|
||||
'max_guests',
|
||||
];
|
||||
|
||||
public function hotel()
|
||||
@@ -21,13 +21,13 @@ class RoomType extends Model
|
||||
return $this->belongsTo(Hotel::class);
|
||||
}
|
||||
|
||||
public function bookings()
|
||||
{
|
||||
return $this->hasMany(Booking::class);
|
||||
}
|
||||
|
||||
public function availabilities()
|
||||
{
|
||||
return $this->hasMany(RoomAvailability::class);
|
||||
return $this->hasMany(Availability::class);
|
||||
}
|
||||
|
||||
public function bookings()
|
||||
{
|
||||
return $this->hasMany(\App\Models\Booking::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,22 +22,19 @@ class RouteServiceProvider extends ServiceProvider
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
public function boot()
|
||||
{
|
||||
$this->configureRateLimiting();
|
||||
|
||||
//$this->routesPath = base_path('routes');
|
||||
$this->routes(function () {
|
||||
Route::middleware('web')
|
||||
->group(base_path('routes/web.php'));
|
||||
|
||||
//$this->routes(function () {
|
||||
//Route::middleware('web')
|
||||
//->group($this->routesPath . '/web.php');
|
||||
|
||||
//Route::middleware(['web', 'auth'])
|
||||
//->prefix('admin')
|
||||
//->group($this->routesPath . '/admin.php');
|
||||
//});
|
||||
// Route::middleware(['web', 'auth'])
|
||||
// ->prefix('admin')
|
||||
// ->group(base_path('routes/admin.php'));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the rate limiters for the application.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user