CREATE FUNCTION [dbo].[GetBalanceReceipt] (@FolioNumber INT, @PkInvoice INT) 
RETURNS @BalanceReceipt TABLE (Price MONEY, Tax MONEY, FkInvoice INT) AS
BEGIN
--NB20230620: Get rid of cursor (opt udf)
--nbl20161229: Created
--Notes: Calculates the Invoice balance and return price and Tax amounts (raw / not currency rate ajusted CR240813) 
DECLARE @Price MONEY, @Tax MONEY
SELECT @Price = SUM(CASE WHEN TSR.Tax = 0 THEN AmountRaw ELSE 0 END)
, @Tax = SUM(CASE WHEN TSR.Tax = 1 THEN AmountRaw ELSE 0 END) 
FROM [dbo].[GetTaxSpecReceipt] (@FolioNumber, @PkInvoice) TSR

INSERT INTO @BalanceReceipt (Price, Tax, FkInvoice) VALUES (@Price, @Tax, @PkInvoice)

  RETURN 
END 