DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`confection_control_source`(vDated DATE, vScopeDays TINYINT)
BEGIN

	DECLARE vMidnight DATETIME DEFAULT TIMESTAMP(vDated,'23:59:59');
	DECLARE vEndingDate DATETIME DEFAULT TIMESTAMPADD(DAY,vScopeDays,vMidnight);
	DECLARE maxAlertLevel INT DEFAULT 2;

	DROP TEMPORARY TABLE IF EXISTS tmp.production_buffer;

	CREATE TEMPORARY TABLE tmp.production_buffer
	ENGINE = MEMORY
		SELECT 
			date(t.Fecha) as Fecha,
			hour(t.Fecha) as Hora,
			hour(t.Fecha) as Departure,
			t.Id_Ticket,
            m.Id_Movimiento,
			m.Cantidad,
			m.Concepte,
            ABS(m.Reservado) Reservado,
			i.Categoria,
            tp.Tipo,
			t.Alias as Cliente,
			wh.name as Almacen,
			t.warehouse_id,
			cs.province_id,
			a.agency_id,
            ct.description as Taller,
            stock.visible,
            stock.available
		FROM vn2008.Tickets t
			JOIN vn2008.Agencias a ON a.Id_Agencia = t.Id_Agencia
			JOIN vn2008.warehouse wh ON wh.id = t.warehouse_id
			JOIN vn2008.Movimientos m ON m.Id_Ticket = t.Id_Ticket
            JOIN vn2008.Articles i ON i.Id_Article = m.Id_Article
			JOIN vn2008.Tipos tp ON tp.tipo_id = i.tipo_id
            JOIN vn.confectionType ct ON ct.id = tp.confeccion
			JOIN vn2008.Consignatarios cs on cs.Id_Consigna = t.Id_Consigna
			LEFT JOIN vn.ticketState tls on tls.ticketFk = t.Id_Ticket
            LEFT JOIN 
				(					
					SELECT item_id, sum(visible) visible, sum(available) available
					FROM
					(
					SELECT a.item_id, 0 as visible, a.available
					FROM cache.cache_calc cc
					LEFT JOIN cache.available a ON a.calc_id = cc.id
					WHERE cc.cache_id IN (2,8) 
						AND cc.params IN (concat("1/", util.VN_CURDATE()),concat("44/", util.VN_CURDATE()))

					UNION ALL

					SELECT v.item_id, v.visible, 0 as available
					FROM cache.cache_calc cc
					LEFT JOIN cache.visible v ON v.calc_id = cc.id
					where cc.cache_id IN (2,8) and cc.params IN ("1","44")
					) sub
					GROUP BY item_id
                ) stock ON stock.item_id = m.Id_Article
		WHERE tp.confeccion 
			AND tls.alertLevel < maxAlertLevel
			AND wh.hasConfectionTeam
			AND t.Fecha BETWEEN vDated AND vEndingDate
            AND m.Cantidad > 0;

	-- Entradas

	INSERT INTO tmp.production_buffer(
										Fecha,
										Id_Ticket,
										Cantidad,
										Concepte,
										Categoria,
										Cliente,
										Almacen,
                                        Taller
										)
	    SELECT 
				tr.shipment AS Fecha, 
				e.Id_Entrada AS Id_Ticket,
				c.Cantidad,
				a.Article,
				a.Categoria,
				whi.name as Cliente,
				who.name as Almacen,
				ct.description as Taller
			FROM vn2008.Compres c
				JOIN vn2008.Entradas e ON e.Id_Entrada = c.Id_Entrada
				JOIN vn2008.travel tr ON tr.id = e.travel_id
				JOIN vn2008.warehouse whi ON whi.id = tr.warehouse_id
				JOIN vn2008.warehouse who ON who.id = tr.warehouse_id_out
				JOIN vn2008.Articles a ON a.Id_Article = c.Id_Article
				JOIN vn2008.Tipos tp ON tp.tipo_id = a.tipo_id
				JOIN vn.confectionType ct ON ct.id = tp.confeccion
			WHERE who.hasConfectionTeam
				AND tp.confeccion
				AND tr.shipment BETWEEN vDated AND vEndingDate;
                
		
		SELECT * FROM tmp.production_buffer;

	
END$$
DELIMITER ;
