157 lines
5.8 KiB
PHP
157 lines
5.8 KiB
PHP
<?php
|
|
include('include/headscript.php');
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$dbHandler = DBHandlerSelect::getInstance();
|
|
$pdo = $dbHandler->getConnection();
|
|
|
|
try {
|
|
// Data richiesta (default oggi). Formato atteso: Y-m-d
|
|
$date = $_GET['date'] ?? date('Y-m-d');
|
|
$d = DateTime::createFromFormat('Y-m-d', $date);
|
|
if (!$d || $d->format('Y-m-d') !== $date) {
|
|
throw new Exception('Data non valida');
|
|
}
|
|
|
|
// --- Export per cliente (status = 'l', giornata su export_date) ---
|
|
// tot = numero commesse, parts = numero parti/campioni
|
|
$stmtC = $pdo->prepare("
|
|
SELECT d.idclient AS idclient,
|
|
COUNT(DISTINCT d.iddatadb) AS tot,
|
|
COUNT(ip.id) AS parts
|
|
FROM datadb d
|
|
LEFT JOIN identification_parts ip ON ip.iddatadb = d.iddatadb
|
|
WHERE d.status = 'l'
|
|
AND DATE(d.export_date) = :d
|
|
GROUP BY d.idclient
|
|
ORDER BY parts DESC
|
|
");
|
|
$stmtC->execute(['d' => $date]);
|
|
$byClient = $stmtC->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// --- Risolvi Nominativo cliente dalla cache locale (search_clienti.php) ---
|
|
$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', giornata su export_date) ---
|
|
// tot = numero commesse, parts = numero parti/campioni
|
|
$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) 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(d.export_date) = :d
|
|
GROUP BY d.user_id, username
|
|
ORDER BY parts DESC
|
|
");
|
|
$stmtU->execute(['d' => $date]);
|
|
$byUser = $stmtU->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Normalizza tipi (COUNT torna stringa in PDO)
|
|
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 export della giornata ---
|
|
$totExport = array_sum(array_column($byClient, 'tot'));
|
|
|
|
// --- Totale PARTI/CAMPIONI della giornata (righe identification_parts legate
|
|
// ai datadb esportati status='l' in questa giornata) ---
|
|
$stmtP = $pdo->prepare("
|
|
SELECT COUNT(*)
|
|
FROM identification_parts ip
|
|
INNER JOIN datadb d ON d.iddatadb = ip.iddatadb
|
|
WHERE d.status = 'l'
|
|
AND DATE(d.export_date) = :d
|
|
");
|
|
$stmtP->execute(['d' => $date]);
|
|
$totParts = (int)$stmtP->fetchColumn();
|
|
|
|
// --- Totale COMMESSE del MESE (status = 'l') ---
|
|
$stmtM = $pdo->prepare("
|
|
SELECT COUNT(*)
|
|
FROM datadb d
|
|
WHERE d.status = 'l'
|
|
AND DATE_FORMAT(d.export_date, '%Y-%m') = :m
|
|
");
|
|
$stmtM->execute(['m' => $d->format('Y-m')]);
|
|
$totExportMonth = (int)$stmtM->fetchColumn();
|
|
|
|
// --- Totale PARTI del MESE (status = 'l') ---
|
|
$stmtMP = $pdo->prepare("
|
|
SELECT COUNT(*)
|
|
FROM identification_parts ip
|
|
INNER JOIN datadb d ON d.iddatadb = ip.iddatadb
|
|
WHERE d.status = 'l'
|
|
AND DATE_FORMAT(d.export_date, '%Y-%m') = :m
|
|
");
|
|
$stmtMP->execute(['m' => $d->format('Y-m')]);
|
|
$totPartsMonth = (int)$stmtMP->fetchColumn();
|
|
|
|
// --- Totale COMMESSE dell'ANNO (status = 'l') ---
|
|
$stmtY = $pdo->prepare("
|
|
SELECT COUNT(*)
|
|
FROM datadb d
|
|
WHERE d.status = 'l'
|
|
AND YEAR(d.export_date) = :y
|
|
");
|
|
$stmtY->execute(['y' => $d->format('Y')]);
|
|
$totExportYear = (int)$stmtY->fetchColumn();
|
|
|
|
// --- Totale PARTI dell'ANNO (status = 'l') ---
|
|
$stmtYP = $pdo->prepare("
|
|
SELECT COUNT(*)
|
|
FROM identification_parts ip
|
|
INNER JOIN datadb d ON d.iddatadb = ip.iddatadb
|
|
WHERE d.status = 'l'
|
|
AND YEAR(d.export_date) = :y
|
|
");
|
|
$stmtYP->execute(['y' => $d->format('Y')]);
|
|
$totPartsYear = (int)$stmtYP->fetchColumn();
|
|
|
|
// --- Campioni totali inseriti nel LIMS per la giornata (tabella lims_daily_samples) ---
|
|
$stmtL = $pdo->prepare("SELECT total_samples FROM lims_daily_samples WHERE sample_date = :d LIMIT 1");
|
|
$stmtL->execute(['d' => $date]);
|
|
$limsSamples = $stmtL->fetchColumn();
|
|
$limsSamples = ($limsSamples !== false) ? (int)$limsSamples : null;
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'date' => $date,
|
|
'month' => $d->format('Y-m'),
|
|
'year' => (int)$d->format('Y'),
|
|
'totals' => ['export' => (int)$totExport, 'parts' => $totParts],
|
|
'totalsMonth' => ['export' => $totExportMonth, 'parts' => $totPartsMonth],
|
|
'totalsYear' => ['export' => $totExportYear, 'parts' => $totPartsYear],
|
|
'byClient' => $byClient,
|
|
'byUser' => $byUser,
|
|
'limsSamples' => $limsSamples,
|
|
]);
|
|
} catch (Exception $e) {
|
|
error_log('Stats export LIMS error: ' . $e->getMessage());
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|