DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` TRIGGER `vn`.`sale_beforeUpdate`
	BEFORE UPDATE ON `sale`
	FOR EACH ROW
BEGIN
/*		
	IF (SELECT COUNT(*) from item i WHERE id = NEW.itemFk AND family = 'SER') THEN
		CALL util.throw('Cannot insert a service item into a ticket');
	END IF;
*/
	SET NEW.editorFk = account.myUser_getId();
	
	IF NEW.discount > 100 THEN
		SET NEW.discount = 0;
	END IF;

	IF old.discount > 0 AND  NEW.discount = 0 THEN
		INSERT INTO ticketLog
		SET originFk = NEW.ticketFk, userFk = account.myUser_getId(), `action` = 'insert', 
			description = CONCAT('Cambio de descuento del item :', ' ', new.itemFk, ' de ', old.discount ,' a 0 ');
	END IF;
END$$
DELIMITER ;
