diff --git a/db/routines/cache/procedures/available_refresh.sql b/db/routines/cache/procedures/available_refresh.sql index cae0458ff..d0939e568 100644 --- a/db/routines/cache/procedures/available_refresh.sql +++ b/db/routines/cache/procedures/available_refresh.sql @@ -5,7 +5,8 @@ proc: BEGIN DECLARE vReserveDate DATETIME; DECLARE vParams CHAR(100); DECLARE vInventoryDate DATE; - DECLARE vIsLogifloraDay BOOLEAN; + DECLARE vLifeScope DATE; + DECLARE vWarehouseFkInventory INT; DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN @@ -26,43 +27,42 @@ proc: BEGIN LEAVE proc; END IF; - -- Invoca al procedimiento que genera el stock virtual de Logiflora, si coincide con la peticion de refresco del disponible - IF vn.isLogifloraDay(vDated, vWarehouse) THEN - -- CALL edi.floramondo_offerRefresh; - SET vIsLogifloraDay = TRUE; - ELSE - SET vIsLogifloraDay = FALSE; - END IF; - -- Calcula algunos parĂ¡metros necesarios SET vStartDate = TIMESTAMP(vDated, '00:00:00'); SELECT inventoried INTO vInventoryDate FROM vn.config; + SELECT DATE_SUB(vStartDate, INTERVAL MAX(life) DAY) INTO vLifeScope FROM vn.itemType; SELECT SUBTIME(util.VN_NOW(), reserveTime) INTO vReserveDate FROM hedera.orderConfig; + SELECT w.id INTO vWarehouseFkInventory + FROM vn.warehouse w + WHERE w.code = 'inv'; + -- Calcula el ultimo dia de vida para cada producto DROP TEMPORARY TABLE IF EXISTS itemRange; CREATE TEMPORARY TABLE itemRange (PRIMARY KEY (itemFk)) ENGINE = MEMORY - SELECT c.itemFk, - IF(it.life IS NULL, - NULL, - TIMESTAMP(TIMESTAMPADD(DAY, it.life, c.landing), '23:59:59')) ended - FROM ( - SELECT b.itemFk, MAX(t.landed) landing - FROM vn.buy b - JOIN vn.entry e ON b.entryFk = e.id - JOIN vn.travel t ON t.id = e.travelFk - JOIN vn.warehouse w ON w.id = t.warehouseInFk - WHERE t.landed BETWEEN vInventoryDate AND vStartDate - AND t.warehouseInFk = vWarehouse - AND NOT e.isExcludedFromAvailable - GROUP BY b.itemFk - ) c - JOIN vn.item i ON i.id = c.itemFk + SELECT i.id itemFk, + util.dayEnd(DATE_ADD(c.maxLanded, INTERVAL it.life DAY)) ended, it.life + FROM vn.item i + LEFT JOIN ( + SELECT b.itemFk, MAX(t.landed) maxLanded + FROM vn.buy b + JOIN vn.entry e ON b.entryFk = e.id + JOIN vn.travel t ON t.id = e.travelFk + JOIN vn.warehouse w ON w.id = t.warehouseInFk + JOIN vn.item i ON i.id = b.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + WHERE t.landed BETWEEN vLifeScope AND vStartDate + AND t.warehouseInFk = vWarehouse + AND t.warehouseOutFk <> vWarehouseFkInventory + AND it.life + AND NOT e.isExcludedFromAvailable + GROUP BY b.itemFk + ) c ON i.id = c.itemFk JOIN vn.itemType it ON it.id = i.typeFk - HAVING ended >= vStartDate OR ended IS NULL; + HAVING ended >= vStartDate OR life IS NULL; -- Calcula el ATP DELETE FROM available WHERE calc_id = vCalc; @@ -86,7 +86,7 @@ proc: BEGIN WHERE i.landed >= vStartDate AND (ir.ended IS NULL OR i.landed <= ir.ended) AND i.warehouseInFk = vWarehouse - AND (ISNULL(wf.entryFk) OR vIsLogifloraDay) + AND ISNULL(wf.entryFk) UNION ALL SELECT i.itemFk, i.shipped, i.quantity FROM vn.itemEntryOut i