DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelvingSale_setQuantity`(
	vItemShelvingSaleFk INT(10),
	vQuantity DECIMAL(10,0),
	vIsItemShelvingSaleEmpty BOOLEAN
)
BEGIN
/**
 * Gestiona la reserva de un itemShelvingFk, actualizando isPicked y quantity
 * en vn.itemShelvingSale y vn.sale.isPicked en caso necesario. 
 * Si la reserva de la ubicación es fallida, se regulariza la situación 
 * 
 * @param vItemShelvingSaleFk Id itemShelvingSaleFK 
 * @param vQuantity Cantidad real que se ha cogido de la ubicación
 * @param vIsItemShelvingSaleEmpty determina si ka ubicación itemShelvingSale se ha 
 * quedado vacio tras el movimiento
 */
	DECLARE vSaleFk INT;
	DECLARE vCursorSaleFk INT;
	DECLARE vItemShelvingFk INT;
	DECLARE vReservedQuantity INT;
	DECLARE vRemainingQuantity INT;
	DECLARE vItemFk INT;
	DECLARE vUserFk INT;
	DECLARE vDone BOOLEAN DEFAULT FALSE;
	DECLARE vSales CURSOR FOR 
		SELECT iss.saleFk, iss.userFk
			FROM itemShelvingSale iss
				JOIN sale s ON s.id = iss.saleFk
			WHERE iss.id = vItemShelvingSaleFk
				AND s.itemFk = vItemFk
				AND NOT iss.isPicked;

	DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;

	IF (SELECT isPicked FROM itemShelvingSale WHERE id = vItemShelvingSaleFk) THEN
		CALL util.throw('Booking completed');
	END IF;

	SELECT s.itemFk, iss.saleFk, iss.itemShelvingFk 
			INTO vItemFk, vSaleFk, vItemShelvingFk
		FROM itemShelvingSale iss
				JOIN sale s ON s.id = iss.saleFk
			WHERE iss.id = vItemShelvingSaleFk
				AND NOT iss.isPicked;
	
	UPDATE itemShelvingSale 
		SET isPicked = TRUE,
			quantity = vQuantity
		WHERE id = vItemShelvingSaleFk;

	UPDATE itemShelving
		SET visible = IF(vIsItemShelvingSaleEmpty, 0, GREATEST(0,visible - vQuantity))
		WHERE id = vItemShelvingFk;

	IF vIsItemShelvingSaleEmpty THEN
		OPEN vSales;
l:		LOOP
			SET vDone = FALSE;
			FETCH vSales INTO vCursorSaleFk, vUserFk;
			IF vDone THEN
				LEAVE l;
			END IF;

			CREATE OR REPLACE TEMPORARY TABLE tmp.sale
			(INDEX(saleFk, userFk))
			ENGINE = MEMORY
			SELECT vCursorSaleFk, vUserFk;

			CALL itemShelvingSale_reserveWhitUser();
			DROP TEMPORARY TABLE tmp.sale;
	
		END LOOP;
		CLOSE vSales;
	
		DELETE iss
			FROM itemShelvingSale iss
				JOIN sale s ON s.id = iss.saleFk
			WHERE iss.id = vItemShelvingSaleFk
				AND s.itemFk = vItemFk
				AND NOT iss.isPicked;
	END IF;

	SELECT SUM(quantity) INTO vRemainingQuantity
		FROM itemShelvingSale
			WHERE saleFk = vSaleFk
				AND NOT isPicked;

	IF vRemainingQuantity THEN 
		CALL itemShelvingSale_reserveBySale (vSaleFk, vRemainingQuantity, NULL);
	
		SELECT SUM(quantity) INTO vRemainingQuantity
			FROM itemShelvingSale
				WHERE saleFk = vSaleFk
					AND NOT isPicked;

		IF NOT vRemainingQuantity <=> 0 THEN
			SELECT SUM(iss.quantity) 
				INTO vReservedQuantity
				FROM itemShelvingSale iss 
					WHERE iss.saleFk = vSaleFk;
	
			CALL saleTracking_new(
				vSaleFk,
				TRUE, 
				vReservedQuantity, 
				`account`.`myUser_getId`(), 
				NULL,
				'PREPARED',
				TRUE);
	
			UPDATE sale s
				SET s.quantity = vReservedQuantity
				WHERE s.id = vSaleFk ;	
		END IF;
	END IF;
END$$
DELIMITER ;
