-- Patch v41 - Compatibilidad negocio_id en mensajes receptor.
-- La API v41 ya envía negocio_id, terminal_fiscal_id, sucursal_fiscal_id y api_key_id cuando existen esas columnas.
-- Este patch solo alinea esquemas antiguos y rellena negocio_id con negocio_id_origen cuando sea posible.

SET @table_name := 'mensajes_receptor';
SET @sql := IF(
  EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = @table_name),
  CONCAT('ALTER TABLE `', @table_name, '` ',
    'ADD COLUMN IF NOT EXISTS `negocio_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `id`, ',
    'ADD COLUMN IF NOT EXISTS `sucursal_fiscal_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `sucursal_id`, ',
    'ADD COLUMN IF NOT EXISTS `terminal_fiscal_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `sucursal_fiscal_id`, ',
    'ADD COLUMN IF NOT EXISTS `api_key_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `terminal_fiscal_id`'),
  'SELECT ''mensajes_receptor no existe, omitido'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := IF(
  EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'mensajes_receptor')
  AND EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'mensajes_receptor' AND column_name = 'negocio_id_origen'),
  'UPDATE `mensajes_receptor` SET `negocio_id` = `negocio_id_origen` WHERE (`negocio_id` IS NULL OR `negocio_id` = 0) AND `negocio_id_origen` IS NOT NULL AND `negocio_id_origen` > 0',
  'SELECT ''mensajes_receptor sin negocio_id_origen, backfill omitido'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @table_name := 'fe_mensajes_receptor';
SET @sql := IF(
  EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = @table_name),
  CONCAT('ALTER TABLE `', @table_name, '` ',
    'ADD COLUMN IF NOT EXISTS `negocio_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `id`, ',
    'ADD COLUMN IF NOT EXISTS `sucursal_fiscal_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `sucursal_id`, ',
    'ADD COLUMN IF NOT EXISTS `terminal_fiscal_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `sucursal_fiscal_id`, ',
    'ADD COLUMN IF NOT EXISTS `api_key_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `terminal_fiscal_id`'),
  'SELECT ''fe_mensajes_receptor no existe, omitido'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := IF(
  EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'fe_mensajes_receptor')
  AND EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'fe_mensajes_receptor' AND column_name = 'negocio_id_origen'),
  'UPDATE `fe_mensajes_receptor` SET `negocio_id` = `negocio_id_origen` WHERE (`negocio_id` IS NULL OR `negocio_id` = 0) AND `negocio_id_origen` IS NOT NULL AND `negocio_id_origen` > 0',
  'SELECT ''fe_mensajes_receptor sin negocio_id_origen, backfill omitido'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
