cau 9939 refactor db/changes files and update structure db

This commit is contained in:
Bernat 2019-02-25 08:25:53 +01:00
parent 04280c5b5a
commit 45527a43da
8 changed files with 4 additions and 336 deletions

View File

@ -1,4 +0,0 @@
CREATE TABLE `vn`.`localLog` (
`id` INT NOT NULL AUTO_INCREMENT,
`ticketFk` VARCHAR(45) NULL,
PRIMARY KEY (`id`));

View File

@ -1,21 +0,0 @@
DROP TRIGGER IF EXISTS `vn2008`.`TicketsAfterUpdate`;
DELIMITER $$
USE `vn2008`$$
CREATE DEFINER=`root`@`%` TRIGGER `vn2008`.`TicketsAfterUpdate`
AFTER UPDATE ON `Tickets` FOR EACH ROW
BEGIN
IF NEW.Id_Ruta IS NULL AND OLD.Id_Ruta IS NOT NULL THEN
INSERT INTO vn.routeLog(originFk, userFk, `action`, description)
VALUES (OLD.Id_Ruta, account.userGetId(), 'update', CONCAT('Saca el ticket ', OLD.Id_Ticket, ' de la ruta'));
ELSEIF NOT (NEW.Id_Ruta <=> OLD.Id_Ruta) THEN
INSERT INTO vn.routeLog(originFk, userFk, `action`, description)
VALUES (NEW.Id_Ruta, account.userGetId(), 'update', CONCAT('Añade el ticket ', OLD.Id_Ticket, ' a la ruta'));
END IF;
CALL stock.queueAdd ('ticket', NEW.Id_Ticket, OLD.Id_Ticket);
INSERT INTO vn.localLog(ticketFk)
VALUES(NEW.Id_Ticket);
END$$
DELIMITER ;

View File

@ -1,58 +0,0 @@
DROP TRIGGER IF EXISTS `vn2008`.`ClientesBeforeUpdate`;
DELIMITER $$
USE `vn2008`$$
CREATE DEFINER=`root`@`%` TRIGGER `ClientesBeforeUpdate`
BEFORE UPDATE ON `Clientes`
FOR EACH ROW
BEGIN
DECLARE vText VARCHAR(255) DEFAULT NULL;
DECLARE isAlreadyUsedIf BOOLEAN;
SELECT (COUNT(*) > 1) INTO isAlreadyUsedIf
FROM Clientes
WHERE `IF` = TRIM(NEW.`IF`);
IF isAlreadyUsedIf THEN
CALL util.throw ('Error. El NIF/CIF está repetido');
END IF;
-- Comprueba que el formato de los teléfonos es válido
IF !(NEW.telefono <=> OLD.telefono) THEN
CALL pbx.phoneIsValid (NEW.telefono);
END IF;
IF !(NEW.movil <=> OLD.movil) THEN
CALL pbx.phoneIsValid (NEW.movil);
END IF;
IF !(NEW.fax <=> OLD.fax) THEN
CALL pbx.phoneIsValid (NEW.fax);
END IF;
-- Avisar al comercial si ha llegado la documentación sepa/core
IF NEW.sepavnl AND !OLD.sepavnl THEN
SET vText = 'Sepa de VNL';
END IF;
IF NEW.corevnl AND !OLD.corevnl THEN
SET vText = 'Core de VNL';
END IF;
IF vText IS NOT NULL
THEN
INSERT INTO mail(`to`, reply_to, subject, text)
SELECT
CONCAT(IF(ac.id,name, 'jgallego'), '@verdnatura.es'),
'administracion@verdnatura.es',
CONCAT('Cliente ', NEW.Id_Cliente),
CONCAT('Recibida la documentación: ', vText)
FROM Trabajadores t
LEFT JOIN account.user u ON t.user_id = u.id AND active
LEFT JOIN account.account ac ON ac.id = u.id
WHERE t.Id_Trabajador = NEW.Id_Trabajador;
END IF;
END$$
DELIMITER ;

View File

@ -1,17 +0,0 @@
USE `vn`;
CREATE
OR REPLACE ALGORITHM = UNDEFINED
DEFINER = `root`@`%`
SQL SECURITY DEFINER
VIEW `itemType` AS
SELECT
`t`.`tipo_id` AS `id`,
`t`.`Id_Tipo` AS `code`,
`t`.`Tipo` AS `name`,
`t`.`reino_id` AS `categoryFk`,
`t`.`life` AS `life`,
`t`.`Id_Trabajador` AS `workerFk`,
`t`.`warehouseFk` AS `warehouseFk`,
`t`.`isPackaging` AS `isPackaging`
FROM
`vn2008`.`Tipos` `t`;

View File

@ -1,154 +0,0 @@
USE `vn`;
DROP procedure IF EXISTS `ticketComponentUpdateSale`;
DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `ticketComponentUpdateSale`(vOption INT)
BEGIN
/**
* A partir de la tabla tmp.sale, crea los Movimientos_componentes
* y modifica el campo Preu de la tabla Movimientos
*
* @param i_option integer tipo de actualizacion
* @param table tmp.sale tabla memory con el campo Id_Movimiento, warehouse_id
**/
DECLARE vComponentFk INT;
DECLARE vRenewComponents BOOLEAN;
DECLARE vKeepPrices BOOLEAN;
CASE vOption
WHEN 1 THEN
SET vRenewComponents = TRUE;
SET vKeepPrices = FALSE;
WHEN 2 THEN
SET vComponentFk = 17;
SET vRenewComponents = TRUE;
SET vKeepPrices = TRUE;
WHEN 3 THEN
SET vComponentFk = 37;
SET vRenewComponents = TRUE;
SET vKeepPrices = TRUE;
WHEN 4 THEN
SET vComponentFk = 34;
SET vRenewComponents = TRUE;
SET vKeepPrices = TRUE;
WHEN 5 THEN
SET vComponentFk = 35;
SET vRenewComponents = TRUE;
SET vKeepPrices = TRUE;
WHEN 6 THEN
SET vComponentFk = 36;
SET vRenewComponents = TRUE;
SET vKeepPrices = TRUE;
WHEN 7 THEN
REPLACE INTO saleComponent(saleFk, componentFk, value)
SELECT s.id, 28, ROUND(((s.price * (100 - s.discount) / 100) - SUM(IFNULL(sc.value, 0))) * 0.8, 3)
FROM sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id
LEFT JOIN saleComponent sc ON sc.saleFk = s.id
AND sc.componentFk NOT IN (28, 29)
GROUP BY s.id;
REPLACE INTO saleComponent(saleFk, componentFk, value)
SELECT s.id, 29, ROUND(((s.price * (100 - s.discount) / 100) - SUM(IFNULL(sc.value, 0))) * 0.2, 3)
FROM sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id
LEFT JOIN saleComponent sc ON sc.saleFk = s.id
AND sc.componentFk NOT IN (28, 29)
GROUP BY s.id;
SET vRenewComponents = FALSE;
SET vKeepPrices = FALSE;
WHEN 8 THEN
DELETE sc.*
FROM tmp.sale tmps JOIN saleComponent sc ON sc.saleFk = tmps.saleFk;
REPLACE INTO saleComponent(saleFk, componentFk, value)
SELECT s.id, 28, ROUND(((s.price * (100 - s.discount) / 100)), 3)
FROM sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id;
SET vRenewComponents = FALSE;
SET vKeepPrices = FALSE;
WHEN 9 THEN
SET vRenewComponents = TRUE;
SET vKeepPrices = TRUE;
END CASE;
IF vRenewComponents THEN
DELETE sc.*
FROM tmp.sale tmps
JOIN saleComponent sc ON sc.saleFk = tmps.saleFk
JOIN componentRate cr ON cr.id = sc.componentFk
WHERE cr.isRenewable;
REPLACE INTO saleComponent(saleFk, componentFk, value)
SELECT s.id, tc.componentFk, ROUND(tc.cost,3)
FROM sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id
JOIN tmp.ticketComponent tc ON tc.itemFk = s.itemFk AND tc.warehouseFk = tmps.warehouseFk
LEFT JOIN saleComponent sc ON sc.saleFk = s.id
AND sc.componentFk = tc.componentFk
LEFT JOIN componentRate cr ON cr.id = tc.componentFk
WHERE IF(sc.componentFk IS NULL AND NOT cr.isRenewable, FALSE, TRUE);
END IF;
IF vKeepPrices THEN
REPLACE INTO saleComponent(saleFk, componentFk, value)
SELECT s.id, vComponentFk, ROUND((s.price * (100 - s.discount) / 100) - SUM(sc.value), 3) dif
FROM sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id
LEFT JOIN saleComponent sc ON sc.saleFk = s.id
WHERE sc.saleFk <> vComponentFk
GROUP BY s.id
HAVING dif <> 0;
ELSE
UPDATE sale s
JOIN item i on i.id = s.itemFk
JOIN itemType it on it.id = i.typeFk
JOIN (SELECT SUM(sc.value) sumValue, sc.saleFk
FROM saleComponent sc
JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk
GROUP BY sc.saleFk) sc ON sc.saleFk = s.id
SET s.price = sumValue
WHERE it.code != 'PRT';
REPLACE INTO saleComponent(saleFk, componentFk, value)
SELECT s.id, 21, ROUND((s.price * (100 - s.discount) / 100) - sum(value),3) saleValue
FROM sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id
LEFT JOIN saleComponent sc ON sc.saleFk = s.id
WHERE sc.componentFk != 21
GROUP BY s.id
HAVING ROUND(saleValue, 4) <> 0;
END IF;
UPDATE sale s
JOIN (
SELECT SUM(sc.value) sumValue, sc.saleFk
FROM saleComponent sc
JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk
JOIN componentRate cr ON cr.id = sc.componentFk
JOIN componentTypeRate ctr on ctr.id = cr.componentTypeRate AND ctr.base
GROUP BY sc.saleFk) sc ON sc.saleFk = s.id
SET s.priceFixed = sumValue, s.isPriceFixed = 1;
DELETE sc.*
FROM saleComponent sc
JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk
JOIN sale s on s.id = sc.saleFk
JOIN item i ON i.id = s.itemFk
JOIN itemType it ON it.id = i.typeFk
WHERE it.code = 'PRT';
INSERT INTO saleComponent(saleFk, componentFk, value)
SELECT s.id, 15, ROUND(s.price,3)
FROM sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id
JOIN item i ON i.id = s.itemFK
JOIN itemType it ON it.id = i.typeFk
WHERE it.code = 'PRT' AND s.price > 0;
END$$
DELIMITER ;

View File

@ -1,40 +0,0 @@
DROP TRIGGER IF EXISTS `vn2008`.`CompresAfterInsert`;
DELIMITER $$
USE `vn2008`$$
CREATE DEFINER=`root`@`%` TRIGGER `CompresAfterInsert` AFTER INSERT ON `Compres` FOR EACH ROW BEGIN
DECLARE vWarehouse INT;
DECLARE vWarehouseOut INT;
DECLARE isMerchandise BOOLEAN;
CALL stock.queueAdd ('buy', NEW.Id_Compra, NULL);
SELECT warehouse_id, warehouse_id_out INTO vWarehouse, vWarehouseOut
FROM Entradas E
JOIN travel T ON T.id = E.travel_id
WHERE E.Id_Entrada = NEW.Id_Entrada;
-- Actualiza el volumen unitario
SELECT k.merchandise INTO isMerchandise
FROM vn.itemCategory k
JOIN vn.itemType it ON it.categoryFk = k.id
JOIN vn.item i ON i.typeFk = it.id
WHERE i.id = NEW.Id_Article;
If isMerchandise THEN
REPLACE bi.rotacion(Id_Article, warehouse_id, cm3)
VALUES (NEW.Id_ARticle, vWarehouse, vn2008.cm3_unidad(NEW.Id_Compra));
END IF;
IF (SELECT fuente FROM warehouse where id = vWarehouseOut AND id <> 13) AND
(SELECT has_notify_passport(NEW.Id_Article,NEW.Id_Compra))
THEN
CALL notify_passport(NEW.Id_Article, NEW.Etiquetas, NEW.Packing,NEW.Id_Compra);
CALL insert_producer_plantpassport(NEW.Id_Compra);
END IF;
END$$
DELIMITER ;

View File

@ -1,38 +0,0 @@
DROP TRIGGER IF EXISTS `vn2008`.`CompresAfterUpdate`;
DELIMITER $$
USE `vn2008`$$
CREATE DEFINER=`root`@`%` TRIGGER `CompresAfterUpdate`
AFTER UPDATE ON `Compres` FOR EACH ROW
BEGIN
DECLARE vWarehouse INT;
DECLARE vWarehouseOut INT;
DECLARE isMerchandise BOOLEAN;
CALL stock.queueAdd ('buy', NEW.Id_Compra, OLD.Id_Compra);
SELECT warehouse_id, warehouse_id_out INTO vWarehouse, vWarehouseOut
FROM Entradas E
JOIN travel T ON T.id = E.travel_id
WHERE E.Id_Entrada = NEW.Id_Entrada;
SELECT k.merchandise INTO isMerchandise
FROM vn.itemCategory k
JOIN vn.itemType it ON it.categoryFk = k.id
JOIN vn.item i ON i.typeFk = it.id
WHERE i.id = NEW.Id_Article;
IF isMerchandise AND NOT(NEW.Id_Cubo <=> OLD.Id_Cubo AND NEW.Id_Entrada <=> OLD.Id_Entrada)
THEN
REPLACE bi.rotacion(Id_Article, warehouse_id, cm3)
VALUES (NEW.Id_ARticle, vWarehouse, vn2008.cm3_unidad(NEW.Id_Compra));
END IF;
IF (SELECT fuente FROM warehouse where id = vWarehouseOut AND id <> 13) AND
(SELECT has_notify_passport(NEW.Id_Article,NEW.Id_Compra)) > 0
THEN
CALL notify_passport(NEW.Id_Article, NEW.Etiquetas, NEW.Packing,NEW.Id_Compra);
CALL insert_producer_plantpassport(NEW.Id_Compra);
END IF;
END$$
DELIMITER ;

View File

@ -593,11 +593,11 @@ INSERT INTO `vn`.`saleComponent`(`saleFk`, `componentFk`, `value`)
(5, 29, -18.72), (5, 29, -18.72),
(5, 39, 0.02), (5, 39, 0.02),
(6, 23, 6.5), (6, 23, 6.5),
(7, 15, 0.29), (7, 15, 0.2899),
(7, 28, 5.6), (7, 28, 5.6),
(7, 29, -4.6), (7, 29, -4.6),
(7, 39, 0.01), (7, 39, 0.01),
(8, 15, 0.044), (8, 15, 0.0435),
(8, 21, -0.004), (8, 21, -0.004),
(8, 28, 20.72), (8, 28, 20.72),
(8, 29, -19.72), (8, 29, -19.72),
@ -624,7 +624,7 @@ INSERT INTO `vn`.`saleComponent`(`saleFk`, `componentFk`, `value`)
(12, 29, -19.72), (12, 29, -19.72),
(12, 37, 2), (12, 37, 2),
(12, 39, 0.01), (12, 39, 0.01),
(13, 15, 0.29), (13, 15, 0.2899),
(13, 28, 5.6), (13, 28, 5.6),
(13, 29, -4.6), (13, 29, -4.6),
(13, 39, 0.01), (13, 39, 0.01),
@ -927,7 +927,7 @@ INSERT INTO `hedera`.`orderRowComponent`(`rowFk`, `componentFk`, `price`)
(5, 29, -18.72), (5, 29, -18.72),
(5, 39, 0.02), (5, 39, 0.02),
(6, 23, 6.5), (6, 23, 6.5),
(7, 15, 0.29), (7, 15, 0.2899),
(7, 28, 5.6), (7, 28, 5.6),
(7, 29, -4.6), (7, 29, -4.6),
(7, 39, 0.01), (7, 39, 0.01),