-- Auto-generated from legacy schema. Do not edit manually.

SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `clientes` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `nombre` varchar(150) NOT NULL,
  `documento` varchar(60) DEFAULT NULL,
  `email` varchar(150) DEFAULT NULL,
  `telefono` varchar(40) DEFAULT NULL,
  `direccion` varchar(255) DEFAULT NULL,
  `saldo_pendiente` decimal(12,2) NOT NULL DEFAULT 0.00,
  `credito_maximo` decimal(12,2) NOT NULL DEFAULT 0.00,
  `activo` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_clientes_empresa` (`empresa_id`),
  CONSTRAINT `fk_clientes_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `cajas` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `sucursal_id` bigint(20) unsigned DEFAULT NULL,
  `nombre` varchar(120) NOT NULL,
  `estado` enum('activa','inactiva') NOT NULL DEFAULT 'activa',
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_cajas_empresa` (`empresa_id`),
  KEY `fk_cajas_sucursal` (`sucursal_id`),
  CONSTRAINT `fk_cajas_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_cajas_sucursal` FOREIGN KEY (`sucursal_id`) REFERENCES `sucursales` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `aperturas_caja` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `caja_id` bigint(20) unsigned DEFAULT NULL,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `monto_apertura` decimal(12,2) NOT NULL DEFAULT 0.00,
  `monto_cierre` decimal(12,2) DEFAULT NULL,
  `diferencia` decimal(12,2) NOT NULL DEFAULT 0.00,
  `estado` enum('abierta','cerrada') NOT NULL DEFAULT 'abierta',
  `fecha_apertura` datetime NOT NULL,
  `fecha_cierre` datetime DEFAULT NULL,
  `notas` text DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_aperturas_empresa_estado` (`empresa_id`,`estado`),
  KEY `fk_aperturas_caja` (`caja_id`),
  KEY `fk_aperturas_usuario` (`usuario_id`),
  CONSTRAINT `fk_aperturas_caja` FOREIGN KEY (`caja_id`) REFERENCES `cajas` (`id`),
  CONSTRAINT `fk_aperturas_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_aperturas_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `ventas` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_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,
  `apertura_caja_id` bigint(20) unsigned DEFAULT NULL,
  `subtotal` decimal(12,2) NOT NULL DEFAULT 0.00,
  `descuento` decimal(12,2) NOT NULL DEFAULT 0.00,
  `impuestos` decimal(12,2) NOT NULL DEFAULT 0.00,
  `total` decimal(12,2) NOT NULL DEFAULT 0.00,
  `estado` enum('pagada','pendiente','anulada') NOT NULL DEFAULT 'pagada',
  `fecha` datetime NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_ventas_empresa_fecha` (`empresa_id`,`fecha`),
  KEY `fk_ventas_sucursal` (`sucursal_id`),
  KEY `fk_ventas_usuario` (`usuario_id`),
  KEY `fk_ventas_cliente` (`cliente_id`),
  KEY `fk_ventas_apertura` (`apertura_caja_id`),
  CONSTRAINT `fk_ventas_apertura` FOREIGN KEY (`apertura_caja_id`) REFERENCES `aperturas_caja` (`id`),
  CONSTRAINT `fk_ventas_cliente` FOREIGN KEY (`cliente_id`) REFERENCES `clientes` (`id`),
  CONSTRAINT `fk_ventas_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_ventas_sucursal` FOREIGN KEY (`sucursal_id`) REFERENCES `sucursales` (`id`),
  CONSTRAINT `fk_ventas_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `venta_detalles` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `venta_id` bigint(20) unsigned NOT NULL,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `producto_id` bigint(20) unsigned NOT NULL,
  `cantidad` decimal(12,2) NOT NULL,
  `precio_unitario` decimal(12,2) NOT NULL,
  `impuesto` decimal(12,2) NOT NULL DEFAULT 0.00,
  `descuento` decimal(12,2) NOT NULL DEFAULT 0.00,
  `total_linea` decimal(12,2) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_venta_detalles_venta` (`venta_id`),
  KEY `fk_venta_detalles_empresa` (`empresa_id`),
  KEY `fk_venta_detalles_producto` (`producto_id`),
  CONSTRAINT `fk_venta_detalles_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_venta_detalles_producto` FOREIGN KEY (`producto_id`) REFERENCES `productos` (`id`),
  CONSTRAINT `fk_venta_detalles_venta` FOREIGN KEY (`venta_id`) REFERENCES `ventas` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `venta_pagos` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `venta_id` bigint(20) unsigned NOT NULL,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `metodo_pago` varchar(50) NOT NULL,
  `monto` decimal(12,2) NOT NULL,
  `referencia` varchar(120) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_venta_pagos_venta` (`venta_id`),
  KEY `fk_venta_pagos_empresa` (`empresa_id`),
  CONSTRAINT `fk_venta_pagos_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_venta_pagos_venta` FOREIGN KEY (`venta_id`) REFERENCES `ventas` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `facturas` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `sucursal_id` bigint(20) unsigned DEFAULT NULL,
  `venta_id` bigint(20) unsigned DEFAULT NULL,
  `cliente_id` bigint(20) unsigned DEFAULT NULL,
  `tipo_documento` enum('factura','nota_credito','nota_debito') NOT NULL DEFAULT 'factura',
  `numero` varchar(80) NOT NULL,
  `subtotal` decimal(12,2) NOT NULL DEFAULT 0.00,
  `impuestos` decimal(12,2) NOT NULL DEFAULT 0.00,
  `total` decimal(12,2) NOT NULL DEFAULT 0.00,
  `estado` enum('borrador','emitida','anulada','aceptada','rechazada') NOT NULL DEFAULT 'emitida',
  `documento_referencia_id` bigint(20) unsigned DEFAULT NULL,
  `motivo_documento` varchar(255) DEFAULT NULL,
  `anulado_por_usuario_id` bigint(20) unsigned DEFAULT NULL,
  `anulado_motivo` varchar(255) DEFAULT NULL,
  `anulado_at` datetime DEFAULT NULL,
  `cufe` varchar(120) DEFAULT NULL,
  `qr_path` varchar(255) DEFAULT NULL,
  `fecha_emision` datetime NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_factura_empresa_numero` (`empresa_id`,`numero`),
  KEY `fk_facturas_venta` (`venta_id`),
  KEY `fk_facturas_cliente` (`cliente_id`),
  KEY `idx_facturas_sucursal` (`sucursal_id`),
  KEY `idx_facturas_referencia` (`empresa_id`,`documento_referencia_id`),
  CONSTRAINT `fk_facturas_cliente` FOREIGN KEY (`cliente_id`) REFERENCES `clientes` (`id`),
  CONSTRAINT `fk_facturas_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_facturas_sucursal` FOREIGN KEY (`sucursal_id`) REFERENCES `sucursales` (`id`),
  CONSTRAINT `fk_facturas_venta` FOREIGN KEY (`venta_id`) REFERENCES `ventas` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `factura_detalles` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `factura_id` bigint(20) unsigned NOT NULL,
  `producto_id` bigint(20) unsigned DEFAULT NULL,
  `descripcion` varchar(255) NOT NULL,
  `cantidad` decimal(12,2) NOT NULL,
  `precio_unitario` decimal(12,2) NOT NULL,
  `impuesto` decimal(12,2) NOT NULL DEFAULT 0.00,
  `total_linea` decimal(12,2) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_factura_detalles_factura` (`factura_id`),
  KEY `fk_factura_detalles_producto` (`producto_id`),
  CONSTRAINT `fk_factura_detalles_factura` FOREIGN KEY (`factura_id`) REFERENCES `facturas` (`id`),
  CONSTRAINT `fk_factura_detalles_producto` FOREIGN KEY (`producto_id`) REFERENCES `productos` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `devoluciones` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `venta_id` bigint(20) unsigned NOT NULL,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `motivo` varchar(255) DEFAULT NULL,
  `total` decimal(12,2) NOT NULL DEFAULT 0.00,
  `fecha` datetime NOT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_devoluciones_empresa` (`empresa_id`),
  KEY `fk_devoluciones_venta` (`venta_id`),
  KEY `fk_devoluciones_usuario` (`usuario_id`),
  CONSTRAINT `fk_devoluciones_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_devoluciones_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`),
  CONSTRAINT `fk_devoluciones_venta` FOREIGN KEY (`venta_id`) REFERENCES `ventas` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `movimientos_caja` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `apertura_id` bigint(20) unsigned NOT NULL,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `tipo` enum('ingreso','egreso') NOT NULL,
  `concepto` varchar(160) NOT NULL,
  `monto` decimal(12,2) NOT NULL DEFAULT 0.00,
  `observacion` text DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_movimientos_caja_apertura` (`empresa_id`,`apertura_id`),
  KEY `fk_movimientos_caja_apertura` (`apertura_id`),
  KEY `fk_movimientos_caja_usuario` (`usuario_id`),
  CONSTRAINT `fk_movimientos_caja_apertura` FOREIGN KEY (`apertura_id`) REFERENCES `aperturas_caja` (`id`),
  CONSTRAINT `fk_movimientos_caja_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_movimientos_caja_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `cierres_caja` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `apertura_id` bigint(20) unsigned NOT NULL,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `monto_esperado` decimal(12,2) NOT NULL DEFAULT 0.00,
  `monto_real` decimal(12,2) NOT NULL DEFAULT 0.00,
  `diferencia` decimal(12,2) NOT NULL DEFAULT 0.00,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_cierres_empresa` (`empresa_id`),
  KEY `fk_cierres_apertura` (`apertura_id`),
  KEY `fk_cierres_usuario` (`usuario_id`),
  CONSTRAINT `fk_cierres_apertura` FOREIGN KEY (`apertura_id`) REFERENCES `aperturas_caja` (`id`),
  CONSTRAINT `fk_cierres_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_cierres_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `traspasos_caja` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `apertura_origen_id` bigint(20) unsigned NOT NULL,
  `apertura_destino_id` bigint(20) unsigned NOT NULL,
  `usuario_origen_id` bigint(20) unsigned NOT NULL,
  `usuario_destino_id` bigint(20) unsigned NOT NULL,
  `monto` decimal(12,2) NOT NULL DEFAULT 0.00,
  `estado` enum('pendiente','aprobado','rechazado') NOT NULL DEFAULT 'pendiente',
  `concepto` varchar(160) NOT NULL,
  `observacion` text DEFAULT NULL,
  `respondido_por_id` bigint(20) unsigned DEFAULT NULL,
  `respondido_at` datetime DEFAULT NULL,
  `observacion_respuesta` text DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_traspasos_empresa_fecha` (`empresa_id`,`created_at`),
  KEY `fk_traspasos_apertura_origen` (`apertura_origen_id`),
  KEY `fk_traspasos_apertura_destino` (`apertura_destino_id`),
  KEY `fk_traspasos_usuario_origen` (`usuario_origen_id`),
  KEY `fk_traspasos_usuario_destino` (`usuario_destino_id`),
  KEY `idx_traspasos_estado_destino` (`empresa_id`,`estado`,`apertura_destino_id`),
  KEY `fk_traspasos_respondido_por` (`respondido_por_id`),
  CONSTRAINT `fk_traspasos_apertura_destino` FOREIGN KEY (`apertura_destino_id`) REFERENCES `aperturas_caja` (`id`),
  CONSTRAINT `fk_traspasos_apertura_origen` FOREIGN KEY (`apertura_origen_id`) REFERENCES `aperturas_caja` (`id`),
  CONSTRAINT `fk_traspasos_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_traspasos_respondido_por` FOREIGN KEY (`respondido_por_id`) REFERENCES `usuarios` (`id`),
  CONSTRAINT `fk_traspasos_usuario_destino` FOREIGN KEY (`usuario_destino_id`) REFERENCES `usuarios` (`id`),
  CONSTRAINT `fk_traspasos_usuario_origen` FOREIGN KEY (`usuario_origen_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
