")
+ .text(part.part_description || "")
+ .html();
+ const escapedNote = part.note
+ ? $("
").text(part.note).html()
+ : "";
const newRow = `
-
- |
- |
-
-
-
-
-
- |
-
-
-
-
-
- |
-
`;
+
+ |
+ |
+
+
+
+
+
+ |
+ |
+
+
+
+
+
+
+ |
+
`;
$("#partsTableBody").append(newRow);
const $select = $("#partsTableBody").find(
`tr[data-part-id="${part.id}"] .part-matrice`,
);
+ const selectedMacro =
+ $("#macro-matrice-filter").val() || "";
initializeSelect2(
$select,
part.part_number,
part.id,
- part.idmatrice,
+ part.idmatrice || null,
+ selectedMacro,
);
if (part.idmatrice) {
partMatrice[part.part_number] = part.idmatrice;
}
});
} else {
- addNewRow(1);
+ addNewRow(1, false); // Riga iniziale normale
}
updateRowButtons();
},
@@ -730,12 +1132,47 @@ $(document).ready(function () {
$(this).remove();
});
}, 5000);
- addNewRow(1);
+ addNewRow(1, false); // Riga iniziale normale
},
});
}
- function initializeSelect2($select, partNumber, partId, idmatrice) {
+ function loadMacroMatrici() {
+ $.ajax({
+ url: "get_macro_matrici.php",
+ method: "GET",
+ dataType: "json",
+ success: function (data) {
+ macroMatrici = data.value || [];
+ initializeMacroMatriceFilter();
+ },
+ error: function (xhr, status, error) {
+ console.error(
+ "Errore nel caricamento delle MacroMatrici:",
+ error,
+ );
+ macroMatrici = [];
+ initializeMacroMatriceFilter();
+ const errorMsg = $(
+ '
Errore nel caricamento delle MacroMatrici: ' +
+ error +
+ " (" +
+ xhr.status +
+ ")
",
+ );
+ $("#partsModal .modal-body").prepend(errorMsg);
+ setTimeout(function () {
+ errorMsg.fadeOut(500, function () {
+ $(this).remove();
+ });
+ }, 5000);
+ },
+ });
+ }
+
+ function initializeMacroMatriceFilter() {
+ const $select = $("#macro-matrice-filter");
+ if ($select.length === 0) return; // Ignora se il filtro non è presente nell'HTML
if (typeof $.fn.select2 === "undefined") {
$select.replaceWith(
'
',
@@ -743,25 +1180,185 @@ $(document).ready(function () {
return;
}
- const options = matrici.map(function (matrice) {
- return { id: matrice.IdMatrice, text: matrice.NomeMatrice };
- });
+ // Popola il dropdown MacroMatrice
+ const options = [
+ { id: "", text: "Tutte le MacroMatrici" },
+ ...macroMatrici.map(function (macro) {
+ return { id: macro, text: macro };
+ }),
+ ];
- $select.select2({
- placeholder: "Seleziona matrice",
+ // Distrugge Select2 esistente, se presente
+ if ($select.hasClass("select2-hidden-accessible")) {
+ $select.select2("destroy");
+ }
+
+ $select.empty().select2({
+ placeholder: "Seleziona MacroMatrice",
allowClear: true,
data: options,
dropdownParent: $("#partsModal"),
+ });
+
+ $select.on("change", function () {
+ const selectedMacro = $(this).val() || "";
+ console.log("Filtro MacroMatrice cambiato:", selectedMacro);
+ // Aggiorna global-matrice
+ initializeGlobalSelect2(selectedMacro);
+ // Aggiorna tutti i part-matrice
+ $("#partsTableBody .part-matrice").each(function () {
+ const $partSelect = $(this);
+ const partNumber = $partSelect
+ .closest("tr")
+ .find(".part-number")
+ .val();
+ const partId = $partSelect.closest("tr").data("part-id");
+ const currentValue =
+ $partSelect.val() || partMatrice[partNumber] || null;
+ initializeSelect2(
+ $partSelect,
+ partNumber,
+ partId,
+ currentValue,
+ selectedMacro,
+ );
+ });
+ });
+ }
+
+ function initializeGlobalSelect2(selectedMacro = null) {
+ const $select = $("#global-matrice");
+ if (typeof $.fn.select2 === "undefined") {
+ $select.replaceWith(
+ '
',
+ );
+ return;
+ }
+
+ // Filtra le matrici in base alla MacroMatrice selezionata
+ const filteredMatrici = selectedMacro
+ ? matrici.filter(
+ (matrice) => matrice.MacroMatrice === selectedMacro,
+ )
+ : matrici;
+
+ // Crea opzioni per Select2
+ const options = filteredMatrici.map(function (matrice) {
+ return { id: matrice.IdMatrice, text: matrice.NomeMatrice };
+ });
+
+ // Memorizza il valore corrente
+ const currentValue = $select.val();
+
+ // Distrugge Select2 esistente, se presente
+ if ($select.hasClass("select2-hidden-accessible")) {
+ $select.select2("destroy");
+ }
+
+ // Inizializza Select2
+ $select.empty().select2({
+ placeholder: filteredMatrici.length
+ ? "Seleziona matrice globale"
+ : "Nessuna matrice disponibile",
+ allowClear: true,
+ data: options,
+ dropdownParent: $("#partsModal"),
+ minimumResultsForSearch: 0, // Abilita ricerca senza limite minimo di caratteri
matcher: function (params, data) {
- if (!params.term || params.term.length < 3) return data;
+ if (!params.term) return data; // Mostra tutte le opzioni se non c'è termine di ricerca
const term = params.term.toUpperCase();
if (data.text.toUpperCase().indexOf(term) >= 0) return data;
return null;
},
});
- if (partId && partId !== "new" && idmatrice) {
- const matrice = matrici.find((m) => m.IdMatrice == idmatrice);
+ // Ripristina il valore corrente se valido
+ if (
+ currentValue &&
+ filteredMatrici.some((m) => m.IdMatrice == currentValue)
+ ) {
+ $select.val(currentValue).trigger("change");
+ } else if (filteredMatrici.length === 0) {
+ const errorMsg = $(
+ '
Nessuna matrice disponibile per la MacroMatrice selezionata.
',
+ );
+ $("#partsModal .modal-body").prepend(errorMsg);
+ setTimeout(function () {
+ errorMsg.fadeOut(500, function () {
+ $(this).remove();
+ });
+ }, 5000);
+ }
+ }
+
+ function initializeSelect2(
+ $select,
+ partNumber,
+ partId,
+ idmatrice,
+ selectedMacro = null,
+ ) {
+ if (typeof $.fn.select2 === "undefined") {
+ $select.replaceWith(
+ '
',
+ );
+ return;
+ }
+
+ // Filtra le matrici in base alla MacroMatrice selezionata
+ const filteredMatrici = selectedMacro
+ ? matrici.filter((m) => m.MacroMatrice === selectedMacro)
+ : matrici;
+
+ // Crea opzioni per Select2, includendo il valore pre-selezionato se non è nel filtro
+ let options = filteredMatrici.map(function (matrice) {
+ return { id: matrice.IdMatrice, text: matrice.NomeMatrice };
+ });
+
+ // Se idmatrice esiste ed è fuori dal filtro, aggiungilo come opzione
+ if (
+ idmatrice &&
+ !filteredMatrici.some((m) => m.IdMatrice == idmatrice)
+ ) {
+ const selectedMatrice = matrici.find(
+ (m) => m.IdMatrice == idmatrice,
+ );
+ if (selectedMatrice) {
+ options.push({
+ id: selectedMatrice.IdMatrice,
+ text: selectedMatrice.NomeMatrice,
+ });
+ }
+ }
+
+ // Memorizza il valore corrente
+ const currentValue = idmatrice || $select.val();
+
+ // Distrugge Select2 esistente, se presente
+ if ($select.hasClass("select2-hidden-accessible")) {
+ $select.select2("destroy");
+ }
+
+ // Inizializza Select2
+ $select.empty().select2({
+ placeholder: filteredMatrici.length
+ ? "Seleziona matrice"
+ : "Nessuna matrice disponibile",
+ allowClear: true,
+ data: options,
+ dropdownParent: $("#partsModal"),
+ minimumResultsForSearch: 0, // Abilita ricerca senza limite minimo di caratteri
+ matcher: function (params, data) {
+ if (!params.term) return data; // Mostra tutte le opzioni se non c'è termine di ricerca
+ const term = params.term.toUpperCase();
+ if (data.text.toUpperCase().indexOf(term) >= 0) return data;
+ return null;
+ },
+ });
+
+ // Ripristina il valore se valido
+ if (partId && partId !== "new" && currentValue) {
+ const matrice = matrici.find((m) => m.IdMatrice == currentValue);
if (matrice) {
const option = new Option(
matrice.NomeMatrice,
@@ -771,6 +1368,10 @@ $(document).ready(function () {
);
$select.append(option).trigger("change");
partMatrice[partNumber] = matrice.IdMatrice;
+ } else {
+ // Aggiusta valore non valido
+ $select.val(null).trigger("change");
+ partMatrice[partNumber] = null;
}
}
@@ -843,33 +1444,29 @@ $(document).ready(function () {
});
}
});
- }
- function initializeGlobalSelect2() {
- const $select = $("#global-matrice");
- if (typeof $.fn.select2 === "undefined") {
- $select.replaceWith(
- '
',
+ // Mostra messaggio se non ci sono matrici
+ if (filteredMatrici.length === 0 && selectedMacro) {
+ const errorMsg = $(
+ '
Nessuna matrice disponibile per la MacroMatrice selezionata.
',
);
- return;
+ $("#partsModal .modal-body").prepend(errorMsg);
+ setTimeout(function () {
+ errorMsg.fadeOut(500, function () {
+ $(this).remove();
+ });
+ }, 5000);
}
- const options = matrici.map(function (matrice) {
- return { id: matrice.IdMatrice, text: matrice.NomeMatrice };
- });
-
- $select.select2({
- placeholder: "Seleziona matrice globale",
- allowClear: true,
- data: options,
- dropdownParent: $("#partsModal"),
- matcher: function (params, data) {
- if (!params.term || params.term.length < 3) return data;
- const term = params.term.toUpperCase();
- if (data.text.toUpperCase().indexOf(term) >= 0) return data;
- return null;
- },
- });
+ // Debug per verificare inizializzazione
+ console.log(
+ "Inizializzo Select2 per partId:",
+ partId,
+ "con idmatrice:",
+ idmatrice,
+ "Macro:",
+ selectedMacro,
+ );
}
$(document).on("click", ".propagate-matrice-btn", function () {
@@ -890,7 +1487,28 @@ $(document).ready(function () {
}
});
+ $(document).on("click", ".propagate-all-btn", function () {
+ const globalVal = $("#global-matrice").val();
+ if (globalVal) {
+ $("#partsTableBody .part-matrice").val(globalVal).trigger("change");
+ } else {
+ const errorMsg = $(
+ '
Seleziona una matrice globale prima di propagare.
',
+ );
+ $("#partsModal .modal-body").prepend(errorMsg);
+ setTimeout(function () {
+ errorMsg.fadeOut(500, function () {
+ $(this).remove();
+ });
+ }, 5000);
+ }
+ });
+
function renumberParts() {
+ console.log(
+ "Inizio rinumera parts, numero righe:",
+ $("#partsTableBody tr").length,
+ );
const $rows = $("#partsTableBody tr");
const iddatadb = $("#partsModal").data("iddatadb");
const idquotations = $("#partsModal").data("idquotations");
@@ -907,12 +1525,23 @@ $(document).ready(function () {
const $row = $(this);
return {
partNumber: $row.find(".part-number").val(),
- partDescription: $row.find(".part-description").val(),
- partId: $row.data("part-id"),
+ partDescription: $row
+ .find(".part-description")
+ .val()
+ .trim(),
+ partId:
+ $row.data("part-id") === "new" ||
+ $row.data("part-id") === ""
+ ? null
+ : $row.data("part-id"),
+ note: $row.data("note") || null,
+ dateexpiry: $row.find(".part-dateexpiry").val() || null,
};
})
.get();
+ console.log("Parts to save prima di rinumerazione:", partsData);
+
partsData.forEach((part, index) => {
const newNumber = index + 1;
newPartMatrice[newNumber] = partMatrice[part.partNumber] || null;
@@ -927,27 +1556,30 @@ $(document).ready(function () {
partMatrice = newPartMatrice;
- const partsToSave = partsData.map((part) => ({
- id: part.partId || null,
- part_number: part.partNumber,
+ const partsToSave = partsData.map((part, index) => ({
+ id: part.partId,
+ part_number: index + 1,
part_description: part.partDescription,
mix: part.partDescription.startsWith("Mix") ? "Y" : "N",
- idmatrice: partMatrice[part.partNumber] || null,
+ idmatrice: partMatrice[index + 1] || null,
+ note: part.note,
+ dateexpiry: part.dateexpiry,
}));
+ console.log("Parts to save inviati al server:", partsToSave);
+
$.ajax({
url: endpoint,
method: "POST",
data: JSON.stringify({ ...data, parts: partsToSave }),
contentType: "application/json",
success: function (response) {
- if (response.success) {
+ console.log("Risposta server:", response);
+ if (response.success && response.part_ids) {
$rows.each(function (index) {
const $row = $(this);
const newPartId =
- response.part_ids && response.part_ids[index]
- ? response.part_ids[index]
- : $row.data("part-id");
+ response.part_ids[index] || $row.data("part-id");
if (newPartId) {
$row.attr("data-part-id", newPartId).data(
"part-id",
@@ -960,11 +1592,15 @@ $(document).ready(function () {
$saveStatus.show();
setTimeout(() => $saveStatus.hide(), 2000);
});
- markUnsaved();
+ $rows
+ .find(".part-number, .part-description")
+ .trigger("blur");
+ unsavedChanges = false;
} else {
+ console.error("Errore risposta server:", response.message);
const errorMsg = $(
'
Errore nella rinumerazione delle parti: ' +
- response.message +
+ (response.message || "Errore sconosciuto") +
"
",
);
$("#partsModal .modal-body").prepend(errorMsg);
@@ -976,6 +1612,11 @@ $(document).ready(function () {
}
},
error: function (xhr, status, error) {
+ console.error(
+ "Errore AJAX rinumerazione:",
+ error,
+ xhr.responseText,
+ );
const errorMsg = $(
'
Errore nella rinumerazione delle parti: ' +
error +
@@ -993,7 +1634,11 @@ $(document).ready(function () {
});
}
- $("#renumberPartsBtn").on("click", renumberParts);
+ $(document).on("click", "#renumberPartsBtn", function (e) {
+ console.log("Clicked Rinumera Parti - Evento triggerato");
+ e.preventDefault();
+ renumberParts();
+ });
function markUnsaved() {
if (!unsavedChanges) {
@@ -1008,10 +1653,232 @@ $(document).ready(function () {
);
$(document).on(
"click",
- ".add-row-global, .add-mix-global, .add-to-mix-row, .remove-row, .propagate-matrice-btn",
+ ".add-row-global, .add-mix-global, .add-mix-row, .remove-row, .propagate-matrice-btn, .propagate-all-btn, .note-btn",
markUnsaved,
);
// Esporta la funzione loadParts per essere usata da import_Edit2.php
window.loadParts = loadParts;
});
+
+$(document).on("change", ".propagate-date-input", function () {
+ const dateexpiry = $(this).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 };
+
+ 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 = partDescription.startsWith("Mix") ? "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 = $(
+ '
Errore nel salvataggio della data comune: ' +
+ response.message +
+ "
",
+ );
+ $("#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 = $(
+ '
Errore nel salvataggio della data comune: ' +
+ error +
+ " (" +
+ xhr.status +
+ ")
",
+ );
+ $("#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 () {
+ const $commonNoteModal = $("#commonNoteModal");
+ $commonNoteModal.find(".part-note").val("");
+ const modalInstance = new bootstrap.Modal($commonNoteModal[0], {
+ backdrop: "static",
+ keyboard: false,
+ });
+ modalInstance.show();
+});
+
+$(document).on("click", ".save-common-note-btn", function () {
+ const $commonNoteModal = $("#commonNoteModal");
+ 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 () {
+ 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 = partDescription.startsWith("Mix") ? "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();
+ }
+ });
+
+ 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.data("note", note);
+ $row.find(".note-btn").toggleClass("has-note", !!note);
+ });
+ bootstrap.Modal.getInstance(
+ document.getElementById("commonNoteModal"),
+ ).hide();
+ if (!response.success) {
+ const errorMsg = $(
+ '
Errore nel salvataggio della nota comune: ' +
+ response.message +
+ "
",
+ );
+ $("#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 = $(
+ '
Errore nel salvataggio della nota comune: ' +
+ error +
+ " (" +
+ xhr.status +
+ ")
",
+ );
+ $("#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();
+ }
+});
diff --git a/public/userarea/save_parts.php b/public/userarea/save_parts.php
index 089732d..1ba83d6 100644
--- a/public/userarea/save_parts.php
+++ b/public/userarea/save_parts.php
@@ -15,50 +15,64 @@ if (!$iddatadb || empty($parts)) {
exit;
}
-$part = $parts[0];
-$partId = $part['id'] ?? null;
-$partNumber = $part['part_number'] ?? null;
-$partDescription = $part['part_description'] ?? '';
-$mix = $part['mix'] ?? 'N';
-$idmatrice = $part['idmatrice'] ?? null; // Nuovo campo idmatrice
+try {
+ $pdo->beginTransaction();
+ $results = [];
-if ($partDescription) {
- try {
- if ($partId) {
- // UPDATE se la parte esiste
- $stmt = $pdo->prepare("UPDATE identification_parts
- SET part_number = :part_number,
- part_description = :part_description,
- mix = :mix,
- idmatrice = :idmatrice,
- updated_at = NOW()
- WHERE id = :id");
- $stmt->execute([
- ':id' => $partId,
- ':part_number' => $partNumber,
- ':part_description' => $partDescription,
- ':mix' => $mix,
- ':idmatrice' => $idmatrice // Può essere NULL
- ]);
- echo json_encode(['success' => true, 'part_id' => $partId, 'part_number' => $partNumber, 'message' => 'Parte aggiornata con successo']);
- } else {
- // INSERT per nuova parte
- $stmt = $pdo->prepare("INSERT INTO identification_parts
- (iddatadb, part_number, part_description, mix, idmatrice, created_at, updated_at)
- VALUES (:iddatadb, :part_number, :part_description, :mix, :idmatrice, NOW(), NOW())");
- $stmt->execute([
- ':iddatadb' => $iddatadb,
- ':part_number' => $partNumber,
- ':part_description' => $partDescription,
- ':mix' => $mix,
- ':idmatrice' => $idmatrice // Può essere NULL
- ]);
- $newId = $pdo->lastInsertId();
- echo json_encode(['success' => true, 'part_id' => $newId, 'part_number' => $partNumber, 'message' => 'Parte salvata con successo']);
+ foreach ($parts as $part) {
+ $partId = $part['id'] ?? null;
+ $partNumber = $part['part_number'] ?? null;
+ $partDescription = $part['part_description'] ?? '';
+ $mix = $part['mix'] ?? 'N';
+ $idmatrice = $part['idmatrice'] ?? null;
+ $note = $part['note'] ?? null;
+ $dateexpiry = $part['dateexpiry'] ?? null;
+
+ if ($partDescription || $note || $dateexpiry) {
+ if ($partId) {
+ // UPDATE se la parte esiste
+ $stmt = $pdo->prepare("UPDATE identification_parts
+ SET part_number = :part_number,
+ part_description = :part_description,
+ mix = :mix,
+ idmatrice = :idmatrice,
+ note = :note,
+ dateexpiry = :dateexpiry,
+ updated_at = NOW()
+ WHERE id = :id");
+ $stmt->execute([
+ ':id' => $partId,
+ ':part_number' => $partNumber,
+ ':part_description' => $partDescription,
+ ':mix' => $mix,
+ ':idmatrice' => $idmatrice,
+ ':note' => $note,
+ ':dateexpiry' => $dateexpiry,
+ ]);
+ $results[] = ['part_id' => $partId, 'part_number' => $partNumber, 'message' => 'Parte aggiornata con successo'];
+ } else {
+ // INSERT per nuova parte
+ $stmt = $pdo->prepare("INSERT INTO identification_parts
+ (iddatadb, part_number, part_description, mix, idmatrice, note, dateexpiry, created_at, updated_at)
+ VALUES (:iddatadb, :part_number, :part_description, :mix, :idmatrice, :note, :dateexpiry, NOW(), NOW())");
+ $stmt->execute([
+ ':iddatadb' => $iddatadb,
+ ':part_number' => $partNumber,
+ ':part_description' => $partDescription,
+ ':mix' => $mix,
+ ':idmatrice' => $idmatrice,
+ ':note' => $note,
+ ':dateexpiry' => $dateexpiry,
+ ]);
+ $newId = $pdo->lastInsertId();
+ $results[] = ['part_id' => $newId, 'part_number' => $partNumber, 'message' => 'Parte salvata con successo'];
+ }
}
- } catch (PDOException $e) {
- echo json_encode(['success' => false, 'message' => 'Errore nel salvataggio: ' . $e->getMessage()]);
}
-} else {
- echo json_encode(['success' => false, 'message' => 'Descrizione mancante']);
+
+ $pdo->commit();
+ echo json_encode(['success' => true, 'results' => $results]);
+} catch (PDOException $e) {
+ $pdo->rollBack();
+ echo json_encode(['success' => false, 'message' => 'Errore nel salvataggio: ' . $e->getMessage()]);
}