DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sale_getRoundingProblem`(
	vSelf INT
)
BEGIN
/**
 * Actualiza los problemas de redondeo para las líneas de venta
 *
 * @param vSelf Id de sale
 */
	DECLARE vItemFk INT;
	DECLARE vWarehouseFk INT;
	DECLARE vDated DATE;
	DECLARE vQuantity INT;

	SELECT s.itemFk, t.warehouseFk, DATE(t.shipped), s.quantity 
			INTO vItemFk, vWarehouseFk, vDated, vQuantity
		FROM sale s 
			JOIN ticket t ON t.id = s.ticketFk 
		WHERE s.id = vSelf;

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

	CALL sale_setProblem('hasRounding');

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