getConnection(); try { // Mese richiesto (default mese corrente). Formato atteso: Y-m $month = $_GET['month'] ?? date('Y-m'); $dm = DateTime::createFromFormat('Y-m', $month); if (!$dm || $dm->format('Y-m') !== $month) { throw new Exception('Mese non valido'); } $year = $dm->format('Y'); // --- Export per cliente (status = 'l', filtro mese) --- // tot = numero commesse, parts = numero parti/campioni (+1 per commessa) $stmtC = $pdo->prepare(" SELECT d.idclient AS idclient, COUNT(DISTINCT d.iddatadb) AS tot, COUNT(ip.id) + COUNT(DISTINCT d.iddatadb) AS parts FROM datadb d LEFT JOIN identification_parts ip ON ip.iddatadb = d.iddatadb WHERE d.status = 'l' AND DATE_FORMAT(d.export_date, '%Y-%m') = :m GROUP BY d.idclient ORDER BY parts DESC "); $stmtC->execute(['m' => $month]); $byClient = $stmtC->fetchAll(PDO::FETCH_ASSOC); // --- Risolvi Nominativo cliente dalla cache locale --- $clientNames = []; $cacheFile = __DIR__ . '/cache/clienti.json'; if (is_file($cacheFile)) { $cache = json_decode(file_get_contents($cacheFile), true); foreach (($cache['value'] ?? []) as $c) { $clientNames[(int)($c['IdCliente'] ?? 0)] = trim($c['Nominativo'] ?? ''); } } // --- Export per utente (status = 'l', filtro mese) --- // tot = numero commesse, parts = numero parti/campioni (+1 per commessa) $stmtU = $pdo->prepare(" SELECT d.user_id AS user_id, TRIM(CONCAT(COALESCE(u.first_name,''),' ',COALESCE(u.last_name,''))) AS username, COUNT(DISTINCT d.iddatadb) AS tot, COUNT(ip.id) + COUNT(DISTINCT d.iddatadb) AS parts FROM datadb d LEFT JOIN auth_users u ON u.id = d.user_id LEFT JOIN identification_parts ip ON ip.iddatadb = d.iddatadb WHERE d.status = 'l' AND DATE_FORMAT(d.export_date, '%Y-%m') = :m GROUP BY d.user_id, username ORDER BY parts DESC "); $stmtU->execute(['m' => $month]); $byUser = $stmtU->fetchAll(PDO::FETCH_ASSOC); // Normalizza tipi foreach ($byClient as &$r) { $r['idclient'] = $r['idclient'] !== null ? (int)$r['idclient'] : null; $r['tot'] = (int)$r['tot']; $r['parts'] = (int)$r['parts']; $nm = $clientNames[$r['idclient']] ?? ''; $r['clientname'] = $nm !== '' ? $nm : null; } unset($r); foreach ($byUser as &$r) { $r['user_id'] = $r['user_id'] !== null ? (int)$r['user_id'] : null; $r['tot'] = (int)$r['tot']; $r['parts'] = (int)$r['parts']; if (isset($r['username'])) $r['username'] = $r['username'] !== null && $r['username'] !== '' ? $r['username'] : null; } unset($r); // --- Totale COMMESSE del mese --- $totExport = array_sum(array_column($byClient, 'tot')); // --- Totale PARTI/CAMPIONI del mese (status = 'l', +1 per commessa) --- $stmtP = $pdo->prepare(" SELECT COUNT(ip.id) + COUNT(DISTINCT d.iddatadb) FROM datadb d LEFT JOIN identification_parts ip ON ip.iddatadb = d.iddatadb WHERE d.status = 'l' AND DATE_FORMAT(d.export_date, '%Y-%m') = :m "); $stmtP->execute(['m' => $month]); $totParts = (int)$stmtP->fetchColumn(); // --- Totale COMMESSE dell'ANNO del mese selezionato --- $stmtY = $pdo->prepare(" SELECT COUNT(*) FROM datadb d WHERE d.status = 'l' AND YEAR(d.export_date) = :y "); $stmtY->execute(['y' => $year]); $totExportYear = (int)$stmtY->fetchColumn(); // --- Totale PARTI dell'ANNO del mese selezionato (+1 per commessa) --- $stmtYP = $pdo->prepare(" SELECT COUNT(ip.id) + COUNT(DISTINCT d.iddatadb) FROM datadb d LEFT JOIN identification_parts ip ON ip.iddatadb = d.iddatadb WHERE d.status = 'l' AND YEAR(d.export_date) = :y "); $stmtYP->execute(['y' => $year]); $totPartsYear = (int)$stmtYP->fetchColumn(); // --- Campioni LIMS: somma dei total_samples di tutti i giorni del mese --- $stmtL = $pdo->prepare(" SELECT COALESCE(SUM(total_samples), 0) FROM lims_daily_samples WHERE DATE_FORMAT(sample_date, '%Y-%m') = :m "); $stmtL->execute(['m' => $month]); $limsSamples = (int)$stmtL->fetchColumn(); echo json_encode([ 'success' => true, 'month' => $month, 'year' => (int)$year, 'totals' => ['export' => (int)$totExport, 'parts' => $totParts], 'totalsYear' => ['export' => $totExportYear, 'parts' => $totPartsYear], 'byClient' => $byClient, 'byUser' => $byUser, 'limsSamples' => $limsSamples, ]); } catch (Exception $e) { error_log('Stats export LIMS monthly error: ' . $e->getMessage()); echo json_encode(['success' => false, 'message' => $e->getMessage()]); }