DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`workerDisable`(vUserFk INT)
l:BEGIN

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

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

	DELETE FROM account.account WHERE id = vUserFk;

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

	DELETE FROM pbx.sip WHERE user_id = vUserFk;

	UPDATE `client` c
			JOIN payMethod p ON p.name = 'CONTADO'
		SET c.credit = 0, 
			c.payMethodFk = p.id, 
			hasCoreVnl = FALSE
		WHERE c.id = vUserFk;
	
	UPDATE `client` c
		SET c.salesPersonFk = NULL,
			c.salesDepartmentFk = NULL
		WHERE c.salesPersonFk = vUserFk;
END$$
DELIMITER ;
