--
-- Host: localhost    Database: cierre
-- ------------------------------------------------------


--
-- Table structure for table `audit_log`
--

CREATE TABLE `audit_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `cierre_id` int(11) DEFAULT NULL,
  `action` varchar(80) COLLATE utf8mb4_unicode_ci NOT NULL,
  `details` text COLLATE utf8mb4_unicode_ci,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_audit_user` (`user_id`),
  KEY `idx_audit_cierre` (`cierre_id`),
  CONSTRAINT `fk_audit_cierre` FOREIGN KEY (`cierre_id`) REFERENCES `cierres` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_audit_user` FOREIGN KEY (`user_id`) REFERENCES `app_users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `audit_log`
--

INSERT INTO `audit_log` VALUES (1,1,22,'extra_add','{\"nombre\":\"fghgf\",\"monto\":62,\"moneda\":\"USD\",\"usd\":62}','2026-01-17 18:03:48'),(2,1,22,'cierre_save','{\"estado\":\"Revision\",\"diff\":62.67}','2026-01-17 18:04:14'),(3,1,NULL,'cierre_delete','{\"fecha\":\"2026-01-17\",\"caja\":\"caja2\"}','2026-01-17 18:05:34');

--
-- Table structure for table `cierre_detalle`
--

CREATE TABLE `cierre_detalle` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cierre_id` int(11) NOT NULL,
  `tipo_pago` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `monto_sistema_usd` decimal(14,2) NOT NULL,
  `monto_contado` decimal(14,2) NOT NULL DEFAULT '0.00',
  `moneda` enum('USD','BS','0') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'USD',
  `monto_contado_usd` decimal(14,2) NOT NULL DEFAULT '0.00',
  `diferencia_usd` decimal(14,2) NOT NULL DEFAULT '0.00',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `es_adicional` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `idx_cierre` (`cierre_id`),
  CONSTRAINT `fk_cierre_detalle` FOREIGN KEY (`cierre_id`) REFERENCES `cierres` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=78 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `cierre_detalle`
--

INSERT INTO `cierre_detalle` VALUES (9,4,'Banesco',73.88,24857.66,'BS',73.88,0.00,'2026-01-17 13:12:05',0),(10,4,'Otros',120.00,120.00,'USD',120.00,0.00,'2026-01-17 13:12:05',0),(11,5,'Banesco',768.64,253943.29,'BS',768.64,0.00,'2026-01-17 13:13:41',0),(12,5,'Otros',20.00,20.00,'USD',20.00,0.00,'2026-01-17 13:13:41',0),(13,6,'Banesco',140.08,46279.63,'BS',140.08,0.00,'2026-01-17 13:15:02',0),(14,6,'Otros',95.00,95.00,'USD',95.00,0.00,'2026-01-17 13:15:02',0),(15,6,'PM Venezuela',33.00,10902.54,'BS',33.00,0.00,'2026-01-17 13:15:02',0),(16,7,'Banesco',248.64,82146.68,'BS',248.64,0.00,'2026-01-17 13:16:46',0),(17,7,'Efectivo!',0.00,0.00,'USD',0.00,0.00,'2026-01-17 13:16:46',0),(18,7,'Línea de crédito',32.00,32.00,'USD',32.00,0.00,'2026-01-17 13:16:47',0),(19,7,'Otros',45.00,45.00,'USD',45.00,0.00,'2026-01-17 13:16:47',0),(20,7,'Venezuela',7.40,2444.82,'BS',7.40,0.00,'2026-01-17 13:16:47',0),(21,8,'Banesco',519.00,0.00,'BS',0.00,-519.00,'2026-01-17 13:19:43',0),(22,8,'Efectivo!',0.00,168139.54,'BS',516.73,516.73,'2026-01-17 13:19:43',0),(23,9,'Banesco',127.76,41571.80,'BS',127.76,0.00,'2026-01-17 13:23:37',0),(24,10,'Banesco',209.60,69247.65,'BS',209.60,0.00,'2026-01-17 13:24:57',0),(25,10,'Binance',60.00,60.00,'USD',60.00,0.00,'2026-01-17 13:24:57',0),(26,10,'Otros',125.00,125.00,'USD',125.00,0.00,'2026-01-17 13:24:57',0),(27,10,'PM Venezuela',164.90,52497.00,'BS',158.90,-6.00,'2026-01-17 13:24:57',0),(28,10,'Venezuela',57.40,18963.81,'BS',57.40,0.00,'2026-01-17 13:24:57',0),(29,11,'Banesco',152.00,40367.65,'BS',131.00,-21.00,'2026-01-17 13:26:58',0),(30,11,'Efectivo!',3.00,920.00,'BS',2.99,-0.01,'2026-01-17 13:26:58',0),(31,11,'Línea de crédito',60.00,60.00,'USD',60.00,0.00,'2026-01-17 13:26:58',0),(32,11,'Otros',85.00,85.00,'USD',85.00,0.00,'2026-01-17 13:26:58',0),(33,11,'PM Venezuela',7.00,8628.55,'BS',28.00,21.00,'2026-01-17 13:26:58',0),(34,11,'Venezuela',49.00,15099.35,'BS',49.00,0.00,'2026-01-17 13:26:58',0),(35,12,'Banesco',41.80,12880.67,'BS',41.80,0.00,'2026-01-17 13:29:11',0),(36,12,'PM Venezuela',329.00,101381.95,'BS',329.00,0.00,'2026-01-17 13:29:11',0),(37,13,'Banesco',60.00,18280.82,'BS',60.01,0.01,'2026-01-17 13:30:10',0),(38,13,'PM Venezuela',84.50,25751.81,'BS',84.54,0.04,'2026-01-17 13:30:10',0),(39,14,'Banesco',257.16,80201.59,'BS',257.16,0.00,'2026-01-17 13:31:38',0),(40,14,'Efectivo!',7.94,2480.00,'BS',7.95,0.01,'2026-01-17 13:31:38',0),(41,14,'Línea de crédito',145.24,145.24,'USD',145.24,0.00,'2026-01-17 13:31:38',0),(42,14,'PM Venezuela',200.06,62395.84,'BS',200.06,0.00,'2026-01-17 13:31:38',0),(43,15,'Banesco',539.36,168216.60,'BS',539.36,0.00,'2026-01-17 13:33:17',0),(44,15,'PM Venezuela',129.00,40232.28,'BS',129.00,0.00,'2026-01-17 13:33:17',0),(45,15,'Venezuela',58.80,18338.54,'BS',58.80,0.00,'2026-01-17 13:33:17',0),(46,16,'Banesco',19.60,6292.19,'BS',19.60,0.00,'2026-01-17 13:35:28',0),(47,16,'Efectivo!',154.00,0.00,'BS',0.00,-154.00,'2026-01-17 13:35:28',0),(48,16,'PM Venezuela',253.48,131234.22,'BS',408.79,155.31,'2026-01-17 13:35:28',0),(49,16,'Venezuela',29.40,9436.28,'BS',29.39,-0.01,'2026-01-17 13:35:28',0),(50,17,'Banesco',525.76,173700.59,'BS',525.76,0.00,'2026-01-17 13:38:10',0),(51,17,'PM Venezuela',19.60,6475.46,'BS',19.60,0.00,'2026-01-17 13:38:10',0),(52,18,'Banesco',19.49,6257.51,'BS',19.49,0.00,'2026-01-17 13:39:16',0),(53,18,'Efectivo!',12.51,4020.00,'BS',12.52,0.01,'2026-01-17 13:39:16',0),(54,18,'Línea de crédito',41.00,41.00,'USD',41.00,0.00,'2026-01-17 13:39:16',0),(55,18,'PM Venezuela',125.00,40131.37,'BS',125.01,0.01,'2026-01-17 13:39:16',0),(56,19,'Banesco',279.14,75421.86,'BS',224.16,-54.98,'2026-01-17 13:40:39',0),(57,19,'Otros',125.00,125.00,'USD',125.00,0.00,'2026-01-17 13:40:39',0),(58,19,'PM Venezuela',23.52,26418.49,'BS',78.52,55.00,'2026-01-17 13:40:39',0),(59,21,'Banesco',204.29,59107.90,'BS',194.04,-10.25,'2026-01-17 13:49:34',0),(60,21,'Efectivo!',0.00,0.00,'BS',0.00,0.00,'2026-01-17 13:49:34',0),(61,21,'Otros',190.00,190.00,'USD',190.00,0.00,'2026-01-17 13:49:34',0),(62,21,'PM Venezuela',181.00,55147.56,'BS',181.04,0.04,'2026-01-17 13:49:34',0),(63,21,'Venezuela',29.00,8835.72,'BS',29.01,0.01,'2026-01-17 13:49:34',0),(64,22,'Banesco',4644.65,2340816.00,'BS',7202.51,2557.86,'2026-01-17 17:09:48',0),(65,22,'Efectivo!',111.94,33090.00,'BS',101.82,-10.12,'2026-01-17 17:09:48',0),(66,22,'Línea de crédito',46.00,0.00,'0',0.00,0.00,'2026-01-17 17:09:48',0),(67,22,'Otros',1792.00,1772.00,'USD',1772.00,-20.00,'2026-01-17 17:09:48',0),(68,22,'Pago Movil',161.14,40000.00,'BS',123.08,-38.06,'2026-01-17 17:09:48',0),(69,22,'PM Venezuela',2834.53,890000.00,'BS',2738.46,-96.07,'2026-01-17 17:09:48',0),(70,22,'Transferencia',200.00,59628.00,'BS',183.47,-16.53,'2026-01-17 17:09:48',0),(71,22,'Venezuela',137.56,42299.80,'BS',130.15,-7.41,'2026-01-17 17:09:48',0),(72,22,'zinli',0.00,-2369.00,'USD',-2369.00,-2369.00,'2026-01-17 17:09:48',1),(77,22,'fghgf',0.00,62.00,'USD',62.00,62.00,'2026-01-17 18:03:48',1);

--
-- Table structure for table `cierres`
--

CREATE TABLE `cierres` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fecha` date NOT NULL,
  `caja_usuario` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `bolsa_manual` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `cambio_tasa` decimal(12,4) NOT NULL COMMENT 'Bs por 1 USD',
  `notas` text COLLATE utf8mb4_unicode_ci,
  `created_by` int(11) DEFAULT NULL,
  `completed_by` int(11) DEFAULT NULL,
  `completed_at` datetime DEFAULT NULL,
  `total_sistema_usd` decimal(14,2) NOT NULL DEFAULT '0.00',
  `total_contado_usd` decimal(14,2) NOT NULL DEFAULT '0.00',
  `diferencia_total_usd` decimal(14,2) NOT NULL DEFAULT '0.00',
  `completo` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `estado` enum('Pendiente','Completado','Revision','CompletadoRevisado') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Pendiente',
  `reviewed_by` int(11) DEFAULT NULL,
  `reviewed_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_cierre` (`fecha`,`caja_usuario`,`bolsa_manual`),
  KEY `idx_created_by` (`created_by`),
  KEY `idx_completed_by` (`completed_by`),
  KEY `idx_reviewed_by` (`reviewed_by`),
  CONSTRAINT `fk_cierres_completed_by` FOREIGN KEY (`completed_by`) REFERENCES `app_users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_cierres_created_by` FOREIGN KEY (`created_by`) REFERENCES `app_users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_cierres_reviewed_by` FOREIGN KEY (`reviewed_by`) REFERENCES `app_users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `cierres`
--

INSERT INTO `cierres` VALUES (4,'2026-01-14','caja2','286',336.4600,'',1,1,'2026-01-17 17:04:52',193.88,193.88,0.00,1,'2026-01-17 13:12:05','2026-01-17 18:07:24','Completado',1,'2026-01-17 18:07:14'),(5,'2026-01-13','caja2','285',330.3800,'',1,1,'2026-01-17 17:05:04',788.64,788.64,0.00,1,'2026-01-17 13:13:41','2026-01-17 17:05:04','Completado',NULL,NULL),(6,'2026-01-13','caja1','284',330.3800,'',1,1,'2026-01-17 17:04:57',268.08,268.08,0.00,1,'2026-01-17 13:15:02','2026-01-17 17:04:57','Completado',NULL,NULL),(7,'2026-01-12','caja1','283',330.3800,'',1,1,'2026-01-17 17:05:18',333.04,333.04,0.00,1,'2026-01-17 13:16:46','2026-01-17 17:05:18','Completado',NULL,NULL),(8,'2026-01-09','caja1','311',325.3900,'la fexha que puso en la bolsa es 09-07-2026\r\nno hay efectivo eso fue punto de venezuela 168.139.54\r\n158139,59',1,1,'2026-01-17 17:06:22',519.00,516.73,-2.27,1,'2026-01-17 13:19:43','2026-01-17 17:06:22','Completado',NULL,NULL),(9,'2026-01-09','caja2','310',325.3900,'',1,1,'2026-01-17 17:05:28',127.76,127.76,0.00,1,'2026-01-17 13:23:37','2026-01-17 17:05:28','Completado',NULL,NULL),(10,'2026-01-10','caja1','299',330.3800,'',1,1,'2026-01-17 14:05:18',616.90,610.90,-6.00,1,'2026-01-17 13:24:56','2026-01-17 14:05:18','Pendiente',NULL,NULL),(11,'2026-01-06','caja1','304',308.1500,'',1,1,'2026-01-17 17:04:36',356.00,355.99,-0.01,1,'2026-01-17 13:26:58','2026-01-17 17:04:36','Completado',NULL,NULL),(12,'2026-01-06','caja2','305',308.1500,'',1,1,'2026-01-17 17:05:50',370.80,370.80,0.00,1,'2026-01-17 13:29:11','2026-01-17 17:05:50','Completado',NULL,NULL),(13,'2026-01-05','caja2','302',304.6200,'',1,1,'2026-01-17 18:07:06',144.50,144.55,0.05,1,'2026-01-17 13:30:10','2026-01-17 18:07:06','Completado',NULL,NULL),(14,'2026-01-07','caja2','306',311.8800,'',1,1,'2026-01-17 17:05:55',610.40,610.41,0.01,1,'2026-01-17 13:31:37','2026-01-17 17:05:55','Completado',NULL,NULL),(15,'2026-01-07','caja1','307',311.8800,'',1,1,'2026-01-17 17:05:42',727.16,727.16,0.00,1,'2026-01-17 13:33:17','2026-01-17 17:05:42','Completado',NULL,NULL),(16,'2026-01-08','caja1','309',321.0300,'',1,1,'2026-01-17 17:06:14',456.48,457.78,1.30,1,'2026-01-17 13:35:28','2026-01-17 17:06:14','Completado',NULL,NULL),(17,'2026-01-12','caja2','282',330.3800,'',1,1,'2026-01-17 17:05:12',545.36,545.36,0.00,1,'2026-01-17 13:38:09','2026-01-17 17:05:12','Completado',NULL,NULL),(18,'2026-01-08','caja2','308',321.0300,'',1,1,'2026-01-17 17:05:34',198.00,198.02,0.02,1,'2026-01-17 13:39:16','2026-01-17 17:05:34','Completado',NULL,NULL),(19,'2026-01-14','caja1','287',336.4600,'',1,1,'2026-01-17 17:04:43',427.66,427.68,0.02,1,'2026-01-17 13:40:38','2026-01-17 17:04:43','Completado',NULL,NULL),(21,'2026-01-05','caja1','303',304.6200,'en la bolsa puso 03-01-2026',1,1,'2026-01-17 13:50:28',604.29,594.09,-10.20,1,'2026-01-17 13:49:34','2026-01-17 14:02:44','Pendiente',NULL,NULL),(22,'2025-12-31','caja1','68168',325.0000,'',1,NULL,NULL,9881.82,9944.49,62.67,0,'2026-01-17 17:09:48','2026-01-17 18:03:48','Revision',NULL,NULL),(24,'2026-01-18','caja1','466',325.0000,'',3,NULL,NULL,0.00,0.00,0.00,0,'2026-01-18 20:50:11',NULL,'Pendiente',NULL,NULL);

--
-- Table structure for table `config_cajas`
--

CREATE TABLE `config_cajas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pos_username` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `alias` varchar(80) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `activo` tinyint(1) NOT NULL DEFAULT '1',
  `orden` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `pos_username` (`pos_username`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `config_cajas`
--

INSERT INTO `config_cajas` VALUES (1,'caja1','Caja Alfa',1,1),(2,'caja2','Caja Beta',1,2);

--
-- Table structure for table `config_general`
--

CREATE TABLE `config_general` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `margen_error_usd` decimal(10,2) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `config_general`
--

INSERT INTO `config_general` VALUES (1,5.00);

--
-- Table structure for table `config_pago`
--

CREATE TABLE `config_pago` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nombre` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `moneda_base` enum('USD','BS') COLLATE utf8mb4_unicode_ci NOT NULL,
  `activo` tinyint(1) NOT NULL DEFAULT '1',
  `orden` int(11) NOT NULL DEFAULT '0',
  `contar_en_total` tinyint(1) NOT NULL DEFAULT '1',
  `source` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'manual',
  `last_seen_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `config_pago`
--

INSERT INTO `config_pago` VALUES (1,'Store Account','USD',1,888,1,'manual',NULL),(2,'Otros','USD',1,10,1,'manual',NULL),(3,'Banesco','BS',1,30,1,'manual',NULL),(4,'Efectivo','BS',1,20,1,'manual',NULL),(5,'PM Venezuela','BS',1,50,1,'manual',NULL);

--
-- Dumping events for database 'cierre'
--

--
-- Dumping routines for database 'cierre'
--


