diff --git a/db/changes/232601/00-packingSiteAdvanced.sql b/db/changes/232601/00-packingSiteAdvanced.sql index 63c348fcc..0e33744cd 100644 --- a/db/changes/232601/00-packingSiteAdvanced.sql +++ b/db/changes/232601/00-packingSiteAdvanced.sql @@ -12,7 +12,7 @@ INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `pri VALUES ('PackingSiteAdvanced', '*', '*', 'ALLOW', 'ROLE', 'production'); -DROP PROCEDURE IF EXISTS `vn`.`packingSite_startCollection`; + DROP PROCEDURE IF EXISTS `vn`.`packingSite_startCollection`; DELIMITER $$ $$ @@ -22,40 +22,48 @@ proc: BEGIN * @param vSelf packingSite id * @param vTicketFk A ticket id from the collection to start */ - DECLARE vNewCollectionFk INT; - DECLARE vIsAdvanced BOOL; - DECLARE vOldCollectionFk INT; - DECLARE vIsPackingByOther BOOL; + DECLARE vExists BOOL; + DECLARE vIsAdvanced BOOL; + DECLARE vNewCollectionFk INT; + DECLARE vOldCollectionFk INT; + DECLARE vIsPackingByOther BOOL; - SELECT COUNT(*) > 0 - INTO vIsAdvanced - FROM packingSiteAdvanced - WHERE ticketFk = vTicketFk; + SELECT id, collectionFk + INTO vExists, vOldCollectionFk + FROM packingSite + WHERE id = vSelf; - IF vIsAdvanced THEN - LEAVE proc; - END IF; + IF NOT vExists THEN + CALL util.throw('packingSiteNotExists'); + END IF; - SELECT collectionFk INTO vNewCollectionFk - FROM ticketCollection WHERE ticketFk = vTicketFk; + SELECT COUNT(*) > 0 + INTO vIsAdvanced + FROM packingSiteAdvanced + WHERE ticketFk = vTicketFk; - SELECT collectionFk INTO vOldCollectionFk - FROM packingSite WHERE id = vSelf; + IF vIsAdvanced THEN + LEAVE proc; + END IF; - IF NOT (vOldCollectionFk <=> vNewCollectionFk) THEN - SELECT COUNT(*) > 0 - INTO vIsPackingByOther - FROM packingSite - WHERE id <> vSelf - AND collectionFk = vOldCollectionFk; + SELECT collectionFk INTO vNewCollectionFk + FROM ticketCollection WHERE ticketFk = vTicketFk; - IF NOT vIsPackingByOther AND NOT collection_isPacked(vOldCollectionFk) AND vSelf IS NOT NULL THEN - CALL util.throw('cannotChangeCollection'); - END IF; - END IF; + IF vOldCollectionFk IS NOT NULL + AND vOldCollectionFk <> vNewCollectionFk THEN + SELECT COUNT(*) > 0 + INTO vIsPackingByOther + FROM packingSite + WHERE id <> vSelf + AND collectionFk = vOldCollectionFk; - UPDATE packingSite SET collectionFk = vNewCollectionFk - WHERE id = vSelf; + IF NOT vIsPackingByOther AND NOT collection_isPacked(vOldCollectionFk) THEN + CALL util.throw('cannotChangeCollection'); + END IF; + END IF; + + UPDATE packingSite SET collectionFk = vNewCollectionFk + WHERE id = vSelf; END$$ DELIMITER ;