2024-08-30 11:08:15 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sale_setProblemRoundingByBuy`(
|
|
|
|
vBuyFk INT
|
|
|
|
)
|
2024-08-30 11:14:45 +00:00
|
|
|
BEGIN
|
2024-08-30 11:08:15 +00:00
|
|
|
/**
|
2024-08-30 12:14:06 +00:00
|
|
|
* Update rounding problem for all sales related to a buy.
|
|
|
|
*
|
|
|
|
* @param vBuyFk Buy id
|
2024-08-30 11:08:15 +00:00
|
|
|
*/
|
2024-08-30 11:14:45 +00:00
|
|
|
DECLARE vItemFk INT;
|
|
|
|
DECLARE vWareHouseFk INT;
|
2024-08-30 11:08:15 +00:00
|
|
|
DECLARE vMaxDated DATE;
|
|
|
|
DECLARE vMinDated DATE;
|
|
|
|
DECLARE vLanding DATE;
|
|
|
|
DECLARE vLastBuy INT;
|
|
|
|
DECLARE vCurrentBuy INT;
|
|
|
|
DECLARE vGrouping INT;
|
|
|
|
|
|
|
|
SELECT b.itemFk, t.warehouseInFk
|
|
|
|
INTO vItemFk, vWareHouseFk
|
2024-08-30 12:14:06 +00:00
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
2024-08-30 11:08:15 +00:00
|
|
|
WHERE b.id = vBuyFk;
|
|
|
|
|
|
|
|
SELECT DATE(MAX(t.shipped)) + INTERVAL 1 DAY, DATE(MIN(t.shipped))
|
|
|
|
INTO vMaxDated, vMinDated
|
|
|
|
FROM sale s
|
|
|
|
JOIN ticket t ON t.id = s.ticketFk
|
2024-08-30 12:14:06 +00:00
|
|
|
WHERE t.shipped >= util.VN_CURDATE()
|
2024-08-30 11:08:15 +00:00
|
|
|
AND s.itemFk = vItemFk
|
|
|
|
AND s.quantity > 0;
|
|
|
|
|
|
|
|
CALL buy_getUltimate(vItemFk, vWareHouseFk, vMinDated);
|
|
|
|
|
|
|
|
SELECT bu.buyFk, b.grouping INTO vLastBuy, vGrouping
|
|
|
|
FROM tmp.buyUltimate bu
|
|
|
|
JOIN buy b ON b.id = bu.buyFk;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tmp.buyUltimate;
|
|
|
|
|
|
|
|
SET vLanding = vMaxDated;
|
|
|
|
|
|
|
|
WHILE vCurrentBuy <> vLastBuy OR vLanding > vMinDated DO
|
|
|
|
SET vMaxDated = vLanding - INTERVAL 1 DAY;
|
|
|
|
|
|
|
|
CALL buy_getUltimate(vItemFk, vWareHouseFk, vMaxDated);
|
|
|
|
|
|
|
|
SELECT buyFk, landing
|
|
|
|
INTO vCurrentBuy, vLanding
|
|
|
|
FROM tmp.buyUltimate;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tmp.buyUltimate;
|
|
|
|
END WHILE;
|
|
|
|
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.sale
|
2024-08-30 12:14:06 +00:00
|
|
|
(INDEX(saleFk, isProblemCalcNeeded))
|
2024-08-30 12:16:03 +00:00
|
|
|
ENGINE = MEMORY
|
2024-08-30 12:14:06 +00:00
|
|
|
SELECT s.id saleFk,
|
2024-08-30 11:08:15 +00:00
|
|
|
MOD(s.quantity, vGrouping) hasProblem,
|
|
|
|
ticket_isProblemCalcNeeded(t.id) isProblemCalcNeeded
|
|
|
|
FROM sale s
|
|
|
|
JOIN ticket t ON t.id = s.ticketFk
|
|
|
|
WHERE s.itemFk = vItemFk
|
|
|
|
AND s.quantity > 0
|
2024-08-30 12:14:06 +00:00
|
|
|
AND t.shipped BETWEEN vMinDated AND util.dayEnd(vMaxDated);
|
2024-08-30 11:08:15 +00:00
|
|
|
|
|
|
|
CALL sale_setProblem('hasRounding');
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tmp.sale;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|