DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`manaSpellersRequery`(vWorkerFk INTEGER)
`whole_proc`:
BEGIN
/**
 * Guarda en workerMana el mana consumido por un trabajador
 *
 * @param vWorkerFk Id Trabajador
 */
	DECLARE vWorkerIsExcluded BOOLEAN;

	SELECT COUNT(*) INTO vWorkerIsExcluded
		FROM workerManaExcluded
		WHERE workerFk = vWorkerFk;

	IF vWorkerIsExcluded THEN
		LEAVE whole_proc;
	END IF;

	CREATE OR REPLACE TEMPORARY TABLE tmp.client
		SELECT id
			FROM client 
			WHERE salesPersonFk = vWorkerFk;

	CALL client_getMana();

	INSERT INTO workerMana (workerFk, amount)
		SELECT vWorkerFk, sum(mana)
			FROM tmp.clientMana
			ON DUPLICATE KEY UPDATE amount = VALUES(amount);

	DROP TEMPORARY TABLE 
		tmp.client,
		tmp.clientMana;
END$$
DELIMITER ;
