DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`invoiceSerialArea`(vType CHAR(1), vTaxArea VARCHAR(25))
	RETURNS char(1) CHARSET utf8mb3 COLLATE utf8mb3_unicode_ci
	DETERMINISTIC
BEGIN
	DECLARE vSerie CHAR(1);

	IF vType = 'R' THEN
		SELECT
			CASE vTaxArea
				WHEN 'CEE' THEN 'H'
				WHEN 'WORLD' THEN 'E'
				ELSE 'T'
			END INTO vSerie;
	-- Factura multiple
	ELSEIF vType = 'M' THEN
		SELECT
			CASE vTaxArea
				WHEN 'CEE' THEN 'H'
				WHEN 'WORLD' THEN 'E'
				ELSE 'M'
			END INTO vSerie;
	-- Factura global
	ELSEIF vType = 'G' THEN
		SELECT
			CASE vTaxArea
				WHEN 'CEE' THEN 'V'
				WHEN 'WORLD' THEN 'X'
				ELSE 'A'
			END INTO vSerie;
	END IF;
	RETURN vSerie;
END$$
DELIMITER ;
