-- Items con existencia actual

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 sublinea,
	la.id_linea_analisis AS cod_linea_analisis,
	la.descripcion AS linea_analisis
FROM
	existencia_item_bodega ex
	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 ex.cantidad > 0
