DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `stock`.`buyOut_refreshOrder`(
	`vTable` VARCHAR(255),
	`vId` INT)
BEGIN

	CREATE OR REPLACE TEMPORARY TABLE tLotStatus
		ENGINE = MEMORY
		SELECT lotFk,
				@isExcluded := o.confirmed OR NOT isReserved OR r.amount <= 0 isExcluded,
				NOT @isExcluded isIncluded
			FROM hedera.orderRow r
				JOIN hedera.`order` o ON o.id = r.orderFk
			WHERE
				   (vTable = 'lot' AND r.lotFk = vId)
				OR (vTable = 'order' AND o.id = vId);

	CREATE OR REPLACE TEMPORARY TABLE tLotAlive
		ENGINE = MEMORY
		SELECT
				oo.lotFk,
				r.warehouseFk,
				r.shipment dated,
				r.itemFk,
				r.amount quantity,
				r.created,
				FALSE isPicked
			FROM tLotStatus oo
				JOIN hedera.orderRow r ON r.lotFk = oo.lotFk
				JOIN hedera.`order` o ON o.id = r.orderFk
			WHERE oo.isIncluded;

	CALL buyOut_refresh(vTable, vId, 'orderRow');

	DROP TEMPORARY TABLE tLotStatus, tLotAlive;
END$$
DELIMITER ;
