salix/db/changes/10180-holyWeek/03-ekt_load.sql

162 lines
3.8 KiB
MySQL
Raw Normal View History

2020-05-15 11:11:23 +00:00
USE `edi`;
DROP procedure IF EXISTS `ekt_load`;
DELIMITER $$
USE `edi`$$
2020-04-30 05:20:43 +00:00
CREATE DEFINER=`root`@`%` PROCEDURE `ekt_load`(IN `vSelf` INT)
BEGIN
DECLARE vRef INT;
DECLARE vBuy INT;
DECLARE vItem INT;
DECLARE vQty INT;
DECLARE vPackage INT;
DECLARE vIsLot BOOLEAN;
DECLARE vForceToPacking INT DEFAULT 2;
-- Carga los datos necesarios del EKT
SELECT ref, qty, package INTO vRef, vQty, vPackage
FROM ekt e
LEFT JOIN item i ON e.ref = i.id
WHERE e.id = vSelf;
-- Inserta el cubo si no existe
IF vPackage = 800
THEN
SET vPackage = 800 + vQty;
INSERT IGNORE INTO vn2008.Cubos SET
Id_Cubo = vPackage,
x = 7200 / vQty,
y = 1;
ELSE
INSERT IGNORE INTO vn2008.Cubos (Id_Cubo, X, Y, Z)
SELECT bucket_id, ROUND(x_size/10), ROUND(y_size/10), ROUND(z_size/10)
FROM bucket WHERE bucket_id = vPackage;
IF ROW_COUNT() > 0
THEN
INSERT INTO vn2008.mail SET
`subject` = 'Cubo añadido',
`text` = CONCAT('Se ha añadido el cubo: ', vPackage),
`to` = 'ekt@verdnatura.es';
END IF;
END IF;
-- Intenta obtener el artículo en base a los atributos holandeses
INSERT IGNORE INTO item_track SET
item_id = vRef;
SELECT c.Id_Compra, c.Id_Article INTO vBuy, vItem
FROM vn2008.buy_edi e
JOIN item_track t ON t.item_id = e.ref
LEFT JOIN vn2008.buy_edi l ON l.ref = e.ref
LEFT JOIN vn2008.Compres c ON c.buy_edi_id = l.id
JOIN vn2008.config cfg
WHERE e.id = vSelf
AND l.id != vSelf
AND c.Id_Article != cfg.generic_item
AND IF(t.s1, l.s1 = e.s1, TRUE)
AND IF(t.s2, l.s2 = e.s2, TRUE)
AND IF(t.s3, l.s3 = e.s3, TRUE)
AND IF(t.s4, l.s4 = e.s4, TRUE)
AND IF(t.s5, l.s5 = e.s5, TRUE)
AND IF(t.s6, l.s6 = e.s6, TRUE)
AND IF(t.kop, l.kop = e.kop, TRUE)
AND IF(t.pac, l.pac = e.pac, TRUE)
AND IF(t.cat, l.cat = e.cat, TRUE)
AND IF(t.ori, l.ori = e.ori, TRUE)
AND IF(t.pro, l.pro = e.pro, TRUE)
AND IF(t.sub, l.sub = e.sub, TRUE)
AND IF(t.package, l.package = e.package, TRUE)
AND c.Id_Article < 170000
ORDER BY l.now DESC, c.Id_Compra ASC LIMIT 1;
-- Determina si el articulo se vende por lotes
IF vItem
THEN
SELECT COUNT(*) > 0 INTO vIsLot
FROM vn2008.Articles a
LEFT JOIN vn2008.Tipos t ON t.tipo_id = a.tipo_id
WHERE a.Id_Article = vItem
AND t.`transaction`;
-- Si el articulo se vende por lotes se inserta un nuevo artículo
IF vIsLot
THEN
INSERT INTO vn2008.Articles (
Article
,Medida
,Categoria
,Id_Origen
,iva_group_id
,Foto
,Color
,Codintrastat
,tipo_id
,Tallos
)
SELECT
i.`name`
,IFNULL(e.s1, e.pac)
,e.cat
,IFNULL(o.id, 17)
,IFNULL(a.iva_group_id, 1)
,a.Foto
,a.Color
,a.Codintrastat
,IFNULL(a.tipo_id, 10)
,IF(a.tipo_id = 15, 0, 1)
FROM vn2008.buy_edi e
LEFT JOIN item i ON i.id = e.ref
LEFT JOIN vn2008.Origen o ON o.Abreviatura = e.ori
LEFT JOIN vn2008.Articles a ON a.Id_Article = vItem
WHERE e.id = vSelf;
SET vItem = LAST_INSERT_ID();
END IF;
END IF;
-- Inserta la compra asociada al EKT
INSERT INTO vn2008.Compres
(
Id_Entrada
,buy_edi_id
,Costefijo
,Id_Article
,`grouping`
,caja
,Packing
,Cantidad
,Productor
,Etiquetas
,Id_Cubo
,`weight`
)
SELECT
cfg.edi_entry
,vSelf
,(@t := IF(a.Tallos, a.Tallos, 1)) * e.pri
,IFNULL(vItem, cfg.generic_item)
,IFNULL(c.`grouping`, e.pac)
,vForceToPacking
,@pac := e.pac / @t
,@pac * e.qty
,s.company_name
,e.qty
,IFNULL(c.Id_Cubo, e.package)
,a.density * (vn.item_getVolume(a.Id_Article, IFNULL(c.Id_Cubo, e.package)) / 1000000)
FROM vn2008.buy_edi e
LEFT JOIN vn2008.Compres c ON c.Id_Compra = vBuy
LEFT JOIN vn2008.Articles a ON a.Id_Article = c.Id_Article
LEFT JOIN supplier s ON e.pro = s.supplier_id
JOIN vn2008.config cfg
WHERE e.id = vSelf
LIMIT 1;
END