DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `edi`.`putOrder_beforeUpdate`
	BEFORE UPDATE ON `putOrder`
	FOR EACH ROW
BEGIN
	
	DECLARE vError VARCHAR(100) DEFAULT 'Orderregel niet meer teruggevonden op basis van de orderps';
    DECLARE vVmpIdError INT DEFAULT 7;
    DECLARE vVmpFk INT;
   	DECLARE vSupplyResponseNumberOfUnits INT;
       
   	SELECT sr.vmpID INTO vVmpFk 
		FROM edi.supplyResponse sr 
		WHERE sr.id = NEW.supplyResponseID;
		
		IF NEW.OrderStatus = 3 
			AND NOT (NEW.OrderStatus <=> OLD.OrderStatus) 
			AND NEW.error = vError 
			AND vVmpFk = vVmpIdError 
		
		THEN 
		
			SET NEW.OrderStatus = 2;
		
		END IF;
	
	-- Error de disponible menor de lo solicitado
	IF NEW.error LIKE 'error2602%' THEN
	
		SELECT NumberOfUnits INTO vSupplyResponseNumberOfUnits
			FROM edi.supplyResponse sr 
			WHERE sr.ID = NEW.supplyResponseID;
		
		SET NEW.error = CONCAT('(',vSupplyResponseNumberOfUnits,') ', NEW.error);
	
	END IF;
END$$
DELIMITER ;
