-- Codigos estandar para catalogo de productos/servicios

SET @codigo_sunat_exists := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'catalogo_items'
      AND column_name = 'codigo_sunat'
);

SET @sql_codigo_sunat := IF(
    @codigo_sunat_exists = 0,
    'ALTER TABLE catalogo_items ADD COLUMN codigo_sunat VARCHAR(20) NULL AFTER codigo',
    'SELECT 1'
);
PREPARE stmt_codigo_sunat FROM @sql_codigo_sunat;
EXECUTE stmt_codigo_sunat;
DEALLOCATE PREPARE stmt_codigo_sunat;

SET @codigo_gs1_exists := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'catalogo_items'
      AND column_name = 'codigo_gs1'
);

SET @sql_codigo_gs1 := IF(
    @codigo_gs1_exists = 0,
    'ALTER TABLE catalogo_items ADD COLUMN codigo_gs1 VARCHAR(20) NULL AFTER codigo_sunat',
    'SELECT 1'
);
PREPARE stmt_codigo_gs1 FROM @sql_codigo_gs1;
EXECUTE stmt_codigo_gs1;
DEALLOCATE PREPARE stmt_codigo_gs1;
