diff --git a/public/userarea/manage-worksheet.php b/public/userarea/manage-worksheet.php index 95b0746..a5d2e0a 100644 --- a/public/userarea/manage-worksheet.php +++ b/public/userarea/manage-worksheet.php @@ -12,13 +12,12 @@ $worksheet_id = isset($_GET['id']) && is_numeric($_GET['id']) ? (int)$_GET['id'] // AJAX HANDLERS if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['ajax'] == '1') { - // Ensure clean JSON output (remove any echoed HTML/notices) + // Ensure clean JSON output while (ob_get_level()) { ob_end_clean(); } - header('Content-Type: application/json; charset=utf-8'); - header('Content-Type: application/json'); + header('Content-Type: application/json; charset=utf-8'); $action = $_POST['action'] ?? ''; @@ -45,6 +44,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj $freq_jig = trim($_POST['control_frequency_jig'] ?? ''); $mode_jig = trim($_POST['control_mode_jig'] ?? ''); + // Package data moved to worksheet header + $requested_package_code = trim($_POST['requested_package_code'] ?? ''); + $meters_per_package = $_POST['meters_per_package'] !== '' ? (int)$_POST['meters_per_package'] : null; + $meters_per_package_tolerance = trim($_POST['meters_per_package_tolerance'] ?? ''); + $meters_per_package_notes = trim($_POST['meters_per_package_notes'] ?? ''); + $box_type = trim($_POST['box_type'] ?? ''); $pkg_box = $_POST['packages_or_pieces_per_box'] !== '' ? (int)$_POST['packages_or_pieces_per_box'] : null; $m_box = $_POST['meters_per_box'] !== '' ? (int)$_POST['meters_per_box'] : null; @@ -73,6 +78,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj control_frequency_drawing = ?, control_frequency_jig = ?, control_mode_jig = ?, + requested_package_code = ?, + meters_per_package = ?, + meters_per_package_tolerance = ?, + meters_per_package_notes = ?, box_type = ?, packages_or_pieces_per_box = ?, meters_per_box = ?, @@ -97,6 +106,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj $freq_draw !== '' ? $freq_draw : null, $freq_jig !== '' ? $freq_jig : null, $mode_jig !== '' ? $mode_jig : null, + $requested_package_code !== '' ? $requested_package_code : null, + $meters_per_package, + $meters_per_package_tolerance !== '' ? $meters_per_package_tolerance : null, + $meters_per_package_notes !== '' ? $meters_per_package_notes : null, $box_type !== '' ? $box_type : null, $pkg_box, $m_box, @@ -117,19 +130,26 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj $stmt = $pdo->prepare(" INSERT INTO work_sheets - (idmatrice, worksheet_date, customer_name, profile_type_code, marking, - prod_control_measure_settings, control_frequency_cut, control_frequency_drawing, control_frequency_jig, control_mode_jig, - box_type, packages_or_pieces_per_box, meters_per_box, - pallet_type, boxes_or_packages_per_pallet, - speed_expected_kg_h, speed_actual_kg_h, speed_expected_m_h, speed_actual_m_h, - approved_by, notes) + ( + idmatrice, worksheet_date, customer_name, profile_type_code, marking, + prod_control_measure_settings, control_frequency_cut, control_frequency_drawing, + control_frequency_jig, control_mode_jig, + requested_package_code, meters_per_package, meters_per_package_tolerance, meters_per_package_notes, + box_type, packages_or_pieces_per_box, meters_per_box, + pallet_type, boxes_or_packages_per_pallet, + speed_expected_kg_h, speed_actual_kg_h, speed_expected_m_h, speed_actual_m_h, + approved_by, notes + ) VALUES - (?, ?, ?, ?, ?, - ?, ?, ?, ?, ?, - ?, ?, ?, - ?, ?, - ?, ?, ?, ?, - ?, ?) + ( + ?, ?, ?, ?, ?, + ?, ?, ?, ?, ?, + ?, ?, ?, ?, + ?, ?, ?, + ?, ?, + ?, ?, ?, ?, + ?, ? + ) "); $stmt->execute([ $idmatrice, @@ -142,6 +162,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj $freq_draw !== '' ? $freq_draw : null, $freq_jig !== '' ? $freq_jig : null, $mode_jig !== '' ? $mode_jig : null, + $requested_package_code !== '' ? $requested_package_code : null, + $meters_per_package, + $meters_per_package_tolerance !== '' ? $meters_per_package_tolerance : null, + $meters_per_package_notes !== '' ? $meters_per_package_notes : null, $box_type !== '' ? $box_type : null, $pkg_box, $m_box, @@ -168,10 +192,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj exit; } - $row_id = (int)($_POST['row_id'] ?? 0); - $idmescola = (int)($_POST['idmescola'] ?? 0); - $pos = 0; // auto-assign - + $row_id = (int)($_POST['row_id'] ?? 0); + $idmescola = (int)($_POST['idmescola'] ?? 0); + $pos = 0; // auto-assign if ($idmescola <= 0) { echo json_encode(['success' => false, 'message' => 'Seleziona una mescola']); @@ -186,43 +209,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj $lub_type = ($_POST['lubrication_type'] ?? '') !== '' ? $_POST['lubrication_type'] : null; $lub_note = trim($_POST['lubrication_notes'] ?? ''); - $pkg_code = trim($_POST['requested_package_code'] ?? ''); - $m_pkg = $_POST['meters_per_package'] !== '' ? (int)$_POST['meters_per_package'] : null; - $m_pkg_note = trim($_POST['meters_per_package_notes'] ?? ''); - $m_pkg_tol = trim($_POST['meters_per_package_tolerance'] ?? ''); - - // Use transaction to keep constraints consistent $pdo->beginTransaction(); if ($row_id > 0) { - - // Auto-assign mix_position = max + 1 for this worksheet - $stmtPos = $pdo->prepare(" - SELECT IFNULL(MAX(mix_position), 0) + 1 - FROM work_sheet_mescole - WHERE worksheet_id = ? - FOR UPDATE - "); - $stmtPos->execute([$worksheet_id]); - $pos = (int)$stmtPos->fetchColumn(); - if ($pos <= 0) $pos = 1; - - $stmt = $pdo->prepare(" - UPDATE work_sheet_mescole SET - idmescola = ?, - mix_weight_g_m = ?, - required_density = ?, - required_hardness_shore_a = ?, - lubrication_type = ?, - lubrication_notes = ?, - requested_package_code = ?, - meters_per_package = ?, - meters_per_package_notes = ?, - meters_per_package_tolerance = ? - WHERE id = ? AND worksheet_id = ? - "); - + UPDATE work_sheet_mescole SET + idmescola = ?, + mix_weight_g_m = ?, + required_density = ?, + required_hardness_shore_a = ?, + lubrication_type = ?, + lubrication_notes = ? + WHERE id = ? AND worksheet_id = ? + "); $stmt->execute([ $idmescola, $mix_weight, @@ -230,37 +229,33 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj $hardness !== '' ? $hardness : null, $lub_type, $lub_note !== '' ? $lub_note : null, - $pkg_code !== '' ? $pkg_code : null, - $m_pkg, - $m_pkg_note !== '' ? $m_pkg_note : null, - $m_pkg_tol !== '' ? $m_pkg_tol : null, $row_id, $worksheet_id ]); - $pdo->commit(); echo json_encode(['success' => true]); exit; } - // If position already used, auto-assign next (max+1) - $chk = $pdo->prepare("SELECT COUNT(*) FROM work_sheet_mescole WHERE worksheet_id = ? AND mix_position = ?"); - $chk->execute([$worksheet_id, $pos]); - if ((int)$chk->fetchColumn() > 0) { - $mx = $pdo->prepare("SELECT IFNULL(MAX(mix_position),0) + 1 FROM work_sheet_mescole WHERE worksheet_id = ?"); - $mx->execute([$worksheet_id]); - $pos = (int)$mx->fetchColumn(); - } - - + $stmtPos = $pdo->prepare(" + SELECT IFNULL(MAX(mix_position), 0) + 1 + FROM work_sheet_mescole + WHERE worksheet_id = ? + FOR UPDATE + "); + $stmtPos->execute([$worksheet_id]); + $pos = (int)$stmtPos->fetchColumn(); + if ($pos <= 0) $pos = 1; $stmt = $pdo->prepare(" INSERT INTO work_sheet_mescole - (worksheet_id, idmescola, mix_position, mix_weight_g_m, required_density, required_hardness_shore_a, - lubrication_type, lubrication_notes, - requested_package_code, meters_per_package, meters_per_package_notes, meters_per_package_tolerance) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ( + worksheet_id, idmescola, mix_position, mix_weight_g_m, + required_density, required_hardness_shore_a, + lubrication_type, lubrication_notes + ) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([ $worksheet_id, @@ -270,11 +265,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj $density !== '' ? $density : null, $hardness !== '' ? $hardness : null, $lub_type, - $lub_note !== '' ? $lub_note : null, - $pkg_code !== '' ? $pkg_code : null, - $m_pkg, - $m_pkg_note !== '' ? $m_pkg_note : null, - $m_pkg_tol !== '' ? $m_pkg_tol : null + $lub_note !== '' ? $lub_note : null ]); $pdo->commit(); @@ -300,7 +291,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj echo json_encode(['success' => false, 'message' => 'Azione sconosciuta']); exit; } catch (Exception $e) { - if ($pdo->inTransaction()) $pdo->rollBack(); + if ($pdo->inTransaction()) { + $pdo->rollBack(); + } echo json_encode(['success' => false, 'message' => $e->getMessage()]); exit; } @@ -313,7 +306,7 @@ $matrici = $pdo->query(" ORDER BY nome ASC ")->fetchAll(PDO::FETCH_ASSOC); -// Load worksheet lookup options +// Load generic worksheet lookup options $lookup = []; $lookupDefault = []; @@ -333,6 +326,33 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) { } } +// Load packaging items from dedicated table +$packagingLookup = [ + 'PACKAGING_TYPE' => [], + 'BOX' => [], + 'PALLET' => [] +]; + +$stmt = $pdo->prepare(" + SELECT category, item_name, item_code + FROM packaging_items + WHERE is_active = 1 + AND category IN ('PACKAGING_TYPE', 'BOX', 'PALLET') + ORDER BY category ASC, item_name ASC +"); +$stmt->execute(); + +foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) { + $label = $r['item_name']; + if (!empty($r['item_code'])) { + $label .= ' — ' . $r['item_code']; + } + + $packagingLookup[$r['category']][] = [ + 'value' => $r['item_code'], + 'label' => $label + ]; +} // Load mescole dropdown for modal $mescole = $pdo->query(" @@ -398,9 +418,25 @@ function ws_options($rows, $selectedValue, $defaultValue = '') return $html; } +function packaging_options($rows, $selectedValue = '') +{ + $selectedValue = (string)$selectedValue; + + $html = ''; + if (!$rows) return $html; + + foreach ($rows as $r) { + $val = h($r['value']); + $lab = h($r['label']); + $sel = ((string)$r['value'] === $selectedValue) ? ' selected' : ''; + $html .= ""; + } + + return $html; +} + $isEdit = ($worksheet_id > 0); ?> - @@ -411,7 +447,6 @@ $isEdit = ($worksheet_id > 0);