2024-07-12 08:27:24 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`itemShelvingSale_addBySale`(
|
2024-07-17 14:09:09 +00:00
|
|
|
vSaleFk INT,
|
|
|
|
vSectorFk INT
|
2024-07-12 08:27:24 +00:00
|
|
|
)
|
2024-07-29 05:20:05 +00:00
|
|
|
proc: BEGIN
|
2024-07-12 08:27:24 +00:00
|
|
|
/**
|
|
|
|
* Reserva una línea de venta en la ubicación más óptima
|
|
|
|
*
|
|
|
|
* @param vSaleFk Id de sale
|
|
|
|
* @param vItemShelvingSaleFk Id de reserva
|
2024-07-17 14:09:09 +00:00
|
|
|
* @param vSectorFk Id del sector del operator
|
2024-07-12 08:27:24 +00:00
|
|
|
*/
|
|
|
|
DECLARE vLastPickingOrder INT;
|
|
|
|
DECLARE vDone INT DEFAULT FALSE;
|
|
|
|
DECLARE vItemShelvingFk INT;
|
|
|
|
DECLARE vAvailable INT;
|
|
|
|
DECLARE vReservedQuantity INT;
|
|
|
|
DECLARE vOutStanding INT;
|
|
|
|
DECLARE vUserFk INT;
|
|
|
|
DECLARE vTotalReservedQuantity INT;
|
|
|
|
DECLARE vSaleQuantity INT;
|
|
|
|
|
|
|
|
DECLARE vItemShelvingAvailable CURSOR FOR
|
|
|
|
SELECT ish.id itemShelvingFk,
|
|
|
|
ish.available
|
|
|
|
FROM sale s
|
|
|
|
JOIN itemShelving ish ON ish.itemFk = s.itemFk
|
|
|
|
JOIN shelving sh ON sh.code = ish.shelvingFk
|
|
|
|
JOIN parking p ON p.id = sh.parkingFk
|
|
|
|
JOIN sector sc ON sc.id = p.sectorFk
|
|
|
|
JOIN productionConfig pc
|
|
|
|
WHERE s.id = vSaleFk
|
|
|
|
AND NOT sc.isHideForPickers
|
2024-07-17 14:09:09 +00:00
|
|
|
AND (sc.id = vSectorFk OR vSectorFk IS NULL)
|
2024-07-12 08:27:24 +00:00
|
|
|
ORDER BY s.id,
|
|
|
|
p.pickingOrder >= vLastPickingOrder,
|
|
|
|
sh.priority DESC,
|
|
|
|
ish.available >= s.quantity DESC,
|
|
|
|
s.quantity MOD ish.grouping = 0 DESC,
|
|
|
|
ish.grouping DESC,
|
|
|
|
IF(pc.orderMode = 'Location', p.pickingOrder, ish.created);
|
|
|
|
|
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
|
|
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
|
|
BEGIN
|
|
|
|
ROLLBACK;
|
|
|
|
RESIGNAL;
|
|
|
|
END;
|
|
|
|
|
|
|
|
SELECT MAX(p.pickingOrder), s.quantity - SUM(IFNULL(iss.quantity, 0)), s.quantity
|
|
|
|
INTO vLastPickingOrder, vOutStanding, vSaleQuantity
|
|
|
|
FROM sale s
|
|
|
|
LEFT JOIN itemShelvingSale iss ON iss.saleFk = s.id
|
|
|
|
LEFT JOIN itemShelving ish ON ish.id = iss.itemShelvingFk
|
|
|
|
LEFT JOIN shelving sh ON sh.code = ish.shelvingFk
|
|
|
|
LEFT JOIN parking p ON p.id = sh.parkingFk
|
|
|
|
WHERE s.id = vSaleFk;
|
|
|
|
|
|
|
|
IF vOutStanding <= 0 THEN
|
|
|
|
LEAVE proc;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT getUser() INTO vUserFk;
|
|
|
|
|
|
|
|
OPEN vItemShelvingAvailable;
|
|
|
|
l: LOOP
|
|
|
|
SET vDone = FALSE;
|
|
|
|
FETCH vItemShelvingAvailable INTO vItemShelvingFk, vAvailable;
|
|
|
|
|
|
|
|
IF vOutStanding <= 0 OR vDone THEN
|
|
|
|
SELECT SUM(IFNULL(quantity, 0))
|
|
|
|
INTO vTotalReservedQuantity
|
|
|
|
FROM itemShelvingSale
|
|
|
|
WHERE saleFk = vSaleFk;
|
|
|
|
|
|
|
|
IF vTotalReservedQuantity <> vSaleQuantity THEN
|
|
|
|
UPDATE sale
|
|
|
|
SET quantity = vTotalReservedQuantity
|
|
|
|
WHERE id = vSaleFk;
|
2024-07-12 08:37:39 +00:00
|
|
|
END IF;
|
2024-07-12 08:27:24 +00:00
|
|
|
LEAVE l;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
START TRANSACTION;
|
|
|
|
|
|
|
|
SELECT id INTO vItemShelvingFk
|
|
|
|
FROM itemShelving
|
|
|
|
WHERE id = vItemShelvingFk
|
|
|
|
FOR UPDATE;
|
|
|
|
|
|
|
|
SELECT LEAST(vOutStanding, vAvailable) INTO vReservedQuantity;
|
|
|
|
SET vOutStanding = vOutStanding - vReservedQuantity;
|
|
|
|
|
|
|
|
IF vReservedQuantity > 0 THEN
|
|
|
|
|
|
|
|
INSERT INTO itemShelvingSale(
|
|
|
|
itemShelvingFk,
|
|
|
|
saleFk,
|
|
|
|
quantity,
|
2024-07-25 06:36:14 +00:00
|
|
|
userFk,
|
2024-07-22 11:42:42 +00:00
|
|
|
isPicked)
|
2024-07-12 08:27:24 +00:00
|
|
|
SELECT vItemShelvingFk,
|
|
|
|
vSaleFk,
|
|
|
|
vReservedQuantity,
|
2024-07-22 11:42:42 +00:00
|
|
|
vUserFk,
|
|
|
|
FALSE;
|
2024-07-12 08:27:24 +00:00
|
|
|
|
|
|
|
UPDATE itemShelving
|
|
|
|
SET available = available - vReservedQuantity
|
|
|
|
WHERE id = vItemShelvingFk;
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
END LOOP;
|
|
|
|
CLOSE vItemShelvingAvailable;
|
|
|
|
END$$
|
2024-01-31 06:11:03 +00:00
|
|
|
DELIMITER ;
|