UPDATE `bs`.nightTask t SET t.`procedure` = 'waste_addSales' WHERE t.id = 54;

DROP PROCEDURE IF EXISTS `bs`.`waste_Add`;

CREATE
    DEFINER = root@`%` PROCEDURE `bs`.`waste_addSales`()
BEGIN

	DECLARE vWeek INT;
    DECLARE vYear INT;
    
    SELECT week, year
		INTO vWeek, vYear
        FROM vn.time
        WHERE dated =  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 
			
		) sub
		ORDER BY mermas DESC;
        
END;


