
USE `vn2008`;
DROP procedure IF EXISTS `vn2008`.`bionic_available_`;

DELIMITER $$
USE `vn2008`$$
CREATE DEFINER=`root`@`%` PROCEDURE `__bionic_available_`(
	vDate DATE,
    vAddress INT,
    vAgency INT)
BEGIN
/**
 * Calcula el disponible para un conjunto de almacenes y fechas
 * devueltos por el procedimiento agencyHourGetShipped()
 *
 * @deprecated Use vn.available_calc()
 * @table t_bionic_available Tabla con los ids de cache
 */
	DECLARE vAvailableCalc INT;
	DECLARE vShipment DATE;
	DECLARE vAgencyId INT;
    DECLARE vWh INT;
	DECLARE vDone BOOL;
	DECLARE cTravelTree CURSOR FOR
		SELECT warehouseFk, shipped FROM tmp.agencyHourGetShipped;
			
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
    
	-- Establecemos los almacenes y las fechas que van a entrar al disponible

	SELECT agency_id INTO vAgencyId
		FROM Agencias WHERE Id_Agencia = vAgency;

	CALL vn.agencyHourGetShipped (vDate, vAddress, vAgencyId);
	
	DROP TEMPORARY TABLE IF EXISTS t_bionic_available;
	CREATE TEMPORARY TABLE t_bionic_available(	
		calc_id INT UNSIGNED,
        PRIMARY KEY (calc_id)
	)
	ENGINE = MEMORY;

	OPEN cTravelTree;
	l: LOOP
		SET vDone = FALSE;
		FETCH cTravelTree INTO vWh, vShipment;
        
        IF vDone THEN
			LEAVE l;
		END IF;

		CALL `cache`.available_refresh (vAvailableCalc, FALSE, vWh, vShipment);

		INSERT IGNORE INTO t_bionic_available
			SET calc_id = vAvailableCalc;
    END LOOP;

	CLOSE cTravelTree;    
  	DROP TEMPORARY TABLE tmp.agencyHourGetShipped;
END$$

DELIMITER ;
;


USE `hedera`;
DROP procedure IF EXISTS `hedera`.`bionic_from_item`;

DELIMITER $$
USE `hedera`$$
CREATE DEFINER=`root`@`%` PROCEDURE `__bionic_from_item`(vItem INT)
BEGIN
	DECLARE vDate DATE;
	DECLARE vAddress INT;
	DECLARE vAgencyMode INT;
    
    SELECT sent, addressFk, agencyModeFk
    INTO vDate, vAddress, vAgencyMode
		FROM myBasket;

	CALL vn2008.bionic_from_item(vDate, vAddress, vAgencyMode, vItem);
END$$

DELIMITER ;
;


USE `vn2008`;
DROP procedure IF EXISTS `vn2008`.`bionic_from_order`;

DELIMITER $$
USE `vn2008`$$
CREATE DEFINER=`root`@`%` PROCEDURE `__bionic_from_order`(
    v_date DATE, --  fecha de recepcion de mercancia
    v_consigna INT,
    v_agencia INT,
    v_order INT)
BEGIN
	DROP TEMPORARY TABLE IF EXISTS tmp.bionic_calc;
	CREATE TEMPORARY TABLE tmp.bionic_calc
	(PRIMARY KEY (item_id))
	ENGINE = MEMORY
		SELECT item_id FROM order_row 
			WHERE order_id = v_order GROUP BY item_id;

	CALL bionic_calc (v_date, v_consigna, v_agencia);
    DROP TEMPORARY TABLE tmp.bionic_calc;
END$$

DELIMITER ;
;


USE `hedera`;
DROP procedure IF EXISTS `hedera`.`bionicCalcWithDate`;

DELIMITER $$
USE `hedera`$$
CREATE DEFINER=`root`@`%` PROCEDURE `__bionicCalcWithDate`(vDate DATE)
BEGIN
    DECLARE vMyAddressFk INTEGER;
    DECLARE vMyAgencyModeFk INTEGER;
    
	SELECT id, agencyModeFk INTO vMyAddressFk, vMyAgencyModeFk 
		FROM myAddress 
        WHERE isDefaultAddress;
      
	CALL vn2008.bionic_calc(vDate, vMyAddressFk, vMyAgencyModeFk);
END$$

DELIMITER ;
;


USE `hedera`;
DROP procedure IF EXISTS `hedera`.`bionic_calc`;

DELIMITER $$
USE `hedera`$$
CREATE DEFINER=`root`@`%` PROCEDURE `__bionic_calc`()
BEGIN
	DECLARE vDate DATE;
	DECLARE vAddress INT;
	DECLARE vAgencyMode INT;
    
    SELECT sent, addressFk, agencyModeFk
    INTO vDate, vAddress, vAgencyMode
		FROM myBasket;

	CALL vn2008.bionic_calc(vDate, vAddress, vAgencyMode);

	IF account.myUserGetName() = 'visitor'
    THEN
		DROP TEMPORARY TABLE tmp.bionic_component;
        UPDATE tmp.bionic_item SET price = NULL;
	END IF;
END$$

DELIMITER ;
;


USE `hedera`;
DROP procedure IF EXISTS `hedera`.`itemGetAvailableOrder`;

DELIMITER $$
USE `hedera`$$
CREATE DEFINER=`root`@`%` PROCEDURE `__itemGetAvailableOrder`(vOrder INT)
BEGIN
/**
 * Gets the available items list with an order params.
 *
 * @table tmp.itemAvailable
 */  
	DECLARE vDelivery DATE;
	DECLARE vAddress INT;
	DECLARE vAgency INT;
    
    SELECT landed, addressFk, agencyModeFk
    INTO vDelivery, vAddress, vAgency
		FROM myOrder
		WHERE id = vOrder;
  
	CALL vn2008.bionic_available_ (vDelivery, vAddress, vAgency);

	DROP TEMPORARY TABLE IF EXISTS tmp.itemAvailable;
	CREATE TEMPORARY TABLE tmp.itemAvailable
		(INDEX (id))
        ENGINE = MEMORY
		SELECT c.item_id id
			FROM cache.available c
				JOIN vn2008.t_bionic_available a ON c.calc_id = a.calc_id
			WHERE c.available > 0
			GROUP BY c.item_id;

	DROP TEMPORARY TABLE vn2008.t_bionic_available;
    
END$$

DELIMITER ;
;


USE `hedera`;
DROP procedure IF EXISTS `hedera`.`itemGetAvailable`;

DELIMITER $$
USE `hedera`$$
CREATE DEFINER=`root`@`%` PROCEDURE `__itemGetAvailable`()
BEGIN
/**
 * Gets the available items list.
 *
 * @table tmp.itemAvailable
 */  
	DECLARE vDelivery DATE;
	DECLARE vAddress INT;
	DECLARE vAgency INT;

	SELECT sent, addressFk, agencyModeFk
	INTO vDelivery, vAddress, vAgency
		FROM myBasket;

	CALL vn2008.bionic_available_ (vDelivery, vAddress, vAgency);

	DROP TEMPORARY TABLE IF EXISTS tmp.itemAvailable;
	CREATE TEMPORARY TABLE tmp.itemAvailable
		(INDEX (id))
		ENGINE = MEMORY
		SELECT c.item_id id
			FROM cache.available c
				JOIN vn2008.t_bionic_available a ON c.calc_id = a.calc_id
			WHERE c.available > 0
			GROUP BY c.item_id;

	DROP TEMPORARY TABLE vn2008.t_bionic_available;
END$$

DELIMITER ;
;
