salix/db/routines/vn/functions/getAlert3StateTest.sql

41 lines
1.0 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`getAlert3StateTest`(vTicket INT)
RETURNS varchar(45) CHARSET latin1 COLLATE latin1_swedish_ci
NOT DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE vDeliveryType INTEGER DEFAULT 0;
DECLARE isWaitingForPickUp BOOLEAN DEFAULT FALSE;
DECLARE vCode VARCHAR(45);
SELECT
a.Vista
INTO vDeliveryType
FROM ticket t
JOIN vn2008.Agencias a ON a.Id_Agencia = t.agencyModeFk
WHERE t.id = vTicket;
CASE vDeliveryType
WHEN 1 THEN -- AGENCIAS
SET vCode = 'DELIVERED';
WHEN 2 THEN -- REPARTO
SET vCode = 'ON_DELIVERY';
ELSE -- MERCADO, OTROS
SELECT t.warehouseFk <> w.warehouse_id INTO isWaitingForPickUp
FROM ticket t
LEFT JOIN vn2008.warehouse_pickup w
ON w.agency_id = t.agencyModeFk AND w.warehouse_id = t.warehouseFk
WHERE t.id = vTicket;
IF isWaitingForPickUp THEN
SET vCode = 'WAITING_FOR_PICKUP';
ELSE
SET vCode = 'DELIVERED';
END IF;
END CASE;
RETURN vCode;
END$$
DELIMITER ;