SET NOCOUNT ON
/********* Determine the beginning of the fiscal year ***************************/
DECLARE @otherlong INT
SELECT
@otherlong = ISNULL(otherlong , 1)
FROM
Medlists
WHERE
tablename = 'fiscalyear'
/******************************************************************************/
SELECT
'01' AS EntityCode,
RIGHT(YEAR(DATEADD(m , -( @otherlong - 1 ) , Visit)) , 2) AS [Fiscal Year],
MONTH(DATEADD(m , -( @otherlong - 1 ) , Visit)) AS [Fiscal Period],
'GL' AS [Subsystem],
'JRNENT' AS [Source Code],
LEFT(ISNULL(doc.Ledger , '') + SPACE(10) , 10) AS [Dept No],
LEFT(LEFT(ISNULL(fac.Ledger , '') , 4) + LEFT(ISNULL(fin.Ledger , '') , 4) + SPACE(10) , 10) AS [SubAccount No],
SUM(pvp.totalfee) AS [Amount],
'C' AS [Debit/Credit],
'Centricity Entry' + SPACE(34) AS [Description],
SPACE(6) AS [Proj Id],
SPACE(16) AS [Filler]
FROM
PatientVisit pv
INNER JOIN DoctorFacility comp ON pv.CompanyId = comp.DoctorFacilityId
INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId
INNER JOIN DoctorFacility doc ON pv.DoctorId = doc.DoctorFacilityId
LEFT OUTER JOIN Medlists fin ON pv.FinancialClassMId = fin.MedListsId
INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId
INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId
INNER JOIN Batch b ON pvp.BatchID = b.BatchID
INNER JOIN PatientVisitProcsAgg pvpa ON pvp.PatientVisitProcsID = pvpa.PatientVisitProcsID
LEFT OUTER JOIN Procedures p ON pvp.ProceduresID = p.ProceduresID
LEFT JOIN MedLists Procs ON p.DepartmentMId = procs.MedListsId
WHERE
b.entry >= ISNULL(NULL , '1/1/1900') AND
b.entry < DATEADD(DAY , 1 , ISNULL(NULL , '1/1/3000')) AND
--Filter on Facility
(
(
NULL IS NOT NULL AND
pv.FacilityID IN ( NULL )
) OR
( NULL IS NULL )
) AND
--Filter on Company
(
(
NULL IS NOT NULL AND
pv.CompanyID IN ( NULL )
) OR
( NULL IS NULL )
) AND
--Filter on Financial Class
(
(
NULL IS NOT NULL AND
pv.FinancialClassMID IN ( NULL )
) OR
( NULL IS NULL )
)
GROUP BY
pv.TicketNumber,
pv.Visit,
doc.Ledger,
fac.Ledger,
fin.Ledger
HAVING
SUM(pvp.TotalFee) <> 0
ORDER BY
pv.TicketNumber
|