salix/db/changes/10120-december/01-buyNotifyPassport.sql

37 lines
1004 B
MySQL
Raw Normal View History

2019-11-26 13:15:43 +00:00
DROP function IF EXISTS `vn`.`buy_hasNotifyPassport`;
DELIMITER $$
CREATE DEFINER=`root`@`%` FUNCTION `vn`.`buy_hasNotifyPassport`(vBuyFk INT, vItemFk INT) RETURNS tinyint(1)
READS SQL DATA
BEGIN
DECLARE vHasPassport BOOL;
DECLARE vIsAlreadySent BOOL;
DECLARE vItemCategoryFk INT;
SELECT id INTO vItemCategoryFk FROM itemCategory WHERE code = 'plant';
SELECT COUNT(*) INTO vHasPassport
FROM botanicExport be
JOIN itemBotanical ib
ON be.ediGenusFk = ib.genusFk
AND IFNULL(be.ediSpecieFk, ib.specieFk) = ib.specieFk
AND be.restriction = 'pasaporte fitosanitario'
JOIN item i ON i.id = ib.itemFk
JOIN itemType it ON it.id = i.typeFk
WHERE ib.itemFk = vItemFk
AND it.categoryFk = vItemCategoryFk;
IF vHasPassport THEN
SELECT COUNT(*) INTO vIsAlreadySent
FROM mail m
JOIN buy b ON b.id = vBuyFk
WHERE m.`body` LIKE CONCAT('%', b.ektFk, '%')
LIMIT 1;
END IF;
RETURN vHasPassport AND NOT vIsAlreadySent;
END$$
DELIMITER ;