DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`waste_addSales`()
BEGIN
	DECLARE vWeek INT;
	DECLARE vYear INT;

	SELECT week, year
		INTO vWeek, vYear
	FROM vn.time
		WHERE dated =  util.VN_CURDATE();
	
	REPLACE bs.waste
	SELECT *, 100 * mermas / total as porcentaje
	FROM (
		SELECT  buyer,  
				year, 
				week,
				family,
				itemFk,
				itemTypeFk,
				floor(sum(value)) as total, 
				floor(sum(IF(clientTypeFk = 'loses', value, 0))) as mermas
			FROM vn.saleValue 
			where year = vYear and week = vWeek
			
			GROUP BY family, itemFk
			
		) sub
		ORDER BY mermas DESC;
END$$
DELIMITER ;
