create table #work_SUTAReport (sessionid int, employeeid int, startdate datetime NULL, enddate datetime NULL,
socsecno varchar (11), Vrtxtaxid int, abbrev varchar (10), taxentityid int, thispdtaxablewages decimal(19,2),
sutataxablewages decimal(19,2), rate decimal(19,2), taxcap decimal(19,2), createtime datetime NULL, w2box1amt decimal(19,2),
priorytdtaxablewages decimal(19,2), sutawageexcess decimal(19,2), calctaxamount decimal(19,2), calctaxablewages decimal(19,2),
periodsworked int, sutataxamount decimal(19,2),)
IF @Official = 1 /*An 'Official' report, so we have to make sure the check dates are less than the period close date*/
BEGIN
insert into #work_SUTAReport
(
-- SessionID,
EmployeeID,
StartDate,
EndDate,
SocSecNo,
VrtxTaxID,
Abbrev,
TaxEntityID,
ThisPdTaxableWages,
SUTATaxableWages,
Rate,
TaxCap
)
select
-- @SessionID,
b.EmployeeID,
@StartDate,
@EndDate,
max(b.SocSecNo),
max(d.VrtxTaxID),
max(d.Abbrev),
c.TaxEntityID,
isnull(sum(isnull(c.Gross,0)),0),
isnull(sum(isnull(c.TaxableGross,0)),0),
max(isnull(e.Rate,0.0)),
max(isnull(e.TaxCap,0.0))
from prCheckHeader a (READCOMMITTED)
LEFT OUTER JOIN empEmployee b (READCOMMITTED)
ON b.EmployeeID = a.EmployeeID
LEFT OUTER JOIN prCheckTax c (READCOMMITTED)
LEFT OUTER JOIN TaxEntity d (READCOMMITTED)
LEFT OUTER JOIN sysTaxRegInfo e (READCOMMITTED)
ON e.TaxEntityID = d.TaxEntityID
ON d.TaxEntityID = c.TaxEntityID
ON c.CheckHistID = a.CheckHistID
where d.VrtxTaxID = 29
and a.CheckDate between @StartDate AND @EndDate
and a.EffDate <= @EndDate
and isnull(a.[1099Flag],0) <> 1
and isnull(c.ExemptFlag,0) = 0
--and isnull(c.Gross,0) <> 0
/* and a.CheckNumber IS NOT NULL*/
group by c.TaxEntityID,b.EmployeeID
order by c.TaxEntityID,b.EmployeeID
--update table, add number of pay periods worked for reporting period
UPDATE #work_SUTAReport
SET PeriodsWorked =
(select count(distinct a.PayPeriodID) from prCheckHeader a (READCOMMITTED)
where a.EmployeeID = #work_SUTAReport.EmployeeID
and a.CheckDate between @StartDate and @EndDate)
-- where #work_SUTAReport.SessionID = @SessionID
--update table, add amount which will appear in W2 Box 1
UPDATE #work_SUTAReport
SET W2Box1Amt =
isnull((select ISNULL(SUM(ISNULL(e.TaxableGross,0)),0) /*Taxable Wages*/
FROM prCheckHeader a (readcommitted)
LEFT OUTER JOIN prCheckTax e (readcommitted)
ON e.CheckHistID = a.CheckHistID
-- WHERE #work_SUTAReport.SessionID = @SessionID
AND a.EmployeeID = #work_SUTAReport.EmployeeID
AND e.TaxEntityID = 1
AND isnull(e.ExemptFlag,0) = 0
AND isnull(a.[1099Flag],0) <> 1
-- AND a.CheckPrinted = 1
-- AND a.ExpenseChk = 0
AND year(a.CheckDate) BETWEEN @StartDate AND @EndDate
AND a.EffDate <= @EndDate
GROUP BY a.EmployeeID),0)
--update table, add Tax Withheld Amount for reporting period
UPDATE #work_SUTAReport
SET SUTATaxAmount =
(
SELECT isnull(sum(isnull(c.TaxAmount,0)),0)
from prCheckHeader a (READCOMMITTED)
LEFT OUTER JOIN empEmployee b (READCOMMITTED)
ON b.EmployeeID = a.EmployeeID
LEFT OUTER JOIN prCheckTax c (READCOMMITTED)
ON c.CheckHistID = a.CheckHistID
-- where #work_SUTAReport.SessionID = @SessionID
and a.EmployeeID = #work_SUTAReport.EmployeeID
and c.TaxEntityID = #work_SUTAReport.TaxEntityID
AND isnull(c.ExemptFlag,0) = 0
AND isnull(a.[1099Flag],0) <> 1
-- AND a.CheckPrinted = 1
-- AND a.ExpenseChk = 0
and a.CheckDate between @StartDate and @EndDate
and a.EffDate <= @EndDate
)
--update table, add Taxable Gross Prior YTD (prior to reporting period, that is)
UPDATE #work_SUTAReport
SET PriorYTDTaxableWages =
(
SELECT isnull(sum(isnull(c.TaxableGross,0)),0)
from prCheckHeader a (READCOMMITTED)
LEFT OUTER JOIN empEmployee b (READCOMMITTED)
ON b.EmployeeID = a.EmployeeID
LEFT OUTER JOIN prCheckTax c (READCOMMITTED)
ON c.CheckHistID = a.CheckHistID
-- where #work_SUTAReport.SessionID = @SessionID
and a.EmployeeID = #work_SUTAReport.EmployeeID
and c.TaxEntityID = #work_SUTAReport.TaxEntityID
and a.CheckDate >= @YearStartDate
and a.CheckDate < @StartDate
and a.EffDate <= @EndDate
AND isnull(c.ExemptFlag,0) = 0
AND isnull(a.[1099Flag],0) <> 1
-- AND a.CheckPrinted = 1
-- AND a.ExpenseChk = 0
)
--update table
UPDATE #work_SUTAReport
SET ThisPdTaxableWages =
ISNULL((
SELECT
SUM( ISNULL( c1.gross, 0 ) )
FROM
prCheckHeader a1 (READCOMMITTED)
INNER JOIN prCheckTax c1 (READCOMMITTED)
ON c1.CheckHistID = a1.CheckHistID
WHERE
a1.EmployeeID = #work_SUTAReport.EmployeeID
AND
a1.CheckDate BETWEEN @StartDate AND @EndDate
AND
ISNULL( a1.[1099Flag], 0 ) = 0
AND
a1.ManualFlag = 0
AND
c1.TaxentityID = #work_SUTAReport.TaxEntityID
AND
NOT EXISTS ( SELECT * FROM prCheckPay WHERE CheckHistID = a1.CheckHistID )
),0)
+
/* (isnull((
SELECT isnull(sum(isnull(c1.GrossPay,0)),0)
from prCheckHeader a1 (READCOMMITTED)
LEFT OUTER JOIN prCheckPay c1 (READCOMMITTED)
LEFT OUTER JOIN sysPayCodes d1 (READCOMMITTED)
ON d1.PayCodeID = c1.PayCodeID
ON c1.CheckHistID = a1.CheckHistID
where a1.EmployeeID = #work_SUTAReport.EmployeeID
and a1.CheckDate between @StartDate and @EndDate
and a1.EffDate <= @EndDate
and isnull(a1.[1099Flag],0) <> 1
-- and a1.ExpenseChk = 0
and isnull(d1.ExpenseFlag,0) = 0
and a1.manualflag = 0
and exists (select * from prchecktax where checkhistid = a1.checkhistid and taxentityid = #work_SUTAReport.taxentityid and isnull(gross,0) <> 0)
),0)
*/
(isnull((
select sum(grosspayroll)
from vw_prchecksummary a1
inner join prcheckheader b1 on a1.checkhistid = b1.checkhistid
where a1.EmployeeID = #work_SUTAReport.EmployeeID
and a1.CheckDate between @StartDate and @EndDate
and a1.EffDate <= @EndDate
and isnull(b1.[1099Flag],0) <> 1
--and b1.manualflag = 0
and b1.SUIEntityID = #work_SUTAReport.taxentityid
),0)
+
isnull((
SELECT sum(isnull(c1.gross,0))
from prCheckHeader a1 (READCOMMITTED)
LEFT OUTER JOIN prCheckTax c1 (READCOMMITTED)
ON c1.CheckHistID = a1.CheckHistID
where a1.EmployeeID = #work_SUTAReport.EmployeeID
and a1.CheckDate between @StartDate and @EndDate
and a1.EffDate <= @EndDate
and isnull(a1.[1099Flag],0) <> 1
-- and a1.ExpenseChk = 0
and a1.manualflag = 1
and c1.taxentityid = #work_SUTAReport.taxentityid
),0)
- --subtract non-wage deductions from total
isnull((SELECT isnull(sum(isnull(a.ActualAmount,0)),0)
FROM prCheckDeduct a
LEFT OUTER JOIN prCheckHeader b
ON b.CheckHistID = a.CheckHistID
WHERE b.EmployeeID = #work_SUTAReport.EmployeeID
and b.CheckDate between @StartDate and @EndDate
and b.EffDate <= @EndDate
and isnull(b.[1099Flag],0) <> 1
and a.PreTaxStatus = 3 --Foreign Key to SSDedState, DedStateID, Non-wage deduction
and exists (select * from prchecktax where checkhistid = a.checkhistid and suientityid = #work_SUTAReport.taxentityid)
-- and exists (select * from prchecktax where checkhistid = a.checkhistid and taxentityid = #work_SUTAReport.taxentityid)
),0)
) -- where #work_SUTAReport.SessionID = @SessionID
--update table, calculate and insert suta wage excess
UPDATE #work_SUTAReport
SET SUTAWageExcess = ThisPdTaxableWages - SUTATaxableWages
/* case
when (PriorYTDTaxableWages + ThisPdTaxableWages) - TaxCap < 0 then 0
else (PriorYTDTaxableWages + ThisPdTaxableWages) - TaxCap
end*/
-- where #work_SUTAReport.SessionID = @SessionID
--update table, add Calced Tax Amount for reporting period
UPDATE #work_SUTAReport
SET CalcTaxAmount = SUTATaxableWages * (Rate/100)
-- where #work_SUTAReport.SessionID = @SessionID
--update table, add Calc Taxable Wages for reporting period
UPDATE #work_SUTAReport
SET CalcTaxableWages = ThisPdTaxableWages - SUTAWageExcess
-- where #work_SUTAReport.SessionID = @SessionID
END
ELSE /*Not an 'official' report, so we don't have to check the EffDates*/
BEGIN
insert into #work_SUTAReport
(
-- SessionID,
EmployeeID,
StartDate,
EndDate,
SocSecNo,
VrtxTaxID,
Abbrev,
TaxEntityID,
ThisPdTaxableWages,
SUTATaxableWages,
Rate,
TaxCap
)
select
-- @SessionID,
b.EmployeeID,
@StartDate,
@EndDate,
max(b.SocSecNo),
max(d.VrtxTaxID),
max(d.Abbrev),
c.TaxEntityID,
isnull(sum(isnull(c.Gross,0)),0),
isnull(sum(isnull(c.TaxableGross,0)),0),
max(isnull(e.Rate,0.0)),
max(isnull(e.TaxCap,0.0))
from prCheckHeader a (READCOMMITTED)
LEFT OUTER JOIN empEmployee b (READCOMMITTED)
ON b.EmployeeID = a.EmployeeID
LEFT OUTER JOIN prCheckTax c (READCOMMITTED)
LEFT OUTER JOIN TaxEntity d (READCOMMITTED)
LEFT OUTER JOIN sysTaxRegInfo e (READCOMMITTED)
ON e.TaxEntityID = d.TaxEntityID
ON d.TaxEntityID = c.TaxEntityID
ON c.CheckHistID = a.CheckHistID
where d.VrtxTaxID = 29
and a.CheckDate between @StartDate AND @EndDate
and isnull(a.[1099Flag],0) <> 1
and isnull(c.ExemptFlag,0) = 0
--and isnull(c.Gross,0) <> 0
/* and a.CheckNumber IS NOT NULL*/
group by c.TaxEntityID,b.EmployeeID
order by c.TaxEntityID,b.EmployeeID
--update table, add number of pay periods worked for reporting period
UPDATE #work_SUTAReport
SET PeriodsWorked =
(select count(distinct a.PayPeriodID) from prCheckHeader a (READCOMMITTED)
where a.EmployeeID = #work_SUTAReport.EmployeeID
and a.CheckDate between @StartDate and @EndDate)
-- where #work_SUTAReport.SessionID = @SessionID
--update table, add amount which will appear in W2 Box 1
UPDATE #work_SUTAReport
SET W2Box1Amt =
isnull((select ISNULL(SUM(ISNULL(e.TaxableGross,0)),0) /*Taxable Wages*/
FROM prCheckHeader a (readcommitted)
LEFT OUTER JOIN prCheckTax e (readcommitted)
ON e.CheckHistID = a.CheckHistID
-- WHERE #work_SUTAReport.SessionID = @SessionID
AND a.EmployeeID = #work_SUTAReport.EmployeeID
AND e.TaxEntityID = 1
AND isnull(e.ExemptFlag,0) = 0
-- AND a.CheckPrinted = 1
AND isnull(a.[1099Flag],0) <> 1
-- AND a.ExpenseChk = 0
AND year(a.CheckDate) BETWEEN @StartDate AND @EndDate
GROUP BY a.EmployeeID),0)
--update table, add Tax Withheld Amount for reporting period
UPDATE #work_SUTAReport
SET SUTATaxAmount =
(
SELECT isnull(sum(isnull(c.TaxAmount,0)),0)
from prCheckHeader a (READCOMMITTED)
LEFT OUTER JOIN empEmployee b (READCOMMITTED)
ON b.EmployeeID = a.EmployeeID
LEFT OUTER JOIN prCheckTax c (READCOMMITTED)
ON c.CheckHistID = a.CheckHistID
-- where #work_SUTAReport.SessionID = @SessionID
and a.EmployeeID = #work_SUTAReport.EmployeeID
and c.TaxEntityID = #work_SUTAReport.TaxEntityID
and isnull(c.ExemptFlag,0) = 0
and a.CheckDate between @StartDate and @EndDate
and isnull(a.[1099Flag],0) <> 1
)
--update table, add Taxable Gross Prior YTD (prior to reporting period, that is)
UPDATE #work_SUTAReport
SET PriorYTDTaxableWages =
(
SELECT isnull(sum(isnull(c.TaxableGross,0)),0)
from prCheckHeader a (READCOMMITTED)
LEFT OUTER JOIN empEmployee b (READCOMMITTED)
ON b.EmployeeID = a.EmployeeID
LEFT OUTER JOIN prCheckTax c (READCOMMITTED)
ON c.CheckHistID = a.CheckHistID
-- where #work_SUTAReport.SessionID = @SessionID
and a.EmployeeID = #work_SUTAReport.EmployeeID
and c.TaxEntityID = #work_SUTAReport.TaxEntityID
and isnull(c.ExemptFlag,0) = 0
and a.CheckDate >= @YearStartDate
and a.CheckDate < @StartDate
and isnull(a.[1099Flag],0) <> 1
)
--update table
UPDATE #work_SUTAReport
SET ThisPdTaxableWages =
ISNULL((
SELECT
SUM( ISNULL( c1.gross, 0 ) )
FROM
prCheckHeader a1 (READCOMMITTED)
INNER JOIN prCheckTax c1 (READCOMMITTED)
ON c1.CheckHistID = a1.CheckHistID
WHERE
a1.EmployeeID = #work_SUTAReport.EmployeeID
AND
a1.CheckDate BETWEEN @StartDate AND @EndDate
AND
ISNULL( a1.[1099Flag], 0 ) = 0
AND
a1.ManualFlag = 0
AND
c1.TaxentityID = #work_SUTAReport.TaxEntityID
AND
NOT EXISTS ( SELECT * FROM prCheckPay WHERE CheckHistID = a1.CheckHistID )
),0)
+
/*
(isnull((
SELECT isnull(sum(isnull(c1.GrossPay,0)),0)
from prCheckHeader a1 (READCOMMITTED)
LEFT OUTER JOIN prCheckPay c1 (READCOMMITTED)
LEFT OUTER JOIN sysPayCodes d1 (READCOMMITTED)
ON d1.PayCodeID = c1.PayCodeID
ON c1.CheckHistID = a1.CheckHistID
where a1.EmployeeID = #work_SUTAReport.EmployeeID
and a1.CheckDate between @StartDate and @EndDate
and isnull(a1.[1099Flag],0) <> 1
-- and a1.ExpenseChk = 0
and isnull(d1.ExpenseFlag,0) = 0
and a1.manualflag = 0
and exists (select * from prchecktax where checkhistid = a1.checkhistid and taxentityid = #work_SUTAReport.taxentityid and isnull(gross,0) <> 0)
),0)
*/
(isnull((
select sum(grosspayroll)
from vw_prchecksummary a1
inner join prcheckheader b1 on a1.checkhistid = b1.checkhistid
where a1.EmployeeID = #work_SUTAReport.EmployeeID
and a1.CheckDate between @StartDate and @EndDate
-- and a1.EffDate <= @EndDate
and isnull(b1.[1099Flag],0) <> 1
-- and b1.manualflag = 0
and b1.SUIEntityID = #work_SUTAReport.taxentityid
),0)
+
isnull((
SELECT sum(isnull(c1.gross,0))
from prCheckHeader a1 (READCOMMITTED)
LEFT OUTER JOIN prCheckTax c1 (READCOMMITTED)
ON c1.CheckHistID = a1.CheckHistID
where a1.EmployeeID = #work_SUTAReport.EmployeeID
and a1.CheckDate between @StartDate and @EndDate
and isnull(a1.[1099Flag],0) <> 1
-- and a1.ExpenseChk = 0
and a1.manualflag = 1
and c1.taxentityid = #work_SUTAReport.taxentityid
),0)
- --subtract non-wage deductions from total
isnull((SELECT isnull(sum(isnull(a.ActualAmount,0)),0)
FROM prCheckDeduct a
LEFT OUTER JOIN prCheckHeader b
ON b.CheckHistID = a.CheckHistID
WHERE b.EmployeeID = #work_SUTAReport.EmployeeID
and b.CheckDate between @StartDate and @EndDate
and isnull(b.[1099Flag],0) <> 1
and a.PreTaxStatus = 3 --Foreign Key to SSDedState, DedStateID, Non-wage deduction
and exists (select * from prchecktax where checkhistid = a.checkhistid and suientityid = #work_SUTAReport.taxentityid)
-- and exists (select * from prchecktax where checkhistid = a.checkhistid and taxentityid = #work_SUTAReport.taxentityid)
),0)
)
-- where #work_SUTAReport.SessionID = @SessionID
--update table, calculate and insert suta wage excess
UPDATE #work_SUTAReport
SET SUTAWageExcess = ThisPdTaxableWages - SUTATaxableWages
/* case
when (PriorYTDTaxableWages + ThisPdTaxableWages) - TaxCap < 0 then 0
else (PriorYTDTaxableWages + ThisPdTaxableWages) - TaxCap
end*/
--update table, add Calced Tax Amount for reporting period
UPDATE #work_SUTAReport
SET CalcTaxAmount = SUTATaxableWages * (Rate/100)
-- where #work_SUTAReport.SessionID = @SessionID
--update table, add Calc Taxable Wages for reporting period
UPDATE #work_SUTAReport
SET CalcTaxableWages = ThisPdTaxableWages - SUTAWageExcess
-- where #work_SUTAReport.SessionID = @SessionID
END
--now to get the data
SELECT
a.EmployeeID,
a.SocSecNo,
a.TaxEntityID,
a.VrtxTaxID,
a.Abbrev,
a.PeriodsWorked,
a.W2Box1Amt,
a.ThisPdTaxableWages,
a.PriorYTDTaxableWages,
a.TaxCap,
a.CalcTaxableWages,
a.SUTATaxableWages,
a.SUTAWageExcess,
a.CalcTaxAmount,
a.SUTATaxAmount,
a.Rate,
EmpName =
CASE
WHEN LEN(LTRIM(RTRIM(b.MiddleName))) > 0 THEN b.LastName + ', ' + b.FirstName + ' ' + SUBSTRING(b.MiddleName,1,1) + '.'
ELSE b.LastName + ', ' + b.FirstName --+ ' ' + SUBSTRING(b.MiddleName,1,1)
END
FROM #work_SUTAReport a (READCOMMITTED)
LEFT OUTER JOIN empEmployee b (READCOMMITTED)
ON b.EmployeeID = a.EmployeeID
-- WHERE
-- a.SessionID = @SessionID
ORDER BY
a.Abbrev,EmpName,a.SocSecNo
drop table #work_SUTAReport
Thanks a bunch!!
-T