149 lines
4.2 KiB
SQL
149 lines
4.2 KiB
SQL
USE `vn`;
|
||
DROP procedure IF EXISTS `catalog_calculate`;
|
||
|
||
DELIMITER $$
|
||
USE `vn`$$
|
||
CREATE DEFINER=`root`@`%` PROCEDURE `catalog_calculate`(
|
||
vLanded DATE,
|
||
vAddressFk INT,
|
||
vAgencyModeFk INT)
|
||
proc: BEGIN
|
||
/**
|
||
* Calcula los articulos disponibles y sus precios
|
||
*
|
||
* @table tmp.item(itemFk) Listado de artÃculos a calcular
|
||
* @param vLanded Fecha de recepcion de mercancia
|
||
* @param vAddressFk Id del consignatario
|
||
* @param vAgencyModeFk Id de la agencia
|
||
* @return tmp.ticketCalculateItem(itemFk, available, producer,
|
||
* item, size, stems, category, inkFk, image, origin, price)
|
||
* @return tmp.ticketLot(warehouseFk, itemFk, available, buyFk)
|
||
* @return tmp.ticketComponent
|
||
* @return tmp.ticketComponentPrice
|
||
* @return tmp.zoneGetShipped
|
||
*/
|
||
|
||
DECLARE vAvailableCalc INT;
|
||
DECLARE vShipped DATE;
|
||
DECLARE vWarehouseFk SMALLINT;
|
||
DECLARE vZoneFk INT;
|
||
DECLARE vDone BOOL;
|
||
DECLARE cTravelTree CURSOR FOR
|
||
SELECT zoneFk, warehouseFk, shipped FROM tmp.zoneGetShipped;
|
||
|
||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
||
|
||
-- Establece los almacenes y las fechas que van a entrar al disponible
|
||
|
||
CALL vn.zone_getShipped (vLanded, vAddressFk, vAgencyModeFk, FALSE);
|
||
|
||
DROP TEMPORARY TABLE IF EXISTS tmp.ticketLot;
|
||
CREATE TEMPORARY TABLE tmp.ticketLot(
|
||
`warehouseFk` smallint(5) unsigned NOT NULL,
|
||
`itemFk` int(11) NOT NULL,
|
||
`available` double DEFAULT NULL,
|
||
`buyFk` int(11) DEFAULT NULL,
|
||
`fix` tinyint(3) unsigned DEFAULT '0',
|
||
`zoneFk` int(11) NOT NULL,
|
||
KEY `itemFk` (`itemFk`),
|
||
KEY `item_warehouse` (`itemFk`,`warehouseFk`) USING HASH
|
||
) ENGINE=MEMORY DEFAULT CHARSET=utf8;
|
||
|
||
CALL catalog_componentPrepare();
|
||
|
||
DROP TEMPORARY TABLE IF EXISTS tmp.ticketCalculateItem;
|
||
CREATE TEMPORARY TABLE tmp.ticketCalculateItem(
|
||
itemFk INT(11) NOT NULL,
|
||
available INT(11),
|
||
producer VARCHAR(50),
|
||
item VARCHAR(50),
|
||
size INT(10) UNSIGNED,
|
||
stems INT(11),
|
||
category VARCHAR(3),
|
||
inkFk VARCHAR(3),
|
||
image VARCHAR(50),
|
||
origin VARCHAR(3),
|
||
price DECIMAL(10,2),
|
||
priceKg DECIMAL(10,2),
|
||
KEY `itemFk` (`itemFk`)
|
||
) ENGINE = MEMORY DEFAULT CHARSET=utf8;
|
||
|
||
OPEN cTravelTree;
|
||
|
||
l: LOOP
|
||
SET vDone = FALSE;
|
||
FETCH cTravelTree INTO vZoneFk, vWarehouseFk, vShipped;
|
||
|
||
IF vDone THEN
|
||
LEAVE l;
|
||
END IF;
|
||
|
||
CALL `cache`.available_refresh (vAvailableCalc, FALSE, vWarehouseFk, vShipped);
|
||
CALL buyUltimate (vWarehouseFk, vShipped);
|
||
|
||
INSERT INTO tmp.ticketLot (warehouseFk, itemFk, available, buyFk, zoneFk)
|
||
SELECT vWarehouseFk,
|
||
i.item_id,
|
||
IFNULL(i.available, 0),
|
||
bu.buyFk,
|
||
vZoneFk
|
||
FROM `cache`.available i
|
||
JOIN tmp.item br ON br.itemFk = i.item_id
|
||
LEFT JOIN item it ON it.id = i.item_id
|
||
LEFT JOIN tmp.buyUltimate bu ON bu.itemFk = i.item_id
|
||
WHERE i.calc_id = vAvailableCalc
|
||
AND i.available > 0;
|
||
|
||
DROP TEMPORARY TABLE tmp.buyUltimate;
|
||
|
||
CALL vn.catalog_componentCalculate(vZoneFk, vAddressFk, vShipped, vWarehouseFk);
|
||
|
||
INSERT INTO tmp.ticketCalculateItem (
|
||
itemFk,
|
||
available,
|
||
producer,
|
||
item,
|
||
size,
|
||
stems,
|
||
category,
|
||
inkFk,
|
||
image,
|
||
origin,
|
||
price,
|
||
priceKg)
|
||
SELECT
|
||
tl.itemFk,
|
||
SUM(tl.available) available,
|
||
p.name producer,
|
||
i.name item,
|
||
i.size size,
|
||
i.stems,
|
||
i.category,
|
||
i.inkFk,
|
||
i.image,
|
||
o.code origin,
|
||
bl.price,
|
||
bl.priceKg
|
||
FROM tmp.ticketLot tl
|
||
JOIN item i ON tl.itemFk = i.id
|
||
LEFT JOIN producer p ON p.id = i.producerFk AND p.isVisible
|
||
JOIN origin o ON o.id = i.originFk
|
||
JOIN (
|
||
SELECT MIN(price) price, itemFk, priceKg
|
||
FROM tmp.ticketComponentPrice
|
||
WHERE warehouseFk = vWarehouseFk
|
||
GROUP BY itemFk
|
||
) bl ON bl.itemFk = tl.itemFk
|
||
WHERE tl.zoneFk = vZoneFk AND tl.warehouseFk = vWarehouseFk
|
||
GROUP BY tl.itemFk;
|
||
-- on duplicatekey update
|
||
|
||
END LOOP;
|
||
|
||
CLOSE cTravelTree;
|
||
|
||
END$$
|
||
|
||
DELIMITER ;
|
||
|