bmc_hub/migrations/024_fix_timetracking_views.sql

40 lines
1.9 KiB
SQL

-- Fix timetracking views to show ALL timelogs regardless of cf_timelog_invoiced status
-- Issue: cf_timelog_invoiced field might not exist or have different values in vTiger
-- Solution: Remove the invoice status filter from views
-- Oversigt over godkendelsesstatus pr. kunde (FIXED - no invoice filter)
CREATE OR REPLACE VIEW tmodule_approval_stats AS
SELECT
c.id AS customer_id,
c.name AS customer_name,
c.vtiger_id AS customer_vtiger_id,
c.uses_time_card AS uses_time_card,
COUNT(t.id) FILTER (WHERE t.billable = true) AS total_entries,
COUNT(t.id) FILTER (WHERE t.billable = true AND t.status = 'pending') AS pending_count,
COUNT(t.id) FILTER (WHERE t.billable = true AND t.status = 'approved') AS approved_count,
COUNT(t.id) FILTER (WHERE t.billable = true AND t.status = 'rejected') AS rejected_count,
COUNT(t.id) FILTER (WHERE t.billable = true AND t.status = 'billed') AS billed_count,
SUM(t.original_hours) FILTER (WHERE t.billable = true) AS total_original_hours,
SUM(t.approved_hours) FILTER (WHERE t.billable = true AND t.status = 'approved') AS total_approved_hours,
MAX(t.worked_date) FILTER (WHERE t.billable = true) AS latest_work_date,
MAX(t.last_synced_at) FILTER (WHERE t.billable = true) AS last_sync
FROM tmodule_customers c
LEFT JOIN tmodule_times t ON c.id = t.customer_id
GROUP BY c.id, c.name, c.vtiger_id, c.uses_time_card;
-- Næste tid der skal godkendes (FIXED - no invoice filter)
CREATE OR REPLACE VIEW tmodule_next_pending AS
SELECT
t.*,
COALESCE(c.vtiger_data->>'case_no', c.title)::VARCHAR(500) AS case_title,
c.status AS case_status,
c.vtiger_id AS case_vtiger_id,
cust.name AS customer_name,
cust.hourly_rate AS customer_rate
FROM tmodule_times t
JOIN tmodule_cases c ON t.case_id = c.id
JOIN tmodule_customers cust ON t.customer_id = cust.id
WHERE t.status = 'pending'
AND t.billable = true -- Only billable timelogs
ORDER BY cust.name, c.title, t.worked_date;