Skip to content

Debugging Guide

Step-by-step procedures for debugging Expensis automation issues.


Debugging Strategy

graph TD
    A[Issue Reported] --> B{Known Issue?}
    B -->|Yes| C[Check Common Issues]
    B -->|No| D[Identify Component]
    D --> E{Component Type?}
    E -->|Sync| F[Debug Sync Issues]
    E -->|Totals| G[Debug Calculation Issues]
    E -->|Chain| H[Debug Chain Issues]
    E -->|Other| I[General Debugging]

    C --> J[Apply Solution]
    F --> J
    G --> J
    H --> J
    I --> J

    style A fill:#e3f2fd
    style J fill:#e8f5e9

Step 1: Identify the Problem

Questions to Ask

  1. What's not working?
  2. Specific sync failing?
  3. All syncs failing?
  4. Totals not calculating?
  5. Chain not executing?

  6. When did it start?

  7. After deployment?
  8. After configuration change?
  9. Suddenly?
  10. Gradually?

  11. Is it consistent?

  12. Always fails?
  13. Intermittent?
  14. Specific customers only?
  15. Specific providers only?

Debugging Sync Issues

Sync Not Running at All

Check 1: Is cron running?

sudo systemctl status cron
crontab -l | grep sync:manager

Check 2: Recent sync tasks

SELECT * FROM sync_task
WHERE created_at > NOW() - INTERVAL 1 HOUR
ORDER BY created_at DESC;

Check 3: Manual execution

cd /var/www/expensis
php bin/console sync:manager -vvv

Specific Provider Sync Failing

Check 1: Sync task status

SELECT
    st.id,
    sp.sync_type,
    st.status,
    st.error_message,
    st.created_at,
    st.updated_at
FROM sync_task st
JOIN sync_profile sp ON st.sync_profile_id = sp.id
WHERE st.customer_id = 123
ORDER BY st.created_at DESC
LIMIT 10;

Check 2: Sync profile active

SELECT
    id,
    sync_type,
    active,
    next_sync_date
FROM sync_profile
WHERE customer_id = 123;

Check 3: Run manually with verbose output

# For KPN SP16
php bin/console kpn:sp16:sync --customer=123 -vvv

# For Vodafone
php bin/console vodafone:sync --customer=123 -vvv

# Generic sync
php bin/console sync:manager --customer=123 --force -vvv

Check 4: Check handler logs

tail -100 /var/www/expensis/var/log/prod.log | grep -i "Sp16\|Vodafone\|TMobile"


Debugging Totals Calculation

Totals Are Zero

Check 1: Do CDRs exist?

SELECT COUNT(*) as cdr_count
FROM call_detail_record
WHERE customer_id = 123
AND call_date >= '2025-01-01'
AND call_date < '2025-02-01';

Check 2: Are CDRs linked to devices?

SELECT
    COUNT(*) as total_cdrs,
    SUM(CASE WHEN device_id IS NULL THEN 1 ELSE 0 END) as unlinked_cdrs
FROM call_detail_record
WHERE customer_id = 123
AND call_date >= '2025-01-01';

Check 3: Do devices have subscriptions?

SELECT
    d.id,
    d.code,
    d.subscription_id,
    s.rate_plan_id
FROM device d
LEFT JOIN subscription s ON d.subscription_id = s.id
WHERE d.customer_id = 123
AND d.subscription_id IS NULL;

Check 4: Run calculation manually

php bin/console totals:new --customer=123 --cycle=2025-01-01 -vvv

Totals Wrong (Not Matching Provider)

Check 1: Compare CDR count

SELECT
    call_type,
    COUNT(*) as count,
    SUM(duration) as total_duration,
    SUM(cost) as total_cost
FROM call_detail_record
WHERE customer_id = 123
AND call_date >= '2025-01-01'
AND call_date < '2025-02-01'
GROUP BY call_type;

Check 2: Verify rate plans

SELECT
    s.id,
    s.name,
    s.rate_plan_id,
    rp.name as rate_plan_name
FROM subscription s
LEFT JOIN rate_plan rp ON s.rate_plan_id = rp.id
WHERE s.customer_id = 123;

Check 3: Check totals breakdown

SELECT * FROM total_usage_new_table
WHERE customer_id = 123
AND cycle_start_date = '2025-01-01';


Debugging Command Chains

Chain Not Executing

Check 1: Is MessageConsumedHandler present?

ls -la src/MessageBus/MessageConsumedHandler.php

Check 2: Is it registered?

php bin/console debug:event-dispatcher | grep -i "worker.message.handled"

Check 3: Test simple chain

# Run totals calculation (has chain to backup)
php bin/console totals:new --customer=123 --cycle=2025-01-01

# Check if backup created

SELECT * FROM total_usage_new_backup_table
WHERE customer_id = 123
AND cycle_start_date = '2025-01-01';

Check 4: Check event logs

tail -100 /var/www/expensis/var/log/prod.log | grep -i "messageconsumed\|chain"


General Debugging Tools

View Recent Errors

tail -200 /var/www/expensis/var/log/prod.log | grep ERROR

Check Messenger Status

php bin/console debug:messenger

View Event Subscribers

php bin/console debug:event-dispatcher

Check Services

php bin/console debug:container | grep -i sync

Test Database Connection

php bin/console doctrine:query:sql "SELECT 1"

Advanced Debugging

Enable Debug Mode

# In .env.local
APP_ENV=dev
APP_DEBUG=1

Warning: Only use in development/staging!

Xdebug Breakpoints

// In handler
public function __invoke(ExampleCommand $command)
{
    xdebug_break();  // Breakpoint here

    // Your code
}

Profiling Slow Operations

$start = microtime(true);

// Your code here

$duration = microtime(true) - $start;
$this->logger->info("Operation took {$duration} seconds");

Memory Debugging

$this->logger->info("Memory usage: " . memory_get_usage(true) / 1024 / 1024 . " MB");