DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceInTax_getFromDua`(vDuaFk INT)
BEGIN
/**
 * Borra los valores de duaTax y sus vctos. y los vuelve a crear en base a la tabla duaEntry
 *  
 * @param vDuaFk Id del dua a recalcular
 */
	DECLARE vDone BOOL DEFAULT FALSE;
	DECLARE vInvoiceInFk INT;

	DECLARE vInvoices CURSOR FOR
		SELECT DISTINCT invoiceInFk
			FROM entry e
				JOIN duaEntry de ON de.entryFk = e.id
			WHERE de.duaFk = vDuaFk;

	DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;

	OPEN vInvoices;
	l: LOOP
		SET vDone = FALSE;
		FETCH vInvoices INTO vInvoiceInFk;

		IF vDone THEN
			LEAVE l;
		END IF;

		CALL invoiceInTax_getFromEntries(vInvoiceInFk);
		CALL invoiceInDueDay_calculate(vInvoiceInFk);

	END LOOP;
	CLOSE vInvoices;
END$$
DELIMITER ;
