salix/db/routines/vn/procedures/item_calcVisible.sql

57 lines
1.7 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-06-11 12:00:59 +00:00
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_calcVisible`(
2024-07-09 07:33:31 +00:00
vSelf INT,
vWarehouseFk INT
)
BEGIN
2024-06-11 11:53:41 +00:00
/**
* Calcula el visible de un artículo o de todos.
*
* @param vWarehouse Warehouse id
2024-07-09 07:33:31 +00:00
* @param vSelf Item id
2024-06-11 11:53:41 +00:00
* @param tmp.itemVisible(item_id, stock, visible)
*/
2024-07-09 12:22:32 +00:00
DECLARE vDated DATE DEFAULT util.VN_CURDATE();
DECLARE vTomorrow DATETIME DEFAULT util.tomorrow();
2024-07-09 07:33:31 +00:00
INSERT INTO tmp.itemVisible(item_id, visible)
SELECT itemFk, SUM(quantity)
FROM (
SELECT i.itemFk, i.quantity
FROM itemTicketOut i
LEFT JOIN ticketState ts ON ts.ticketFk = i.ticketFk
JOIN `state` s ON s.id = ts.stateFk
LEFT JOIN (
SELECT DISTINCT st.saleFk
FROM saleTracking st
JOIN state s ON s.id = st.stateFk
WHERE st.created > vDated
AND (s.isPicked OR st.isChecked)
) stPrevious ON `stPrevious`.`saleFk` = i.saleFk
2024-07-09 07:33:31 +00:00
WHERE IFNULL(vWarehouseFk, i.warehouseFk) = i.warehouseFk
AND (vSelf IS NULL OR i.itemFk = vSelf)
AND (s.isPicked OR i.reserved OR stPrevious.saleFk)
AND i.shipped >= vDated AND i.shipped < vTomorrow
UNION ALL
SELECT itemFk, quantity
FROM itemEntryIn
WHERE isReceived
AND landed >= vDated AND landed < vTomorrow
2024-07-09 07:33:31 +00:00
AND IFNULL(vWarehouseFk, warehouseInFk) = warehouseInFk
AND (vSelf IS NULL OR itemFk = vSelf)
AND NOT isVirtualStock
UNION ALL
SELECT itemFk, quantity
FROM itemEntryOut
WHERE isDelivered
AND shipped >= vDated
AND shipped < vTomorrow
2024-07-09 07:33:31 +00:00
AND IFNULL(vWarehouseFk, warehouseOutFk) = warehouseOutFk
AND (vSelf IS NULL OR itemFk = vSelf)
) t
GROUP BY itemFk
ON DUPLICATE KEY UPDATE
visible = IFNULL(stock, 0) + VALUES(visible);
END$$
DELIMITER ;