✨ feat(ReserverController): Gère la durée et le prix total des produits.
⬆️ refactor(FlowReserve.js): Affiche le prix total au lieu du prix par jour.
This commit is contained in:
@@ -139,63 +139,192 @@ class ReserverController extends AbstractController
|
||||
return new Response('', Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
#[Route('/basket/json', name: 'reservation_basket_json', methods: ['POST'])]
|
||||
public function basketJson(Request $request, ProductRepository $productRepository, UploaderHelper $uploaderHelper): Response
|
||||
{
|
||||
$data = json_decode($request->getContent(), true);
|
||||
$ids = $data['ids'] ?? [];
|
||||
#[Route('/basket/json', name: 'reservation_basket_json', methods: ['POST'])]
|
||||
|
||||
public function basketJson(Request $request, ProductRepository $productRepository, UploaderHelper $uploaderHelper): Response
|
||||
|
||||
{
|
||||
|
||||
$data = json_decode($request->getContent(), true);
|
||||
|
||||
$ids = $data['ids'] ?? [];
|
||||
|
||||
$startStr = $data['start'] ?? null;
|
||||
|
||||
$endStr = $data['end'] ?? null;
|
||||
|
||||
|
||||
|
||||
// Calcul de la durée
|
||||
|
||||
$duration = 1;
|
||||
|
||||
if ($startStr && $endStr) {
|
||||
|
||||
try {
|
||||
|
||||
$start = new \DateTimeImmutable($startStr);
|
||||
|
||||
$end = new \DateTimeImmutable($endStr);
|
||||
|
||||
// Différence en jours + 1 car inclusif (ex: du 7 au 7 = 1 jour)
|
||||
|
||||
// On s'assure que end >= start
|
||||
|
||||
if ($end >= $start) {
|
||||
|
||||
$duration = $start->diff($end)->days + 1;
|
||||
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
||||
$duration = 1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Protection contre les données invalides
|
||||
|
||||
if (!is_array($ids)) {
|
||||
|
||||
$ids = [];
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Récupération des produits
|
||||
|
||||
$products = [];
|
||||
|
||||
if (!empty($ids)) {
|
||||
|
||||
$products = $productRepository->findBy(['id' => $ids]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$items = [];
|
||||
|
||||
$totalHT = 0;
|
||||
|
||||
$tvaEnabled = isset($_ENV['TVA_ENABLED']) && $_ENV['TVA_ENABLED'] === "true";
|
||||
|
||||
$tvaRate = $tvaEnabled ? 0.20 : 0;
|
||||
|
||||
|
||||
|
||||
foreach ($products as $product) {
|
||||
|
||||
$price1Day = $product->getPriceDay();
|
||||
|
||||
$priceSup = $product->getPriceSup() ?? 0.0;
|
||||
|
||||
|
||||
|
||||
// Calcul du coût total pour ce produit selon la durée
|
||||
|
||||
|
||||
|
||||
// 1er jour @ price1Day, jours suivants @ priceSup
|
||||
|
||||
|
||||
|
||||
$productTotalHT = $price1Day + ($priceSup * max(0, $duration - 1));
|
||||
|
||||
|
||||
|
||||
$productTotalTTC = $productTotalHT * (1 + $tvaRate);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
$items[] = [
|
||||
|
||||
|
||||
|
||||
'id' => $product->getId(),
|
||||
|
||||
|
||||
|
||||
'name' => $product->getName(),
|
||||
|
||||
|
||||
|
||||
'image' => $uploaderHelper->asset($product, 'imageFile'),
|
||||
|
||||
|
||||
|
||||
'priceHt1Day' => $price1Day,
|
||||
|
||||
|
||||
|
||||
'priceHTSupDay' => $priceSup,
|
||||
|
||||
|
||||
|
||||
'priceTTC1Day' => $price1Day * (1 + $tvaRate),
|
||||
|
||||
|
||||
|
||||
'totalPriceHT' => $productTotalHT,
|
||||
|
||||
|
||||
|
||||
'totalPriceTTC' => $productTotalTTC,
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
$totalHT += $productTotalHT;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$totalTva = $totalHT * $tvaRate;
|
||||
|
||||
$totalTTC = $totalHT + $totalTva;
|
||||
|
||||
|
||||
|
||||
return new JsonResponse([
|
||||
|
||||
'start_date' => $startStr,
|
||||
|
||||
'end_date' => $endStr,
|
||||
|
||||
'products' => $items,
|
||||
|
||||
'total' => [
|
||||
|
||||
'totalHT' => $totalHT,
|
||||
|
||||
'totalTva' => $totalTva,
|
||||
|
||||
'totalTTC' => $totalTTC
|
||||
|
||||
]
|
||||
|
||||
]);
|
||||
|
||||
// Protection contre les données invalides
|
||||
if (!is_array($ids)) {
|
||||
$ids = [];
|
||||
}
|
||||
|
||||
// Récupération des produits
|
||||
$products = [];
|
||||
if (!empty($ids)) {
|
||||
$products = $productRepository->findBy(['id' => $ids]);
|
||||
}
|
||||
|
||||
$items = [];
|
||||
$totalHT = 0;
|
||||
$tvaEnabled = isset($_ENV['TVA_ENABLED']) && $_ENV['TVA_ENABLED'] === "true";
|
||||
$tvaRate = $tvaEnabled ? 0.20 : 0;
|
||||
|
||||
foreach ($products as $product) {
|
||||
$priceHT = $product->getPriceDay();
|
||||
|
||||
$items[] = [
|
||||
'id' => $product->getId(),
|
||||
'name' => $product->getName(),
|
||||
'image' => $uploaderHelper->asset($product, 'imageFile'),
|
||||
'priceHt1Day' => $priceHT,
|
||||
'priceHTSupDay' => $product->getPriceSup(),
|
||||
'priceTTC1Day' => $priceHT * (1 + $tvaRate),
|
||||
];
|
||||
|
||||
$totalHT += $priceHT;
|
||||
}
|
||||
|
||||
$totalTva = $totalHT * $tvaRate;
|
||||
$totalTTC = $totalHT + $totalTva;
|
||||
|
||||
// Récupération des dates depuis la session si disponibles (exemple)
|
||||
$session = $request->getSession();
|
||||
$startDate = $session->get('reservation_start');
|
||||
$endDate = $session->get('reservation_end');
|
||||
|
||||
return new JsonResponse([
|
||||
'start_date' => $startDate, // Format attendu : "DD/MM/YYYY" ou similaire par le JS ? Le JS affiche tel quel.
|
||||
'end_date' => $endDate,
|
||||
'products' => $items,
|
||||
'total' => [
|
||||
'totalHT' => $totalHT,
|
||||
'totalTva' => $totalTva,
|
||||
'totalTTC' => $totalTTC
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/umami', name: 'reservation_umami', methods: ['POST'])]
|
||||
public function umami(
|
||||
Request $request,
|
||||
|
||||
Reference in New Issue
Block a user