2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`addressTaxArea`()
|
2024-01-15 11:31:03 +00:00
|
|
|
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 ;
|