DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`workerDisable`(vUserId int)
mainLabel:BEGIN

	IF (SELECT COUNT(*) FROM workerDisableExcluded WHERE workerFk = vUserId AND (dated > util.VN_CURDATE() OR dated IS NULL)) > 0 THEN
		LEAVE mainLabel;
	END IF;

	DELETE cp FROM clientProtected cp
		JOIN client c ON c.id = cp.clientFk
		WHERE c.salesPersonFk = vUserId;

	DELETE FROM account.account
		WHERE id = vUserId;

	UPDATE account.user 
		SET role = 2
		WHERE id = vUserId;	

	DELETE FROM pbx.sip 
		WHERE user_id = vUserId;   

	UPDATE `client` c
		JOIN payMethod p ON p.name = 'CONTADO'
		SET c.credit = 0, c.payMethodFk = p.id, hasCoreVnl = FALSE
		WHERE c.id = vUserId;
	
	UPDATE `client` c
		SET c.salesPersonFk = null
		WHERE c.salesPersonFk = vUserId;

	UPDATE locker l
		SET l.workerFk = NULL
		WHERE l.workerFk = vUserId; 
END$$
DELIMITER ;
