fixed add instrument

This commit is contained in:
Claudio 2025-12-05 12:29:33 +01:00
parent 37909e8175
commit 824ae278d1
6 changed files with 717 additions and 51 deletions

View File

@ -0,0 +1,47 @@
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
header('Content-Type: application/json');
require_once 'include/headscript.php';
try {
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
$id = (int)($_POST['id'] ?? 0);
$name = trim($_POST['name'] ?? '');
$tool_type = trim($_POST['tool_type'] ?? '');
$description = trim($_POST['description'] ?? '');
$is_active = isset($_POST['is_active']) ? (int)$_POST['is_active'] : 1;
if ($id <= 0) {
echo json_encode(['success' => false, 'message' => 'Invalid ID.']);
exit;
}
if ($name === '') {
echo json_encode(['success' => false, 'message' => 'Name is required.']);
exit;
}
$sql = "UPDATE production_tools
SET name = :name,
tool_type = :tool_type,
description = :description,
is_active = :is_active
WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
'name' => $name,
'tool_type' => $tool_type ?: null,
'description' => $description ?: null,
'is_active' => $is_active,
'id' => $id
]);
echo json_encode(['success' => true]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}

View File

@ -0,0 +1,57 @@
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
header('Content-Type: application/json');
require_once 'include/headscript.php';
try {
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
$id = (int)($_POST['id'] ?? 0);
$name = trim($_POST['name'] ?? '');
$registrationNumber = trim($_POST['registration_number'] ?? '');
$serialNumber = trim($_POST['serial_number'] ?? '');
$toolType = trim($_POST['tool_type'] ?? '');
$manufacturer = trim($_POST['manufacturer'] ?? '');
$description = trim($_POST['description'] ?? '');
$isActive = isset($_POST['is_active']) ? (int)$_POST['is_active'] : 1;
if ($id <= 0) {
echo json_encode(['success' => false, 'message' => 'Invalid ID.']);
exit;
}
if ($name === '') {
echo json_encode(['success' => false, 'message' => 'Name is required.']);
exit;
}
$sql = "UPDATE production_tools
SET name = :name,
registration_number = :registration_number,
serial_number = :serial_number,
tool_type = :tool_type,
manufacturer = :manufacturer,
description = :description,
is_active = :is_active
WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
'name' => $name,
'registration_number' => $registrationNumber ?: null,
'serial_number' => $serialNumber ?: null,
'tool_type' => $toolType ?: null,
'manufacturer' => $manufacturer ?: null,
'description' => $description ?: null,
'is_active' => $isActive,
'id' => $id
]);
echo json_encode(['success' => true]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}

View File

@ -264,31 +264,47 @@ if (!empty($_GET['ajax'])) {
$lineRaw = $_GET['line'] ?? '';
$lineArray = $lineRaw !== '' ? explode(',', $lineRaw) : [];
// --- RECORD IN PRODUZIONE (2,7,8)
// --- RECORD IN PRODUZIONE (2,7,8)
$sql = "SELECT
p.*,
m.nome AS matrice,
m.photo AS matrice_photo,
(
SELECT GROUP_CONCAT(m.nome ORDER BY m.nome SEPARATOR ' | ')
p.*,
m.nome AS matrice,
m.photo AS matrice_photo,
(
SELECT GROUP_CONCAT(mes.nome ORDER BY mes.nome SEPARATOR ' | ')
FROM productiondata_mescole pm
JOIN mescole m ON m.id = pm.id_mescola
JOIN mescole mes ON mes.id = pm.id_mescola
WHERE pm.id_productiondata = p.id
) AS mescole_list,
l.name AS linea,
c.nome AS cliente,
s.nome AS status_nome,
s.badge_color,
s.line_color,
p.tempo_totale_produzione
FROM productiondata p
LEFT JOIN matrice m ON p.idmatrice = m.id
LEFT JOIN production_lines l ON p.id_linea = l.id
LEFT JOIN clients c ON p.id_cliente = c.id
LEFT JOIN production_status s ON p.id_status = s.id
WHERE p.id_status IN (2, 7, 8)
" . (!empty($lineArray) ? " AND p.id_linea IN (" . implode(',', array_map('intval', $lineArray)) . ")" : "") . "
ORDER BY l.line_number, p.Data";
) AS mescole_list,
l.name AS linea,
c.nome AS cliente,
s.nome AS status_nome,
s.badge_color,
s.line_color,
p.tempo_totale_produzione,
-- 🔧 ADD: tools list & count
(
SELECT GROUP_CONCAT(t.name ORDER BY t.name SEPARATOR ' | ')
FROM productiondata_tools pt
JOIN production_tools t ON t.id = pt.tool_id
WHERE pt.productiondata_id = p.id
) AS tools_list,
(
SELECT COUNT(*)
FROM productiondata_tools pt
WHERE pt.productiondata_id = p.id
) AS tools_count
FROM productiondata p
LEFT JOIN matrice m ON p.idmatrice = m.id
LEFT JOIN production_lines l ON p.id_linea = l.id
LEFT JOIN clients c ON p.id_cliente = c.id
LEFT JOIN production_status s ON p.id_status = s.id
WHERE p.id_status IN (2, 7, 8)
" . (!empty($lineArray) ? " AND p.id_linea IN (" . implode(',', array_map('intval', $lineArray)) . ")" : "") . "
ORDER BY l.line_number, p.Data";
$stmt = $pdo->prepare($sql);
$stmt->execute();
@ -312,30 +328,44 @@ if (!empty($_GET['ajax'])) {
// --- RECORD IN STATO 6 ORDINATI PER PRIORITY
$sql2 = "SELECT
p.*,
m.nome AS matrice,
m.photo AS matrice_photo,
(
SELECT GROUP_CONCAT(m2.nome ORDER BY m2.nome SEPARATOR ' | ')
FROM productiondata_mescole pm
JOIN mescole m2 ON m2.id = pm.id_mescola
WHERE pm.id_productiondata = p.id
) AS mescole_list,
l.name AS linea,
c.nome AS cliente,
s.nome AS status_nome,
s.badge_color,
s.line_color,
p.tempo_totale_produzione
FROM productiondata p
LEFT JOIN matrice m ON p.idmatrice = m.id
LEFT JOIN production_lines l ON p.id_linea = l.id
LEFT JOIN clients c ON p.id_cliente = c.id
LEFT JOIN production_status s ON p.id_status = s.id
WHERE p.id_status = 6
" . (!empty($lineArray) ? " AND p.id_linea IN (" . implode(',', array_map('intval', $lineArray)) . ")" : "") . "
ORDER BY p.priority ASC
";
p.*,
m.nome AS matrice,
m.photo AS matrice_photo,
(
SELECT GROUP_CONCAT(mes.nome ORDER BY mes.nome SEPARATOR ' | ')
FROM productiondata_mescole pm
JOIN mescole mes ON mes.id = pm.id_mescola
WHERE pm.id_productiondata = p.id
) AS mescole_list,
l.name AS linea,
c.nome AS cliente,
s.nome AS status_nome,
s.badge_color,
s.line_color,
p.tempo_totale_produzione,
-- 🔧 ADD: tools list & count
(
SELECT GROUP_CONCAT(t.name ORDER BY t.name SEPARATOR ' | ')
FROM productiondata_tools pt
JOIN production_tools t ON t.id = pt.tool_id
WHERE pt.productiondata_id = p.id
) AS tools_list,
(
SELECT COUNT(*)
FROM productiondata_tools pt
WHERE pt.productiondata_id = p.id
) AS tools_count
FROM productiondata p
LEFT JOIN matrice m ON p.idmatrice = m.id
LEFT JOIN production_lines l ON p.id_linea = l.id
LEFT JOIN clients c ON p.id_cliente = c.id
LEFT JOIN production_status s ON p.id_status = s.id
WHERE p.id_status = 6
" . (!empty($lineArray) ? " AND p.id_linea IN (" . implode(',', array_map('intval', $lineArray)) . ")" : "") . "
ORDER BY p.priority ASC
";
$stmt2 = $pdo->prepare($sql2);
$stmt2->execute();
@ -479,10 +509,12 @@ if (!empty($_GET['ajax'])) {
#recordsContainer {
flex: 1;
overflow-y: auto;
padding: 1rem 1.5rem;
/* top right bottom left */
padding: 1rem 1.5rem 5rem 1.5rem;
background: #f8fafc;
}
.record-card {
margin-bottom: 1rem;
border-radius: 1rem;
@ -893,6 +925,11 @@ if (!empty($_GET['ajax'])) {
font-weight: 700;
font-size: 1rem;
}
.photo-tools-row .qc-right {
margin-left: auto;
/* la porta tutta a destra */
}
</style>
</head>
@ -1260,6 +1297,22 @@ if (!empty($_GET['ajax'])) {
startAllTimers();
setupEventHandlers();
// 🔧 Sposta l'icona qualità all'estrema destra della riga
$('.photo-tools-row').each(function() {
const $row = $(this);
const $qc = $row.find('.qc-btn').last(); // assume che il bottone qualità abbia classe .qc-btn
if ($qc.length) {
// la stacco dal gruppo foto
$qc.detach();
// la ri-attacco come ultimo elemento della riga
$qc.appendTo($row);
// le do la classe che la spinge a destra
$qc.addClass('qc-right');
}
});
$(document).on("click", ".photo-btn", function(e) {
e.stopPropagation();

View File

@ -0,0 +1,393 @@
<?php include('include/headscript.php'); ?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="assets/images/logo-zibo.svg" type="image/svg+xml" />
<?php include('cssinclude.php'); ?>
<title>Gestione Strumenti Aggiuntivi - <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?></title>
<!-- jQuery, Bootstrap, DataTables, SweetAlert -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<link rel="stylesheet"
href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<style>
body {
font-size: 1.05rem;
background: #f8fafc;
}
.card {
border-radius: 16px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}
th,
td {
vertical-align: middle !important;
text-align: center !important;
}
.badge-active {
background: #198754;
color: white;
padding: 6px 12px;
border-radius: 6px;
}
.badge-inactive {
background: #6c757d;
color: white;
padding: 6px 12px;
border-radius: 6px;
}
.btn-action {
border: none;
background: transparent;
cursor: pointer;
font-size: 1.3rem;
}
.btn-action.edit {
color: #0d6efd;
}
.btn-action.delete {
color: #dc3545;
}
.btn-action:hover {
transform: scale(1.2);
}
.back-dashboard {
background-color: #cfe3ff !important;
color: #1f2d3d !important;
border-radius: 10px;
font-weight: 600;
padding: 10px 18px;
}
</style>
</head>
<body>
<div class="wrapper toggled">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="card p-3">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0 text-center w-100">Gestione Strumenti Aggiuntivi</h5>
<button type="button"
class="btn back-dashboard position-absolute end-0 me-3"
onclick="location.href='production_dashboard.php'">
↩️ Torna alla Dashboard
</button>
</div>
<div class="card-body">
<div class="d-flex justify-content-between align-items-center mb-3">
<h6 class="fw-semibold mb-0">Elenco Strumenti</h6>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addToolModal">
Aggiungi Strumento
</button>
</div>
<div class="table-responsive">
<table id="tabTools" class="table table-striped align-middle">
<thead>
<tr>
<th>Nome</th>
<th>N. Registrazione</th>
<th>N. Seriale</th>
<th>Tipo</th>
<th>Produttore</th>
<th>Descrizione</th>
<th>Stato</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
$stmt = $pdo->query("SELECT * FROM production_tools ORDER BY id ASC");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)):
?>
<tr>
<td><?= htmlspecialchars($row['name']) ?></td>
<td><?= htmlspecialchars($row['registration_number'] ?? '') ?></td>
<td><?= htmlspecialchars($row['serial_number'] ?? '') ?></td>
<td><?= htmlspecialchars($row['tool_type'] ?? '') ?></td>
<td><?= htmlspecialchars($row['manufacturer'] ?? '') ?></td>
<td><?= nl2br(htmlspecialchars($row['description'] ?? '')) ?></td>
<td>
<?php if ((int)$row['is_active'] === 1): ?>
<span class="badge-active">Attivo</span>
<?php else: ?>
<span class="badge-inactive">Non attivo</span>
<?php endif; ?>
</td>
<td>
<button class="btn-action edit"
title="Modifica"
data-id="<?= $row['id'] ?>"
data-name="<?= htmlspecialchars($row['name'], ENT_QUOTES) ?>"
data-reg="<?= htmlspecialchars($row['registration_number'] ?? '', ENT_QUOTES) ?>"
data-serial="<?= htmlspecialchars($row['serial_number'] ?? '', ENT_QUOTES) ?>"
data-type="<?= htmlspecialchars($row['tool_type'] ?? '', ENT_QUOTES) ?>"
data-manufacturer="<?= htmlspecialchars($row['manufacturer'] ?? '', ENT_QUOTES) ?>"
data-desc="<?= htmlspecialchars($row['description'] ?? '', ENT_QUOTES) ?>"
data-active="<?= (int)$row['is_active'] ?>">
<i class="fas fa-edit"></i>
</button>
<button class="btn-action delete"
title="Elimina"
data-id="<?= $row['id'] ?>">
<i class="fas fa-trash"></i>
</button>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<!-- MODALE AGGIUNTA -->
<div class="modal fade" id="addToolModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header" style="background:#cfe3ff">
<h5 class="modal-title">Aggiungi Strumento</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form id="addToolForm">
<div class="modal-body">
<label class="fw-semibold">Nome</label>
<input type="text" name="name" class="form-control mb-3" required>
<label class="fw-semibold">N. Registrazione</label>
<input type="text" name="registration_number" class="form-control mb-3">
<label class="fw-semibold">N. Seriale</label>
<input type="text" name="serial_number" class="form-control mb-3">
<label class="fw-semibold">Tipo (es. Taglierina, Laser, Marcatura)</label>
<input type="text" name="tool_type" class="form-control mb-3">
<label class="fw-semibold">Produttore</label>
<input type="text" name="manufacturer" class="form-control mb-3">
<label class="fw-semibold">Descrizione (opzionale)</label>
<textarea name="description" class="form-control mb-3"></textarea>
<label class="fw-semibold">Stato</label>
<select name="is_active" class="form-control">
<option value="1">Attivo</option>
<option value="0">Non attivo</option>
</select>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">💾 Salva</button>
</div>
</form>
</div>
</div>
</div>
<!-- MODALE MODIFICA -->
<div class="modal fade" id="editToolModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header" style="background:#cfe3ff">
<h5 class="modal-title">Modifica Strumento</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form id="editToolForm">
<div class="modal-body">
<input type="hidden" id="edit_id" name="id">
<label class="fw-semibold">Nome</label>
<input type="text" id="edit_name" name="name" class="form-control mb-3" required>
<label class="fw-semibold">N. Registrazione</label>
<input type="text" id="edit_registration_number" name="registration_number" class="form-control mb-3">
<label class="fw-semibold">N. Seriale</label>
<input type="text" id="edit_serial_number" name="serial_number" class="form-control mb-3">
<label class="fw-semibold">Tipo</label>
<input type="text" id="edit_tool_type" name="tool_type" class="form-control mb-3">
<label class="fw-semibold">Produttore</label>
<input type="text" id="edit_manufacturer" name="manufacturer" class="form-control mb-3">
<label class="fw-semibold">Descrizione</label>
<textarea id="edit_description" name="description" class="form-control mb-3"></textarea>
<label class="fw-semibold">Stato</label>
<select id="edit_is_active" name="is_active" class="form-control">
<option value="1">Attivo</option>
<option value="0">Non attivo</option>
</select>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">💾 Aggiorna</button>
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#tabTools').DataTable({
order: [
[0, 'asc']
],
pageLength: 50,
language: {
url: 'https://cdn.datatables.net/plug-ins/1.13.6/i18n/it-IT.json'
}
});
// ADD TOOL
$("#addToolForm").on("submit", function(e) {
e.preventDefault();
fetch("save_tool.php", {
method: "POST",
body: new FormData(this)
})
.then(r => r.json())
.then(d => {
if (d.success) {
Swal.fire("Salvato!", "Strumento aggiunto.", "success")
.then(() => location.reload());
} else {
Swal.fire("Errore", d.message || "Errore durante il salvataggio.", "error");
}
})
.catch(() => {
Swal.fire("Errore", "Errore di comunicazione con il server.", "error");
});
});
// ✏️ OPEN EDIT MODAL
$(document).on("click", ".edit", function() {
$("#edit_id").val($(this).data("id"));
$("#edit_name").val($(this).data("name"));
$("#edit_registration_number").val($(this).data("reg"));
$("#edit_serial_number").val($(this).data("serial"));
$("#edit_tool_type").val($(this).data("type"));
$("#edit_manufacturer").val($(this).data("manufacturer"));
$("#edit_description").val($(this).data("desc"));
$("#edit_is_active").val($(this).data("active"));
$("#editToolModal").modal("show");
});
// 💾 SAVE EDIT
$("#editToolForm").on("submit", function(e) {
e.preventDefault();
fetch("edit_tool.php", {
method: "POST",
body: new FormData(this)
})
.then(r => r.json())
.then(d => {
if (d.success) {
Swal.fire("Aggiornato!", "Strumento modificato.", "success")
.then(() => location.reload());
} else {
Swal.fire("Errore", d.message || "Errore durante l'aggiornamento.", "error");
}
})
.catch(() => {
Swal.fire("Errore", "Errore di comunicazione con il server.", "error");
});
});
// 🗑️ DELETE TOOL
$(document).on("click", ".delete", function() {
const id = $(this).data("id");
Swal.fire({
title: "Eliminare lo strumento?",
text: "Questa azione non può essere annullata.",
icon: "warning",
showCancelButton: true,
confirmButtonText: "Sì, elimina",
cancelButtonText: "Annulla"
}).then(result => {
if (result.isConfirmed) {
fetch("delete_tool.php?id=" + encodeURIComponent(id))
.then(r => r.json())
.then(d => {
if (d.success) {
Swal.fire("Eliminato!", "Strumento rimosso.", "success")
.then(() => location.reload());
} else {
Swal.fire("Errore", d.message || "Errore durante l'eliminazione.", "error");
}
})
.catch(() => {
Swal.fire("Errore", "Errore di comunicazione con il server.", "error");
});
}
});
});
});
</script>
</body>
</html>

View File

@ -90,6 +90,29 @@ if ($matricePhoto) {
$mescArray = $mescRaw !== '' ? explode(' | ', $mescRaw) : [];
$mescCount = count($mescArray);
?>
<?php
// 🔧 Strumenti aggiuntivi: un badge per ogni strumento
$toolsRaw = $r['tools_list'] ?? ''; // es: "Marcatura Laser | Taglierina"
$toolsArray = [];
if ($toolsRaw !== '') {
// normalizzo i vari separatori possibili a un carattere unico
$normalized = str_replace(
[' | ', '|', ' · '], // quello che può arrivare dalla GROUP_CONCAT
'§', // separatore interno "di servizio"
$toolsRaw
);
$pieces = explode('§', $normalized);
// tolgo spazi e eventuali vuoti
$toolsArray = array_filter(array_map('trim', $pieces));
}
?>
<div>
<strong>Mescola</strong>
@ -141,7 +164,6 @@ if ($matricePhoto) {
$paramsLinea = $r['params_linea'] ?? [];
// 🔥 Carica già anche eventuali foto di slot parametri
// (necessario perché param_grid.php usa $photosSlots)
$photosSlots = [];
$stmt = $pdo->prepare("
SELECT param_position, filename
@ -154,13 +176,10 @@ if ($matricePhoto) {
$photosSlots[(int)$p['param_position']] = $p['filename'];
}
// 👉 Include la griglia parametri con le variabili già disponibili
$paramsLinea = $r['param_slots'] ?? [];
include __DIR__ . "/components/param_grid.php";
?>
<!-- Teorici nascosti (per modale finale) -->
<span data-field="kg_sp" style="display:none;"><?= $r['kg_sp'] ?></span>
<span data-field="metri" style="display:none;"><?= $r['metri'] ?></span>
@ -177,15 +196,67 @@ if ($matricePhoto) {
<?php if (in_array($r['id_status'], [2, 7])): // 2 produzione, 7 pausa
?>
<?php include __DIR__ . "/components/photo_buttons.php"; ?>
<div class="photo-tools-row"
style="margin-top:0.75rem; display:flex; flex-wrap:wrap; gap:0.6rem; align-items:center;">
<!-- 👈 GRUPPO 3 ICONE FOTO -->
<div class="photo-buttons-group"
style="display:flex; flex-wrap:wrap; gap:0.4rem; align-items:center;">
<?php include __DIR__ . "/components/photo_buttons.php"; ?>
</div>
<!-- 🎯 UN BADGE PER OGNI STRUMENTO, SUBITO DOPO LE FOTO -->
<?php foreach ($toolsArray as $toolName): ?>
<span class="badge tools-badge"
style="
background:#0d6efd;
color:#ffffff;
font-size:0.78rem;
font-weight:600;
border-radius:999px;
padding:0.30rem 0.85rem;
white-space:nowrap;
">
<?= htmlspecialchars($toolName) ?>
</span>
<?php endforeach; ?>
<!-- l'icona qualità (.qc-btn) resta dove l'hai già messa e viene spostata tutta a destra dal JS -->
</div>
<?php endif; ?>
<?php else: ?>
<button class="action-btn start-btn" data-id="<?= $r['id'] ?>">AVVIA PRODUZIONE</button>
<?php foreach ($toolsArray as $toolName): ?>
<span class="badge tools-badge"
style="
display:inline-block;
margin-left:0.5rem;
background:#0d6efd;
color:#ffffff;
font-size:0.78rem;
font-weight:600;
border-radius:999px;
padding:0.30rem 0.85rem;
white-space:nowrap;
">
<?= htmlspecialchars($toolName) ?>
</span>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>

View File

@ -0,0 +1,45 @@
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
header('Content-Type: application/json');
require_once 'include/headscript.php';
try {
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
$name = trim($_POST['name'] ?? '');
$registrationNumber = trim($_POST['registration_number'] ?? '');
$serialNumber = trim($_POST['serial_number'] ?? '');
$toolType = trim($_POST['tool_type'] ?? '');
$manufacturer = trim($_POST['manufacturer'] ?? '');
$description = trim($_POST['description'] ?? '');
$isActive = isset($_POST['is_active']) ? (int)$_POST['is_active'] : 1;
if ($name === '') {
echo json_encode(['success' => false, 'message' => 'Name is required.']);
exit;
}
$sql = "INSERT INTO production_tools
(name, registration_number, serial_number, tool_type, manufacturer, description, is_active)
VALUES
(:name, :registration_number, :serial_number, :tool_type, :manufacturer, :description, :is_active)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
'name' => $name,
'registration_number' => $registrationNumber ?: null,
'serial_number' => $serialNumber ?: null,
'tool_type' => $toolType ?: null,
'manufacturer' => $manufacturer ?: null,
'description' => $description ?: null,
'is_active' => $isActive
]);
echo json_encode(['success' => true]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}