DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`buy_getRoundingProblem`(
	vSelf INT
)
BEGIN
/**
 * Actualiza los problemas de redondeo para las líneas de venta relacionadas con un buy
 *
 * @param vSelf Id de ticket
 */
	DECLARE vWarehouseFk INT;
	DECLARE vDated DATE;

	SELECT warehouseFk, DATE(shipped) 
			INTO vWarehouseFk, vDated
		FROM ticket
		WHERE id = vSelf;

	CALL buyUltimate(vWarehouseFk, vDated);	
	
	CREATE OR REPLACE TEMPORARY TABLE tmp.ticket 
		SELECT s.id saleFk , MOD(s.quantity, b.`grouping`) hasProblem
			FROM ticket t
				JOIN sale s ON s.ticketFk = tl.ticketFk
				JOIN tmp.buyUltimate bu ON bu.itemFk = s.itemFk
				JOIN buy b ON b.id = bu.buyFk
			WHERE t.id = vSelf;

	CALL sale_setProblem('hasRounding');

	DROP TEMPORARY TABLE tmp.ticket;
END$$
DELIMITER ;