2024-01-31 08:54:47 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`available_traslate`(
|
2024-01-31 08:54:47 +00:00
|
|
|
vWarehouseLanding INT,
|
|
|
|
vDated DATE,
|
|
|
|
vWarehouseShipment INT)
|
|
|
|
proc: BEGIN
|
2024-03-28 13:27:29 +00:00
|
|
|
/**
|
2024-04-08 07:56:36 +00:00
|
|
|
* Calcular la disponibilidad dependiendo del almacen
|
|
|
|
* de origen y destino según la fecha.
|
2024-03-28 13:27:29 +00:00
|
|
|
*
|
2024-04-08 10:57:01 +00:00
|
|
|
* @param vWarehouseLanding Almacén de llegada
|
|
|
|
* @param vDated Fecha del calculo para la disponibilidad de articulos
|
|
|
|
* @param vWarehouseShipment Almacén de destino
|
2024-03-28 13:27:29 +00:00
|
|
|
*/
|
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);
|
|
|
|
|
2024-04-08 10:57:01 +00:00
|
|
|
-- Calcula algunos parámetros necesarios.
|
2024-04-23 12:55:05 +00:00
|
|
|
SET vDatedFrom = vDated;
|
|
|
|
SET vDatedTo = util.dayEnd (vDated + INTERVAL 4 DAY);
|
2024-04-10 07:00:41 +00:00
|
|
|
SELECT inventoried INTO vDatedInventory FROM config;
|
2024-01-31 08:54:47 +00:00
|
|
|
SELECT SUBTIME(util.VN_NOW(), reserveTime) INTO vDatedReserve
|
|
|
|
FROM hedera.orderConfig;
|
|
|
|
|
2024-04-08 10:57:01 +00:00
|
|
|
-- Calcula el ultimo dia de vida para cada producto.
|
2024-01-31 08:54:47 +00:00
|
|
|
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;
|
|
|
|
|
2024-04-08 07:56:36 +00:00
|
|
|
-- Tabla con el ultimo dia de last_buy para cada producto
|
|
|
|
-- que hace un replace de la anterior.
|
2024-01-31 08:54:47 +00:00
|
|
|
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
|
2024-04-08 07:56:36 +00:00
|
|
|
ON DUPLICATE KEY UPDATE tItemRange.dated = GREATEST(tItemRange.dated,
|
|
|
|
tr.landed);
|
2024-01-31 08:54:47 +00:00
|
|
|
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tItemRangeLive
|
|
|
|
(PRIMARY KEY (itemFk))
|
|
|
|
ENGINE = MEMORY
|
2024-04-08 05:44:45 +00:00
|
|
|
SELECT ir.itemFk, util.dayEnd(ir.dated + INTERVAL it.life DAY) dated
|
2024-01-31 08:54:47 +00:00
|
|
|
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;
|
|
|
|
|
2024-04-08 07:56:36 +00:00
|
|
|
-- Calcula el ATP.
|
2024-01-31 08:54:47 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.itemCalc
|
|
|
|
(INDEX (itemFk,warehouseFk))
|
|
|
|
ENGINE = MEMORY
|
2024-04-08 07:56:36 +00:00
|
|
|
SELECT i.itemFk,
|
2024-04-08 11:28:56 +00:00
|
|
|
vWarehouseLanding warehouseFk,
|
|
|
|
i.shipped dated,
|
|
|
|
i.quantity
|
2024-04-05 10:58:31 +00:00
|
|
|
FROM itemTicketOut i
|
|
|
|
JOIN tItemRangeLive ir ON ir.itemFK = i.itemFk
|
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
|
2024-04-08 07:56:36 +00:00
|
|
|
SELECT b.itemFk,
|
2024-04-08 11:28:56 +00:00
|
|
|
vWarehouseLanding,
|
|
|
|
t.landed,
|
|
|
|
b.quantity
|
2024-01-31 08:54:47 +00:00
|
|
|
FROM buy b
|
2024-04-08 10:57:01 +00:00
|
|
|
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)
|
2024-01-31 08:54:47 +00:00
|
|
|
UNION ALL
|
2024-04-02 07:56:46 +00:00
|
|
|
SELECT i.itemFk, vWarehouseLanding, i.shipped, i.quantity
|
2024-04-05 10:58:31 +00:00
|
|
|
FROM itemEntryOut i
|
2024-04-02 07:56:46 +00:00
|
|
|
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 ;
|