format($format) === $date; } // Params $reportType = $_GET['type'] ?? 'countries_origin'; // countries_origin, countries_dest, cities_origin, cities_dest, shippers $startDate = $_GET['start_date'] ?? date('Y-m-01'); // Default to first day of current month $endDate = $_GET['end_date'] ?? date('Y-m-t'); // Default to last day of current month if (!validate_date($startDate)) $startDate = date('Y-m-01'); if (!validate_date($endDate)) $endDate = date('Y-m-t'); // Build Query $where = "s.payment_status = 'paid' AND DATE(s.created_at) BETWEEN ? AND ?"; $params = [$startDate, $endDate]; $groupBy = ''; $selectName = ''; $join = ''; $orderBy = 'total_amount DESC'; switch ($reportType) { case 'countries_origin': $selectName = "COALESCE(co.name_en, 'Unknown') as name"; $join = "LEFT JOIN cities c ON s.origin_city = c.name_en LEFT JOIN countries co ON c.country_id = co.id"; $groupBy = "co.name_en"; $pageTitle = "Shipments by Origin Country"; break; case 'countries_dest': $selectName = "COALESCE(co.name_en, 'Unknown') as name"; $join = "LEFT JOIN cities c ON s.destination_city = c.name_en LEFT JOIN countries co ON c.country_id = co.id"; $groupBy = "co.name_en"; $pageTitle = "Shipments by Destination Country"; break; case 'cities_origin': $selectName = "s.origin_city as name"; $groupBy = "s.origin_city"; $pageTitle = "Shipments by Origin City"; break; case 'cities_dest': $selectName = "s.destination_city as name"; $groupBy = "s.destination_city"; $pageTitle = "Shipments by Destination City"; break; case 'shippers': $selectName = "s.shipper_name as name"; // simplified, could join users table $groupBy = "s.shipper_name"; $pageTitle = "Shipments by Shipper"; break; default: $reportType = 'countries_origin'; $selectName = "COALESCE(co.name_en, 'Unknown') as name"; $join = "LEFT JOIN cities c ON s.origin_city = c.name_en LEFT JOIN countries co ON c.country_id = co.id"; $groupBy = "co.name_en"; $pageTitle = "Shipments by Origin Country"; break; } $sql = " SELECT $selectName, COUNT(s.id) as shipment_count, SUM(s.total_price) as total_amount, SUM(s.platform_fee) as total_profit FROM shipments s $join WHERE $where GROUP BY $groupBy ORDER BY $orderBy "; try { $stmt = db()->prepare($sql); $stmt->execute($params); $results = $stmt->fetchAll(); } catch (PDOException $e) { $results = []; $error = "Database error: " . $e->getMessage(); } // Calculate totals for footer $totalShipments = 0; $grandTotalAmount = 0.0; $grandTotalProfit = 0.0; foreach ($results as $row) { $totalShipments += $row['shipment_count']; $grandTotalAmount += $row['total_amount']; $grandTotalProfit += $row['total_profit']; } render_header('Reports Summary', 'reports_summary', true); ?>

Summary Reports

Analyze shipment performance, revenue, and profits.

to

No paid shipments found for this period.

Name Shipments Total Amount Profit
$ $
TOTAL $ $