-- Add journal_entry_id column to generic_invoices and quotations tables
-- This links invoices/quotations to their corresponding Finance module journal entries

-- Add to generic_invoices
ALTER TABLE generic_invoices 
ADD COLUMN IF NOT EXISTS journal_entry_id UUID REFERENCES gl_journal_entries(id) ON DELETE SET NULL;

CREATE INDEX IF NOT EXISTS idx_generic_invoices_journal 
ON generic_invoices(journal_entry_id);

COMMENT ON COLUMN generic_invoices.journal_entry_id IS 'Reference to GL journal entry created for this invoice';

-- Add to quotations
ALTER TABLE quotations 
ADD COLUMN IF NOT EXISTS journal_entry_id UUID REFERENCES gl_journal_entries(id) ON DELETE SET NULL;

CREATE INDEX IF NOT EXISTS idx_quotations_journal 
ON quotations(journal_entry_id);

COMMENT ON COLUMN quotations.journal_entry_id IS 'Reference to GL journal entry created for this quotation';
