From a0b12463c05b9c5432f65f083ae77fbd81b1b0bf Mon Sep 17 00:00:00 2001 From: Claudio Date: Fri, 3 Oct 2025 08:52:37 +0200 Subject: [PATCH] fixed for matrici --- public/userarea/import_edit2.php | 2 + public/userarea/modal_parts.php | 70 ++++++++- public/userarea/parts.js | 241 ++++++++++++++++++++++++++++++- 3 files changed, 303 insertions(+), 10 deletions(-) diff --git a/public/userarea/import_edit2.php b/public/userarea/import_edit2.php index e721821..94f2340 100644 --- a/public/userarea/import_edit2.php +++ b/public/userarea/import_edit2.php @@ -80,6 +80,7 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { + \ No newline at end of file diff --git a/public/userarea/parts.js b/public/userarea/parts.js index 0170fee..c306715 100644 --- a/public/userarea/parts.js +++ b/public/userarea/parts.js @@ -12,11 +12,13 @@ $(document).ready(function () { let photoAnnotations = {}; let partColors = {}; + let partMatrice = {}; let selectedPartNumber = null; let unsavedChanges = false; let fabricCanvas = null; let descriptionTextbox = null; let markerObjects = {}; + let matrici = []; // =================== // VOICE RECOGNITION SETUP @@ -137,8 +139,35 @@ $(document).ready(function () { .data("iddatadb", iddatadb) .data("idquotations", idquotations); - loadPhoto(iddatadb, idquotations); - loadExistingParts(iddatadb, idquotations); + // Precarica le matrici se iddatadb (assumendo matrici solo per iddatadb) + if (iddatadb) { + if (matrici.length === 0) { + $.ajax({ + url: "get_matrice.php", + method: "GET", + dataType: "json", + success: function (data) { + matrici = data.value || []; + initializeGlobalSelect2(); + loadPhoto(iddatadb, idquotations); + loadExistingParts(iddatadb, idquotations); + }, + error: function (xhr, status, error) { + matrici = []; + initializeGlobalSelect2(); + loadPhoto(iddatadb, idquotations); + loadExistingParts(iddatadb, idquotations); + }, + }); + } else { + initializeGlobalSelect2(); + loadPhoto(iddatadb, idquotations); + loadExistingParts(iddatadb, idquotations); + } + } else { + loadPhoto(iddatadb, idquotations); + loadExistingParts(iddatadb, idquotations); + } const modal = new bootstrap.Modal( document.getElementById("partsModal"), @@ -175,6 +204,7 @@ $(document).ready(function () { }; photoAnnotations = {}; partColors = {}; + partMatrice = {}; selectedPartNumber = null; unsavedChanges = false; if (fabricCanvas) { @@ -184,10 +214,12 @@ $(document).ready(function () { } descriptionTextbox = null; markerObjects = {}; + matrici = []; // Clear UI elements $("#photoSelectorContainer").empty().hide(); $("#samplePhoto").attr("src", ""); $("#partsTableBody").empty(); + $("#global-matrice").empty(); // Pulisci select globale se presente // Remove any temporary messages $(".temp-alert").remove(); }); @@ -535,6 +567,7 @@ $(document).ready(function () { if (response.success) { $row.remove(); delete partColors[partNumber]; + delete partMatrice[partNumber]; if (markerObjects[partNumber]) { fabricCanvas.remove(markerObjects[partNumber]); delete markerObjects[partNumber]; @@ -576,6 +609,7 @@ $(document).ready(function () { } else { $row.remove(); delete partColors[partNumber]; + delete partMatrice[partNumber]; if (markerObjects[partNumber]) { fabricCanvas.remove(markerObjects[partNumber]); delete markerObjects[partNumber]; @@ -717,6 +751,9 @@ $(document).ready(function () { `; $("#partsTableBody").append(newRow); partColors[part.part_number] = defaultColor; + if (part.idmatrice) { + partMatrice[part.part_number] = part.idmatrice; + } }); } else { addNewRow(1); @@ -743,6 +780,163 @@ $(document).ready(function () { }); } + // Funzione per inizializzare Select2 sulle tendine delle matrici + function initializeSelect2($select, partNumber, partId, idmatrice) { + if (typeof $.fn.select2 === "undefined") { + $select.replaceWith( + '', + ); + return; + } + + const options = matrici.map(function (matrice) { + return { + id: matrice.IdMatrice, + text: matrice.NomeMatriceTraduzione, + }; + }); + + $select.select2({ + placeholder: "Seleziona matrice", + 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; + }, + }); + + if (partId && partId !== "new" && idmatrice) { + const matrice = matrici.find((m) => m.IdMatrice == idmatrice); + if (matrice) { + const option = new Option( + matrice.NomeMatriceTraduzione, + matrice.IdMatrice, + true, + true, + ); + $select.append(option).trigger("change"); + partMatrice[partNumber] = matrice.IdMatrice; + } + } + + $select.on("change", function () { + const idmatrice = $(this).val(); + const $listItem = $(this).closest("li"); + const $saveStatus = $listItem.find(".save-status"); + const $saveLoading = $listItem.find(".save-loading"); + + partMatrice[partNumber] = idmatrice || null; + + 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"; // Assumendo esista per quotations, altrimenti adatta + 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 = $( + '", + ); + $("#partsModal .modal-body").prepend(errorMsg); + setTimeout(function () { + errorMsg.fadeOut(500, function () { + $(this).remove(); + }); + }, 5000); + $saveLoading.hide(); + } + }, + error: function (xhr, status, error) { + const errorMsg = $( + '", + ); + $("#partsModal .modal-body").prepend(errorMsg); + setTimeout(function () { + errorMsg.fadeOut(500, function () { + $(this).remove(); + }); + }, 5000); + $saveLoading.hide(); + }, + }); + } + }); + } + + // Funzione per inizializzare Select2 sul dropdown globale + function initializeGlobalSelect2() { + const $select = $("#global-matrice"); + if (typeof $.fn.select2 === "undefined") { + $select.replaceWith( + '', + ); + return; + } + + const options = matrici.map(function (matrice) { + return { + id: matrice.IdMatrice, + text: matrice.NomeMatriceTraduzione, + }; + }); + + $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; + }, + }); + } + // =================== // PARTS LIST // =================== @@ -763,6 +957,7 @@ $(document).ready(function () { $("#partsTableBody tr").each(function () { const partNumber = $(this).find(".part-number").val(); const partDescription = $(this).find(".part-description").val(); + const partId = $(this).data("part-id"); const partColor = partColors[partNumber] || (partDescription.startsWith("Mix") ? "#0000ff" : "#ff0000"); @@ -778,17 +973,30 @@ $(document).ready(function () { ) .join(""); const listItem = ` -
  • +
  • ${partNumber} - ${partDescription}
    + +
    ${colorOptions}
    + +
  • `; $("#partsList").append(listItem); + const $select = $("#partsList").find( + `li[data-part-number="${partNumber}"] .part-matrice`, + ); + initializeSelect2( + $select, + partNumber, + partId, + partMatrice[partNumber], + ); } }); @@ -862,11 +1070,32 @@ $(document).ready(function () { } }); + $(document).on("click", ".propagate-matrice-btn", function () { + const $listItem = $(this).closest("li"); + const globalVal = $("#global-matrice").val(); + if (globalVal) { + $listItem.find(".part-matrice").val(globalVal).trigger("change"); + } else { + const errorMsg = $( + '', + ); + $("#partsModal .modal-body").prepend(errorMsg); + setTimeout(function () { + errorMsg.fadeOut(500, function () { + $(this).remove(); + }); + }, 5000); + } + }); + $("#partsList").on("click", "li", function (e) { if ( $(e.target).hasClass("add-to-mix-btn") || $(e.target).hasClass("color-option") || - $(e.target).closest(".color-picker-container").length + $(e.target).closest(".color-picker-container").length || + $(e.target).hasClass("part-matrice") || + $(e.target).closest(".select2-container").length || + $(e.target).hasClass("propagate-matrice-btn") ) return; selectedPartNumber = $(this).data("part-number"); @@ -892,6 +1121,7 @@ $(document).ready(function () { ? { idquotations: idquotations } : { iddatadb: iddatadb }; let newPartColors = {}; + let newPartMatrice = {}; let newMarkerObjects = {}; let partsData = $rows @@ -908,6 +1138,7 @@ $(document).ready(function () { partsData.forEach((part, index) => { const newNumber = index + 1; newPartColors[newNumber] = partColors[part.partNumber] || "#ff0000"; + newPartMatrice[newNumber] = partMatrice[part.partNumber] || null; if (markerObjects[part.partNumber]) { newMarkerObjects[newNumber] = markerObjects[part.partNumber]; } @@ -934,6 +1165,7 @@ $(document).ready(function () { } partColors = newPartColors; + partMatrice = newPartMatrice; markerObjects = newMarkerObjects; const partsToSave = partsData.map((part) => ({ @@ -941,6 +1173,7 @@ $(document).ready(function () { part_number: part.partNumber, part_description: part.partDescription, mix: part.partDescription.startsWith("Mix") ? "Y" : "N", + idmatrice: partMatrice[part.partNumber] || null, })); $.ajax({