2024-01-31 08:54:47 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`available_traslate`(
|
|
|
|
vWarehouseLanding INT,
|
|
|
|
vDated DATE,
|
|
|
|
vWarehouseShipment INT)
|
|
|
|
proc: BEGIN
|
2024-03-28 13:27:29 +00:00
|
|
|
/**
|
|
|
|
* Calcular la disponibilidad dependiendo del almacen de origen y destino según la fecha
|
|
|
|
*
|
|
|
|
* @param vWarehouseLanding almacén de llegada.
|
|
|
|
* @param vDated la fecha para la cual se está calculando la disponibilidad de articulos.
|
|
|
|
* @param vWarehouseShipment almacén de destino.
|
|
|
|
*/
|
2024-01-31 08:54:47 +00:00
|
|
|
DECLARE vDatedFrom DATE;
|
|
|
|
DECLARE vDatedTo DATETIME;
|
|
|
|
DECLARE vDatedReserve DATETIME;
|
|
|
|
DECLARE vDatedInventory DATE;
|
|
|
|
|
|
|
|
IF vDated < util.VN_CURDATE() THEN
|
|
|
|
LEAVE proc;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
CALL item_getStock (vWarehouseLanding, vDated, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
-- Calcula algunos parámetros necesarios
|
|
|
|
SET vDatedFrom = TIMESTAMP(vDated, '00:00:00');
|
|
|
|
SET vDatedTo = TIMESTAMP(TIMESTAMPADD(DAY, 4, vDated), '23:59:59');
|
|
|
|
SELECT FechaInventario INTO vDatedInventory FROM tblContadores;
|
|
|
|
SELECT SUBTIME(util.VN_NOW(), reserveTime) INTO vDatedReserve
|
|
|
|
FROM hedera.orderConfig;
|
|
|
|
|
|
|
|
-- Calcula el ultimo dia de vida para cada producto
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tItemRange
|
|
|
|
(PRIMARY KEY (itemFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT c.itemFk, MAX(t.landed) dated
|
|
|
|
FROM buy c
|
|
|
|
JOIN entry e ON c.entryFk = e.id
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
JOIN warehouse w ON w.id = t.warehouseInFk
|
|
|
|
WHERE t.landed BETWEEN vDatedInventory AND vDatedFrom
|
|
|
|
AND t.warehouseInFk = vWarehouseLanding
|
|
|
|
AND NOT e.isExcludedFromAvailable
|
|
|
|
AND NOT e.isRaid
|
|
|
|
GROUP BY c.itemFk;
|
|
|
|
|
|
|
|
-- Tabla con el ultimo dia de last_buy para cada producto que hace un replace de la anterior
|
|
|
|
CALL buyUltimate(vWarehouseShipment, util.VN_CURDATE());
|
|
|
|
|
|
|
|
INSERT INTO tItemRange
|
|
|
|
SELECT t.itemFk, tr.landed
|
|
|
|
FROM tmp.buyUltimate t
|
|
|
|
JOIN buy b ON b.id = t.buyFk
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel tr ON tr.id = e.travelFk
|
|
|
|
LEFT JOIN tItemRange i ON t.itemFk = i.itemFk
|
|
|
|
WHERE t.warehouseFk = vWarehouseShipment
|
|
|
|
AND NOT e.isRaid
|
|
|
|
ON DUPLICATE KEY UPDATE tItemRange.dated = GREATEST(tItemRange.dated, tr.landed);
|
|
|
|
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tItemRangeLive
|
|
|
|
(PRIMARY KEY (itemFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT ir.itemFk, TIMESTAMP(TIMESTAMPADD(DAY, it.life, ir.dated), '23:59:59') dated
|
|
|
|
FROM tItemRange ir
|
|
|
|
JOIN item i ON i.id = ir.itemFk
|
|
|
|
JOIN itemType it ON it.id = i.typeFk
|
|
|
|
HAVING dated >= vDatedFrom OR dated IS NULL;
|
|
|
|
|
|
|
|
-- Calcula el ATP
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.itemCalc
|
|
|
|
(INDEX (itemFk,warehouseFk))
|
|
|
|
ENGINE = MEMORY
|
2024-04-02 07:56:46 +00:00
|
|
|
SELECT i.itemFk, vWarehouseLanding warehouseFk, i.shipped dated, i.quantity
|
|
|
|
FROM vn.itemTicketOut i
|
2024-01-31 08:54:47 +00:00
|
|
|
JOIN tItemRangeLive ir ON ir.itemFK = i.item_id
|
2024-04-02 07:56:46 +00:00
|
|
|
WHERE i.shipped >= vDatedFrom
|
|
|
|
AND (ir.dated IS NULL OR i.shipped <= ir.dated)
|
|
|
|
AND i.warehouseFk = vWarehouseLanding
|
2024-01-31 08:54:47 +00:00
|
|
|
UNION ALL
|
|
|
|
SELECT b.itemFk, vWarehouseLanding, t.landed, b.quantity
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON b.entryFk = e.id
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
JOIN tItemRangeLive ir ON ir.itemFk = b.itemFk
|
|
|
|
WHERE NOT e.isExcludedFromAvailable
|
|
|
|
AND b.quantity <> 0
|
|
|
|
AND NOT e.isRaid
|
|
|
|
AND t.warehouseInFk = vWarehouseLanding
|
|
|
|
AND t.landed >= vDatedFrom
|
|
|
|
AND (ir.dated IS NULL OR t.landed <= ir.dated)
|
|
|
|
UNION ALL
|
2024-04-02 07:56:46 +00:00
|
|
|
SELECT i.itemFk, vWarehouseLanding, i.shipped, i.quantity
|
|
|
|
FROM vn.itemEntryOut i
|
|
|
|
JOIN tItemRangeLive ir ON ir.itemFk = i.itemFk
|
|
|
|
WHERE i.shipped >= vDatedFrom
|
|
|
|
AND (ir.dated IS NULL OR i.shipped <= ir.dated)
|
|
|
|
AND i.warehouseOutFk = vWarehouseLanding
|
2024-01-31 08:54:47 +00:00
|
|
|
UNION ALL
|
|
|
|
SELECT r.item_id, vWarehouseLanding, r.shipment, -r.amount
|
|
|
|
FROM hedera.order_row r
|
|
|
|
JOIN hedera.`order` o ON o.id = r.order_id
|
|
|
|
JOIN tItemRangeLive ir ON ir.itemFk = r.item_id
|
|
|
|
WHERE r.shipment >= vDatedFrom
|
|
|
|
AND (ir.dated IS NULL OR r.shipment <= ir.dated)
|
|
|
|
AND r.warehouse_id = vWarehouseLanding
|
|
|
|
AND r.created >= vDatedReserve
|
|
|
|
AND NOT o.confirmed;
|
|
|
|
|
|
|
|
CALL item_getAtp(vDated);
|
|
|
|
|
2024-03-22 07:32:38 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.availableTraslate
|
2024-01-31 08:54:47 +00:00
|
|
|
(PRIMARY KEY (item_id))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT t.item_id, SUM(stock) available
|
|
|
|
FROM (
|
|
|
|
SELECT ti.itemFk item_id, stock
|
|
|
|
FROM tmp.itemList ti
|
|
|
|
JOIN tItemRange ir ON ir.itemFk = ti.itemFk
|
|
|
|
UNION ALL
|
|
|
|
SELECT itemFk, quantity
|
|
|
|
FROM tmp.itemAtp
|
|
|
|
) t
|
|
|
|
GROUP BY t.item_id
|
|
|
|
HAVING available <> 0;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tmp.itemList, tItemRange, tItemRangeLive;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|