-- ══════════════════════════════════════════════════ -- Migration 008: Enhance invoice_lines for tax classification -- Adds tax_amount, discount_amount, net_total, tax_category -- ══════════════════════════════════════════════════ -- Add tax_amount column (calculated from tax_rate × line_total) ALTER TABLE invoice_lines ADD COLUMN tax_amount DECIMAL(12,3) DEFAULT 0 AFTER tax_rate; -- Add discount_amount column ALTER TABLE invoice_lines ADD COLUMN discount_amount DECIMAL(12,3) DEFAULT 0 AFTER tax_amount; -- Add net_total column (subtotal + tax - discount) ALTER TABLE invoice_lines ADD COLUMN net_total DECIMAL(12,3) DEFAULT 0 AFTER discount_amount; -- Add tax_category for classification -- standard = 16%, zero_rated = 0%, exempt = no tax, special = variable rate ALTER TABLE invoice_lines ADD COLUMN tax_category VARCHAR(20) DEFAULT 'standard' AFTER net_total; -- Backfill existing data: calculate tax_amount from line_total * tax_rate UPDATE invoice_lines SET tax_amount = ROUND(line_total * tax_rate, 3) WHERE tax_amount = 0 AND line_total > 0; -- Backfill: net_total = line_total + tax_amount - discount UPDATE invoice_lines SET net_total = ROUND(line_total + tax_amount - discount_amount, 3) WHERE net_total = 0 AND line_total > 0; -- Classify zero-rated items UPDATE invoice_lines SET tax_category = 'zero_rated' WHERE tax_rate = 0 OR tax_rate IS NULL; -- Classify standard rate items UPDATE invoice_lines SET tax_category = 'standard' WHERE tax_rate = 0.16;