40 lines
1.0 KiB
SQL
40 lines
1.0 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`item_calculateStock`(
|
|
vDated DATE
|
|
)
|
|
BEGIN
|
|
/**
|
|
* Calculate the stock of the auction warehouse from the inventory date to vDated
|
|
*
|
|
* @param vDated Date to calculate the stock.
|
|
* @return tmp.item, tmp.buyUltimate
|
|
*/
|
|
DECLARE vAuctionWarehouseFk INT;
|
|
|
|
SELECT warehouseFk INTO vAuctionWarehouseFk
|
|
FROM auctionConfig;
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.item
|
|
(UNIQUE INDEX i USING HASH (itemFk))
|
|
ENGINE = MEMORY
|
|
SELECT itemFk, SUM(quantity) quantity
|
|
FROM itemEntryIn
|
|
WHERE landed = vDated
|
|
AND vDated >= util.VN_CURDATE()
|
|
AND warehouseInFk = vAuctionWarehouseFk
|
|
AND NOT isVirtualStock
|
|
GROUP BY itemFk
|
|
HAVING quantity;
|
|
|
|
CALL `cache`.stock_refresh(FALSE);
|
|
|
|
INSERT INTO tmp.item (itemFk, quantity)
|
|
SELECT item_id, amount
|
|
FROM `cache`.stock
|
|
WHERE warehouse_id = vAuctionWarehouseFk
|
|
ON DUPLICATE KEY UPDATE quantity = tmp.item.quantity + VALUES(quantity);
|
|
|
|
CALL buy_getUltimate(NULL, vAuctionWarehouseFk, vDated);
|
|
END$$
|
|
DELIMITER ;
|