2024-06-11 10:40:07 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`item_calcVisible`(
|
2024-07-09 07:33:31 +00:00
|
|
|
vSelf INT,
|
|
|
|
vWarehouseFk INT
|
2024-06-11 10:40:07 +00:00
|
|
|
)
|
|
|
|
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-06-11 10:40:07 +00:00
|
|
|
|
2024-07-09 07:33:31 +00:00
|
|
|
INSERT INTO tmp.itemVisible(item_id, visible)
|
2024-06-11 10:40:07 +00:00
|
|
|
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
|
2024-07-23 11:59:36 +00:00
|
|
|
JOIN warehouse w ON w.id = i.warehouseFk
|
2024-06-11 10:40:07 +00:00
|
|
|
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-23 11:59:36 +00:00
|
|
|
WHERE (vWarehouseFk IS NULL OR i.warehouseFk = vWarehouseFk)
|
2024-07-09 07:33:31 +00:00
|
|
|
AND (vSelf IS NULL OR i.itemFk = vSelf)
|
2024-06-11 10:40:07 +00:00
|
|
|
AND (s.isPicked OR i.reserved OR stPrevious.saleFk)
|
|
|
|
AND i.shipped >= vDated AND i.shipped < vTomorrow
|
2024-07-23 12:17:01 +00:00
|
|
|
AND w.isComparative
|
2024-06-11 10:40:07 +00:00
|
|
|
UNION ALL
|
2024-07-23 11:59:36 +00:00
|
|
|
SELECT iei.itemFk, iei.quantity
|
|
|
|
FROM itemEntryIn iei
|
|
|
|
JOIN warehouse w ON w.id = iei.warehouseInFk
|
|
|
|
WHERE iei.isReceived
|
|
|
|
AND iei.landed >= vDated AND iei.landed < vTomorrow
|
|
|
|
AND (vWarehouseFk IS NULL OR iei.warehouseInFk = vWarehouseFk)
|
|
|
|
AND (vSelf IS NULL OR iei.itemFk = vSelf)
|
|
|
|
AND NOT iei.isVirtualStock
|
2024-07-23 12:17:01 +00:00
|
|
|
AND w.isComparative
|
2024-06-11 10:40:07 +00:00
|
|
|
UNION ALL
|
2024-07-23 11:59:36 +00:00
|
|
|
SELECT ieo.itemFk, ieo.quantity
|
|
|
|
FROM itemEntryOut ieo
|
|
|
|
JOIN warehouse w ON w.id = ieo.warehouseOutFk
|
|
|
|
WHERE ieo.isDelivered
|
|
|
|
AND ieo.shipped >= vDated
|
|
|
|
AND ieo.shipped < vTomorrow
|
|
|
|
AND (vWarehouseFk IS NULL OR ieo.warehouseOutFk = vWarehouseFk)
|
|
|
|
AND (vSelf IS NULL OR ieo.itemFk = vSelf)
|
2024-07-23 12:17:01 +00:00
|
|
|
AND w.isComparative
|
2024-06-11 10:40:07 +00:00
|
|
|
) t
|
|
|
|
GROUP BY itemFk
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
visible = IFNULL(stock, 0) + VALUES(visible);
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|