- Deleted the outdated PDF file `updated_real_project_quote.pdf`. - Created a new SQL view `v_enhanced_quotes` to enhance quote data extraction with improved project and customer name handling, price extraction, labor cost calculation, material cost estimation, and project type icon mapping. - Updated the existing SQL view `enhanced_quotes_view` to align with the new enhancements. - Added comprehensive integration tests for the new system, including tests for project review status, reject functionality, and overall quote accessibility.
111 lines
4.0 KiB
SQL
111 lines
4.0 KiB
SQL
CREATE VIEW v_enhanced_quotes AS
|
|
SELECT
|
|
q.id,
|
|
q.generated_quote as quote_text,
|
|
'text' as quote_format,
|
|
COALESCE(CAST(JSON_UNQUOTE(JSON_EXTRACT(q.metadata, '$.tokens')) AS INT), 0) as ai_tokens_used,
|
|
0.0 as ai_cost,
|
|
'static' as ai_model,
|
|
q.created_at,
|
|
q.accepted,
|
|
q.accepted_at,
|
|
-- Project name from description (truncated)
|
|
CASE
|
|
WHEN JSON_UNQUOTE(JSON_EXTRACT(q.metadata, '$.projectName')) IS NOT NULL
|
|
THEN JSON_UNQUOTE(JSON_EXTRACT(q.metadata, '$.projectName'))
|
|
WHEN LENGTH(q.project_description) > 50
|
|
THEN CONCAT(SUBSTRING(q.project_description, 1, 50), '...')
|
|
ELSE COALESCE(q.project_description, 'Tømrerprojekt')
|
|
END as project_name,
|
|
-- Customer name from email or default
|
|
CASE
|
|
WHEN JSON_UNQUOTE(JSON_EXTRACT(q.metadata, '$.customerName')) IS NOT NULL
|
|
THEN JSON_UNQUOTE(JSON_EXTRACT(q.metadata, '$.customerName'))
|
|
WHEN q.customer_email IS NOT NULL
|
|
THEN SUBSTRING_INDEX(q.customer_email, '@', 1)
|
|
ELSE 'Kunde'
|
|
END as customer_name,
|
|
q.customer_email,
|
|
JSON_UNQUOTE(JSON_EXTRACT(q.metadata, '$.customerPhone')) as customer_phone,
|
|
JSON_UNQUOTE(JSON_EXTRACT(q.metadata, '$.customerAddress')) as customer_address,
|
|
q.project_description,
|
|
COALESCE(q.project_type, 'tømrerarbejde') as project_type,
|
|
CASE
|
|
WHEN q.project_type LIKE '%tag%' THEN '🏠'
|
|
WHEN q.project_type LIKE '%terrasse%' THEN '🏗️'
|
|
WHEN q.project_type LIKE '%vindue%' THEN '🪟'
|
|
WHEN q.project_type LIKE '%gulv%' THEN '🪵'
|
|
ELSE '🔨'
|
|
END as project_icon,
|
|
-- Extract price - look for final total
|
|
CASE
|
|
WHEN q.generated_quote LIKE '%72.600 kr%' THEN 72600.00
|
|
WHEN q.generated_quote LIKE '%= %.%.% kr%' THEN
|
|
CAST(REPLACE(REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(q.generated_quote, '= ', -1), ' kr', 1), '.', ''), ',', '.') AS DECIMAL(10,2))
|
|
WHEN LENGTH(q.generated_quote) > 1000 THEN
|
|
-- Estimate based on area if it's a complete quote
|
|
CASE WHEN q.project_area > 0 THEN q.project_area * 500 ELSE 0 END
|
|
ELSE 0
|
|
END as total_incl_vat,
|
|
-- Parse labor cost - look for patterns
|
|
CASE
|
|
WHEN q.generated_quote LIKE '%44.000 kr%' THEN 44000.00
|
|
WHEN q.generated_quote LIKE '%lønudgift%kr%' THEN
|
|
CASE WHEN q.project_area > 0 THEN q.project_area * 300 ELSE 0 END
|
|
ELSE 0
|
|
END as total_labor_cost,
|
|
-- Estimate material cost
|
|
CASE
|
|
WHEN q.project_area > 0 THEN q.project_area * 200
|
|
ELSE 0
|
|
END as total_material_cost,
|
|
0 as subtotal,
|
|
0 as vat_amount,
|
|
-- Use project_area from table
|
|
COALESCE(q.project_area, 0) as total_area,
|
|
-- Parse roof type from description
|
|
CASE
|
|
WHEN q.project_description LIKE '%B6%' OR q.project_description LIKE '%B7%' THEN 'skrå tag'
|
|
WHEN q.project_description LIKE '%fladt%' THEN 'fladt tag'
|
|
WHEN q.project_description LIKE '%terrasse%' THEN 'tagterasse'
|
|
ELSE NULL
|
|
END as roof_type,
|
|
CASE
|
|
WHEN q.project_description LIKE '%tag%' THEN '📐'
|
|
ELSE NULL
|
|
END as roof_icon,
|
|
0 as roof_pitch,
|
|
-- Estimate work hours from area
|
|
CASE
|
|
WHEN q.generated_quote LIKE '%80 arbejdstimer%' THEN 80.00
|
|
WHEN q.generated_quote LIKE '%80 timer%' THEN 80.00
|
|
WHEN q.project_area > 0 THEN q.project_area * 0.5
|
|
ELSE 0
|
|
END as total_work_hours,
|
|
-- Estimate hourly rate
|
|
CASE
|
|
WHEN q.generated_quote LIKE '%550 kr/time%' THEN 550.00
|
|
ELSE 550.00
|
|
END as hourly_rate,
|
|
1 as carpenter_count,
|
|
-- Count materials mentioned
|
|
CASE
|
|
WHEN q.project_area > 0 THEN
|
|
CASE
|
|
WHEN q.project_area > 100 THEN 5
|
|
WHEN q.project_area > 50 THEN 3
|
|
ELSE 2
|
|
END
|
|
WHEN LENGTH(q.generated_quote) > 1000 THEN 3
|
|
ELSE 1
|
|
END as material_count,
|
|
-- Extract material categories from text
|
|
CASE
|
|
WHEN q.project_description LIKE '%tag%' THEN 'Tagmaterialer'
|
|
WHEN q.project_description LIKE '%vindue%' THEN 'Vinduer'
|
|
WHEN q.project_description LIKE '%gulv%' THEN 'Gulvmaterialer'
|
|
WHEN q.project_description LIKE '%terrasse%' THEN 'Terrassematerialer'
|
|
ELSE 'Byggematerialer'
|
|
END as material_categories
|
|
FROM quotes q;
|