-- Add VAT columns to pos_items
ALTER TABLE pos_items ADD COLUMN vat_rate DECIMAL(5,2) DEFAULT 0.00 AFTER unit_price;
ALTER TABLE pos_items ADD COLUMN vat_amount DECIMAL(15,3) DEFAULT 0.000 AFTER vat_rate;

-- Update existing pos_items if possible (optional, but good for consistency)
-- This might not be accurate if items changed their vat rate after transaction, 
-- but it's better than nothing.
UPDATE pos_items pi 
JOIN stock_items si ON pi.product_id = si.id 
SET pi.vat_rate = si.vat_rate;

UPDATE pos_items SET vat_amount = subtotal * (vat_rate / (100 + vat_rate));

-- Update pos_transactions to ensure tax_amount is populated for old transactions
UPDATE pos_transactions pt 
SET tax_amount = (SELECT SUM(vat_amount) FROM pos_items WHERE transaction_id = pt.id);
