﻿
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION [dbo].[fds__getInvoiceTreeIds]
(	
	@invid varchar(20)
)
RETURNS TABLE 
AS
RETURN 
(
	with fds_srq as (
			SELECT distinct [type] = 'servicerequest', [id] = mfrs.[id]
				FROM [dbo].[fds__invoices] as _fi 
					JOIN [dbo].[fds__invoice_servicerequests] as _fs on _fi.Id = _fs.[InvId] and _fs.[mfr__servicerequest] is not null
					JOIN [dbo].[mfr__servicerequests] as mfrs on _fs.[mfr__servicerequest] = mfrs.id
				WHERE _fi.[Id] = @invid
	)
	SELECT * FROM fds_srq
)