commit 08.01

This commit is contained in:
Владимир
2026-01-08 12:38:09 +00:00
parent bbe639b604
commit f5c68bf0c7
13 changed files with 4444 additions and 542 deletions

View File

@@ -10,7 +10,6 @@ use Illuminate\Support\Str;
class BookingsController extends Controller
{
// POST api/bookings - создание брони (ТОЛЬКО клиенты)
public function store(Request $request)
{
$request->validate([
@@ -25,7 +24,6 @@ class BookingsController extends Controller
return response()->json(['error' => 'Авторизация обязательна'], 401);
}
// Проверить активную услугу
$service = Services::where('id', $request->service_id)
->where('isactive', true)
->first();
@@ -36,7 +34,6 @@ class BookingsController extends Controller
$durationMinutes = $service->durationminutes;
$endtime = date('H:i:s', strtotime($request->starttime . " +{$durationMinutes} minutes"));
// Проверить доступность сотрудника
$availability = EmployeeAvailability::where('employee_id', $request->employee_id)
->where('date', $request->date)
->where('starttime', '<=', $request->starttime)
@@ -48,7 +45,6 @@ class BookingsController extends Controller
return response()->json(['error' => 'Сотрудник недоступен в это время'], 400);
}
// Проверить уникальность слота
$bookingExists = Booking::where('employee_id', $request->employee_id)
->where('bookingdate', $request->date)
->where('starttime', $request->starttime)
@@ -59,7 +55,6 @@ class BookingsController extends Controller
return response()->json(['error' => 'Слот уже забронирован'], 400);
}
// Создать бронь
$bookingNumber = 'CL-' . date('Y') . '-' . str_pad(Booking::count() + 1, 4, '0', STR_PAD_LEFT);
$booking = Booking::create([
@@ -79,17 +74,14 @@ class BookingsController extends Controller
], 201);
}
// POST api/bookings/{id}/cancel - отмена клиентом
public function cancel(Request $request, $id)
{
$booking = Booking::findOrFail($id);
// Только автор брони может отменить
if ($booking->client_id != auth()->id()) {
return response()->json(['error' => 'Можете отменить только свою бронь'], 403);
}
// Только confirmed брони
if ($booking->status != 'confirmed') {
return response()->json(['error' => 'Можно отменить только подтвержденные'], 400);
}
@@ -105,4 +97,70 @@ class BookingsController extends Controller
'booking' => $booking
]);
}
}
public function adminCancel(Request $request, $id)
{
$booking = Booking::findOrFail($id);
if (!auth()->user()->isEmployeeOrAdmin()) {
return response()->json(['error' => 'Доступ только для админов/сотрудников'], 403);
}
$booking->update([
'status' => 'cancelled',
'cancelledby' => 'admin',
'cancelreason' => $request->reason ?? null
]);
return response()->json([
'message' => 'Бронь отменена администратором',
'booking' => $booking
]);
}
public function clientIndex(Request $request)
{
$clientId = auth()->id();
$query = Booking::where('client_id', $clientId);
if ($request->date) {
$query->where('bookingdate', $request->date);
}
if ($request->status) {
$query->where('status', $request->status);
}
$bookings = $query->with(['service', 'employee'])
->orderBy('bookingdate', 'desc')
->orderBy('starttime', 'asc')
->get();
return response()->json($bookings);
}
public function adminIndex(Request $request)
{
if (!auth()->user()->isEmployeeOrAdmin()) {
return response()->json(['error' => 'Доступ запрещен'], 403);
}
$query = Booking::with(['service', 'client', 'employee']);
if ($request->date) {
$query->where('bookingdate', $request->date);
}
if ($request->status) {
$query->where('status', $request->status);
}
if ($request->employee_id) {
$query->where('employee_id', $request->employee_id);
}
$bookings = $query->orderBy('bookingdate', 'desc')
->orderBy('starttime', 'asc')
->get();
return response()->json($bookings);
}
}

View File

@@ -2,7 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
// бронирование ĸлиентов
// бронирование клиентов
class Booking extends Model {
use HasFactory;
@@ -20,4 +20,20 @@ class Booking extends Model {
'cancelledby',
'cancelreason'
];
// списки броней
public function service()
{
return $this->belongsTo(Services::class);
}
public function client()
{
return $this->belongsTo(User::class, 'client_id');
}
public function employee()
{
return $this->belongsTo(User::class, 'employee_id');
}
}