Add debug endpoint for timetracking invoice field diagnostics

This commit is contained in:
Christian 2026-01-02 00:01:12 +01:00
parent 8ac3a9db2f
commit bbb9ce8487

View File

@ -118,6 +118,69 @@ async def test_vtiger_connection():
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))
@router.get("/debug/raw-stats", tags=["Debug"])
async def get_debug_raw_stats():
"""
🔍 DEBUG: Vis statistik fra databasen uden filtering.
Bruges til at diagnosticere hvorfor timelogs ikke vises.
"""
try:
# Total counts without any filtering
total_query = """
SELECT
(SELECT COUNT(*) FROM tmodule_customers) as total_customers,
(SELECT COUNT(*) FROM tmodule_cases) as total_cases,
(SELECT COUNT(*) FROM tmodule_times) as total_times,
(SELECT COUNT(*) FROM tmodule_times WHERE billable = true) as billable_times,
(SELECT COUNT(*) FROM tmodule_times WHERE status = 'pending') as pending_times,
(SELECT COUNT(*) FROM tmodule_times WHERE vtiger_data->>'cf_timelog_invoiced' = '0') as not_invoiced_times,
(SELECT COUNT(*) FROM tmodule_times WHERE vtiger_data->>'cf_timelog_invoiced' IS NULL) as null_invoiced_times,
(SELECT COUNT(*) FROM tmodule_times WHERE billable = true AND status = 'pending') as billable_pending,
(SELECT COUNT(*) FROM tmodule_times WHERE billable = true AND status = 'pending' AND vtiger_data->>'cf_timelog_invoiced' = '0') as filtered_pending
"""
totals = execute_query_single(total_query)
# Sample timelogs to see actual data
sample_query = """
SELECT
id, vtiger_id, description, worked_date, original_hours,
status, billable,
vtiger_data->>'cf_timelog_invoiced' as cf_timelog_invoiced,
vtiger_data->>'isbillable' as is_billable_field,
customer_id, case_id
FROM tmodule_times
ORDER BY worked_date DESC
LIMIT 10
"""
samples = execute_query(sample_query)
# Check what invoice statuses actually exist
invoice_status_query = """
SELECT
vtiger_data->>'cf_timelog_invoiced' as invoice_status,
COUNT(*) as count
FROM tmodule_times
GROUP BY vtiger_data->>'cf_timelog_invoiced'
ORDER BY count DESC
"""
invoice_statuses = execute_query(invoice_status_query)
return {
"totals": totals,
"sample_timelogs": samples,
"invoice_statuses": invoice_statuses,
"explanation": {
"issue": "If not_invoiced_times is 0 but total_times > 0, then the cf_timelog_invoiced field is not '0' in vTiger",
"solution": "The SQL views filter on cf_timelog_invoiced = '0' but vTiger might use different values",
"check": "Look at invoice_statuses to see what values actually exist"
}
}
except Exception as e:
logger.error(f"❌ Debug query failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
# ============================================================================ # ============================================================================
# WIZARD / APPROVAL ENDPOINTS # WIZARD / APPROVAL ENDPOINTS
# ============================================================================ # ============================================================================