DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`saleBuy_Add`(vSaleFk INT, vBuyFk INT)
BEGIN
	
	/* Añade un registro a la tabla saleBuy en el caso de que sea posible mantener la trazabilidad
	 * 
	 * @param vSaleFk clave primaria de vn.sale
	 * @param vBuyFk clave primaria de vn.buy
	 */
	/*IF (SELECT COUNT(*) 
			FROM vn.sale s
				JOIN vn.item i ON i.id = s.itemFk
				JOIN vn.itemType it ON it.id = i.typeFk 
				JOIN vn.itemCategory ic ON ic.id = it.categoryFk 
			WHERE s.id = vSaleFk
				AND ic.code = 'fruit') THEN */
	
	IF (SELECT COUNT(*) FROM vn.buy WHERE id = vBuyFk AND itemOriginalFk) THEN 
	
		INSERT INTO vn.saleBuy (saleFk, buyFk)
			VALUES(vSaleFk, vBuyFk);
		
	END IF;
	
END$$
DELIMITER ;
