-- phpMyAdmin SQL Dump
-- version 5.2.2
-- https://www.phpmyadmin.net/
--
-- Servidor: localhost:3306
-- Tiempo de generación: 01-07-2026 a las 18:08:11
-- Versión del servidor: 10.6.27-MariaDB
-- Versión de PHP: 8.4.22

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Base de datos: `anthonpos_a3s7axc8HS0`
--

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `afiliados`
--

CREATE TABLE `afiliados` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED NOT NULL,
  `codigo` varchar(80) NOT NULL,
  `estado` enum('pendiente','activo','rechazado','suspendido') NOT NULL DEFAULT 'pendiente',
  `porcentaje_comision` decimal(5,2) NOT NULL DEFAULT 0.00,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `aprobado_el` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `afiliado_comisiones`
--

CREATE TABLE `afiliado_comisiones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `afiliado_id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `monto_base` decimal(12,2) NOT NULL DEFAULT 0.00,
  `porcentaje` decimal(5,2) NOT NULL DEFAULT 0.00,
  `monto_comision` decimal(12,2) NOT NULL DEFAULT 0.00,
  `estado` enum('pendiente','aprobada','pagada','anulada') NOT NULL DEFAULT 'pendiente',
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `pagado_el` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `afiliado_referidos`
--

CREATE TABLE `afiliado_referidos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `afiliado_id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `estado` enum('registrado','activo','cancelado') NOT NULL DEFAULT 'registrado',
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `ai_conversaciones`
--

CREATE TABLE `ai_conversaciones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `canal` enum('publico','panel','superadmin') NOT NULL DEFAULT 'publico',
  `session_hash` char(64) NOT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `ruta_actual` varchar(255) DEFAULT NULL,
  `ultimo_mensaje` varchar(255) DEFAULT NULL,
  `estado` enum('abierta','cerrada','escalada') NOT NULL DEFAULT 'abierta',
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `ai_mensajes`
--

CREATE TABLE `ai_mensajes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `conversacion_id` bigint(20) UNSIGNED NOT NULL,
  `rol` enum('user','assistant','system') NOT NULL,
  `mensaje` text NOT NULL,
  `tokens` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `ai_rate_limits`
--

CREATE TABLE `ai_rate_limits` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `ip_hash` char(64) NOT NULL,
  `canal` enum('publico','panel','superadmin') NOT NULL DEFAULT 'publico',
  `window_start` datetime NOT NULL,
  `attempts` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `blocked_until` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `ai_seguimientos`
--

CREATE TABLE `ai_seguimientos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `conversacion_id` bigint(20) UNSIGNED NOT NULL,
  `canal` enum('publico','panel','superadmin') NOT NULL DEFAULT 'publico',
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `negocio_nombre` varchar(180) DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_nombre` varchar(180) DEFAULT NULL,
  `tipo` enum('consulta','ayuda','asesoramiento','incidencia','demo','venta','facturacion','llave_fiscal','pagos','addon','seguridad','otro') NOT NULL DEFAULT 'consulta',
  `prioridad` enum('baja','media','alta','critica') NOT NULL DEFAULT 'media',
  `estado` enum('nuevo','abierto','en_revision','agendado','resuelto','cerrado') NOT NULL DEFAULT 'nuevo',
  `titulo` varchar(180) NOT NULL,
  `resumen` text DEFAULT NULL,
  `ultimo_mensaje` text DEFAULT NULL,
  `ruta_actual` varchar(255) DEFAULT NULL,
  `proxima_accion` varchar(255) DEFAULT NULL,
  `nota_interna` text DEFAULT NULL,
  `agendado_para` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `auditoria`
--

CREATE TABLE `auditoria` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `accion` varchar(120) NOT NULL,
  `modulo` varchar(120) DEFAULT NULL,
  `referencia` varchar(120) DEFAULT NULL,
  `detalle` text DEFAULT NULL,
  `ip` varchar(60) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `cache`
--

CREATE TABLE `cache` (
  `key` varchar(255) NOT NULL,
  `value` mediumtext NOT NULL,
  `expiration` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `cache_locks`
--

CREATE TABLE `cache_locks` (
  `key` varchar(255) NOT NULL,
  `owner` varchar(255) NOT NULL,
  `expiration` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `caja_movimientos`
--

CREATE TABLE `caja_movimientos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `caja_sesion_id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `venta_id` bigint(20) UNSIGNED DEFAULT NULL,
  `metodo_pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `tipo` enum('apertura','venta','ingreso','egreso','cierre','ajuste') NOT NULL,
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `monto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `descripcion` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `caja_sesiones`
--

CREATE TABLE `caja_sesiones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `caja_config_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED NOT NULL,
  `fe_api_key_id` int(11) DEFAULT NULL,
  `device_hash` varchar(128) NOT NULL,
  `nombre_caja` varchar(80) NOT NULL DEFAULT 'Caja principal',
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `monto_inicial` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `monto_efectivo_sistema` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `monto_efectivo_contado` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `diferencia_cierre` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total_ventas` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total_ingresos` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total_egresos` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `estado` enum('abierta','cerrada','anulada') NOT NULL DEFAULT 'abierta',
  `modo_fiscal` tinyint(1) NOT NULL DEFAULT 0,
  `abierta_en` datetime NOT NULL DEFAULT current_timestamp(),
  `cerrada_en` datetime DEFAULT NULL,
  `ultimo_movimiento_en` datetime DEFAULT NULL,
  `cierre_observacion` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `catalogo_configuracion`
--

CREATE TABLE `catalogo_configuracion` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `titulo` varchar(160) DEFAULT NULL,
  `descripcion` varchar(255) DEFAULT NULL,
  `mensaje_envio` varchar(255) DEFAULT 'El monto de envío se confirma después de revisar el pedido y el destino.',
  `producto_envio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `imagen_portada` varchar(255) DEFAULT NULL,
  `imagen_banner` varchar(255) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `directorio_publico` tinyint(1) NOT NULL DEFAULT 0,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `catalogo_pedidos`
--

CREATE TABLE `catalogo_pedidos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `numero` varchar(40) NOT NULL,
  `estado` enum('pendiente','revision','aprobado','rechazado','cancelado') NOT NULL DEFAULT 'pendiente',
  `forma_entrega` enum('envio','retiro') NOT NULL DEFAULT 'envio',
  `requiere_fe` tinyint(1) NOT NULL DEFAULT 0,
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `subtotal` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `impuesto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `envio_monto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `metodo_pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cliente_id` bigint(20) UNSIGNED DEFAULT NULL,
  `venta_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cliente_nombre` varchar(160) NOT NULL,
  `cliente_tipo_identificacion` varchar(2) DEFAULT NULL,
  `cliente_identificacion` varchar(60) DEFAULT NULL,
  `cliente_codigo_actividad` varchar(20) DEFAULT NULL,
  `cliente_correo` varchar(180) DEFAULT NULL,
  `cliente_telefono` varchar(40) DEFAULT NULL,
  `cliente_whatsapp` varchar(40) DEFAULT NULL,
  `cliente_provincia` varchar(1) DEFAULT NULL,
  `cliente_canton` varchar(2) DEFAULT NULL,
  `cliente_distrito` varchar(2) DEFAULT NULL,
  `cliente_sennas` varchar(180) DEFAULT NULL,
  `comprobante_path` varchar(255) DEFAULT NULL,
  `nota_cliente` text DEFAULT NULL,
  `nota_admin` text DEFAULT NULL,
  `aprobado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `aprobado_en` datetime DEFAULT NULL,
  `rechazado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `rechazado_en` datetime DEFAULT NULL,
  `ip_cliente` varchar(60) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `catalogo_pedido_detalles`
--

CREATE TABLE `catalogo_pedido_detalles` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `pedido_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `producto_nombre` varchar(180) NOT NULL,
  `cantidad` decimal(14,3) NOT NULL DEFAULT 1.000,
  `precio_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `tarifa_iva` decimal(8,5) NOT NULL DEFAULT 0.00000,
  `impuesto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total_linea` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `catalogo_reacciones`
--

CREATE TABLE `catalogo_reacciones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `tipo` enum('me_gusta','buena','confiable','no_me_gusta') NOT NULL,
  `visitor_hash` varchar(80) NOT NULL,
  `ip` varchar(60) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `categorias`
--

CREATE TABLE `categorias` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nombre` varchar(120) NOT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `catalogo_destacada` tinyint(1) NOT NULL DEFAULT 0,
  `catalogo_icono` varchar(60) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `clientes`
--

CREATE TABLE `clientes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nombre` varchar(160) NOT NULL,
  `nombre_comercial` varchar(100) DEFAULT NULL,
  `tipo_identificacion` varchar(2) DEFAULT NULL,
  `identificacion` varchar(60) DEFAULT NULL,
  `codigo_actividad` varchar(20) DEFAULT NULL,
  `telefono` varchar(40) DEFAULT NULL,
  `correo` varchar(180) DEFAULT NULL,
  `direccion` varchar(255) DEFAULT NULL,
  `provincia` varchar(1) DEFAULT NULL,
  `canton` varchar(2) DEFAULT NULL,
  `distrito` varchar(2) DEFAULT NULL,
  `otras_sennas` varchar(160) DEFAULT NULL,
  `es_general` tinyint(1) NOT NULL DEFAULT 0,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `fe_exoneracion_activa` tinyint(1) NOT NULL DEFAULT 0,
  `fe_exoneracion_numero` varchar(60) DEFAULT NULL,
  `fe_exoneracion_tipo_documento` varchar(4) DEFAULT NULL,
  `fe_exoneracion_codigo_institucion` varchar(4) DEFAULT NULL,
  `fe_exoneracion_nombre_institucion` varchar(160) DEFAULT NULL,
  `fe_exoneracion_nombre_institucion_otros` varchar(160) DEFAULT NULL,
  `fe_exoneracion_fecha_emision` date DEFAULT NULL,
  `fe_exoneracion_fecha_vencimiento` date DEFAULT NULL,
  `fe_exoneracion_tarifa` decimal(8,5) NOT NULL DEFAULT 0.00000,
  `fe_exoneracion_articulo` varchar(6) DEFAULT NULL,
  `fe_exoneracion_inciso` varchar(6) DEFAULT NULL,
  `fe_exoneracion_cabys_json` longtext DEFAULT NULL,
  `fe_exoneracion_respuesta_json` longtext DEFAULT NULL,
  `fe_exoneracion_consultado_en` datetime DEFAULT NULL,
  `fe_sector_agropecuario` tinyint(1) NOT NULL DEFAULT 0,
  `fe_agro_tipo` varchar(20) DEFAULT NULL,
  `fe_agro_identificacion` varchar(20) DEFAULT NULL,
  `fe_agro_respuesta_json` longtext DEFAULT NULL,
  `fe_agro_consultado_en` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `configuracion_negocio`
--

CREATE TABLE `configuracion_negocio` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `nombre_ticket` varchar(160) DEFAULT NULL,
  `texto_ticket` varchar(255) DEFAULT NULL,
  `impuesto_default` decimal(5,2) NOT NULL DEFAULT 0.00,
  `imprimir_auto` tinyint(1) NOT NULL DEFAULT 0,
  `mostrar_logo_ticket` tinyint(1) NOT NULL DEFAULT 1,
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `contacto_mensajes`
--

CREATE TABLE `contacto_mensajes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(160) NOT NULL,
  `empresa` varchar(160) DEFAULT NULL,
  `telefono` varchar(40) NOT NULL,
  `correo` varchar(180) NOT NULL,
  `asunto` varchar(40) NOT NULL DEFAULT 'consulta',
  `mensaje` text NOT NULL,
  `estado` enum('pendiente','revision','respondido','cerrado') NOT NULL DEFAULT 'pendiente',
  `respuesta_admin` text DEFAULT NULL,
  `seguridad_score` smallint(5) UNSIGNED NOT NULL DEFAULT 0,
  `ip` varchar(60) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `contacto_rate_limits`
--

CREATE TABLE `contacto_rate_limits` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `ip_hash` char(64) NOT NULL,
  `accion` varchar(40) NOT NULL,
  `window_start` datetime NOT NULL,
  `attempts` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `blocked_until` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `demo_solicitudes`
--

CREATE TABLE `demo_solicitudes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `cedula` varchar(30) NOT NULL,
  `nombre` varchar(160) NOT NULL,
  `empresa` varchar(160) NOT NULL,
  `telefono` varchar(40) NOT NULL,
  `correo` varchar(180) NOT NULL,
  `modulos_json` longtext DEFAULT NULL,
  `estado` enum('pendiente','revision','respondido','cerrado') NOT NULL DEFAULT 'pendiente',
  `respuesta_admin` text DEFAULT NULL,
  `ip` varchar(60) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `failed_jobs`
--

CREATE TABLE `failed_jobs` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `uuid` varchar(255) NOT NULL,
  `connection` text NOT NULL,
  `queue` text NOT NULL,
  `payload` longtext NOT NULL,
  `exception` longtext NOT NULL,
  `failed_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_api_cache`
--

CREATE TABLE `fe_api_cache` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `endpoint` varchar(120) NOT NULL,
  `cache_key` varchar(180) NOT NULL,
  `response_json` longtext NOT NULL,
  `http_code` smallint(6) DEFAULT NULL,
  `expires_at` datetime NOT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_api_keys`
--

CREATE TABLE `fe_api_keys` (
  `id` int(11) NOT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cod_sucursal` varchar(3) DEFAULT NULL,
  `cod_terminal` varchar(5) DEFAULT NULL,
  `device_hash` varchar(128) DEFAULT NULL,
  `primer_uso_en` datetime DEFAULT NULL,
  `ultimo_uso_en` datetime DEFAULT NULL,
  `bloqueada_en` datetime DEFAULT NULL,
  `motivo_bloqueo` varchar(255) DEFAULT NULL,
  `api_key` varchar(150) NOT NULL,
  `sucursal_id` int(11) NOT NULL,
  `terminal_id` int(10) UNSIGNED DEFAULT NULL,
  `estado` enum('activa','suspendida','revocada') NOT NULL DEFAULT 'activa',
  `actor_tipo_origen` varchar(30) DEFAULT NULL,
  `actor_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `negocio_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `sucursal_operativa_id` bigint(20) UNSIGNED DEFAULT NULL,
  `caja_config_id` bigint(20) UNSIGNED DEFAULT NULL,
  `aprobada_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `activada_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_api_key_eventos`
--

CREATE TABLE `fe_api_key_eventos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `fe_api_key_id` int(11) NOT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `accion` varchar(40) NOT NULL,
  `device_hash_anterior` varchar(128) DEFAULT NULL,
  `device_hash_nuevo` varchar(128) DEFAULT NULL,
  `estado_anterior` varchar(30) DEFAULT NULL,
  `estado_nuevo` varchar(30) DEFAULT NULL,
  `detalle` varchar(255) DEFAULT NULL,
  `ip` varchar(60) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_cabys_items`
--

CREATE TABLE `fe_cabys_items` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `codigo` varchar(20) NOT NULL,
  `descripcion` varchar(1000) NOT NULL,
  `impuesto` decimal(5,2) DEFAULT NULL,
  `unidad` varchar(10) DEFAULT NULL,
  `activo` tinyint(1) NOT NULL DEFAULT 1,
  `fuente` varchar(50) NOT NULL DEFAULT 'bccr',
  `raw_response` mediumtext DEFAULT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_clientes`
--

CREATE TABLE `fe_clientes` (
  `id` int(11) NOT NULL,
  `negocio_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `nombre` varchar(100) NOT NULL,
  `tipo_identificacion` varchar(2) NOT NULL,
  `identificacion` varchar(20) NOT NULL,
  `regimen` enum('general','tradicional','simplificado') DEFAULT 'tradicional',
  `pin` varchar(6) DEFAULT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_condiciones_agropecuarias`
--

CREATE TABLE `fe_condiciones_agropecuarias` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `identificacion` varchar(20) DEFAULT NULL,
  `tipo` enum('agropecuario','pesca') DEFAULT NULL,
  `nombre` varchar(180) DEFAULT NULL,
  `payload_json` longtext DEFAULT NULL,
  `raw_response` mediumtext DEFAULT NULL,
  `estado` varchar(50) DEFAULT NULL,
  `vigente` tinyint(1) NOT NULL DEFAULT 0,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_contribuyentes_ae`
--

CREATE TABLE `fe_contribuyentes_ae` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `identificacion` varchar(20) NOT NULL,
  `nombre` varchar(255) NOT NULL,
  `tipo_identificacion` varchar(10) DEFAULT NULL,
  `situacion` varchar(50) DEFAULT NULL,
  `regimen` varchar(100) DEFAULT NULL,
  `es_extranjero` tinyint(1) NOT NULL DEFAULT 0,
  `actividades` longtext DEFAULT NULL,
  `fuente` varchar(50) NOT NULL DEFAULT 'hacienda',
  `raw_response` mediumtext DEFAULT NULL,
  `ultimo_http_code` smallint(6) DEFAULT NULL,
  `error_message` varchar(255) DEFAULT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp(),
  `ultimo_intento` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_documentos`
--

CREATE TABLE `fe_documentos` (
  `id` int(11) NOT NULL,
  `negocio_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `sucursal_operativa_id` bigint(20) UNSIGNED DEFAULT NULL,
  `venta_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `sucursal_id` int(11) NOT NULL,
  `tipo_documento` varchar(2) DEFAULT NULL,
  `fec_caso` varchar(60) DEFAULT NULL,
  `cod_sucursal` varchar(3) DEFAULT NULL,
  `cod_terminal` varchar(5) DEFAULT NULL,
  `ambiente` varchar(15) DEFAULT NULL,
  `receptor_nombre` varchar(100) DEFAULT NULL,
  `receptor_nombre_comercial` varchar(80) DEFAULT NULL,
  `receptor_tipo_ident` varchar(2) DEFAULT NULL,
  `receptor_ident` varchar(20) DEFAULT NULL,
  `receptor_correo` varchar(150) DEFAULT NULL,
  `receptor_provincia` int(11) DEFAULT NULL,
  `receptor_canton` varchar(2) DEFAULT NULL,
  `receptor_distrito` varchar(2) DEFAULT NULL,
  `receptor_barrio` varchar(50) DEFAULT NULL,
  `receptor_otras_sennas` varchar(160) DEFAULT NULL,
  `proveedor_compra_nombre` varchar(255) DEFAULT NULL,
  `proveedor_compra_tipo_ident` varchar(2) DEFAULT NULL,
  `proveedor_compra_ident` varchar(30) DEFAULT NULL,
  `proveedor_compra_correo` varchar(160) DEFAULT NULL,
  `proveedor_compra_otras_sennas_extranjero` varchar(300) DEFAULT NULL,
  `proveedor_compra_json` longtext DEFAULT NULL,
  `fec_payload_json` longtext DEFAULT NULL,
  `clave` varchar(50) DEFAULT NULL,
  `proveedor_sistemas` varchar(20) DEFAULT NULL,
  `consecutivo` varchar(20) DEFAULT NULL,
  `situacion` varchar(2) DEFAULT NULL,
  `condicion_venta` varchar(2) NOT NULL,
  `plazo_credito` int(11) DEFAULT NULL,
  `moneda` varchar(3) NOT NULL,
  `tipo_cambio` decimal(10,2) NOT NULL,
  `medio_pago` varchar(2) NOT NULL,
  `medio_pago_otros` varchar(100) DEFAULT NULL,
  `total_serv_gravados` decimal(18,5) NOT NULL,
  `total_serv_exentos` decimal(18,5) NOT NULL,
  `total_serv_exonerados` decimal(18,5) NOT NULL,
  `total_serv_no_sujetos` decimal(18,5) NOT NULL,
  `total_merc_gravadas` decimal(18,5) NOT NULL,
  `total_merc_exentas` decimal(18,5) NOT NULL,
  `total_merc_exoneradas` decimal(18,5) NOT NULL,
  `total_merc_no_sujetas` decimal(18,5) NOT NULL,
  `total_gravado` decimal(18,5) NOT NULL,
  `total_exento` decimal(18,5) NOT NULL,
  `total_exonerado` decimal(18,5) NOT NULL,
  `total_no_sujeto` decimal(18,5) NOT NULL,
  `total_venta` decimal(18,5) NOT NULL,
  `total_descuentos` decimal(18,5) NOT NULL,
  `total_venta_neta` decimal(18,5) NOT NULL,
  `total_impuesto` decimal(18,5) NOT NULL,
  `total_imp_asum_emi` decimal(18,5) NOT NULL,
  `total_desglose_impuestos_json` longtext DEFAULT NULL,
  `total_iva_devuelto` decimal(18,5) NOT NULL,
  `total_comprobante` decimal(18,5) NOT NULL,
  `observaciones` text DEFAULT NULL,
  `cod_actividad_emisor` varchar(20) DEFAULT NULL,
  `cod_actividad_receptor` varchar(20) DEFAULT NULL,
  `xml_firmado` longtext DEFAULT NULL,
  `xml_respuesta` longtext DEFAULT NULL,
  `correo_enviado_en` datetime DEFAULT NULL,
  `correo_error` varchar(255) DEFAULT NULL,
  `webhook_url` varchar(500) DEFAULT NULL,
  `webhook_token` varchar(180) DEFAULT NULL,
  `ultimo_webhook_payload_json` longtext DEFAULT NULL,
  `webhook_en` datetime DEFAULT NULL,
  `webhook_error` varchar(255) DEFAULT NULL,
  `respuesta_hacienda` varchar(15) DEFAULT NULL,
  `respuesta_error_hacienda` text DEFAULT NULL,
  `mh_intentos_webhook` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `mh_webhook_ultimo_en` datetime DEFAULT NULL,
  `xml_schema_version` varchar(10) NOT NULL DEFAULT '4.4',
  `condicion_venta_otros` varchar(100) DEFAULT NULL,
  `estado` varchar(50) DEFAULT NULL,
  `fecha_emision` varchar(150) DEFAULT NULL,
  `ref_tipo_documento` varchar(2) DEFAULT NULL,
  `ref_clave` varchar(50) DEFAULT NULL,
  `ref_numero_documento` varchar(50) DEFAULT NULL,
  `ref_fecha_emision` varchar(150) DEFAULT NULL,
  `ref_codigo` varchar(2) DEFAULT NULL,
  `ref_razon` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_documento_detalles`
--

CREATE TABLE `fe_documento_detalles` (
  `id` int(11) NOT NULL,
  `documento_id` int(11) NOT NULL,
  `num_linea` int(11) NOT NULL,
  `cod_prod_interno` varchar(50) DEFAULT NULL,
  `codigo_cabys` varchar(13) NOT NULL,
  `codigo_comercial_tipo` varchar(2) DEFAULT NULL,
  `codigo_comercial` varchar(20) DEFAULT NULL,
  `unidad_medida` varchar(15) NOT NULL,
  `unidad_medida_comercial` varchar(20) DEFAULT NULL,
  `partida_arancelaria` varchar(12) DEFAULT NULL,
  `tipo_transaccion` varchar(2) DEFAULT NULL,
  `detalle` varchar(200) NOT NULL,
  `registro_medicamento` varchar(100) DEFAULT NULL,
  `forma_farmaceutica` varchar(3) DEFAULT NULL,
  `numero_vin_serie` varchar(17) DEFAULT NULL,
  `precio_unitario` decimal(18,5) NOT NULL,
  `cantidad` decimal(18,3) NOT NULL,
  `monto_total` decimal(18,5) NOT NULL,
  `codigo_descuento` varchar(2) DEFAULT NULL,
  `codigo_descuento_otros` varchar(100) DEFAULT NULL,
  `razon_descuento` varchar(80) DEFAULT NULL,
  `monto_descuento` decimal(18,5) DEFAULT NULL,
  `sub_total` decimal(18,5) NOT NULL,
  `base_imponible` decimal(18,5) NOT NULL,
  `codigo_impuesto` varchar(2) NOT NULL,
  `codigo_impuesto_otros` varchar(100) DEFAULT NULL,
  `codigo_tarifa_iva` varchar(2) DEFAULT NULL,
  `tarifa_impuesto` decimal(4,2) DEFAULT NULL,
  `monto_impuesto` decimal(18,5) DEFAULT NULL,
  `impuesto_exonerado` decimal(18,5) NOT NULL DEFAULT 0.00000,
  `impuesto_iva_fabrica` decimal(18,5) NOT NULL DEFAULT 0.00000,
  `impuesto_asumido_emisor` decimal(18,5) DEFAULT NULL,
  `impuesto_neto` decimal(18,5) DEFAULT NULL,
  `monto_total_linea` decimal(18,5) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_documento_detalle_descuentos`
--

CREATE TABLE `fe_documento_detalle_descuentos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `documento_detalle_id` int(11) NOT NULL,
  `codigo_descuento` varchar(2) DEFAULT NULL,
  `codigo_descuento_otros` varchar(100) DEFAULT NULL,
  `naturaleza_descuento` varchar(80) DEFAULT NULL,
  `monto_descuento` decimal(18,5) NOT NULL DEFAULT 0.00000,
  `codigo` varchar(2) NOT NULL,
  `codigo_otros` varchar(100) DEFAULT NULL,
  `monto` decimal(18,5) NOT NULL DEFAULT 0.00000,
  `naturaleza` varchar(80) DEFAULT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_documento_detalle_impuestos`
--

CREATE TABLE `fe_documento_detalle_impuestos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `documento_detalle_id` int(11) NOT NULL,
  `codigo` varchar(2) NOT NULL,
  `codigo_otros` varchar(100) DEFAULT NULL,
  `codigo_tarifa_iva` varchar(2) DEFAULT NULL,
  `tarifa` decimal(5,2) DEFAULT NULL,
  `factor_calculo_iva` decimal(10,5) DEFAULT NULL,
  `monto` decimal(18,5) NOT NULL DEFAULT 0.00000,
  `monto_exoneracion` decimal(18,5) NOT NULL DEFAULT 0.00000,
  `impuesto_asumido_emisor_fabrica` decimal(18,5) NOT NULL DEFAULT 0.00000,
  `impuesto_neto` decimal(18,5) NOT NULL DEFAULT 0.00000,
  `exoneracion_tipo_documento_ex` varchar(4) DEFAULT NULL,
  `exoneracion_tipo_documento_ex_otros` varchar(100) DEFAULT NULL,
  `exoneracion_numero_documento` varchar(40) DEFAULT NULL,
  `exoneracion_articulo` varchar(6) DEFAULT NULL,
  `exoneracion_inciso` varchar(6) DEFAULT NULL,
  `exoneracion_nombre_institucion` varchar(4) DEFAULT NULL,
  `exoneracion_nombre_institucion_otros` varchar(160) DEFAULT NULL,
  `exoneracion_fecha_emision` varchar(35) DEFAULT NULL,
  `exoneracion_tarifa` decimal(8,5) NOT NULL DEFAULT 0.00000,
  `exoneracion_json` longtext DEFAULT NULL,
  `datos_especificos_json` longtext DEFAULT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_documento_medios_pago`
--

CREATE TABLE `fe_documento_medios_pago` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `documento_id` int(11) NOT NULL,
  `tipo_medio_pago` varchar(2) NOT NULL,
  `medio_pago_otro` varchar(100) DEFAULT NULL,
  `total_medio_pago` decimal(18,5) DEFAULT NULL,
  `orden` int(11) NOT NULL DEFAULT 1,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_documento_recepciones_xml`
--

CREATE TABLE `fe_documento_recepciones_xml` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `clave` varchar(50) DEFAULT NULL,
  `tipo_documento` varchar(2) DEFAULT NULL,
  `numero_consecutivo` varchar(20) DEFAULT NULL,
  `fecha_emision` varchar(35) DEFAULT NULL,
  `emisor_nombre` varchar(160) DEFAULT NULL,
  `emisor_tipo_identificacion` varchar(2) DEFAULT NULL,
  `emisor_identificacion` varchar(20) DEFAULT NULL,
  `receptor_nombre` varchar(160) DEFAULT NULL,
  `receptor_tipo_identificacion` varchar(2) DEFAULT NULL,
  `receptor_identificacion` varchar(20) DEFAULT NULL,
  `moneda` varchar(3) DEFAULT 'CRC',
  `total_impuesto` decimal(18,5) DEFAULT 0.00000,
  `total_comprobante` decimal(18,5) DEFAULT 0.00000,
  `mensaje_receptor_id` int(11) DEFAULT NULL,
  `mensaje` varchar(1) DEFAULT NULL,
  `detalle_mensaje` varchar(160) DEFAULT NULL,
  `condicion_impuesto` varchar(2) DEFAULT NULL,
  `codigo_actividad` varchar(20) DEFAULT NULL,
  `monto_impuesto_acreditar` decimal(18,5) DEFAULT NULL,
  `monto_gasto_aplicable` decimal(18,5) DEFAULT NULL,
  `estado_mh` varchar(20) DEFAULT NULL,
  `respuesta_error_hacienda` text DEFAULT NULL,
  `xml_firmado` longtext DEFAULT NULL,
  `xml_respuesta` longtext DEFAULT NULL,
  `archivo_nombre` varchar(255) DEFAULT NULL,
  `usuario_id` int(11) DEFAULT NULL,
  `enviado_en` datetime DEFAULT NULL,
  `consultado_en` datetime DEFAULT NULL,
  `xml` longtext DEFAULT NULL,
  `estado` varchar(50) DEFAULT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_exoneraciones_consultas`
--

CREATE TABLE `fe_exoneraciones_consultas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `numero_documento` varchar(60) DEFAULT NULL,
  `tipo_documento_ex` varchar(4) DEFAULT NULL,
  `codigo_institucion` varchar(20) DEFAULT NULL,
  `fecha_emision` date DEFAULT NULL,
  `fecha_vencimiento` date DEFAULT NULL,
  `tarifa_exonerada` decimal(5,2) DEFAULT NULL,
  `cabys_json` mediumtext DEFAULT NULL,
  `vigente` tinyint(1) NOT NULL DEFAULT 0,
  `payload_json` longtext DEFAULT NULL,
  `raw_response` mediumtext DEFAULT NULL,
  `estado` varchar(50) DEFAULT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_formas_farmaceuticas`
--

CREATE TABLE `fe_formas_farmaceuticas` (
  `id` int(10) UNSIGNED NOT NULL,
  `codigo` varchar(3) NOT NULL,
  `nombre` varchar(160) NOT NULL,
  `activo` tinyint(1) NOT NULL DEFAULT 1,
  `fuente` varchar(80) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_mensajes_receptor`
--

CREATE TABLE `fe_mensajes_receptor` (
  `id` int(11) NOT NULL,
  `negocio_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `sucursal_id` int(11) NOT NULL,
  `tipo_documento` varchar(2) NOT NULL,
  `clave` varchar(50) NOT NULL,
  `numero_consecutivo_receptor` varchar(20) NOT NULL,
  `mensaje` varchar(1) NOT NULL,
  `detalle_mensaje` varchar(160) DEFAULT NULL,
  `numero_cedula_emisor` varchar(20) NOT NULL,
  `tipo_identificacion_emisor` varchar(2) NOT NULL,
  `numero_cedula_receptor` varchar(20) NOT NULL,
  `tipo_identificacion_receptor` varchar(2) NOT NULL,
  `fecha_emision_doc` varchar(35) NOT NULL,
  `monto_total_impuesto` decimal(18,5) DEFAULT NULL,
  `codigo_actividad` varchar(20) DEFAULT NULL,
  `condicion_impuesto` varchar(2) DEFAULT NULL,
  `monto_total_impuesto_acreditar` decimal(18,5) DEFAULT NULL,
  `monto_total_de_gasto_aplicable` decimal(18,5) DEFAULT NULL,
  `total_factura` decimal(18,5) NOT NULL,
  `ambiente` varchar(15) DEFAULT NULL,
  `origen` varchar(20) DEFAULT NULL,
  `validado_previo` tinyint(1) NOT NULL DEFAULT 0,
  `observacion_validacion` text DEFAULT NULL,
  `usuario_creador_id` int(11) DEFAULT NULL,
  `usuario_envio_id` int(11) DEFAULT NULL,
  `webhook_url` varchar(500) DEFAULT NULL,
  `webhook_token` varchar(96) DEFAULT NULL,
  `webhook_en` datetime DEFAULT NULL,
  `ultimo_webhook_payload_json` longtext DEFAULT NULL,
  `xml_firmado` longtext DEFAULT NULL,
  `xml_respuesta` longtext DEFAULT NULL,
  `respuesta_hacienda` varchar(15) DEFAULT NULL,
  `respuesta_error_hacienda` text DEFAULT NULL,
  `estado` varchar(50) DEFAULT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_mh_credenciales`
--

CREATE TABLE `fe_mh_credenciales` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `mh_username` varchar(100) NOT NULL,
  `mh_password` varchar(100) NOT NULL,
  `mh_environment` enum('sandbox','production') NOT NULL,
  `cert_path` varchar(255) NOT NULL,
  `cert_pin` varchar(10) NOT NULL,
  `cert_vigente_hasta` datetime DEFAULT NULL,
  `cert_estado` varchar(30) DEFAULT NULL,
  `cert_detalle` varchar(255) DEFAULT NULL,
  `client_id` int(11) NOT NULL,
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_sucursales`
--

CREATE TABLE `fe_sucursales` (
  `id` int(11) NOT NULL,
  `negocio_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `sucursal_operativa_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cod_pais` varchar(3) DEFAULT '506',
  `cod_sucursal` varchar(3) NOT NULL,
  `cod_terminal` varchar(5) DEFAULT '00001',
  `cod_actividad` varchar(20) NOT NULL,
  `cliente_id` int(11) NOT NULL,
  `provincia` varchar(1) NOT NULL,
  `canton` varchar(2) NOT NULL,
  `distrito` varchar(2) NOT NULL,
  `barrio` varchar(2) DEFAULT NULL,
  `sennas` varchar(160) NOT NULL,
  `telefono` varchar(20) DEFAULT NULL,
  `correo` varchar(100) NOT NULL,
  `mh_ambiente` enum('sandbox','production') NOT NULL,
  `consec_fe` int(11) NOT NULL,
  `consec_te` int(11) NOT NULL,
  `nombre_comercial` varchar(100) DEFAULT NULL,
  `pdf_logo_path` varchar(255) DEFAULT NULL,
  `pdf_logo_w_mm` decimal(6,2) NOT NULL DEFAULT 28.00,
  `pdf_logo_h_mm` decimal(6,2) NOT NULL DEFAULT 0.00,
  `consec_nc` int(11) NOT NULL DEFAULT 0,
  `consec_nd` int(11) NOT NULL,
  `consec_fec` int(11) NOT NULL DEFAULT 0,
  `consec_fee` int(11) NOT NULL DEFAULT 0,
  `consec_cce` int(11) NOT NULL DEFAULT 0,
  `consec_cpce` int(11) NOT NULL DEFAULT 0,
  `consec_rce` int(11) NOT NULL DEFAULT 0,
  `consec_rep` int(11) NOT NULL DEFAULT 0,
  `api_key` varchar(64) DEFAULT NULL,
  `api_key_activa` tinyint(1) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_terminales`
--

CREATE TABLE `fe_terminales` (
  `id` int(10) UNSIGNED NOT NULL,
  `negocio_id_origen` bigint(20) UNSIGNED DEFAULT NULL,
  `sucursal_operativa_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cliente_id` int(10) UNSIGNED NOT NULL,
  `cod_sucursal` char(3) NOT NULL,
  `cod_terminal` char(5) NOT NULL,
  `nombre` varchar(120) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado_en` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_tipo_cambio`
--

CREATE TABLE `fe_tipo_cambio` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `venta` decimal(15,4) DEFAULT NULL,
  `fuente` varchar(50) NOT NULL DEFAULT 'hacienda',
  `raw_response` mediumtext DEFAULT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp(),
  `compra` decimal(15,4) DEFAULT NULL,
  `fecha` date NOT NULL,
  `moneda` varchar(10) NOT NULL DEFAULT 'USD'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `fe_webhook_hacienda_logs`
--

CREATE TABLE `fe_webhook_hacienda_logs` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `clave` varchar(50) NOT NULL,
  `estado` varchar(30) NOT NULL,
  `payload_hash` char(64) DEFAULT NULL,
  `payload` longtext DEFAULT NULL,
  `headers` longtext DEFAULT NULL,
  `ip` varchar(45) DEFAULT NULL,
  `procesado` tinyint(1) NOT NULL DEFAULT 0,
  `error` text DEFAULT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `hotel_config`
--

CREATE TABLE `hotel_config` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `habitaciones_incluidas` int(10) UNSIGNED NOT NULL DEFAULT 10,
  `habitaciones_adicionales` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `precio_habitacion_adicional` decimal(12,2) NOT NULL DEFAULT 0.00,
  `moneda` varchar(3) NOT NULL DEFAULT 'USD',
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `hotel_habitaciones`
--

CREATE TABLE `hotel_habitaciones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `tipo_id` bigint(20) UNSIGNED NOT NULL,
  `numero` varchar(40) NOT NULL,
  `nombre` varchar(140) DEFAULT NULL,
  `piso` varchar(40) DEFAULT NULL,
  `ubicacion` varchar(180) DEFAULT NULL,
  `imagen_path` varchar(255) DEFAULT NULL,
  `estado_operativo` enum('disponible','ocupada','reservada','mantenimiento','limpieza','bloqueada') NOT NULL DEFAULT 'disponible',
  `bloqueo_hasta` datetime DEFAULT NULL,
  `bloqueo_motivo` varchar(180) DEFAULT NULL,
  `descripcion` text DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `hotel_reservas`
--

CREATE TABLE `hotel_reservas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `habitacion_id` bigint(20) UNSIGNED NOT NULL,
  `tipo_habitacion_id` bigint(20) UNSIGNED NOT NULL,
  `cliente_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `venta_id` bigint(20) UNSIGNED DEFAULT NULL,
  `codigo` varchar(40) NOT NULL,
  `huesped_nombre` varchar(180) NOT NULL,
  `tipo_identificacion` varchar(2) DEFAULT NULL,
  `identificacion` varchar(40) DEFAULT NULL,
  `correo` varchar(180) DEFAULT NULL,
  `telefono` varchar(40) DEFAULT NULL,
  `whatsapp` varchar(40) DEFAULT NULL,
  `fecha_ingreso` datetime NOT NULL,
  `fecha_salida_programada` datetime DEFAULT NULL,
  `fecha_salida_real` datetime DEFAULT NULL,
  `salida_abierta` tinyint(1) NOT NULL DEFAULT 0,
  `modalidad_pago` enum('entrada','salida','mixto') NOT NULL DEFAULT 'salida',
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `noches` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `precio_noche` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `subtotal` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `descuento` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `impuesto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `abonos_total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `saldo` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `requiere_fe` tinyint(1) NOT NULL DEFAULT 0,
  `tipo_documento_fiscal` varchar(2) DEFAULT NULL,
  `estado` enum('reservada','checkin','checkout','cancelada','no_show') NOT NULL DEFAULT 'reservada',
  `observaciones` text DEFAULT NULL,
  `facturada_en` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `hotel_reserva_cargos`
--

CREATE TABLE `hotel_reserva_cargos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `reserva_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED DEFAULT NULL,
  `descripcion` varchar(220) NOT NULL,
  `cantidad` decimal(14,3) NOT NULL DEFAULT 1.000,
  `precio_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `impuesto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `tipo` enum('hospedaje','producto','servicio','extra') NOT NULL DEFAULT 'extra',
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `hotel_reserva_pagos`
--

CREATE TABLE `hotel_reserva_pagos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `reserva_id` bigint(20) UNSIGNED NOT NULL,
  `metodo_pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `monto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `referencia` varchar(160) DEFAULT NULL,
  `comprobante_path` varchar(255) DEFAULT NULL,
  `tipo` enum('anticipo','abono','pago_final') NOT NULL DEFAULT 'abono',
  `estado` enum('pendiente','validado','rechazado') NOT NULL DEFAULT 'pendiente',
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `hotel_tipos_habitacion`
--

CREATE TABLE `hotel_tipos_habitacion` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nombre` varchar(140) NOT NULL,
  `descripcion` text DEFAULT NULL,
  `capacidad` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `precio_noche` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `codigo_cabys` varchar(13) DEFAULT NULL,
  `unidad_medida` varchar(15) NOT NULL DEFAULT 'Sp',
  `codigo_impuesto` varchar(2) NOT NULL DEFAULT '01',
  `codigo_tarifa_iva` varchar(2) NOT NULL DEFAULT '08',
  `tarifa_iva` decimal(5,2) NOT NULL DEFAULT 13.00,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_boletas_farmaceuticas`
--

CREATE TABLE `inventario_boletas_farmaceuticas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `lote_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cliente_id` bigint(20) UNSIGNED DEFAULT NULL,
  `venta_id` bigint(20) UNSIGNED DEFAULT NULL,
  `numero_boleta` varchar(120) NOT NULL,
  `paciente_nombre` varchar(180) DEFAULT NULL,
  `paciente_identificacion` varchar(60) DEFAULT NULL,
  `medico` varchar(180) DEFAULT NULL,
  `receta` varchar(180) DEFAULT NULL,
  `detalle` text DEFAULT NULL,
  `fecha_boleta` date NOT NULL,
  `estado` enum('emitida','anulada') NOT NULL DEFAULT 'emitida',
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_codigos_barras`
--

CREATE TABLE `inventario_codigos_barras` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `lote_id` bigint(20) UNSIGNED DEFAULT NULL,
  `codigo` varchar(120) NOT NULL,
  `origen` enum('manual','generado','xml') NOT NULL DEFAULT 'generado',
  `tipo_uso` enum('principal','oferta_combo') NOT NULL DEFAULT 'principal',
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_compras`
--

CREATE TABLE `inventario_compras` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `proveedor_nombre` varchar(180) DEFAULT NULL,
  `proveedor_identificacion` varchar(60) DEFAULT NULL,
  `numero_documento` varchar(80) DEFAULT NULL,
  `tipo_documento` enum('xml','pdf','xml_pdf','manual') NOT NULL DEFAULT 'manual',
  `archivo_xml` varchar(255) DEFAULT NULL,
  `archivo_pdf` varchar(255) DEFAULT NULL,
  `fecha_documento` date DEFAULT NULL,
  `subtotal` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `impuesto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `moneda` char(3) NOT NULL DEFAULT 'CRC',
  `estado` enum('borrador','procesada','anulada') NOT NULL DEFAULT 'procesada',
  `margen_general_tipo` enum('porcentaje','monto') NOT NULL DEFAULT 'porcentaje',
  `margen_general_valor` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `recepcion_xml_id` bigint(20) UNSIGNED DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_compra_detalles`
--

CREATE TABLE `inventario_compra_detalles` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `compra_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED DEFAULT NULL,
  `descripcion` varchar(500) NOT NULL,
  `codigo` varchar(120) DEFAULT NULL,
  `codigo_cabys` varchar(20) DEFAULT NULL,
  `unidad` varchar(30) DEFAULT NULL,
  `cantidad` decimal(14,3) NOT NULL DEFAULT 0.000,
  `costo_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `impuesto_monto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `impuesto_porcentaje` decimal(8,5) NOT NULL DEFAULT 0.00000,
  `margen_tipo` enum('porcentaje','monto') NOT NULL DEFAULT 'porcentaje',
  `margen_valor` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `precio_sugerido` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `lote_id` bigint(20) UNSIGNED DEFAULT NULL,
  `ubicacion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `fecha_vencimiento` date DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_garantias`
--

CREATE TABLE `inventario_garantias` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `venta_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cliente_id` bigint(20) UNSIGNED DEFAULT NULL,
  `serie` varchar(120) DEFAULT NULL,
  `numero_garantia` varchar(80) DEFAULT NULL,
  `tipo_garantia` enum('fabricante','tienda','proveedor','extendida','otro') NOT NULL DEFAULT 'tienda',
  `proveedor_garantia` varchar(180) DEFAULT NULL,
  `fecha_inicio` date NOT NULL,
  `fecha_fin` date DEFAULT NULL,
  `detalle` text DEFAULT NULL,
  `cobertura` text DEFAULT NULL,
  `condiciones` text DEFAULT NULL,
  `exclusiones` text DEFAULT NULL,
  `procedimiento_reclamo` text DEFAULT NULL,
  `contacto_soporte` varchar(180) DEFAULT NULL,
  `documento_archivo` varchar(255) DEFAULT NULL,
  `documento_nombre` varchar(180) DEFAULT NULL,
  `documento_tipo` varchar(120) DEFAULT NULL,
  `observaciones` text DEFAULT NULL,
  `estado` enum('activa','cerrada','anulada') NOT NULL DEFAULT 'activa',
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_lotes`
--

CREATE TABLE `inventario_lotes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `ubicacion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `codigo_lote` varchar(120) NOT NULL,
  `fecha_ingreso` date NOT NULL,
  `fecha_vencimiento` date DEFAULT NULL,
  `cantidad_inicial` decimal(14,3) NOT NULL DEFAULT 0.000,
  `cantidad_actual` decimal(14,3) NOT NULL DEFAULT 0.000,
  `costo_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `margen_tipo` enum('porcentaje','monto') NOT NULL DEFAULT 'porcentaje',
  `margen_valor` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `precio_sugerido` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `proveedor_nombre` varchar(180) DEFAULT NULL,
  `documento_compra_id` bigint(20) UNSIGNED DEFAULT NULL,
  `estado` enum('activo','agotado','bloqueado','vencido') NOT NULL DEFAULT 'activo',
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_movimientos`
--

CREATE TABLE `inventario_movimientos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `lote_id` bigint(20) UNSIGNED DEFAULT NULL,
  `ubicacion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `sucursal_origen_id` bigint(20) UNSIGNED DEFAULT NULL,
  `sucursal_destino_id` bigint(20) UNSIGNED DEFAULT NULL,
  `traslado_id` bigint(20) UNSIGNED DEFAULT NULL,
  `venta_id` bigint(20) UNSIGNED DEFAULT NULL,
  `compra_id` bigint(20) UNSIGNED DEFAULT NULL,
  `tipo` enum('entrada','salida','ajuste','traslado','devolucion','garantia') NOT NULL,
  `cantidad` decimal(14,3) NOT NULL DEFAULT 0.000,
  `costo_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `precio_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `detalle` varchar(255) DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_productos_farmaceuticos`
--

CREATE TABLE `inventario_productos_farmaceuticos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `registro_sanitario` varchar(120) DEFAULT NULL,
  `forma_farmaceutica` varchar(3) DEFAULT NULL,
  `concentracion` varchar(120) DEFAULT NULL,
  `presentacion` varchar(160) DEFAULT NULL,
  `laboratorio` varchar(180) DEFAULT NULL,
  `titular_registro` varchar(180) DEFAULT NULL,
  `condiciones_almacenamiento` varchar(180) DEFAULT NULL,
  `certificacion_tipo` enum('registro_sanitario','bpm','calidad','proveedor','otro') NOT NULL DEFAULT 'registro_sanitario',
  `certificacion_numero` varchar(120) DEFAULT NULL,
  `certificacion_archivo` varchar(255) DEFAULT NULL,
  `fecha_emision_certificacion` date DEFAULT NULL,
  `fecha_vencimiento_certificacion` date DEFAULT NULL,
  `observaciones` text DEFAULT NULL,
  `estado` enum('vigente','por_vencer','vencido','inactivo') NOT NULL DEFAULT 'vigente',
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_stock_ubicaciones`
--

CREATE TABLE `inventario_stock_ubicaciones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED NOT NULL,
  `ubicacion_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `lote_id` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
  `cantidad` decimal(14,3) NOT NULL DEFAULT 0.000,
  `costo_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp(),
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_traslados`
--

CREATE TABLE `inventario_traslados` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `numero` varchar(30) NOT NULL,
  `origen_sucursal_id` bigint(20) UNSIGNED NOT NULL,
  `destino_sucursal_id` bigint(20) UNSIGNED NOT NULL,
  `origen_ubicacion_id` bigint(20) UNSIGNED NOT NULL,
  `destino_ubicacion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `estado` enum('borrador','en_transito','recibido','anulado') NOT NULL DEFAULT 'borrador',
  `token` varchar(128) NOT NULL,
  `token_hash` varchar(128) DEFAULT NULL,
  `responsable_salida_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `responsable_recibido_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `creado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `observaciones` text DEFAULT NULL,
  `costo_total` decimal(18,5) NOT NULL DEFAULT 0.00000,
  `precio_total` decimal(18,5) NOT NULL DEFAULT 0.00000,
  `salida_at` datetime DEFAULT NULL,
  `recibido_at` datetime DEFAULT NULL,
  `anulado_at` datetime DEFAULT NULL,
  `anulado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `motivo_anulacion` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_traslado_eventos`
--

CREATE TABLE `inventario_traslado_eventos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `traslado_id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `evento` varchar(40) NOT NULL,
  `detalle` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_traslado_lineas`
--

CREATE TABLE `inventario_traslado_lineas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `traslado_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `destino_producto_id` bigint(20) UNSIGNED DEFAULT NULL,
  `lote_id` bigint(20) UNSIGNED DEFAULT NULL,
  `destino_lote_id` bigint(20) UNSIGNED DEFAULT NULL,
  `origen_ubicacion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `destino_ubicacion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cantidad` decimal(14,3) NOT NULL DEFAULT 0.000,
  `recibido_cantidad` decimal(14,3) NOT NULL DEFAULT 0.000,
  `recibido_modo` enum('manual','qr') NOT NULL DEFAULT 'manual',
  `recibido_codigo` varchar(160) DEFAULT NULL,
  `recibido_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `recibido_at` datetime DEFAULT NULL,
  `costo_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `precio_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `detalle` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `inventario_ubicaciones`
--

CREATE TABLE `inventario_ubicaciones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nombre` varchar(160) NOT NULL,
  `tipo` enum('bodega','pasillo','modulo','punto_venta') NOT NULL DEFAULT 'bodega',
  `bodega` varchar(120) DEFAULT NULL,
  `pasillo` varchar(120) DEFAULT NULL,
  `modulo` varchar(120) DEFAULT NULL,
  `referencia` varchar(255) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `jobs`
--

CREATE TABLE `jobs` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `queue` varchar(255) NOT NULL,
  `payload` longtext NOT NULL,
  `attempts` tinyint(3) UNSIGNED NOT NULL,
  `reserved_at` int(10) UNSIGNED DEFAULT NULL,
  `available_at` int(10) UNSIGNED NOT NULL,
  `created_at` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `job_batches`
--

CREATE TABLE `job_batches` (
  `id` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `total_jobs` int(11) NOT NULL,
  `pending_jobs` int(11) NOT NULL,
  `failed_jobs` int(11) NOT NULL,
  `failed_job_ids` longtext NOT NULL,
  `options` mediumtext DEFAULT NULL,
  `cancelled_at` int(11) DEFAULT NULL,
  `created_at` int(11) NOT NULL,
  `finished_at` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `landing_paginas`
--

CREATE TABLE `landing_paginas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `tipo` enum('predefinida','normal') NOT NULL DEFAULT 'normal',
  `template` varchar(40) NOT NULL DEFAULT 'normal',
  `titulo` varchar(180) NOT NULL,
  `slug` varchar(180) NOT NULL,
  `subtitulo` varchar(255) DEFAULT NULL,
  `imagen_portada` varchar(255) DEFAULT NULL,
  `contenido_html` longtext DEFAULT NULL,
  `contenido_json` longtext DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `mostrar_menu` tinyint(1) NOT NULL DEFAULT 0,
  `orden` int(11) NOT NULL DEFAULT 0,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `landing_testimonios`
--

CREATE TABLE `landing_testimonios` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `solicitado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nombre_cliente` varchar(160) NOT NULL,
  `detalle_cliente` varchar(160) DEFAULT NULL,
  `comentario` varchar(200) DEFAULT NULL,
  `calificacion` tinyint(1) NOT NULL DEFAULT 5,
  `foto` varchar(255) DEFAULT NULL,
  `estado` enum('solicitado','pendiente','aprobado','rechazado') NOT NULL DEFAULT 'solicitado',
  `visible` tinyint(1) NOT NULL DEFAULT 0,
  `solicitud_masiva` tinyint(1) NOT NULL DEFAULT 0,
  `solicitado_en` datetime DEFAULT NULL,
  `respondido_en` datetime DEFAULT NULL,
  `aprobado_en` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `login_challenges`
--

CREATE TABLE `login_challenges` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `selector` char(32) NOT NULL,
  `token_hash` varchar(255) NOT NULL,
  `expira_en` datetime NOT NULL,
  `usado_en` datetime DEFAULT NULL,
  `ip` varchar(60) DEFAULT NULL,
  `host` varchar(120) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `login_rate_limits`
--

CREATE TABLE `login_rate_limits` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `login_hash` char(64) NOT NULL,
  `window_start` datetime NOT NULL,
  `attempts` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `blocked_until` datetime DEFAULT NULL,
  `ip` varchar(60) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `metodos_pago`
--

CREATE TABLE `metodos_pago` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nombre` varchar(100) NOT NULL,
  `clave` varchar(80) NOT NULL,
  `codigo_mh` varchar(2) DEFAULT NULL,
  `codigo_mh_otro` varchar(100) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `catalogo_visible` tinyint(1) NOT NULL DEFAULT 1,
  `titular_pago` varchar(160) DEFAULT NULL,
  `numero_cuenta` varchar(180) DEFAULT NULL,
  `numero_sinpe` varchar(60) DEFAULT NULL,
  `instrucciones_pago` text DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `modulos`
--

CREATE TABLE `modulos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(120) NOT NULL,
  `clave` varchar(120) NOT NULL,
  `descripcion` varchar(255) DEFAULT NULL,
  `detalle_html` longtext DEFAULT NULL,
  `imagenes_json` longtext DEFAULT NULL,
  `facturacion_cabys` varchar(20) DEFAULT NULL,
  `facturacion_detalle` varchar(255) DEFAULT NULL,
  `facturacion_tarifa_iva` decimal(8,5) NOT NULL DEFAULT 13.00000,
  `icono` varchar(80) DEFAULT NULL,
  `precio_mensual` decimal(12,2) NOT NULL DEFAULT 0.00,
  `precio_unico` decimal(12,2) NOT NULL DEFAULT 0.00,
  `precio_por_documento` decimal(12,4) NOT NULL DEFAULT 0.0000,
  `tipo_cobro` enum('gratis','mensual','unico','mixto') NOT NULL DEFAULT 'mensual',
  `requiere_cantidad_documentos` tinyint(1) NOT NULL DEFAULT 0,
  `requiere_cantidad_habitaciones` tinyint(1) NOT NULL DEFAULT 0,
  `requiere_cantidad_sucursales` tinyint(1) NOT NULL DEFAULT 0,
  `sucursales_incluidas` int(10) UNSIGNED NOT NULL DEFAULT 2,
  `precio_por_sucursal` decimal(12,2) NOT NULL DEFAULT 0.00,
  `terminales_fe_incluidas` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `precio_por_terminal_fe` decimal(12,2) NOT NULL DEFAULT 0.00,
  `habitaciones_incluidas` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `precio_por_habitacion` decimal(12,2) NOT NULL DEFAULT 0.00,
  `ruta_base` varchar(180) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `orden` int(11) NOT NULL DEFAULT 0,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `nanaclub_comision_primer_mes` decimal(8,5) DEFAULT NULL,
  `nanaclub_comision_renovacion` decimal(8,5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `modulo_personalizado_solicitudes`
--

CREATE TABLE `modulo_personalizado_solicitudes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `negocio_nombre` varchar(160) NOT NULL,
  `negocio_correo` varchar(180) DEFAULT NULL,
  `negocio_telefono` varchar(40) DEFAULT NULL,
  `admin_nombre` varchar(160) DEFAULT NULL,
  `admin_correo` varchar(180) DEFAULT NULL,
  `titulo` varchar(160) NOT NULL,
  `descripcion_html` mediumtext NOT NULL,
  `adjuntos_json` mediumtext DEFAULT NULL,
  `estado` enum('pendiente','revision','respondido','cerrado') NOT NULL DEFAULT 'pendiente',
  `respuesta_admin` text DEFAULT NULL,
  `revisado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `revisado_en` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `nanaclub_comisiones`
--

CREATE TABLE `nanaclub_comisiones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `miembro_id` bigint(20) UNSIGNED NOT NULL,
  `pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `tipo_comision` enum('primer_pago_plan','primer_pago_addon','recurrente_plan','recurrente_addon') NOT NULL,
  `origen_producto` enum('plan','addon') NOT NULL,
  `plan_id` bigint(20) UNSIGNED DEFAULT NULL,
  `modulo_id` bigint(20) UNSIGNED DEFAULT NULL,
  `monto_base` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `monto_total_pago` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `monto_impuesto_estimado` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `porcentaje` decimal(8,5) NOT NULL DEFAULT 0.00000,
  `nanacoins` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `estado` enum('aprobada','anulada','retirada','usada') NOT NULL DEFAULT 'aprobada',
  `descripcion` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `aprobado_en` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `nanaclub_configuraciones`
--

CREATE TABLE `nanaclub_configuraciones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `clave` varchar(120) NOT NULL,
  `valor` text DEFAULT NULL,
  `descripcion` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `nanaclub_miembros`
--

CREATE TABLE `nanaclub_miembros` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `tipo` enum('afiliado','admin_pos') NOT NULL DEFAULT 'afiliado',
  `codigo` varchar(80) NOT NULL,
  `estado` enum('pendiente','activo','suspendido','rechazado') NOT NULL DEFAULT 'activo',
  `saldo_nanacoins` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total_generado` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total_retirado` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `contribuyente` tinyint(1) NOT NULL DEFAULT 0,
  `cobro_metodo` enum('sinpe','transferencia') DEFAULT NULL,
  `cobro_titular` varchar(160) DEFAULT NULL,
  `cobro_tipo_identificacion` varchar(2) DEFAULT NULL,
  `cobro_identificacion` varchar(60) DEFAULT NULL,
  `cobro_correo` varchar(180) DEFAULT NULL,
  `cobro_sinpe` varchar(40) DEFAULT NULL,
  `cobro_banco` varchar(120) DEFAULT NULL,
  `cobro_iban` varchar(80) DEFAULT NULL,
  `cobro_provincia` varchar(1) DEFAULT NULL,
  `cobro_canton` varchar(2) DEFAULT NULL,
  `cobro_distrito` varchar(2) DEFAULT NULL,
  `cobro_otras_sennas` varchar(180) DEFAULT NULL,
  `factura_tipo_preferido` enum('auto','FE','FEC') NOT NULL DEFAULT 'auto',
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `nanaclub_referencias`
--

CREATE TABLE `nanaclub_referencias` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `miembro_id` bigint(20) UNSIGNED NOT NULL,
  `codigo` varchar(80) NOT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `primer_pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `estado` enum('capturado','activo','cancelado') NOT NULL DEFAULT 'capturado',
  `ip` varchar(60) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `activado_en` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `nanaclub_solicitudes_afiliados`
--

CREATE TABLE `nanaclub_solicitudes_afiliados` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(160) NOT NULL,
  `correo` varchar(180) NOT NULL,
  `telefono` varchar(40) NOT NULL,
  `identificacion` varchar(60) DEFAULT NULL,
  `canal` varchar(80) DEFAULT NULL,
  `experiencia` text DEFAULT NULL,
  `acepta_reglas` tinyint(1) NOT NULL DEFAULT 0,
  `estado` enum('pendiente','aprobada','rechazada') NOT NULL DEFAULT 'pendiente',
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `miembro_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nota_admin` text DEFAULT NULL,
  `ip` varchar(60) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `revisado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `revisado_en` datetime DEFAULT NULL,
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `nanaclub_solicitudes_pago`
--

CREATE TABLE `nanaclub_solicitudes_pago` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `miembro_id` bigint(20) UNSIGNED NOT NULL,
  `monto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `moneda` char(3) NOT NULL DEFAULT 'USD',
  `metodo` enum('sinpe','transferencia') NOT NULL,
  `titular` varchar(160) DEFAULT NULL,
  `identificacion` varchar(60) DEFAULT NULL,
  `detalle_pago` varchar(255) DEFAULT NULL,
  `documento_tipo` enum('FE','FEC') NOT NULL DEFAULT 'FEC',
  `documento_estado` enum('pendiente','registrado','emitido','anulado') NOT NULL DEFAULT 'pendiente',
  `fe_documento_id` int(11) DEFAULT NULL,
  `clave` varchar(60) DEFAULT NULL,
  `consecutivo` varchar(30) DEFAULT NULL,
  `fe_payload_json` longtext DEFAULT NULL,
  `respuesta_mh` mediumtext DEFAULT NULL,
  `error_mensaje` varchar(255) DEFAULT NULL,
  `estado` enum('pendiente','aprobada','rechazada','pagada') NOT NULL DEFAULT 'pendiente',
  `comprobante_path` varchar(255) DEFAULT NULL,
  `comprobante_referencia` varchar(120) DEFAULT NULL,
  `comprobante_detalle` text DEFAULT NULL,
  `comprobante_fecha` datetime DEFAULT NULL,
  `comprobante_subido_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nota_admin` text DEFAULT NULL,
  `solicitado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `revisado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `revisado_en` datetime DEFAULT NULL,
  `pagado_en` datetime DEFAULT NULL,
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `nanaclub_wallet_movimientos`
--

CREATE TABLE `nanaclub_wallet_movimientos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `miembro_id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `comision_id` bigint(20) UNSIGNED DEFAULT NULL,
  `solicitud_pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `tipo` enum('credito','debito','canje_plan','canje_addon','retiro','ajuste') NOT NULL,
  `monto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `saldo_anterior` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `saldo_nuevo` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `descripcion` varchar(255) DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocios`
--

CREATE TABLE `negocios` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `plan_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuarios_adicionales` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `nombre_comercial` varchar(160) NOT NULL,
  `slug` varchar(180) NOT NULL,
  `correo` varchar(180) DEFAULT NULL,
  `telefono` varchar(40) DEFAULT NULL,
  `logo` varchar(255) DEFAULT NULL,
  `moneda` char(3) NOT NULL DEFAULT 'CRC',
  `color_principal` varchar(20) DEFAULT NULL,
  `estado` enum('activo','pendiente','suspendido','cancelado') NOT NULL DEFAULT 'pendiente',
  `vence_el` date DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocio_facturacion_responsable`
--

CREATE TABLE `negocio_facturacion_responsable` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(160) NOT NULL,
  `nombre_comercial` varchar(120) DEFAULT NULL,
  `tipo_identificacion` varchar(2) NOT NULL,
  `identificacion` varchar(60) NOT NULL,
  `codigo_actividad` varchar(20) DEFAULT NULL,
  `correo` varchar(180) NOT NULL,
  `telefono` varchar(40) DEFAULT NULL,
  `provincia` varchar(1) NOT NULL,
  `canton` varchar(2) NOT NULL,
  `distrito` varchar(2) DEFAULT NULL,
  `otras_sennas` varchar(180) NOT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocio_fe_configuracion`
--

CREATE TABLE `negocio_fe_configuracion` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_operativa_id` bigint(20) UNSIGNED DEFAULT NULL,
  `modulo_id` bigint(20) UNSIGNED DEFAULT NULL,
  `api_base_url` varchar(255) DEFAULT NULL,
  `ambiente` enum('sandbox','production') NOT NULL DEFAULT 'sandbox',
  `fe_cliente_id` int(11) DEFAULT NULL,
  `fe_sucursal_id` int(11) DEFAULT NULL,
  `fe_terminal_id` int(10) UNSIGNED DEFAULT NULL,
  `fe_api_key_id` int(11) DEFAULT NULL,
  `mh_credencial_id` bigint(20) UNSIGNED DEFAULT NULL,
  `api_key` varchar(150) DEFAULT NULL,
  `cod_sucursal` varchar(3) DEFAULT '001',
  `cod_terminal` varchar(5) DEFAULT '00001',
  `codigo_actividad` varchar(20) DEFAULT NULL,
  `regimen_tipo` enum('simplificado','tradicional','general') NOT NULL DEFAULT 'general',
  `tipo_identificacion_emisor` varchar(2) DEFAULT NULL,
  `identificacion_emisor` varchar(20) DEFAULT NULL,
  `nombre_emisor` varchar(100) DEFAULT NULL,
  `nombre_comercial_emisor` varchar(80) DEFAULT NULL,
  `telefono_emisor` varchar(20) DEFAULT NULL,
  `correo_emisor` varchar(100) DEFAULT NULL,
  `provincia` varchar(1) DEFAULT NULL,
  `canton` varchar(2) DEFAULT NULL,
  `distrito` varchar(2) DEFAULT NULL,
  `otras_sennas` varchar(160) DEFAULT NULL,
  `cert_path` varchar(255) DEFAULT NULL,
  `cert_vigente_hasta` datetime DEFAULT NULL,
  `cert_estado` varchar(30) DEFAULT NULL,
  `cert_detalle` varchar(255) DEFAULT NULL,
  `condicion_venta_default` varchar(2) NOT NULL DEFAULT '01',
  `tipo_documento_default` varchar(2) NOT NULL DEFAULT '04',
  `emitir_al_vender` tinyint(1) NOT NULL DEFAULT 0,
  `enviar_correo` tinyint(1) NOT NULL DEFAULT 0,
  `estado` enum('pendiente','activa','incompleta','suspendida','revocada') NOT NULL DEFAULT 'pendiente',
  `ultimo_test_estado` varchar(40) DEFAULT NULL,
  `ultimo_test_mensaje` varchar(255) DEFAULT NULL,
  `ultimo_test_en` datetime DEFAULT NULL,
  `activado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocio_fe_paquetes`
--

CREATE TABLE `negocio_fe_paquetes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `modulo_id` bigint(20) UNSIGNED NOT NULL,
  `documentos_mes` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `documentos_usados` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `terminales_autorizadas` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `precio_por_documento` decimal(12,4) NOT NULL DEFAULT 0.0000,
  `monto_mensual` decimal(12,2) NOT NULL DEFAULT 0.00,
  `moneda` char(3) NOT NULL DEFAULT 'USD',
  `periodo_inicio` date DEFAULT NULL,
  `periodo_fin` date DEFAULT NULL,
  `estado` enum('pendiente','aprobado','activo','agotado','suspendido','cancelado') NOT NULL DEFAULT 'pendiente',
  `aprobado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `aprobado_el` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocio_fe_recargas`
--

CREATE TABLE `negocio_fe_recargas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `paquete_id` bigint(20) UNSIGNED DEFAULT NULL,
  `pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `documentos` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `precio_por_documento` decimal(12,4) NOT NULL DEFAULT 0.0000,
  `monto` decimal(12,2) NOT NULL DEFAULT 0.00,
  `moneda` char(3) NOT NULL DEFAULT 'USD',
  `estado` enum('pendiente','aprobada','rechazada','cancelada') NOT NULL DEFAULT 'pendiente',
  `aprobado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `aprobado_el` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocio_modulos`
--

CREATE TABLE `negocio_modulos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `modulo_id` bigint(20) UNSIGNED NOT NULL,
  `origen` enum('plan','premium','cortesia') NOT NULL DEFAULT 'premium',
  `estado` enum('activo','pendiente','suspendido','cancelado') NOT NULL DEFAULT 'pendiente',
  `precio_mensual` decimal(12,2) NOT NULL DEFAULT 0.00,
  `precio_unico` decimal(12,2) NOT NULL DEFAULT 0.00,
  `activado_el` datetime DEFAULT NULL,
  `vence_el` date DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocio_sucursales`
--

CREATE TABLE `negocio_sucursales` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `codigo` varchar(3) NOT NULL,
  `nombre` varchar(160) NOT NULL,
  `tipo` enum('principal','sucursal','bodega') NOT NULL DEFAULT 'sucursal',
  `administrador_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `telefono` varchar(30) DEFAULT NULL,
  `correo` varchar(160) DEFAULT NULL,
  `provincia` varchar(1) DEFAULT NULL,
  `canton` varchar(2) DEFAULT NULL,
  `distrito` varchar(2) DEFAULT NULL,
  `barrio` varchar(80) DEFAULT NULL,
  `direccion` varchar(255) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `incluida_plan` tinyint(1) NOT NULL DEFAULT 1,
  `es_principal` tinyint(1) NOT NULL DEFAULT 0,
  `hereda_modulos_principal` tinyint(1) NOT NULL DEFAULT 0,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocio_sucursales_config`
--

CREATE TABLE `negocio_sucursales_config` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `limite_incluido` int(10) UNSIGNED NOT NULL DEFAULT 2,
  `precio_adicional_mensual` decimal(14,2) NOT NULL DEFAULT 0.00,
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocio_sucursal_cajas`
--

CREATE TABLE `negocio_sucursal_cajas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED NOT NULL,
  `codigo_caja` varchar(20) NOT NULL,
  `nombre` varchar(80) NOT NULL,
  `fe_sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `fe_terminal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `fe_api_key_id` bigint(20) UNSIGNED DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `actualizado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocio_sucursal_usuarios`
--

CREATE TABLE `negocio_sucursal_usuarios` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED NOT NULL,
  `rol_sucursal` enum('administrador','responsable','operador') NOT NULL DEFAULT 'operador',
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `negocio_usuarios`
--

CREATE TABLE `negocio_usuarios` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED NOT NULL,
  `rol_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `pagos_suscripciones`
--

CREATE TABLE `pagos_suscripciones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `destino_sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `terminales_fe` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `miembros_equipo` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `responsable_facturacion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `factura_servicio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `tipo` enum('plan','addon','recarga_documentos','manual') NOT NULL,
  `plan_id` bigint(20) UNSIGNED DEFAULT NULL,
  `modulo_id` bigint(20) UNSIGNED DEFAULT NULL,
  `documentos` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `habitaciones` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `sucursales` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `addon_operacion` varchar(40) DEFAULT NULL,
  `periodo` enum('mensual','anual','unico') NOT NULL DEFAULT 'mensual',
  `cantidad_periodos` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `monto` decimal(12,2) NOT NULL DEFAULT 0.00,
  `monto_base` decimal(12,2) NOT NULL DEFAULT 0.00,
  `monto_impuesto` decimal(12,2) NOT NULL DEFAULT 0.00,
  `monto_mensual_recurrente` decimal(12,2) NOT NULL DEFAULT 0.00,
  `moneda` char(3) NOT NULL DEFAULT 'USD',
  `tipo_cambio` decimal(14,6) NOT NULL DEFAULT 1.000000,
  `metodo_pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `metodo_pago_nombre` varchar(120) DEFAULT NULL,
  `referencia_pago` varchar(160) DEFAULT NULL,
  `comprobante_path` varchar(255) DEFAULT NULL,
  `nota_cliente` text DEFAULT NULL,
  `addons_renovacion_json` longtext DEFAULT NULL,
  `nota_admin` text DEFAULT NULL,
  `estado` enum('pendiente','aprobado','rechazado','cancelado') NOT NULL DEFAULT 'pendiente',
  `aprobado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `aprobado_el` datetime DEFAULT NULL,
  `rechazado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `rechazado_el` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp(),
  `nanaclub_codigo` varchar(80) DEFAULT NULL,
  `nanaclub_miembro_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nanacoins_usados` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `nanaclub_movimiento_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nanaclub_cupon_codigo` varchar(80) DEFAULT NULL,
  `nanaclub_descuento_porcentaje` decimal(8,5) NOT NULL DEFAULT 0.00000,
  `nanaclub_descuento_monto` decimal(14,5) NOT NULL DEFAULT 0.00000
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `password_resets`
--

CREATE TABLE `password_resets` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED NOT NULL,
  `correo` varchar(180) NOT NULL,
  `codigo_hash` varchar(255) NOT NULL,
  `token` varchar(120) NOT NULL,
  `expira_en` datetime NOT NULL,
  `usado_en` datetime DEFAULT NULL,
  `intentos` tinyint(3) UNSIGNED NOT NULL DEFAULT 0,
  `ip` varchar(60) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `permisos`
--

CREATE TABLE `permisos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(120) NOT NULL,
  `clave` varchar(120) NOT NULL,
  `grupo` varchar(80) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `planes`
--

CREATE TABLE `planes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(120) NOT NULL,
  `descripcion` varchar(255) DEFAULT NULL,
  `facturacion_cabys` varchar(20) DEFAULT NULL,
  `facturacion_detalle` varchar(255) DEFAULT NULL,
  `facturacion_tarifa_iva` decimal(8,5) NOT NULL DEFAULT 13.00000,
  `precio_mensual` decimal(12,2) NOT NULL DEFAULT 0.00,
  `precio_anual` decimal(12,2) NOT NULL DEFAULT 0.00,
  `max_usuarios` int(11) NOT NULL DEFAULT 1,
  `precio_usuario_adicional` decimal(12,2) NOT NULL DEFAULT 0.00,
  `max_productos` int(11) NOT NULL DEFAULT 100,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `nanaclub_comision_primer_mes` decimal(8,5) DEFAULT NULL,
  `nanaclub_comision_renovacion` decimal(8,5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `plan_checkout_rate_limits`
--

CREATE TABLE `plan_checkout_rate_limits` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `ip_hash` char(64) NOT NULL,
  `accion` varchar(40) NOT NULL,
  `window_start` datetime NOT NULL,
  `attempts` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `blocked_until` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `plan_modulos`
--

CREATE TABLE `plan_modulos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `plan_id` bigint(20) UNSIGNED NOT NULL,
  `modulo_id` bigint(20) UNSIGNED NOT NULL,
  `incluido` tinyint(1) NOT NULL DEFAULT 1,
  `documentos_mes` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `precio_mensual` decimal(12,2) NOT NULL DEFAULT 0.00,
  `precio_unico` decimal(12,2) NOT NULL DEFAULT 0.00,
  `precio_por_documento` decimal(12,4) NOT NULL DEFAULT 0.0000,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `plan_solicitudes`
--

CREATE TABLE `plan_solicitudes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `responsable_facturacion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_acceso_id` bigint(20) UNSIGNED DEFAULT NULL,
  `checkout_token` varchar(96) DEFAULT NULL,
  `origen` varchar(40) NOT NULL DEFAULT 'checkout_publico',
  `tipo_identificacion` varchar(2) DEFAULT NULL,
  `identificacion` varchar(60) DEFAULT NULL,
  `cliente_nombre` varchar(160) DEFAULT NULL,
  `cliente_nombre_comercial` varchar(120) DEFAULT NULL,
  `cliente_codigo_actividad` varchar(20) DEFAULT NULL,
  `regimen_tipo` enum('simplificado','tradicional','general') NOT NULL DEFAULT 'general',
  `cliente_correo` varchar(180) DEFAULT NULL,
  `cliente_telefono` varchar(40) DEFAULT NULL,
  `cliente_provincia` varchar(1) DEFAULT NULL,
  `cliente_canton` varchar(2) DEFAULT NULL,
  `cliente_distrito` varchar(2) DEFAULT NULL,
  `cliente_otras_sennas` varchar(180) DEFAULT NULL,
  `factura_preferencia` enum('registrado','otro') NOT NULL DEFAULT 'registrado',
  `factura_nombre` varchar(160) DEFAULT NULL,
  `factura_nombre_comercial` varchar(120) DEFAULT NULL,
  `factura_tipo_identificacion` varchar(2) DEFAULT NULL,
  `factura_identificacion` varchar(60) DEFAULT NULL,
  `factura_codigo_actividad` varchar(20) DEFAULT NULL,
  `factura_correo` varchar(180) DEFAULT NULL,
  `factura_telefono` varchar(40) DEFAULT NULL,
  `factura_provincia` varchar(1) DEFAULT NULL,
  `factura_canton` varchar(2) DEFAULT NULL,
  `factura_distrito` varchar(2) DEFAULT NULL,
  `factura_otras_sennas` varchar(180) DEFAULT NULL,
  `factura_actualizado_en` datetime DEFAULT NULL,
  `plan_id_actual` bigint(20) UNSIGNED DEFAULT NULL,
  `plan_id_solicitado` bigint(20) UNSIGNED NOT NULL,
  `periodo` enum('mensual','anual') NOT NULL DEFAULT 'mensual',
  `cantidad_periodos` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `monto_solicitado` decimal(12,2) NOT NULL DEFAULT 0.00,
  `monto_base` decimal(12,2) NOT NULL DEFAULT 0.00,
  `monto_impuesto` decimal(12,2) NOT NULL DEFAULT 0.00,
  `monto_usd` decimal(12,2) NOT NULL DEFAULT 0.00,
  `monto_crc` decimal(12,2) NOT NULL DEFAULT 0.00,
  `tipo_cambio` decimal(14,6) NOT NULL DEFAULT 1.000000,
  `monto_aprobado` decimal(12,2) DEFAULT NULL,
  `moneda` char(3) NOT NULL DEFAULT 'USD',
  `metodo_pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `metodo_pago` varchar(100) DEFAULT NULL,
  `referencia_pago` varchar(160) DEFAULT NULL,
  `comprobante_path` varchar(255) DEFAULT NULL,
  `nota_cliente` text DEFAULT NULL,
  `seguridad_score` smallint(5) UNSIGNED NOT NULL DEFAULT 0,
  `nota_admin` text DEFAULT NULL,
  `estado` enum('pendiente','aprobada','rechazada','cancelada') NOT NULL DEFAULT 'pendiente',
  `aprobado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `aprobado_el` datetime DEFAULT NULL,
  `rechazado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `rechazado_el` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp(),
  `nanaclub_codigo` varchar(80) DEFAULT NULL,
  `nanaclub_miembro_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nanaclub_cupon_codigo` varchar(80) DEFAULT NULL,
  `nanaclub_descuento_porcentaje` decimal(8,5) NOT NULL DEFAULT 0.00000,
  `nanaclub_descuento_monto` decimal(14,5) NOT NULL DEFAULT 0.00000
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `plataforma_facturas_servicios`
--

CREATE TABLE `plataforma_facturas_servicios` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `responsable_facturacion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `origen` enum('automatico','manual') NOT NULL DEFAULT 'automatico',
  `tipo` enum('plan','addon','recarga_documentos','terminales_fe','ampliacion_equipo','manual') NOT NULL DEFAULT 'manual',
  `tipo_documento` varchar(2) NOT NULL DEFAULT '01',
  `plan_id` bigint(20) UNSIGNED DEFAULT NULL,
  `modulo_id` bigint(20) UNSIGNED DEFAULT NULL,
  `moneda` char(3) NOT NULL DEFAULT 'USD',
  `subtotal` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `impuesto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `cliente_nombre` varchar(160) NOT NULL,
  `cliente_nombre_comercial` varchar(120) DEFAULT NULL,
  `cliente_tipo_identificacion` varchar(2) NOT NULL,
  `cliente_identificacion` varchar(60) NOT NULL,
  `cliente_codigo_actividad` varchar(20) DEFAULT NULL,
  `cliente_correo` varchar(180) NOT NULL,
  `cliente_telefono` varchar(40) DEFAULT NULL,
  `cliente_provincia` varchar(1) NOT NULL,
  `cliente_canton` varchar(2) NOT NULL,
  `cliente_distrito` varchar(2) DEFAULT NULL,
  `cliente_otras_sennas` varchar(180) NOT NULL,
  `estado` enum('pendiente','emitida','enviada','aceptada','rechazada','anulada','error') NOT NULL DEFAULT 'pendiente',
  `clave` varchar(60) DEFAULT NULL,
  `consecutivo` varchar(30) DEFAULT NULL,
  `fe_documento_id` int(11) DEFAULT NULL,
  `fe_payload_json` longtext DEFAULT NULL,
  `documento_referencia_id` bigint(20) UNSIGNED DEFAULT NULL,
  `referencia_clave` varchar(60) DEFAULT NULL,
  `referencia_consecutivo` varchar(30) DEFAULT NULL,
  `referencia_codigo` varchar(2) DEFAULT NULL,
  `referencia_razon` varchar(180) DEFAULT NULL,
  `pdf_path` varchar(255) DEFAULT NULL,
  `xml_firmado_path` varchar(255) DEFAULT NULL,
  `xml_respuesta_path` varchar(255) DEFAULT NULL,
  `respuesta_mh` mediumtext DEFAULT NULL,
  `error_mensaje` varchar(255) DEFAULT NULL,
  `nota_admin` text DEFAULT NULL,
  `facturada_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `facturada_en` datetime DEFAULT NULL,
  `correo_enviado_en` datetime DEFAULT NULL,
  `correo_error` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `plataforma_factura_servicio_lineas`
--

CREATE TABLE `plataforma_factura_servicio_lineas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `factura_id` bigint(20) UNSIGNED NOT NULL,
  `tipo_linea` enum('plan','addon','recarga_documentos','terminales_fe','ampliacion_equipo','manual','ajuste') NOT NULL DEFAULT 'manual',
  `plan_id` bigint(20) UNSIGNED DEFAULT NULL,
  `modulo_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cabys_codigo` varchar(20) DEFAULT NULL,
  `detalle` varchar(255) NOT NULL,
  `cantidad` decimal(14,3) NOT NULL DEFAULT 1.000,
  `precio_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `tarifa_iva` decimal(8,5) NOT NULL DEFAULT 13.00000,
  `subtotal` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `impuesto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `plataforma_fe_configuracion`
--

CREATE TABLE `plataforma_fe_configuracion` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `fe_cliente_id` int(11) DEFAULT NULL,
  `fe_sucursal_id` int(11) DEFAULT NULL,
  `fe_terminal_id` int(10) UNSIGNED DEFAULT NULL,
  `fe_api_key_id` int(11) DEFAULT NULL,
  `mh_credencial_id` bigint(20) UNSIGNED DEFAULT NULL,
  `ambiente` enum('sandbox','production') NOT NULL DEFAULT 'sandbox',
  `codigo_actividad` varchar(20) DEFAULT NULL,
  `cod_sucursal` varchar(3) NOT NULL DEFAULT '001',
  `cod_terminal` varchar(5) NOT NULL DEFAULT '00001',
  `estado` enum('incompleta','activa','suspendida') NOT NULL DEFAULT 'incompleta',
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `plataforma_metodos_pago`
--

CREATE TABLE `plataforma_metodos_pago` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(120) NOT NULL,
  `tipo` enum('sinpe','transferencia','link','otro') NOT NULL DEFAULT 'otro',
  `moneda` char(3) NOT NULL DEFAULT 'USD',
  `detalle` text DEFAULT NULL,
  `link_pago` varchar(255) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `productos`
--

CREATE TABLE `productos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `producto_origen_id` bigint(20) UNSIGNED DEFAULT NULL,
  `categoria_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nombre` varchar(180) NOT NULL,
  `codigo` varchar(80) DEFAULT NULL,
  `codigo_barras` varchar(120) DEFAULT NULL,
  `codigo_cabys` varchar(13) DEFAULT NULL,
  `unidad_medida` varchar(15) NOT NULL DEFAULT 'Unid',
  `unidad_medida_comercial` varchar(20) DEFAULT NULL,
  `descripcion` text DEFAULT NULL,
  `precio_venta` decimal(12,2) NOT NULL DEFAULT 0.00,
  `costo` decimal(12,2) NOT NULL DEFAULT 0.00,
  `codigo_impuesto` varchar(2) NOT NULL DEFAULT '01',
  `codigo_tarifa_iva` varchar(2) NOT NULL DEFAULT '08',
  `tarifa_iva` decimal(5,2) NOT NULL DEFAULT 13.00,
  `tipo_linea_fiscal` enum('mercancia','servicio') NOT NULL DEFAULT 'mercancia',
  `fe_tipo_transaccion` varchar(2) DEFAULT NULL,
  `fe_exoneracion_activa` tinyint(1) NOT NULL DEFAULT 0,
  `fe_exoneracion_numero` varchar(60) DEFAULT NULL,
  `fe_exoneracion_tipo_documento` varchar(4) DEFAULT NULL,
  `fe_exoneracion_codigo_institucion` varchar(20) DEFAULT NULL,
  `fe_exoneracion_nombre_institucion` varchar(180) DEFAULT NULL,
  `fe_exoneracion_nombre_institucion_otros` varchar(160) DEFAULT NULL,
  `fe_exoneracion_fecha_emision` date DEFAULT NULL,
  `fe_exoneracion_fecha_vencimiento` date DEFAULT NULL,
  `fe_exoneracion_tarifa` decimal(5,2) DEFAULT NULL,
  `fe_exoneracion_articulo` varchar(10) DEFAULT NULL,
  `fe_exoneracion_inciso` varchar(10) DEFAULT NULL,
  `fe_exoneracion_cabys_json` mediumtext DEFAULT NULL,
  `fe_exoneracion_respuesta_json` mediumtext DEFAULT NULL,
  `fe_exoneracion_consultado_en` datetime DEFAULT NULL,
  `fe_sector_agropecuario` tinyint(1) NOT NULL DEFAULT 0,
  `fe_agro_tipo` enum('agropecuario','pesca') DEFAULT NULL,
  `fe_agro_identificacion` varchar(20) DEFAULT NULL,
  `fe_agro_respuesta_json` mediumtext DEFAULT NULL,
  `fe_agro_consultado_en` datetime DEFAULT NULL,
  `stock` decimal(12,3) NOT NULL DEFAULT 0.000,
  `inv_stock_minimo` decimal(12,3) NOT NULL DEFAULT 0.000,
  `inv_stock_maximo` decimal(12,3) NOT NULL DEFAULT 0.000,
  `inv_unidad_compra` varchar(30) DEFAULT NULL,
  `inv_unidad_venta` varchar(30) DEFAULT NULL,
  `inv_factor_conversion` decimal(14,6) NOT NULL DEFAULT 1.000000,
  `inv_ubicacion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `inv_maneja_lotes` tinyint(1) NOT NULL DEFAULT 0,
  `inv_maneja_vencimiento` tinyint(1) NOT NULL DEFAULT 0,
  `inv_garantia_dias` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `inv_boleta_farmaceutica` tinyint(1) NOT NULL DEFAULT 0,
  `imagen` varchar(255) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `recepcion_avanzada_correos`
--

CREATE TABLE `recepcion_avanzada_correos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `email` varchar(180) NOT NULL,
  `local_part` varchar(120) NOT NULL,
  `domain` varchar(120) NOT NULL DEFAULT 'nanapos.com',
  `password_enc` text DEFAULT NULL,
  `forward_email` varchar(180) DEFAULT NULL,
  `forward_enabled` tinyint(1) NOT NULL DEFAULT 0,
  `estado` varchar(40) NOT NULL DEFAULT 'pendiente',
  `ultimo_error` varchar(500) DEFAULT NULL,
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado_en` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `roles`
--

CREATE TABLE `roles` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nombre` varchar(80) NOT NULL,
  `clave` varchar(80) NOT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `rol_permisos`
--

CREATE TABLE `rol_permisos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `rol_id` bigint(20) UNSIGNED NOT NULL,
  `permiso_id` bigint(20) UNSIGNED NOT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `salon_citas`
--

CREATE TABLE `salon_citas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `servicio_id` bigint(20) UNSIGNED NOT NULL,
  `profesional_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cliente_id` bigint(20) UNSIGNED DEFAULT NULL,
  `venta_id` bigint(20) UNSIGNED DEFAULT NULL,
  `codigo` varchar(40) NOT NULL,
  `cliente_nombre` varchar(180) NOT NULL,
  `tipo_identificacion` varchar(2) DEFAULT NULL,
  `identificacion` varchar(60) DEFAULT NULL,
  `correo` varchar(180) DEFAULT NULL,
  `telefono` varchar(40) DEFAULT NULL,
  `fecha_inicio` datetime NOT NULL,
  `fecha_fin` datetime NOT NULL,
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `mano_obra` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `productos_total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `requiere_fe` tinyint(1) NOT NULL DEFAULT 0,
  `tipo_documento_fiscal` varchar(2) NOT NULL DEFAULT '04',
  `estado` enum('programada','confirmada','atendida','cancelada','no_asistio','facturada') NOT NULL DEFAULT 'programada',
  `observaciones` text DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `salon_cita_productos`
--

CREATE TABLE `salon_cita_productos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `cita_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `nombre_producto` varchar(180) NOT NULL,
  `cantidad` decimal(14,3) NOT NULL DEFAULT 1.000,
  `precio_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total_linea` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `origen` enum('servicio','extra') NOT NULL DEFAULT 'servicio',
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `salon_profesionales`
--

CREATE TABLE `salon_profesionales` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(160) NOT NULL,
  `especialidad` varchar(160) DEFAULT NULL,
  `telefono` varchar(40) DEFAULT NULL,
  `correo` varchar(180) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `salon_servicios`
--

CREATE TABLE `salon_servicios` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(180) NOT NULL,
  `categoria` varchar(120) DEFAULT NULL,
  `duracion_minutos` int(10) UNSIGNED NOT NULL DEFAULT 60,
  `precio_mano_obra` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `producto_servicio_id` bigint(20) UNSIGNED DEFAULT NULL,
  `descripcion` text DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `salon_servicio_productos`
--

CREATE TABLE `salon_servicio_productos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `servicio_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED NOT NULL,
  `cantidad` decimal(14,3) NOT NULL DEFAULT 1.000,
  `nota` varchar(180) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `seo_blog_posts`
--

CREATE TABLE `seo_blog_posts` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `titulo` varchar(190) NOT NULL,
  `slug` varchar(190) NOT NULL,
  `resumen` varchar(320) DEFAULT NULL,
  `descripcion_principal` text DEFAULT NULL,
  `descripcion_detallada` longtext DEFAULT NULL,
  `categoria` varchar(120) DEFAULT NULL,
  `contenido_html` longtext DEFAULT NULL,
  `meta_titulo` varchar(190) DEFAULT NULL,
  `meta_descripcion` varchar(320) DEFAULT NULL,
  `meta_keywords` text DEFAULT NULL,
  `imagen` varchar(255) DEFAULT NULL,
  `imagen_alt` varchar(190) DEFAULT NULL,
  `imagen_title` varchar(190) DEFAULT NULL,
  `tags` varchar(255) DEFAULT NULL,
  `autor` varchar(120) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `publicado_en` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `seo_paginas`
--

CREATE TABLE `seo_paginas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `tipo` varchar(40) NOT NULL DEFAULT 'principal',
  `url_path` varchar(190) NOT NULL,
  `titulo` varchar(180) NOT NULL,
  `meta_titulo` varchar(190) DEFAULT NULL,
  `meta_descripcion` varchar(320) DEFAULT NULL,
  `meta_keywords` text DEFAULT NULL,
  `h1` varchar(220) DEFAULT NULL,
  `intro` text DEFAULT NULL,
  `contenido_html` longtext DEFAULT NULL,
  `og_image` varchar(255) DEFAULT NULL,
  `canonical_url` varchar(255) DEFAULT NULL,
  `robots` varchar(80) NOT NULL DEFAULT 'index,follow',
  `sitemap_priority` decimal(3,2) NOT NULL DEFAULT 0.70,
  `sitemap_changefreq` varchar(20) NOT NULL DEFAULT 'weekly',
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `sessions`
--

CREATE TABLE `sessions` (
  `id` varchar(255) NOT NULL,
  `user_id` bigint(20) UNSIGNED DEFAULT NULL,
  `ip_address` varchar(45) DEFAULT NULL,
  `user_agent` text DEFAULT NULL,
  `payload` longtext NOT NULL,
  `last_activity` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `sistema_actualizaciones`
--

CREATE TABLE `sistema_actualizaciones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `version` varchar(30) NOT NULL,
  `titulo` varchar(180) NOT NULL,
  `tipo` enum('mejora','nuevo_addon','correccion','seguridad','general') NOT NULL DEFAULT 'general',
  `fecha` date NOT NULL,
  `descripcion` text DEFAULT NULL,
  `visible_admin_pos` tinyint(1) NOT NULL DEFAULT 1,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `sistema_configuraciones`
--

CREATE TABLE `sistema_configuraciones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `clave` varchar(120) NOT NULL,
  `valor` text DEFAULT NULL,
  `descripcion` varchar(255) DEFAULT NULL,
  `actualizado` datetime DEFAULT NULL ON UPDATE current_timestamp(),
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `taller_configuracion`
--

CREATE TABLE `taller_configuracion` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `tipos_dispositivo` set('celular','computo','electrodomestico','otro') NOT NULL DEFAULT 'celular,computo,electrodomestico',
  `garantia_default_dias` int(11) NOT NULL DEFAULT 30,
  `impuesto_default` decimal(8,5) NOT NULL DEFAULT 13.00000,
  `producto_mano_obra_id` bigint(20) UNSIGNED DEFAULT NULL,
  `producto_diagnostico_id` bigint(20) UNSIGNED DEFAULT NULL,
  `mensaje_recepcion` varchar(255) DEFAULT 'Equipo recibido para revisión.',
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `taller_mapas`
--

CREATE TABLE `taller_mapas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `clave` varchar(60) NOT NULL,
  `dispositivo_tipo` enum('celular','computo','electrodomestico','otro') NOT NULL DEFAULT 'otro',
  `titulo` varchar(120) NOT NULL,
  `archivo_path` varchar(255) NOT NULL,
  `es_default` tinyint(1) NOT NULL DEFAULT 0,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `taller_ordenes`
--

CREATE TABLE `taller_ordenes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `tecnico_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cliente_id` bigint(20) UNSIGNED DEFAULT NULL,
  `servicio_producto_id` bigint(20) UNSIGNED DEFAULT NULL,
  `servicio_nombre` varchar(180) DEFAULT NULL,
  `servicio_precio` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `servicio_tarifa_iva` decimal(8,5) NOT NULL DEFAULT 0.00000,
  `venta_id` bigint(20) UNSIGNED DEFAULT NULL,
  `codigo` varchar(40) NOT NULL,
  `estado` enum('recibido','diagnostico','cotizado','aprobado','en_reparacion','esperando_repuesto','listo','entregado','cancelado','sin_reparacion') NOT NULL DEFAULT 'recibido',
  `prioridad` enum('normal','alta','urgente') NOT NULL DEFAULT 'normal',
  `cliente_nombre` varchar(180) NOT NULL,
  `cliente_tipo_identificacion` varchar(2) DEFAULT NULL,
  `cliente_identificacion` varchar(60) DEFAULT NULL,
  `cliente_codigo_actividad` varchar(20) DEFAULT NULL,
  `cliente_telefono` varchar(40) DEFAULT NULL,
  `cliente_whatsapp` varchar(40) DEFAULT NULL,
  `cliente_correo` varchar(180) DEFAULT NULL,
  `dispositivo_tipo` enum('celular','computo','electrodomestico','otro') NOT NULL DEFAULT 'celular',
  `dispositivo_marca` varchar(120) DEFAULT NULL,
  `dispositivo_modelo` varchar(160) DEFAULT NULL,
  `dispositivo_serie` varchar(120) DEFAULT NULL,
  `dispositivo_imei` varchar(80) DEFAULT NULL,
  `dispositivo_color` varchar(80) DEFAULT NULL,
  `dispositivo_capacidad` varchar(80) DEFAULT NULL,
  `sistema_operativo` varchar(120) DEFAULT NULL,
  `accesorios_recibidos` text DEFAULT NULL,
  `clave_acceso` varchar(160) DEFAULT NULL,
  `patron_acceso` varchar(160) DEFAULT NULL,
  `desbloqueo_autorizado` tinyint(1) NOT NULL DEFAULT 0,
  `respaldo_autorizado` tinyint(1) NOT NULL DEFAULT 0,
  `borrar_datos_autorizado` tinyint(1) NOT NULL DEFAULT 0,
  `condicion_recepcion` text DEFAULT NULL,
  `fallas_reportadas` text DEFAULT NULL,
  `mapa_json` longtext DEFAULT NULL,
  `mapa_tipo` varchar(60) DEFAULT NULL,
  `diagnostico` text DEFAULT NULL,
  `solucion` text DEFAULT NULL,
  `garantia_dias` int(11) NOT NULL DEFAULT 0,
  `garantia_detalle` text DEFAULT NULL,
  `diagnostico_costo` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `mano_obra` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `repuestos_total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `subtotal` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `impuesto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `requiere_fe` tinyint(1) NOT NULL DEFAULT 0,
  `tipo_documento_fiscal` varchar(2) DEFAULT NULL,
  `fecha_estimada` datetime DEFAULT NULL,
  `recibido_en` datetime NOT NULL DEFAULT current_timestamp(),
  `entregado_en` datetime DEFAULT NULL,
  `facturada_en` datetime DEFAULT NULL,
  `observaciones` text DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `taller_orden_adjuntos`
--

CREATE TABLE `taller_orden_adjuntos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `orden_id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `tipo` enum('recepcion','diagnostico','entrega','otro') NOT NULL DEFAULT 'recepcion',
  `archivo_path` varchar(255) NOT NULL,
  `mime` varchar(120) DEFAULT NULL,
  `nombre_original` varchar(180) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `taller_orden_eventos`
--

CREATE TABLE `taller_orden_eventos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `orden_id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `estado` varchar(40) DEFAULT NULL,
  `detalle` text DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `taller_orden_items`
--

CREATE TABLE `taller_orden_items` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `orden_id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `producto_id` bigint(20) UNSIGNED DEFAULT NULL,
  `tipo` enum('repuesto','accesorio','servicio','otro') NOT NULL DEFAULT 'repuesto',
  `descripcion` varchar(220) NOT NULL,
  `cantidad` decimal(14,3) NOT NULL DEFAULT 1.000,
  `precio_unitario` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `tarifa_iva` decimal(8,5) NOT NULL DEFAULT 0.00000,
  `impuesto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total_linea` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `estado` enum('pendiente','aprobado','usado','devuelto','anulado') NOT NULL DEFAULT 'pendiente',
  `nota` varchar(255) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `taller_tecnicos`
--

CREATE TABLE `taller_tecnicos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(160) NOT NULL,
  `especialidad` varchar(160) DEFAULT NULL,
  `telefono` varchar(40) DEFAULT NULL,
  `correo` varchar(180) DEFAULT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `usuarios`
--

CREATE TABLE `usuarios` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `nombre` varchar(150) NOT NULL,
  `correo` varchar(180) NOT NULL,
  `password` varchar(255) NOT NULL,
  `telefono` varchar(40) DEFAULT NULL,
  `foto` varchar(255) DEFAULT NULL,
  `estado` enum('activo','pendiente','suspendido','bloqueado') NOT NULL DEFAULT 'pendiente',
  `ultimo_acceso` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `ventas`
--

CREATE TABLE `ventas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `usuario_id` bigint(20) UNSIGNED NOT NULL,
  `cliente_id` bigint(20) UNSIGNED DEFAULT NULL,
  `numero` varchar(40) NOT NULL,
  `subtotal` decimal(12,2) NOT NULL DEFAULT 0.00,
  `descuento` decimal(12,2) NOT NULL DEFAULT 0.00,
  `impuesto` decimal(12,2) NOT NULL DEFAULT 0.00,
  `otros_cargos_total` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total` decimal(12,2) NOT NULL DEFAULT 0.00,
  `subtotal_crc` decimal(14,5) DEFAULT NULL,
  `descuento_crc` decimal(14,5) DEFAULT NULL,
  `impuesto_crc` decimal(14,5) DEFAULT NULL,
  `otros_cargos_total_crc` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `total_crc` decimal(14,5) DEFAULT NULL,
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `tipo_cambio` decimal(14,6) NOT NULL DEFAULT 1.000000,
  `condicion_venta` varchar(2) NOT NULL DEFAULT '01',
  `condicion_venta_detalle` varchar(100) DEFAULT NULL,
  `plazo_credito` int(5) UNSIGNED DEFAULT NULL,
  `descuento_tipo` enum('monto','porcentaje') NOT NULL DEFAULT 'monto',
  `descuento_valor` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `descuento_codigo` varchar(2) DEFAULT NULL,
  `descuento_naturaleza` varchar(80) DEFAULT NULL,
  `metodo_pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `pago_recibido` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `vuelto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `estado` enum('pagada','anulada','pendiente') NOT NULL DEFAULT 'pagada',
  `observaciones` text DEFAULT NULL,
  `caja_sesion_id` bigint(20) UNSIGNED DEFAULT NULL,
  `caja_config_id` bigint(20) UNSIGNED DEFAULT NULL,
  `fe_api_key_id` int(11) DEFAULT NULL,
  `fe_tipo_documento` varchar(2) DEFAULT NULL,
  `fe_estado` enum('no_emitida','pendiente','enviada','aceptada','rechazada','error','anulada') NOT NULL DEFAULT 'no_emitida',
  `fe_documento_id` int(11) DEFAULT NULL,
  `fe_clave` varchar(50) DEFAULT NULL,
  `fe_consecutivo` varchar(20) DEFAULT NULL,
  `fe_payload_json` longtext DEFAULT NULL,
  `fe_respuesta_json` longtext DEFAULT NULL,
  `fe_error` text DEFAULT NULL,
  `fe_intentos` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `fe_emitido_en` datetime DEFAULT NULL,
  `fe_actualizado_en` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `ventas_eliminaciones`
--

CREATE TABLE `ventas_eliminaciones` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `venta_id` bigint(20) UNSIGNED NOT NULL,
  `venta_numero` varchar(40) DEFAULT NULL,
  `tipo_documento` varchar(2) DEFAULT NULL,
  `fe_clave` varchar(60) DEFAULT NULL,
  `estado_mh` varchar(30) DEFAULT NULL,
  `motivo` text NOT NULL,
  `snapshot_json` longtext DEFAULT NULL,
  `eliminado_por_usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `correo_admin` varchar(180) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `venta_detalles`
--

CREATE TABLE `venta_detalles` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `venta_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `producto_id` bigint(20) UNSIGNED DEFAULT NULL,
  `nombre_producto` varchar(180) NOT NULL,
  `cantidad` decimal(12,3) NOT NULL DEFAULT 1.000,
  `precio_unitario` decimal(12,2) NOT NULL DEFAULT 0.00,
  `precio_unitario_crc` decimal(14,5) DEFAULT NULL,
  `descuento` decimal(12,2) NOT NULL DEFAULT 0.00,
  `descuento_codigo` varchar(2) DEFAULT NULL,
  `descuento_naturaleza` varchar(80) DEFAULT NULL,
  `impuesto` decimal(12,2) NOT NULL DEFAULT 0.00,
  `total_linea` decimal(12,2) NOT NULL DEFAULT 0.00,
  `fe_tipo_transaccion` varchar(2) DEFAULT NULL,
  `fe_exoneracion_json` mediumtext DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `venta_otros_cargos`
--

CREATE TABLE `venta_otros_cargos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `venta_id` bigint(20) UNSIGNED NOT NULL,
  `tipo_documento` varchar(2) NOT NULL,
  `tipo_documento_otro` varchar(100) DEFAULT NULL,
  `detalle` varchar(160) NOT NULL,
  `tercero_nombre` varchar(100) DEFAULT NULL,
  `tercero_tipo_identificacion` varchar(2) DEFAULT NULL,
  `tercero_identificacion` varchar(20) DEFAULT NULL,
  `porcentaje` decimal(8,5) DEFAULT NULL,
  `monto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `monto_crc` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `venta_pagos`
--

CREATE TABLE `venta_pagos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `venta_id` bigint(20) UNSIGNED NOT NULL,
  `sucursal_id` bigint(20) UNSIGNED DEFAULT NULL,
  `metodo_pago_id` bigint(20) UNSIGNED DEFAULT NULL,
  `metodo_pago_nombre` varchar(120) DEFAULT NULL,
  `codigo_mh` varchar(2) DEFAULT NULL,
  `moneda` varchar(3) NOT NULL DEFAULT 'CRC',
  `monto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `monto_crc` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `monto_recibido` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `vuelto` decimal(14,5) NOT NULL DEFAULT 0.00000,
  `referencia` varchar(120) DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `whatsapp_configuracion`
--

CREATE TABLE `whatsapp_configuracion` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `prefijo_pais` varchar(8) NOT NULL DEFAULT '506',
  `telefono_default` varchar(40) DEFAULT NULL,
  `incluir_enlace_publico` tinyint(1) NOT NULL DEFAULT 1,
  `dias_vigencia_enlace` int(11) NOT NULL DEFAULT 30,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `whatsapp_enlaces_publicos`
--

CREATE TABLE `whatsapp_enlaces_publicos` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `token` varchar(96) NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `tipo` varchar(40) NOT NULL,
  `referencia_id` bigint(20) UNSIGNED NOT NULL,
  `documento_id` bigint(20) UNSIGNED DEFAULT NULL,
  `formato` varchar(30) NOT NULL DEFAULT 'pdf',
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `vence_en` datetime DEFAULT NULL,
  `usos` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `ultimo_acceso` datetime DEFAULT NULL,
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `whatsapp_envios`
--

CREATE TABLE `whatsapp_envios` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `usuario_id` bigint(20) UNSIGNED DEFAULT NULL,
  `modulo` varchar(80) DEFAULT NULL,
  `contexto` varchar(80) NOT NULL,
  `referencia_id` bigint(20) UNSIGNED DEFAULT NULL,
  `cliente_id` bigint(20) UNSIGNED DEFAULT NULL,
  `telefono` varchar(40) DEFAULT NULL,
  `mensaje` text NOT NULL,
  `enlace_publico` varchar(255) DEFAULT NULL,
  `whatsapp_url` mediumtext DEFAULT NULL,
  `estado` enum('generado','enviado_manual','cancelado') NOT NULL DEFAULT 'generado',
  `creado` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `whatsapp_plantillas`
--

CREATE TABLE `whatsapp_plantillas` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `negocio_id` bigint(20) UNSIGNED NOT NULL,
  `modulo` varchar(80) NOT NULL,
  `contexto` varchar(80) NOT NULL,
  `nombre` varchar(140) NOT NULL,
  `contenido` text NOT NULL,
  `estado` tinyint(1) NOT NULL DEFAULT 1,
  `creado` datetime NOT NULL DEFAULT current_timestamp(),
  `actualizado` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Índices para tablas volcadas
--

--
-- Indices de la tabla `afiliados`
--
ALTER TABLE `afiliados`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `codigo` (`codigo`),
  ADD KEY `fk_afiliados_usuario` (`usuario_id`);

--
-- Indices de la tabla `afiliado_comisiones`
--
ALTER TABLE `afiliado_comisiones`
  ADD PRIMARY KEY (`id`),
  ADD KEY `fk_afiliado_comisiones_afiliado` (`afiliado_id`),
  ADD KEY `fk_afiliado_comisiones_negocio` (`negocio_id`);

--
-- Indices de la tabla `afiliado_referidos`
--
ALTER TABLE `afiliado_referidos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `fk_afiliado_referidos_afiliado` (`afiliado_id`),
  ADD KEY `fk_afiliado_referidos_negocio` (`negocio_id`);

--
-- Indices de la tabla `ai_conversaciones`
--
ALTER TABLE `ai_conversaciones`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_ai_conversaciones_canal_estado` (`canal`,`estado`,`actualizado`),
  ADD KEY `idx_ai_conversaciones_usuario` (`usuario_id`),
  ADD KEY `idx_ai_conversaciones_negocio` (`negocio_id`),
  ADD KEY `idx_ai_conversaciones_session` (`session_hash`);

--
-- Indices de la tabla `ai_mensajes`
--
ALTER TABLE `ai_mensajes`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_ai_mensajes_conversacion` (`conversacion_id`,`id`);

--
-- Indices de la tabla `ai_rate_limits`
--
ALTER TABLE `ai_rate_limits`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_ai_rate_ip_canal` (`ip_hash`,`canal`),
  ADD KEY `idx_ai_rate_block` (`blocked_until`);

--
-- Indices de la tabla `ai_seguimientos`
--
ALTER TABLE `ai_seguimientos`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_ai_seguimientos_conversacion` (`conversacion_id`),
  ADD KEY `idx_ai_seguimientos_estado` (`estado`),
  ADD KEY `idx_ai_seguimientos_tipo` (`tipo`),
  ADD KEY `idx_ai_seguimientos_prioridad` (`prioridad`),
  ADD KEY `idx_ai_seguimientos_negocio` (`negocio_id`),
  ADD KEY `idx_ai_seguimientos_agenda` (`agendado_para`),
  ADD KEY `idx_ai_seguimientos_actualizado` (`actualizado`);

--
-- Indices de la tabla `auditoria`
--
ALTER TABLE `auditoria`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_auditoria_negocio_fecha` (`negocio_id`,`creado`),
  ADD KEY `fk_auditoria_usuario` (`usuario_id`);

--
-- Indices de la tabla `cache`
--
ALTER TABLE `cache`
  ADD PRIMARY KEY (`key`);

--
-- Indices de la tabla `cache_locks`
--
ALTER TABLE `cache_locks`
  ADD PRIMARY KEY (`key`);

--
-- Indices de la tabla `caja_movimientos`
--
ALTER TABLE `caja_movimientos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_caja_movimientos_sesion` (`caja_sesion_id`,`creado`),
  ADD KEY `idx_caja_movimientos_negocio` (`negocio_id`,`creado`),
  ADD KEY `idx_caja_movimientos_venta` (`venta_id`),
  ADD KEY `idx_caja_movimientos_sucursal` (`negocio_id`,`sucursal_id`);

--
-- Indices de la tabla `caja_sesiones`
--
ALTER TABLE `caja_sesiones`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_caja_sesiones_negocio_estado` (`negocio_id`,`estado`),
  ADD KEY `idx_caja_sesiones_usuario_device` (`negocio_id`,`usuario_id`,`device_hash`,`estado`),
  ADD KEY `idx_caja_sesiones_fe_key` (`fe_api_key_id`,`estado`),
  ADD KEY `idx_caja_sesiones_sucursal` (`negocio_id`,`sucursal_id`,`estado`),
  ADD KEY `idx_caja_sesiones_config_estado` (`negocio_id`,`caja_config_id`,`estado`),
  ADD KEY `idx_caja_sesiones_fe_estado` (`negocio_id`,`fe_api_key_id`,`estado`);

--
-- Indices de la tabla `catalogo_configuracion`
--
ALTER TABLE `catalogo_configuracion`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_catalogo_config_negocio` (`negocio_id`),
  ADD KEY `idx_catalogo_config_producto_envio` (`producto_envio_id`),
  ADD KEY `idx_catalogo_config_directorio` (`directorio_publico`,`estado`);

--
-- Indices de la tabla `catalogo_pedidos`
--
ALTER TABLE `catalogo_pedidos`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_catalogo_pedido_numero` (`negocio_id`,`numero`),
  ADD KEY `idx_catalogo_pedidos_estado` (`negocio_id`,`estado`,`creado`),
  ADD KEY `idx_catalogo_pedidos_cliente` (`cliente_identificacion`,`cliente_whatsapp`),
  ADD KEY `idx_catalogo_pedidos_venta` (`venta_id`);

--
-- Indices de la tabla `catalogo_pedido_detalles`
--
ALTER TABLE `catalogo_pedido_detalles`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_catalogo_pedido_detalles_pedido` (`pedido_id`),
  ADD KEY `idx_catalogo_pedido_detalles_producto` (`producto_id`);

--
-- Indices de la tabla `catalogo_reacciones`
--
ALTER TABLE `catalogo_reacciones`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_catalogo_reaccion_visitante` (`negocio_id`,`visitor_hash`),
  ADD KEY `idx_catalogo_reacciones_tipo` (`negocio_id`,`tipo`);

--
-- Indices de la tabla `categorias`
--
ALTER TABLE `categorias`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_categorias_negocio` (`negocio_id`),
  ADD KEY `idx_categorias_catalogo_destacada` (`negocio_id`,`catalogo_destacada`,`estado`),
  ADD KEY `idx_categorias_negocio_sucursal` (`negocio_id`,`sucursal_id`);

--
-- Indices de la tabla `clientes`
--
ALTER TABLE `clientes`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_clientes_negocio` (`negocio_id`),
  ADD KEY `idx_clientes_busqueda` (`nombre`,`identificacion`,`telefono`,`correo`),
  ADD KEY `idx_clientes_negocio_sucursal` (`negocio_id`,`sucursal_id`);

--
-- Indices de la tabla `configuracion_negocio`
--
ALTER TABLE `configuracion_negocio`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `negocio_id` (`negocio_id`);

--
-- Indices de la tabla `contacto_mensajes`
--
ALTER TABLE `contacto_mensajes`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_contacto_mensajes_estado` (`estado`,`creado`),
  ADD KEY `idx_contacto_mensajes_correo` (`correo`),
  ADD KEY `idx_contacto_mensajes_asunto` (`asunto`);

--
-- Indices de la tabla `contacto_rate_limits`
--
ALTER TABLE `contacto_rate_limits`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_contacto_rate` (`ip_hash`,`accion`),
  ADD KEY `idx_contacto_rate_blocked` (`blocked_until`);

--
-- Indices de la tabla `demo_solicitudes`
--
ALTER TABLE `demo_solicitudes`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_demo_solicitudes_estado` (`estado`,`creado`),
  ADD KEY `idx_demo_solicitudes_correo` (`correo`);

--
-- Indices de la tabla `failed_jobs`
--
ALTER TABLE `failed_jobs`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `failed_jobs_uuid_unique` (`uuid`);

--
-- Indices de la tabla `fe_api_cache`
--
ALTER TABLE `fe_api_cache`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_fe_api_cache_endpoint_key` (`endpoint`,`cache_key`),
  ADD KEY `idx_fe_api_cache_expires` (`expires_at`);

--
-- Indices de la tabla `fe_api_keys`
--
ALTER TABLE `fe_api_keys`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_fe_api_key` (`api_key`),
  ADD KEY `idx_fe_api_keys_sucursal_estado` (`sucursal_id`,`estado`),
  ADD KEY `idx_fe_api_keys_terminal` (`terminal_id`),
  ADD KEY `idx_fe_api_keys_origen` (`actor_tipo_origen`,`actor_id_origen`,`negocio_id_origen`),
  ADD KEY `idx_fe_keys_operativa` (`negocio_id_origen`,`sucursal_operativa_id`,`estado`),
  ADD KEY `idx_fe_api_keys_sucursal_operativa` (`negocio_id_origen`,`sucursal_operativa_id`,`estado`),
  ADD KEY `idx_fe_api_keys_caja_config` (`caja_config_id`,`estado`);

--
-- Indices de la tabla `fe_api_key_eventos`
--
ALTER TABLE `fe_api_key_eventos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_key_eventos_key` (`fe_api_key_id`,`creado`),
  ADD KEY `idx_key_eventos_negocio` (`negocio_id`,`creado`);

--
-- Indices de la tabla `fe_cabys_items`
--
ALTER TABLE `fe_cabys_items`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_fe_cabys_codigo` (`codigo`),
  ADD UNIQUE KEY `uq_fe_cabys_codigo` (`codigo`),
  ADD KEY `idx_fe_cabys_descripcion` (`descripcion`(191)),
  ADD KEY `idx_fe_cabys_activo_codigo` (`activo`,`codigo`);

--
-- Indices de la tabla `fe_clientes`
--
ALTER TABLE `fe_clientes`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_fe_clientes_identificacion` (`identificacion`),
  ADD KEY `idx_fe_clientes_negocio` (`negocio_id_origen`);

--
-- Indices de la tabla `fe_condiciones_agropecuarias`
--
ALTER TABLE `fe_condiciones_agropecuarias`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_agro_identificacion` (`identificacion`);

--
-- Indices de la tabla `fe_contribuyentes_ae`
--
ALTER TABLE `fe_contribuyentes_ae`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_fe_contribuyentes_identificacion` (`identificacion`),
  ADD KEY `idx_fe_contribuyentes_nombre` (`nombre`),
  ADD KEY `idx_fe_ae_identificacion_actualizado` (`identificacion`,`actualizado_en`);

--
-- Indices de la tabla `fe_documentos`
--
ALTER TABLE `fe_documentos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_documentos_tipo_estado` (`tipo_documento`,`estado`),
  ADD KEY `idx_fe_documentos_clave` (`clave`),
  ADD KEY `idx_fe_documentos_referencia` (`ref_clave`),
  ADD KEY `idx_fe_documentos_negocio_fecha` (`negocio_id_origen`,`fecha_emision`),
  ADD KEY `idx_fe_documentos_venta` (`venta_id_origen`),
  ADD KEY `idx_fe_docs_sucursal_operativa` (`negocio_id_origen`,`sucursal_operativa_id`),
  ADD KEY `idx_fe_docs_global_fecha` (`fecha_emision`),
  ADD KEY `idx_fe_docs_global_estado` (`respuesta_hacienda`,`estado`),
  ADD KEY `idx_fe_docs_global_ambiente` (`ambiente`),
  ADD KEY `idx_fe_docs_global_tipo_fecha` (`tipo_documento`,`fecha_emision`);

--
-- Indices de la tabla `fe_documento_detalles`
--
ALTER TABLE `fe_documento_detalles`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_doc_detalle_documento` (`documento_id`),
  ADD KEY `idx_fe_doc_detalle_cabys` (`codigo_cabys`);

--
-- Indices de la tabla `fe_documento_detalle_descuentos`
--
ALTER TABLE `fe_documento_detalle_descuentos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_ddd_detalle` (`documento_detalle_id`);

--
-- Indices de la tabla `fe_documento_detalle_impuestos`
--
ALTER TABLE `fe_documento_detalle_impuestos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_ddi_detalle` (`documento_detalle_id`),
  ADD KEY `idx_fe_ddi_codigo` (`codigo`,`codigo_tarifa_iva`);

--
-- Indices de la tabla `fe_documento_medios_pago`
--
ALTER TABLE `fe_documento_medios_pago`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_dmp_documento` (`documento_id`),
  ADD KEY `idx_fe_dmp_tipo` (`tipo_medio_pago`);

--
-- Indices de la tabla `fe_documento_recepciones_xml`
--
ALTER TABLE `fe_documento_recepciones_xml`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_recepciones_clave` (`clave`),
  ADD KEY `idx_fe_recepciones_negocio` (`negocio_id_origen`),
  ADD KEY `idx_fe_recepciones_estado` (`estado`),
  ADD KEY `idx_fe_recepciones_mensaje` (`mensaje_receptor_id`),
  ADD KEY `idx_fe_recepciones_emisor` (`emisor_identificacion`),
  ADD KEY `idx_fe_recepciones_fecha` (`creado_en`),
  ADD KEY `idx_recepcion_xml_negocio_estado` (`negocio_id_origen`,`estado`),
  ADD KEY `idx_recepcion_xml_clave` (`clave`),
  ADD KEY `idx_recepcion_xml_emisor` (`emisor_identificacion`),
  ADD KEY `idx_recepcion_xml_fecha` (`fecha_emision`);

--
-- Indices de la tabla `fe_exoneraciones_consultas`
--
ALTER TABLE `fe_exoneraciones_consultas`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_exoneraciones_numero` (`numero_documento`);

--
-- Indices de la tabla `fe_formas_farmaceuticas`
--
ALTER TABLE `fe_formas_farmaceuticas`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_fe_formas_farmaceuticas_codigo` (`codigo`);

--
-- Indices de la tabla `fe_mensajes_receptor`
--
ALTER TABLE `fe_mensajes_receptor`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_msg_clave` (`clave`),
  ADD KEY `idx_fe_msg_estado` (`estado`),
  ADD KEY `idx_fe_msg_negocio` (`negocio_id_origen`);

--
-- Indices de la tabla `fe_mh_credenciales`
--
ALTER TABLE `fe_mh_credenciales`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_mh_client_env` (`client_id`,`mh_environment`),
  ADD KEY `idx_fe_mh_credenciales_negocio` (`negocio_id_origen`),
  ADD KEY `idx_fe_mh_credenciales_cliente` (`client_id`);

--
-- Indices de la tabla `fe_sucursales`
--
ALTER TABLE `fe_sucursales`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_sucursales_cliente` (`cliente_id`),
  ADD KEY `idx_fe_sucursales_negocio` (`negocio_id_origen`),
  ADD KEY `idx_fe_sucursales_codes` (`cod_sucursal`,`cod_terminal`,`mh_ambiente`),
  ADD KEY `idx_fe_sucursales_operativa` (`negocio_id_origen`,`sucursal_operativa_id`);

--
-- Indices de la tabla `fe_terminales`
--
ALTER TABLE `fe_terminales`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_fe_terminales_cliente` (`cliente_id`),
  ADD KEY `idx_fe_terminales_negocio` (`negocio_id_origen`),
  ADD KEY `idx_fe_terminales_codes` (`cod_sucursal`,`cod_terminal`),
  ADD KEY `idx_fe_terminales_operativa` (`negocio_id_origen`,`sucursal_operativa_id`),
  ADD KEY `idx_fe_terminales_sucursal_operativa` (`negocio_id_origen`,`sucursal_operativa_id`,`estado`);

--
-- Indices de la tabla `fe_tipo_cambio`
--
ALTER TABLE `fe_tipo_cambio`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_fe_tipo_cambio_fecha_moneda` (`fecha`,`moneda`),
  ADD KEY `idx_fe_tipo_cambio_moneda_fecha` (`moneda`,`fecha`);

--
-- Indices de la tabla `fe_webhook_hacienda_logs`
--
ALTER TABLE `fe_webhook_hacienda_logs`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_fe_webhook_clave_payload` (`clave`,`payload_hash`),
  ADD KEY `idx_fe_webhook_clave` (`clave`),
  ADD KEY `idx_fe_webhook_estado` (`estado`),
  ADD KEY `idx_fe_webhook_procesado` (`procesado`),
  ADD KEY `idx_fe_webhook_clave_estado` (`clave`,`estado`);

--
-- Indices de la tabla `hotel_config`
--
ALTER TABLE `hotel_config`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uniq_hotel_config_negocio` (`negocio_id`);

--
-- Indices de la tabla `hotel_habitaciones`
--
ALTER TABLE `hotel_habitaciones`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uniq_hotel_habitacion_numero` (`negocio_id`,`numero`),
  ADD KEY `idx_hotel_habitacion_tipo` (`tipo_id`),
  ADD KEY `idx_hotel_habitacion_estado` (`estado_operativo`),
  ADD KEY `idx_hotel_habitacion_bloqueo` (`bloqueo_hasta`);

--
-- Indices de la tabla `hotel_reservas`
--
ALTER TABLE `hotel_reservas`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uniq_hotel_reserva_codigo` (`negocio_id`,`codigo`),
  ADD KEY `idx_hotel_reserva_negocio_estado` (`negocio_id`,`estado`),
  ADD KEY `idx_hotel_reserva_habitacion` (`habitacion_id`),
  ADD KEY `idx_hotel_reserva_fechas` (`fecha_ingreso`,`fecha_salida_programada`),
  ADD KEY `idx_hotel_reserva_venta` (`venta_id`);

--
-- Indices de la tabla `hotel_reserva_cargos`
--
ALTER TABLE `hotel_reserva_cargos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_hotel_cargo_reserva_tipo` (`reserva_id`,`tipo`),
  ADD KEY `idx_hotel_cargo_negocio` (`negocio_id`),
  ADD KEY `idx_hotel_cargo_producto` (`producto_id`);

--
-- Indices de la tabla `hotel_reserva_pagos`
--
ALTER TABLE `hotel_reserva_pagos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_hotel_pago_negocio` (`negocio_id`),
  ADD KEY `idx_hotel_pago_reserva` (`reserva_id`),
  ADD KEY `idx_hotel_pago_estado` (`estado`);

--
-- Indices de la tabla `hotel_tipos_habitacion`
--
ALTER TABLE `hotel_tipos_habitacion`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_hotel_tipo_negocio` (`negocio_id`),
  ADD KEY `idx_hotel_tipo_producto` (`producto_id`);

--
-- Indices de la tabla `inventario_boletas_farmaceuticas`
--
ALTER TABLE `inventario_boletas_farmaceuticas`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_inv_boletas_negocio` (`negocio_id`),
  ADD KEY `idx_inv_boletas_producto` (`producto_id`),
  ADD KEY `idx_inv_boletas_fecha` (`fecha_boleta`);

--
-- Indices de la tabla `inventario_codigos_barras`
--
ALTER TABLE `inventario_codigos_barras`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_inv_codigo_barras_codigo` (`codigo`),
  ADD KEY `idx_inv_codigo_barras_producto` (`producto_id`);

--
-- Indices de la tabla `inventario_compras`
--
ALTER TABLE `inventario_compras`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_inv_compras_negocio` (`negocio_id`),
  ADD KEY `idx_inv_compras_documento` (`numero_documento`),
  ADD KEY `idx_inv_compras_fecha` (`fecha_documento`),
  ADD KEY `idx_inventario_compras_recepcion_xml` (`recepcion_xml_id`),
  ADD KEY `idx_inv_compras_sucursal` (`negocio_id`,`sucursal_id`,`id`);

--
-- Indices de la tabla `inventario_compra_detalles`
--
ALTER TABLE `inventario_compra_detalles`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_inv_compra_detalles_compra` (`compra_id`),
  ADD KEY `idx_inv_compra_detalles_producto` (`producto_id`);

--
-- Indices de la tabla `inventario_garantias`
--
ALTER TABLE `inventario_garantias`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_inv_garantias_negocio` (`negocio_id`),
  ADD KEY `idx_inv_garantias_producto` (`producto_id`),
  ADD KEY `idx_inv_garantias_cliente` (`cliente_id`),
  ADD KEY `idx_inv_garantias_tipo` (`tipo_garantia`),
  ADD KEY `idx_inv_garantias_fin` (`fecha_fin`);

--
-- Indices de la tabla `inventario_lotes`
--
ALTER TABLE `inventario_lotes`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_inv_lotes_negocio` (`negocio_id`),
  ADD KEY `idx_inv_lotes_producto` (`producto_id`),
  ADD KEY `idx_inv_lotes_vencimiento` (`fecha_vencimiento`),
  ADD KEY `idx_inv_lotes_estado` (`estado`),
  ADD KEY `idx_inv_lotes_sucursal` (`sucursal_id`);

--
-- Indices de la tabla `inventario_movimientos`
--
ALTER TABLE `inventario_movimientos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_inv_movimientos_negocio` (`negocio_id`),
  ADD KEY `idx_inv_movimientos_producto` (`producto_id`),
  ADD KEY `idx_inv_movimientos_tipo` (`tipo`),
  ADD KEY `idx_inv_movimientos_creado` (`creado`),
  ADD KEY `idx_inv_mov_sucursal_origen` (`sucursal_origen_id`),
  ADD KEY `idx_inv_mov_sucursal_destino` (`sucursal_destino_id`),
  ADD KEY `idx_inv_mov_traslado` (`traslado_id`),
  ADD KEY `idx_inv_mov_sucursal` (`negocio_id`,`sucursal_origen_id`,`sucursal_destino_id`);

--
-- Indices de la tabla `inventario_productos_farmaceuticos`
--
ALTER TABLE `inventario_productos_farmaceuticos`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_inv_farma_producto` (`negocio_id`,`producto_id`),
  ADD KEY `idx_inv_farma_negocio` (`negocio_id`),
  ADD KEY `idx_inv_farma_producto` (`producto_id`),
  ADD KEY `idx_inv_farma_vencimiento` (`fecha_vencimiento_certificacion`),
  ADD KEY `idx_inv_farma_estado` (`estado`);

--
-- Indices de la tabla `inventario_stock_ubicaciones`
--
ALTER TABLE `inventario_stock_ubicaciones`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_stock_ubicacion` (`negocio_id`,`sucursal_id`,`ubicacion_id`,`producto_id`,`lote_id`),
  ADD KEY `idx_stock_ubicacion_negocio` (`negocio_id`),
  ADD KEY `idx_stock_ubicacion_producto` (`producto_id`),
  ADD KEY `idx_stock_ubicacion_sucursal` (`sucursal_id`),
  ADD KEY `idx_stock_ubicacion_ubicacion` (`ubicacion_id`),
  ADD KEY `idx_inv_stock_sucursal` (`negocio_id`,`sucursal_id`,`producto_id`,`ubicacion_id`),
  ADD KEY `idx_stock_salida_disponible` (`negocio_id`,`sucursal_id`,`producto_id`,`lote_id`);

--
-- Indices de la tabla `inventario_traslados`
--
ALTER TABLE `inventario_traslados`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_inv_traslado_numero` (`negocio_id`,`numero`),
  ADD UNIQUE KEY `uq_inv_traslado_token` (`token`),
  ADD KEY `idx_inv_traslados_negocio` (`negocio_id`),
  ADD KEY `idx_inv_traslados_estado` (`estado`),
  ADD KEY `idx_inv_traslados_origen` (`origen_sucursal_id`),
  ADD KEY `idx_inv_traslados_destino` (`destino_sucursal_id`),
  ADD KEY `idx_inv_traslados_anulado_usuario` (`anulado_por_usuario_id`);

--
-- Indices de la tabla `inventario_traslado_eventos`
--
ALTER TABLE `inventario_traslado_eventos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_inv_traslado_eventos_traslado` (`traslado_id`),
  ADD KEY `idx_inv_traslado_eventos_usuario` (`usuario_id`);

--
-- Indices de la tabla `inventario_traslado_lineas`
--
ALTER TABLE `inventario_traslado_lineas`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_inv_traslado_lineas_traslado` (`traslado_id`),
  ADD KEY `idx_inv_traslado_lineas_producto` (`producto_id`),
  ADD KEY `idx_inv_traslado_lineas_lote` (`lote_id`),
  ADD KEY `idx_inv_traslado_lineas_destino_producto` (`destino_producto_id`),
  ADD KEY `idx_inv_traslado_lineas_destino_lote` (`destino_lote_id`);

--
-- Indices de la tabla `inventario_ubicaciones`
--
ALTER TABLE `inventario_ubicaciones`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_inv_ubicaciones_negocio` (`negocio_id`),
  ADD KEY `idx_inv_ubicaciones_estado` (`estado`),
  ADD KEY `idx_inv_ubicaciones_sucursal` (`sucursal_id`);

--
-- Indices de la tabla `jobs`
--
ALTER TABLE `jobs`
  ADD PRIMARY KEY (`id`),
  ADD KEY `jobs_queue_index` (`queue`);

--
-- Indices de la tabla `job_batches`
--
ALTER TABLE `job_batches`
  ADD PRIMARY KEY (`id`);

--
-- Indices de la tabla `landing_paginas`
--
ALTER TABLE `landing_paginas`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_landing_paginas_slug` (`slug`),
  ADD KEY `idx_landing_paginas_estado` (`estado`,`orden`),
  ADD KEY `idx_landing_paginas_tipo` (`tipo`,`orden`);

--
-- Indices de la tabla `landing_testimonios`
--
ALTER TABLE `landing_testimonios`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_landing_testimonios_negocio_estado` (`negocio_id`,`estado`),
  ADD KEY `idx_landing_testimonios_publico` (`estado`,`visible`,`aprobado_en`),
  ADD KEY `idx_landing_testimonios_usuario` (`usuario_id`),
  ADD KEY `idx_landing_testimonios_solicitante` (`solicitado_por_usuario_id`);

--
-- Indices de la tabla `login_challenges`
--
ALTER TABLE `login_challenges`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_login_challenges_selector` (`selector`),
  ADD KEY `idx_login_challenges_expira` (`expira_en`,`usado_en`);

--
-- Indices de la tabla `login_rate_limits`
--
ALTER TABLE `login_rate_limits`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_login_rate_hash` (`login_hash`),
  ADD KEY `idx_login_rate_blocked` (`blocked_until`);

--
-- Indices de la tabla `metodos_pago`
--
ALTER TABLE `metodos_pago`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_metodos_pago_negocio` (`negocio_id`);

--
-- Indices de la tabla `modulos`
--
ALTER TABLE `modulos`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `clave` (`clave`);

--
-- Indices de la tabla `modulo_personalizado_solicitudes`
--
ALTER TABLE `modulo_personalizado_solicitudes`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_mps_estado` (`estado`),
  ADD KEY `idx_mps_negocio` (`negocio_id`),
  ADD KEY `idx_mps_creado` (`creado`);

--
-- Indices de la tabla `nanaclub_comisiones`
--
ALTER TABLE `nanaclub_comisiones`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_nanaclub_comision_pago_miembro_tipo` (`pago_id`,`miembro_id`,`tipo_comision`,`origen_producto`,`plan_id`,`modulo_id`),
  ADD KEY `idx_nanaclub_comisiones_miembro` (`miembro_id`,`estado`),
  ADD KEY `idx_nanaclub_comisiones_negocio` (`negocio_id`);

--
-- Indices de la tabla `nanaclub_configuraciones`
--
ALTER TABLE `nanaclub_configuraciones`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_nanaclub_config_clave` (`clave`);

--
-- Indices de la tabla `nanaclub_miembros`
--
ALTER TABLE `nanaclub_miembros`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_nanaclub_miembro_usuario` (`usuario_id`),
  ADD UNIQUE KEY `uk_nanaclub_miembro_codigo` (`codigo`),
  ADD KEY `idx_nanaclub_miembros_estado` (`estado`,`tipo`),
  ADD KEY `idx_nanaclub_miembros_negocio` (`negocio_id`);

--
-- Indices de la tabla `nanaclub_referencias`
--
ALTER TABLE `nanaclub_referencias`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_nanaclub_ref_miembro` (`miembro_id`),
  ADD KEY `idx_nanaclub_ref_negocio` (`negocio_id`),
  ADD KEY `idx_nanaclub_ref_codigo` (`codigo`);

--
-- Indices de la tabla `nanaclub_solicitudes_afiliados`
--
ALTER TABLE `nanaclub_solicitudes_afiliados`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_nanaclub_solicitudes_afiliados_estado` (`estado`,`creado`),
  ADD KEY `idx_nanaclub_solicitudes_afiliados_correo` (`correo`),
  ADD KEY `idx_nanaclub_solicitudes_afiliados_usuario` (`usuario_id`);

--
-- Indices de la tabla `nanaclub_solicitudes_pago`
--
ALTER TABLE `nanaclub_solicitudes_pago`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_nanaclub_solicitudes_miembro` (`miembro_id`,`estado`),
  ADD KEY `idx_nanaclub_solicitudes_estado` (`estado`,`solicitado_en`);

--
-- Indices de la tabla `nanaclub_wallet_movimientos`
--
ALTER TABLE `nanaclub_wallet_movimientos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_nanaclub_wallet_miembro` (`miembro_id`,`creado`),
  ADD KEY `idx_nanaclub_wallet_pago` (`pago_id`);

--
-- Indices de la tabla `negocios`
--
ALTER TABLE `negocios`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `slug` (`slug`),
  ADD KEY `fk_negocios_plan` (`plan_id`);

--
-- Indices de la tabla `negocio_facturacion_responsable`
--
ALTER TABLE `negocio_facturacion_responsable`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_nfr_negocio` (`negocio_id`),
  ADD KEY `idx_nfr_identificacion` (`tipo_identificacion`,`identificacion`);

--
-- Indices de la tabla `negocio_fe_configuracion`
--
ALTER TABLE `negocio_fe_configuracion`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_negocio_fe_configuracion` (`negocio_id`),
  ADD KEY `idx_negocio_fe_estado` (`estado`),
  ADD KEY `idx_negocio_fe_key` (`fe_api_key_id`),
  ADD KEY `fk_negocio_fe_configuracion_modulo` (`modulo_id`),
  ADD KEY `fk_negocio_fe_configuracion_usuario` (`activado_por_usuario_id`),
  ADD KEY `idx_negocio_fe_config_mh_credencial` (`mh_credencial_id`);

--
-- Indices de la tabla `negocio_fe_paquetes`
--
ALTER TABLE `negocio_fe_paquetes`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_negocio_sucursal_modulo_periodo` (`negocio_id`,`sucursal_id`,`modulo_id`,`periodo_inicio`),
  ADD KEY `idx_negocio_fe_paquetes_negocio` (`negocio_id`),
  ADD KEY `idx_negocio_fe_paquetes_estado` (`estado`),
  ADD KEY `idx_fe_paquetes_fk_negocio` (`negocio_id`),
  ADD KEY `idx_fe_paquetes_fk_modulo` (`modulo_id`),
  ADD KEY `idx_fe_paquetes_sucursal` (`negocio_id`,`sucursal_id`,`modulo_id`,`estado`),
  ADD KEY `idx_negocio_fe_paquetes_sucursal_estado` (`negocio_id`,`sucursal_id`,`estado`);

--
-- Indices de la tabla `negocio_fe_recargas`
--
ALTER TABLE `negocio_fe_recargas`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_negocio_fe_recargas_negocio` (`negocio_id`,`estado`),
  ADD KEY `idx_negocio_fe_recargas_pago` (`pago_id`);

--
-- Indices de la tabla `negocio_modulos`
--
ALTER TABLE `negocio_modulos`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_negocio_sucursal_modulo` (`negocio_id`,`sucursal_id`,`modulo_id`),
  ADD KEY `fk_negocio_modulos_modulo` (`modulo_id`),
  ADD KEY `idx_negocio_modulos_fk_negocio` (`negocio_id`),
  ADD KEY `idx_negocio_modulos_fk_modulo` (`modulo_id`),
  ADD KEY `idx_negocio_modulos_sucursal` (`negocio_id`,`sucursal_id`,`modulo_id`,`estado`);

--
-- Indices de la tabla `negocio_sucursales`
--
ALTER TABLE `negocio_sucursales`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_negocio_sucursal_codigo` (`negocio_id`,`codigo`),
  ADD KEY `idx_negocio_sucursales_negocio` (`negocio_id`),
  ADD KEY `idx_negocio_sucursales_admin` (`administrador_usuario_id`),
  ADD KEY `idx_negocio_sucursales_estado` (`estado`),
  ADD KEY `idx_negocio_sucursales_hereda_modulos` (`negocio_id`,`hereda_modulos_principal`,`estado`);

--
-- Indices de la tabla `negocio_sucursales_config`
--
ALTER TABLE `negocio_sucursales_config`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_sucursales_config_negocio` (`negocio_id`);

--
-- Indices de la tabla `negocio_sucursal_cajas`
--
ALTER TABLE `negocio_sucursal_cajas`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_negocio_sucursal_caja` (`negocio_id`,`sucursal_id`,`codigo_caja`),
  ADD UNIQUE KEY `uq_negocio_caja_fe_key` (`negocio_id`,`fe_api_key_id`),
  ADD KEY `idx_negocio_sucursal_estado` (`negocio_id`,`sucursal_id`,`estado`),
  ADD KEY `idx_negocio_terminal_fe` (`negocio_id`,`fe_terminal_id`,`estado`);

--
-- Indices de la tabla `negocio_sucursal_usuarios`
--
ALTER TABLE `negocio_sucursal_usuarios`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_sucursal_usuario` (`negocio_id`,`sucursal_id`,`usuario_id`),
  ADD KEY `idx_sucursal_usuarios_negocio` (`negocio_id`),
  ADD KEY `idx_sucursal_usuarios_usuario` (`usuario_id`);

--
-- Indices de la tabla `negocio_usuarios`
--
ALTER TABLE `negocio_usuarios`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_negocio_usuario` (`negocio_id`,`usuario_id`),
  ADD KEY `fk_negocio_usuarios_usuario` (`usuario_id`),
  ADD KEY `fk_negocio_usuarios_rol` (`rol_id`),
  ADD KEY `idx_negocio_usuarios_sucursal` (`negocio_id`,`sucursal_id`,`estado`);

--
-- Indices de la tabla `pagos_suscripciones`
--
ALTER TABLE `pagos_suscripciones`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_pagos_suscripciones_negocio` (`negocio_id`,`estado`),
  ADD KEY `idx_pagos_suscripciones_tipo` (`tipo`,`estado`),
  ADD KEY `idx_pagos_suscripciones_modulo` (`modulo_id`),
  ADD KEY `idx_pagos_suscripciones_plan` (`plan_id`),
  ADD KEY `idx_pagos_suscripciones_sucursal` (`negocio_id`,`sucursal_id`,`estado`),
  ADD KEY `idx_pagos_suscripciones_destino_sucursal` (`destino_sucursal_id`,`estado`);

--
-- Indices de la tabla `password_resets`
--
ALTER TABLE `password_resets`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_password_resets_token` (`token`),
  ADD KEY `idx_password_resets_usuario` (`usuario_id`,`usado_en`,`expira_en`),
  ADD KEY `idx_password_resets_correo` (`correo`,`creado`);

--
-- Indices de la tabla `permisos`
--
ALTER TABLE `permisos`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `clave` (`clave`);

--
-- Indices de la tabla `planes`
--
ALTER TABLE `planes`
  ADD PRIMARY KEY (`id`);

--
-- Indices de la tabla `plan_checkout_rate_limits`
--
ALTER TABLE `plan_checkout_rate_limits`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_checkout_rate` (`ip_hash`,`accion`),
  ADD KEY `idx_plan_checkout_rate_blocked` (`blocked_until`);

--
-- Indices de la tabla `plan_modulos`
--
ALTER TABLE `plan_modulos`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_plan_modulo` (`plan_id`,`modulo_id`),
  ADD KEY `fk_plan_modulos_modulo` (`modulo_id`);

--
-- Indices de la tabla `plan_solicitudes`
--
ALTER TABLE `plan_solicitudes`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_plan_solicitudes_negocio` (`negocio_id`),
  ADD KEY `idx_plan_solicitudes_plan` (`plan_id_solicitado`),
  ADD KEY `idx_plan_solicitudes_estado` (`estado`),
  ADD KEY `idx_plan_solicitudes_creado` (`creado`),
  ADD KEY `idx_plan_solicitudes_usuario` (`usuario_id`),
  ADD KEY `idx_plan_solicitudes_checkout_token` (`checkout_token`),
  ADD KEY `idx_plan_solicitudes_origen_estado` (`origen`,`estado`);

--
-- Indices de la tabla `plataforma_facturas_servicios`
--
ALTER TABLE `plataforma_facturas_servicios`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_pfs_pago` (`pago_id`),
  ADD KEY `idx_pfs_negocio_estado` (`negocio_id`,`estado`),
  ADD KEY `idx_pfs_estado` (`estado`,`creado`),
  ADD KEY `idx_pfs_tipo` (`tipo`,`origen`);

--
-- Indices de la tabla `plataforma_factura_servicio_lineas`
--
ALTER TABLE `plataforma_factura_servicio_lineas`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_pfs_lineas_factura` (`factura_id`);

--
-- Indices de la tabla `plataforma_fe_configuracion`
--
ALTER TABLE `plataforma_fe_configuracion`
  ADD PRIMARY KEY (`id`);

--
-- Indices de la tabla `plataforma_metodos_pago`
--
ALTER TABLE `plataforma_metodos_pago`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_plataforma_metodos_pago_estado` (`estado`);

--
-- Indices de la tabla `productos`
--
ALTER TABLE `productos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_productos_negocio` (`negocio_id`),
  ADD KEY `idx_productos_busqueda` (`nombre`,`codigo`,`codigo_barras`),
  ADD KEY `fk_productos_categoria` (`categoria_id`),
  ADD KEY `idx_productos_negocio_sucursal` (`negocio_id`,`sucursal_id`),
  ADD KEY `idx_productos_origen_sucursal` (`negocio_id`,`producto_origen_id`,`sucursal_id`);

--
-- Indices de la tabla `recepcion_avanzada_correos`
--
ALTER TABLE `recepcion_avanzada_correos`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_recepcion_avanzada_negocio` (`negocio_id`),
  ADD UNIQUE KEY `uq_recepcion_avanzada_email` (`email`);

--
-- Indices de la tabla `roles`
--
ALTER TABLE `roles`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `clave` (`clave`);

--
-- Indices de la tabla `rol_permisos`
--
ALTER TABLE `rol_permisos`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_rol_permiso` (`rol_id`,`permiso_id`),
  ADD KEY `fk_rol_permisos_permiso` (`permiso_id`);

--
-- Indices de la tabla `salon_citas`
--
ALTER TABLE `salon_citas`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_salon_cita_codigo` (`negocio_id`,`codigo`),
  ADD KEY `idx_salon_cita_fecha` (`negocio_id`,`fecha_inicio`,`fecha_fin`),
  ADD KEY `idx_salon_cita_prof` (`negocio_id`,`profesional_id`,`fecha_inicio`),
  ADD KEY `idx_salon_cita_venta` (`venta_id`);

--
-- Indices de la tabla `salon_cita_productos`
--
ALTER TABLE `salon_cita_productos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_salon_citaprod_cita` (`cita_id`),
  ADD KEY `idx_salon_citaprod_producto` (`producto_id`);

--
-- Indices de la tabla `salon_profesionales`
--
ALTER TABLE `salon_profesionales`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_salon_prof_negocio` (`negocio_id`,`estado`);

--
-- Indices de la tabla `salon_servicios`
--
ALTER TABLE `salon_servicios`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_salon_serv_negocio` (`negocio_id`,`estado`),
  ADD KEY `idx_salon_serv_producto` (`producto_servicio_id`);

--
-- Indices de la tabla `salon_servicio_productos`
--
ALTER TABLE `salon_servicio_productos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_salon_servprod_servicio` (`servicio_id`),
  ADD KEY `idx_salon_servprod_producto` (`producto_id`);

--
-- Indices de la tabla `seo_blog_posts`
--
ALTER TABLE `seo_blog_posts`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_seo_blog_posts_slug` (`slug`),
  ADD KEY `idx_seo_blog_posts_estado_publicado` (`estado`,`publicado_en`);

--
-- Indices de la tabla `seo_paginas`
--
ALTER TABLE `seo_paginas`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_seo_paginas_url` (`url_path`),
  ADD KEY `idx_seo_paginas_tipo_estado` (`tipo`,`estado`),
  ADD KEY `idx_seo_paginas_estado` (`estado`);

--
-- Indices de la tabla `sessions`
--
ALTER TABLE `sessions`
  ADD PRIMARY KEY (`id`),
  ADD KEY `sessions_user_id_index` (`user_id`),
  ADD KEY `sessions_last_activity_index` (`last_activity`);

--
-- Indices de la tabla `sistema_actualizaciones`
--
ALTER TABLE `sistema_actualizaciones`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_sistema_actualizaciones_estado` (`estado`,`visible_admin_pos`,`fecha`),
  ADD KEY `idx_sistema_actualizaciones_version` (`version`);

--
-- Indices de la tabla `sistema_configuraciones`
--
ALTER TABLE `sistema_configuraciones`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_sistema_configuraciones_clave` (`clave`);

--
-- Indices de la tabla `taller_configuracion`
--
ALTER TABLE `taller_configuracion`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_taller_config_negocio` (`negocio_id`),
  ADD KEY `idx_taller_config_negocio` (`negocio_id`);

--
-- Indices de la tabla `taller_mapas`
--
ALTER TABLE `taller_mapas`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_taller_mapas_negocio_clave` (`negocio_id`,`clave`),
  ADD KEY `idx_taller_mapas_negocio_tipo` (`negocio_id`,`dispositivo_tipo`,`estado`);

--
-- Indices de la tabla `taller_ordenes`
--
ALTER TABLE `taller_ordenes`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_taller_codigo_negocio` (`negocio_id`,`codigo`),
  ADD KEY `idx_taller_ordenes_negocio_estado` (`negocio_id`,`estado`,`creado`),
  ADD KEY `idx_taller_ordenes_cliente` (`negocio_id`,`cliente_id`),
  ADD KEY `idx_taller_ordenes_venta` (`venta_id`),
  ADD KEY `idx_taller_ordenes_servicio` (`negocio_id`,`servicio_producto_id`),
  ADD KEY `idx_taller_ordenes_cliente_fiscal` (`negocio_id`,`cliente_identificacion`);

--
-- Indices de la tabla `taller_orden_adjuntos`
--
ALTER TABLE `taller_orden_adjuntos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_taller_adjuntos_orden` (`orden_id`,`tipo`);

--
-- Indices de la tabla `taller_orden_eventos`
--
ALTER TABLE `taller_orden_eventos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_taller_eventos_orden` (`orden_id`,`creado`);

--
-- Indices de la tabla `taller_orden_items`
--
ALTER TABLE `taller_orden_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_taller_items_orden` (`orden_id`),
  ADD KEY `idx_taller_items_negocio` (`negocio_id`,`producto_id`);

--
-- Indices de la tabla `taller_tecnicos`
--
ALTER TABLE `taller_tecnicos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_taller_tecnicos_negocio` (`negocio_id`,`estado`);

--
-- Indices de la tabla `usuarios`
--
ALTER TABLE `usuarios`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `correo` (`correo`);

--
-- Indices de la tabla `ventas`
--
ALTER TABLE `ventas`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_negocio_numero` (`negocio_id`,`numero`),
  ADD KEY `idx_ventas_negocio_fecha` (`negocio_id`,`creado`),
  ADD KEY `fk_ventas_usuario` (`usuario_id`),
  ADD KEY `fk_ventas_cliente` (`cliente_id`),
  ADD KEY `fk_ventas_metodo_pago` (`metodo_pago_id`),
  ADD KEY `idx_ventas_caja_sesion` (`caja_sesion_id`),
  ADD KEY `idx_ventas_fe_api_key` (`fe_api_key_id`),
  ADD KEY `idx_ventas_negocio_sucursal` (`negocio_id`,`sucursal_id`),
  ADD KEY `idx_ventas_caja_config` (`negocio_id`,`caja_config_id`);

--
-- Indices de la tabla `ventas_eliminaciones`
--
ALTER TABLE `ventas_eliminaciones`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_ventas_eliminaciones_negocio` (`negocio_id`,`creado`),
  ADD KEY `idx_ventas_eliminaciones_venta` (`venta_id`),
  ADD KEY `idx_ventas_eliminaciones_clave` (`fe_clave`);

--
-- Indices de la tabla `venta_detalles`
--
ALTER TABLE `venta_detalles`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_venta_detalles_venta` (`venta_id`),
  ADD KEY `fk_venta_detalles_producto` (`producto_id`),
  ADD KEY `idx_venta_detalles_sucursal` (`sucursal_id`,`producto_id`);

--
-- Indices de la tabla `venta_otros_cargos`
--
ALTER TABLE `venta_otros_cargos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_venta_otros_cargos_venta` (`venta_id`);

--
-- Indices de la tabla `venta_pagos`
--
ALTER TABLE `venta_pagos`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_venta_pagos_venta` (`venta_id`),
  ADD KEY `idx_venta_pagos_metodo` (`metodo_pago_id`),
  ADD KEY `idx_venta_pagos_codigo_mh` (`codigo_mh`),
  ADD KEY `idx_venta_pagos_sucursal` (`sucursal_id`);

--
-- Indices de la tabla `whatsapp_configuracion`
--
ALTER TABLE `whatsapp_configuracion`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_whatsapp_config_negocio` (`negocio_id`);

--
-- Indices de la tabla `whatsapp_enlaces_publicos`
--
ALTER TABLE `whatsapp_enlaces_publicos`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_whatsapp_enlace_token` (`token`),
  ADD KEY `idx_whatsapp_enlace_ref` (`negocio_id`,`tipo`,`referencia_id`,`formato`,`estado`);

--
-- Indices de la tabla `whatsapp_envios`
--
ALTER TABLE `whatsapp_envios`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_whatsapp_envios_negocio` (`negocio_id`,`creado`),
  ADD KEY `idx_whatsapp_envios_contexto` (`contexto`,`referencia_id`);

--
-- Indices de la tabla `whatsapp_plantillas`
--
ALTER TABLE `whatsapp_plantillas`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_whatsapp_plantilla_contexto` (`negocio_id`,`contexto`),
  ADD KEY `idx_whatsapp_plantillas_negocio` (`negocio_id`,`estado`);

--
-- AUTO_INCREMENT de las tablas volcadas
--

--
-- AUTO_INCREMENT de la tabla `afiliados`
--
ALTER TABLE `afiliados`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `afiliado_comisiones`
--
ALTER TABLE `afiliado_comisiones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `afiliado_referidos`
--
ALTER TABLE `afiliado_referidos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `ai_conversaciones`
--
ALTER TABLE `ai_conversaciones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `ai_mensajes`
--
ALTER TABLE `ai_mensajes`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `ai_rate_limits`
--
ALTER TABLE `ai_rate_limits`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `ai_seguimientos`
--
ALTER TABLE `ai_seguimientos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `auditoria`
--
ALTER TABLE `auditoria`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `caja_movimientos`
--
ALTER TABLE `caja_movimientos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `caja_sesiones`
--
ALTER TABLE `caja_sesiones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `catalogo_configuracion`
--
ALTER TABLE `catalogo_configuracion`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `catalogo_pedidos`
--
ALTER TABLE `catalogo_pedidos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `catalogo_pedido_detalles`
--
ALTER TABLE `catalogo_pedido_detalles`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `catalogo_reacciones`
--
ALTER TABLE `catalogo_reacciones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `categorias`
--
ALTER TABLE `categorias`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `clientes`
--
ALTER TABLE `clientes`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `configuracion_negocio`
--
ALTER TABLE `configuracion_negocio`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `contacto_mensajes`
--
ALTER TABLE `contacto_mensajes`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `contacto_rate_limits`
--
ALTER TABLE `contacto_rate_limits`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `demo_solicitudes`
--
ALTER TABLE `demo_solicitudes`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `failed_jobs`
--
ALTER TABLE `failed_jobs`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_api_cache`
--
ALTER TABLE `fe_api_cache`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_api_keys`
--
ALTER TABLE `fe_api_keys`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_api_key_eventos`
--
ALTER TABLE `fe_api_key_eventos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_cabys_items`
--
ALTER TABLE `fe_cabys_items`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_clientes`
--
ALTER TABLE `fe_clientes`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_condiciones_agropecuarias`
--
ALTER TABLE `fe_condiciones_agropecuarias`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_contribuyentes_ae`
--
ALTER TABLE `fe_contribuyentes_ae`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_documentos`
--
ALTER TABLE `fe_documentos`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_documento_detalles`
--
ALTER TABLE `fe_documento_detalles`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_documento_detalle_descuentos`
--
ALTER TABLE `fe_documento_detalle_descuentos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_documento_detalle_impuestos`
--
ALTER TABLE `fe_documento_detalle_impuestos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_documento_medios_pago`
--
ALTER TABLE `fe_documento_medios_pago`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_documento_recepciones_xml`
--
ALTER TABLE `fe_documento_recepciones_xml`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_exoneraciones_consultas`
--
ALTER TABLE `fe_exoneraciones_consultas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_formas_farmaceuticas`
--
ALTER TABLE `fe_formas_farmaceuticas`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_mensajes_receptor`
--
ALTER TABLE `fe_mensajes_receptor`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_mh_credenciales`
--
ALTER TABLE `fe_mh_credenciales`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_sucursales`
--
ALTER TABLE `fe_sucursales`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_terminales`
--
ALTER TABLE `fe_terminales`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_tipo_cambio`
--
ALTER TABLE `fe_tipo_cambio`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `fe_webhook_hacienda_logs`
--
ALTER TABLE `fe_webhook_hacienda_logs`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `hotel_config`
--
ALTER TABLE `hotel_config`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `hotel_habitaciones`
--
ALTER TABLE `hotel_habitaciones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `hotel_reservas`
--
ALTER TABLE `hotel_reservas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `hotel_reserva_cargos`
--
ALTER TABLE `hotel_reserva_cargos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `hotel_reserva_pagos`
--
ALTER TABLE `hotel_reserva_pagos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `hotel_tipos_habitacion`
--
ALTER TABLE `hotel_tipos_habitacion`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_boletas_farmaceuticas`
--
ALTER TABLE `inventario_boletas_farmaceuticas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_codigos_barras`
--
ALTER TABLE `inventario_codigos_barras`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_compras`
--
ALTER TABLE `inventario_compras`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_compra_detalles`
--
ALTER TABLE `inventario_compra_detalles`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_garantias`
--
ALTER TABLE `inventario_garantias`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_lotes`
--
ALTER TABLE `inventario_lotes`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_movimientos`
--
ALTER TABLE `inventario_movimientos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_productos_farmaceuticos`
--
ALTER TABLE `inventario_productos_farmaceuticos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_stock_ubicaciones`
--
ALTER TABLE `inventario_stock_ubicaciones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_traslados`
--
ALTER TABLE `inventario_traslados`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_traslado_eventos`
--
ALTER TABLE `inventario_traslado_eventos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_traslado_lineas`
--
ALTER TABLE `inventario_traslado_lineas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `inventario_ubicaciones`
--
ALTER TABLE `inventario_ubicaciones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `jobs`
--
ALTER TABLE `jobs`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `landing_paginas`
--
ALTER TABLE `landing_paginas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `landing_testimonios`
--
ALTER TABLE `landing_testimonios`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `login_challenges`
--
ALTER TABLE `login_challenges`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `login_rate_limits`
--
ALTER TABLE `login_rate_limits`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `metodos_pago`
--
ALTER TABLE `metodos_pago`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `modulos`
--
ALTER TABLE `modulos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `modulo_personalizado_solicitudes`
--
ALTER TABLE `modulo_personalizado_solicitudes`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `nanaclub_comisiones`
--
ALTER TABLE `nanaclub_comisiones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `nanaclub_configuraciones`
--
ALTER TABLE `nanaclub_configuraciones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `nanaclub_miembros`
--
ALTER TABLE `nanaclub_miembros`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `nanaclub_referencias`
--
ALTER TABLE `nanaclub_referencias`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `nanaclub_solicitudes_afiliados`
--
ALTER TABLE `nanaclub_solicitudes_afiliados`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `nanaclub_solicitudes_pago`
--
ALTER TABLE `nanaclub_solicitudes_pago`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `nanaclub_wallet_movimientos`
--
ALTER TABLE `nanaclub_wallet_movimientos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocios`
--
ALTER TABLE `negocios`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocio_facturacion_responsable`
--
ALTER TABLE `negocio_facturacion_responsable`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocio_fe_configuracion`
--
ALTER TABLE `negocio_fe_configuracion`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocio_fe_paquetes`
--
ALTER TABLE `negocio_fe_paquetes`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocio_fe_recargas`
--
ALTER TABLE `negocio_fe_recargas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocio_modulos`
--
ALTER TABLE `negocio_modulos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocio_sucursales`
--
ALTER TABLE `negocio_sucursales`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocio_sucursales_config`
--
ALTER TABLE `negocio_sucursales_config`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocio_sucursal_cajas`
--
ALTER TABLE `negocio_sucursal_cajas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocio_sucursal_usuarios`
--
ALTER TABLE `negocio_sucursal_usuarios`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `negocio_usuarios`
--
ALTER TABLE `negocio_usuarios`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `pagos_suscripciones`
--
ALTER TABLE `pagos_suscripciones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `password_resets`
--
ALTER TABLE `password_resets`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `permisos`
--
ALTER TABLE `permisos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `planes`
--
ALTER TABLE `planes`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `plan_checkout_rate_limits`
--
ALTER TABLE `plan_checkout_rate_limits`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `plan_modulos`
--
ALTER TABLE `plan_modulos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `plan_solicitudes`
--
ALTER TABLE `plan_solicitudes`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `plataforma_facturas_servicios`
--
ALTER TABLE `plataforma_facturas_servicios`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `plataforma_factura_servicio_lineas`
--
ALTER TABLE `plataforma_factura_servicio_lineas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `plataforma_fe_configuracion`
--
ALTER TABLE `plataforma_fe_configuracion`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `plataforma_metodos_pago`
--
ALTER TABLE `plataforma_metodos_pago`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `productos`
--
ALTER TABLE `productos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `recepcion_avanzada_correos`
--
ALTER TABLE `recepcion_avanzada_correos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `roles`
--
ALTER TABLE `roles`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `rol_permisos`
--
ALTER TABLE `rol_permisos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `salon_citas`
--
ALTER TABLE `salon_citas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `salon_cita_productos`
--
ALTER TABLE `salon_cita_productos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `salon_profesionales`
--
ALTER TABLE `salon_profesionales`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `salon_servicios`
--
ALTER TABLE `salon_servicios`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `salon_servicio_productos`
--
ALTER TABLE `salon_servicio_productos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `seo_blog_posts`
--
ALTER TABLE `seo_blog_posts`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `seo_paginas`
--
ALTER TABLE `seo_paginas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `sistema_actualizaciones`
--
ALTER TABLE `sistema_actualizaciones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `sistema_configuraciones`
--
ALTER TABLE `sistema_configuraciones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `taller_configuracion`
--
ALTER TABLE `taller_configuracion`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `taller_mapas`
--
ALTER TABLE `taller_mapas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `taller_ordenes`
--
ALTER TABLE `taller_ordenes`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `taller_orden_adjuntos`
--
ALTER TABLE `taller_orden_adjuntos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `taller_orden_eventos`
--
ALTER TABLE `taller_orden_eventos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `taller_orden_items`
--
ALTER TABLE `taller_orden_items`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `taller_tecnicos`
--
ALTER TABLE `taller_tecnicos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `usuarios`
--
ALTER TABLE `usuarios`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `ventas`
--
ALTER TABLE `ventas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `ventas_eliminaciones`
--
ALTER TABLE `ventas_eliminaciones`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `venta_detalles`
--
ALTER TABLE `venta_detalles`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `venta_otros_cargos`
--
ALTER TABLE `venta_otros_cargos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `venta_pagos`
--
ALTER TABLE `venta_pagos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `whatsapp_configuracion`
--
ALTER TABLE `whatsapp_configuracion`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `whatsapp_enlaces_publicos`
--
ALTER TABLE `whatsapp_enlaces_publicos`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `whatsapp_envios`
--
ALTER TABLE `whatsapp_envios`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `whatsapp_plantillas`
--
ALTER TABLE `whatsapp_plantillas`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- Restricciones para tablas volcadas
--

--
-- Filtros para la tabla `afiliados`
--
ALTER TABLE `afiliados`
  ADD CONSTRAINT `fk_afiliados_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `afiliado_comisiones`
--
ALTER TABLE `afiliado_comisiones`
  ADD CONSTRAINT `fk_afiliado_comisiones_afiliado` FOREIGN KEY (`afiliado_id`) REFERENCES `afiliados` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_afiliado_comisiones_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `afiliado_referidos`
--
ALTER TABLE `afiliado_referidos`
  ADD CONSTRAINT `fk_afiliado_referidos_afiliado` FOREIGN KEY (`afiliado_id`) REFERENCES `afiliados` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_afiliado_referidos_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `ai_mensajes`
--
ALTER TABLE `ai_mensajes`
  ADD CONSTRAINT `fk_ai_mensajes_conversacion` FOREIGN KEY (`conversacion_id`) REFERENCES `ai_conversaciones` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `auditoria`
--
ALTER TABLE `auditoria`
  ADD CONSTRAINT `fk_auditoria_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_auditoria_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`) ON DELETE SET NULL;

--
-- Filtros para la tabla `categorias`
--
ALTER TABLE `categorias`
  ADD CONSTRAINT `fk_categorias_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `clientes`
--
ALTER TABLE `clientes`
  ADD CONSTRAINT `fk_clientes_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `configuracion_negocio`
--
ALTER TABLE `configuracion_negocio`
  ADD CONSTRAINT `fk_configuracion_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `metodos_pago`
--
ALTER TABLE `metodos_pago`
  ADD CONSTRAINT `fk_metodos_pago_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `negocios`
--
ALTER TABLE `negocios`
  ADD CONSTRAINT `fk_negocios_plan` FOREIGN KEY (`plan_id`) REFERENCES `planes` (`id`) ON DELETE SET NULL;

--
-- Filtros para la tabla `negocio_fe_configuracion`
--
ALTER TABLE `negocio_fe_configuracion`
  ADD CONSTRAINT `fk_negocio_fe_configuracion_modulo` FOREIGN KEY (`modulo_id`) REFERENCES `modulos` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_negocio_fe_configuracion_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_negocio_fe_configuracion_usuario` FOREIGN KEY (`activado_por_usuario_id`) REFERENCES `usuarios` (`id`) ON DELETE SET NULL;

--
-- Filtros para la tabla `negocio_modulos`
--
ALTER TABLE `negocio_modulos`
  ADD CONSTRAINT `fk_negocio_modulos_modulo` FOREIGN KEY (`modulo_id`) REFERENCES `modulos` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_negocio_modulos_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `negocio_usuarios`
--
ALTER TABLE `negocio_usuarios`
  ADD CONSTRAINT `fk_negocio_usuarios_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_negocio_usuarios_rol` FOREIGN KEY (`rol_id`) REFERENCES `roles` (`id`),
  ADD CONSTRAINT `fk_negocio_usuarios_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `plan_modulos`
--
ALTER TABLE `plan_modulos`
  ADD CONSTRAINT `fk_plan_modulos_modulo` FOREIGN KEY (`modulo_id`) REFERENCES `modulos` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_plan_modulos_plan` FOREIGN KEY (`plan_id`) REFERENCES `planes` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `productos`
--
ALTER TABLE `productos`
  ADD CONSTRAINT `fk_productos_categoria` FOREIGN KEY (`categoria_id`) REFERENCES `categorias` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_productos_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `rol_permisos`
--
ALTER TABLE `rol_permisos`
  ADD CONSTRAINT `fk_rol_permisos_permiso` FOREIGN KEY (`permiso_id`) REFERENCES `permisos` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_rol_permisos_rol` FOREIGN KEY (`rol_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `ventas`
--
ALTER TABLE `ventas`
  ADD CONSTRAINT `fk_ventas_cliente` FOREIGN KEY (`cliente_id`) REFERENCES `clientes` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_ventas_metodo_pago` FOREIGN KEY (`metodo_pago_id`) REFERENCES `metodos_pago` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_ventas_negocio` FOREIGN KEY (`negocio_id`) REFERENCES `negocios` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_ventas_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`);

--
-- Filtros para la tabla `venta_detalles`
--
ALTER TABLE `venta_detalles`
  ADD CONSTRAINT `fk_venta_detalles_producto` FOREIGN KEY (`producto_id`) REFERENCES `productos` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_venta_detalles_venta` FOREIGN KEY (`venta_id`) REFERENCES `ventas` (`id`) ON DELETE CASCADE;

--
-- Filtros para la tabla `venta_otros_cargos`
--
ALTER TABLE `venta_otros_cargos`
  ADD CONSTRAINT `fk_venta_otros_cargos_venta` FOREIGN KEY (`venta_id`) REFERENCES `ventas` (`id`) ON DELETE CASCADE;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
