DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_recalc`(vSelf INT)
BEGIN
/**
 * Recalculates the order total.
 *
 * @param vSelf The order id
 */
	DECLARE vTaxableBase DECIMAL(10,2);
	DECLARE vTax DECIMAL(10,2);

	DROP TEMPORARY TABLE IF EXISTS tmp.`order`;
	CREATE TEMPORARY TABLE tmp.`order`
		ENGINE = MEMORY
		SELECT vSelf orderFk;
		
	CALL order_getTax;

	SELECT IFNULL(SUM(taxableBase), 0.0), IFNULL(SUM(tax), 0.0)
		INTO vTaxableBase, vTax
		FROM tmp.orderAmount;

	UPDATE `order`
		SET taxableBase = vTaxableBase,
			tax = vTax,
			total = vTaxableBase + vTax
		WHERE id = vSelf;

	DROP TEMPORARY TABLE
		tmp.`order`,
		tmp.orderTax;
END$$
DELIMITER ;