parts with save button

This commit is contained in:
2026-07-15 16:59:27 +02:00
parent 2a6eddeb44
commit be450f9d85
2 changed files with 179 additions and 469 deletions
+3
View File
@@ -29,6 +29,9 @@
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; min-width: 0;"> <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; min-width: 0;">
<h6 style="margin: 0; white-space: nowrap;">Elenco Parti</h6> <h6 style="margin: 0; white-space: nowrap;">Elenco Parti</h6>
<div style="display: flex; align-items: center; min-width: 0; gap: 8px;"> <div style="display: flex; align-items: center; min-width: 0; gap: 8px;">
<button type="button" id="savePartsBtn" class="btn btn-success btn-sm">
<i class="fas fa-save"></i> Salva
</button>
<button type="button" <button type="button"
class="btn btn-dark btn-sm open-analysis-modal-btn" class="btn btn-dark btn-sm open-analysis-modal-btn"
id="openAnalysisModalBtn" id="openAnalysisModalBtn"
+176 -469
View File
@@ -245,7 +245,7 @@ $(document).ready(function () {
$row.data("extra-value-id", valId); $row.data("extra-value-id", valId);
$(this).closest("td").find(".part-extra-value-id").val(valId); $(this).closest("td").find(".part-extra-value-id").val(valId);
saveRow($row); markUnsaved();
}); });
$(document).on("click", ".extra-propagate-btn", function (e) { $(document).on("click", ".extra-propagate-btn", function (e) {
@@ -316,9 +316,9 @@ $(document).ready(function () {
$row.data("extra-value-text", propagateValueText); $row.data("extra-value-text", propagateValueText);
} }
} }
saveRow($row);
}); });
markUnsaved();
}); });
function applyExtraFieldColumn() { function applyExtraFieldColumn() {
@@ -987,15 +987,9 @@ $(document).ready(function () {
.get(); .get();
const maxPartNumber = partNumbers.length ? Math.max(...partNumbers) : 0; const maxPartNumber = partNumbers.length ? Math.max(...partNumbers) : 0;
// Crea la riga Mix // Crea la riga Mix (verrà salvata col pulsante Salva)
addNewRow(maxPartNumber + 1, true); addNewRow(maxPartNumber + 1, true);
const $mixRow = $("#partsTableBody tr:last"); markUnsaved();
// Consenti SOLO ora la creazione (INSERT) della riga Mix
$mixRow.data("allowCreateMix", true);
// esegue SUBITO l'INSERT così ottieni part-id
saveRow($mixRow);
}); });
function extractPartId(response) { function extractPartId(response) {
@@ -1183,7 +1177,7 @@ $(document).ready(function () {
}) })
.last(); .last();
// Se non esiste una riga Mix, ne creo una e la INSERISCO SUBITO (come fa il bottone in header) // Se non esiste una riga Mix, ne creo una nuova (salvata col pulsante Salva)
if ($mixRow.length === 0) { if ($mixRow.length === 0) {
const maxPartNumber = Math.max( const maxPartNumber = Math.max(
...$("#partsTableBody tr") ...$("#partsTableBody tr")
@@ -1198,12 +1192,8 @@ $(document).ready(function () {
addNewRow(maxPartNumber + 1, true); addNewRow(maxPartNumber + 1, true);
$mixRow = $("#partsTableBody tr:last"); $mixRow = $("#partsTableBody tr:last");
$mixRow.find(".part-description").val(`Mix ${partDescription}`); $mixRow.find(".part-description").val(`Mix ${partDescription}`);
markUnsaved();
// Consenti la creazione (INSERT) della riga Mix e salvala subito return;
$mixRow.data("allowCreateMix", true);
saveRow($mixRow); // -> INSERT
return; // la descrizione include già l'elemento appena aggiunto
} }
// Aggiorna la descrizione del Mix esistente // Aggiorna la descrizione del Mix esistente
@@ -1214,22 +1204,7 @@ $(document).ready(function () {
newDesc = currentMix + " + " + partDescription; newDesc = currentMix + " + " + partDescription;
$mixRow.find(".part-description").val(newDesc); $mixRow.find(".part-description").val(newDesc);
markUnsaved();
// Se il Mix è già in salvataggio (INSERT o UPDATE in corso), accodiamo un solo UPDATE
if ($mixRow.data("saving") === true) {
// evita più code accumulate
if (!$mixRow.data("pendingUpdate")) {
$mixRow.data("pendingUpdate", true);
$mixRow.one("row:saved", function () {
$mixRow.removeData("pendingUpdate");
// ora che saving è false, salviamo l'ultima descrizione impostata
saveRow($mixRow);
});
}
} else {
// libero: salva subito
saveRow($mixRow);
}
}); });
function addNewRow(nextPartNumber, isMix = false) { function addNewRow(nextPartNumber, isMix = false) {
@@ -1291,186 +1266,21 @@ $(document).ready(function () {
$(document).on("click", ".save-note-btn", function () { $(document).on("click", ".save-note-btn", function () {
const $noteModal = $("#noteModal"); const $noteModal = $("#noteModal");
const $row = $noteModal.data("row"); const $row = $noteModal.data("row");
const partId = $noteModal.data("part-id");
const note = $noteModal.find(".part-note").val().trim(); const note = $noteModal.find(".part-note").val().trim();
const iddatadb = $("#partsModal").data("iddatadb");
const idquotations = $("#partsModal").data("idquotations");
const endpoint = idquotations
? "save_parts_quotation.php"
: "save_parts.php";
const data = idquotations
? { idquotations: idquotations }
: { iddatadb: iddatadb };
// Raccogli tutti i dati della riga per evitare sovrascritture $row.data("note", note);
const partNumber = $row.find(".part-number").val(); $row.find(".note-btn").toggleClass("has-note", !!note);
const partDescription = $row.find(".part-description").val().trim(); bootstrap.Modal.getInstance(
const mix = getRowMix($row); document.getElementById("noteModal"),
const idmatrice = $row.find(".part-matrice").val() || null; ).hide();
const dateexpiry = $row.find(".part-dateexpiry").val() || null; markUnsaved();
if (partId && partId !== "new") {
const $saveStatus = $row.find(".save-status");
const $saveLoading = $row.find(".save-loading");
$saveLoading.show();
$saveStatus.hide();
$.ajax({
url: endpoint,
method: "POST",
data: JSON.stringify({
...data,
parts: [
{
id: partId,
part_number: partNumber,
part_description: partDescription,
mix: mix,
idmatrice: idmatrice,
note: note || null,
dateexpiry: dateexpiry,
},
],
}),
contentType: "application/json",
success: function (response) {
$saveLoading.hide();
if (response.success) {
$row.data("note", note);
$row.find(".note-btn").toggleClass("has-note", !!note);
$saveStatus.show();
setTimeout(() => $saveStatus.hide(), 2000);
bootstrap.Modal.getInstance(
document.getElementById("noteModal"),
).hide();
} else {
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio della nota: ' +
response.message +
"</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
}
},
error: function (xhr, status, error) {
$saveLoading.hide();
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio della nota: ' +
error +
" (" +
xhr.status +
")</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
},
});
} else {
$row.data("note", note);
$row.find(".note-btn").toggleClass("has-note", !!note);
bootstrap.Modal.getInstance(
document.getElementById("noteModal"),
).hide();
markUnsaved();
}
}); });
// =================== // ===================
// DATEEXPIRY HANDLING // DATEEXPIRY HANDLING
// =================== // ===================
$(document).on("change", ".part-dateexpiry", function () { $(document).on("change", ".part-dateexpiry", function () {
const $input = $(this); markUnsaved();
const $row = $input.closest("tr");
const partId = getPartId($row);
const dateexpiry = $input.val();
const iddatadb = $("#partsModal").data("iddatadb");
const idquotations = $("#partsModal").data("idquotations");
const endpoint = idquotations
? "save_parts_quotation.php"
: "save_parts.php";
const data = idquotations
? { idquotations: idquotations }
: { iddatadb: iddatadb };
// Raccogli tutti i dati della riga per evitare sovrascritture
const partNumber = $row.find(".part-number").val();
const partDescription = $row.find(".part-description").val().trim();
const mix = getRowMix($row);
const idmatrice = $row.find(".part-matrice").val() || null;
const note = $row.data("note") || null;
if (partId && partId !== "new") {
const $saveStatus = $row.find(".save-status");
const $saveLoading = $row.find(".save-loading");
$saveLoading.show();
$saveStatus.hide();
$.ajax({
url: endpoint,
method: "POST",
data: JSON.stringify({
...data,
parts: [
{
id: partId,
part_number: partNumber,
part_description: partDescription,
mix: mix,
idmatrice: idmatrice,
note: note,
dateexpiry: dateexpiry || null,
},
],
}),
contentType: "application/json",
success: function (response) {
$saveLoading.hide();
if (response.success) {
$saveStatus.show();
setTimeout(() => $saveStatus.hide(), 2000);
} else {
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio della data: ' +
response.message +
"</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
}
},
error: function (xhr, status, error) {
$saveLoading.hide();
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio della data: ' +
error +
" (" +
xhr.status +
")</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
},
});
} else {
markUnsaved();
}
}); });
// =================== // ===================
@@ -1688,7 +1498,7 @@ $(document).ready(function () {
"blur", "blur",
".part-description, .part-number, .part-extra-field", ".part-description, .part-number, .part-extra-field",
function () { function () {
saveRow($(this).closest("tr")); markUnsaved();
}, },
); );
@@ -2050,71 +1860,10 @@ $(document).ready(function () {
const idmatrice = $(this).val(); const idmatrice = $(this).val();
const $row = $(this).closest("tr"); const $row = $(this).closest("tr");
const partId = $row.data("part-id");
const partNumber = $row.find(".part-number").val(); const partNumber = $row.find(".part-number").val();
const $saveStatus = $row.find(".save-status");
const $saveLoading = $row.find(".save-loading");
partMatrice[partNumber] = idmatrice || null; partMatrice[partNumber] = idmatrice || null;
markUnsaved();
if (partId && partId !== "new") {
$saveLoading.show();
$saveStatus.hide();
const iddatadb = $("#partsModal").data("iddatadb");
const idquotations = $("#partsModal").data("idquotations");
const endpoint = idquotations
? "save_matrice_quotation.php"
: "save_matrice.php";
const data = idquotations
? { idquotations: idquotations }
: { iddatadb: iddatadb };
$.ajax({
url: endpoint,
method: "POST",
data: JSON.stringify({
...data,
parts: [{ id: partId, idmatrice: idmatrice || null }],
}),
contentType: "application/json",
success: function (response) {
if (response.success) {
$saveLoading.hide();
$saveStatus.show();
setTimeout(() => $saveStatus.hide(), 2000);
} else {
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio della matrice: ' +
response.message +
"</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
$saveLoading.hide();
}
},
error: function (xhr, status, error) {
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio della matrice: ' +
error +
" (" +
xhr.status +
")</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
$saveLoading.hide();
},
});
}
}); });
// Messaggio se macro selezionata ma nessun risultato sarà gestito dal placeholder Select2 // Messaggio se macro selezionata ma nessun risultato sarà gestito dal placeholder Select2
@@ -2178,7 +1927,152 @@ $(document).ready(function () {
}, 5000); }, 5000);
} }
}); });
function saveAllParts(done = null) {
const $rows = $("#partsTableBody tr");
const iddatadb = $("#partsModal").data("iddatadb");
const idquotations = $("#partsModal").data("idquotations");
const endpoint = idquotations
? "save_parts_quotation.php"
: "save_parts.php";
const ctx = idquotations ? { idquotations } : { iddatadb };
if (!iddatadb && !idquotations) {
if (done) done(false);
return;
}
// Stesso criterio del PHP: una riga nuova viene salvata solo se ha
// descrizione, nota o data. Filtriamo qui per tenere l'allineamento
// per indice tra righe inviate e results[] restituiti.
const rowHasContent = ($row) => {
const hasId = !!getPartId($row);
const desc = $row.find(".part-description").val().trim();
const note = $row.data("note") || "";
const date = $row.find(".part-dateexpiry").val() || "";
return hasId || desc || note || date;
};
const $rowsToSave = $rows.filter(function () {
return rowHasContent($(this));
});
const partsToSave = $rowsToSave
.map(function () {
const $row = $(this);
return {
id: getPartId($row), // null se "new"/""
part_number: $row.find(".part-number").val(),
part_description: $row
.find(".part-description")
.val()
.trim(),
mix: getRowMix($row),
idmatrice: $row.find(".part-matrice").val() || null,
dateexpiry: $row.find(".part-dateexpiry").val() || null,
note: $row.data("note") || null,
extra_field_id:
$row.find(".part-extra-field-id").val() || null,
extra_value_id:
$row.find(".part-extra-value-id").val() || null,
extra_value_text: $row.find(".part-extra-field").length
? ($row.find(".part-extra-field").val() || "").trim()
: null,
};
})
.get();
if (partsToSave.length === 0) {
unsavedChanges = false;
if (done) done(true);
return;
}
const $btn = $("#savePartsBtn");
const originalHtml = $btn.length ? $btn.html() : "";
if ($btn.length) {
$btn.prop("disabled", true).html(
'<i class="fas fa-spinner fa-spin"></i> Salvataggio...',
);
}
$rowsToSave.find(".save-loading").show();
$rowsToSave.find(".save-status").hide();
$.ajax({
url: endpoint,
method: "POST",
data: JSON.stringify({ ...ctx, parts: partsToSave }),
contentType: "application/json",
success: function (response) {
if ($btn.length)
$btn.prop("disabled", false).html(originalHtml);
$rowsToSave.find(".save-loading").hide();
if (response.success) {
// results[] è nell'ordine delle righe inviate
const results = response.results || [];
$rowsToSave.each(function (i) {
const r = results[i];
const newId = r ? r.part_id || r.id : null;
if (newId) setPartId($(this), newId);
});
$rowsToSave.find(".save-status").show();
setTimeout(
() => $rowsToSave.find(".save-status").hide(),
2000,
);
unsavedChanges = false;
if (!$("#quotationeBtn").hasClass("d-none"))
$("#quotationeBtn").addClass("d-none");
if (done) done(true);
} else {
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio: ' +
(response.message || "Errore sconosciuto") +
"</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(() => {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
if (done) done(false);
}
},
error: function (xhr, status, error) {
if ($btn.length)
$btn.prop("disabled", false).html(originalHtml);
$rowsToSave.find(".save-loading").hide();
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio: ' +
error +
" (" +
xhr.status +
")</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(() => {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
if (done) done(false);
},
});
}
$(document).on("click", "#savePartsBtn", function (e) {
e.preventDefault();
saveAllParts();
});
window.saveAllParts = saveAllParts;
function renumberParts() { function renumberParts() {
console.log( console.log(
"Inizio rinumera parts, numero righe:", "Inizio rinumera parts, numero righe:",
@@ -2322,7 +2216,11 @@ $(document).ready(function () {
unsavedChanges = true; unsavedChanges = true;
} }
} }
$(window).on("beforeunload", function () {
if (unsavedChanges && $("#partsModal").hasClass("show")) {
return "Hai modifiche non salvate.";
}
});
$(document).on( $(document).on(
"input change", "input change",
"#partsTableBody input, #partsTableBody select", "#partsTableBody input, #partsTableBody select",
@@ -2612,102 +2510,8 @@ $(document).ready(function () {
$(document).on("change", ".propagate-date-input", function () { $(document).on("change", ".propagate-date-input", function () {
const dateexpiry = $(this).val(); const dateexpiry = $(this).val();
const iddatadb = $("#partsModal").data("iddatadb"); $("#partsTableBody tr").find(".part-dateexpiry").val(dateexpiry);
const idquotations = $("#partsModal").data("idquotations"); markUnsaved();
const endpoint = idquotations
? "save_parts_quotation.php"
: "save_parts.php";
const data = idquotations
? { idquotations: idquotations }
: { iddatadb: iddatadb };
const partsToSave = [];
$("#partsTableBody tr").each(function () {
const $row = $(this);
const partId = $row.data("part-id");
const partNumber = $row.find(".part-number").val();
const partDescription = $row.find(".part-description").val().trim();
const mix = $row.attr("data-is-mix") === "Y" ? "Y" : "N";
const idmatrice = $row.find(".part-matrice").val() || null;
const note = $row.data("note") || null;
partsToSave.push({
id: partId && partId !== "new" ? partId : null,
part_number: partNumber,
part_description: partDescription,
mix: mix,
idmatrice: idmatrice,
note: note,
dateexpiry: dateexpiry || null,
});
if (partId && partId !== "new") {
$row.find(".save-loading").show();
$row.find(".save-status").hide();
}
});
if (partsToSave.length > 0) {
$.ajax({
url: endpoint,
method: "POST",
data: JSON.stringify({
...data,
parts: partsToSave,
}),
contentType: "application/json",
success: function (response) {
$("#partsTableBody tr").each(function () {
const $row = $(this);
const partId = $row.data("part-id");
if (partId && partId !== "new") {
$row.find(".save-loading").hide();
$row.find(".save-status").show();
setTimeout(
() => $row.find(".save-status").hide(),
2000,
);
}
$row.find(".part-dateexpiry").val(dateexpiry);
});
if (!response.success) {
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio della data comune: ' +
response.message +
"</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
}
},
error: function (xhr, status, error) {
$("#partsTableBody tr").each(function () {
const $row = $(this);
$row.find(".save-loading").hide();
});
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio della data comune: ' +
error +
" (" +
xhr.status +
")</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
},
});
} else {
$("#partsTableBody tr").find(".part-dateexpiry").val(dateexpiry);
markUnsaved();
}
}); });
$(document).on("click", ".propagate-note-btn", function () { $(document).on("click", ".propagate-note-btn", function () {
@@ -2723,113 +2527,16 @@ $(document).on("click", ".propagate-note-btn", function () {
$(document).on("click", ".save-common-note-btn", function () { $(document).on("click", ".save-common-note-btn", function () {
const $commonNoteModal = $("#commonNoteModal"); const $commonNoteModal = $("#commonNoteModal");
const note = $commonNoteModal.find(".part-note").val().trim(); const note = $commonNoteModal.find(".part-note").val().trim();
const iddatadb = $("#partsModal").data("iddatadb");
const idquotations = $("#partsModal").data("idquotations");
const endpoint = idquotations
? "save_parts_quotation.php"
: "save_parts.php";
const data = idquotations
? { idquotations: idquotations }
: { iddatadb: iddatadb };
const partsToSave = [];
$("#partsTableBody tr").each(function () { $("#partsTableBody tr").each(function () {
const $row = $(this); const $row = $(this);
const partId = $row.data("part-id"); $row.data("note", note);
const partNumber = $row.find(".part-number").val(); $row.find(".note-btn").toggleClass("has-note", !!note);
const partDescription = $row.find(".part-description").val().trim();
const mix = $row.attr("data-is-mix") === "Y" ? "Y" : "N";
const idmatrice = $row.find(".part-matrice").val() || null;
const dateexpiry = $row.find(".part-dateexpiry").val() || null;
partsToSave.push({
id: partId && partId !== "new" ? partId : null,
part_number: partNumber,
part_description: partDescription,
mix: mix,
idmatrice: idmatrice,
note: note || null,
dateexpiry: dateexpiry,
});
if (partId && partId !== "new") {
$row.find(".save-loading").show();
$row.find(".save-status").hide();
}
}); });
bootstrap.Modal.getInstance(
if (partsToSave.length > 0) { document.getElementById("commonNoteModal"),
$.ajax({ ).hide();
url: endpoint, markUnsaved();
method: "POST",
data: JSON.stringify({
...data,
parts: partsToSave,
}),
contentType: "application/json",
success: function (response) {
$("#partsTableBody tr").each(function () {
const $row = $(this);
const partId = $row.data("part-id");
if (partId && partId !== "new") {
$row.find(".save-loading").hide();
$row.find(".save-status").show();
setTimeout(
() => $row.find(".save-status").hide(),
2000,
);
}
$row.data("note", note);
$row.find(".note-btn").toggleClass("has-note", !!note);
});
bootstrap.Modal.getInstance(
document.getElementById("commonNoteModal"),
).hide();
if (!response.success) {
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio della nota comune: ' +
response.message +
"</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
}
},
error: function (xhr, status, error) {
$("#partsTableBody tr").each(function () {
const $row = $(this);
$row.find(".save-loading").hide();
});
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Errore nel salvataggio della nota comune: ' +
error +
" (" +
xhr.status +
")</div>",
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
},
});
} else {
$("#partsTableBody tr").each(function () {
const $row = $(this);
$row.data("note", note);
$row.find(".note-btn").toggleClass("has-note", !!note);
});
bootstrap.Modal.getInstance(
document.getElementById("commonNoteModal"),
).hide();
markUnsaved();
}
}); });
$(document).on("click", "#showHideImageBtn", function () { $(document).on("click", "#showHideImageBtn", function () {