getConnection(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Permesso specifico: prezzi di vendita per matrice e cliente (dati riservati) if (!userCan('sales.matrici.view')) { if (isset($_GET['action'])) { MagoSalesStats::jsonOut(['success' => false, 'message' => 'Non hai i permessi per questa pagina']); } while (ob_get_level()) ob_end_clean(); http_response_code(403); echo '
'; exit; } $canDeliveries = userCan('sales.deliveries.view'); /* ============================================================ * AJAX * ============================================================ */ function mx_date($v, $def) { $v = (string)$v; return preg_match('/^\d{4}-\d{2}-\d{2}$/', $v) ? $v : $def; } function mx_customers() { $c = trim((string)($_GET['c'] ?? '')); return $c === '' ? [] : array_slice(array_values(array_filter(array_map('trim', explode(',', $c)))), 0, 200); } $action = $_GET['action'] ?? ''; if ($action === 'filters') { MagoSalesStats::jsonOut(MagoSalesStats::filterOptions($pdo)); } if ($action === 'list' || $action === 'detail') { try { $from = mx_date($_GET['from'] ?? '', date('Y-m-d', strtotime('-365 days'))); $to = mx_date($_GET['to'] ?? '', date('Y-m-d')); if ($from < MagoMatriceStats::SINCE) $from = MagoMatriceStats::SINCE; if ($to < $from) $to = $from; if ($action === 'list') { MagoSalesStats::jsonOut(MagoMatriceStats::buildList($pdo, $from, $to, mx_customers())); } MagoSalesStats::jsonOut(MagoMatriceStats::buildDetail($pdo, (string)($_GET['k'] ?? ''), $from, $to, mx_customers())); } catch (Throwable $e) { MagoSalesStats::jsonOut(['success' => false, 'message' => $e->getMessage()]); } } ?>