﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION [dbo].[fds__fn_requestsforinvoice]
(	
	
)
RETURNS TABLE 
AS
RETURN 
(
	SELECT 
		 [Id]
		 ,sreq.[ExternalId]
		 ,sreq.[Name]
		 ,sreq.[workdoneat]
		 ,sreq.[ReleasedAt]
		 ,sreq.[CustomerId]
	FROM [dbo].[mfr__servicerequests] as sreq 
		WHERE [state] not in ('Closed')
			AND [type] in ('IsProject', 'IsMaintenance')
			AND EXISTS (SELECT 0 FROM dbo.[mfr__*PartnerSet] as ps WHERE ps.[Property] = 'ServiceRequest:Tags' and ps.[PartnerId] = '16034758659' and sreq.[id] = ps.[EntityId])
			AND NOT EXISTS( SELECT 0 FROM [dbo].[fds__invoice_servicerequests] as isr join [dbo].[fds__invoices] as inv on isr.[InvId] = inv.[id] and isr.[mfr__servicerequest] = sreq.[id]) 
)