getConnection(); try { $date = $_POST['date'] ?? null; $total = $_POST['total_samples'] ?? null; $d = $date ? DateTime::createFromFormat('Y-m-d', $date) : null; if (!$d || $d->format('Y-m-d') !== $date) { throw new Exception('Data non valida'); } if (!is_numeric($total) || (int)$total < 0) { throw new Exception('Valore campioni non valido'); } $total = (int)$total; // Upsert: un solo record per giornata (unique su sample_date) $stmt = $pdo->prepare(" INSERT INTO lims_daily_samples (sample_date, total_samples) VALUES (:d, :t) ON DUPLICATE KEY UPDATE total_samples = VALUES(total_samples) "); $stmt->execute(['d' => $date, 't' => $total]); echo json_encode(['success' => true, 'date' => $date, 'total_samples' => $total]); } catch (Exception $e) { error_log('Save LIMS samples error: ' . $e->getMessage()); echo json_encode(['success' => false, 'message' => $e->getMessage()]); }