migrations and models
This commit is contained in:
39
app/Http/Controllers/AdminAuthController.php
Normal file
39
app/Http/Controllers/AdminAuthController.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Admin;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class AdminAuthController extends Controller
|
||||
{
|
||||
public function login(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'email' => 'required|email',
|
||||
'password' => 'required',
|
||||
]);
|
||||
|
||||
$admin = Admin::where('email', $request->email)->first();
|
||||
|
||||
if (!$admin || !Hash::check($request->password, $admin->password)) {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => ['The provided credentials are incorrect.'],
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'token' => $admin->createToken('admin-token')->plainTextToken,
|
||||
'admin' => $admin,
|
||||
]);
|
||||
}
|
||||
|
||||
public function logout(Request $request)
|
||||
{
|
||||
$request->user()->currentAccessToken()->delete();
|
||||
|
||||
return response()->json(['message' => 'Logged out successfully']);
|
||||
}
|
||||
}
|
||||
129
app/Http/Controllers/BookingController.php
Normal file
129
app/Http/Controllers/BookingController.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
62
app/Http/Controllers/HotelController.php
Normal file
62
app/Http/Controllers/HotelController.php
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Hotel;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class HotelController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return Hotel::all();
|
||||
}
|
||||
|
||||
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']);
|
||||
}
|
||||
}
|
||||
81
app/Http/Controllers/InvoiceController.php
Normal file
81
app/Http/Controllers/InvoiceController.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Booking;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\RoomAvailability;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class InvoiceController extends Controller
|
||||
{
|
||||
|
||||
public function generate(Request $request, $bookingId)
|
||||
{
|
||||
$booking = Booking::findOrFail($bookingId);
|
||||
|
||||
if ($booking->status === 'cancelled') {
|
||||
return response()->json(['error' => 'Cannot generate invoice for cancelled booking'], 400);
|
||||
}
|
||||
|
||||
$totalAmount = 0;
|
||||
$currentDate = new \DateTime($booking->check_in);
|
||||
$endDate = new \DateTime($booking->check_out);
|
||||
|
||||
while ($currentDate < $endDate) {
|
||||
$date = $currentDate->format('Y-m-d');
|
||||
|
||||
$availability = RoomAvailability::where('room_type_id', $booking->room_type_id)
|
||||
->where('date', $date)
|
||||
->first();
|
||||
|
||||
$price = $availability && $availability->price_override !== null
|
||||
? $availability->price_override
|
||||
: $booking->roomType->base_price;
|
||||
|
||||
$totalAmount += $price;
|
||||
|
||||
$currentDate->modify('+1 day');
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$invoice = Invoice::create([
|
||||
'booking_id' => $booking->id,
|
||||
'total_amount' => $totalAmount,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
|
||||
$pdfPath = $this->generatePdf($invoice);
|
||||
|
||||
if ($pdfPath) {
|
||||
$invoice->update(['pdf_path' => $pdfPath]);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json($invoice, 201);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollback();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function generatePdf($invoice)
|
||||
{
|
||||
|
||||
$pdf = \PDF::loadView('invoices.show', compact('invoice'));
|
||||
|
||||
$fileName = "invoice_{$invoice->id}.pdf";
|
||||
$path = "invoices/{$fileName}";
|
||||
|
||||
Storage::put($path, $pdf->output());
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
86
app/Http/Controllers/RoomAvailabilityController.php
Normal file
86
app/Http/Controllers/RoomAvailabilityController.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
75
app/Http/Controllers/RoomTypeController.php
Normal file
75
app/Http/Controllers/RoomTypeController.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user