-- Items con entradas en los ultimos 12 meses

SELECT
	DISTINCT i.id_item AS cod_item,
	i.id_sucursal AS sucursal,
	i.descripcion AS item,
	i.unidad1 AS unidad_medida,
	i.peso AS peso_kg,
	IF(i.tipo = 1, 'Producto', 'Servicio') AS tipo,
	l.id_linea AS cod_linea,
	l.descripcion AS linea,
	la.id_linea_analisis AS cod_linea_analisis,
	la.descripcion AS linea_analisis
FROM
	entrada e 
        JOIN entrada_has_item ei 
        USING(id_entrada,id_sucursal) 
	JOIN item i
	USING (id_item,id_sucursal)
	LEFT JOIN linea l
	ON l.id_linea = LEFT( i.id_linea, 2 )
	AND l.tipo = i.tipo
	LEFT JOIN linea_has_linea_analisis lla
	ON lla.id_linea = LEFT( i.id_linea, 2 )
	LEFT JOIN linea_analisis la
	USING (id_linea_analisis)

WHERE PERIOD_DIFF(
          DATE_FORMAT(now(),'%Y%m'),
          DATE_FORMAT(e.fecha,'%Y%m')
        ) <= 12

