@@ -948,7 +948,7 @@ function toggleStepDates(type) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ajout magique d'une dépense d'essence SÉCURISÉE
|
// Ajout magique d'une dépense d'essence SÉCURISÉE et INSTANTANÉE
|
||||||
function addQuickTransitExpense(
|
function addQuickTransitExpense(
|
||||||
holidayId,
|
holidayId,
|
||||||
sortOrder,
|
sortOrder,
|
||||||
@@ -963,11 +963,19 @@ function addQuickTransitExpense(
|
|||||||
)
|
)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (btnElement) {
|
// 1. UI OPTIMISTE : On change visuellement le bouton tout de suite sans attendre le serveur
|
||||||
btnElement.disabled = true;
|
const parentContainer = btnElement.parentElement;
|
||||||
btnElement.innerText = "⏳...";
|
if (parentContainer) {
|
||||||
btnElement.style.cursor = "not-allowed";
|
parentContainer.innerHTML = `<span style="color:var(--success); font-weight:bold; margin-left: 5px;">✓ Ajouté</span>`;
|
||||||
btnElement.style.opacity = "0.7";
|
}
|
||||||
|
|
||||||
|
// 2. On met à jour discrètement le compteur global en haut de page (+ montant)
|
||||||
|
const totalTransitEl = document.querySelector(".hol-summary-value strong");
|
||||||
|
if (totalTransitEl) {
|
||||||
|
const currentTotal =
|
||||||
|
parseFloat(totalTransitEl.innerText.replace(" €", "").replace(" ", "")) ||
|
||||||
|
0;
|
||||||
|
totalTransitEl.innerText = Math.round(currentTotal + amount) + " €";
|
||||||
}
|
}
|
||||||
|
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
@@ -979,10 +987,17 @@ function addQuickTransitExpense(
|
|||||||
fd.append("amount", amount);
|
fd.append("amount", amount);
|
||||||
fd.append("context", "transit");
|
fd.append("context", "transit");
|
||||||
|
|
||||||
|
// 3. Envoi en arrière-plan sans bloquer l'utilisateur
|
||||||
fetch("/modules/holidays/includes/api/save_checkpoint.php", {
|
fetch("/modules/holidays/includes/api/save_checkpoint.php", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: fd,
|
body: fd,
|
||||||
}).then(() => window.location.reload());
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
if (!data.success) {
|
||||||
|
alert("Erreur lors de la sauvegarde en arrière-plan : " + data.error);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Permet de modifier le prix du carburant à la volée
|
// Permet de modifier le prix du carburant à la volée
|
||||||
|
|||||||
@@ -4,24 +4,25 @@ require dirname(__DIR__, 4) . '/includes/auth.php';
|
|||||||
require dirname(__DIR__, 4) . '/includes/db.php';
|
require dirname(__DIR__, 4) . '/includes/db.php';
|
||||||
require_login();
|
require_login();
|
||||||
|
|
||||||
// --------------------------------===========================================
|
// ---------------------------------------------------------------------------
|
||||||
// INTERCEPTIONS AJAX (Execution ultra rapide sans rechargement de page)
|
// INTERCEPTIONS AJAX (Exécution ultra rapide sans rechargement lourd)
|
||||||
// --------------------------------===========================================
|
// ---------------------------------------------------------------------------
|
||||||
if (isset($_POST['action']) && in_array($_POST['action'], ['update_item_datetime', 'update_item_duration', 'add_single_item'])) {
|
if (isset($_POST['action']) && in_array($_POST['action'], ['update_item_datetime', 'update_item_duration', 'add_single_item'])) {
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
session_write_close(); // 🚀 LIBÈRE LA SESSION : Permet d'autres requêtes simultanées sans bloquer le navigateur
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 🔥 AJOUT SÉCURISÉ D'UNE DÉPENSE UNIQUE (Essence OSRM)
|
|
||||||
if ($_POST['action'] === 'add_single_item') {
|
if ($_POST['action'] === 'add_single_item') {
|
||||||
$holiday_id = (int)$_POST['holiday_id'];
|
$holiday_id = (int)$_POST['holiday_id'];
|
||||||
$sort_order = (int)$_POST['sort_order'];
|
$sort_order = (int)$_POST['sort_order'];
|
||||||
|
|
||||||
// On récupère proprement les métadonnées de l'étape pour lier le frais
|
|
||||||
$stmt = $pdo->prepare("SELECT location_name, lat, lng, step_start_date, step_end_date, step_type FROM pf_holidays_items WHERE holiday_id = ? AND sort_order = ? LIMIT 1");
|
$stmt = $pdo->prepare("SELECT location_name, lat, lng, step_start_date, step_end_date, step_type FROM pf_holidays_items WHERE holiday_id = ? AND sort_order = ? LIMIT 1");
|
||||||
$stmt->execute([$holiday_id, $sort_order]);
|
$stmt->execute([$holiday_id, $sort_order]);
|
||||||
$stepInfo = $stmt->fetch();
|
$stepInfo = $stmt->fetch();
|
||||||
|
|
||||||
if ($stepInfo) {
|
if ($stepInfo) {
|
||||||
|
// Utilisation de transactions isolées pour l'AJAX
|
||||||
|
$pdo->beginTransaction();
|
||||||
$ins = $pdo->prepare("INSERT INTO pf_holidays_items (holiday_id, category, name, amount, is_paid, location_name, lat, lng, sort_order, step_start_date, step_end_date, step_type, expense_context, duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)");
|
$ins = $pdo->prepare("INSERT INTO pf_holidays_items (holiday_id, category, name, amount, is_paid, location_name, lat, lng, sort_order, step_start_date, step_end_date, step_type, expense_context, duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)");
|
||||||
$ins->execute([
|
$ins->execute([
|
||||||
$holiday_id, $_POST['category'], $_POST['name'], (float)$_POST['amount'], 0,
|
$holiday_id, $_POST['category'], $_POST['name'], (float)$_POST['amount'], 0,
|
||||||
@@ -29,6 +30,7 @@ if (isset($_POST['action']) && in_array($_POST['action'], ['update_item_datetime
|
|||||||
$sort_order, $stepInfo['step_start_date'], $stepInfo['step_end_date'],
|
$sort_order, $stepInfo['step_start_date'], $stepInfo['step_end_date'],
|
||||||
$stepInfo['step_type'], $_POST['context']
|
$stepInfo['step_type'], $_POST['context']
|
||||||
]);
|
]);
|
||||||
|
$pdo->commit();
|
||||||
echo json_encode(['success' => true]);
|
echo json_encode(['success' => true]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
@@ -57,6 +59,7 @@ if (isset($_POST['action']) && in_array($_POST['action'], ['update_item_datetime
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
if ($pdo->inTransaction()) { $pdo->rollBack(); }
|
||||||
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user