salix/db/routines/vn/procedures/itemShelvingSale_setQuantit...

121 lines
3.5 KiB
MySQL
Raw Normal View History

2024-01-31 06:11:03 +00:00
DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`itemShelvingSale_setQuantity`(
2024-01-31 06:11:03 +00:00
vItemShelvingSaleFk INT(10),
vQuantity DECIMAL(10,0),
2024-07-17 14:09:09 +00:00
vIsItemShelvingSaleEmpty BOOLEAN,
2024-10-17 10:44:05 +00:00
vSectorFk INT,
vShelvingFk VARCHAR(10)
2024-01-31 06:11:03 +00:00
)
BEGIN
/**
* Gestiona la reserva de un itemShelvingFk, actualizando isPicked y quantity
* en itemShelvingSale y sale.isPicked en caso necesario.
* Si la reserva de la ubicación es fallida, se regulariza la situación
*
* @param vItemShelvingSaleFk Id itemShelvingSaleFK
* @param vQuantity Cantidad real que se ha cogido de la ubicación
* @param vIsItemShelvingSaleEmpty determina si la ubicación itemShelvingSale se ha
* quedado vacio tras el movimiento
2024-07-17 14:09:09 +00:00
* @param vSectorFk Id del sector
2024-01-31 06:11:03 +00:00
*/
DECLARE vSaleFk INT;
DECLARE vItemShelvingFk INT;
DECLARE vReservedQuantity INT;
DECLARE vRemainingQuantity INT;
DECLARE vItemFk INT;
DECLARE vTotalQuantity INT;
2024-07-11 16:09:00 +00:00
DECLARE vStateCode VARCHAR(45);
2024-10-17 10:44:05 +00:00
DECLARE vCurrentShelvingFk VARCHAR(10);
2024-01-31 06:11:03 +00:00
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
2024-07-18 10:42:00 +00:00
IF vQuantity > vReservedQuantity
OR (vQuantity < vReservedQuantity AND NOT vIsItemShelvingSaleEmpty)
OR (vQuantity = vReservedQuantity AND vIsItemShelvingSaleEmpty) THEN
CALL util.throw('The quantity cannot be different from the reserved');
END IF;
2024-01-31 06:11:03 +00:00
IF (SELECT isPicked FROM itemShelvingSale WHERE id = vItemShelvingSaleFk) THEN
CALL util.throw('Reservation completed');
2024-01-31 06:11:03 +00:00
END IF;
2024-07-12 07:06:05 +00:00
SELECT s.itemFk,
iss.saleFk,
iss.itemShelvingFk,
SUM(IFNULL(iss.quantity,0)),
2024-10-17 10:44:05 +00:00
IF(sgd.id, 'PREVIOUS_PREPARATION', 'PREPARED'),
sh.code
2024-07-12 07:06:05 +00:00
INTO vItemFk,
vSaleFk,
vItemShelvingFk,
vReservedQuantity,
2024-10-17 10:44:05 +00:00
vStateCode,
vCurrentShelvingFk
2024-01-31 06:11:03 +00:00
FROM itemShelvingSale iss
JOIN sale s ON s.id = iss.saleFk
2024-10-17 10:44:05 +00:00
JOIN itemShelving ish ON ish.id = iss.itemShelvingFk
LEFT JOIN saleGroupDetail sgd ON sgd.saleFk = iss.saleFk
LEFT JOIN shelving sh ON sh.id = ish.shelvingFk
2024-01-31 06:11:03 +00:00
WHERE iss.id = vItemShelvingSaleFk
AND NOT iss.isPicked;
2024-10-17 10:44:05 +00:00
IF NOT (vCurrentShelvingFk <=> vShelvingFk) THEN
CALL util.throw('The shelving can not be different from the shelving reserved');
END IF;
2024-01-31 06:11:03 +00:00
IF vQuantity > vReservedQuantity
2024-07-18 10:42:00 +00:00
OR (vQuantity < vReservedQuantity AND NOT vIsItemShelvingSaleEmpty)
OR (vQuantity = vReservedQuantity AND vIsItemShelvingSaleEmpty) THEN
CALL util.throw('The quantity cannot be different from the reserved');
2024-01-31 06:11:03 +00:00
END IF;
START TRANSACTION;
UPDATE itemShelvingSale
SET isPicked = TRUE,
quantity = vQuantity
WHERE id = vItemShelvingSaleFk;
SELECT id INTO vItemShelvingFk
FROM itemShelving
WHERE id = vItemShelvingFk
2024-01-31 06:11:03 +00:00
FOR UPDATE;
UPDATE itemShelving
SET visible = GREATEST(0, visible - vQuantity)
2024-01-31 06:11:03 +00:00
WHERE id = vItemShelvingFk;
2024-07-17 14:09:09 +00:00
SELECT SUM(IF(isPicked OR id = vItemShelvingSaleFk, 0, quantity)), SUM(quantity)
2024-01-31 06:11:03 +00:00
INTO vRemainingQuantity, vTotalQuantity
FROM itemShelvingSale
WHERE saleFk = vSaleFk;
2024-06-13 12:21:59 +00:00
IF vRemainingQuantity = 0 AND NOT vIsItemShelvingSaleEmpty THEN
2024-01-31 06:11:03 +00:00
CALL saleTracking_new(
vSaleFk,
TRUE,
vTotalQuantity,
`account`.`myUser_getId`(),
NULL,
2024-07-11 16:09:00 +00:00
vStateCode,
2024-01-31 06:11:03 +00:00
TRUE);
UPDATE sale s
SET s.quantity = vTotalQuantity,
isPicked = TRUE
WHERE s.id = vSaleFk;
END IF;
COMMIT;
IF vIsItemShelvingSaleEmpty AND vQuantity <> vReservedQuantity THEN
2024-07-30 08:24:50 +00:00
INSERT INTO itemShelvingSaleReserve (saleFk, sectorFk)
2024-07-18 10:42:00 +00:00
SELECT vSaleFk, vSectorFk;
2024-07-17 14:09:09 +00:00
CALL itemShelvingSale_reallocate(vItemShelvingFk, vItemFk, vSectorFk);
2024-01-31 06:11:03 +00:00
END IF;
END$$
DELIMITER ;