-- Entradas de almacen por linea del mes

SELECT
	l.id_linea AS Product_line_code,
       	l.descripcion AS Product_line,
       	SUM(ei.costo * ei.cantidad) 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
		ON ( ei.id_item = i.id_item
			AND ei.id_sucursal = i.id_sucursal )
	JOIN linea l
		ON ( l.id_linea = left( i.id_linea, 2 )
			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
