DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`currency_getRate`(
	vSelf VARCHAR(3),
	vDated DATE
)
	RETURNS DECIMAL(10,2)
	NOT DETERMINISTIC
	READS SQL DATA
BEGIN
/**
 * Returns the rate exchange based on the specified currency
 * and yesterday's exchange rate.
 *
 * @param vSelf The currency code
 * @param vDated The date of exchangue
 * @return vForeignRate The rate e of currency exchange
 */
	DECLARE vForeignRate DECIMAL(10,2);

	SELECT value INTO vForeignRate
		FROM referenceRate
		WHERE dated = IFNULL(vDated, util.yesterday())
			AND currencyFk = vSelf
		ORDER BY dated DESC
		LIMIT 1;

	RETURN vForeignRate;
END$$
DELIMITER ;
