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

SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `ordenes_compra` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `sucursal_id` bigint(20) unsigned DEFAULT NULL,
  `proveedor_id` bigint(20) unsigned NOT NULL,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `numero` varchar(80) NOT NULL,
  `fecha` datetime 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('abierta','parcial','completa','anulada') NOT NULL DEFAULT 'abierta',
  `observacion` text DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_ordenes_compra_numero` (`empresa_id`,`numero`),
  KEY `idx_ordenes_compra_estado` (`empresa_id`,`estado`),
  KEY `fk_ordenes_compra_sucursal` (`sucursal_id`),
  KEY `fk_ordenes_compra_proveedor` (`proveedor_id`),
  KEY `fk_ordenes_compra_usuario` (`usuario_id`),
  CONSTRAINT `fk_ordenes_compra_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_ordenes_compra_proveedor` FOREIGN KEY (`proveedor_id`) REFERENCES `proveedores` (`id`),
  CONSTRAINT `fk_ordenes_compra_sucursal` FOREIGN KEY (`sucursal_id`) REFERENCES `sucursales` (`id`),
  CONSTRAINT `fk_ordenes_compra_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `orden_compra_detalles` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `orden_compra_id` bigint(20) unsigned NOT NULL,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `producto_id` bigint(20) unsigned NOT NULL,
  `variante_id` bigint(20) unsigned DEFAULT NULL,
  `cantidad` decimal(12,2) NOT NULL,
  `cantidad_recibida` decimal(12,2) NOT NULL DEFAULT 0.00,
  `costo_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 `idx_orden_detalle_orden` (`orden_compra_id`,`empresa_id`),
  KEY `fk_orden_detalle_producto` (`producto_id`),
  KEY `idx_orden_detalle_variante` (`empresa_id`,`variante_id`),
  KEY `fk_orden_detalle_variante` (`variante_id`),
  CONSTRAINT `fk_orden_detalle_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_orden_detalle_orden` FOREIGN KEY (`orden_compra_id`) REFERENCES `ordenes_compra` (`id`),
  CONSTRAINT `fk_orden_detalle_producto` FOREIGN KEY (`producto_id`) REFERENCES `productos` (`id`),
  CONSTRAINT `fk_orden_detalle_variante` FOREIGN KEY (`variante_id`) REFERENCES `producto_variantes` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `recepciones_compra` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `orden_compra_id` bigint(20) unsigned NOT NULL,
  `sucursal_id` bigint(20) unsigned DEFAULT NULL,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `numero_recepcion` varchar(80) NOT NULL,
  `fecha_recepcion` datetime NOT NULL,
  `estado` enum('recibida','anulada') NOT NULL DEFAULT 'recibida',
  `observacion` text DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_recepcion_numero` (`empresa_id`,`numero_recepcion`),
  KEY `idx_recepcion_orden` (`empresa_id`,`orden_compra_id`),
  KEY `fk_recepciones_orden` (`orden_compra_id`),
  KEY `fk_recepciones_sucursal` (`sucursal_id`),
  KEY `fk_recepciones_usuario` (`usuario_id`),
  CONSTRAINT `fk_recepciones_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_recepciones_orden` FOREIGN KEY (`orden_compra_id`) REFERENCES `ordenes_compra` (`id`),
  CONSTRAINT `fk_recepciones_sucursal` FOREIGN KEY (`sucursal_id`) REFERENCES `sucursales` (`id`),
  CONSTRAINT `fk_recepciones_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `recepcion_compra_detalles` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `recepcion_id` bigint(20) unsigned NOT NULL,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `orden_detalle_id` bigint(20) unsigned NOT NULL,
  `producto_id` bigint(20) unsigned NOT NULL,
  `variante_id` bigint(20) unsigned DEFAULT NULL,
  `cantidad` decimal(12,2) NOT NULL,
  `costo_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 `idx_recepcion_detalle_recepcion` (`recepcion_id`,`empresa_id`),
  KEY `fk_recepcion_detalle_empresa` (`empresa_id`),
  KEY `fk_recepcion_detalle_orden` (`orden_detalle_id`),
  KEY `fk_recepcion_detalle_producto` (`producto_id`),
  KEY `fk_recepcion_detalle_variante` (`variante_id`),
  CONSTRAINT `fk_recepcion_detalle_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_recepcion_detalle_orden` FOREIGN KEY (`orden_detalle_id`) REFERENCES `orden_compra_detalles` (`id`),
  CONSTRAINT `fk_recepcion_detalle_producto` FOREIGN KEY (`producto_id`) REFERENCES `productos` (`id`),
  CONSTRAINT `fk_recepcion_detalle_recepcion` FOREIGN KEY (`recepcion_id`) REFERENCES `recepciones_compra` (`id`),
  CONSTRAINT `fk_recepcion_detalle_variante` FOREIGN KEY (`variante_id`) REFERENCES `producto_variantes` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `compras` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `orden_compra_id` bigint(20) unsigned DEFAULT NULL,
  `recepcion_compra_id` bigint(20) unsigned DEFAULT NULL,
  `proveedor_id` bigint(20) unsigned NOT NULL,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `fecha` datetime 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','recibida','anulada') NOT NULL DEFAULT 'recibida',
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_compras_proveedor` (`proveedor_id`),
  KEY `fk_compras_usuario` (`usuario_id`),
  KEY `idx_compras_orden` (`empresa_id`,`orden_compra_id`),
  CONSTRAINT `fk_compras_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_compras_proveedor` FOREIGN KEY (`proveedor_id`) REFERENCES `proveedores` (`id`),
  CONSTRAINT `fk_compras_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `compra_detalles` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `compra_id` bigint(20) unsigned NOT NULL,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `producto_id` bigint(20) unsigned NOT NULL,
  `variante_id` bigint(20) unsigned DEFAULT NULL,
  `cantidad` decimal(12,2) NOT NULL,
  `costo_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_compra_detalle_compra` (`compra_id`),
  KEY `fk_compra_detalle_empresa` (`empresa_id`),
  KEY `fk_compra_detalle_producto` (`producto_id`),
  KEY `fk_compra_detalle_variante` (`variante_id`),
  CONSTRAINT `fk_compra_detalle_compra` FOREIGN KEY (`compra_id`) REFERENCES `compras` (`id`),
  CONSTRAINT `fk_compra_detalle_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_compra_detalle_producto` FOREIGN KEY (`producto_id`) REFERENCES `productos` (`id`),
  CONSTRAINT `fk_compra_detalle_variante` FOREIGN KEY (`variante_id`) REFERENCES `producto_variantes` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `pagos_compra` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `compra_id` bigint(20) unsigned NOT NULL,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `monto` decimal(12,2) NOT NULL,
  `fecha_pago` datetime NOT NULL,
  `metodo_pago` varchar(50) NOT NULL,
  `referencia` varchar(120) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_pagos_compra_compra` (`compra_id`),
  KEY `fk_pagos_compra_empresa` (`empresa_id`),
  CONSTRAINT `fk_pagos_compra_compra` FOREIGN KEY (`compra_id`) REFERENCES `compras` (`id`),
  CONSTRAINT `fk_pagos_compra_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
