132 lines
5.0 KiB
JavaScript
132 lines
5.0 KiB
JavaScript
document.addEventListener("DOMContentLoaded", () => {
|
|
const datetimeElement = document.getElementById("datetime");
|
|
const previousAmountElement = document.getElementById("previous-amount");
|
|
const articleTotalElement = document.getElementById("article-total");
|
|
const finalTotalElement = document.getElementById("final-total");
|
|
const articlesTableBody = document.querySelector("#articles-table tbody");
|
|
const addRowButton = document.getElementById("add-row");
|
|
const saveButton = document.getElementById("save");
|
|
|
|
// Mettre à jour la date et l'heure
|
|
const updateDatetime = () => {
|
|
const now = new Date();
|
|
datetimeElement.textContent = now.toLocaleString("fr-FR");
|
|
};
|
|
setInterval(updateDatetime, 1000);
|
|
|
|
// Charger le montant précédent
|
|
fetch("/montant_precedent")
|
|
.then(response => response.text())
|
|
.then(data => {
|
|
const previousAmount = parseFloat(data) || 0;
|
|
previousAmountElement.textContent = `${previousAmount.toFixed(2)} €`;
|
|
updateTotals();
|
|
});
|
|
|
|
// Ajouter une ligne
|
|
addRowButton.addEventListener("click", () => {
|
|
const row = document.createElement("tr");
|
|
|
|
// Montant
|
|
const montantCell = document.createElement("td");
|
|
const montantInput = document.createElement("input");
|
|
montantInput.type = "number";
|
|
montantInput.step = "0.01";
|
|
montantInput.placeholder = "0.00";
|
|
montantCell.appendChild(montantInput);
|
|
|
|
// Quantité
|
|
const quantiteCell = document.createElement("td");
|
|
const quantiteSelect = document.createElement("select");
|
|
for (let i = 1; i <= 10; i++) {
|
|
const option = document.createElement("option");
|
|
option.value = i;
|
|
option.textContent = i;
|
|
quantiteSelect.appendChild(option);
|
|
}
|
|
quantiteCell.appendChild(quantiteSelect);
|
|
|
|
// Total
|
|
const totalCell = document.createElement("td");
|
|
totalCell.textContent = "0.00 €";
|
|
|
|
// Bouton Option
|
|
const optionCell = document.createElement("td");
|
|
const optionButton = document.createElement("button");
|
|
optionButton.textContent = "Option";
|
|
optionButton.addEventListener("click", () => {
|
|
const montant = parseFloat(montantInput.value || "0");
|
|
montantInput.value = montant > 0 ? -montant : Math.abs(montant);
|
|
updateRowTotal();
|
|
});
|
|
optionCell.appendChild(optionButton);
|
|
|
|
// Calculer le total de la ligne
|
|
const updateRowTotal = () => {
|
|
const montant = parseFloat(montantInput.value || "0");
|
|
const quantite = parseFloat(quantiteSelect.value || "1");
|
|
const total = montant * quantite;
|
|
totalCell.textContent = `${total.toFixed(2)} €`;
|
|
updateTotals();
|
|
};
|
|
|
|
montantInput.addEventListener("input", updateRowTotal);
|
|
quantiteSelect.addEventListener("change", updateRowTotal);
|
|
|
|
// Ajouter les cellules à la ligne
|
|
row.appendChild(montantCell);
|
|
row.appendChild(quantiteCell);
|
|
row.appendChild(totalCell);
|
|
row.appendChild(optionCell);
|
|
articlesTableBody.appendChild(row);
|
|
|
|
// Placer le curseur sur le champ montant
|
|
montantInput.focus();
|
|
});
|
|
|
|
// Mettre à jour les totaux
|
|
const updateTotals = () => {
|
|
let articleTotal = 0;
|
|
articlesTableBody.querySelectorAll("tr").forEach(row => {
|
|
const totalCell = row.cells[2];
|
|
articleTotal += parseFloat(totalCell.textContent || "0");
|
|
});
|
|
articleTotalElement.textContent = `${articleTotal.toFixed(2)} €`;
|
|
|
|
const previousAmount = parseFloat(previousAmountElement.textContent) || 0;
|
|
finalTotalElement.textContent = `${(previousAmount + articleTotal).toFixed(2)} €`;
|
|
};
|
|
|
|
// Sauvegarder les données
|
|
saveButton.addEventListener("click", () => {
|
|
const articles = [];
|
|
articlesTableBody.querySelectorAll("tr").forEach(row => {
|
|
const montant = parseFloat(row.cells[0].querySelector("input").value || "0");
|
|
const quantite = parseFloat(row.cells[1].querySelector("select").value || "1");
|
|
const total = montant * quantite;
|
|
articles.push({ montant, quantite, total });
|
|
});
|
|
|
|
const data = {
|
|
date: new Date().toISOString(),
|
|
previousAmount: parseFloat(previousAmountElement.textContent) || 0,
|
|
articles,
|
|
articleTotal: parseFloat(articleTotalElement.textContent) || 0,
|
|
finalTotal: parseFloat(finalTotalElement.textContent) || 0
|
|
};
|
|
|
|
fetch("/save", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data)
|
|
}).then(response => {
|
|
if (response.ok) {
|
|
alert("Données sauvegardées !");
|
|
location.reload();
|
|
} else {
|
|
alert("Erreur lors de la sauvegarde.");
|
|
}
|
|
});
|
|
});
|
|
});
|