-- Migration 0059 : Ajout des colonnes de comptage physique a linve_immo et perimetre sur inve_immo

-- Colonnes sur linve_immo (lignes d'inventaire)
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'linve_immo')
   AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'linve_immo' AND COLUMN_NAME = 'statut')
BEGIN
    EXEC('
        ALTER TABLE linve_immo ADD statut INT NOT NULL DEFAULT 0;
        ALTER TABLE linve_immo ADD etat INT NOT NULL DEFAULT 0;
        ALTER TABLE linve_immo ADD qte_theorique NUMERIC(12,2) NOT NULL DEFAULT 0;
        ALTER TABLE linve_immo ADD localisation_reelle VARCHAR(50) NOT NULL DEFAULT '''';
        ALTER TABLE linve_immo ADD code_barres VARCHAR(50) NOT NULL DEFAULT '''';
        ALTER TABLE linve_immo ADD operateur VARCHAR(50) NOT NULL DEFAULT '''';
        ALTER TABLE linve_immo ADD observations VARCHAR(200) NOT NULL DEFAULT '''';
    ');
    PRINT 'Colonnes de comptage ajoutees a linve_immo';
END
ELSE
BEGIN
    PRINT 'Colonnes deja presentes ou table absente - migration ignoree';
END

-- Colonnes perimetre sur inve_immo (en-tete)
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'inve_immo')
   AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'inve_immo' AND COLUMN_NAME = 'IDFamille_immo')
BEGIN
    EXEC('
        ALTER TABLE inve_immo ADD IDFamille_immo NUMERIC(12,0) NOT NULL DEFAULT 0;
        ALTER TABLE inve_immo ADD IDLocalisation NUMERIC(12,0) NOT NULL DEFAULT 0;
    ');
    PRINT 'Colonnes perimetre ajoutees a inve_immo';
END
ELSE
BEGIN
    PRINT 'Colonnes perimetre deja presentes ou table absente - migration ignoree';
END
