fix: saving and various times editting report

This commit is contained in:
Angie 2025-08-03 22:10:59 +02:00
parent 15f19d626f
commit 9e68a8aa4a

View File

@ -180,12 +180,9 @@ document.getElementById("loginForm").addEventListener("submit", async (e) => {
// Save user info in global state
appState.currentUser = result.user;
// Save today's reports if present (for workers), or empty for admin ===
if (result.todaysReports) {
appState.todaysReports = result.todaysReports;
} else {
appState.todaysReports = [];
}
// save globally reports of today for user if they are presented in what login giving back
appState.todaysReports = result.todaysReports || [];
// Hide login, show correct UI
document.getElementById("loginScreen").classList.add("hidden");
@ -212,8 +209,8 @@ async function showUserInterface() {
loadUserStores();
setupFormCalculations();
await loadReports();
refreshTodaysReports();
}
// Показать интерфейс администратора
@ -637,6 +634,7 @@ function viewReport(reportId) {
function editReport(report) {
// console.log("editReport() called with:", report);
appState.editingReportId = report.id;
const content = document.getElementById("reportViewContent");
const buttons = document.getElementById("reportModalButtons");
@ -1456,6 +1454,21 @@ function safeToFixed(value, digits = 2) {
return (Number(value) || 0).toFixed(digits);
}
function upsertTodaysReport(report) {
const today =
report.reportDate || report.date || new Date().toISOString().split("T")[0];
const storeId = String(report.storeId);
appState.todaysReports = (appState.todaysReports || []).filter(
(r) =>
!(
String(r.storeId) === storeId &&
(r.reportDate || r.date) === today &&
(r.userId == report.userId || r.username === report.username)
)
);
appState.todaysReports.push(report);
}
// save report as user-worker
document.getElementById("reportForm").addEventListener("submit", async (e) => {
e.preventDefault();
@ -1504,48 +1517,49 @@ document.getElementById("reportForm").addEventListener("submit", async (e) => {
return;
}
if (result.success) {
// if (result.success && (!appState.editingReportId || result.id)) {
if (
(appState.editingReportId && result.success) || // edit: only need success
(!appState.editingReportId && result.success && result.id) // create: need id
) {
resetReportForm();
const wasEdit = !!appState.editingReportId;
const reportId = appState.editingReportId || result.id;
appState.editingReportId = null;
window.scrollTo({ top: 0, behavior: "smooth" });
document.getElementById("reportForm").reset();
document.getElementById("wagesContainer").innerHTML = "";
addWageRow();
document.getElementById("expensesContainer").innerHTML = "";
addExpenseRow();
const newReport = {
...formData,
id: reportId,
userId: appState.currentUser.id,
username: appState.currentUser.username,
};
document.getElementById("totalWages").textContent = "0.00";
document.getElementById("totalExpensesInternal").textContent = "0.00";
document.getElementById("cajaFinal").textContent = "€0.00";
upsertTodaysReport(newReport);
showNotification(
wasEdit ? "Отчет успешно отредактирован!" : "Отчет успешно создан!"
);
await loadReports();
refreshTodaysReports();
console.log("Updated todaysReports:", appState.todaysReports);
} else {
resetReportForm();
window.scrollTo({ top: 0, behavior: "smooth" });
document.getElementById("reportForm").reset();
showNotification(result.error || "Ошибка создания отчета", "error");
}
});
function refreshTodaysReports() {
const today = new Date().toISOString().split("T")[0];
appState.todaysReports = (appState.reportsList || [])
.filter((r) => (r.reportDate || r.date) === today)
.map((r) => ({ ...r, storeId: String(r.storeId) }));
}
function resetReportForm() {
document.getElementById("reportForm").reset();
document.getElementById("wagesContainer").innerHTML = "";
addWageRow();
function refreshTodaysReports() {
const today = new Date().toISOString().split("T")[0];
appState.todaysReports = (appState.reportsList || [])
.filter((r) => (r.reportDate || r.date) === today)
.map((r) => ({ ...r, storeId: String(r.storeId) }));
document.getElementById("expensesContainer").innerHTML = "";
addExpenseRow();
document.getElementById("totalWages").textContent = "0.00";
document.getElementById("totalExpensesInternal").textContent = "0.00";
document.getElementById("cajaFinal").textContent = "0.00";
}
// Отчет за сегодня для пользователя
@ -1556,14 +1570,15 @@ document.getElementById("todayReportBtn").addEventListener("click", () => {
return;
}
// Filter today's reports only
const report = (appState.todaysReports || []).find(
(r) => String(r.storeId) === String(storeId)
// optionally, also filter by user if needed:
// && String(r.userId) === String(appState.currentUser.id)
const todaysReport = (appState.todaysReports || []).find(
(r) => String(r.storeId) === storeId
);
if (report) {
showReportModal(report, false);
console.log(todaysReport);
console.log("Matching today's report:", todaysReport);
if (todaysReport) {
showReportModal(todaysReport, false);
} else {
showNotification("Сегодняшний отчет еще не создан", "error");
document.getElementById("reportForm").reset();