[dbo].[GetVanLineStatementImportableDate]
CREATE FUNCTION [dbo].[GetVanLineStatementImportableDate](
@inVanlineGroupExternalCode varchar(10),
@inStatementYear int,
@inStatementNumber int,
@inVanLineStatementDefaultEndDate datetime )
returns datetime as
begin
declare @outStatementImportableDate datetime
declare @theVanLineStatementPeriodEndDate datetime = dbo.GetVanLineStatementPeriodEndDate(
@inVanlineGroupExternalCode, @inStatementYear, @inStatementNumber,
@inVanLineStatementDefaultEndDate )
if( @inVanlineGroupExternalCode = 'SIRVA' )
begin
declare @theCounter int = 2;
set @outStatementImportableDate = @theVanLineStatementPeriodEndDate
while( @theCounter > 0 )
begin
set @theCounter = @theCounter - 1
set @outStatementImportableDate = dateadd( day, 1, @outStatementImportableDate )
if( case when ( ( ( datepart( dw, @outStatementImportableDate ) - 1 ) + @@datefirst ) % 7 ) in ( 0, 6 ) then 1 else 0 end = 1 )
begin
set @theCounter = @theCounter + 1
end
end
set @outStatementImportableDate = dateadd( day, 1, @outStatementImportableDate )
end
return isnull( @outStatementImportableDate, dateadd( day, 1, @theVanLineStatementPeriodEndDate ) )
end
GO
GRANT EXECUTE ON [dbo].[GetVanLineStatementImportableDate] TO [MssExec]
GO