DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`salePersonEvolutionAdd`(IN vDateStart DATETIME)
BEGIN
	DELETE FROM bs.salePersonEvolution 
				WHERE  dated <=  DATE_SUB(util.VN_CURDATE(), INTERVAL 1 YEAR);
    
    
    INSERT INTO bs.salePersonEvolution (dated, amount, equalizationTax, salesPersonFk)
		SELECT fecha dated, 
				CAST(SUM(importe) AS DECIMAL(10,2) ) amount, 
				CAST(SUM(recargo) AS DECIMAL(10,2) ) equalizationTax ,
				IFNULL(salesPersonFk,0) salesPersonFk
			FROM bs.ventas v
				JOIN vn.client c ON v.Id_Cliente = c.id 
				JOIN vn.company co ON co.id = v.empresa_id
			WHERE co.code = "VNL" AND fecha >= vDateStart
		 GROUP BY v.fecha,c.salesPersonFk
		 ORDER BY salesPersonFk,dated ASC;    
END$$
DELIMITER ;
