DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`invoiceSerial`(vClientFk INT, vCompanyFk INT, vType CHAR(1))
	RETURNS char(1) CHARSET utf8mb3 COLLATE utf8mb3_general_ci
	DETERMINISTIC
BEGIN
	/**
	* Obtiene la serie de de una factura 
	* dependiendo del area del cliente.
	* 
	* @param vClientFk Id del cliente
	* @param vCompanyFk Id de la empresa
	* @param vType Tipo de factura ["R", "M", "G"]
	* @return Serie de la factura
	*/
	DECLARE vTaxArea VARCHAR(25);
	DECLARE vSerie CHAR(1);

	IF (SELECT hasInvoiceSimplified FROM client WHERE id = vClientFk) THEN
		RETURN 'S';
	END IF;

	SELECT clientTaxArea(vClientFk, vCompanyFk) INTO vTaxArea;
    SELECT invoiceSerialArea(vType,vTaxArea) INTO vSerie;
	RETURN vSerie;
END$$
DELIMITER ;
