﻿USE [AIADatabaseV5]
GO
/****** Object:  StoredProcedure [dbo].[GetExportedImageDetails]    Script Date: 02/06/2018 10:49:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		ADAM 
-- Create date: 16 July 2013
-- Description:	Get details of exported image based on parameters
-- =============================================
CREATE PROCEDURE [dbo].[GetExportedImageDetails]
	@sStartDate varchar(20) = '', @sEndDate varchar(20) = '', @sAccoutNumber varchar(50)=''
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	SELECT   
			LID.LicenseId,
			LID.ExportedDate,
			LID.ImageName,
			L.AccountNumber,
			LID.OriginalFileName,
			LID.Title,
			LID.ModuleName,
			(SELECT TOP(1) LSD.NoofImages FROM LicenseSubscriptionDetail LSD WHERE LSD.LicenseId = LID.LicenseId order by LSD.SubscriptionValidFrom desc) as ExportLimit,
			USR.FirstName + ' '+ USR.LastName as UserName,
			(SELECT COUNT(LID1.Id) FROM LicenseImageExportDetail LID1 WHERE LID1.LicenseId = LID.LicenseId group by LID1.LicenseId) as imageCount
	FROM         
		 LicenseImageExportDetail LID 	
		 LEFT JOIN License L ON LID.LicenseId =L.Id
		 INNER JOIN  AIAUser USR ON LID.UserId = USR.Id	 
	WHERE
		 ((LEN(@sStartDate)=0) OR (LID.ExportedDate >= (CONVERT(DATETIME,@sStartDate)))) AND
		 ((LEN(@sEndDate)=0) OR (LID.ExportedDate <= (DATEADD(ms,-3,DATEADD(DAY,1,CONVERT(DATETIME,@sEndDate)))))) AND
		 ((LEN(@sAccoutNumber)=0) OR (AccountNumber LIKE '%'+@sAccoutNumber+'%'))
END
GO
