CREATE PROCEDURE dbo.OnlineGetPaymentTotal @AReferenceNumber INT, @AGuestNumber INT, @APaymentTotal MONEY OUTPUT AS
SET NOCOUNT ON
/*
  Created by Kent Johansen January 2008
  Returns: The total payment on a specific reference and guest for Cash, CreditCard, Currency, ReceivedDeposit

*/

SELECT @APaymentTotal = SUM(Transactions.PriceOverwrite * (CASE Transactions.Debit WHEN 1 THEN 1 ELSE -1 END)*
CASE WHEN Accounts.AccountType IN (4,5,6) THEN 1 ELSE 1+(VatValue/100) END)
FROM Accounts WITH (NOLOCK)
FULL OUTER JOIN Transactions WITH (NOLOCK) ON Accounts.AccountNumber = Transactions.AccountNumber
INNER JOIN FOLIOS WITH (NOLOCK) ON Transactions.FolioNumber = FOLIOS.FolioNumber AND Folios.ReferenceNumber = @AReferenceNumber AND Folios.GuestNumber = @AGuestNumber
WHERE Payment IN (3,4,6,12) --Cash, CreditCard, Currency, ReceivedDeposit
AND ShowOnBillFlag = 1