-- Placa vehicular opcional para comprobantes de facturacion y flag por empresa.

SET @empresa_placa_exists := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'empresas'
      AND column_name = 'requiere_placa_vehicular'
);

SET @sql_empresa_placa := IF(
    @empresa_placa_exists = 0,
    'ALTER TABLE empresas ADD COLUMN requiere_placa_vehicular TINYINT(1) NOT NULL DEFAULT 0',
    'SELECT 1'
);
PREPARE stmt_empresa_placa FROM @sql_empresa_placa;
EXECUTE stmt_empresa_placa;
DEALLOCATE PREPARE stmt_empresa_placa;

SET @comprobante_placa_exists := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'facturacion_comprobantes'
      AND column_name = 'placa_vehicular'
);

SET @sql_comprobante_placa := IF(
    @comprobante_placa_exists = 0,
    'ALTER TABLE facturacion_comprobantes ADD COLUMN placa_vehicular VARCHAR(15) NULL AFTER moneda',
    'SELECT 1'
);
PREPARE stmt_comprobante_placa FROM @sql_comprobante_placa;
EXECUTE stmt_comprobante_placa;
DEALLOCATE PREPARE stmt_comprobante_placa;
