DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`itemShelvingSale_setPicked`(
	vSaleGroupFk INT(10)
)
BEGIN
/**
 * Gestiona la reserva de un vn.saleGroup actualizando vn.itemShelvingSale.isPicked 
 * y cambiando el estado de la vn.sale
 * 
 * @param vSaleGroupFk Id saleGroupFk 
 */
	DECLARE EXIT HANDLER FOR SQLEXCEPTION
	BEGIN
		ROLLBACK;
		RESIGNAL;
	END;

	IF NOT (SELECT COUNT(*) FROM saleGroup WHERE id = vSaleGroupFk) THEN
		CALL util.throw('Sale group not exists');
	END IF;

	START TRANSACTION;

	UPDATE itemShelvingSale iss
			JOIN sale s ON s.id = iss.saleFk
			JOIN saleGroupDetail sg ON sg.saleFk = s.id
		SET iss.isPicked = TRUE
		WHERE sg.saleGroupFk = vSaleGroupFk;

	CALL saleTracking_addPreparedSaleGroup(vSaleGroupFk);

	COMMIT;
END$$
DELIMITER ;