Files
api-btekno/bin/debug_daily_summary.php

101 lines
4.2 KiB
PHP
Raw Normal View History

<?php
require __DIR__ . '/../vendor/autoload.php';
use App\Config\AppConfig;
use App\Support\Database;
AppConfig::loadEnv(__DIR__ . '/..');
$db = Database::getConnection(
AppConfig::get('DB_HOST'),
AppConfig::get('DB_NAME'),
AppConfig::get('DB_USER'),
AppConfig::get('DB_PASS')
);
$date = '2025-12-15'; // Date with mismatch
echo "=== Debugging daily_summary for $date ===\n\n";
// Total events
$stmt = $db->prepare('SELECT COUNT(*) as count FROM entry_events WHERE DATE(event_time) = ?');
$stmt->execute([$date]);
$totalEvents = $stmt->fetch()['count'];
echo "Total entry_events: $totalEvents\n\n";
// Events with active locations
$stmt = $db->prepare('SELECT COUNT(*) as count FROM entry_events e
INNER JOIN locations l ON e.location_code = l.code AND l.is_active = 1
WHERE DATE(e.event_time) = ?');
$stmt->execute([$date]);
$withActiveLocation = $stmt->fetch()['count'];
echo "Events with active location: $withActiveLocation\n";
// Events with active gates
$stmt = $db->prepare('SELECT COUNT(*) as count FROM entry_events e
INNER JOIN locations l ON e.location_code = l.code AND l.is_active = 1
INNER JOIN gates g ON e.location_code = g.location_code AND e.gate_code = g.gate_code AND g.is_active = 1
WHERE DATE(e.event_time) = ?');
$stmt->execute([$date]);
$withActiveGate = $stmt->fetch()['count'];
echo "Events with active location + gate: $withActiveGate\n\n";
// Events without active location
$stmt = $db->prepare('SELECT COUNT(*) as count FROM entry_events e
LEFT JOIN locations l ON e.location_code = l.code
WHERE DATE(e.event_time) = ? AND (l.code IS NULL OR l.is_active = 0)');
$stmt->execute([$date]);
$withoutActiveLocation = $stmt->fetch()['count'];
echo "Events WITHOUT active location: $withoutActiveLocation\n";
// Events without active gate
$stmt = $db->prepare('SELECT COUNT(*) as count FROM entry_events e
INNER JOIN locations l ON e.location_code = l.code AND l.is_active = 1
LEFT JOIN gates g ON e.location_code = g.location_code AND e.gate_code = g.gate_code
WHERE DATE(e.event_time) = ? AND (g.gate_code IS NULL OR g.is_active = 0)');
$stmt->execute([$date]);
$withoutActiveGate = $stmt->fetch()['count'];
echo "Events with active location but WITHOUT active gate: $withoutActiveGate\n\n";
// Check daily_summary
$stmt = $db->prepare('SELECT SUM(total_count) as total FROM daily_summary WHERE summary_date = ?');
$stmt->execute([$date]);
$summaryTotal = $stmt->fetch()['total'] ?? 0;
echo "daily_summary total_count: $summaryTotal\n\n";
// Check what's in daily_summary
$stmt = $db->prepare('SELECT location_code, gate_code, category, total_count, total_amount FROM daily_summary WHERE summary_date = ? ORDER BY location_code, gate_code, category');
$stmt->execute([$date]);
$summaryRows = $stmt->fetchAll();
echo "daily_summary records:\n";
foreach ($summaryRows as $row) {
echo " - " . $row['location_code'] . " | " . $row['gate_code'] . " | " . $row['category'] . " | count: " . $row['total_count'] . " | amount: " . $row['total_amount'] . "\n";
}
// Check events that should be aggregated
$stmt = $db->prepare('SELECT
e.location_code,
e.gate_code,
e.category,
COUNT(*) as count,
COALESCE(t.price, 0) as price
FROM entry_events e
INNER JOIN locations l ON e.location_code = l.code AND l.is_active = 1
INNER JOIN gates g ON e.location_code = g.location_code AND e.gate_code = g.gate_code AND g.is_active = 1
LEFT JOIN tariffs t ON e.location_code = t.location_code AND e.gate_code = t.gate_code AND e.category = t.category
WHERE DATE(e.event_time) = ?
GROUP BY e.location_code, e.gate_code, e.category, COALESCE(t.price, 0)
ORDER BY e.location_code, e.gate_code, e.category');
$stmt->execute([$date]);
$shouldBeAggregated = $stmt->fetchAll();
echo "\nEvents that SHOULD be aggregated:\n";
$totalShouldBe = 0;
foreach ($shouldBeAggregated as $row) {
$totalShouldBe += $row['count'];
echo " - " . $row['location_code'] . " | " . $row['gate_code'] . " | " . $row['category'] . " | count: " . $row['count'] . " | price: " . $row['price'] . "\n";
}
echo "\nTotal that should be aggregated: $totalShouldBe\n";
echo "Total in daily_summary: $summaryTotal\n";
echo "Difference: " . ($totalShouldBe - $summaryTotal) . "\n";