CREATE PROCEDURE ProfileTransactions 
	@profileId int = 0,
	@startDate nvarchar(200) = null,
	@endDate nvarchar(200) = null
AS

BEGIN 

SET NOCOUNT ON;

 SELECT  
	   tx.Id 
	  ,tx.ProfileId 
	  ,tx.TransactionDate 
	  ,tx.Description 
	  ,tx.Reference 
	  ,txType.Description  AS  Type 
	  ,txStatus.Description AS StatusType 
	  ,tx.Amount 
	  ,feeType.Description as FeeTypeDescription 
	  ,feeType.Amount  AS  FeeAmount 
  FROM [Transaction]  tx
	 JOIN  TransactionType  txType
	ON tx.TransactionTypeId  = txType.Id
	
	 JOIN  TransactionStatus  txStatus
	ON tx.TransactionStatusId  = txStatus.Id

	 JOIN  TransactionFee  txFee
		ON tx.Id  = txFee.TransactionId 

		JOIN  FeeType  feeType
			ON txFee.FeeTypeId = feeType.Id

		WHERE ProfileId = @profileId
			AND ( (@startDate is null OR TransactionDate > @startDate) 
				AND ( @endDate is null OR TransactionDate <= @endDate))
	END