salix/db/routines/vn/procedures/itemPlacementSupplyGetOrder...

38 lines
1.2 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemPlacementSupplyGetOrder`(vSector INT )
BEGIN
DECLARE vId INT;
DECLARE vLastParkingFk INT;
DECLARE vNextParkingFk INT;
SELECT sh.parkingFk INTO vLastParkingFk
FROM vn.itemShelvingPlacementSupply isps
JOIN vn.itemShelving ish ON ish.id = isps.itemShelvingFk
JOIN vn.shelving sh ON sh.code = ish.shelvingFk
WHERE isps.userFk = getUser()
ORDER BY isps.created DESC
LIMIT 1;
SET vNextParkingFk = vn.itemShelvingPlacementSupply_ClosestGet(vLastParkingFk);
SELECT ipsl.id INTO vId
FROM vn.itemPlacementSupplyList ipsl
JOIN vn.itemShelvingPlacementSupplyStock ispss ON ispss.itemFk = ipsl.itemFk
WHERE ipsl.saldo > 0
AND (ipsl.repoUserFk is NULL OR ipsl.repoUserFk = getUser())
AND ipsl.sectorFk = vSector
ORDER BY ipsl.repoUserFk DESC, ipsl.priority DESC, (ispss.parkingFk = vNextParkingFk) DESC, ispss.parking DESC, ipsl.created
LIMIT 1;
UPDATE vn.itemPlacementSupply
SET repoUserFk = getUser()
WHERE id = vId;
SELECT * FROM vn.itemPlacementSupplyList
WHERE id = vId
AND sectorFk = vSector;
END$$
DELIMITER ;