-- Entradas de almacen por linea del mes (sublinea en Odoo)

SET NAMES 'utf8';

SELECT
	RIGHT(l.id_linea,2) AS Product_line_code,
       	l.descripcion AS Product_line,
       	SUM(ei.costo) AS Cost, 
	ROUND(SUM(ei.cantidad * i.peso), 2) AS Amount_Kg

FROM
	entrada e
	JOIN entrada_has_item ei
		ON ( e.id_entrada = ei.id_entrada
			AND e.id_sucursal = ei.id_sucursal )
	JOIN item i
		USING ( id_item )
	JOIN linea l
		ON ( l.id_linea = i.id_linea
			AND l.tipo = i.tipo )
	JOIN proveedor p
		ON ( p.id_proveedor = e.id_proveedor )
	JOIN proveedor_has_sucursal ps
		ON ( p.id_proveedor = ps.id_proveedor
			AND ps.id_sucursal = e.id_sucursal )

WHERE
	p.id_proveedor > 999
	AND EXTRACT(YEAR_MONTH FROM e.fecha) = @MES

GROUP BY
	l.id_linea

ORDER BY
	Amount_Kg DESC
