salix/db/routines/vn/procedures/itemShelvingTransfer.sql

47 lines
1.2 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelvingTransfer`(vItemShelvingFk INT, vShelvingFk VARCHAR(3))
BEGIN
/**
* Transfiere producto de una ubicación a otra, fusionando si coincide el
* packing y la fecha.
*
* @param vItemShelvingFk Identificador de itemShelving
* @param vShelvingFk Identificador de shelving
*/
DECLARE vNewItemShelvingFk INT DEFAULT 0;
SELECT MAX(ish.id)
INTO vNewItemShelvingFk
FROM itemShelving ish
JOIN (
SELECT
itemFk,
packing,
created
FROM itemShelving
WHERE id = vItemShelvingFk
) ish2
ON ish2.itemFk = ish.itemFk
AND ish2.packing = ish.packing
AND date(ish2.created) = date(ish.created)
2024-02-06 12:31:58 +00:00
AND ish2.buyFk = ish.buyFk
WHERE ish.shelvingFk = vShelvingFk COLLATE utf8_unicode_ci;
IF vNewItemShelvingFk THEN
UPDATE itemShelving ish
JOIN itemShelving ish2 ON ish2.id = vItemShelvingFk
SET ish.visible = ish.visible + ish2.visible
WHERE ish.id = vNewItemShelvingFk;
DELETE FROM itemShelving
WHERE id = vItemShelvingFk;
ELSE
UPDATE itemShelving
SET shelvingFk = vShelvingFk
WHERE id = vItemShelvingFk;
END IF;
SELECT true;
END$$
DELIMITER ;