39499-vm/bookings/index.php
2026-04-06 06:47:41 +00:00

85 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/taxilanz.php';
app_boot();
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
redirect_to('/');
}
$payload = [
'offer_id' => (int) ($_POST['offer_id'] ?? 0),
'ride_uuid' => trim((string) ($_POST['ride_uuid'] ?? '')),
'customer_name' => trim((string) ($_POST['customer_name'] ?? '')),
'customer_email' => trim((string) ($_POST['customer_email'] ?? '')),
'customer_phone' => trim((string) ($_POST['customer_phone'] ?? '')),
'party_size' => (int) ($_POST['party_size'] ?? 1),
'booking_for' => trim((string) ($_POST['booking_for'] ?? '')),
'notes' => trim((string) ($_POST['notes'] ?? '')),
];
$offer = $payload['offer_id'] > 0 ? find_offer_by_id($payload['offer_id']) : null;
if (!$offer) {
set_flash('danger', 'Oferta no disponible', 'No hemos podido iniciar la reserva porque la oferta no existe.');
redirect_to('/');
}
$errors = [];
if ($payload['customer_name'] === '') {
$errors[] = 'Introduce tu nombre.';
}
if ($payload['customer_email'] === '' && $payload['customer_phone'] === '') {
$errors[] = 'Indica email o telefono.';
}
if ($payload['customer_email'] !== '' && !filter_var($payload['customer_email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'El email no es valido.';
}
if ($payload['party_size'] < 1 || $payload['party_size'] > 12) {
$errors[] = 'El numero de personas debe estar entre 1 y 12.';
}
if ($payload['booking_for'] !== '') {
$timestamp = strtotime($payload['booking_for']);
if ($timestamp === false || $timestamp < time() - 300) {
$errors[] = 'La fecha de reserva debe ser actual o futura.';
}
}
if ($errors) {
remember_form('booking_form', [
'customer_name' => $payload['customer_name'],
'customer_email' => $payload['customer_email'],
'customer_phone' => $payload['customer_phone'],
'party_size' => $payload['party_size'],
'booking_for' => $payload['booking_for'],
'notes' => $payload['notes'],
]);
set_flash('danger', 'Reserva incompleta', implode(' ', $errors));
$backUrl = '/offers/?slug=' . urlencode($offer['slug']);
if ($payload['ride_uuid'] !== '') {
$backUrl .= '&ride=' . urlencode($payload['ride_uuid']);
}
redirect_to($backUrl);
}
try {
$booking = create_booking($payload);
set_flash('success', 'Reserva confirmada', 'La solicitud de reserva ya quedo registrada y lista para seguimiento.');
redirect_to('/bookings/success.php?booking=' . urlencode($booking['uuid']));
} catch (Throwable $e) {
remember_form('booking_form', [
'customer_name' => $payload['customer_name'],
'customer_email' => $payload['customer_email'],
'customer_phone' => $payload['customer_phone'],
'party_size' => $payload['party_size'],
'booking_for' => $payload['booking_for'],
'notes' => $payload['notes'],
]);
set_flash('danger', 'No pudimos guardar la reserva', 'La oferta sigue disponible para que vuelvas a intentarlo.');
$backUrl = '/offers/?slug=' . urlencode($offer['slug']);
if ($payload['ride_uuid'] !== '') {
$backUrl .= '&ride=' . urlencode($payload['ride_uuid']);
}
redirect_to($backUrl);
}