From e6c50a20339fcba86124f7d33cfd15cc7f0f4c3c Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 22 Jun 2021 16:23:39 +0200 Subject: [PATCH] Updated structure --- db/dump/structure.sql | 2796 +++++++++++++++++++++++++++++++++++++++- db/export-structure.sh | 1 + 2 files changed, 2796 insertions(+), 1 deletion(-) diff --git a/db/dump/structure.sql b/db/dump/structure.sql index c07919155..b415e1d99 100644 --- a/db/dump/structure.sql +++ b/db/dump/structure.sql @@ -6681,6 +6681,2585 @@ DELIMITER ; /*!50003 SET character_set_results = @saved_cs_results */ ; /*!50003 SET collation_connection = @saved_col_connection */ ; +-- +-- Current Database: `bi` +-- + +CREATE DATABASE /*!32312 IF NOT EXISTS*/ `bi` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */; + +USE `bi`; + +-- +-- Table structure for table `Equalizator` +-- + +DROP TABLE IF EXISTS `Equalizator`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `Equalizator` ( + `Vista` int(11) NOT NULL, + `Pedido` int(11) DEFAULT NULL, + `Impreso` int(11) DEFAULT NULL, + `Encajado` int(11) DEFAULT NULL, + PRIMARY KEY (`Vista`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `Greuge_Evolution` +-- + +DROP TABLE IF EXISTS `Greuge_Evolution`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `Greuge_Evolution` ( + `Id_Cliente` int(11) NOT NULL, + `Fecha` date NOT NULL, + `Greuge` decimal(10,2) NOT NULL DEFAULT '0.00', + `Ventas` decimal(10,2) NOT NULL DEFAULT '0.00', + `Fosil` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT 'greuge fósil, correspondiente a los clientes muertos', + `Recobro` decimal(10,2) NOT NULL DEFAULT '0.00', + PRIMARY KEY (`Id_Cliente`,`Fecha`), + KEY `greuge_evolution_idx1` (`Fecha`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Almacenamos la evolucion del greuge de los ultimos dias '; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `Greuge_comercial_recobro` +-- + +DROP TABLE IF EXISTS `Greuge_comercial_recobro`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `Greuge_comercial_recobro` ( + `Id_Trabajador` int(11) NOT NULL, + `recobro` decimal(10,2) NOT NULL DEFAULT '0.00', + `peso_cartera` decimal(10,2) NOT NULL DEFAULT '0.00', + PRIMARY KEY (`Id_Trabajador`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `Greuges_comercial_detail` +-- + +DROP TABLE IF EXISTS `Greuges_comercial_detail`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `Greuges_comercial_detail` ( + `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Id_Trabajador` int(10) unsigned NOT NULL, + `Comentario` varchar(45) COLLATE utf8_unicode_ci NOT NULL, + `Importe` decimal(10,2) NOT NULL, + `Fecha` datetime DEFAULT NULL, + PRIMARY KEY (`Id`) +) ENGINE=InnoDBDEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPACT; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `Last_buy_id__` +-- + +DROP TABLE IF EXISTS `Last_buy_id__`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `Last_buy_id__` ( + `Id_Article` int(11) NOT NULL DEFAULT '90', + `Id_Compra` int(11) NOT NULL DEFAULT '0', + `warehouse_id` smallint(6) unsigned NOT NULL, + PRIMARY KEY (`warehouse_id`,`Id_Article`), + UNIQUE KEY `Id_Compra_UNIQUE` (`Id_Compra`), + CONSTRAINT `Id_CompraFK` FOREIGN KEY (`Id_Compra`) REFERENCES `vn`.`buy` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `Ticket_Portes` +-- + +DROP TABLE IF EXISTS `Ticket_Portes`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `Ticket_Portes` ( + `Id_Ticket` int(11) NOT NULL, + `rate` tinyint(4) NOT NULL COMMENT 'Tarifa', + `real_amount` double NOT NULL COMMENT 'Cantidad pactada con la agencia', + `payed_amount` double NOT NULL COMMENT 'Cantidad reflejada en el Ticket', + PRIMARY KEY (`Id_Ticket`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `XDiario_ALL` +-- + +DROP TABLE IF EXISTS `XDiario_ALL`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `XDiario_ALL` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `empresa_id` int(5) NOT NULL, + `SUBCTA` varchar(11) CHARACTER SET utf8 NOT NULL, + `Eurodebe` double DEFAULT NULL, + `Eurohaber` double DEFAULT NULL, + `Fecha` date DEFAULT NULL, + `FECHA_EX` date DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `Cuenta` (`SUBCTA`), + KEY `empresa` (`empresa_id`), + KEY `Fecha` (`Fecha`) +) ENGINE=InnoDBDEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `analisis_grafico_simple` +-- + +DROP TABLE IF EXISTS `analisis_grafico_simple`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `analisis_grafico_simple` ( + `Año` smallint(5) unsigned NOT NULL, + `Semana` tinyint(3) unsigned NOT NULL, + `Importe` double DEFAULT NULL, + UNIQUE KEY `Año` (`Año`,`Semana`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `analisis_grafico_ventas` +-- + +DROP TABLE IF EXISTS `analisis_grafico_ventas`; +/*!50001 DROP VIEW IF EXISTS `analisis_grafico_ventas`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `analisis_grafico_ventas` ( + `Año` tinyint NOT NULL, + `Semana` tinyint NOT NULL, + `Importe` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Table structure for table `analisis_ventas` +-- + +DROP TABLE IF EXISTS `analisis_ventas`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `analisis_ventas` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Familia` varchar(30) COLLATE utf8_unicode_ci NOT NULL, + `Reino` varchar(45) COLLATE utf8_unicode_ci NOT NULL, + `Comercial` varchar(3) COLLATE utf8_unicode_ci NOT NULL, + `Comprador` varchar(3) COLLATE utf8_unicode_ci NOT NULL, + `Provincia` varchar(30) COLLATE utf8_unicode_ci NOT NULL, + `almacen` varchar(20) COLLATE utf8_unicode_ci NOT NULL, + `Año` smallint(5) unsigned NOT NULL, + `Mes` tinyint(3) unsigned NOT NULL, + `Semana` tinyint(3) unsigned NOT NULL, + `Vista` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, + `Importe` double NOT NULL, + PRIMARY KEY (`id`), + KEY `Año` (`Año`,`Semana`) +) ENGINE=InnoDBDEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `analisis_ventas_almacen_evolution` +-- + +DROP TABLE IF EXISTS `analisis_ventas_almacen_evolution`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `analisis_ventas_almacen_evolution` ( + `Semana` int(11) NOT NULL, + `Almacen` varchar(20) COLLATE utf8_unicode_ci NOT NULL, + `Ventas` int(11) NOT NULL, + `Año` int(11) NOT NULL, + `Periodo` int(11) NOT NULL, + UNIQUE KEY `Almacen` (`Almacen`,`Periodo`), + KEY `Periodo` (`Periodo`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `analisis_ventas_comprador_evolution` +-- + +DROP TABLE IF EXISTS `analisis_ventas_comprador_evolution`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `analisis_ventas_comprador_evolution` ( + `semana` int(11) NOT NULL, + `comprador` varchar(3) COLLATE utf8_unicode_ci NOT NULL, + `ventas` int(11) NOT NULL, + `año` int(11) NOT NULL, + `periodo` int(11) NOT NULL, + UNIQUE KEY `comprador` (`comprador`,`periodo`), + KEY `periodo` (`periodo`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `analisis_ventas_familia_evolution` +-- + +DROP TABLE IF EXISTS `analisis_ventas_familia_evolution`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `analisis_ventas_familia_evolution` ( + `semana` int(11) NOT NULL, + `familia` varchar(50) COLLATE utf8_unicode_ci NOT NULL, + `ventas` int(11) NOT NULL, + `año` int(11) NOT NULL, + `periodo` int(11) NOT NULL, + UNIQUE KEY `familia` (`familia`,`periodo`), + KEY `periodo` (`periodo`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `analisis_ventas_provincia_evolution` +-- + +DROP TABLE IF EXISTS `analisis_ventas_provincia_evolution`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `analisis_ventas_provincia_evolution` ( + `semana` int(11) NOT NULL, + `provincia` varchar(30) COLLATE utf8_unicode_ci NOT NULL, + `ventas` int(11) NOT NULL, + `año` int(11) NOT NULL, + `periodo` int(11) NOT NULL, + `countryCode` varchar(2) COLLATE utf8_unicode_ci NOT NULL, + UNIQUE KEY `provincia` (`provincia`,`periodo`), + KEY `periodo` (`periodo`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `analisis_ventas_reino_evolution` +-- + +DROP TABLE IF EXISTS `analisis_ventas_reino_evolution`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `analisis_ventas_reino_evolution` ( + `semana` int(11) NOT NULL, + `reino` varchar(20) COLLATE utf8_unicode_ci NOT NULL, + `ventas` int(11) NOT NULL, + `año` int(11) NOT NULL, + `periodo` int(11) NOT NULL, + UNIQUE KEY `reino` (`reino`,`periodo`), + KEY `periodo` (`periodo`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `analisis_ventas_simple` +-- + +DROP TABLE IF EXISTS `analisis_ventas_simple`; +/*!50001 DROP VIEW IF EXISTS `analisis_ventas_simple`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `analisis_ventas_simple` ( + `Año` tinyint NOT NULL, + `Semana` tinyint NOT NULL, + `Importe` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Table structure for table `analisis_ventas_vendedor_evolution` +-- + +DROP TABLE IF EXISTS `analisis_ventas_vendedor_evolution`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `analisis_ventas_vendedor_evolution` ( + `semana` int(11) NOT NULL, + `vendedor` varchar(3) COLLATE utf8_unicode_ci NOT NULL, + `ventas` int(11) NOT NULL, + `año` int(11) NOT NULL, + `periodo` int(11) NOT NULL, + UNIQUE KEY `vendedor` (`vendedor`,`periodo`), + KEY `periodo` (`periodo`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `analisis_ventas_vista_evolution` +-- + +DROP TABLE IF EXISTS `analisis_ventas_vista_evolution`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `analisis_ventas_vista_evolution` ( + `semana` int(11) NOT NULL, + `vista` varchar(45) COLLATE utf8_unicode_ci NOT NULL, + `ventas` int(11) NOT NULL, + `año` int(11) NOT NULL, + `periodo` int(11) NOT NULL, + UNIQUE KEY `vista` (`vista`,`periodo`), + KEY `periodo` (`periodo`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `calidad_detalle` +-- + +DROP TABLE IF EXISTS `calidad_detalle`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `calidad_detalle` ( + `Id_Cliente` int(11) NOT NULL, + `calidad_parametros_id` int(2) NOT NULL, + `valor` int(3) DEFAULT NULL, + PRIMARY KEY (`Id_Cliente`,`calidad_parametros_id`), + KEY `calidad_parametros_detalle_idx` (`calidad_parametros_id`), + CONSTRAINT `calidad_parametros_detalle` FOREIGN KEY (`calidad_parametros_id`) REFERENCES `calidad_parametros` (`calidad_parametros_id`) ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `calidad_parametros` +-- + +DROP TABLE IF EXISTS `calidad_parametros`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `calidad_parametros` ( + `calidad_parametros_id` int(2) NOT NULL, + `descripcion` varchar(45) COLLATE utf8_unicode_ci NOT NULL, + PRIMARY KEY (`calidad_parametros_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `claims_ratio` +-- + +DROP TABLE IF EXISTS `claims_ratio`; +/*!50001 DROP VIEW IF EXISTS `claims_ratio`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `claims_ratio` ( + `Id_Cliente` tinyint NOT NULL, + `Consumo` tinyint NOT NULL, + `Reclamaciones` tinyint NOT NULL, + `Ratio` tinyint NOT NULL, + `recobro` tinyint NOT NULL, + `inflacion` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Table structure for table `customerDebtInventory` +-- + +DROP TABLE IF EXISTS `customerDebtInventory`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `customerDebtInventory` ( + `Id_Cliente` int(11) NOT NULL, + `Debt` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT 'CREATE TABLE bi.customerDebtInventory\n\nSELECT Id_Cliente, sum(Euros) as Debt\n\nFROM \n(\nSELECT Id_Cliente, Entregado as Euros\n\nFROM Recibos \n\nWHERE Fechacobro < ''2017-01-01\n''\nUNION ALL\n\nSELECT Id_Cliente, - Importe \nFROM Facturas\nWHERE Fecha < ''2017-01-01''\n) sub \nGROUP BY Id_Cliente', + PRIMARY KEY (`Id_Cliente`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `customerRiskOverdue` +-- + +DROP TABLE IF EXISTS `customerRiskOverdue`; +/*!50001 DROP VIEW IF EXISTS `customerRiskOverdue`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `customerRiskOverdue` ( + `customer_id` tinyint NOT NULL, + `amount` tinyint NOT NULL, + `company_id` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Temporary table structure for view `customer_risk` +-- + +DROP TABLE IF EXISTS `customer_risk`; +/*!50001 DROP VIEW IF EXISTS `customer_risk`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `customer_risk` ( + `customer_id` tinyint NOT NULL, + `company_id` tinyint NOT NULL, + `amount` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Temporary table structure for view `defaulters` +-- + +DROP TABLE IF EXISTS `defaulters`; +/*!50001 DROP VIEW IF EXISTS `defaulters`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `defaulters` ( + `client` tinyint NOT NULL, + `date` tinyint NOT NULL, + `amount` tinyint NOT NULL, + `defaulterSince` tinyint NOT NULL, + `hasChanged` tinyint NOT NULL, + `frozened` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Table structure for table `defaulting` +-- + +DROP TABLE IF EXISTS `defaulting`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `defaulting` ( + `date` date NOT NULL, + `amount` double NOT NULL, + PRIMARY KEY (`date`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `f_tvc` +-- + +DROP TABLE IF EXISTS `f_tvc`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `f_tvc` ( + `Id_Ticket` int(11) NOT NULL, + PRIMARY KEY (`Id_Ticket`), + CONSTRAINT `id_ticket_to_comisionantes` FOREIGN KEY (`Id_Ticket`) REFERENCES `vn`.`ticket` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Almacenamos la lista de tickets para agilizar la consulta. Corresponde a los clientes REAL y en los almacenes COMISIONANTES'; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `facturacion_media_anual` +-- + +DROP TABLE IF EXISTS `facturacion_media_anual`; +/*!50001 DROP VIEW IF EXISTS `facturacion_media_anual`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `facturacion_media_anual` ( + `Id_Cliente` tinyint NOT NULL, + `Consumo` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Table structure for table `lastaction__` +-- + +DROP TABLE IF EXISTS `lastaction__`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `lastaction__` ( + `Id_Cliente` int(11) unsigned NOT NULL, + `Cliente` varchar(50) COLLATE utf8_unicode_ci NOT NULL, + `Ultima_accion` date DEFAULT NULL, + `Comercial` varchar(3) CHARACTER SET utf8 DEFAULT NULL, + PRIMARY KEY (`Id_Cliente`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `live_counter` +-- + +DROP TABLE IF EXISTS `live_counter`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `live_counter` ( + `odbc_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `amount` double NOT NULL, + PRIMARY KEY (`odbc_date`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `partitioning_information` +-- + +DROP TABLE IF EXISTS `partitioning_information`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `partitioning_information` ( + `schema_name` varchar(10) CHARACTER SET utf8 NOT NULL, + `table_name` varchar(20) CHARACTER SET utf8 NOT NULL, + `date_field` varchar(20) CHARACTER SET utf8 DEFAULT NULL, + `table_depending` varchar(15) CHARACTER SET utf8 DEFAULT NULL, + `execution_order` tinyint(3) unsigned NOT NULL, + PRIMARY KEY (`schema_name`,`table_name`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `primer_pedido` +-- + +DROP TABLE IF EXISTS `primer_pedido`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `primer_pedido` ( + `Id_Cliente` int(11) NOT NULL, + `Id_Ticket` int(11) NOT NULL, + `month` tinyint(1) NOT NULL, + `year` smallint(2) NOT NULL, + `total` decimal(10,2) NOT NULL DEFAULT '0.00', + PRIMARY KEY (`Id_Cliente`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `rotacion` +-- + +DROP TABLE IF EXISTS `rotacion`; +/*!50001 DROP VIEW IF EXISTS `rotacion`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `rotacion` ( + `Id_Article` tinyint NOT NULL, + `warehouse_id` tinyint NOT NULL, + `total` tinyint NOT NULL, + `rotacion` tinyint NOT NULL, + `cm3` tinyint NOT NULL, + `almacenaje` tinyint NOT NULL, + `manipulacion` tinyint NOT NULL, + `auxiliar` tinyint NOT NULL, + `mermas` tinyint NOT NULL, + `cm3reparto` tinyint NOT NULL, + `grams` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Table structure for table `rutasBoard` +-- + +DROP TABLE IF EXISTS `rutasBoard`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `rutasBoard` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `Id_Ruta` int(10) unsigned NOT NULL DEFAULT '0', + `Id_Agencia` int(11) NOT NULL DEFAULT '0', + `km` bigint(10) NOT NULL DEFAULT '0', + `Dia` varchar(9) CHARACTER SET utf8 DEFAULT NULL, + `Fecha` date NOT NULL, + `Terceros` int(11) NOT NULL DEFAULT '0', + `Bultos` int(11) NOT NULL DEFAULT '0', + `Matricula` varchar(10) COLLATE utf8_unicode_ci, + `Tipo` varchar(1) CHARACTER SET utf8 NOT NULL DEFAULT '', + `year` int(4) NOT NULL, + `month` int(2) NOT NULL, + `warehouse_id` smallint(5) unsigned NOT NULL COMMENT 'A nulo si se puede enrutar desde todos los almacenes', + `coste_bulto` decimal(10,2) unsigned NOT NULL DEFAULT '0.00', + `teorico` decimal(10,2) NOT NULL DEFAULT '0.00', + `practico` decimal(10,2) NOT NULL DEFAULT '0.00', + `greuge` decimal(10,2) NOT NULL DEFAULT '0.00', + PRIMARY KEY (`id`), + UNIQUE KEY `rutasBoard_Ruta` (`Id_Ruta`), + KEY `rutasBoard_ix1` (`year`), + KEY `rutasBoard_ix2` (`month`), + KEY `rutasBoard_ix3` (`warehouse_id`) +) ENGINE=InnoDBDEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `saleVolume__` +-- + +DROP TABLE IF EXISTS `saleVolume__`; +/*!50001 DROP VIEW IF EXISTS `saleVolume__`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `saleVolume__` ( + `saleFk` tinyint NOT NULL, + `m3` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Temporary table structure for view `tarifa_componentes` +-- + +DROP TABLE IF EXISTS `tarifa_componentes`; +/*!50001 DROP VIEW IF EXISTS `tarifa_componentes`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `tarifa_componentes` ( + `Id_Componente` tinyint NOT NULL, + `Componente` tinyint NOT NULL, + `tarifa_componentes_series_id` tinyint NOT NULL, + `tarifa_class` tinyint NOT NULL, + `tax` tinyint NOT NULL, + `is_renewable` tinyint NOT NULL, + `code` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Temporary table structure for view `tarifa_componentes_series` +-- + +DROP TABLE IF EXISTS `tarifa_componentes_series`; +/*!50001 DROP VIEW IF EXISTS `tarifa_componentes_series`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `tarifa_componentes_series` ( + `tarifa_componentes_series_id` tinyint NOT NULL, + `Serie` tinyint NOT NULL, + `base` tinyint NOT NULL, + `margen` tinyint NOT NULL +) ENGINE=MyISAM */; +SET character_set_client = @saved_cs_client; + +-- +-- Table structure for table `tarifa_premisas` +-- + +DROP TABLE IF EXISTS `tarifa_premisas`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `tarifa_premisas` ( + `Id_Premisa` int(11) NOT NULL AUTO_INCREMENT, + `premisa` varchar(45) COLLATE utf8_unicode_ci NOT NULL, + PRIMARY KEY (`Id_Premisa`) +) ENGINE=InnoDBDEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `tarifa_warehouse` +-- + +DROP TABLE IF EXISTS `tarifa_warehouse`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `tarifa_warehouse` ( + `Id_Tarifa_Warehouse` int(11) NOT NULL AUTO_INCREMENT, + `warehouse_id` int(11) NOT NULL, + `Id_Premisa` int(11) NOT NULL, + `Valor` double NOT NULL, + PRIMARY KEY (`Id_Tarifa_Warehouse`) +) ENGINE=InnoDBDEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Almacena los valores de gasto por almacen'; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping events for database 'bi' +-- + +-- +-- Dumping routines for database 'bi' +-- +/*!50003 DROP FUNCTION IF EXISTS `nz` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = '' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` FUNCTION `nz`(dblCANTIDAD DOUBLE) RETURNS double +BEGIN + +DECLARE dblRESULT DOUBLE; + +SET dblRESULT = IFNULL(dblCANTIDAD,0); + +RETURN dblRESULT; + +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `analisis_ventas_evolution_add` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8mb4 */ ; +/*!50003 SET character_set_results = utf8mb4 */ ; +/*!50003 SET collation_connection = utf8mb4_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `analisis_ventas_evolution_add`() +BEGIN + DECLARE vPreviousPeriod INT; + DECLARE vCurrentPeriod INT; + DECLARE vLastPeriod INT; + DECLARE vMinPeriod INT DEFAULT 201400; + DECLARE vMaxPeriod INT DEFAULT vn2008.vnperiod(CURDATE()); + + DECLARE vYear INT; + DECLARE vWeek INT; + + -- Almacen + + SET vCurrentPeriod = IFNULL(vLastPeriod, vMinPeriod); + + WHILE vCurrentPeriod < vMaxPeriod + DO + SELECT MAX(Periodo) INTO vPreviousPeriod + FROM bi.analisis_ventas_almacen_evolution + WHERE Periodo < vMaxPeriod; + + SELECT MIN(period) INTO vCurrentPeriod + FROM vn2008.time + WHERE period > vPreviousPeriod; + + SET vYear = FLOOR(vCurrentPeriod / 100); + SET vWeek = vCurrentPeriod - (vYear * 100); + + DELETE FROM bi.analisis_ventas_almacen_evolution + WHERE Periodo = vCurrentPeriod; + + REPLACE bi.analisis_ventas_almacen_evolution(Almacen, Ventas, Semana,Año, Periodo) + SELECT Almacen, sum(Ventas) AS Ventas, vWeek, vYear, vCurrentPeriod + FROM ( + SELECT almacen, sum(Importe) AS Ventas + FROM bi.analisis_ventas + WHERE vYear = Año + AND vWeek = Semana + GROUP BY almacen + UNION ALL + SELECT almacen, - sum(Importe) AS Ventas + FROM bi.analisis_ventas + WHERE vYear - 1 = Año + AND vWeek = Semana + GROUP BY almacen + UNION ALL + SELECT Almacen, Ventas + FROM bi.analisis_ventas_almacen_evolution + WHERE Periodo = vPreviousPeriod + ) sub + GROUP BY Almacen; + END WHILE; + + -- Reino + + SET vCurrentPeriod = vMinPeriod; + + WHILE vCurrentPeriod < vMaxPeriod + DO + SELECT MAX(periodo) INTO vPreviousPeriod + FROM bi.analisis_ventas_reino_evolution + WHERE periodo < vMaxPeriod; + + SELECT MIN(period) INTO vCurrentPeriod + FROM vn2008.time + WHERE period > vPreviousPeriod; + + SET vYear = FLOOR(vCurrentPeriod / 100); + SET vWeek = vCurrentPeriod - (vYear * 100); + + DELETE FROM bi.analisis_ventas_reino_evolution + WHERE Periodo = vCurrentPeriod; + + REPLACE bi.analisis_ventas_reino_evolution(reino, ventas, semana,año, periodo) + SELECT reino, sum(ventas) AS ventas, vWeek, vYear, vCurrentPeriod + FROM ( + SELECT Reino, sum(Importe) AS ventas + FROM bi.analisis_ventas + WHERE vYear = Año + AND vWeek = Semana + GROUP BY Reino + UNION ALL + SELECT Reino, - sum(Importe) AS ventas + FROM bi.analisis_ventas + WHERE vYear - 1 = Año + AND vWeek = Semana + GROUP BY Reino + UNION ALL + SELECT reino, ventas + FROM bi.analisis_ventas_reino_evolution + WHERE Periodo = vPreviousPeriod + ) sub + GROUP BY reino; + END WHILE; + + -- Familia + + SET vCurrentPeriod = vMinPeriod; + + WHILE vCurrentPeriod < vMaxPeriod + DO + SELECT MAX(periodo) INTO vPreviousPeriod + FROM bi.analisis_ventas_familia_evolution + WHERE periodo < vMaxPeriod; + + SELECT MIN(period) INTO vCurrentPeriod + FROM vn2008.time + WHERE period > vPreviousPeriod; + + SET vYear = FLOOR(vCurrentPeriod / 100); + SET vWeek = vCurrentPeriod - (vYear * 100); + + DELETE FROM bi.analisis_ventas_familia_evolution + WHERE Periodo = vCurrentPeriod; + + REPLACE bi.analisis_ventas_familia_evolution(familia, ventas, semana,año, periodo) + SELECT Familia, sum(ventas) AS ventas, vWeek, vYear, vCurrentPeriod + FROM ( + SELECT Familia, sum(Importe) AS ventas + FROM bi.analisis_ventas + WHERE vYear = Año + AND vWeek = Semana + GROUP BY familia + UNION ALL + SELECT Familia, - sum(Importe) AS ventas + FROM bi.analisis_ventas + WHERE vYear - 1 = Año + AND vWeek = Semana + GROUP BY familia + UNION ALL + SELECT familia, ventas + FROM bi.analisis_ventas_familia_evolution + WHERE Periodo = vPreviousPeriod + ) sub + GROUP BY Familia; + END WHILE; + + -- Comprador + -- FIXME: Bucle infinito porque la tabla está vacía +/* + SET vCurrentPeriod = vMinPeriod; + + WHILE vCurrentPeriod < vMaxPeriod + DO + SELECT IFNULL(MAX(periodo),vMinPeriod) INTO vPreviousPeriod + FROM bi.analisis_ventas_comprador_evolution + WHERE periodo < vMaxPeriod; + + SELECT MIN(period) INTO vCurrentPeriod + FROM vn2008.time + WHERE period > vPreviousPeriod; + + SET vYear = FLOOR(vCurrentPeriod / 100); + SET vWeek = vCurrentPeriod - (vYear * 100); + + DELETE FROM bi.analisis_ventas_comprador_evolution + WHERE Periodo = vCurrentPeriod; + + REPLACE bi.analisis_ventas_comprador_evolution(comprador, ventas, semana,año, periodo) + SELECT Comprador, sum(ventas) AS ventas, vWeek, vYear, vCurrentPeriod + FROM ( + SELECT Comprador, sum(Importe) AS ventas + FROM bi.analisis_ventas + WHERE vYear = Año + AND vWeek = Semana + GROUP BY Comprador + UNION ALL + SELECT Comprador, - sum(Importe) AS ventas + FROM bi.analisis_ventas + WHERE vYear - 1 = Año + AND vWeek = Semana + GROUP BY Comprador + UNION ALL + SELECT comprador, IFNULL(ventas,0) + FROM bi.analisis_ventas_comprador_evolution + WHERE Periodo = vPreviousPeriod + ) sub + GROUP BY Comprador; + END WHILE; +*/ + -- Provincia + + SET vCurrentPeriod = vMinPeriod; + + WHILE vCurrentPeriod < vMaxPeriod + DO + SELECT MAX(periodo) INTO vPreviousPeriod + FROM bi.analisis_ventas_provincia_evolution + WHERE periodo < vMaxPeriod; + + SELECT MIN(period) INTO vCurrentPeriod + FROM vn2008.time + WHERE period > vPreviousPeriod; + + SET vYear = FLOOR(vCurrentPeriod / 100); + SET vWeek = vCurrentPeriod - (vYear * 100); + + DELETE FROM bi.analisis_ventas_provincia_evolution + WHERE Periodo = vCurrentPeriod; + + REPLACE bi.analisis_ventas_provincia_evolution(provincia, countryCode, ventas, semana,año, periodo) + SELECT Provincia, code, SUM(ventas) AS ventas, vWeek, vYear, vCurrentPeriod + FROM ( + SELECT a.Provincia, c.code, SUM(a.Importe) AS ventas + FROM bi.analisis_ventas a + JOIN vn.province p ON p.name= a.provincia + JOIN vn.country c ON c.id = p.countryFk + WHERE vYear = a.Año + AND vWeek = a.Semana + GROUP BY a.Provincia + UNION ALL + SELECT a.Provincia, c.code, - sum(a.Importe) AS ventas + FROM bi.analisis_ventas a + JOIN vn.province p ON p.name= a.provincia + JOIN vn.country c ON c.id = p.countryFk + WHERE vYear - 1 = a.Año + AND vWeek = a.Semana + GROUP BY a.Provincia + UNION ALL + SELECT a.provincia,a.countryCode, a.ventas + FROM bi.analisis_ventas_provincia_evolution a + WHERE a.Periodo = vPreviousPeriod + ) sub + GROUP BY Provincia; + + + END WHILE; + + -- Vista + + SET vCurrentPeriod = vMinPeriod; + + WHILE vCurrentPeriod < vMaxPeriod + DO + SELECT MAX(periodo) INTO vPreviousPeriod + FROM bi.analisis_ventas_vista_evolution + WHERE periodo < vMaxPeriod; + + SELECT MIN(period) INTO vCurrentPeriod + FROM vn2008.time + WHERE period > vPreviousPeriod; + + SET vYear = FLOOR(vCurrentPeriod / 100); + SET vWeek = vCurrentPeriod - (vYear * 100); + + DELETE FROM bi.analisis_ventas_vista_evolution + WHERE Periodo = vCurrentPeriod; + + REPLACE bi.analisis_ventas_vista_evolution(vista, ventas, semana,año, periodo) + SELECT vista, sum(ventas) AS ventas, vWeek, vYear, vCurrentPeriod + FROM ( + SELECT Vista, sum(Importe) AS ventas + FROM bi.analisis_ventas + WHERE vYear = Año + AND vWeek = Semana + GROUP BY Vista + UNION ALL + SELECT Vista, - sum(Importe) AS ventas + FROM bi.analisis_ventas + WHERE vYear - 1 = Año + AND vWeek = Semana + GROUP BY Vista + UNION ALL + SELECT vista, ventas + FROM bi.analisis_ventas_vista_evolution + WHERE Periodo = vPreviousPeriod + ) sub + GROUP BY Vista; + END WHILE; + + -- Vendedor + + SET vCurrentPeriod = vMinPeriod; + + WHILE vCurrentPeriod < vMaxPeriod + DO + SELECT MAX(periodo) INTO vPreviousPeriod + FROM bi.analisis_ventas_vendedor_evolution + WHERE periodo < vMaxPeriod; + + SELECT MIN(period) INTO vCurrentPeriod + FROM vn2008.time + WHERE period > vPreviousPeriod; + + SET vYear = FLOOR(vCurrentPeriod / 100); + SET vWeek = vCurrentPeriod - (vYear * 100); + + DELETE FROM bi.analisis_ventas_vendedor_evolution + WHERE Periodo = vCurrentPeriod; + + REPLACE bi.analisis_ventas_vendedor_evolution(vendedor, ventas, semana,año, periodo) + SELECT Comercial AS vendedor, sum(ventas) AS ventas, vWeek, vYear, vCurrentPeriod + FROM ( + SELECT Comercial, sum(Importe) AS ventas + from bi.analisis_ventas + WHERE vYear = Año + AND vWeek = Semana + GROUP BY Comercial + UNION ALL + SELECT Comercial, - sum(Importe) AS ventas + from bi.analisis_ventas + WHERE vYear - 1 = Año + AND vWeek = Semana + GROUP BY Comercial + UNION ALL + SELECT vendedor, ventas + FROM bi.analisis_ventas_vendedor_evolution + WHERE Periodo = vPreviousPeriod + ) sub + GROUP BY vendedor; + END WHILE; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `analisis_ventas_simple` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `analisis_ventas_simple`() +BEGIN + +TRUNCATE bi.analisis_grafico_simple; + +INSERT INTO bi.analisis_grafico_simple SELECT * FROM bi.analisis_grafico_ventas; + +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `analisis_ventas_update` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8mb4 */ ; +/*!50003 SET character_set_results = utf8mb4 */ ; +/*!50003 SET collation_connection = utf8mb4_unicode_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `analisis_ventas_update`() +BEGIN + DECLARE vLastMonth DATE; + + SET vLastMonth = util.firstDayOfMonth(TIMESTAMPADD(MONTH, -1, CURDATE())); + + DELETE FROM analisis_ventas + WHERE Año > YEAR(vLastMonth) + OR (Año = YEAR(vLastMonth) AND Mes >= MONTH(vLastMonth)); + + INSERT INTO analisis_ventas ( + Familia, + Reino, + Comercial, + Comprador, + Provincia, + almacen, + Año, + Mes, + Semana, + Vista, + Importe + ) + SELECT + tp.Tipo AS Familia, + r.reino AS Reino, + tr.CodigoTrabajador AS Comercial, + tr2.CodigoTrabajador AS Comprador, + p.name AS Provincia, + w.name AS almacen, + tm.year AS Año, + tm.month AS Mes, + tm.week AS Semana, + v.vista AS Vista, + bt.importe AS Importe + FROM bs.ventas bt + LEFT JOIN vn2008.Tipos tp ON tp.tipo_id = bt.tipo_id + LEFT JOIN vn2008.reinos r ON r.id = tp.reino_id + LEFT JOIN vn2008.Clientes c on c.Id_Cliente = bt.Id_Cliente + LEFT JOIN vn2008.Trabajadores tr ON tr.Id_Trabajador = c.Id_Trabajador + LEFT JOIN vn2008.Trabajadores tr2 ON tr2.Id_Trabajador = tp.Id_Trabajador + JOIN vn2008.time tm ON tm.date = bt.fecha + JOIN vn2008.Movimientos m ON m.Id_Movimiento = bt.Id_Movimiento + LEFT JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket + JOIN vn2008.Agencias a ON a.Id_Agencia = t.Id_Agencia + LEFT JOIN vn2008.Vistas v ON v.vista_id = a.Vista + LEFT JOIN vn2008.Consignatarios cs ON cs.Id_Consigna = t.Id_Consigna + LEFT JOIN vn2008.province p ON p.province_id = cs.province_id + LEFT JOIN vn2008.warehouse w ON w.id = t.warehouse_id + WHERE bt.fecha >= vLastMonth AND r.mercancia; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `call_add` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8mb4 */ ; +/*!50003 SET character_set_results = utf8mb4 */ ; +/*!50003 SET collation_connection = utf8mb4_unicode_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `call_add`() +BEGIN + DECLARE datSTART DATETIME DEFAULT '2012-03-01'/*CURDATE()*/ ; + + -- Seleccionamos la ultima fecha introducida + SELECT MAX(Fecha) INTO datSTART FROM `call`; + + -- Borramos todas las entradas del dia datSTART por si hubiese registros nuevos + DELETE FROM `call` WHERE Fecha = datSTART; + + INSERT INTO bi.`call`(CodTrabajadorCartera,CodTrabajadorTelefono,dur_in,dur_out,Fecha,`year`,`month`,`week`,`hour`,phone) + SELECT vn2008.Averiguar_ComercialCliente(pb.Id_Cliente,Fecha) CodTrabajadorCartera,ll.CodigoTrabajador, dur_in, + dur_out, ll.Fecha,YEAR(ll.Fecha) `year`, MONTH(ll.Fecha) `month`,WEEK(ll.Fecha,7) `week`, Hora,phone + FROM ( + SELECT Id_Trabajador,CodigoTrabajador, IFNULL(billsec,0) dur_in, 0 dur_out, 1 as Recibidas, NULL as Emitidas, calldate as Fecha, + hour(calldate) as Hora,src as phone + FROM vn2008.Trabajadores T + JOIN vn2008.cdr C ON C.dstchannel LIKE CONCAT('%', T.extension, '%') + WHERE calldate >= datSTART AND LENGTH(C.src) >=9 AND disposition = 'ANSWERED' AND duration + UNION ALL + SELECT Id_Trabajador,CodigoTrabajador,0 dur_in, IFNULL(billsec,0) dur_out, NULL as Recibidas, 1 as Emitidas, date(calldate), hour(calldate),dst + FROM vn2008.Trabajadores T + JOIN vn2008.cdr C ON C.src = T.extension + WHERE calldate >= datSTART AND LENGTH(C.dst) >=9 AND disposition = 'ANSWERED' AND duration + ) ll + JOIN vn2008.Permisos USING(Id_Trabajador) + LEFT JOIN vn2008.v_phonebook pb ON pb.Telefono = ll.phone + WHERE Id_Grupo = 6; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `claim_ratio_routine` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `claim_ratio_routine`() +BEGIN + DECLARE vMonthToRefund INT DEFAULT 4; + + /* + * PAK 2015-11-20 + * Se trata de añadir a la tabla Greuges todos los + * cargos que luego vamos a utilizar para calcular el recobro + */ + + -- descuentos comerciales COD 11 + + INSERT INTO vn2008.Greuges(Fecha, Id_Cliente, Comentario, Importe, Id_Ticket) + SELECT Fecha, Id_Cliente, Concat('COD11 : ',Concepte), - round(Cantidad * + Preu * (100 - Descuento) / 100 ,2) AS Importe, t.Id_Ticket + FROM vn2008.Tickets t + JOIN vn2008.Movimientos m ON m.Id_Ticket = t.Id_Ticket + WHERE Id_Article = 11 + AND Concepte NOT LIKE '$%' + AND Fecha > '2014-01-01' + HAVING nz(Importe) <> 0; + + DELETE mc.* + FROM vn2008.Movimientos_componentes mc + JOIN vn2008.Movimientos m ON m.Id_Movimiento = mc.Id_Movimiento + JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket + WHERE m.Id_Article = 11 + AND m.Concepte NOT LIKE '$%' + AND t.Fecha > '2017-01-01'; + + INSERT INTO vn2008.Movimientos_componentes(Id_Movimiento, Id_Componente, Valor) + SELECT m.Id_Movimiento, 34, round(m.Preu * (100 - m.Descuento)/100,4) + FROM vn2008.Movimientos m + JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket + WHERE m.Id_Article = 11 + AND m.Concepte NOT LIKE '$%' + AND t.Fecha > '2017-01-01'; + + UPDATE vn2008.Movimientos m + JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket + SET Concepte = CONCAT('$ ',Concepte) + WHERE Id_Article = 11 + AND Concepte NOT LIKE '$%' + AND Fecha > '2014-01-01'; + + -- Reclamaciones demasiado sensibles + + INSERT INTO vn2008.Greuges(Fecha, Id_Cliente, Comentario, + Importe, Greuges_type_id,Id_Ticket) + SELECT cm.Fecha + , cm.Id_Cliente + , concat('Claim ',cm.id,' : ', m.Concepte) + ,round( -1 * ((sensib -1)/4) * Cantidad * + Preu * (100 - Descuento) / 100, 2) AS Reclamaciones + , 4 + , m.Id_Ticket + FROM vn2008.Movimientos m + JOIN vn2008.cl_act ca USING(Id_Movimiento) + JOIN vn2008.cl_main cm ON cm.id = ca.cl_main_id + WHERE ca.cl_sol_id NOT IN (1,5) + AND ca.greuge = 0 + AND cm.cl_est_id = 3; + + -- Reclamaciones que pasan a Maná + + INSERT INTO vn2008.Greuges(Fecha, Id_Cliente, Comentario, + Importe , Greuges_type_id,Id_Ticket) + SELECT cm.Fecha + , cm.Id_Cliente + , concat('Claim_mana ',cm.id,' : ', m.Concepte) + ,round( ((sensib -1)/4) * Cantidad * Preu * (100 - Descuento) / 100, 2) + AS Reclamaciones + ,3 + ,m.Id_Ticket + FROM vn2008.Movimientos m + JOIN vn2008.cl_act ca USING(Id_Movimiento) + JOIN vn2008.cl_main cm ON cm.id = ca.cl_main_id + WHERE ca.cl_sol_id NOT IN (1,5) + AND ca.greuge = 0 + AND cm.cl_est_id = 3 + AND cm.mana; + + -- Marcamos para no repetir + UPDATE vn2008.cl_act ca + JOIN vn2008.cl_main cm ON cm.id = ca.cl_main_id + SET greuge = 1 + WHERE ca.cl_sol_id NOT IN (1,5) + AND ca.greuge = 0 + AND cm.cl_est_id = 3; + + -- Recobros + + DROP TEMPORARY TABLE IF EXISTS tmp.ticket_list; + CREATE TEMPORARY TABLE tmp.ticket_list + (PRIMARY KEY (Id_Ticket)) + SELECT DISTINCT t.Id_Ticket + FROM vn2008.Movimientos_componentes mc + JOIN vn2008.Movimientos m ON mc.Id_Movimiento = m.Id_Movimiento + JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket + JOIN vn2008.Tickets_state ts ON ts.Id_Ticket = t.Id_Ticket + JOIN vncontrol.inter i ON i.inter_id = ts.inter_id + JOIN vn2008.state s ON s.id = i.state_id + WHERE mc.Id_Componente = 17 + AND mc.greuge = 0 + AND t.Fecha >= '2016-10-01' + AND t.Fecha < CURDATE() + AND s.alert_level >= 3; + + DELETE g.* + FROM vn2008.Greuges g + JOIN tmp.ticket_list t ON g.Id_Ticket = t.Id_Ticket + WHERE Greuges_type_id = 2; + + INSERT INTO vn2008.Greuges (Id_Cliente,Comentario,Importe,Fecha, + Greuges_type_id, Id_Ticket) + SELECT Id_Cliente + ,concat('recobro ', m.Id_Ticket), - round(SUM(mc.Valor*Cantidad),2) + AS dif + ,date(t.Fecha) + , 2 + ,tt.Id_Ticket + FROM vn2008.Movimientos m + JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket + JOIN tmp.ticket_list tt ON tt.Id_Ticket = t.Id_Ticket + JOIN vn2008.Movimientos_componentes mc + ON mc.Id_Movimiento = m.Id_Movimiento AND mc.Id_Componente = 17 + GROUP BY t.Id_Ticket + HAVING ABS(dif) > 1; + + UPDATE vn2008.Movimientos_componentes mc + JOIN vn2008.Movimientos m ON m.Id_Movimiento = mc.Id_Movimiento + JOIN tmp.ticket_list tt ON tt.Id_Ticket = m.Id_Ticket + SET greuge = 1 + WHERE Id_Componente = 17; + + /* + * Recalculamos la ratio de las reclamaciones, que luego + * se va a utilizar en el recobro + */ + + REPLACE bi.claims_ratio(Id_Cliente, Consumo, Reclamaciones, Ratio, recobro) + SELECT Id_Cliente, 0,0,0,0 + FROM vn2008.Clientes; + + REPLACE bi.claims_ratio(Id_Cliente, Consumo, Reclamaciones, Ratio, recobro) + SELECT fm.Id_Cliente, 12 * fm.Consumo, Reclamaciones, + round(Reclamaciones / (12*fm.Consumo),4) AS Ratio, 0 + FROM bi.facturacion_media_anual fm + LEFT JOIN( + SELECT cm.Id_Cliente, round(sum(-1 * ((sensib -1)/4) * + Cantidad * Preu * (100 - Descuento) / 100)) + AS Reclamaciones + FROM vn2008.Movimientos m + JOIN vn2008.cl_act ca + ON ca.Id_Movimiento = m.Id_Movimiento + JOIN vn2008.cl_main cm ON cm.id = ca.cl_main_id + WHERE ca.cl_sol_id NOT IN (1,5) + AND cm.cl_est_id = 3 + AND cm.Fecha >= TIMESTAMPADD(YEAR, -1, CURDATE()) + GROUP BY cm.Id_Cliente + ) claims ON claims.Id_Cliente = fm.Id_Cliente; + + + -- Calculamos el porcentaje del recobro para añadirlo al precio de venta + UPDATE bi.claims_ratio cr + JOIN ( + SELECT Id_Cliente, nz(SUM(Importe)) AS Greuge + FROM vn2008.Greuges + WHERE Fecha <= CURDATE() + GROUP BY Id_Cliente + ) g ON g.Id_Cliente = cr.Id_Cliente + SET recobro = GREATEST(0,round(nz(Greuge) / + (nz(Consumo) * vMonthToRefund / 12 ) ,3)); + + -- Protección neonatos + UPDATE bi.claims_ratio cr + JOIN vn.firstTicketShipped fts ON fts.clientFk = cr.Id_Cliente + SET recobro = 0, Ratio = 0 + WHERE fts.shipped > TIMESTAMPADD(MONTH,-1,CURDATE()); + + -- CLIENTE 7983, JULIAN SUAU + UPDATE bi.claims_ratio SET recobro = LEAST(0.05, recobro) WHERE Id_Cliente = 7983; + + -- CLIENTE 4358 + UPDATE bi.claims_ratio SET recobro = GREATEST(0.05, recobro) WHERE Id_Cliente = 4358; + + -- CLIENTE 5523, VERDECORA + UPDATE bi.claims_ratio SET recobro = GREATEST(0.12, recobro) WHERE Id_Cliente = 5523; + + -- CLIENTE 15979, SERVEIS VETERINARIS + UPDATE bi.claims_ratio SET recobro = GREATEST(0.05, recobro) WHERE Id_Cliente = 15979; + + -- CLIENTE 5189 i 8942, son de CSR i son el mateix client + UPDATE bi.claims_ratio cr + JOIN (SELECT sum(Consumo * recobro)/sum(Consumo) as recobro + FROM bi.claims_ratio + WHERE Id_Cliente IN ( 5189,8942) + ) sub + SET cr.recobro = sub.recobro + WHERE Id_Cliente IN ( 5189,8942); +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `clean` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `clean`() +BEGIN + DECLARE vDateShort DATETIME; + DECLARE vDateLong DATETIME; + DECLARE vOneYearAgo DATETIME; + + SET vDateShort = TIMESTAMPADD(MONTH, -2, CURDATE()); + SET vDateLong = TIMESTAMPADD(MONTH, -18,CURDATE()); + SET vOneYearAgo = TIMESTAMPADD(YEAR, -1,CURDATE()); + + DELETE FROM bi.Greuge_Evolution + WHERE (Fecha < vDateShort AND weekday(Fecha) != 1) + OR Fecha < vOneYearAgo; + + DELETE FROM bi.defaulters WHERE `date` < vDateLong; + DELETE FROM bi.defaulting WHERE `date` < vDateLong; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `clean_launcher` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `clean_launcher`() +BEGIN + + call vn2008.clean(0); + +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `comparativa_add` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `comparativa_add`() +BEGIN + DECLARE lastCOMP INT; # Se trata de una variable para almacenar el ultimo valor del Periodo + + SELECT MAX(Periodo) INTO lastCOMP FROM vn2008.Comparativa; + -- Fijaremos las ventas con más de un mes de antiguedad en la tabla Comparativa + + IF lastCOMP < vn2008.vnperiod(CURDATE())- 3 AND vn2008.vnweek(CURDATE()) > 3 THEN + + REPLACE vn2008.Comparativa(Periodo, Id_Article, warehouse_id, Cantidad,price) + SELECT tm.period as Periodo, m.Id_Article, t.warehouse_id, sum(m.Cantidad), sum(v.importe) + FROM bs.ventas v + JOIN vn2008.time tm ON tm.date = v.fecha + JOIN vn2008.Movimientos m ON m.Id_Movimiento = v.Id_Movimiento + JOIN vn2008.Tipos tp ON tp.tipo_id = v.tipo_id + JOIN vn2008.reinos r ON r.id = tp.reino_id + JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket + WHERE tm.period BETWEEN lastCOMP AND vn2008.vnperiod(CURDATE())- 3 + AND t.Id_Cliente NOT IN(400,200) + AND r.display <> 0 + AND t.warehouse_id NOT IN (0,13) + GROUP BY m.Id_Article, Periodo, t.warehouse_id; + + END IF; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `comparativa_add_manual` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8mb4 */ ; +/*!50003 SET character_set_results = utf8mb4 */ ; +/*!50003 SET collation_connection = utf8mb4_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `comparativa_add_manual`(IN vStarted DATE, IN vEnded DATE) +BEGIN +/** + * Recalcula la tabla Comparativa para dos valores dados + * + * @param vStarted fecha desde + * @param vEnded fecha hasta + */ + + DECLARE periodStart INT; + DECLARE periodEnd INT; + + -- Seleccionamos la fecha minima/maxima del periodo que vamos a consultar + SET periodStart = vn2008.vnperiod(vStarted); + SET periodEnd = vn2008.vnperiod(vEnded); + + DELETE FROM vn2008.Comparativa + WHERE Periodo BETWEEN periodStart AND periodEnd; + + INSERT INTO vn2008.Comparativa(Periodo, Id_Article, warehouse_id, Cantidad,price) + SELECT tm.period as Periodo, m.Id_Article, t.warehouse_id, sum(m.Cantidad), sum(v.importe) + FROM bs.ventas v + JOIN vn2008.time tm ON tm.date = v.fecha + JOIN vn2008.Movimientos m ON m.Id_Movimiento = v.Id_Movimiento + JOIN vn2008.Tipos tp ON tp.tipo_id = v.tipo_id + JOIN vn2008.reinos r ON r.id = tp.reino_id + JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket + WHERE tm.period BETWEEN periodStart AND periodEnd + AND t.Id_Cliente NOT IN(400,200) + AND r.display <> 0 + AND t.warehouse_id NOT IN (0,13) + GROUP BY m.Id_Article, Periodo, t.warehouse_id; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `customer_risk_update` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `customer_risk_update`(v_customer INT, v_company INT, v_amount DECIMAL(10,2)) +BEGIN + CALL vn.clientRisk_update(v_customer, v_company, v_amount); +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `defaultersFromDate` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `defaultersFromDate`(IN vDate DATE) +BEGIN + + SELECT t1.*, c.Cliente, w.code AS workerCode, c.pay_met_id,c.Vencimiento + FROM ( + -- Filtramos aquellos clientes cuyo saldo se ha incrementado de ayer a hoy + select * from( + select today.client, today.amount todayAmount, yesterday.amount yesterdayAmount, round(yesterday.amount - today.amount,2) as difference, defaulterSince + from + (select client, amount, defaulterSince + from defaulters + where date = vDate and hasChanged) today + join + (select client, amount + from defaulters + where date = TIMESTAMPADD(DAY,-1,vDate)) yesterday using(client) + + having today.amount > 0 and difference <> 0 + ) newDefaulters + )t1 left join vn2008.Clientes c ON t1.client = c.Id_Cliente + left join vn.worker w ON w.id = c.Id_Trabajador; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `defaulting` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8mb4 */ ; +/*!50003 SET character_set_results = utf8mb4 */ ; +/*!50003 SET collation_connection = utf8mb4_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `defaulting`(IN `vDate` DATE) +BEGIN + DECLARE vDone BOOLEAN; + DECLARE vClient INT; + DECLARE vAmount INT; + DECLARE vDued DATE; + DECLARE vAmountInvoice DECIMAL(10,2); + DECLARE vGraceDays INT; + DECLARE defaulters CURSOR FOR + SELECT client, amount, graceDays FROM bi.defaulters d + JOIN vn2008.Clientes c ON c.Id_Cliente = d.client + JOIN vn2008.pay_met pm ON pm.id = c.pay_met_id + WHERE hasChanged AND date = vDate; + + DECLARE invoices CURSOR FOR + SELECT Vencimiento, importe FROM vn2008.Facturas f + WHERE f.Fecha >= '2016-01-01' AND f.Id_Cliente = vClient ORDER BY f.Fecha DESC; + + DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; + + DELETE FROM bi.defaulters WHERE date = vDate; + + INSERT INTO bi.defaulters(client, date, amount) + SELECT c.id, vDate, IFNULL(FLOOR(SUM(cro.amount)),0) AS amount + FROM vn.`client` c + LEFT JOIN bi.customerRiskOverdue cro ON c.id = cro.customer_id + GROUP BY c.id; + + -- marcamos si ha cambiado y heredamos la fecha defaulterSince + UPDATE bi.defaulters d + LEFT JOIN ( + SELECT * FROM( + SELECT client, amount , defaulterSince, frozened FROM bi.defaulters + WHERE date <= TIMESTAMPADD(DAY,-1, vDate) + ORDER BY date DESC) t GROUP BY client + ) yesterday using(client) + SET d.hasChanged = (IFNULL(d.amount,0) <> IFNULL(yesterday.amount,0)), + d.defaulterSince = yesterday.defaulterSince, + d.frozened = yesterday.frozened + WHERE d.date = vDate ; + + OPEN defaulters; + defaulters: LOOP + SET vDone = FALSE; + SET vAmount = 0; + FETCH defaulters INTO vClient,vAmount, vGraceDays; + IF vDone THEN + LEAVE defaulters; + END IF; + OPEN invoices; + invoices:LOOP + + FETCH invoices INTO vDued, vAmountInvoice; + IF vDone THEN + LEAVE invoices; + END IF; + + IF TIMESTAMPADD(DAY, vGraceDays, vDued) <= vDate THEN + SET vAmount = vAmount - vAmountInvoice; + IF vAmount <= 0 THEN + + UPDATE defaulters SET defaulterSince = vDued + WHERE client = vClient and date = vDate; + + SET vAmount = 0; + LEAVE invoices; + END IF; + END IF; + END LOOP; + CLOSE invoices; + END LOOP; + CLOSE defaulters; + + DELETE FROM defaulters + WHERE amount = 0 + AND hasChanged = FALSE + AND `date` = vDate; + + UPDATE defaulters d + JOIN vn.config ON TRUE + SET d.frozened = NULL + WHERE `date` = vDate + AND d.amount <= config.defaultersMaxAmount; + + CALL vn.clientFreeze(); + + -- actualizamos defaulting + DELETE FROM bi.defaulting WHERE date = vDate; + + INSERT INTO bi.defaulting(date, amount) + SELECT vDate, SUM(amount) + FROM bi.defaulters + WHERE date = vDate and amount > 0; + +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `defaulting_launcher` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `defaulting_launcher`() +BEGIN + + CALL bi.defaulting(curdate()); + +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `facturacion_media_anual_update` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `facturacion_media_anual_update`() +BEGIN + TRUNCATE TABLE bs.clientAnnualConsumption; + + REPLACE bi.facturacion_media_anual(Id_Cliente, Consumo) + SELECT clientFk, avg(Facturacion) + FROM ( + SELECT clientFk, YEAR(issued) year, MONTH(issued) month, sum(amount) as Facturacion + FROM vn.invoiceOut + WHERE issued BETWEEN TIMESTAMPADD(YEAR,-1,CURDATE()) AND TIMESTAMPADD(DAY, - DAY(CURDATE()),CURDATE()) + GROUP BY clientFk, year, month + ) vol + GROUP BY clientFk; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `greuge_dif_porte_add` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `greuge_dif_porte_add`() +BEGIN + DECLARE datSTART DATETIME DEFAULT TIMESTAMPADD(DAY,-60,CURDATE()); -- '2019-07-01' + DECLARE datEND DATETIME DEFAULT TIMESTAMPADD(DAY,-1,CURDATE()); + + DROP TEMPORARY TABLE IF EXISTS tmp.dp; + + -- Agencias que no cobran por volumen + CREATE TEMPORARY TABLE tmp.dp + (PRIMARY KEY (ticketFk)) + ENGINE = MEMORY + SELECT t.id ticketFk, + SUM((t.zonePrice - t.zoneBonus) * ebv.ratio) AS teorico, + 00000.00 as practico, + 00000.00 as greuge, + t.clientFk, + t.shipped + FROM + vn.ticket t + JOIN vn2008.Clientes cli ON cli.Id_cliente = t.clientFk + LEFT JOIN vn2008.expeditions e ON e.ticket_id = t.id + JOIN vn.expeditionBoxVol ebv ON ebv.boxFk = e.EsBulto + JOIN vn.zone z ON t.zoneFk = z.id + WHERE + t.shipped between datSTART AND datEND + AND cli.`real` + AND t.companyFk IN (442 , 567) + AND z.isVolumetric = FALSE + GROUP BY t.id; + + -- Agencias que cobran por volumen + INSERT INTO tmp.dp + SELECT sv.ticketFk, + SUM(IFNULL(sv.freight,0)) AS teorico, + 00000.00 as practico, + 00000.00 as greuge, + sv.clientFk, + sv.shipped + FROM vn.saleVolume sv + JOIN vn.zone z ON z.id = sv.zoneFk + AND sv.shipped BETWEEN datSTART AND datEND + AND z.isVolumetric != FALSE + GROUP BY sv.ticketFk; + + DROP TEMPORARY TABLE IF EXISTS tmp.dp_aux; + + CREATE TEMPORARY TABLE tmp.dp_aux + (PRIMARY KEY (ticketFk)) + ENGINE = MEMORY + SELECT dp.ticketFk, sum(Cantidad * Valor) as valor + FROM tmp.dp + JOIN vn2008.Movimientos m ON m.Id_Ticket = dp.ticketFk + JOIN vn2008.Movimientos_componentes mc using(Id_Movimiento) + WHERE mc.Id_Componente = 15 + GROUP BY dp.ticketFk; + + UPDATE tmp.dp + JOIN tmp.dp_aux USING(ticketFk) + SET practico = IFNULL(valor,0); + + DROP TEMPORARY TABLE tmp.dp_aux; + + CREATE TEMPORARY TABLE tmp.dp_aux + (PRIMARY KEY (ticketFk)) + ENGINE = MEMORY + SELECT dp.ticketFk, sum(Importe) Importe + FROM tmp.dp + JOIN vn2008.Greuges g ON g.Id_Ticket = dp.ticketFk + WHERE Greuges_type_id = 1 -- dif_porte + GROUP BY dp.ticketFk; + + UPDATE tmp.dp + JOIN tmp.dp_aux USING(ticketFk) + SET greuge = IFNULL(Importe,0); + + + INSERT INTO vn2008.Greuges (Id_Cliente,Comentario,Importe,Fecha, Greuges_type_id, Id_Ticket) + SELECT dp.clientFk + , concat('dif_porte ', dp.ticketFk) + , round(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0),2) as Importe + , date(dp.shipped) + , 1 + ,dp.ticketFk + FROM tmp.dp + JOIN vn.client c ON c.id = dp.clientFk + WHERE ABS(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0)) > 1 + AND c.isRelevant; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `Greuge_Evolution_Add` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8mb4 */ ; +/*!50003 SET character_set_results = utf8mb4 */ ; +/*!50003 SET collation_connection = utf8mb4_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `Greuge_Evolution_Add`() +BEGIN +/* + Inserta en la tabla Greuge_Evolution el saldo acumulado de cada cliente, + así como las ventas acumuladas en los ultimos 365 dias, para poder controlar + su evolucion. +*/ + DECLARE datFEC DATE; + DECLARE datFEC_TOMORROW DATE; + DECLARE datFEC_LASTYEAR DATE; + + DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN + + GET DIAGNOSTICS CONDITION 2 @errno = MYSQL_ERRNO, @text = MESSAGE_TEXT; + SELECT CONCAT('ERROR ', IFNULL(@errno,0), ': ', ifnull(@text,'texto')); + INSERT INTO vn2008.mail (`to`,`subject`,`text`) VALUES ('jgallego@verdnatura.es', 'Greuge_Evolution_Add' ,CONCAT('ERROR ', IFNULL(@errno,0), ': ', ifnull(@text,'texto'))); + INSERT INTO vn2008.mail (`to`,`subject`,`text`) VALUES ('pako@verdnatura.es', 'Greuge_Evolution_Add' ,CONCAT('ERROR ', IFNULL(@errno,0), ': ', ifnull(@text,'texto'))); + + END; + + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + + DELETE FROM bi.Greuge_Evolution WHERE Fecha >= TIMESTAMPADD(MONTH,-1,CURDATE()); + + SELECT TIMESTAMPADD(DAY,1,MAX(Fecha)), + TIMESTAMPADD(DAY,2,MAX(Fecha)) + INTO datFEC, + datFEC_TOMORROW + FROM bi.Greuge_Evolution; + + SET datFEC_LASTYEAR = TIMESTAMPADD(YEAR,-1,datFEC); + + DELETE FROM bi.Greuge_Evolution WHERE Fecha >= datFEC; + + DROP TEMPORARY TABLE IF EXISTS maxInvoice; + + CREATE TEMPORARY TABLE maxInvoice + (PRIMARY KEY (Id_Cliente)) + ENGINE = MEMORY + SELECT DISTINCT Id_Cliente, max(Fecha) as maxFecha + FROM vn2008.Facturas + GROUP BY Id_Cliente + HAVING maxFecha < timestampadd(month,-2,datFEC); + + WHILE datFEC < CURDATE() DO + + REPLACE bi.Greuge_Evolution(Id_Cliente, Fecha, Greuge, Ventas, Fosil) + + SELECT Id_Cliente, datFEC as Fecha, Greuge, Ventas, 0 + FROM ( + SELECT Id_Cliente, sum(Importe) as Greuge + FROM vn2008.Greuges + where Fecha <= datFEC + group by Id_Cliente + ) sub + RIGHT JOIN + + ( + + SELECT Id_Cliente, sum(Ventas) as Ventas + FROM + ( + + SELECT Id_Cliente, IF (fecha != datFEC, -1,1) * (importe + recargo) as Ventas + FROM bs.ventas + WHERE fecha = datFEC or fecha = datFEC_LASTYEAR + + UNION ALL + + SELECT Id_Cliente, Ventas + FROM bi.Greuge_Evolution + WHERE Fecha = TIMESTAMPADD(DAY, -1, datFEC) + + ) sub + group by Id_Cliente + + ) v using(Id_Cliente) + ; + + -- Ahora calcularemos el greuge muerto + UPDATE bi.Greuge_Evolution ge + JOIN maxInvoice m using(Id_Cliente) + SET FOSIL = GREUGE + WHERE m.maxFecha < TIMESTAMPADD(MONTH,-2,ge.Fecha); + + -- Recobro + UPDATE bi.Greuge_Evolution ge + JOIN ( + SELECT cs.Id_Cliente, sum(Valor * Cantidad) as Importe + FROM vn2008.Tickets t + JOIN vn2008.Consignatarios cs on cs.Id_Consigna = t.Id_Consigna + JOIN vn2008.Movimientos m on m.Id_Ticket = t.Id_Ticket + JOIN vn2008.Movimientos_componentes mc on mc.Id_Movimiento = m.Id_Movimiento + WHERE t.Fecha >= datFEC + AND t.Fecha < datFEC_TOMORROW + AND mc.Id_Componente = 17 -- Recobro + GROUP BY cs.Id_Cliente + ) sub using(Id_Cliente) + SET Recobro = Importe + WHERE ge.Fecha = datFEC; + + SET datFEC = datFEC_TOMORROW; + SET datFEC_TOMORROW = TIMESTAMPADD(DAY,1,datFEC_TOMORROW); + SET datFEC_LASTYEAR = TIMESTAMPADD(YEAR,-1,datFEC); + + END WHILE; + + DROP TEMPORARY TABLE IF EXISTS maxInvoice; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `nightly_tasks` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `nightly_tasks`() +BEGIN + +call bi.analisis_ventas_evolution_add; + +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `nigthlyAnalisisVentas` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8mb4 */ ; +/*!50003 SET character_set_results = utf8mb4 */ ; +/*!50003 SET collation_connection = utf8mb4_unicode_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `nigthlyAnalisisVentas`() +BEGIN + CALL analisis_ventas_update; + CALL analisis_ventas_simple; + CALL analisis_ventas_evolution_add; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `partitioning` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `partitioning`(IN intyear INT) +BEGIN + + DECLARE v_sql TEXT; + DECLARE strSquemaName VARCHAR(10); + DECLARE strTableName VARCHAR(20); + DECLARE strFieldName VARCHAR(20); + DECLARE strDateField VARCHAR(20); + DECLARE strTableDependingOn VARCHAR(20); + DECLARE strFieldDependingOn VARCHAR(20); + DECLARE done BIT DEFAULT 0; + DECLARE strCacheSchema VARCHAR(5); + DECLARE dat_start,dat_end DATE; + DECLARE cur1 CURSOR FOR + SELECT `schema_name`,`table_name` FROM `cache`.partitioning_information ORDER BY execution_order; + DECLARE cur2 CURSOR FOR + SELECT `schema_name`,`table_name`,`date_field` FROM `cache`.partitioning_information WHERE date_field > '' ORDER BY execution_order; + DECLARE cur3 CURSOR FOR + SELECT `schema_name`,`table_name`,`table_depending` FROM `cache`.partitioning_information WHERE table_depending > '' ORDER BY execution_order; + DECLARE cur4 CURSOR FOR + SELECT `schema_name`,`table_name`,`table_depending` FROM `cache`.partitioning_information WHERE table_depending > '' ORDER BY execution_order DESC; + DECLARE cur5 CURSOR FOR + SELECT `schema_name`,`table_name`,`date_field` FROM `cache`.partitioning_information WHERE date_field > '' ORDER BY execution_order DESC; + + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; + + SET FOREIGN_KEY_CHECKS=0; + IF LENGTH(intyear) <> 4 THEN + CALL el_año_debe_contener_4_caracteres_yyyy(); + END IF; + SET dat_start = STR_TO_DATE(CONCAT('01,01,',intyear),'%d,%m,%Y'); + SET dat_end = STR_TO_DATE(CONCAT('31,12,',intyear),'%d,%m,%Y'); + SET strCacheSchema = CONCAT('vn_',right(intyear,2)); + SET v_sql = sql_printf ('CREATE SCHEMA IF NOT EXISTS %t',strCacheSchema); + CALL util.exec (v_sql); + + -- Insertamos en las tablas del cur1 + OPEN cur1; + FETCH cur1 INTO strSquemaName,strTableName; + WHILE NOT done DO + SET v_sql = sql_printf ('CREATE TABLE IF NOT EXISTS %t.%t LIKE %t.%t', + strCacheSchema, + strTableName, + strSquemaName, + strTableName); + + CALL util.exec (v_sql); + + FETCH cur1 INTO strSquemaName,strTableName; + + END WHILE; + CLOSE cur1; + + -- Insertamos en las tablas del cur2 + OPEN cur2; + FETCH cur2 INTO strSquemaName,strTableName,strDateField; + WHILE NOT done DO + SET v_sql = sql_printf ('REPLACE INTO %t.%t SELECT * FROM %t.%t WHERE %t BETWEEN %v AND %v', + strCacheSchema, + strTableName, + strSquemaName, + strTableName, + strDateField, + dat_start, + dat_end); + + SELECT v_sql; + CALL util.exec (v_sql); + + FETCH cur2 INTO strSquemaName,strTableName,strDateField; + + END WHILE; + CLOSE cur2; + + -- Insertamos en las tablas del cur3 + OPEN cur3; + SET done = 0; + FETCH cur3 INTO strSquemaName,strTableName,strTableDependingOn; + WHILE NOT done DO + + + -- Torna la columna per la qual vincular amb el seu pare + SELECT kcu.column_name INTO strFieldName + FROM information_schema.key_column_usage kcu + WHERE table_schema = CONVERT(strSquemaName USING utf8) COLLATE utf8_general_ci + AND REFERENCED_TABLE_NAME = CONVERT(strTableDependingOn USING utf8) COLLATE utf8_general_ci + AND table_name = CONVERT(strTableName USING utf8) COLLATE utf8_general_ci; + + -- Torna la columna per la qual vincular amb el seu fill + SELECT kcu.column_name INTO strFieldDependingOn + FROM information_schema.key_column_usage kcu + WHERE table_schema = CONVERT(strSquemaName USING utf8) COLLATE utf8_general_ci -- FIX mirar l'esquema del pare + AND constraint_name = 'PRIMARY' + AND table_name = CONVERT(strTableDependingOn USING utf8) COLLATE utf8_general_ci; + + SET v_sql = sql_printf ('REPLACE INTO %t.%t SELECT a.* FROM %t.%t a JOIN %t.%t b ON a.%t = b.%t', + strCacheSchema, + strTableName, + strSquemaName, + strTableName, + strCacheSchema, + strTableDependingOn, + strFieldName, + strFieldDependingOn); + select v_sql; + CALL util.exec (v_sql); + + FETCH cur3 INTO strSquemaName,strTableName,strTableDependingOn; + END WHILE; + + CLOSE cur3; + + -- Borramos en las tablas del cur4 que es igual que el dos pero en sentido descendente + -- para evitar errores con las foreign key + OPEN cur4; + SET done = 0; + FETCH cur4 INTO strSquemaName,strTableName,strTableDependingOn; + WHILE NOT done DO + + + -- Torna la columna per la qual vincular amb el seu pare + SELECT kcu.column_name INTO strFieldName + FROM information_schema.key_column_usage kcu + WHERE table_schema = CONVERT(strSquemaName USING utf8) COLLATE utf8_general_ci + AND REFERENCED_TABLE_NAME = CONVERT(strTableDependingOn USING utf8) COLLATE utf8_general_ci + AND table_name = CONVERT(strTableName USING utf8) COLLATE utf8_general_ci; + + -- Torna la columna per la qual vincular amb el seu fill + SELECT kcu.column_name INTO strFieldDependingOn + FROM information_schema.key_column_usage kcu + WHERE table_schema = CONVERT(strSquemaName USING utf8) COLLATE utf8_general_ci + AND constraint_name = 'PRIMARY' + AND table_name = CONVERT(strTableDependingOn USING utf8) COLLATE utf8_general_ci; + + SELECT v_sql; + SET v_sql = sql_printf ('DELETE a.* FROM %t.%t a JOIN %t.%t b ON a.%t = b.%t', + strSquemaName, + strTableName, + strCacheSchema, + strTableDependingOn, + strFieldName, + strFieldDependingOn); + CALL util.exec (v_sql); + + FETCH cur4 INTO strSquemaName,strTableName,strTableDependingOn; + END WHILE; + + CLOSE cur4; + + -- Borramos en las tablas del cur5 + OPEN cur5; + SET done = 0; + FETCH cur5 INTO strSquemaName,strTableName,strDateField; + WHILE NOT done DO + + SET v_sql = sql_printf ('DELETE FROM %t WHERE %t BETWEEN %v AND %v', + strTableName, + strDateField, + dat_start, + dat_end); + CALL util.exec (v_sql); + + FETCH cur5 INTO strSquemaName,strTableName,strDateField; + + END WHILE; + CLOSE cur5; + SET FOREIGN_KEY_CHECKS=1; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `primer_pedido_add` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `primer_pedido_add`() +BEGIN + INSERT IGNORE INTO bi.primer_pedido(Id_Ticket, Id_Cliente, month, year, total) + SELECT * + FROM + (SELECT + `m`.`Id_Ticket` , + `v`.`Id_Cliente` , + `t`.`month`, + `t`.`year`, + v.importe + v.recargo as total + FROM + bs.ventas v + JOIN + vn2008.Movimientos m on m.Id_Movimiento = v.Id_Movimiento + JOIN + vn2008.time t on t.date = v.fecha + WHERE + fecha > CURDATE() + INTERVAL -(1) YEAR + ORDER BY fecha) `s` + GROUP BY `s`.`Id_Cliente`; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `regularidad` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = '' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `regularidad`() +BEGIN + +select Id_Cliente, Meses, IF(Antiguedad = 13,12,Antiguedad) , Meses / IF(Antiguedad = 13,12,Antiguedad) as Regularidad from ( +SELECT Id_Cliente, count(*) as Meses, FLOOR(DATEDIFF('2012-07-31', IF(Created < '2011-08-01','2011-08-01', Created)) / 30) +1 as Antiguedad from +( + +SELECT DISTINCT Id_Cliente, Periodo(Fecha) as periodo from Facturas +where Fecha between '2011-08-01' AND '2012-07-31' + +) sub + +join Clientes using(Id_Cliente) +where Created <= '2012-07-31' + +group by Id_Cliente +having Antiguedad > 0 + +) sub44 +order by Antiguedad ; + + +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `rotacion_update` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8mb4 */ ; +/*!50003 SET character_set_results = utf8mb4 */ ; +/*!50003 SET collation_connection = utf8mb4_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `rotacion_update`() +BEGIN +/** + * Llama a la función rotacion_update_manual para todos los articulos, desde hace una semana hasta hoy. + */ + DECLARE vMinItemFk INT; + DECLARE vMaxItemFk INT; + + + SELECT MIN(id), MAX(id) + INTO vMinItemFk, vMaxItemFk + FROM vn.item i; + CALL rotacion_update_manual(vMinItemFk, vMaxItemFk, TIMESTAMPADD(WEEK, -1, CURDATE()), CURDATE()); +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `rotacion_update_manual` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `rotacion_update_manual`( + vItemFrom INT, + vItemTo INT, + vStartDate DATE, + vEndDate DATE +) +BEGIN + DECLARE vInventoryProvider INT DEFAULT 4; + DECLARE vLastEventFinished DATETIME; + + DROP TEMPORARY TABLE IF EXISTS timing; + CREATE TEMPORARY TABLE timing ( + `event` VARCHAR(255), + finished DATETIME, + seconds INT + ); + + INSERT INTO timing VALUES ('start', NOW(), 0); + + DROP TEMPORARY TABLE IF EXISTS tItems; + CREATE TEMPORARY TABLE tItems + (INDEX (id)) + ENGINE = MEMORY + SELECT i.id + FROM vn.item i + JOIN vn.itemType t ON t.id = i.typeFk + JOIN vn.itemCategory c ON c.id = t.categoryFk + WHERE i.id BETWEEN vItemFrom AND vItemTo + AND c.merchandise + AND c.display; + + DROP TEMPORARY TABLE IF EXISTS tTemp; + CREATE TEMPORARY TABLE tTemp + ENGINE = MEMORY + SELECT b.itemFk, t.landed dated, b.quantity, t.warehouseInFk warehouseFk + FROM vn.entry e + JOIN vn.buy b ON b.entryFk = e.id + STRAIGHT_JOIN tItems i ON i.id = b.itemFk + JOIN vn.travel t ON t.id = e.travelFk + JOIN vn.warehouse w ON w.id = t.warehouseInFk + WHERE t.landed BETWEEN vStartDate AND vEndDate + AND e.supplierFk <> vInventoryProvider + AND NOT e.isInventory + AND NOT e.isRaid + AND NOT w.isFeedStock; + + INSERT INTO tTemp + SELECT b.itemFk, t.shipped, -b.quantity, t.warehouseOutFk + FROM vn.entry e + JOIN vn.buy b ON b.entryFk = e.id + STRAIGHT_JOIN tItems i ON i.id = b.itemFk + JOIN vn.travel t ON t.id = e.travelFk + JOIN vn.warehouse w ON w.id = t.warehouseOutFk + WHERE t.shipped BETWEEN vStartDate AND vEndDate + AND e.supplierFk <> vInventoryProvider + AND NOT e.isInventory + AND NOT e.isRaid + AND NOT w.isFeedStock; + + INSERT INTO tTemp + SELECT s.itemFk, DATE(t.shipped), -s.quantity, t.warehouseFk + FROM vn.ticket t + JOIN vn.sale s ON s.ticketFk = t.id + STRAIGHT_JOIN tItems i ON i.id = s.itemFk + JOIN vn.warehouse w ON w.id = t.warehouseFk + WHERE t.shipped BETWEEN vStartDate AND vEndDate + AND t.clientFk NOT IN (2066, 2067, 2068) + AND NOT w.isFeedStock; + + REPLACE bi.rotacion(Id_Article, warehouse_id, total, rotacion) + SELECT b.itemFk, b.warehouseFk, t.total, IF(t.total = 0, 0, ROUND((SUM(b.weightedBalance) / t.total), 2)) + FROM ( + SELECT itemFk, + warehouseFk, + @ival := IF(warehouseFk = @warehouse AND itemFk = @item, + DATEDIFF(dated, @dated), + 1 + ), + IF(warehouseFk = @warehouse AND itemFk = @item, + IF(@sd < 0, 0, @sd) * @ival, + IF(units < 0, 0, units) + ) weightedBalance, + IF(warehouseFk = @warehouse AND itemFk = @item, + @sd := @sd + units, + @sd:= units + ), + @dated := dated, + @item := itemFk, + @warehouse := warehouseFk + FROM ( + SELECT itemFk, dated, SUM(quantity) units, + warehouseFk, @dated := vStartDate, @item := 0, + @sd:= 0, @warehouse := 0, @ival := 0 + FROM tTemp + GROUP BY itemFk, warehouseFk, dated + ) t + ) b + JOIN ( + SELECT b.itemFk, t.warehouseInFk warehouseFk, SUM(b.quantity) total + FROM vn.entry e + JOIN vn.buy b ON b.entryFk = e.id + STRAIGHT_JOIN tItems i ON i.id = b.itemFk + JOIN vn.travel t ON t.id = e.travelFk + JOIN vn.warehouse w ON w.id = t.warehouseInFk + WHERE t.landed BETWEEN vStartDate AND vEndDate + AND e.supplierFk <> vInventoryProvider + AND NOT e.isInventory + AND NOT e.isRaid + AND NOT w.isFeedStock + GROUP BY itemFk, warehouseFk + ) t USING(itemFk, warehouseFk) + GROUP BY itemFk, warehouseFk; + + DROP TEMPORARY TABLE tItems, tTemp; + + SELECT MAX(finished) INTO vLastEventFinished FROM timing; + INSERT INTO timing VALUES ('replace', NOW(), TIMEDIFF(NOW(), vLastEventFinished)); + + -- Añadimos el volumen por unidad de venta + + CALL `cache`.last_buy_refresh(FALSE); + + UPDATE bi.rotacion r + JOIN `cache`.last_buy b ON r.Id_Article = b.item_id AND r.warehouse_id = b.warehouse_id + JOIN vn2008.Compres c ON c.Id_Compra = b.buy_id + JOIN vn.packaging p ON p.id = c.Id_Cubo + JOIN vn2008.Articles a ON a.Id_Article = c.Id_Article + JOIN vn2008.Tipos tp ON tp.tipo_id = a.tipo_id + JOIN vn2008.reinos re ON re.id = tp.reino_id + SET cm3 = IF(p.volume > 0, p.volume, p.width * p.depth * IF(p.height, p.height, a.medida + 10)) / IF(c.packing, c.packing, 1), + cm3reparto = a.compression * IF(p.volume > 0, p.volume, p.width * p.depth * IF(p.height, p.height, a.medida + 10)) / IF(c.packing, c.packing, 1), + grams = IF(IFNULL(c.weight,0), + (1000 * c.weight / c.Packing) / GREATEST(1,IFNULL(a.Tallos,0)), + (a.density / 1000000) * a.`compression` * IF(p.volume > 0, p.volume, p.width * p.depth * IF(p.height, p.height, a.medida + 10)) / IF(c.packing, c.packing, 1)/ GREATEST(1,IFNULL(a.Tallos,0))) + WHERE re.mercancia = TRUE + AND r.Id_Article BETWEEN vItemFrom AND vItemTo; + + SELECT MAX(finished) INTO vLastEventFinished FROM timing; + INSERT INTO timing VALUES ('volume', NOW(), TIMEDIFF(NOW(), vLastEventFinished)); + + -- Añadimos el coste de almacenamiento y manipulacion + + UPDATE bi.rotacion + JOIN ( + SELECT warehouse_id, SUM(total * rotacion * cm3) AS operatingCost + FROM bi.rotacion + WHERE Id_Article BETWEEN vItemFrom AND vItemTo + GROUP BY warehouse_id + ) sub USING(warehouse_id) + JOIN ( + SELECT warehouse_id, SUM(Valor) AS Coste_Auxiliar + FROM bi.tarifa_warehouse + WHERE Id_Premisa = 1 + GROUP BY warehouse_id + ) sub2 USING(warehouse_id) + JOIN ( + SELECT warehouse_id, SUM(Valor) AS Coste_Operativo + FROM bi.tarifa_warehouse + WHERE Id_Premisa IN (2, 3) + GROUP BY warehouse_id + ) sub3 USING(warehouse_id) + JOIN ( + SELECT warehouse_id, SUM(Valor) AS Coste_Manipulacion + FROM bi.tarifa_warehouse + WHERE Id_Premisa = 4 + GROUP BY warehouse_id + ) sub4 USING(warehouse_id) + SET auxiliar = IF(rotacion < 0 OR operatingCost = 0, 0, ROUND(rotacion * cm3 * Coste_Auxiliar / operatingCost, 4)), + almacenaje = IF(rotacion < 0 OR operatingCost = 0, 0, ROUND(rotacion * cm3 * Coste_Operativo / operatingCost, 4)), + manipulacion = IF(rotacion < 0 OR operatingCost = 0, 0, ROUND(rotacion * cm3 * Coste_Manipulacion / operatingCost, 4)) + WHERE Id_Article BETWEEN vItemFrom AND vItemTo; + + SELECT MAX(finished) INTO vLastEventFinished FROM timing; + INSERT INTO timing VALUES ('almacenamiento', NOW(), TIMEDIFF(NOW(), vLastEventFinished)); + + SELECT * FROM timing; + DROP TEMPORARY TABLE timing; +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `rotacion_volumen_update` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8mb4 */ ; +/*!50003 SET character_set_results = utf8mb4 */ ; +/*!50003 SET collation_connection = utf8mb4_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `rotacion_volumen_update`() +BEGIN +/* + CALL vn2008.item_last_buy_(NULL,curdate()); + + -- Añadimos el volumen por unidad de venta + update bi.rotacion r + JOIN vn2008.t_item_last_buy b ON r.Id_Article = b.item_id AND r.warehouse_id = b.warehouse_id + join vn2008.Compres c ON c.Id_Compra = b.buy_id + join vn.item i ON i.id = c.Id_Article + join vn.itemType tp ON tp.id = i.typeFk + join vn.itemCategory ic ON ic.id = tp.categoryFk + set cm3 = vn.item_getVolume(r.Id_Article, Id_Cubo) / Packing + where ic.merchandise = TRUE; +*/ +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; +/*!50003 DROP PROCEDURE IF EXISTS `Velocity_Add` */; +/*!50003 SET @saved_cs_client = @@character_set_client */ ; +/*!50003 SET @saved_cs_results = @@character_set_results */ ; +/*!50003 SET @saved_col_connection = @@collation_connection */ ; +/*!50003 SET character_set_client = utf8 */ ; +/*!50003 SET character_set_results = utf8 */ ; +/*!50003 SET collation_connection = utf8_general_ci */ ; +/*!50003 SET @saved_sql_mode = @@sql_mode */ ; +/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `Velocity_Add`() +BEGIN + +DECLARE bol_EXISTS BOOL; +DECLARE datMAX DATETIME; +DECLARE v_buffer VARCHAR(11); +DECLARE v_sql VARCHAR(255); + + + +SELECT MAX(Fecha) INTO datMAX FROM bi.Velocity; + +IF Date(datMAX) = CURDATE() AND hour(datMAX) > hour(now()) THEN + + + SET v_buffer = vn2008.buffer_name(CURDATE(),1); + + SELECT count(*) INTO bol_EXISTS FROM information_schema.`TABLES` + WHERE TABLE_NAME = CONVERT(v_buffer using utf8) collate utf8_general_ci; + + IF bol_EXISTS THEN + + SET v_sql = sql_printf ( + 'INSERT INTO bi.Velocity(tipo_id, Fecha, Disponible, Visible) + SELECT A.tipo_id, NOW(), sum(avalaible), sum(visible) + FROM vn2008.%t b + JOIN Articles A ON b.item_id = A.Id_Article GROUP BY tipo_id; + ', + v_buffer + ); + + + CALL util.exec (v_sql); + + END IF; + +END IF; + +END ;; +DELIMITER ; +/*!50003 SET sql_mode = @saved_sql_mode */ ; +/*!50003 SET character_set_client = @saved_cs_client */ ; +/*!50003 SET character_set_results = @saved_cs_results */ ; +/*!50003 SET collation_connection = @saved_col_connection */ ; + -- -- Current Database: `cache` -- @@ -81980,6 +84559,221 @@ USE `bs`; /*!50001 SET character_set_results = @saved_cs_results */; /*!50001 SET collation_connection = @saved_col_connection */; +-- +-- Current Database: `bi` +-- + +USE `bi`; + +-- +-- Final view structure for view `analisis_grafico_ventas` +-- + +/*!50001 DROP TABLE IF EXISTS `analisis_grafico_ventas`*/; +/*!50001 DROP VIEW IF EXISTS `analisis_grafico_ventas`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `analisis_grafico_ventas` AS select `analisis_ventas`.`Año` AS `Año`,`analisis_ventas`.`Semana` AS `Semana`,sum(`analisis_ventas`.`Importe`) AS `Importe` from `analisis_ventas` group by `analisis_ventas`.`Año`,`analisis_ventas`.`Semana` */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `analisis_ventas_simple` +-- + +/*!50001 DROP TABLE IF EXISTS `analisis_ventas_simple`*/; +/*!50001 DROP VIEW IF EXISTS `analisis_ventas_simple`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `analisis_ventas_simple` AS select `analisis_ventas`.`Año` AS `Año`,`analisis_ventas`.`Semana` AS `Semana`,sum(`analisis_ventas`.`Importe`) AS `Importe` from `analisis_ventas` group by `analisis_ventas`.`Año`,`analisis_ventas`.`Semana` */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `claims_ratio` +-- + +/*!50001 DROP TABLE IF EXISTS `claims_ratio`*/; +/*!50001 DROP VIEW IF EXISTS `claims_ratio`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `claims_ratio` AS select `cr`.`clientFk` AS `Id_Cliente`,`cr`.`yearSale` AS `Consumo`,`cr`.`claimAmount` AS `Reclamaciones`,`cr`.`claimingRate` AS `Ratio`,`cr`.`priceIncreasing` AS `recobro`,`cr`.`packingRate` AS `inflacion` from `vn`.`claimRatio` `cr` */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `customerRiskOverdue` +-- + +/*!50001 DROP TABLE IF EXISTS `customerRiskOverdue`*/; +/*!50001 DROP VIEW IF EXISTS `customerRiskOverdue`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `customerRiskOverdue` AS select `cr`.`customer_id` AS `customer_id`,`cr`.`amount` AS `amount`,`cr`.`company_id` AS `company_id` from (((`bi`.`customer_risk` `cr` join `vn`.`company` `co` on((`co`.`id` = `cr`.`company_id`))) join `vn`.`client` `c` on((`cr`.`customer_id` = `c`.`id`))) join `vn`.`payMethod` `pm` on((`pm`.`id` = `c`.`payMethodFk`))) where (`cr`.`amount` and `co`.`isDefaulter` and `pm`.`outstandingDebt` and (`c`.`typeFk` = 'normal')) union all select `i`.`clientFk` AS `Id_Cliente`,-(round(`i`.`amount`,2)) AS `importe`,`i`.`companyFk` AS `empresa_id` from (((`vn`.`invoiceOut` `i` join `vn`.`client` `c` on((`i`.`clientFk` = `c`.`id`))) join `vn`.`company` `co` on((`co`.`id` = `i`.`companyFk`))) join `vn`.`payMethod` `pm` on((`pm`.`id` = `c`.`payMethodFk`))) where ((`i`.`issued` > (curdate() + interval -(101) day)) and ((`i`.`dued` + interval `pm`.`graceDays` day) > curdate()) and (`i`.`amount` > 0) and `co`.`isDefaulter` and `pm`.`outstandingDebt` and (`c`.`typeFk` = 'normal')) */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `customer_risk` +-- + +/*!50001 DROP TABLE IF EXISTS `customer_risk`*/; +/*!50001 DROP VIEW IF EXISTS `customer_risk`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `customer_risk` AS select `cr`.`clientFk` AS `customer_id`,`cr`.`companyFk` AS `company_id`,`cr`.`amount` AS `amount` from `vn`.`clientRisk` `cr` */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `defaulters` +-- + +/*!50001 DROP TABLE IF EXISTS `defaulters`*/; +/*!50001 DROP VIEW IF EXISTS `defaulters`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `defaulters` AS select `d`.`clientFk` AS `client`,`d`.`created` AS `date`,`d`.`amount` AS `amount`,`d`.`defaulterSinced` AS `defaulterSince`,`d`.`hasChanged` AS `hasChanged`,`d`.`frozened` AS `frozened` from `bs`.`defaulter` `d` */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `facturacion_media_anual` +-- + +/*!50001 DROP TABLE IF EXISTS `facturacion_media_anual`*/; +/*!50001 DROP VIEW IF EXISTS `facturacion_media_anual`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `facturacion_media_anual` AS select `cac`.`clientFk` AS `Id_Cliente`,`cac`.`invoiced` AS `Consumo` from `bs`.`clientAnnualConsumption` `cac` */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `rotacion` +-- + +/*!50001 DROP TABLE IF EXISTS `rotacion`*/; +/*!50001 DROP VIEW IF EXISTS `rotacion`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=MERGE */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `rotacion` AS select `ic`.`itemFk` AS `Id_Article`,`ic`.`warehouseFk` AS `warehouse_id`,`ic`.`quantity` AS `total`,`ic`.`rotation` AS `rotacion`,`ic`.`cm3` AS `cm3`,`ic`.`storage` AS `almacenaje`,`ic`.`handling` AS `manipulacion`,`ic`.`extraCharge` AS `auxiliar`,`ic`.`wasted` AS `mermas`,`ic`.`cm3delivery` AS `cm3reparto`,`ic`.`grams` AS `grams` from `vn`.`itemCost` `ic` */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `saleVolume__` +-- + +/*!50001 DROP TABLE IF EXISTS `saleVolume__`*/; +/*!50001 DROP VIEW IF EXISTS `saleVolume__`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8mb4 */; +/*!50001 SET character_set_results = utf8mb4 */; +/*!50001 SET collation_connection = utf8mb4_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `saleVolume__` AS select `s`.`id` AS `saleFk`,sum((((`r`.`cm3` * `i`.`compression`) * `s`.`quantity`) / 1000000)) AS `m3` from (((`vn`.`ticket` `t` join `vn`.`sale` `s` on((`s`.`ticketFk` = `t`.`id`))) join `vn`.`item` `i` on((`i`.`id` = `s`.`itemFk`))) join `bi`.`rotacion` `r` on(((`r`.`Id_Article` = `i`.`id`) and (`r`.`warehouse_id` = `t`.`warehouseFk`)))) */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `tarifa_componentes` +-- + +/*!50001 DROP TABLE IF EXISTS `tarifa_componentes`*/; +/*!50001 DROP VIEW IF EXISTS `tarifa_componentes`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `tarifa_componentes` AS select `c`.`id` AS `Id_Componente`,`c`.`name` AS `Componente`,`c`.`typeFk` AS `tarifa_componentes_series_id`,`c`.`classRate` AS `tarifa_class`,`c`.`tax` AS `tax`,`c`.`isRenewable` AS `is_renewable`,`c`.`code` AS `code` from `vn`.`component` `c` */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `tarifa_componentes_series` +-- + +/*!50001 DROP TABLE IF EXISTS `tarifa_componentes_series`*/; +/*!50001 DROP VIEW IF EXISTS `tarifa_componentes_series`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`%` SQL SECURITY DEFINER */ +/*!50001 VIEW `tarifa_componentes_series` AS select `ct`.`id` AS `tarifa_componentes_series_id`,`ct`.`type` AS `Serie`,`ct`.`isBase` AS `base`,`ct`.`isMargin` AS `margen` from `vn`.`componentType` `ct` */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + -- -- Current Database: `cache` -- @@ -85241,4 +88035,4 @@ USE `vncontrol`; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2021-06-22 14:38:24 +-- Dump completed on 2021-06-22 16:15:53 diff --git a/db/export-structure.sh b/db/export-structure.sh index c5c65ef34..e2a5a618b 100755 --- a/db/export-structure.sh +++ b/db/export-structure.sh @@ -3,6 +3,7 @@ SCHEMAS=( account bs + bi cache edi hedera