
CREATE VIEW [dbo].[v_ReceiptListing] AS

SELECT	MAX(CONVERT(VARCHAR(10),A.dateOfService, 120)) [PaymentDateTime]
		,dbo.GetInitials(U.name+' '+U.surname) AS [PaymentReceiptingAuthorizedBy]		
		,'REC'+CONVERT(VARCHAR(2),RIGHT(dbo.GetFiscalYear(A.dateOfService),2))+REPLACE(STR(A.receiptNo, 4), SPACE(1), '0') AS [ReceiptNumber]
		,A.patientNumber AS [PatientNumber]
		,A.patientName+' '+A.patientSurname AS [Patient]
		,A.medAidName AS [MedicalAid]
		,A.procedureCode AS [PaymentMethod]
		,SUM(A.amount)*-1 AS [TotalReceiptedAmount]
		--,CONVERT(DECIMAL(18,2),(SUM((100 / (100 + A.vatRate)) * A.amount))) [Nett Amount]
		--,CONVERT(DECIMAL(18,2),SUM((A.amount) - ((100 / (100 + A.vatRate)) * A.amount))) [Vat Amount]
		,dbo.GetAmountPaidToAssistantFee(A.allocatedReference) AS [AmountPaidToAssistantFee]
FROM	pal_Account A
		JOIN [Framework_EVOL-1].dbo.pal_User U
			ON U.recId = A.userId
WHERE	A.isVisible = 1
		AND A.procedureType IN ('PM')
		--AND A.patientNumber = 1016
GROUP BY dbo.GetInitials(U.name+' '+U.surname)		
		,'REC'+CONVERT(VARCHAR(2),RIGHT(dbo.GetFiscalYear(A.dateOfService),2))+REPLACE(STR(A.receiptNo, 4), SPACE(1), '0')
		,A.patientNumber
		,A.patientName+' '+A.patientSurname
		,A.medAidName
		,A.procedureCode
		,dbo.GetAmountPaidToAssistantFee(A.allocatedReference)


GO


