113 lines
3.5 KiB
PHP
113 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Booking;
|
|
use App\Models\Service;
|
|
use App\Models\User;
|
|
|
|
class BookingsController extends Controller
|
|
{
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'service_id' => 'required|exists:services,id',
|
|
'employee_id' => 'required|exists:users,id',
|
|
'date' => 'required|date',
|
|
'start_time' => 'required|date_format:H:i:s'
|
|
]);
|
|
|
|
// Получаем длительность услуги
|
|
$service = Service::findOrFail($validated['service_id']);
|
|
$duration = $service->duration_minutes;
|
|
|
|
// Вычисляем end_time
|
|
$start = new \DateTime($validated['start_time']);
|
|
$end = clone $start;
|
|
$end->modify("+$duration minutes");
|
|
$end_time = $end->format('H:i:s');
|
|
|
|
// Проверяем, свободен ли слот
|
|
$conflict = Booking::where('employee_id', $validated['employee_id'])
|
|
->where('booking_date', $validated['date'])
|
|
->where('start_time', '<', $end_time)
|
|
->where('end_time', '>', $validated['start_time'])
|
|
->exists();
|
|
|
|
if ($conflict) {
|
|
return response()->json(['message' => 'Слот занят'], 400);
|
|
}
|
|
|
|
// Создаём бронирование
|
|
$booking = Booking::create([
|
|
'booking_number' => 'CL-' . date('Y') . '-' . str_pad(Booking::count() + 1, 4, '0', STR_PAD_LEFT),
|
|
'client_id' => auth()->id(),
|
|
'employee_id' => $validated['employee_id'],
|
|
'service_id' => $validated['service_id'],
|
|
'booking_date' => $validated['date'],
|
|
'start_time' => $validated['start_time'],
|
|
'end_time' => $end_time,
|
|
'status' => 'confirmed'
|
|
]);
|
|
|
|
return response()->json($booking, 201);
|
|
}
|
|
|
|
public function clientIndex()
|
|
{
|
|
$bookings = Booking::where('client_id', auth()->id())->get();
|
|
return response()->json($bookings);
|
|
}
|
|
|
|
// adminIndex
|
|
public function adminIndex()
|
|
{
|
|
$bookings = Booking::with(['client', 'employee', 'service'])
|
|
->orderBy('booking_date', 'desc')
|
|
->get();
|
|
return response()->json($bookings);
|
|
}
|
|
|
|
public function cancel($id)
|
|
{
|
|
$booking = Booking::findOrFail($id);
|
|
|
|
if ($booking->client_id !== auth()->id()) {
|
|
return response()->json(['message' => 'Нет прав'], 403);
|
|
}
|
|
|
|
$booking->update([
|
|
'status' => 'cancelled',
|
|
'cancelled_by' => 'client',
|
|
'cancel_reason' => request('reason')
|
|
]);
|
|
|
|
return response()->json(['message' => 'Бронь отменена']);
|
|
}
|
|
|
|
// adminCancel
|
|
public function adminCancel($id)
|
|
{
|
|
$booking = Booking::findOrFail($id);
|
|
$booking->update([
|
|
'status' => 'cancelled',
|
|
'cancelled_by' => 'admin',
|
|
'cancel_reason' => request('reason')
|
|
]);
|
|
|
|
return response()->json(['message' => 'Бронь отменена администратором']);
|
|
}
|
|
|
|
// Назначение сотрудника
|
|
public function assignEmployee(Request $request, $id)
|
|
{
|
|
$request->validate(['employee_id' => 'required|exists:users,id']);
|
|
|
|
$booking = Booking::findOrFail($id);
|
|
$booking->employee_id = $request->employee_id;
|
|
$booking->save();
|
|
|
|
return response()->json($booking);
|
|
}
|
|
} |