DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`worker_updateBusiness`(vSelf INT)
BEGIN
/**
 * Activates an account and configures its email settings.
 *
 * @param vSelf account id.
 */
	DECLARE vOldBusinessFk INT;
	DECLARE vNewBusinessFk INT;

	SELECT businessFk INTO vOldBusinessFk FROM worker WHERE id = vSelf;

	SELECT id INTO vNewBusinessFk
		FROM business
		WHERE workerFk = vSelf
			AND util.VN_CURDATE() BETWEEN started AND IFNULL(ended, util.VN_CURDATE())
		LIMIT 1;

	UPDATE worker
		SET businessFk = vNewBusinessFk
		WHERE id = vSelf;

	IF vOldBusinessFk IS NULL THEN
		CALL account.account_enable(vSelf);
				
		UPDATE client c
				JOIN payMethod pm ON pm.code = 'bankDraft'
			SET c.payMethodFk = pm.id
			WHERE c.id = vSelf
				AND c.iban;
	END IF;
END$$
DELIMITER ;
