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

53 lines
1.4 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`setParking`(
vParam VARCHAR(8),
vParkingCode VARCHAR(8)
)
BEGIN
/**
* Aparca una colección, un ticket, un saleGroup o un shelving en un parking
*
* @param vParam id del ticket, colección, saleGroup o shelving
* @param vParkingCode código del parking
*/
DECLARE vParkingFk INT;
DECLARE vLastWeek DATE;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
SET vParkingCode = REPLACE(vParkingCode, ' ', '');
SELECT id INTO vParkingFk
FROM parking
WHERE code = vParkingCode COLLATE utf8_unicode_ci;
IF vParkingFk IS NULL THEN
CALL util.throw('parkingNotExist');
END IF;
START TRANSACTION;
IF vParam REGEXP '^[0-9]+$' THEN
SET vLastWeek = util.VN_CURDATE() - INTERVAL 1 WEEK;
-- Comprobamos si es una prep. previa, ticket o colección
IF (SELECT TRUE FROM saleGroup WHERE id = vParam) THEN
CALL saleGroup_setParking(vParam, vParkingFk);
ELSEIF (SELECT TRUE FROM ticket WHERE id = vParam AND shipped >= vLastWeek) THEN
CALL ticket_setParking(vParam, vParkingFk);
ELSEIF (SELECT TRUE FROM `collection` WHERE id = vParam AND created >= vLastWeek) THEN
CALL collection_setParking(vParam, vParkingFk);
END IF;
ELSE
-- Por descarte, se considera una matrícula
CALL shelving_setParking(vParam, vParkingFk);
END IF;
COMMIT;
END$$
DELIMITER ;