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

45 lines
1018 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`ticket_get`(vParamFk INT)
RETURNS INT(11)
NOT DETERMINISTIC
READS SQL DATA
BEGIN
/**
* Devuelve el número de ticket / collection / sectorCollection consultando
* en que tabla se encuantra en la última semana
*
* @param vParamFk Número a validar
* @return vReturn Identificador validado
*/
DECLARE vReturn INT DEFAULT NULL;
DECLARE vDated DATE;
SET vDated = util.VN_CURDATE() - INTERVAL 1 WEEK;
SELECT COALESCE(
(SELECT s.ticketFk
FROM sale s
JOIN saleGroupDetail sgd ON sgd.saleFk = s.id
JOIN saleGroup sg ON sg.id = sgd.saleGroupFk
WHERE sg.id = vParamFk
AND sg.created > vDated
LIMIT 1),
(SELECT c.id
FROM collection c
WHERE c.id = vParamFk
AND c.created > vDated),
(SELECT id
FROM ticket
WHERE id = vParamFk
AND shipped > vDated),
(SELECT id
FROM sectorCollection
WHERE id = vParamFk
AND created > vDated)
) INTO vReturn;
RETURN vReturn;
END$$
DELIMITER ;