use account;

INSERT INTO role
 	SET name = 'reviewer',
		description = 'Revisor de producción',
		hasLogin = TRUE,
		created = util.VN_CURDATE(),
		modified = util.VN_CURDATE(),
		editorFk = NULL;

INSERT INTO roleInherit(
	role,
	inheritsFrom
)
	SELECT r1.id,
				r2.id 
			FROM role r1
				JOIN role  r2
			WHERE r1.name = 'reviewer'
				AND r2.name = 'production'
	UNION
	SELECT ri.role, 
			r2.id
		FROM roleInherit ri
			JOIN role r1 ON r1.id = ri.role
			JOIN role r2 ON r2.name = 'reviewer'
		WHERE r1.name IN ('claimManager', 'productionBoss')
		GROUP BY ri.role; 

DELETE ri
	FROM roleInherit ri
		JOIN role r1 ON ri.role = r1.id
		JOIN role r2 ON ri.inheritsFrom = r2.id
	WHERE r1.name = 'replenisher' 
		AND r2.name = 'buyer';

UPDATE salix.ACL 
	SET principalId = 'reviewer'
	WHERE property = 'isInPreparing';

UPDATE user u
		JOIN vn.workerDepartment wd ON wd.workerFk = u.id
		JOIN vn.department d ON wd.departmentFk = d.id 
		JOIN role r ON r.name = 'reviewer'
	SET u.role = r.id
	WHERE d.name IN ('REVISION', 'PREVIA');