DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`ticket_getSplitList`(vDated DATE, vHour TIME, vWarehouseFk INT)
BEGIN
/**
 * Devuelve un listado con los tickets posibles para splitar HOY.
 *
 * @param vDated Fecha a estudiar
 * @param vHour Hora a partir de la cual todavía se puede enviar, si la fecha es la de hoy
 * @param vWarehouseFk Almacén
 */
	DECLARE vAvailableCache INT;
	DECLARE vVisibleCache INT;

	CALL cache.available_refresh(vAvailableCache, FALSE, vWarehouseFk, util.VN_CURDATE());
	CALL cache.visible_refresh(vVisibleCache, FALSE, vWarehouseFk);
	CALL `vn`.`zoneClosure_recalc`();

	CREATE OR REPLACE TEMPORARY TABLE tmp.salesToSplit
		(saleFk INT PRIMARY KEY,
		ticketFk INT,
		problem VARCHAR(1),
		INDEX(ticketFk)) ENGINE = MEMORY;

	CREATE OR REPLACE TEMPORARY TABLE tItemShelvingStock_byWarehouse
		(INDEX (itemFk, warehouseFk))
		ENGINE = MEMORY
		SELECT ish.itemFk itemFk,
				SUM(ish.visible) visible,
				s.warehouseFk warehouseFk
			FROM itemShelving ish
				JOIN shelving sh ON sh.code = ish.shelvingFk
				JOIN parking p ON p.id = sh.parkingFk
				JOIN sector s ON s.id = p.sectorFk
			GROUP BY ish.itemFk,
				s.warehouseFk;

	INSERT INTO tmp.salesToSplit(saleFk, ticketFk, problem)
		SELECT 	s.id, s.ticketFk, 'F'
			FROM ticket t
				JOIN sale s ON s.ticketFk = t.id
				JOIN item i ON i.id = s.itemFk
				JOIN itemType it on it.id = i.typeFk
				JOIN itemCategory ic ON ic.id = it.categoryFk 
				LEFT JOIN cache.visible v ON v.item_id = i.id
					AND v.calc_id = vVisibleCache
				LEFT JOIN cache.available av ON av.item_id = i.id
					AND av.calc_id = vAvailableCache
				LEFT JOIN tItemShelvingStock_byWarehouse issw ON issw.itemFk = i.id
					AND issw.warehouseFk = t.warehouseFk 
			WHERE IFNULL(v.visible,0) < s.quantity
				AND IFNULL(av.available ,0) < s.quantity
				AND IFNULL(issw.visible, 0) < s.quantity
				AND s.isPicked = FALSE
				AND s.reserved = FALSE
				AND ic.merchandise = TRUE
				AND t.shipped BETWEEN util.VN_CURDATE() AND util.dayend(util.VN_CURDATE())
				AND NOT i.generic
				AND t.warehouseFk = vWarehouseFk;
				
	INSERT IGNORE INTO tmp.salesToSplit(saleFk, ticketFk, problem)
		SELECT 	s.id, s.ticketFk, 'I'
			FROM ticket t 
				JOIN sale s ON s.ticketFk = t.id
				JOIN item i ON i.id = s.itemFk
				JOIN itemType it on it.id = i.typeFk
				JOIN itemCategory ic ON ic.id = it.categoryFk
				LEFT JOIN cache.visible v ON v.item_id = s.itemFk
					AND v.calc_id = vVisibleCache
				LEFT JOIN tItemShelvingStock_byWarehouse issw ON issw.itemFk = i.id
					AND issw.warehouseFk = t.warehouseFk 
			WHERE IFNULL(v.visible,0) >= s.quantity
				AND IFNULL(issw.visible, 0) < s.quantity
				AND s.quantity > 0
				AND NOT s.isPicked
				AND NOT s.reserved
				AND ic.merchandise
				AND t.shipped BETWEEN util.VN_CURDATE() AND util.dayend(util.VN_CURDATE())
				AND NOT i.generic
				AND t.warehouseFk = vWarehouseFk;
				
	INSERT IGNORE INTO tmp.salesToSplit(saleFk, ticketFk, problem)
		SELECT 	s.id, s.ticketFk, 'R'
			FROM ticket t
				JOIN sale s ON s.ticketFk = t.id
				JOIN item i ON i.id = s.itemFk
				JOIN itemType it on it.id = i.typeFk
				JOIN itemCategory ic ON ic.id = it.categoryFk
				LEFT JOIN cache.visible v ON v.item_id = s.itemFk
					AND v.calc_id = vVisibleCache
				LEFT JOIN cache.available av ON av.item_id = i.id
					AND av.calc_id = vAvailableCache
				LEFT JOIN tItemShelvingStock_byWarehouse issw ON issw.itemFk = i.id
					AND issw.warehouseFk = t.warehouseFk 
			WHERE IFNULL(v.visible,0) < s.quantity
				AND IFNULL(av.available ,0) >= s.quantity
				AND IFNULL(issw.visible, 0) < s.quantity
				AND s.quantity > 0
				AND NOT s.isPicked
				AND NOT s.reserved
				AND ic.merchandise
				AND t.shipped BETWEEN util.VN_CURDATE() AND util.dayend(util.VN_CURDATE())
				AND NOT i.generic
				AND t.warehouseFk = vWarehouseFk;
		
	CREATE OR REPLACE TEMPORARY TABLE tTicketsToSplit(
		ticketFk INT PRIMARY KEY,
		problems VARCHAR(100),
		addressFk INT,
		ipt VARCHAR(10),
		etd TIME,
		INDEX(addressFk)
	) ENGINE = MEMORY;

	INSERT INTO tTicketsToSplit
		SELECT ss.ticketFk,
			GROUP_CONCAT(DISTINCT ss.problem ORDER BY ss.problem) problems,
			t.addressFk,
			GROUP_CONCAT(DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk) ipt,
			DATE_FORMAT(IF(HOUR(t.shipped), 
				t.shipped, 
				IFNULL(zc.hour, z.HOUR)),'%H:%i') etd
			FROM tmp.salesToSplit ss
				JOIN ticket t ON t.id = ss.ticketFk
				JOIN sale s ON s.id = ss.saleFk
				JOIN item i ON i.id = s.itemFk
				LEFT JOIN zoneClosure zc ON zc.zoneFk = t.zoneFk
				LEFT JOIN zone z ON z.id = t.zoneFk 
			GROUP BY ss.ticketFk;

	SELECT ts.*, 
			t.id ticketFuture,
			st.name state,
			zc.`hour` tfEtd,
			GROUP_CONCAT(
				DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk
			) tfIpt,
			st2.name tfState
		FROM tTicketsToSplit ts
			LEFT JOIN ticketState ts2 ON ts2.ticketFk = ts.ticketFk
			LEFT JOIN `state` st ON st.id = ts2.stateFk
			LEFT JOIN ticket t ON t.id != ts.ticketFk 
				AND t.addressFk = ts.addressFk
				AND t.shipped BETWEEN vDated AND util.dayend(vDated)
			LEFT JOIN zoneClosure zc ON zc.zoneFk = t.zoneFk  AND zc.dated = vDated
			LEFT JOIN sale s ON s.ticketFk = t.id
			LEFT JOIN item i ON i.id = s.itemFk
			LEFT JOIN ticketState tst ON tst.ticketFk = t.id 
			LEFT JOIN state st2 ON st2.id = tst.stateFk
		WHERE (t.id IS NULL 
				OR (vDated > util.VN_CURDATE())
				OR (vDated = util.VN_CURDATE() AND vHour >= zc.`hour`))
			AND ts.etd <= vHour
		GROUP BY ts.ticketFk, t.id
		ORDER BY ts.etd, ts.ticketFk;
		
	DROP TEMPORARY TABLE
		tTicketsToSplit,
		tItemShelvingStock_byWarehouse;
END$$
DELIMITER ;
