148 lines
4.6 KiB
JavaScript
148 lines
4.6 KiB
JavaScript
const statusTimeline = document.getElementById("statusTimeline");
|
|
|
|
// Fonction pour récupérer le token CSRF depuis les cookies
|
|
function getCookie(name) {
|
|
let cookieValue = null;
|
|
if (document.cookie && document.cookie !== '') {
|
|
const cookies = document.cookie.split(';');
|
|
for (let i = 0; i < cookies.length; i++) {
|
|
const cookie = cookies[i].trim();
|
|
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return cookieValue;
|
|
}
|
|
|
|
const csrftoken = getCookie('csrftoken');
|
|
const orderId = document.getElementById('orderId').value;
|
|
|
|
let currentIndex = -1;
|
|
|
|
// Trouver l'index du statut actuel
|
|
STATUS_CHOICES.forEach((status, index) => {
|
|
if (status.key === currentStatus) {
|
|
currentIndex = index;
|
|
}
|
|
});
|
|
|
|
let nextActionsIndex = [];
|
|
if (!['cancelled'].includes(currentStatus)) {
|
|
nextActionsIndex = [currentIndex + 1];
|
|
}
|
|
let pastActionsIndex = [];
|
|
|
|
// Définir les actions possibles selon le statut actuel
|
|
// if (['sent'].includes(currentStatus)) {
|
|
// nextActionsIndex.push(currentIndex + 2); // completed
|
|
// }
|
|
// else if (['completed'].includes(currentStatus)) {
|
|
// nextActionsIndex.push(currentIndex + 2); // invoiced
|
|
// }
|
|
// else if (['invoiced'].includes(currentStatus)) {
|
|
// nextActionsIndex.push(currentIndex + 2); // paid
|
|
// }
|
|
|
|
// Ajouter la possibilité d'annuler depuis n'importe quel statut sauf 'completed', 'invoiced', 'paid' et 'cancelled'
|
|
if (!['completed', 'invoiced', 'paid', 'cancelled'].includes(currentStatus)) {
|
|
const cancelledIndex = STATUS_CHOICES.findIndex(s => s.key === 'cancelled');
|
|
if (cancelledIndex !== -1) {
|
|
nextActionsIndex.push(cancelledIndex);
|
|
}
|
|
}
|
|
|
|
// Générer la timeline
|
|
STATUS_CHOICES.forEach((status, index) => {
|
|
let statusClass = "hide";
|
|
let actionClass = "";
|
|
let dashedClass = "";
|
|
let buttonClass = "";
|
|
let label = status.label;
|
|
let icon = "";
|
|
const isAllowed = allowedStatuses.includes(status.key);
|
|
|
|
if (index === currentIndex) {
|
|
statusClass = "current";
|
|
dashedClass = "next-step-dashed";
|
|
} else if (index < currentIndex) {
|
|
statusClass = "past";
|
|
if (pastActionsIndex.includes(index)) {
|
|
if (isAllowed) {
|
|
actionClass = "action";
|
|
buttonClass = "button";
|
|
icon = "bi-arrow-left";
|
|
}
|
|
} else {
|
|
label = "";
|
|
}
|
|
} else {
|
|
if (nextActionsIndex.includes(index)) {
|
|
statusClass = "next";
|
|
if (isAllowed) {
|
|
actionClass = "action";
|
|
buttonClass = "button";
|
|
dashedClass = "next-step-dashed";
|
|
icon = "bi-arrow-right";
|
|
}
|
|
}
|
|
}
|
|
|
|
if (index === (STATUS_CHOICES.length - 1)) {
|
|
dashedClass = "";
|
|
}
|
|
|
|
if (statusClass !== "hide") {
|
|
const step = document.createElement("div");
|
|
step.className = `step ${statusClass} ${dashedClass}`;
|
|
step.innerHTML = `
|
|
<div id="status_${status.key}" status="${status.key}" class="circle ${actionClass} ${buttonClass} ${status.key}">
|
|
${icon ? '<i class="bi '+icon+'"></i>' : (index + 1)}
|
|
</div>
|
|
<div class="steplabel">${label}</div>
|
|
`;
|
|
statusTimeline.appendChild(step);
|
|
}
|
|
});
|
|
|
|
// Gérer les clics sur les cercles d'action avec délégation d'événements
|
|
$(document).on('click', '.circle.action', function () {
|
|
const newStatus = $(this).attr('status');
|
|
|
|
// Confirmer l'action pour l'annulation
|
|
if (newStatus === 'cancelled') {
|
|
if (!confirm(gettext("Êtes-vous sûr de vouloir annuler cette commande ?"))) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
updateStatus(newStatus);
|
|
});
|
|
|
|
function updateStatus(newStatus) {
|
|
$.ajax({
|
|
url: `/contracts/orders/${orderId}/update-status/`,
|
|
method: 'POST',
|
|
headers: {'X-CSRFToken': csrftoken},
|
|
data: {
|
|
order_id: orderId,
|
|
new_status: newStatus
|
|
},
|
|
success: function (response) {
|
|
location.reload();
|
|
},
|
|
error: function (xhr) {
|
|
let message = gettext("Erreur lors de la mise à jour du statut");
|
|
try {
|
|
const errorData = JSON.parse(xhr.responseText);
|
|
if (errorData.message) {
|
|
message = errorData.message;
|
|
}
|
|
} catch (e) {
|
|
// Ignore JSON parse errors
|
|
}
|
|
alert(message);
|
|
}
|
|
});
|
|
}
|