DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`sharingCart_beforeUpdate`
	BEFORE UPDATE ON `sharingCart`
	FOR EACH ROW
BEGIN
	IF NEW.workerFk = NEW.workerSubstitute THEN
		CALL util.throw ('worker and workerSubstitute must be different');
	ELSE
		DELETE sc FROM sharingCartDaily sc  
			WHERE sc.dated BETWEEN OLD.started AND OLD.ended AND 
				  sc.ownerFk = OLD.workerFk AND 
				  sc.substituteFk = OLD.workerSubstitute;
                  
        INSERT IGNORE INTO sharingCartDaily (ownerFk, substituteFk, dated) 
			SELECT NEW.workerFk, NEW.workerSubstitute, dated 
				FROM time 
                WHERE dated BETWEEN NEW.started AND NEW.ended;

    END IF;
END$$
DELIMITER ;
