ALTER PROCEDURE [App].[Pr_ApplicationRequestDetailsAwaitingReceipts]
(
	@UserID BIGINT,
	@PaymentTypeIDs VARCHAR(20)
)
AS
BEGIN
			
	SELECT ARD.*,
			App.Fn_GetReceiptCost(Ard.ApplicationRequestDetailID, Ard.ApplicationRequestTypeID,Ard.IsRenewal,Ard.IsLtpsConversion,Ard.VehicleCount) -
			ISNULL((
				SELECT SUM(Rd.TotalCost)
				FROM App.ReceiptDetails AS Rd           
				WHERE Rd.ApplicationRequestDetailID = ARD.ApplicationRequestDetailID
					AND Rd.IsCancelled = 0
			),0) AS TotalCost
	FROM App.Vw_ApplicationRequestDetails AS ARD
		INNER JOIN Wf.WorkflowInstanceTasks AS WIT ON ARD.WorkflowInstanceID = WIT.WorkflowInstanceID
		INNER JOIN Wf.WorkflowTemplateSteps AS WTS ON WIT.WorkflowTemplateStepID = WTS.WorkflowTemplateStepID
	WHERE WTS.Step = 3 -- Step is 3
		AND WorkflowInstanceTaskStatusID = 1 -- In Progress
		AND WIT.AssignedToID = @UserID     
		AND ARD.PaymentTypeID IN (SELECT Value FROM System.Fn_GenSplitCsv(@PaymentTypeIDs,','))
END


