✨ 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:
@@ -241,7 +241,7 @@ export class FlowReserve extends HTMLAnchorElement {
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex justify-between items-end mt-2">
|
||||
<span class="text-[#0782bc] font-black text-sm">${this.formatPrice(product.priceTTC1Day || product.priceHt1Day)} <span class="text-[9px] text-slate-400 font-bold">/j</span></span>
|
||||
<span class="text-[#0782bc] font-black text-sm">${this.formatPrice(product.totalPriceTTC || product.totalPriceHT)} <span class="text-[9px] text-slate-400 font-bold">Total</span></span>
|
||||
<button class="text-red-400 hover:text-red-600 p-1 remove-btn" data-remove-id="${product.id}">
|
||||
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
|
||||
</button>
|
||||
@@ -252,10 +252,11 @@ export class FlowReserve extends HTMLAnchorElement {
|
||||
|
||||
container.innerHTML = `<div class="space-y-3">${datesHtml}${productsHtml}</div>`;
|
||||
|
||||
// Attach event listeners for remove buttons
|
||||
// Attach event listeners
|
||||
container.querySelectorAll('.remove-btn').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
this.removeFromList(btn.dataset.removeId);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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