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

40 lines
1.1 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`entry_isIntrastat`(vSelf INT)
RETURNS int(11)
NOT DETERMINISTIC
READS SQL DATA
BEGIN
/**
* Devuelve si el proveedor de la entrada es de la CEE
*
* @param vSelf Id de la entrada
* @return true si es de la CEE
*/
DECLARE vIsUeeSupplier BOOLEAN;
DECLARE vIsUeeCompany BOOLEAN;
DECLARE vSupplierCountry INT;
DECLARE vCompanyCountry INT;
SELECT IFNULL(a.isUeeMember, c.isUeeMember), c.id
INTO vIsUeeSupplier, vSupplierCountry
FROM entry e
JOIN supplier s ON s.id = e.supplierFk
JOIN country c ON c.id = s.countryFk
LEFT JOIN province p ON p.id = s.provinceFk
LEFT JOIN autonomy a ON a.id = p.autonomyFk
WHERE e.id = vSelf;
SELECT IFNULL(a.isUeeMember, c.isUeeMember), c.id
INTO vIsUeeCompany, vCompanyCountry
FROM entry e
JOIN supplier s ON s.id = e.companyFk
JOIN country c ON c.id = s.countryFk
LEFT JOIN province p ON p.id = s.provinceFk
LEFT JOIN autonomy a ON a.id = p.autonomyFk
WHERE e.id = vSelf;
RETURN vIsUeeCompany AND vIsUeeSupplier AND (vSupplierCountry <> vCompanyCountry);
END$$
DELIMITER ;