DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`addressTaxArea`() READS SQL DATA BEGIN /** * Devuelve el taxArea para un conjunto de Consignatarios y empresas, * * @table tmp.addressCompany(addressFk, companyFk) valores a calcular * @return tmp.addressTaxArea(addressFk,companyFk) */ DROP TEMPORARY TABLE IF EXISTS tmp.addressTaxArea; CREATE TEMPORARY TABLE tmp.addressTaxArea (PRIMARY KEY (addressFk, companyFk)) ENGINE = MEMORY SELECT CASE WHEN (NOT IFNULL(auClient.isUeeMember, cClient.isUeeMember) OR NOT cSupplier.isUeeMember) AND NOT (auSupplier.id <=> auClient.id) THEN 'WORLD' WHEN cClient.isUeeMember AND cSupplier.isUeeMember AND c.countryFk != s.countryFk AND c.isVies THEN 'CEE' WHEN a.isEqualizated AND cClient.code = 'ES' THEN 'EQU' ELSE 'NATIONAL' END AS areaFk, ac.addressFk, ac.companyFk FROM tmp.addressCompany ac JOIN address a ON a.id = ac.addressFk JOIN `client` c ON c.id = a.clientFk JOIN country cClient ON cClient.id = c.countryFk LEFT JOIN province pClient ON pClient.id = c.provinceFk LEFT JOIN autonomy auClient ON auClient.id = pClient.autonomyFk JOIN supplier s ON s.id = ac.companyFk JOIN country cSupplier ON cSupplier.id = s.countryFk LEFT JOIN province pSupplier ON pSupplier.id = s.provinceFk LEFT JOIN autonomy auSupplier ON auSupplier.id = pSupplier.autonomyFk; END$$ DELIMITER ;