-- Phoenix P2.5.8 LTS — traçabilité facture directe depuis devis
SET @has_column := (
    SELECT COUNT(*) FROM information_schema.columns
    WHERE table_schema = DATABASE() AND table_name = 'factures' AND column_name = 'devis_id'
);
SET @sql := IF(@has_column = 0,
    'ALTER TABLE factures ADD COLUMN devis_id INT NULL AFTER client_id, ADD INDEX idx_factures_devis (devis_id)',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
