Lets start over. Here is the whole query. I want to modify it to also show quarterly records that will be due in the future. Only the first ones upcoming. Right now it only shows the records that are due for the month given in the parameters. USE STDLDB;
DECLARE
@UserId INT = 1889,
@Recon BIT = 0,
@StateAbbrev VARCHAR(2) = '',
@StartDueDate DATE = '2023-11-01',
@EndDueDate DATE = '2023-11-30',
@ClientId INT = 608,
@PeriodIdsListStr VARCHAR(MAX) = '',
@STDLAdmin BIT = 0,
@CompanyAdmin BIT = 1,
@PractitionerId INT = 1384,
@Has100 BIT = 0,
@Has810 BIT = 0,
@Has809 BIT = 0,
@Has50 BIT = 0,
@Has51 BIT = 0,
@Has1150 BIT = 0;
DECLARE @EndDueDateMonth INT = MONTH(@EndDueDate);
DECLARE @PeriodIdsListLength INT = LEN(@PeriodIdsListStr);
WITH PeriodCodeDates AS (
SELECT
PC.Id,
PC.PeriodCode,
PC.FrequencyId,
PC.DueDateInt,
CONVERT(DATE,CONVERT(VARCHAR(10),PC.DueDateInt)) AS DueDate,
PC.StartPeriodInt,
CONVERT(DATE,CONVERT(VARCHAR(10),PC.StartPeriodInt)) AS StartPeriod,
PC.EndPeriodInt,
CONVERT(DATE,CONVERT(VARCHAR(10),PC.EndPeriodInt)) AS EndPeriod,
PC.DueDateDirectionalId,
PC.ProcessDateTime
FROM dbo.PeriodCode PC (nolock)
),
IncludedPeriods AS (
SELECT LTRIM(RTRIM(value)) AS PeriodId, 0 AS FilterSource FROM STRING_SPLIT(@PeriodIdsListStr, ',')
UNION
SELECT Id, 1 AS FilterSource FROM dbo.PeriodCode (nolock)
),
PeriodCodeTaxCalendar AS (
SELECT
PCD.Id AS PeriodCodeId,
PCD.PeriodCode,
PCD.FrequencyId,
PCD.DueDateInt,
PCD.DueDate,
PCD.StartPeriodInt,
PCD.StartPeriod,
PCD.EndPeriodInt,
PCD.EndPeriod,
PCD.ProcessDateTime,
TCS.Id AS TaxCalendarSetupId,
TCS.FormNumber,
TCS.FilingFrequencyId,
TCS.JurisdictionId,
TCS.Description,
TCS.ReturnTypeId,
TCS.Efile,
TCS.Deleted,
TCS.Eform,
TCS.OutputMappingSetId,
TCS.StateLevel,
TCS.CountyLevel,
TCS.CityLevel,
TCS.StateUrlNew,
TCS.StateUrlEFormNew,
CA.ClientId,
CA.AccountNumber,
CA.Id AS ClientAccountId,
CL.CompanyName,
ff.Description AS FilingFrequencyDescription,
rt.Description AS ReturnTypeDescription
FROM
dbo.TaxCalendarSetup TCS (nolock)
INNER JOIN
PeriodCodeDates PCD ON PCD.PeriodCode = tcs.PeriodCode
INNER JOIN
IncludedPeriods IP ON (@PeriodIdsListLength > 0 AND PCD.Id = IP.PeriodId AND IP.FilterSource = 0)
OR (@PeriodIdsListLength = 0 AND PCD.Id = IP.PeriodId AND IP.FilterSource = 1)
INNER JOIN
ClientAccount ca (nolock) ON TCS.FormNumber = ca.FormId
AND (ca.ReturnTypeId = tcs.ReturnTypeId OR ISNULL(ca.ReturnTypeId, '') = '')
AND TCS.FilingFrequencyId = ca.FilingFrequencyId
INNER JOIN
Client cl (nolock) ON cl.Id = ca.ClientId
INNER JOIN
FilingFrequency ff (nolock) ON ff.Id = TCS.FilingFrequencyId
AND ff.id = CA.FilingFrequencyId
INNER JOIN
ReturnType rt (nolock) ON rt.Id = TCS.ReturnTypeId
AND rt.id = CA.ReturnTypeId
WHERE
TCS.Deleted = 0
AND (ca.ClientId = @ClientId OR @ClientId = 0)
AND (cl.PractitionerId = @PractitionerId OR @STDLAdmin = 1)
AND (
(@STDLAdmin = 0 AND @CompanyAdmin = 0 AND EXISTS (SELECT UserID, ClientId FROM UserClient (nolock) WHERE cl.Id = ClientId AND UserId = @UserId))
OR @STDLAdmin = 1
OR @CompanyAdmin = 1
)
AND ((@Recon = 0 AND PCD.DueDate BETWEEN @StartDueDate AND @EndDueDate)
OR
(
TCS.FilingFrequencyId = 2 -- Assuming 2 is the ID for Quarterly
AND YEAR(PCD.DueDate) > YEAR(@EndDueDate) -- Check if the year of the due date is the same as @EndDueDate
AND MONTH(PCD.DueDate) > MONTH(@EndDueDate) -- Check if the month of the due date is the same as @EndDueDate
)
OR
(
@Recon = 1
AND PCD.DueDate >= @StartDueDate
AND
(
(
PCD.EndPeriod BETWEEN DATEADD(M, -1, @StartDueDate) AND @EndDueDate
AND tcs.FilingFrequencyId not in (1, 10, 17, 27, 28, 30, 2, 11, 6, 7, 12, 14, 31)
)
OR
(
PCD.EndPeriod <= DATEADD(M, -1, @EndDueDate)
and tcs.FilingFrequencyId = 12
)
OR
(
PCD.DueDate <= DATEADD(D, 15, @StartDueDate)
AND PCD.EndPeriod <= DATEADD(M, -1, @EndDueDate)
AND tcs.FilingFrequencyId = 14 and tcs.PeriodCode = '55'
)
OR
(
PCD.DueDate <= DATEADD(D, 15, @StartDueDate)
AND tcs.FilingFrequencyId = 14 and tcs.PeriodCode != '55'
)
OR
(
PCD.DueDate < DATEADD(YY, 1, @StartDueDate)
AND tcs.FilingFrequencyId = 6
)
OR
(
PCD.DueDate < DATEADD(M, 6, @StartDueDate)
AND tcs.FilingFrequencyId = 7
)
OR
(
PCD.DueDate < DATEADD(M, 2, @StartDueDate)
AND tcs.FilingFrequencyId = 31
)
OR
(
PCD.DueDate < DATEADD(M, 3, @StartDueDate)
AND tcs.FilingFrequencyId IN (2,11)
)
OR
(
PCD.DueDate <= @EndDueDate
AND tcs.FilingFrequencyId IN (1, 10, 17, 27, 28, 30)
)
)
)
)
),
IncludedForms AS (
SELECT
F.Id,
F.Name
FROM Form F (NOLOCK)
WHERE
( --New Jersey
(@Has50 = 1 AND @Has51 = 1 AND @EndDueDateMonth IN (1,4,7,10) AND F.Name != 'ST-51')
OR (@Has50 = 1 AND @Has51 = 1 AND @EndDueDateMonth NOT IN (1,4,7,10) AND F.Name != 'ST-50')
OR @Has50 = 0
OR @Has51 = 0
)
AND (--California
(@Has1150 = 1 AND @EndDueDateMonth IN (1,4,7,10) AND F.Name != 'BOE-1150')
OR (@Has1150 = 1 AND @EndDueDateMonth NOT IN (1,4,7,10) AND F.Name NOT LIKE 'BOE-401%')
OR @Has1150 = 0
)
AND (--New York
(@Has810 = 1 AND @Has809 = 1 AND @EndDueDateMonth IN (3,6,9,12) AND F.Name != 'ST-809')
OR (@Has810 = 1 AND @Has809 = 1 AND @EndDueDateMonth NOT IN (3,6,9,12) AND F.Name != 'ST-810')
OR @Has810 = 0
OR @Has809 = 0
)
AND (--New York
(@Has100 = 1 AND @Has809 = 1 AND @EndDueDateMonth IN (3,6,9,12) AND F.Name != 'ST-809')
OR (@Has100 = 1 AND @Has809 = 1 AND @EndDueDateMonth NOT IN (3,6,9,12) AND F.Name != 'ST-100')
OR @Has100 = 0
OR @Has809 = 0
)
)
SELECT DISTINCT
PCTC.PeriodCodeId AS Id,
PCTC.DueDateInt,
PCTC.StartPeriodInt,
PCTC.EndPeriodInt,
f.Name Form,
PCTC.ReturnTypeDescription ReturnType,
PCTC.FilingFrequencyDescription Fillingfreq,
PCTC.CompanyName CompanyName,
j.Description jurisdiction,
CONVERT(varchar(12), CAST(convert(char(8), PCTC.DueDateInt) AS date), 7) as FormattedDate,
CAST(PCTC.TaxCalendarSetupId AS VARCHAR) + '' + CAST(PCTC.ClientId AS VARCHAR) TaxCalendarIdClientId,
CAST(PCTC.TaxCalendarSetupId AS VARCHAR) + '' + CAST(PCTC.ClientId AS VARCHAR) + '' + CAST(PCTC.PeriodCodeId AS VARCHAR) + '' + [dbo].getFullJurisdictionNames(j.Id) TaxCalendarIdClientIdDueDatesId,
PCTC.StateUrlNew eFileUrl,
PCTC.StateUrlEFormNew eFormUrl,
PCTC.ClientId clientId,
PCTC.TaxCalendarSetupId AS TaxCalendarId,
[dbo].getFullJurisdictionNames(j.Id) fullJurisdiction,
ReturnTypeId,
[dbo].getFullJurisdictionReversedNames(j.Id) FullJurisdictionRevertedNames,
PCTC.AccountNumber,
j.JurisdictionTypeId,
j.Description
FROM
PeriodCodeTaxCalendar PCTC
INNER JOIN
ClientAccountJurisdiction caj WITH(NOLOCK)
ON caj.ClientAccountId = PCTC.ClientAccountId
INNER JOIN
IncludedForms f WITH(NOLOCK)
ON f.Id = PCTC.FormNumber
INNER JOIN
Jurisdiction j WITH(NOLOCK)
ON j.Id = caj.JurisdictionId
WHERE
1=1
AND (LEFT(j.LongDescription,2) = @StateAbbrev OR @StateAbbrev = '')
AND (
(
PCTC.StateLevel IS NOT NULL
AND EXISTS (SELECT
JurisdictionId,
Name,
UsesLocations,
HasDirectTax,
StateAbbrev,
IsExempt,
ByPassJurisdError,
StateId
FROM [State] (NOLOCK)
WHERE PCTC.StateLevel = [State].Name
AND [State].JurisdictionId = j.Id)
)
OR
(
PCTC.CountyLevel IS NOT NULL
AND j.Description = PCTC.CountyLevel
AND PCTC.JurisdictionId = j.ParentJurisdictionId
)
OR
(
PCTC.CityLevel IS NOT NULL
AND j.Description = PCTC.CityLevel
AND EXISTS (SELECT
Id,
Description,
ParentJurisdictionId,
JurisdictionTypeId,
LongDescription
FROM Jurisdiction WITH(nolock)
WHERE PCTC.JurisdictionId = Jurisdiction.ParentJurisdictionId
AND Jurisdiction.Id = j.ParentJurisdictionId)
)
)
ORDER BY
fullJurisdiction,
CompanyName,
Fillingfreq,
ReturnType,
Form;