From 29c3fe3dd16ba8fa1590a8509f96728612f018b7 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 17 Nov 2025 16:00:33 -0700 Subject: [PATCH 01/26] Update of Lease Fee queries to use CTEs for cleaner and easlier to read code --- .../onprc_billing/LeaseFeesRateswithCTE.sql | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 onprc_billing/resources/queries/onprc_billing/LeaseFeesRateswithCTE.sql diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeesRateswithCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeesRateswithCTE.sql new file mode 100644 index 000000000..6463dc5f5 --- /dev/null +++ b/onprc_billing/resources/queries/onprc_billing/LeaseFeesRateswithCTE.sql @@ -0,0 +1,23 @@ +WITH + BaseAssignments AS (...), + JoinedAssignments AS (...), + Metrics AS (...), + + ResourceRules AS (...), + ObeseRules AS (...), + TMBRules AS (...), + InfantRules AS (...), + CreditSourceRules AS (...), + ChargeIdRules AS (...), + QuantityRules AS (...), + + FinalLeaseFees AS (...), + FinalSetupFees AS (...), + FinalAdjustments AS (...) + +SELECT * FROM FinalLeaseFees +UNION ALL +SELECT * FROM FinalSetupFees +UNION ALL +SELECT * FROM FinalAdjustments +; From 15fbe3d19f9c874eb2ca7684d55c91f92b03e7d3 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 18 Nov 2025 09:14:26 -0700 Subject: [PATCH 02/26] Update of Lease Fee queries to use CTEs for cleaner and easlier to read code --- .../onprc_billing/LeaseFeeswithCTE.sql | 481 ++++++++++++++++++ 1 file changed, 481 insertions(+) create mode 100644 onprc_billing/resources/queries/onprc_billing/LeaseFeeswithCTE.sql diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeeswithCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeeswithCTE.sql new file mode 100644 index 000000000..93d435696 --- /dev/null +++ b/onprc_billing/resources/queries/onprc_billing/LeaseFeeswithCTE.sql @@ -0,0 +1,481 @@ +PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) + +WITH +-- ------------------------------------------------------------------ +-- Shared lookups / container paths +-- ------------------------------------------------------------------ +billing_charge_items AS ( + SELECT * + FROM Site.{substitutePath moduleProperty('ONPRC_Billing','BillingContainer')} + .onprc_billing.chargeableItems +), +public_charge_items AS ( + SELECT * + FROM onprc_billing_public.chargeableItems +), +births AS ( + SELECT * + FROM study.birth +), +resource_assignments AS ( + SELECT * + FROM Site.{substitutePath moduleProperty('EHR','EHRStudyContainer')} + .study.assignment +), +flags_4034 AS ( + SELECT f.* + FROM study.flags f + WHERE f.flag.code = 4034 +), +lease_defs AS ( + SELECT * + FROM onprc_billing.leaseFeeDefinition + WHERE active = true +), + +-- ------------------------------------------------------------------ +-- Base assignment sets (filter by QC + date ranges once) +-- ------------------------------------------------------------------ +assign_finalized AS ( + SELECT a.* + FROM study.assignment a + WHERE a.qcstate.publicdata = true + AND CAST(a.datefinalized AS DATE) >= CAST(STARTDATE AS DATE) + AND CAST(a.datefinalized AS DATE) <= CAST(ENDDATE AS DATE) +), +assign_end_finalized AS ( + SELECT a.* + FROM study.assignment a + WHERE a.qcstate.publicdata = true + AND a.enddatefinalized IS NOT NULL + AND CAST(a.enddatefinalized AS DATE) >= CAST(STARTDATE AS DATE) + AND CAST(a.enddatefinalized AS DATE) <= CAST(ENDDATE AS DATE) +), + +-- ------------------------------------------------------------------ +-- Standard lease fee rows +-- ------------------------------------------------------------------ +standard_rows AS ( + SELECT + a.id, + a.date, + a.project, + a.date AS assignmentStart, + a.enddate, + a.projectedReleaseCondition, + a.releaseCondition, + a.assignCondition, + a.releaseType, + a5.id AS ESPFAnimal, + a.ageAtTime.AgeAtTimeYearsRounded AS ageAtTime, + 'Lease Fees' AS category, + + -- chargeId selection + CASE + -- Determine if the animal is currently an Obese Animal / part of 0833 / etc. + -- U42 ESPF dual assignment + WHEN a5.id IS NOT NULL + THEN '5348' + + -- remove setup fee from Day Leases + WHEN ( + a3.id IS NOT NULL + AND (TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 AND a.enddate IS NULL) + ) + AND a.assignCondition = a.projectedreleasecondition + THEN ( + SELECT c.rowid + FROM public_charge_items c + WHERE c.itemCode = 'ONR01' + ) + + -- short term obese lease (0833 / 0622 etc., >14 and <90 days) + WHEN ( + a3.id IS NOT NULL + AND ( + TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 14 + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) < 90 + ) + AND a.assignCondition = a.projectedreleasecondition + AND a.enddate IS NULL + ) + THEN ( + SELECT c.rowid + FROM billing_charge_items c + WHERE c.itemCode = 'ONR24' + ) + + -- obese long term terminal assignment + WHEN ( + a3.id IS NOT NULL + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 90 + AND a.enddate IS NULL + AND a.projectedReleaseCondition = 206 + ) + THEN ( + SELECT c.rowid + FROM billing_charge_items c + WHERE c.itemCode = 'ONR45' + ) + + -- obese long term (>90 days) + WHEN ( + a3.id IS NOT NULL + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 90 + AND a.enddate IS NULL + ) + THEN ( + SELECT c.rowid + FROM billing_charge_items c + WHERE c.itemCode = 'ONR25' + ) + + -- Infant/Dam Day Lease + WHEN ( + a4.id IS NOT NULL + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 + AND a.endDate IS NULL + AND a.ageAtTime.AgeAtTimeYearsRounded < 1 + AND a.remark LIKE '%Diet%' + ) + THEN ( + SELECT c.rowid + FROM billing_charge_items c + WHERE c.itemCode = 'ONR40' + ) + + WHEN ( + a4.id IS NOT NULL + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 + AND a.endDate IS NULL + AND a.ageAtTime.AgeAtTimeYearsRounded < 1 + AND a.remark LIKE 'Control%' + ) + THEN ( + SELECT c.rowid + FROM billing_charge_items c + WHERE c.itemCode = 'ONR41' + ) + + WHEN ( + a4.id IS NOT NULL + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 + AND a.endDate IS NULL + AND a.ageAtTime.AgeAtTimeYearsRounded < 1 + ) + THEN ( + SELECT c.rowid + FROM billing_charge_items c + WHERE c.itemCode = 'ONR44' + ) + + -- one-day/short assignments + WHEN ( + a.duration <= CAST(javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_MAX_DURATION') AS INTEGER) + AND a.enddate IS NOT NULL + AND a.assignCondition = a.releaseCondition + ) + THEN ( + SELECT ci.rowid + FROM public_charge_items ci + WHERE ci.active = TRUE + AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_NAME') + ) + + -- (duplicate WHEN from original kept intentionally) + WHEN ( + a.duration <= CAST(javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_MAX_DURATION') AS INTEGER) + AND a.enddate IS NOT NULL + AND a.assignCondition = a.releaseCondition + ) + THEN ( + SELECT ci.rowid + FROM public_charge_items ci + WHERE ci.active = TRUE + AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_NAME') + ) + + -- TMB lease + WHEN a2.id IS NOT NULL + THEN ( + SELECT ci.rowid + FROM public_charge_items ci + WHERE (ci.startDate <= a.date AND ci.endDate >= a.date) + AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.TMB_LEASE_NAME') + ) + + ELSE lf.chargeId + END AS chargeId, + + -- quantity logic + CASE + WHEN a3.id IS NOT NULL + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 14 + THEN 1 + WHEN a4.id IS NOT NULL + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 14 + THEN 1 + WHEN a3.id IS NOT NULL + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 + THEN TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) + WHEN a4.id IS NOT NULL + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 + THEN TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) + WHEN a4.id IS NOT NULL + AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 14 + THEN TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) + + -- This looks for TMB infant and sets count to 0 based on mom being TMB + WHEN ( + SELECT COUNT(*) AS c + FROM births b + LEFT JOIN resource_assignments a1 + ON b.id = a.id AND a.date = b.dateOnly + LEFT JOIN resource_assignments a2 + ON b.dam = a2.id + AND a2.project = 559 + AND ( + (a2.date <= b.dateOnly AND a2.endDate >= b.dateOnly) + OR a2.enddate IS NULL + ) + WHERE b.id = a.id + AND a1.project.protocol != a2.project.protocol + ) > 0 + THEN 0 + + -- infants born to resource dams + WHEN ( + SELECT COUNT(*) AS c + FROM births b + LEFT JOIN resource_assignments a1 + ON b.id = a.id + AND a.date = b.dateOnly + AND a.project.use_category IN ('Center Resource', 'U42', 'U24') + LEFT JOIN resource_assignments a2 + ON b.dam = a2.id + AND a2.project.use_category IN ('Center Resource', 'U42', 'U24') + AND ( + (a2.date <= b.dateOnly AND a2.endDate >= b.dateOnly) + OR a2.enddate IS NULL + ) + WHERE b.id = a.id + AND a1.project.protocol = a2.project.protocol + ) > 0 + THEN 0 + + WHEN (a.duration = 0 AND a.enddate IS NOT NULL AND a.assignCondition = a.releaseCondition) + THEN 1 + + WHEN (fl.id IS NOT NULL) + THEN 0 + + WHEN ( + a.duration <= CAST(javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_MAX_DURATION') AS INTEGER) + AND a.enddate IS NOT NULL + AND a.assignCondition = a.releaseCondition + ) + THEN a.duration + + ELSE 1 + END AS quantity, + + CAST(NULL AS INTEGER) AS leaseCharge1, + CAST(NULL AS INTEGER) AS leaseCharge2, + a.objectid AS sourceRecord, + NULL AS chargeCategory, + NULL AS isAdjustment, + a.datefinalized, + a.enddatefinalized + FROM assign_finalized a + + -- find overlapping TMB at date of assignment + LEFT JOIN study.assignment a2 ON ( + a.id = a2.id + AND a.project != a2.project + AND a2.dateOnly <= a.dateOnly + AND a2.endDateCoalesced >= a.dateOnly + AND a2.project.name = javaConstant('org.labkey.onprc_ehr.ONPRC_EHRManager.TMB_PROJECT') + ) + + -- Obese 0833 animals id 1609 + LEFT JOIN onprc_billing.assignment_ObeseResource a3 ON ( + a.id = a3.id + AND a.project != a3.project + AND a3.project = 1609 + AND a3.dateonly <= a.dateOnly + AND a3.endDateCoalesced >= a.dateOnly + ) + + -- Obese 0622-01 animals id 1082 + LEFT JOIN onprc_billing.assignment_ObeseResource a4 ON ( + a.id = a4.id + AND a.project != a4.project + AND a4.project = 1082 + AND a4.dateonly <= a.dateOnly + AND a4.endDateCoalesced >= a.dateOnly + ) + + -- ESPF animals being dual assigned + LEFT JOIN assignment_U42ESPF a5 ON ( + a.id = a5.id + AND a.project != a5.project + AND a5.project = 1107 + AND a5.dateonly <= a.dateOnly + AND a5.endDateCoalesced >= a.dateOnly + ) + + -- lease fee definition + LEFT JOIN lease_defs lf ON ( + a3.id IS NULL + AND a4.id IS NULL + AND a5.id IS NULL + AND lf.assignCondition = a.assignCondition + AND lf.releaseCondition = a.projectedReleaseCondition + AND (a.ageAtTime.AgeAtTimeYearsRounded >= lf.minAge OR lf.minAge IS NULL) + AND (a.ageAtTime.AgeAtTimeYearsRounded < lf.maxAge OR lf.maxAge IS NULL) + ) + + -- research-owned animal exemption + LEFT JOIN flags_4034 fl ON ( + a.id = fl.id + AND a.date >= fl.date + AND a.date <= COALESCE(fl.enddate, NOW()) + ) +), + +-- ------------------------------------------------------------------ +-- Lease setup fee rows +-- ------------------------------------------------------------------ +setup_rows AS ( + SELECT + a.id, + a.date, + a.project, + a.date AS assignmentStart, + a.enddate, + a.projectedReleaseCondition, + a.releaseCondition, + a.assignCondition, + a.releaseType, + a.ageAtTime.AgeAtTimeYearsRounded AS ageAtTime, + ' ' AS ESPFAnimal, + 'Lease Setup Fees' AS category, + ( + SELECT ci.rowid + FROM public_charge_items ci + WHERE ci.active = TRUE + AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.LEASE_SETUP_FEES') + ) AS chargeId, + 1 AS quantity, + CAST(NULL AS INTEGER) AS leaseCharge1, + CAST(NULL AS INTEGER) AS leaseCharge2, + a.objectid AS sourceRecord, + NULL AS chargeCategory, + NULL AS isAdjustment, + a.datefinalized, + a.enddatefinalized + FROM assign_finalized a + WHERE + -- only charge setup fee for leases > 24H. + -- note: duration assumes today as end, so exclude null enddates + ( + a.duration > CAST(javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_MAX_DURATION') AS INTEGER) + OR (a.assignCondition != a.releaseCondition AND a.enddate IS NULL) + ) + AND a.id.demographics.species NOT IN ('Rabbit', 'Guinea Pigs') +), + +-- ------------------------------------------------------------------ +-- Adjustment rows for released animals +-- ------------------------------------------------------------------ +adjustment_rows AS ( + SELECT + a.id, + CASE + WHEN a.enddate < a.dateFinalized THEN a.dateFinalized + ELSE a.enddate + END AS date, + a.project, + a.date AS assignmentStart, + a.enddate, + a.projectedReleaseCondition, + a.releaseCondition, + a.assignCondition, + a.releaseType, + a.ageAtTime.AgeAtTimeYearsRounded AS ageAtTime, + a5.id AS ESPFAnimal, + 'Lease Fees' AS category, + ( + SELECT MAX(ci.rowid) AS rowid + FROM public_charge_items ci + WHERE ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.LEASE_FEE_ADJUSTMENT') + AND ci.active = TRUE + ) AS chargeId, + CASE + WHEN (fl.id IS NOT NULL) THEN 0 + ELSE 1 + END AS quantity, + lf2.chargeId AS leaseCharge1, + lf.chargeId AS leaseCharge2, + a.objectid AS sourceRecord, + 'Adjustment - Automatic' AS chargeCategory, + 'Y' AS isAdjustment, + a.datefinalized, + a.enddatefinalized + FROM assign_end_finalized a + + LEFT JOIN onprc_billing.leaseFeeDefinition lf ON ( + lf.assignCondition = a.assignCondition + AND lf.releaseCondition = a.releaseCondition + AND (a.ageAtTime.AgeAtTimeYearsRounded >= lf.minAge OR lf.minAge IS NULL) + AND (a.ageAtTime.AgeAtTimeYearsRounded < lf.maxAge OR lf.maxAge IS NULL) + ) + + LEFT JOIN onprc_billing.leaseFeeDefinition lf2 ON ( + lf2.assignCondition = a.assignCondition + AND lf2.releaseCondition = a.projectedReleaseCondition + AND (a.ageAtTime.AgeAtTimeYearsRounded >= lf2.minAge OR lf2.minAge IS NULL) + AND (a.ageAtTime.AgeAtTimeYearsRounded < lf2.maxAge OR lf2.maxAge IS NULL) + AND (a.date >= lf2.startDate AND a.date <= lf2.endDate) + ) + + -- find overlapping TMB at date of assignment + LEFT JOIN study.assignment a2 ON ( + a.id = a2.id + AND a.project != a2.project + AND a2.dateOnly <= a.dateOnly + AND a2.endDateCoalesced >= a.dateOnly + AND a2.project.name = javaConstant('org.labkey.onprc_ehr.ONPRC_EHRManager.TMB_PROJECT') + ) + + LEFT JOIN assignment_U42ESPF a5 ON ( + a.id = a5.id + AND a.project != a5.project + AND a5.project = 1107 + AND a5.dateonly <= a.dateOnly + AND a5.endDateCoalesced >= a.dateOnly + ) + + -- research-owned animal exemption + LEFT JOIN flags_4034 fl ON ( + a.id = fl.id + AND a.date >= fl.date + AND a.date <= COALESCE(fl.enddate, NOW()) + ) + + WHERE + a.releaseCondition != a.projectedReleaseCondition + AND (a.id != a5.id OR a5.id IS NULL) + AND lf.active = TRUE + AND a2.id IS NULL + AND a.participantID NOT LIKE '[a-z]%' +) + +-- ------------------------------------------------------------------ +-- Final unified result +-- ------------------------------------------------------------------ +SELECT * FROM standard_rows +UNION ALL +SELECT * FROM setup_rows +UNION ALL +SELECT * FROM adjustment_rows; From f8b0a3d1204e3e32fbef0e851236b9257c03d1e2 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Wed, 19 Nov 2025 09:53:28 -0700 Subject: [PATCH 03/26] Update of Lease Fee queries to use CTEs for cleaner and easlier to read code --- .../resources/queries/onprc_billing/NonExemptAssignments.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 onprc_billing/resources/queries/onprc_billing/NonExemptAssignments.sql diff --git a/onprc_billing/resources/queries/onprc_billing/NonExemptAssignments.sql b/onprc_billing/resources/queries/onprc_billing/NonExemptAssignments.sql new file mode 100644 index 000000000..239396462 --- /dev/null +++ b/onprc_billing/resources/queries/onprc_billing/NonExemptAssignments.sql @@ -0,0 +1,3 @@ +SELECT * +FROM study.AssignmentsInRange a +WHERE project NOT IN ('0300','0456') -- resource exemptions (TMB/Aging) From 861d2934dc401209829e12e26673c83ddd13ba38 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sat, 22 Nov 2025 06:47:53 -0700 Subject: [PATCH 04/26] This is the working copy of a new Lease Fee query using CTEs based on a revied workfrom from Finance --- .../queries/onprc_ehr/gdj_NewLeaseFeeCTE.sql | 341 ++++++++++++++++++ 1 file changed, 341 insertions(+) create mode 100644 onprc_ehr/resources/queries/onprc_ehr/gdj_NewLeaseFeeCTE.sql diff --git a/onprc_ehr/resources/queries/onprc_ehr/gdj_NewLeaseFeeCTE.sql b/onprc_ehr/resources/queries/onprc_ehr/gdj_NewLeaseFeeCTE.sql new file mode 100644 index 000000000..a6359f36f --- /dev/null +++ b/onprc_ehr/resources/queries/onprc_ehr/gdj_NewLeaseFeeCTE.sql @@ -0,0 +1,341 @@ +PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP +-- ======================================================== +-- This is the QUery using CTES that was AI Generated for revised lease fees +-- Date: 2025-11-22 +-- Step 1 Assignments has bene modified to use LabkeySQL structure. +-- ========================================================= +WITH +-- ========================================================= +-- 1) Base assignments in date range +--This was modified to use LabkeySQL structure. +-- ========================================================= +assignments AS ( + SELECT + a.Id, + -- create an identifer as assignment ID + a.lsid as assignmentId, + a.date AS assignmentDate, + a.enddate AS assignmentEndDate, + a.project, + --Create a sub query for resource ID + (Select r.project from study.resourceAssigned r where r.id = a.id) as resourceCode, + a.assignCondition, + a.releaseCondition, + a.projectedReleaseDate, + --Create a looklup for is a research project + Case when a.id in (Select ra.id from study.researchAssigned ra where ra.id = a.id) then 1 + Else 0 + End as isResearchAssignment, + -- a.isResearchAssignment, -- 1 = research, 0 = resource (or derive later) + (Select b.dam from study.birth b where b.id = a.id) as damId, -- dam of infant, if applicable + a.sex, + -- Using the release code assign a value for is terminal + Case when a.projectedReleaseCondition in (206,207) then 1 + Else 0 + End as isTerminalAssignment, -- 1 = terminal, 0 = non-terminal + a.container + FROM study.assignment a + WHERE a.date >= StartDate + AND a.date < EndDate +) +-- Select * from assignments + , + +-- ========================================================= +-- 2) Animal age / infant vs adult +-- (You may already have an ageAtTime dataset; this is illustrative) +-- change this to age in years +-- ========================================================= +age_at_assignment AS ( + SELECT + aa.Id, + aa.assignmentId, + aa.assignmentDate, + aa.project, + aa.resourceCode, + aa.assignCondition, + aa.releaseCondition, + aa.proposedReleaseDate, + aa.damId, + aa.sex, + aa.isTerminalAssignment, + aa.isResearchAssignment, + aa.container, + d.birth, + -- change this to accepted Labkey Code + TIMESTAMPDIFF('SQL_TSI_day', d.birth, aa.assignmentDate) AS ageDays, + CASE + WHEN TIMESTAMPDIFF('SQL_TSI_YEAR', d.birth, aa.assignmentDate) <= 1 THEN 1 -- infant threshold example + ELSE 0 + END AS isInfant + FROM assignments aa + LEFT JOIN study.demographics d + ON d.Id = aa.Id +), + +-- ========================================================= +-- 3) PI purchase flag (for research assignments) +-- ========================================================= +pi_purchase AS ( + SELECT DISTINCT + f.Id, + 1 AS hasPIPurchase + FROM study.flags f + WHERE f.value = 'PI Purchased NHP' + --AND f.isActive = true +), + +assign_with_pi AS ( + SELECT + a.*, + COALESCE(p.hasPIPurchase, 0) AS hasPIPurchase + FROM age_at_assignment a + LEFT JOIN pi_purchase p + ON a.Id = p.Id +), + +-- ========================================================= +-- 4) Assignment length & condition change +-- ========================================================= +assignment_length AS ( + SELECT + a.*, + DATEDIFF('day', a.assignmentDate, COALESCE(a.proposedReleaseDate, a.assignmentEndDate)) AS assignmentDays, + CASE + WHEN a.assignCondition = a.releaseCondition + OR a.releaseCondition IS NULL + THEN 0 + ELSE 1 + END AS hasConditionChange + FROM assign_with_pi a +), + +-- ========================================================= +-- 5) Resource type classification +-- ========================================================= +resource_type AS ( + SELECT + a.*, + CASE + WHEN a.resourceCode = '0300' THEN 'TMB' + WHEN a.resourceCode = '0456' THEN 'AGING' + WHEN a.resourceCode = '0833' THEN 'OBESE' + WHEN a.resourceCode = '0492-03' THEN 'SPF9' + WHEN a.resourceCode = '1092-50' THEN 'AMR' + WHEN a.resourceCode = '0492' THEN 'COLONY' + WHEN a.resourceCode = '0492-02' THEN 'U42' + WHEN a.resourceCode = '0492-45' THEN 'JMR' + ELSE 'OTHER' + END AS resourceGroup + FROM assignment_length a +), + +-- ========================================================= +-- 6) Dam/resource match (for infant/resource rules) +-- You may need a proper housing/resource history join here. +-- ========================================================= +dam_resource AS ( + SELECT + r.Id, + r.assignmentId, + r.resourceCode AS damResourceCode + FROM resource_type r + WHERE r.damId IS NOT NULL + -- join to housing/resource assignments if needed +), + +with_dam_match AS ( + SELECT + a.*, + CASE + WHEN a.isInfant = 1 + AND dr.damResourceCode = a.resourceCode + THEN 1 + ELSE 0 + END AS infantSameDamResource + FROM resource_type a + LEFT JOIN dam_resource dr + ON dr.Id = a.damId + AND dr.assignmentId = a.assignmentId -- or appropriate key +), + +-- ========================================================= +-- 7) Determine lease type (core rule engine) +-- ========================================================= +lease_type AS ( + SELECT + a.*, + + -- Main lease type decision + CASE + -- ------------------------------ + -- 1) Resource assignments + -- ------------------------------ + WHEN a.isResearchAssignment = 0 THEN + CASE + -- 1.1 Infant, dam in same resource → no lease + WHEN a.isInfant = 1 + AND a.infantSameDamResource = 1 + THEN 'NONE' + + -- 1.2 TMB (0300) + WHEN a.resourceGroup = 'TMB' THEN + CASE + -- TMB dam always charged a TMB lease + WHEN a.isInfant = 0 + AND a.sex = 'F' -- example for dam logic + THEN 'TMB_LEASE' + + -- infant from assigned TMB dam → no lease + WHEN a.isInfant = 1 + AND a.infantSameDamResource = 1 + THEN 'NONE' + + -- infant from unassigned TMB dam → P51 lease, credit TMB + WHEN a.isInfant = 1 + THEN 'P51_FULL' + + -- male assigned from TMB resource → P51 rate + WHEN a.sex = 'M' + THEN 'P51_FULL' + + -- day lease to TMB + ELSE 'DAY_LEASE' + END + + -- 1.3 Aging (0456) → no lease for assignments + WHEN a.resourceGroup = 'AGING' THEN 'NONE' + + -- 1.4 OBESE (0833) + WHEN a.resourceGroup = 'OBESE' THEN + CASE + WHEN a.assignmentDays BETWEEN 1 AND 14 + AND a.hasConditionChange = 0 + THEN 'OBESE_DAY' -- ONR01 + WHEN a.isTerminalAssignment = 1 + THEN 'OBESE_ADULT_TERM' -- ONR45 + ELSE 'OBESE_ADULT' -- ONR25 + END + + -- 1.5 SPF9 (0492-03) + WHEN a.resourceGroup = 'SPF9' + THEN 'SPF9_EXPANDED' + + -- 1.6 AMR (1092-50) – no lease when assigned to AMR + WHEN a.resourceGroup = 'AMR' + THEN 'NONE' + + -- 1.7 Colony / U42 / JMR / Other resource → P51 rules + ELSE 'P51_FULL' + END + + -- ------------------------------ + -- 2) Research assignments + -- ------------------------------ + ELSE + CASE + -- 2.1 PI Purchased → no lease + WHEN a.hasPIPurchase = 1 + THEN 'NONE' + + -- 2.2 One day – 14 days + WHEN a.assignmentDays BETWEEN 1 AND 14 THEN + CASE + WHEN a.hasConditionChange = 0 + THEN 'DAY_LEASE' + ELSE 'FULL_LEASE' + END + + -- 2.3 > 14 days → full lease + WHEN a.assignmentDays > 14 + THEN 'FULL_LEASE' + + ELSE 'NONE' + END + END AS leaseType + FROM with_dam_match a +), + +-- ========================================================= +-- 8) Map leaseType to itemCodes & credit aliases +-- ========================================================= +lease_mapping AS ( + SELECT + l.*, + + -- Item code mapping (example codes – replace with real ones) + CASE l.leaseType + WHEN 'DAY_LEASE' THEN 'DAY01' -- generic day lease + WHEN 'FULL_LEASE' THEN 'FULL01' + WHEN 'P51_FULL' THEN 'P51' + WHEN 'OBESE_DAY' THEN 'ONR01' + WHEN 'OBESE_ADULT' THEN 'ONR25' + WHEN 'OBESE_ADULT_TERM' THEN 'ONR45' + WHEN 'SPF9_EXPANDED' THEN 'SPF9X' + WHEN 'TMB_LEASE' THEN 'TMB01' + ELSE NULL + END AS leaseItemCode, + + -- Credit resource mapping + CASE + WHEN l.leaseType = 'NONE' THEN NULL + + -- Aging never gets credit; credit colony + WHEN l.resourceGroup = 'AGING' THEN 'COLONY_ALIAS' + + -- Obese always credits Obese resource + WHEN l.resourceGroup = 'OBESE' THEN 'OBESE_ALIAS' + + -- SPF9 credits U42e funding + WHEN l.resourceGroup = 'SPF9' THEN 'U42E_ALIAS' + + -- TMB credits TMB resource + WHEN l.resourceGroup = 'TMB' THEN 'TMB_ALIAS' + + -- AMR → credit colony or original resource + WHEN l.resourceGroup = 'AMR' THEN 'COLONY_ALIAS' + + -- Colony/U42/JMR map to their own aliases + WHEN l.resourceGroup = 'COLONY' THEN 'COLONY_ALIAS' + WHEN l.resourceGroup = 'U42' THEN 'U42_ALIAS' + WHEN l.resourceGroup = 'JMR' THEN 'JMR_ALIAS' + + -- fallback: credit the originating resource + ELSE 'ORIGIN_RESOURCE_ALIAS' + END AS creditAlias + FROM lease_type l +) +Select * from lease_mapping +/*, +-- ========================================================= +-- 9) Final output +-- ========================================================= +final AS ( + SELECT + f.Id, + f.assignmentId, + f.assignmentDate, + f.assignmentEndDate, + f.project, + f.resourceCode, + f.resourceGroup, + f.isResearchAssignment, + f.isInfant, + f.assignmentDays, + f.hasConditionChange, + f.hasPIPurchase, + f.isTerminalAssignment, + f.leaseType, + f.leaseItemCode, + f.creditAlias, + CASE + WHEN f.leaseType = 'NONE' + THEN 'No lease per business rules' + ELSE 'Lease generated per leaseType=' + f.leaseType + END AS leaseNote + FROM lease_mapping f +) + +SELECT * +FROM final +ORDER BY assignmentDate, Id, assignmentId; From 9094ee5d23ad568bf39f4e6f44769a8f7f95a737 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sat, 22 Nov 2025 06:51:38 -0700 Subject: [PATCH 05/26] This is the working copy of a new Lease Fee query using CTEs based on a revied workfrom from Finance --- .../resources/queries/onprc_billing}/gdj_NewLeaseFeeCTE.sql | 1 + 1 file changed, 1 insertion(+) rename {onprc_ehr/resources/queries/onprc_ehr => onprc_billing/resources/queries/onprc_billing}/gdj_NewLeaseFeeCTE.sql (99%) diff --git a/onprc_ehr/resources/queries/onprc_ehr/gdj_NewLeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql similarity index 99% rename from onprc_ehr/resources/queries/onprc_ehr/gdj_NewLeaseFeeCTE.sql rename to onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql index a6359f36f..9060f21af 100644 --- a/onprc_ehr/resources/queries/onprc_ehr/gdj_NewLeaseFeeCTE.sql +++ b/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql @@ -3,6 +3,7 @@ PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP -- This is the QUery using CTES that was AI Generated for revised lease fees -- Date: 2025-11-22 -- Step 1 Assignments has bene modified to use LabkeySQL structure. +-- THis was moved to the onprc_billing query section -- ========================================================= WITH -- ========================================================= From f4db58980bfdaa8ab339ee689e869ea94740e5e3 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 24 Nov 2025 09:15:55 -0700 Subject: [PATCH 06/26] This is the working copy of a new Lease Fee query using CTEs based on a revied workfrom from Finance --- .../onprc_billing/LeaseFeesRateswithCTE.sql | 23 - .../onprc_billing/LeaseFeeswithCTE.sql | 481 ------------------ .../onprc_billing/gdj_NewLeaseFeeCTE.sql | 23 +- 3 files changed, 14 insertions(+), 513 deletions(-) delete mode 100644 onprc_billing/resources/queries/onprc_billing/LeaseFeesRateswithCTE.sql delete mode 100644 onprc_billing/resources/queries/onprc_billing/LeaseFeeswithCTE.sql diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeesRateswithCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeesRateswithCTE.sql deleted file mode 100644 index 6463dc5f5..000000000 --- a/onprc_billing/resources/queries/onprc_billing/LeaseFeesRateswithCTE.sql +++ /dev/null @@ -1,23 +0,0 @@ -WITH - BaseAssignments AS (...), - JoinedAssignments AS (...), - Metrics AS (...), - - ResourceRules AS (...), - ObeseRules AS (...), - TMBRules AS (...), - InfantRules AS (...), - CreditSourceRules AS (...), - ChargeIdRules AS (...), - QuantityRules AS (...), - - FinalLeaseFees AS (...), - FinalSetupFees AS (...), - FinalAdjustments AS (...) - -SELECT * FROM FinalLeaseFees -UNION ALL -SELECT * FROM FinalSetupFees -UNION ALL -SELECT * FROM FinalAdjustments -; diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeeswithCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeeswithCTE.sql deleted file mode 100644 index 93d435696..000000000 --- a/onprc_billing/resources/queries/onprc_billing/LeaseFeeswithCTE.sql +++ /dev/null @@ -1,481 +0,0 @@ -PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) - -WITH --- ------------------------------------------------------------------ --- Shared lookups / container paths --- ------------------------------------------------------------------ -billing_charge_items AS ( - SELECT * - FROM Site.{substitutePath moduleProperty('ONPRC_Billing','BillingContainer')} - .onprc_billing.chargeableItems -), -public_charge_items AS ( - SELECT * - FROM onprc_billing_public.chargeableItems -), -births AS ( - SELECT * - FROM study.birth -), -resource_assignments AS ( - SELECT * - FROM Site.{substitutePath moduleProperty('EHR','EHRStudyContainer')} - .study.assignment -), -flags_4034 AS ( - SELECT f.* - FROM study.flags f - WHERE f.flag.code = 4034 -), -lease_defs AS ( - SELECT * - FROM onprc_billing.leaseFeeDefinition - WHERE active = true -), - --- ------------------------------------------------------------------ --- Base assignment sets (filter by QC + date ranges once) --- ------------------------------------------------------------------ -assign_finalized AS ( - SELECT a.* - FROM study.assignment a - WHERE a.qcstate.publicdata = true - AND CAST(a.datefinalized AS DATE) >= CAST(STARTDATE AS DATE) - AND CAST(a.datefinalized AS DATE) <= CAST(ENDDATE AS DATE) -), -assign_end_finalized AS ( - SELECT a.* - FROM study.assignment a - WHERE a.qcstate.publicdata = true - AND a.enddatefinalized IS NOT NULL - AND CAST(a.enddatefinalized AS DATE) >= CAST(STARTDATE AS DATE) - AND CAST(a.enddatefinalized AS DATE) <= CAST(ENDDATE AS DATE) -), - --- ------------------------------------------------------------------ --- Standard lease fee rows --- ------------------------------------------------------------------ -standard_rows AS ( - SELECT - a.id, - a.date, - a.project, - a.date AS assignmentStart, - a.enddate, - a.projectedReleaseCondition, - a.releaseCondition, - a.assignCondition, - a.releaseType, - a5.id AS ESPFAnimal, - a.ageAtTime.AgeAtTimeYearsRounded AS ageAtTime, - 'Lease Fees' AS category, - - -- chargeId selection - CASE - -- Determine if the animal is currently an Obese Animal / part of 0833 / etc. - -- U42 ESPF dual assignment - WHEN a5.id IS NOT NULL - THEN '5348' - - -- remove setup fee from Day Leases - WHEN ( - a3.id IS NOT NULL - AND (TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 AND a.enddate IS NULL) - ) - AND a.assignCondition = a.projectedreleasecondition - THEN ( - SELECT c.rowid - FROM public_charge_items c - WHERE c.itemCode = 'ONR01' - ) - - -- short term obese lease (0833 / 0622 etc., >14 and <90 days) - WHEN ( - a3.id IS NOT NULL - AND ( - TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 14 - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) < 90 - ) - AND a.assignCondition = a.projectedreleasecondition - AND a.enddate IS NULL - ) - THEN ( - SELECT c.rowid - FROM billing_charge_items c - WHERE c.itemCode = 'ONR24' - ) - - -- obese long term terminal assignment - WHEN ( - a3.id IS NOT NULL - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 90 - AND a.enddate IS NULL - AND a.projectedReleaseCondition = 206 - ) - THEN ( - SELECT c.rowid - FROM billing_charge_items c - WHERE c.itemCode = 'ONR45' - ) - - -- obese long term (>90 days) - WHEN ( - a3.id IS NOT NULL - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 90 - AND a.enddate IS NULL - ) - THEN ( - SELECT c.rowid - FROM billing_charge_items c - WHERE c.itemCode = 'ONR25' - ) - - -- Infant/Dam Day Lease - WHEN ( - a4.id IS NOT NULL - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 - AND a.endDate IS NULL - AND a.ageAtTime.AgeAtTimeYearsRounded < 1 - AND a.remark LIKE '%Diet%' - ) - THEN ( - SELECT c.rowid - FROM billing_charge_items c - WHERE c.itemCode = 'ONR40' - ) - - WHEN ( - a4.id IS NOT NULL - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 - AND a.endDate IS NULL - AND a.ageAtTime.AgeAtTimeYearsRounded < 1 - AND a.remark LIKE 'Control%' - ) - THEN ( - SELECT c.rowid - FROM billing_charge_items c - WHERE c.itemCode = 'ONR41' - ) - - WHEN ( - a4.id IS NOT NULL - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 - AND a.endDate IS NULL - AND a.ageAtTime.AgeAtTimeYearsRounded < 1 - ) - THEN ( - SELECT c.rowid - FROM billing_charge_items c - WHERE c.itemCode = 'ONR44' - ) - - -- one-day/short assignments - WHEN ( - a.duration <= CAST(javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_MAX_DURATION') AS INTEGER) - AND a.enddate IS NOT NULL - AND a.assignCondition = a.releaseCondition - ) - THEN ( - SELECT ci.rowid - FROM public_charge_items ci - WHERE ci.active = TRUE - AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_NAME') - ) - - -- (duplicate WHEN from original kept intentionally) - WHEN ( - a.duration <= CAST(javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_MAX_DURATION') AS INTEGER) - AND a.enddate IS NOT NULL - AND a.assignCondition = a.releaseCondition - ) - THEN ( - SELECT ci.rowid - FROM public_charge_items ci - WHERE ci.active = TRUE - AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_NAME') - ) - - -- TMB lease - WHEN a2.id IS NOT NULL - THEN ( - SELECT ci.rowid - FROM public_charge_items ci - WHERE (ci.startDate <= a.date AND ci.endDate >= a.date) - AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.TMB_LEASE_NAME') - ) - - ELSE lf.chargeId - END AS chargeId, - - -- quantity logic - CASE - WHEN a3.id IS NOT NULL - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 14 - THEN 1 - WHEN a4.id IS NOT NULL - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 14 - THEN 1 - WHEN a3.id IS NOT NULL - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 - THEN TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) - WHEN a4.id IS NOT NULL - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) <= 14 - THEN TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) - WHEN a4.id IS NOT NULL - AND TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) > 14 - THEN TIMESTAMPDIFF('SQL_TSI_Day', a.date, a.projectedRelease) - - -- This looks for TMB infant and sets count to 0 based on mom being TMB - WHEN ( - SELECT COUNT(*) AS c - FROM births b - LEFT JOIN resource_assignments a1 - ON b.id = a.id AND a.date = b.dateOnly - LEFT JOIN resource_assignments a2 - ON b.dam = a2.id - AND a2.project = 559 - AND ( - (a2.date <= b.dateOnly AND a2.endDate >= b.dateOnly) - OR a2.enddate IS NULL - ) - WHERE b.id = a.id - AND a1.project.protocol != a2.project.protocol - ) > 0 - THEN 0 - - -- infants born to resource dams - WHEN ( - SELECT COUNT(*) AS c - FROM births b - LEFT JOIN resource_assignments a1 - ON b.id = a.id - AND a.date = b.dateOnly - AND a.project.use_category IN ('Center Resource', 'U42', 'U24') - LEFT JOIN resource_assignments a2 - ON b.dam = a2.id - AND a2.project.use_category IN ('Center Resource', 'U42', 'U24') - AND ( - (a2.date <= b.dateOnly AND a2.endDate >= b.dateOnly) - OR a2.enddate IS NULL - ) - WHERE b.id = a.id - AND a1.project.protocol = a2.project.protocol - ) > 0 - THEN 0 - - WHEN (a.duration = 0 AND a.enddate IS NOT NULL AND a.assignCondition = a.releaseCondition) - THEN 1 - - WHEN (fl.id IS NOT NULL) - THEN 0 - - WHEN ( - a.duration <= CAST(javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_MAX_DURATION') AS INTEGER) - AND a.enddate IS NOT NULL - AND a.assignCondition = a.releaseCondition - ) - THEN a.duration - - ELSE 1 - END AS quantity, - - CAST(NULL AS INTEGER) AS leaseCharge1, - CAST(NULL AS INTEGER) AS leaseCharge2, - a.objectid AS sourceRecord, - NULL AS chargeCategory, - NULL AS isAdjustment, - a.datefinalized, - a.enddatefinalized - FROM assign_finalized a - - -- find overlapping TMB at date of assignment - LEFT JOIN study.assignment a2 ON ( - a.id = a2.id - AND a.project != a2.project - AND a2.dateOnly <= a.dateOnly - AND a2.endDateCoalesced >= a.dateOnly - AND a2.project.name = javaConstant('org.labkey.onprc_ehr.ONPRC_EHRManager.TMB_PROJECT') - ) - - -- Obese 0833 animals id 1609 - LEFT JOIN onprc_billing.assignment_ObeseResource a3 ON ( - a.id = a3.id - AND a.project != a3.project - AND a3.project = 1609 - AND a3.dateonly <= a.dateOnly - AND a3.endDateCoalesced >= a.dateOnly - ) - - -- Obese 0622-01 animals id 1082 - LEFT JOIN onprc_billing.assignment_ObeseResource a4 ON ( - a.id = a4.id - AND a.project != a4.project - AND a4.project = 1082 - AND a4.dateonly <= a.dateOnly - AND a4.endDateCoalesced >= a.dateOnly - ) - - -- ESPF animals being dual assigned - LEFT JOIN assignment_U42ESPF a5 ON ( - a.id = a5.id - AND a.project != a5.project - AND a5.project = 1107 - AND a5.dateonly <= a.dateOnly - AND a5.endDateCoalesced >= a.dateOnly - ) - - -- lease fee definition - LEFT JOIN lease_defs lf ON ( - a3.id IS NULL - AND a4.id IS NULL - AND a5.id IS NULL - AND lf.assignCondition = a.assignCondition - AND lf.releaseCondition = a.projectedReleaseCondition - AND (a.ageAtTime.AgeAtTimeYearsRounded >= lf.minAge OR lf.minAge IS NULL) - AND (a.ageAtTime.AgeAtTimeYearsRounded < lf.maxAge OR lf.maxAge IS NULL) - ) - - -- research-owned animal exemption - LEFT JOIN flags_4034 fl ON ( - a.id = fl.id - AND a.date >= fl.date - AND a.date <= COALESCE(fl.enddate, NOW()) - ) -), - --- ------------------------------------------------------------------ --- Lease setup fee rows --- ------------------------------------------------------------------ -setup_rows AS ( - SELECT - a.id, - a.date, - a.project, - a.date AS assignmentStart, - a.enddate, - a.projectedReleaseCondition, - a.releaseCondition, - a.assignCondition, - a.releaseType, - a.ageAtTime.AgeAtTimeYearsRounded AS ageAtTime, - ' ' AS ESPFAnimal, - 'Lease Setup Fees' AS category, - ( - SELECT ci.rowid - FROM public_charge_items ci - WHERE ci.active = TRUE - AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.LEASE_SETUP_FEES') - ) AS chargeId, - 1 AS quantity, - CAST(NULL AS INTEGER) AS leaseCharge1, - CAST(NULL AS INTEGER) AS leaseCharge2, - a.objectid AS sourceRecord, - NULL AS chargeCategory, - NULL AS isAdjustment, - a.datefinalized, - a.enddatefinalized - FROM assign_finalized a - WHERE - -- only charge setup fee for leases > 24H. - -- note: duration assumes today as end, so exclude null enddates - ( - a.duration > CAST(javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_MAX_DURATION') AS INTEGER) - OR (a.assignCondition != a.releaseCondition AND a.enddate IS NULL) - ) - AND a.id.demographics.species NOT IN ('Rabbit', 'Guinea Pigs') -), - --- ------------------------------------------------------------------ --- Adjustment rows for released animals --- ------------------------------------------------------------------ -adjustment_rows AS ( - SELECT - a.id, - CASE - WHEN a.enddate < a.dateFinalized THEN a.dateFinalized - ELSE a.enddate - END AS date, - a.project, - a.date AS assignmentStart, - a.enddate, - a.projectedReleaseCondition, - a.releaseCondition, - a.assignCondition, - a.releaseType, - a.ageAtTime.AgeAtTimeYearsRounded AS ageAtTime, - a5.id AS ESPFAnimal, - 'Lease Fees' AS category, - ( - SELECT MAX(ci.rowid) AS rowid - FROM public_charge_items ci - WHERE ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.LEASE_FEE_ADJUSTMENT') - AND ci.active = TRUE - ) AS chargeId, - CASE - WHEN (fl.id IS NOT NULL) THEN 0 - ELSE 1 - END AS quantity, - lf2.chargeId AS leaseCharge1, - lf.chargeId AS leaseCharge2, - a.objectid AS sourceRecord, - 'Adjustment - Automatic' AS chargeCategory, - 'Y' AS isAdjustment, - a.datefinalized, - a.enddatefinalized - FROM assign_end_finalized a - - LEFT JOIN onprc_billing.leaseFeeDefinition lf ON ( - lf.assignCondition = a.assignCondition - AND lf.releaseCondition = a.releaseCondition - AND (a.ageAtTime.AgeAtTimeYearsRounded >= lf.minAge OR lf.minAge IS NULL) - AND (a.ageAtTime.AgeAtTimeYearsRounded < lf.maxAge OR lf.maxAge IS NULL) - ) - - LEFT JOIN onprc_billing.leaseFeeDefinition lf2 ON ( - lf2.assignCondition = a.assignCondition - AND lf2.releaseCondition = a.projectedReleaseCondition - AND (a.ageAtTime.AgeAtTimeYearsRounded >= lf2.minAge OR lf2.minAge IS NULL) - AND (a.ageAtTime.AgeAtTimeYearsRounded < lf2.maxAge OR lf2.maxAge IS NULL) - AND (a.date >= lf2.startDate AND a.date <= lf2.endDate) - ) - - -- find overlapping TMB at date of assignment - LEFT JOIN study.assignment a2 ON ( - a.id = a2.id - AND a.project != a2.project - AND a2.dateOnly <= a.dateOnly - AND a2.endDateCoalesced >= a.dateOnly - AND a2.project.name = javaConstant('org.labkey.onprc_ehr.ONPRC_EHRManager.TMB_PROJECT') - ) - - LEFT JOIN assignment_U42ESPF a5 ON ( - a.id = a5.id - AND a.project != a5.project - AND a5.project = 1107 - AND a5.dateonly <= a.dateOnly - AND a5.endDateCoalesced >= a.dateOnly - ) - - -- research-owned animal exemption - LEFT JOIN flags_4034 fl ON ( - a.id = fl.id - AND a.date >= fl.date - AND a.date <= COALESCE(fl.enddate, NOW()) - ) - - WHERE - a.releaseCondition != a.projectedReleaseCondition - AND (a.id != a5.id OR a5.id IS NULL) - AND lf.active = TRUE - AND a2.id IS NULL - AND a.participantID NOT LIKE '[a-z]%' -) - --- ------------------------------------------------------------------ --- Final unified result --- ------------------------------------------------------------------ -SELECT * FROM standard_rows -UNION ALL -SELECT * FROM setup_rows -UNION ALL -SELECT * FROM adjustment_rows; diff --git a/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql index 9060f21af..f3d605608 100644 --- a/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql +++ b/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql @@ -1,14 +1,16 @@ -PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP +PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) -- ======================================================== -- This is the QUery using CTES that was AI Generated for revised lease fees -- Date: 2025-11-22 -- Step 1 Assignments has bene modified to use LabkeySQL structure. -- THis was moved to the onprc_billing query section +-- Working thru each CTE for code updates - Works as Designed in Dev -- ========================================================= WITH -- ========================================================= -- 1) Base assignments in date range --This was modified to use LabkeySQL structure. +-- 2025-11-24 Validation of this CTE -- ========================================================= assignments AS ( SELECT @@ -22,14 +24,14 @@ assignments AS ( (Select r.project from study.resourceAssigned r where r.id = a.id) as resourceCode, a.assignCondition, a.releaseCondition, - a.projectedReleaseDate, + a.projectedRelease, --Create a looklup for is a research project Case when a.id in (Select ra.id from study.researchAssigned ra where ra.id = a.id) then 1 Else 0 End as isResearchAssignment, -- a.isResearchAssignment, -- 1 = research, 0 = resource (or derive later) (Select b.dam from study.birth b where b.id = a.id) as damId, -- dam of infant, if applicable - a.sex, + a.id.dataset.Demographics.gender as sex, -- Using the release code assign a value for is terminal Case when a.projectedReleaseCondition in (206,207) then 1 Else 0 @@ -38,14 +40,14 @@ assignments AS ( FROM study.assignment a WHERE a.date >= StartDate AND a.date < EndDate -) --- Select * from assignments - , +), +--Select * from assignment, -- ========================================================= -- 2) Animal age / infant vs adult -- (You may already have an ageAtTime dataset; this is illustrative) -- change this to age in years +-- 2025-11-24 Starting Validation Review -- ========================================================= age_at_assignment AS ( SELECT @@ -56,7 +58,7 @@ age_at_assignment AS ( aa.resourceCode, aa.assignCondition, aa.releaseCondition, - aa.proposedReleaseDate, + aa.projectedRelease, aa.damId, aa.sex, aa.isTerminalAssignment, @@ -72,7 +74,10 @@ age_at_assignment AS ( FROM assignments aa LEFT JOIN study.demographics d ON d.Id = aa.Id -), +) +Select * from age_at_assignment + +/*, -- ========================================================= -- 3) PI purchase flag (for research assignments) @@ -339,4 +344,4 @@ final AS ( SELECT * FROM final -ORDER BY assignmentDate, Id, assignmentId; +ORDER BY assignmentDate, Id, assignmentId;*/ From 8187d4b39a49b3e3c3552aa4f52f8fdcfdc002a3 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 24 Nov 2025 10:12:53 -0700 Subject: [PATCH 07/26] Update on CTE Testing Solid to Point indicated in Code --- .../onprc_billing/gdj_NewLeaseFeeCTE.sql | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql index f3d605608..1d4fcdcc8 100644 --- a/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql +++ b/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql @@ -21,7 +21,7 @@ assignments AS ( a.enddate AS assignmentEndDate, a.project, --Create a sub query for resource ID - (Select r.project from study.resourceAssigned r where r.id = a.id) as resourceCode, + (Select r.project.displayName from study.resourceAssigned r where r.id = a.id) as resourceCode, a.assignCondition, a.releaseCondition, a.projectedRelease, @@ -54,6 +54,7 @@ age_at_assignment AS ( aa.Id, aa.assignmentId, aa.assignmentDate, + aa.assignmentEndDate, aa.project, aa.resourceCode, aa.assignCondition, @@ -75,9 +76,9 @@ age_at_assignment AS ( LEFT JOIN study.demographics d ON d.Id = aa.Id ) -Select * from age_at_assignment +--Select * from age_at_assignment -/*, +, -- ========================================================= -- 3) PI purchase flag (for research assignments) @@ -85,28 +86,34 @@ Select * from age_at_assignment pi_purchase AS ( SELECT DISTINCT f.Id, + f.value, 1 AS hasPIPurchase FROM study.flags f - WHERE f.value = 'PI Purchased NHP' + WHERE f.flag.value = 'PI Purchased NHP' --AND f.isActive = true ), assign_with_pi AS ( SELECT a.*, + p.value, COALESCE(p.hasPIPurchase, 0) AS hasPIPurchase FROM age_at_assignment a LEFT JOIN pi_purchase p ON a.Id = p.Id -), +) +--select * from pi_purchase + , -- ========================================================= -- 4) Assignment length & condition change + --This is returening total days assigned to the project -- ========================================================= assignment_length AS ( SELECT a.*, - DATEDIFF('day', a.assignmentDate, COALESCE(a.proposedReleaseDate, a.assignmentEndDate)) AS assignmentDays, + TIMESTAMPDIFF('SQL_TSI_day', a.assignmentDate, COALESCE(a.projectedRelease, a.assignmentenddate)) AS assignmentDays, + --DATEDIFF('day', a.assignmentDate, COALESCE(a.proposedReleaseDate, a.assignmentEndDate)) AS assignmentDays, CASE WHEN a.assignCondition = a.releaseCondition OR a.releaseCondition IS NULL @@ -114,7 +121,10 @@ assignment_length AS ( ELSE 1 END AS hasConditionChange FROM assign_with_pi a -), +) +-- select * from assignment_length + + , -- ========================================================= -- 5) Resource type classification @@ -134,7 +144,10 @@ resource_type AS ( ELSE 'OTHER' END AS resourceGroup FROM assignment_length a -), +) + -- select * from resource_type + + , -- ========================================================= -- 6) Dam/resource match (for infant/resource rules) @@ -163,8 +176,10 @@ with_dam_match AS ( LEFT JOIN dam_resource dr ON dr.Id = a.damId AND dr.assignmentId = a.assignmentId -- or appropriate key -), +) +-- Select * from with_dam_match +, -- ========================================================= -- 7) Determine lease type (core rule engine) -- ========================================================= @@ -260,8 +275,10 @@ lease_type AS ( END END AS leaseType FROM with_dam_match a -), - +) + --select * from lease_type + , +-- ++++++++ 2025-11-24 All CTEs above return results as expected -- ========================================================= -- 8) Map leaseType to itemCodes & credit aliases -- ========================================================= From 3536ead5806c678c286528fb6debcccfd2165354 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 1 Dec 2025 07:17:08 -0700 Subject: [PATCH 08/26] Update of LeaseFee Processing using CTEs --- .../queries/onprc_billing/LeaseFeeCTE.sql | 348 ++++++++++++++++++ 1 file changed, 348 insertions(+) create mode 100644 onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql new file mode 100644 index 000000000..c62fcadbe --- /dev/null +++ b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql @@ -0,0 +1,348 @@ +PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) + +-- ======================================================== +-- Revised Lease Fee CTE Query - LabKey SQL Compatible +-- Date: 2025-11-24 (reviewed/cleaned) +-- ======================================================== +WITH +-- ======================================================== +-- 1) Base assignments in date range +-- ======================================================== +assignments AS ( + SELECT + a.Id, + -- create an identifier as assignment ID + a.lsid AS assignmentId, + a.date AS assignmentDate, + a.enddate AS assignmentEndDate, + a.project, + + -- Resource code from resourceAssigned (latest by date if multiple) + (Select r.project.displayName from study.resourceAssigned r where r.id = a.id and r.project.enddate is Null) as resourceCode, + + a.assignCondition, + a.releaseCondition, + a.projectedRelease, + a.projectedReleaseCondition, + + --Create a looklup for is a research project + Case when a.id in (Select ra.id from study.researchAssigned ra where ra.id = a.id) then 1 + Else 0 + End as isResearchAssignment, + + -- dam of infant, if applicable + ( + SELECT b.dam + FROM study.birth b + WHERE b.id = a.id + ) AS damId, + + a.container + FROM study.assignment a + WHERE a.date >= StartDate + AND a.date < EndDate +), + +-- ======================================================== +-- 2) Animal age / infant vs adult +-- ======================================================== +age_at_assignment AS ( + SELECT + aa.Id, + aa.assignmentId, + aa.assignmentDate, + aa.assignmentEndDate, + aa.project, + aa.resourceCode, + aa.assignCondition, + aa.releaseCondition, + aa.projectedRelease, + aa.damId, + aa.isResearchAssignment, + aa.container, + d.birth, + d.gender AS sex, + + -- Age in days + TIMESTAMPDIFF('SQL_TSI_DAY', d.birth, aa.assignmentDate) AS ageDays, + + -- Infant flag: <= 1 year old at assignment + CASE + WHEN TIMESTAMPDIFF('SQL_TSI_YEAR', d.birth, aa.assignmentDate) <= 1 THEN 1 + ELSE 0 + END AS isInfant, + + -- Using projected release condition as terminal flag + CASE + WHEN aa.projectedReleaseCondition IN (206, 207) THEN 1 + ELSE 0 + END AS isTerminalAssignment + FROM assignments aa + LEFT JOIN study.demographics d + ON d.Id = aa.Id +), + +-- ======================================================== +-- 3) PI purchase flag (for research assignments) +-- ======================================================== +pi_purchase AS ( + SELECT DISTINCT + f.Id, + f.value, + 1 AS hasPIPurchase + FROM study.flags f + WHERE f.flag.value = 'PI Purchased NHP' + -- AND f.isActive = true -- uncomment if isActive exists and should filter +), + +assign_with_pi AS ( + SELECT + a.*, + p.value, + COALESCE(p.hasPIPurchase, 0) AS hasPIPurchase + FROM age_at_assignment a + LEFT JOIN pi_purchase p + ON a.Id = p.Id +), + +-- ======================================================== +-- 4) Assignment length & condition change +-- ======================================================== +assignment_length AS ( + SELECT + a.*, + TIMESTAMPDIFF( + 'SQL_TSI_DAY', + a.assignmentDate, + COALESCE(a.projectedRelease, a.assignmentEndDate) + ) AS assignmentDays, + + CASE + WHEN a.assignCondition = a.releaseCondition + OR a.releaseCondition IS NULL + THEN 0 + ELSE 1 + END AS hasConditionChange + FROM assign_with_pi a +), + +-- ======================================================== +-- 5) Resource type classification +-- ======================================================== +resource_type AS ( + SELECT + a.*, + CASE + WHEN a.resourceCode = '0300' THEN 'TMB' + WHEN a.resourceCode = '0456' THEN 'AGING' + WHEN a.resourceCode = '0833' THEN 'OBESE' + WHEN a.resourceCode = '0492-03' THEN 'SPF9' + WHEN a.resourceCode = '1092-50' THEN 'AMR' + WHEN a.resourceCode = '0492' THEN 'COLONY' + WHEN a.resourceCode = '0492-02' THEN 'U42' + WHEN a.resourceCode = '0492-45' THEN 'JMR' + ELSE 'OTHER' + END AS resourceGroup + FROM assignment_length a +), + +-- ======================================================== +-- 6) Dam/resource match (for infant/resource rules) +-- Map damId -> damResourceCode +-- ======================================================== +dam_resource AS ( + SELECT DISTINCT + r.damId, + r.resourceCode AS damResourceCode + FROM resource_type r + WHERE r.damId IS NOT NULL +), + +with_dam_match AS ( + SELECT + a.*, + CASE + WHEN a.isInfant = 1 + AND dr.damResourceCode = a.resourceCode + THEN 1 + ELSE 0 + END AS infantSameDamResource + FROM resource_type a + LEFT JOIN dam_resource dr + ON dr.damId = a.damId +), + +-- ======================================================== +-- 7) Determine lease type (core rule engine) +-- ======================================================== +lease_type AS ( + SELECT + a.*, + + CASE + -- 1) Resource assignments + WHEN a.isResearchAssignment = 0 THEN + CASE + -- 1.1 Infant, dam in same resource → no lease + WHEN a.isInfant = 1 + AND a.infantSameDamResource = 1 + THEN 'NONE' + + -- 1.2 TMB (0300) + WHEN a.resourceGroup = 'TMB' THEN + CASE + -- Example dam logic + WHEN a.isInfant = 0 + AND a.sex = 'F' + THEN 'TMB_LEASE' + + -- infant from assigned TMB dam → no lease + WHEN a.isInfant = 1 + AND a.infantSameDamResource = 1 + THEN 'NONE' + + -- infant from unassigned TMB dam → P51 lease + WHEN a.isInfant = 1 + THEN 'P51_FULL' + + -- male assigned from TMB resource → P51 rate + WHEN a.sex = 'M' + THEN 'P51_FULL' + + -- default: day lease to TMB + ELSE 'DAY_LEASE' + END + + -- 1.3 Aging (0456) → no lease for assignments + WHEN a.resourceGroup = 'AGING' THEN 'NONE' + + -- 1.4 OBESE (0833) + WHEN a.resourceGroup = 'OBESE' THEN + CASE + WHEN a.assignmentDays BETWEEN 1 AND 14 + AND a.hasConditionChange = 0 + THEN 'OBESE_DAY' -- ONR01 + WHEN a.isTerminalAssignment = 1 + THEN 'OBESE_ADULT_TERM' -- ONR45 + ELSE 'OBESE_ADULT' -- ONR25 + END + + -- 1.5 SPF9 (0492-03) + WHEN a.resourceGroup = 'SPF9' + THEN 'SPF9_EXPANDED' + + -- 1.6 AMR (1092-50) – no lease when assigned to AMR + WHEN a.resourceGroup = 'AMR' + THEN 'NONE' + + -- 1.7 Colony / U42 / JMR / Other resource → P51 rules + ELSE 'P51_FULL' + END + + -- 2) Research assignments + ELSE + CASE + -- 2.1 PI Purchased → no lease + WHEN a.hasPIPurchase = 1 + THEN 'NONE' + + -- 2.2 One day – 14 days + WHEN a.assignmentDays BETWEEN 1 AND 14 THEN + CASE + WHEN a.hasConditionChange = 0 + THEN 'DAY_LEASE' + ELSE 'FULL_LEASE' + END + + -- 2.3 > 14 days → full lease + WHEN a.assignmentDays > 14 + THEN 'FULL_LEASE' + + ELSE 'NONE' + END + END AS leaseType + FROM with_dam_match a +), + +-- ========================================================= +-- 8) Map leaseType to itemCodes & credit aliases +-- ======================================================== +lease_mapping AS ( + SELECT + l.*, + + -- Item code mapping (example codes – replace with real ones) + CASE l.leaseType + WHEN 'DAY_LEASE' THEN 'DAY01' -- generic day lease + WHEN 'FULL_LEASE' THEN 'FULL01' + WHEN 'P51_FULL' THEN 'P51' + WHEN 'OBESE_DAY' THEN 'ONR01' + WHEN 'OBESE_ADULT' THEN 'ONR25' + WHEN 'OBESE_ADULT_TERM' THEN 'ONR45' + WHEN 'SPF9_EXPANDED' THEN 'SPF9X' + WHEN 'TMB_LEASE' THEN 'TMB01' + ELSE NULL + END AS leaseItemCode, + + -- Credit resource mapping (aliases to be tied to real chargeableItems / accounts) + CASE + WHEN l.leaseType = 'NONE' THEN NULL + + -- Aging never gets credit; credit colony + WHEN l.resourceGroup = 'AGING' THEN 'COLONY_ALIAS' + + -- Obese always credits Obese resource + WHEN l.resourceGroup = 'OBESE' THEN 'OBESE_ALIAS' + + -- SPF9 credits U42E funding + WHEN l.resourceGroup = 'SPF9' THEN 'U42E_ALIAS' + + -- TMB credits TMB resource + WHEN l.resourceGroup = 'TMB' THEN 'TMB_ALIAS' + + -- AMR → credit colony + WHEN l.resourceGroup = 'AMR' THEN 'COLONY_ALIAS' + + -- Colony/U42/JMR map to their own aliases + WHEN l.resourceGroup = 'COLONY' THEN 'COLONY_ALIAS' + WHEN l.resourceGroup = 'U42' THEN 'U42_ALIAS' + WHEN l.resourceGroup = 'JMR' THEN 'JMR_ALIAS' + + -- fallback: credit the originating resource + ELSE 'ORIGIN_RESOURCE_ALIAS' + END AS creditAlias + FROM lease_type l +), + +-- ========================================================= +-- 9) Final output +-- ======================================================== +final AS ( + SELECT + f.Id, + f.assignmentId, + f.assignmentDate, + f.assignmentEndDate, + f.project, + f.resourceCode, + f.resourceGroup, + f.isResearchAssignment, + f.isInfant, + f.assignmentDays, + f.hasConditionChange, + f.hasPIPurchase, + f.isTerminalAssignment, + f.leaseType, + f.leaseItemCode, + f.creditAlias, + CASE + WHEN f.leaseType = 'NONE' + THEN 'No lease per business rules' + ELSE ('Lease generated per leaseType=' + f.leaseType) + END AS leaseNote + FROM lease_mapping f +) + +SELECT * +FROM final +ORDER BY assignmentDate, Id, assignmentId; From 126474e14e2885646033b5b36245920c97c50da6 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 1 Dec 2025 07:38:30 -0700 Subject: [PATCH 09/26] Old LeaseFee Foles --- ...NewLeaseFeeCTE.sql => gdj_NewLeaseFeeCTE_original.sql} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename onprc_billing/resources/queries/onprc_billing/{gdj_NewLeaseFeeCTE.sql => gdj_NewLeaseFeeCTE_original.sql} (98%) diff --git a/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE_original.sql similarity index 98% rename from onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql rename to onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE_original.sql index 1d4fcdcc8..0f8e639ce 100644 --- a/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE.sql +++ b/onprc_billing/resources/queries/onprc_billing/gdj_NewLeaseFeeCTE_original.sql @@ -328,8 +328,8 @@ lease_mapping AS ( END AS creditAlias FROM lease_type l ) -Select * from lease_mapping -/*, +--Select * from lease_mapping +, -- ========================================================= -- 9) Final output -- ========================================================= @@ -354,11 +354,11 @@ final AS ( CASE WHEN f.leaseType = 'NONE' THEN 'No lease per business rules' - ELSE 'Lease generated per leaseType=' + f.leaseType + ELSE ('Lease generated per leaseType=' || f.leaseType) END AS leaseNote FROM lease_mapping f ) SELECT * FROM final -ORDER BY assignmentDate, Id, assignmentId;*/ +ORDER BY assignmentDate, Id, assignmentId; From f3c08794121ef320db78b1250eded9216d692d59 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 1 Dec 2025 08:54:27 -0700 Subject: [PATCH 10/26] Changes to Lease Fees using CTEs for clarification --- onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql | 1 + .../resources/queries/onprc_billing/NonExemptAssignments.sql | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 onprc_billing/resources/queries/onprc_billing/NonExemptAssignments.sql diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql index c62fcadbe..c54df54e2 100644 --- a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql +++ b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql @@ -3,6 +3,7 @@ PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) -- ======================================================== -- Revised Lease Fee CTE Query - LabKey SQL Compatible -- Date: 2025-11-24 (reviewed/cleaned) +-- 2025-12-01 Final testing complete deploying to test instance for review -- ======================================================== WITH -- ======================================================== diff --git a/onprc_billing/resources/queries/onprc_billing/NonExemptAssignments.sql b/onprc_billing/resources/queries/onprc_billing/NonExemptAssignments.sql deleted file mode 100644 index 239396462..000000000 --- a/onprc_billing/resources/queries/onprc_billing/NonExemptAssignments.sql +++ /dev/null @@ -1,3 +0,0 @@ -SELECT * -FROM study.AssignmentsInRange a -WHERE project NOT IN ('0300','0456') -- resource exemptions (TMB/Aging) From 83e1dd42b23f9f746774910858b7dbeeb0a40816 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 15 Dec 2025 10:18:59 -0700 Subject: [PATCH 11/26] Update of Lease Fees with CTE adjustments for Day lease in LEase Type --- .../queries/onprc_billing/LeaseFeeCTE.sql | 388 +++++++++--------- 1 file changed, 191 insertions(+), 197 deletions(-) diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql index c54df54e2..9653d31ca 100644 --- a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql +++ b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql @@ -1,155 +1,145 @@ PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) -- ======================================================== --- Revised Lease Fee CTE Query - LabKey SQL Compatible --- Date: 2025-11-24 (reviewed/cleaned) --- 2025-12-01 Final testing complete deploying to test instance for review +-- Lease Fee Logic – Production Version (Revised) -- ======================================================== + WITH + -- ======================================================== --- 1) Base assignments in date range +-- 1) Base Assignments -- ======================================================== assignments AS ( SELECT a.Id, - -- create an identifier as assignment ID a.lsid AS assignmentId, - a.date AS assignmentDate, - a.enddate AS assignmentEndDate, + a.date AS assignmentDate, + a.enddate AS assignmentEndDate, a.project, - -- Resource code from resourceAssigned (latest by date if multiple) - (Select r.project.displayName from study.resourceAssigned r where r.id = a.id and r.project.enddate is Null) as resourceCode, + -- Most recent resource assignment (LabKey-safe) + ( + SELECT r.project.displayName + FROM study.resourceAssigned r + WHERE r.id = a.id + AND (r.project.enddate IS NULL OR r.project.enddate >= a.date) + AND r.project.startdate = ( + SELECT MAX(r2.project.startdate) + FROM study.resourceAssigned r2 + WHERE r2.id = a.id + ) + ) AS resourceCode, a.assignCondition, a.releaseCondition, a.projectedRelease, a.projectedReleaseCondition, - --Create a looklup for is a research project - Case when a.id in (Select ra.id from study.researchAssigned ra where ra.id = a.id) then 1 - Else 0 - End as isResearchAssignment, + CASE WHEN EXISTS ( + SELECT 1 FROM study.researchAssigned ra WHERE ra.id = a.id + ) THEN 1 ELSE 0 END AS isResearchAssignment, - -- dam of infant, if applicable - ( + ( SELECT b.dam FROM study.birth b WHERE b.id = a.id + AND b.date = ( + SELECT MAX(b2.date) + FROM study.birth b2 + WHERE b2.id = a.id + ) ) AS damId, a.container FROM study.assignment a - WHERE a.date >= StartDate - AND a.date < EndDate + WHERE a.datefinalized >= StartDate + AND (a.datefinalized <= EndDate OR a.enddate IS NULL) ), - -- ======================================================== --- 2) Animal age / infant vs adult +-- 2) Age, infant status, demographics -- ======================================================== age_at_assignment AS ( SELECT - aa.Id, - aa.assignmentId, - aa.assignmentDate, - aa.assignmentEndDate, - aa.project, - aa.resourceCode, - aa.assignCondition, - aa.releaseCondition, - aa.projectedRelease, - aa.damId, - aa.isResearchAssignment, - aa.container, + aa.*, d.birth, d.gender AS sex, - -- Age in days - TIMESTAMPDIFF('SQL_TSI_DAY', d.birth, aa.assignmentDate) AS ageDays, + TIMESTAMPDIFF('SQL_TSI_YEAR', d.birth, aa.assignmentDate) AS ageYears, + TIMESTAMPDIFF('SQL_TSI_DAY', d.birth, aa.assignmentDate) AS ageDays, - -- Infant flag: <= 1 year old at assignment - CASE - WHEN TIMESTAMPDIFF('SQL_TSI_YEAR', d.birth, aa.assignmentDate) <= 1 THEN 1 - ELSE 0 - END AS isInfant, + CASE WHEN TIMESTAMPDIFF('SQL_TSI_YEAR', d.birth, aa.assignmentDate) < 1 + THEN 1 ELSE 0 END AS isInfant, - -- Using projected release condition as terminal flag - CASE - WHEN aa.projectedReleaseCondition IN (206, 207) THEN 1 - ELSE 0 - END AS isTerminalAssignment + CASE WHEN aa.projectedReleaseCondition IN (206,207) + THEN 1 ELSE 0 END AS isTerminalAssignment FROM assignments aa - LEFT JOIN study.demographics d - ON d.Id = aa.Id + LEFT JOIN study.demographics d ON d.Id = aa.Id ), -- ======================================================== --- 3) PI purchase flag (for research assignments) +-- 3) PI Purchase Flags -- ======================================================== pi_purchase AS ( - SELECT DISTINCT + SELECT f.Id, - f.value, + MAX(f.date) AS lastFlag, 1 AS hasPIPurchase FROM study.flags f WHERE f.flag.value = 'PI Purchased NHP' - -- AND f.isActive = true -- uncomment if isActive exists and should filter + GROUP BY f.Id ), assign_with_pi AS ( SELECT a.*, - p.value, - COALESCE(p.hasPIPurchase, 0) AS hasPIPurchase + COALESCE(p.hasPIPurchase,0) AS hasPIPurchase FROM age_at_assignment a - LEFT JOIN pi_purchase p - ON a.Id = p.Id + LEFT JOIN pi_purchase p ON p.Id = a.Id ), --- ======================================================== --- 4) Assignment length & condition change + + -- ======================================================== assignment_length AS ( SELECT a.*, + + -- Date-only diff to prevent false 1-day assignments TIMESTAMPDIFF( 'SQL_TSI_DAY', - a.assignmentDate, - COALESCE(a.projectedRelease, a.assignmentEndDate) + CAST(a.assignmentDate AS DATE), + CAST(COALESCE(a.projectedRelease, a.assignmentEndDate) AS DATE) ) AS assignmentDays, CASE WHEN a.assignCondition = a.releaseCondition OR a.releaseCondition IS NULL - THEN 0 - ELSE 1 - END AS hasConditionChange + THEN 0 ELSE 1 END AS hasConditionChange FROM assign_with_pi a ), -- ======================================================== --- 5) Resource type classification +-- 5) Resource Type Mapping -- ======================================================== resource_type AS ( SELECT a.*, CASE - WHEN a.resourceCode = '0300' THEN 'TMB' - WHEN a.resourceCode = '0456' THEN 'AGING' - WHEN a.resourceCode = '0833' THEN 'OBESE' - WHEN a.resourceCode = '0492-03' THEN 'SPF9' - WHEN a.resourceCode = '1092-50' THEN 'AMR' - WHEN a.resourceCode = '0492' THEN 'COLONY' - WHEN a.resourceCode = '0492-02' THEN 'U42' - WHEN a.resourceCode = '0492-45' THEN 'JMR' + WHEN a.resourceCode = '0300' THEN 'TMB' + WHEN a.resourceCode = '0456' THEN 'AGING' + WHEN a.resourceCode = '0833' THEN 'OBESE' + WHEN a.resourceCode = '0492-03' THEN 'SPF9' + WHEN a.resourceCode = '1092-50' THEN 'AMR' + WHEN a.resourceCode = '0492' THEN 'COLONY' + WHEN a.resourceCode = '0492-02' THEN 'U42' + WHEN a.resourceCode = '0492-45' THEN 'JMR' ELSE 'OTHER' END AS resourceGroup FROM assignment_length a ), -- ======================================================== --- 6) Dam/resource match (for infant/resource rules) --- Map damId -> damResourceCode +-- 6) Infant / Dam / Resource Matching -- ======================================================== dam_resource AS ( SELECT DISTINCT @@ -164,182 +154,186 @@ with_dam_match AS ( a.*, CASE WHEN a.isInfant = 1 - AND dr.damResourceCode = a.resourceCode - THEN 1 - ELSE 0 - END AS infantSameDamResource + AND dr.damResourceCode = a.resourceCode + THEN 1 ELSE 0 END AS infantSameDamResource FROM resource_type a - LEFT JOIN dam_resource dr - ON dr.damId = a.damId + LEFT JOIN dam_resource dr ON dr.damId = a.damId ), -- ======================================================== --- 7) Determine lease type (core rule engine) +-- 7) Lease Type Determination -- ======================================================== lease_type AS ( SELECT a.*, - CASE - -- 1) Resource assignments WHEN a.isResearchAssignment = 0 THEN CASE - -- 1.1 Infant, dam in same resource → no lease - WHEN a.isInfant = 1 - AND a.infantSameDamResource = 1 + WHEN a.isInfant = 1 AND a.infantSameDamResource = 1 THEN 'NONE' - -- 1.2 TMB (0300) - WHEN a.resourceGroup = 'TMB' THEN - CASE - -- Example dam logic - WHEN a.isInfant = 0 - AND a.sex = 'F' - THEN 'TMB_LEASE' - - -- infant from assigned TMB dam → no lease - WHEN a.isInfant = 1 - AND a.infantSameDamResource = 1 - THEN 'NONE' - - -- infant from unassigned TMB dam → P51 lease - WHEN a.isInfant = 1 - THEN 'P51_FULL' - - -- male assigned from TMB resource → P51 rate - WHEN a.sex = 'M' - THEN 'P51_FULL' - - -- default: day lease to TMB - ELSE 'DAY_LEASE' - END + WHEN a.resourceGroup = 'TMB' + THEN 'TMB_LEASE' - -- 1.3 Aging (0456) → no lease for assignments - WHEN a.resourceGroup = 'AGING' THEN 'NONE' + WHEN a.resourceGroup = 'AGING' + THEN 'NONE' - -- 1.4 OBESE (0833) WHEN a.resourceGroup = 'OBESE' THEN CASE - WHEN a.assignmentDays BETWEEN 1 AND 14 + WHEN a.assignmentDays BETWEEN 0 AND 14 AND a.hasConditionChange = 0 - THEN 'OBESE_DAY' -- ONR01 + THEN 'OBESE_DAY' WHEN a.isTerminalAssignment = 1 - THEN 'OBESE_ADULT_TERM' -- ONR45 - ELSE 'OBESE_ADULT' -- ONR25 + THEN 'OBESE_ADULT_TERM' + ELSE 'OBESE_ADULT' END - -- 1.5 SPF9 (0492-03) WHEN a.resourceGroup = 'SPF9' THEN 'SPF9_EXPANDED' - -- 1.6 AMR (1092-50) – no lease when assigned to AMR WHEN a.resourceGroup = 'AMR' THEN 'NONE' - -- 1.7 Colony / U42 / JMR / Other resource → P51 rules - ELSE 'P51_FULL' - END - - -- 2) Research assignments + -- ELSE 'FULL_LEASE' + /* END ELSE - CASE - -- 2.1 PI Purchased → no lease - WHEN a.hasPIPurchase = 1 - THEN 'NONE' - - -- 2.2 One day – 14 days - WHEN a.assignmentDays BETWEEN 1 AND 14 THEN - CASE - WHEN a.hasConditionChange = 0 - THEN 'DAY_LEASE' - ELSE 'FULL_LEASE' - END - - -- 2.3 > 14 days → full lease - WHEN a.assignmentDays > 14 - THEN 'FULL_LEASE' - + CASE*/ + WHEN a.hasPIPurchase = 1 THEN 'NONE' + WHEN a.assignmentDays BETWEEN 0 AND 14 THEN + CASE WHEN a.hasConditionChange = 0 THEN 'DAY_LEASE' + ELSE 'FULL_LEASE' END + WHEN a.assignmentDays > 14 THEN 'FULL_LEASE' ELSE 'NONE' END END AS leaseType FROM with_dam_match a +) +Select * from lease_type +/*-- ======================================================== +-- 8) Full Lease Matrix (LabKey-safe: aliases in first SELECT) +-- ======================================================== +full_lease_matrix AS ( + SELECT + 'LT1' AS matrixKey, + '<1' AS ageBucket, + '201' AS assignCond, + '201' AS releaseCond, + '1533' AS chargeRowId + UNION ALL SELECT 'LT2', '<1', '201', '202', '1534' + UNION ALL SELECT 'LT3', '<1', '201', '204', '1535' + UNION ALL SELECT 'LT4', '<1', '201', '206', '1537' + UNION ALL SELECT 'LT5', '<1', '202', '202', '1538' + UNION ALL SELECT 'LT6', '<1', '202', '204', '1539' + UNION ALL SELECT 'LT7', '<1', '202', '206', '1541' + UNION ALL SELECT 'LT8', '<1', '204', '204', '1546' + UNION ALL SELECT 'LT9', '<1', '204', '206', '1548' + UNION ALL SELECT 'LT10', '<1', '207', '206', '5250' + UNION ALL SELECT 'LT11', '<1', '207', '207', '1551' + + UNION ALL SELECT 'LT20','1-4','201','201','1495' + UNION ALL SELECT 'LT21','1-4','201','202','1496' + UNION ALL SELECT 'LT22','1-4','201','204','1497' + UNION ALL SELECT 'LT23','1-4','201','206','1499' + UNION ALL SELECT 'LT24','1-4','202','202','1500' + UNION ALL SELECT 'LT25','1-4','202','204','1501' + UNION ALL SELECT 'LT26','1-4','202','206','1503' + UNION ALL SELECT 'LT27','1-4','204','204','1508' + UNION ALL SELECT 'LT28','1-4','204','206','1510' + UNION ALL SELECT 'LT29','1-4','207','206','5253' + UNION ALL SELECT 'LT30','1-4','207','207','1513' + + UNION ALL SELECT 'LT40','4+','201','201','5315' + UNION ALL SELECT 'LT41','4+','201','202','5316' + UNION ALL SELECT 'LT42','4+','201','204','5317' + UNION ALL SELECT 'LT43','4+','201','206','5318' + UNION ALL SELECT 'LT44','4+','202','202','5319' + UNION ALL SELECT 'LT45','4+','202','204','5320' + UNION ALL SELECT 'LT46','4+','202','206','5321' + UNION ALL SELECT 'LT47','4+','204','204','5322' + UNION ALL SELECT 'LT48','4+','204','206','5323' + UNION ALL SELECT 'LT49','4+','207','206','5324' + UNION ALL SELECT 'LT50','4+','207','207','5325' ), --- ========================================================= --- 8) Map leaseType to itemCodes & credit aliases +-- ======================================================== +-- 9) Match Full Lease to Matrix +-- ======================================================== +full_lease_match AS ( + SELECT + lt.assignmentId, + lt.Id, + lt.ageYears, + lt.assignCondition, + lt.projectedReleaseCondition, + m.matrixKey, + m.ageBucket, + m.assignCond, + m.releaseCond, + m.chargeRowId + FROM lease_type lt + LEFT JOIN full_lease_matrix m + ON ( + (m.ageBucket = '<1' AND lt.ageYears < 1) OR + (m.ageBucket = '1-4' AND lt.ageYears >= 1 AND lt.ageYears < 4) OR + (m.ageBucket = '4+' AND lt.ageYears >= 4) + ) + AND m.assignCond = CAST(lt.assignCondition AS VARCHAR(5)) + AND m.releaseCond = CAST(lt.projectedReleaseCondition AS VARCHAR(5)) + WHERE lt.leaseType = 'FULL_LEASE' +), + +-- ======================================================== +-- 10) Lease Mapping → Charge IDs -- ======================================================== lease_mapping AS ( SELECT - l.*, - - -- Item code mapping (example codes – replace with real ones) - CASE l.leaseType - WHEN 'DAY_LEASE' THEN 'DAY01' -- generic day lease - WHEN 'FULL_LEASE' THEN 'FULL01' - WHEN 'P51_FULL' THEN 'P51' - WHEN 'OBESE_DAY' THEN 'ONR01' - WHEN 'OBESE_ADULT' THEN 'ONR25' - WHEN 'OBESE_ADULT_TERM' THEN 'ONR45' - WHEN 'SPF9_EXPANDED' THEN 'SPF9X' - WHEN 'TMB_LEASE' THEN 'TMB01' - ELSE NULL - END AS leaseItemCode, + lt.*, - -- Credit resource mapping (aliases to be tied to real chargeableItems / accounts) CASE - WHEN l.leaseType = 'NONE' THEN NULL - - -- Aging never gets credit; credit colony - WHEN l.resourceGroup = 'AGING' THEN 'COLONY_ALIAS' - - -- Obese always credits Obese resource - WHEN l.resourceGroup = 'OBESE' THEN 'OBESE_ALIAS' - - -- SPF9 credits U42E funding - WHEN l.resourceGroup = 'SPF9' THEN 'U42E_ALIAS' - - -- TMB credits TMB resource - WHEN l.resourceGroup = 'TMB' THEN 'TMB_ALIAS' - - -- AMR → credit colony - WHEN l.resourceGroup = 'AMR' THEN 'COLONY_ALIAS' - - -- Colony/U42/JMR map to their own aliases - WHEN l.resourceGroup = 'COLONY' THEN 'COLONY_ALIAS' - WHEN l.resourceGroup = 'U42' THEN 'U42_ALIAS' - WHEN l.resourceGroup = 'JMR' THEN 'JMR_ALIAS' + WHEN lt.leaseType = 'FULL_LEASE' + THEN fm.chargeRowId + WHEN lt.leaseType = 'DAY_LEASE' + THEN '90' + WHEN lt.leaseType = 'OBESE_DAY' + THEN '5367' + WHEN lt.leaseType = 'OBESE_ADULT' + THEN '5368' + WHEN lt.leaseType = 'OBESE_ADULT_TERM' + THEN '5369' + WHEN lt.leaseType = 'TMB_LEASE' + THEN '1552' + WHEN lt.leaseType = 'SPF9_EXPANDED' + THEN '5348' + ELSE NULL + END AS chargeID, - -- fallback: credit the originating resource + CASE + WHEN lt.leaseType = 'NONE' THEN NULL + WHEN lt.resourceGroup = 'AGING' THEN 'COLONY_ALIAS' + WHEN lt.resourceGroup = 'OBESE' THEN 'OBESE_ALIAS' + WHEN lt.resourceGroup = 'SPF9' THEN 'U42E_ALIAS' + WHEN lt.resourceGroup = 'TMB' THEN 'TMB_ALIAS' + WHEN lt.resourceGroup = 'AMR' THEN 'COLONY_ALIAS' + WHEN lt.resourceGroup = 'COLONY' THEN 'COLONY_ALIAS' + WHEN lt.resourceGroup = 'U42' THEN 'U42_ALIAS' + WHEN lt.resourceGroup = 'JMR' THEN 'JMR_ALIAS' ELSE 'ORIGIN_RESOURCE_ALIAS' END AS creditAlias - FROM lease_type l + + FROM lease_type lt + LEFT JOIN full_lease_match fm ON fm.assignmentId = lt.assignmentId ), --- ========================================================= --- 9) Final output +-- ======================================================== +-- 11) Final Output -- ======================================================== final AS ( SELECT - f.Id, - f.assignmentId, - f.assignmentDate, - f.assignmentEndDate, - f.project, - f.resourceCode, - f.resourceGroup, - f.isResearchAssignment, - f.isInfant, - f.assignmentDays, - f.hasConditionChange, - f.hasPIPurchase, - f.isTerminalAssignment, - f.leaseType, - f.leaseItemCode, - f.creditAlias, + f.*, CASE WHEN f.leaseType = 'NONE' THEN 'No lease per business rules' - ELSE ('Lease generated per leaseType=' + f.leaseType) + ELSE CONCAT('Lease generated per leaseType=', f.leaseType) END AS leaseNote FROM lease_mapping f ) From 1297cc1ace0a5ffc4259b53981057977fa05d75c Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 15 Dec 2025 10:22:08 -0700 Subject: [PATCH 12/26] Update of Lease Fees with CTE adjustments for Day lease in LEase Type --- onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql index 9653d31ca..cc4f78c6a 100644 --- a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql +++ b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql @@ -2,6 +2,7 @@ PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) -- ======================================================== -- Lease Fee Logic – Production Version (Revised) +--2025-12-15 jonesga Latest update changes in lease type -- ======================================================== WITH From 0d335a2036808e00f807b1974936ce5dcaf508ef Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 15 Dec 2025 10:26:22 -0700 Subject: [PATCH 13/26] Update of Lease Fees with CTE adjustments for Day lease in LEase Type --- onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql index cc4f78c6a..c1d0e9874 100644 --- a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql +++ b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql @@ -3,6 +3,7 @@ PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) -- ======================================================== -- Lease Fee Logic – Production Version (Revised) --2025-12-15 jonesga Latest update changes in lease type +--test as Deploy Looks to have issues -- ======================================================== WITH From 194806599dc87a7849eebc602c85f583a565f698 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 15 Dec 2025 11:25:14 -0700 Subject: [PATCH 14/26] Update of Lease Fees with CTE adjustments for Day lease in LEase Type --- onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql index c1d0e9874..e0ace8906 100644 --- a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql +++ b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql @@ -4,6 +4,7 @@ PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) -- Lease Fee Logic – Production Version (Revised) --2025-12-15 jonesga Latest update changes in lease type --test as Deploy Looks to have issues +--Cop0mpared to Staging lease fees and provided correct number of rows -- ======================================================== WITH From 47a2c10bcdf80edfe956b3e76512835b922974d9 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 15 Dec 2025 11:27:41 -0700 Subject: [PATCH 15/26] Update of Lease Fees with CTE adjustments for Day lease in LEase Type --- onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql index e0ace8906..9172bd4ae 100644 --- a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql +++ b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql @@ -3,8 +3,9 @@ PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) -- ======================================================== -- Lease Fee Logic – Production Version (Revised) --2025-12-15 jonesga Latest update changes in lease type +-- Deployed to Github --test as Deploy Looks to have issues ---Cop0mpared to Staging lease fees and provided correct number of rows +--Compared to Staging lease fees and provided correct number of rows -- ======================================================== WITH From 7e3fe7d4b578a9f4d271e4da405ee8ac36769a61 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 16 Dec 2025 14:58:11 -0700 Subject: [PATCH 16/26] New QUery to Match fields in leaseFeeRates query. Dropping old CTE lease fee query --- .../queries/onprc_billing/LeaseFeeCTE.sql | 347 ----------------- .../queries/onprc_billing/leaseFeeRates.sql | 366 ------------------ .../queries/onprc_billing/leasefeerates.sql | 294 ++++++++++++++ 3 files changed, 294 insertions(+), 713 deletions(-) delete mode 100644 onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql delete mode 100644 onprc_billing/resources/queries/onprc_billing/leaseFeeRates.sql create mode 100644 onprc_billing/resources/queries/onprc_billing/leasefeerates.sql diff --git a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql b/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql deleted file mode 100644 index 9172bd4ae..000000000 --- a/onprc_billing/resources/queries/onprc_billing/LeaseFeeCTE.sql +++ /dev/null @@ -1,347 +0,0 @@ -PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) - --- ======================================================== --- Lease Fee Logic – Production Version (Revised) ---2025-12-15 jonesga Latest update changes in lease type --- Deployed to Github ---test as Deploy Looks to have issues ---Compared to Staging lease fees and provided correct number of rows --- ======================================================== - -WITH - --- ======================================================== --- 1) Base Assignments --- ======================================================== -assignments AS ( - SELECT - a.Id, - a.lsid AS assignmentId, - a.date AS assignmentDate, - a.enddate AS assignmentEndDate, - a.project, - - -- Most recent resource assignment (LabKey-safe) - ( - SELECT r.project.displayName - FROM study.resourceAssigned r - WHERE r.id = a.id - AND (r.project.enddate IS NULL OR r.project.enddate >= a.date) - AND r.project.startdate = ( - SELECT MAX(r2.project.startdate) - FROM study.resourceAssigned r2 - WHERE r2.id = a.id - ) - ) AS resourceCode, - - a.assignCondition, - a.releaseCondition, - a.projectedRelease, - a.projectedReleaseCondition, - - CASE WHEN EXISTS ( - SELECT 1 FROM study.researchAssigned ra WHERE ra.id = a.id - ) THEN 1 ELSE 0 END AS isResearchAssignment, - - ( - SELECT b.dam - FROM study.birth b - WHERE b.id = a.id - AND b.date = ( - SELECT MAX(b2.date) - FROM study.birth b2 - WHERE b2.id = a.id - ) - ) AS damId, - - a.container - FROM study.assignment a - WHERE a.datefinalized >= StartDate - AND (a.datefinalized <= EndDate OR a.enddate IS NULL) -), --- ======================================================== --- 2) Age, infant status, demographics --- ======================================================== -age_at_assignment AS ( - SELECT - aa.*, - d.birth, - d.gender AS sex, - - TIMESTAMPDIFF('SQL_TSI_YEAR', d.birth, aa.assignmentDate) AS ageYears, - TIMESTAMPDIFF('SQL_TSI_DAY', d.birth, aa.assignmentDate) AS ageDays, - - CASE WHEN TIMESTAMPDIFF('SQL_TSI_YEAR', d.birth, aa.assignmentDate) < 1 - THEN 1 ELSE 0 END AS isInfant, - - CASE WHEN aa.projectedReleaseCondition IN (206,207) - THEN 1 ELSE 0 END AS isTerminalAssignment - FROM assignments aa - LEFT JOIN study.demographics d ON d.Id = aa.Id -), - --- ======================================================== --- 3) PI Purchase Flags --- ======================================================== -pi_purchase AS ( - SELECT - f.Id, - MAX(f.date) AS lastFlag, - 1 AS hasPIPurchase - FROM study.flags f - WHERE f.flag.value = 'PI Purchased NHP' - GROUP BY f.Id -), - -assign_with_pi AS ( - SELECT - a.*, - COALESCE(p.hasPIPurchase,0) AS hasPIPurchase - FROM age_at_assignment a - LEFT JOIN pi_purchase p ON p.Id = a.Id -), - - - --- ======================================================== -assignment_length AS ( - SELECT - a.*, - - -- Date-only diff to prevent false 1-day assignments - TIMESTAMPDIFF( - 'SQL_TSI_DAY', - CAST(a.assignmentDate AS DATE), - CAST(COALESCE(a.projectedRelease, a.assignmentEndDate) AS DATE) - ) AS assignmentDays, - - CASE - WHEN a.assignCondition = a.releaseCondition - OR a.releaseCondition IS NULL - THEN 0 ELSE 1 END AS hasConditionChange - FROM assign_with_pi a -), - --- ======================================================== --- 5) Resource Type Mapping --- ======================================================== -resource_type AS ( - SELECT - a.*, - CASE - WHEN a.resourceCode = '0300' THEN 'TMB' - WHEN a.resourceCode = '0456' THEN 'AGING' - WHEN a.resourceCode = '0833' THEN 'OBESE' - WHEN a.resourceCode = '0492-03' THEN 'SPF9' - WHEN a.resourceCode = '1092-50' THEN 'AMR' - WHEN a.resourceCode = '0492' THEN 'COLONY' - WHEN a.resourceCode = '0492-02' THEN 'U42' - WHEN a.resourceCode = '0492-45' THEN 'JMR' - ELSE 'OTHER' - END AS resourceGroup - FROM assignment_length a -), - --- ======================================================== --- 6) Infant / Dam / Resource Matching --- ======================================================== -dam_resource AS ( - SELECT DISTINCT - r.damId, - r.resourceCode AS damResourceCode - FROM resource_type r - WHERE r.damId IS NOT NULL -), - -with_dam_match AS ( - SELECT - a.*, - CASE - WHEN a.isInfant = 1 - AND dr.damResourceCode = a.resourceCode - THEN 1 ELSE 0 END AS infantSameDamResource - FROM resource_type a - LEFT JOIN dam_resource dr ON dr.damId = a.damId -), - --- ======================================================== --- 7) Lease Type Determination --- ======================================================== -lease_type AS ( - SELECT - a.*, - CASE - WHEN a.isResearchAssignment = 0 THEN - CASE - WHEN a.isInfant = 1 AND a.infantSameDamResource = 1 - THEN 'NONE' - - WHEN a.resourceGroup = 'TMB' - THEN 'TMB_LEASE' - - WHEN a.resourceGroup = 'AGING' - THEN 'NONE' - - WHEN a.resourceGroup = 'OBESE' THEN - CASE - WHEN a.assignmentDays BETWEEN 0 AND 14 - AND a.hasConditionChange = 0 - THEN 'OBESE_DAY' - WHEN a.isTerminalAssignment = 1 - THEN 'OBESE_ADULT_TERM' - ELSE 'OBESE_ADULT' - END - - WHEN a.resourceGroup = 'SPF9' - THEN 'SPF9_EXPANDED' - - WHEN a.resourceGroup = 'AMR' - THEN 'NONE' - - -- ELSE 'FULL_LEASE' - /* END - ELSE - CASE*/ - WHEN a.hasPIPurchase = 1 THEN 'NONE' - WHEN a.assignmentDays BETWEEN 0 AND 14 THEN - CASE WHEN a.hasConditionChange = 0 THEN 'DAY_LEASE' - ELSE 'FULL_LEASE' END - WHEN a.assignmentDays > 14 THEN 'FULL_LEASE' - ELSE 'NONE' - END - END AS leaseType - FROM with_dam_match a -) -Select * from lease_type -/*-- ======================================================== --- 8) Full Lease Matrix (LabKey-safe: aliases in first SELECT) --- ======================================================== -full_lease_matrix AS ( - SELECT - 'LT1' AS matrixKey, - '<1' AS ageBucket, - '201' AS assignCond, - '201' AS releaseCond, - '1533' AS chargeRowId - UNION ALL SELECT 'LT2', '<1', '201', '202', '1534' - UNION ALL SELECT 'LT3', '<1', '201', '204', '1535' - UNION ALL SELECT 'LT4', '<1', '201', '206', '1537' - UNION ALL SELECT 'LT5', '<1', '202', '202', '1538' - UNION ALL SELECT 'LT6', '<1', '202', '204', '1539' - UNION ALL SELECT 'LT7', '<1', '202', '206', '1541' - UNION ALL SELECT 'LT8', '<1', '204', '204', '1546' - UNION ALL SELECT 'LT9', '<1', '204', '206', '1548' - UNION ALL SELECT 'LT10', '<1', '207', '206', '5250' - UNION ALL SELECT 'LT11', '<1', '207', '207', '1551' - - UNION ALL SELECT 'LT20','1-4','201','201','1495' - UNION ALL SELECT 'LT21','1-4','201','202','1496' - UNION ALL SELECT 'LT22','1-4','201','204','1497' - UNION ALL SELECT 'LT23','1-4','201','206','1499' - UNION ALL SELECT 'LT24','1-4','202','202','1500' - UNION ALL SELECT 'LT25','1-4','202','204','1501' - UNION ALL SELECT 'LT26','1-4','202','206','1503' - UNION ALL SELECT 'LT27','1-4','204','204','1508' - UNION ALL SELECT 'LT28','1-4','204','206','1510' - UNION ALL SELECT 'LT29','1-4','207','206','5253' - UNION ALL SELECT 'LT30','1-4','207','207','1513' - - UNION ALL SELECT 'LT40','4+','201','201','5315' - UNION ALL SELECT 'LT41','4+','201','202','5316' - UNION ALL SELECT 'LT42','4+','201','204','5317' - UNION ALL SELECT 'LT43','4+','201','206','5318' - UNION ALL SELECT 'LT44','4+','202','202','5319' - UNION ALL SELECT 'LT45','4+','202','204','5320' - UNION ALL SELECT 'LT46','4+','202','206','5321' - UNION ALL SELECT 'LT47','4+','204','204','5322' - UNION ALL SELECT 'LT48','4+','204','206','5323' - UNION ALL SELECT 'LT49','4+','207','206','5324' - UNION ALL SELECT 'LT50','4+','207','207','5325' -), - --- ======================================================== --- 9) Match Full Lease to Matrix --- ======================================================== -full_lease_match AS ( - SELECT - lt.assignmentId, - lt.Id, - lt.ageYears, - lt.assignCondition, - lt.projectedReleaseCondition, - m.matrixKey, - m.ageBucket, - m.assignCond, - m.releaseCond, - m.chargeRowId - FROM lease_type lt - LEFT JOIN full_lease_matrix m - ON ( - (m.ageBucket = '<1' AND lt.ageYears < 1) OR - (m.ageBucket = '1-4' AND lt.ageYears >= 1 AND lt.ageYears < 4) OR - (m.ageBucket = '4+' AND lt.ageYears >= 4) - ) - AND m.assignCond = CAST(lt.assignCondition AS VARCHAR(5)) - AND m.releaseCond = CAST(lt.projectedReleaseCondition AS VARCHAR(5)) - WHERE lt.leaseType = 'FULL_LEASE' -), - --- ======================================================== --- 10) Lease Mapping → Charge IDs --- ======================================================== -lease_mapping AS ( - SELECT - lt.*, - - CASE - WHEN lt.leaseType = 'FULL_LEASE' - THEN fm.chargeRowId - WHEN lt.leaseType = 'DAY_LEASE' - THEN '90' - WHEN lt.leaseType = 'OBESE_DAY' - THEN '5367' - WHEN lt.leaseType = 'OBESE_ADULT' - THEN '5368' - WHEN lt.leaseType = 'OBESE_ADULT_TERM' - THEN '5369' - WHEN lt.leaseType = 'TMB_LEASE' - THEN '1552' - WHEN lt.leaseType = 'SPF9_EXPANDED' - THEN '5348' - ELSE NULL - END AS chargeID, - - CASE - WHEN lt.leaseType = 'NONE' THEN NULL - WHEN lt.resourceGroup = 'AGING' THEN 'COLONY_ALIAS' - WHEN lt.resourceGroup = 'OBESE' THEN 'OBESE_ALIAS' - WHEN lt.resourceGroup = 'SPF9' THEN 'U42E_ALIAS' - WHEN lt.resourceGroup = 'TMB' THEN 'TMB_ALIAS' - WHEN lt.resourceGroup = 'AMR' THEN 'COLONY_ALIAS' - WHEN lt.resourceGroup = 'COLONY' THEN 'COLONY_ALIAS' - WHEN lt.resourceGroup = 'U42' THEN 'U42_ALIAS' - WHEN lt.resourceGroup = 'JMR' THEN 'JMR_ALIAS' - ELSE 'ORIGIN_RESOURCE_ALIAS' - END AS creditAlias - - FROM lease_type lt - LEFT JOIN full_lease_match fm ON fm.assignmentId = lt.assignmentId -), - --- ======================================================== --- 11) Final Output --- ======================================================== -final AS ( - SELECT - f.*, - CASE - WHEN f.leaseType = 'NONE' - THEN 'No lease per business rules' - ELSE CONCAT('Lease generated per leaseType=', f.leaseType) - END AS leaseNote - FROM lease_mapping f -) - -SELECT * -FROM final -ORDER BY assignmentDate, Id, assignmentId; diff --git a/onprc_billing/resources/queries/onprc_billing/leaseFeeRates.sql b/onprc_billing/resources/queries/onprc_billing/leaseFeeRates.sql deleted file mode 100644 index 9ae0ec136..000000000 --- a/onprc_billing/resources/queries/onprc_billing/leaseFeeRates.sql +++ /dev/null @@ -1,366 +0,0 @@ - -/* - * Copyright (c) 2013 LabKey Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -SELECT - t.id, - t.date, - t.enddate, - t.project, - t.account, - t.projectedReleaseCondition, - t.releaseCondition, - t.assignCondition, - t.releaseType, - t.ageAtTime, - t.category, - t.chargeId, - t.serviceCenter, - t.item, - - t.leaseCharge1, - t.leaseCharge2, - t.sourceRecord, - t.chargeCategory, - - round(CAST(CASE - WHEN t.displayName = javaConstant('org.labkey.onprc_ehr.ONPRC_EHRManager.BASE_GRANT_PROJECT') THEN 0 - --handle adjustments and non-adjustments separately - WHEN (t.isAdjustment IS NULL) THEN t.unitCost1 - --note: we take the amount that should have been paid and subtract what was predicted to have been paid - ELSE (t.unitCost3 - t.unitCost2) - END AS DOUBLE), 2) as unitCost, - CAST(CASE - --handle adjustments and non-adjustments separately - WHEN (t.isAdjustment IS NULL) THEN t.nihRate1 - --note: we take the amount that should have been paid and subtract what was predicted to have been paid - ELSE (t.nihRate3 - t.nihRate2) - END AS DOUBLE) as nihRate, - - t.unitCost1, - t.nihRate1, - t.unitCost2, - t.nihRate2, - t.unitCost3, - t.nihRate3, - - t.quantity, - t.creditAccount, - t.creditAccountId, - t.comment, - t.investigatorId, - t.isExemption, - t.isNonStandardRate, - t.lacksRate, - t.rateId, - t.exemptionId, - t.isMiscCharge, - t.isAdjustment, - t.isMissingAccount, - t.isMissingFaid, - t.isAcceptingCharges, - t.isExpiredAccount, - t.isOldCharge, - t.currentActiveAlias, - t.datefinalized, - t.enddatefinalized - - -FROM ( -SELECT - p.id, - p.date, - p.enddate, - p.project, - alias.alias as account, - p.project.displayName as displayName, - p.projectedReleaseCondition, - p.releaseCondition, - p.assignCondition, - p.releaseType, - p.ageAtTime, - p.category, - p.chargeId, - p.chargeId.departmentCode as serviceCenter, - p.chargeId.name as item, - - p.leaseCharge1, - p.leaseCharge2, - p.sourceRecord, - p.chargeCategory, - - --this is the cost of the original lease, based on projected release type - CAST(CASE - --order of priority for unit cost: - --project-level exemption: pay this value - WHEN (e.unitCost IS NOT NULL) THEN e.unitCost - --project-level multiplier: multiply NIH rate by this value - WHEN (pm.multiplier IS NOT NULL AND cr.unitCost IS NOT NULL) THEN (cr.unitCost * pm.multiplier) - --if there is not a known rate, we dont know what do to - WHEN (cr.unitCost IS NULL) THEN null - --for non-OGA aliases, we always use the NIH rate - WHEN (alias.category IS NOT NULL AND alias.category != 'OGA') THEN cr.unitCost - --if we dont know the aliasType, we also dont know what do to - WHEN (alias.aliasType.aliasType IS NULL) THEN null - --remove both subsidy and raise F&A if needed - WHEN (alias.aliasType.removeSubsidy = true AND (alias.aliasType.canRaiseFA = true AND p.chargeId.canRaiseFA = true)) THEN ((cr.unitCost / (1 - COALESCE(cr.subsidy, 0))) * (CASE WHEN (alias.faRate IS NOT NULL AND alias.faRate < CAST(ir.indirectRate AS DOUBLE)) THEN ((1 + (CAST(ir.IndirectRate AS DOUBLE) / (1 + alias.faRate)))) ELSE 1 END)) - --remove subsidy only - WHEN (alias.aliasType.removeSubsidy = true AND alias.aliasType.canRaiseFA = false) THEN (cr.unitCost / (1 - COALESCE(cr.subsidy, 0))) - --raise F&A on ly - WHEN (alias.aliasType.removeSubsidy = false AND (alias.aliasType.canRaiseFA = true AND p.chargeId.canRaiseFA = true)) THEN (cr.unitCost * (CASE WHEN (alias.faRate IS NOT NULL AND alias.faRate < CAST(ir.indirectRate AS DOUBLE)) THEN ((1 + (CAST(ir.indirectRate AS DOUBLE)))/(1+ alias.faRate)) ELSE 1 END)) - --the NIH rate - ELSE cr.unitCost - END AS DOUBLE) as unitCost1, - cr.unitCost as nihRate1, - - --for adjustments, this is the first lease charge - CAST(CASE - --order of priority for unit cost: - --project-level exemption: pay this value - WHEN (e2.unitCost IS NOT NULL) THEN e2.unitCost - --project-level multiplier: multiply NIH rate by this value - WHEN (pm.multiplier IS NOT NULL AND cr2.unitCost IS NOT NULL) THEN (cr2.unitCost * pm.multiplier) - --if there is not a known rate, we dont know what do to - WHEN (cr2.unitCost IS NULL) THEN null - --for non-OGA aliases, we always use the NIH rate - WHEN (alias.category IS NOT NULL AND alias.category != 'OGA') THEN cr2.unitCost - --if we dont know the aliasType, we also dont know what do to - WHEN (alias.aliasType.aliasType IS NULL) THEN null - --remove both subsidy and raise F&A if needed - WHEN (alias.aliasType.removeSubsidy = true AND (alias.aliasType.canRaiseFA = true AND p.chargeId.canRaiseFA = true)) THEN ((cr2.unitCost / (1 - COALESCE(cr2.subsidy, 0))) * (CASE WHEN (alias.faRate IS NOT NULL AND alias.faRate < CAST(ir.indirectRate AS DOUBLE)) THEN (1 + (CAST(ir.indirectRate AS DOUBLE) / (1 + alias.faRate))) ELSE 1 END)) - --remove subsidy only - WHEN (alias.aliasType.removeSubsidy = true AND alias.aliasType.canRaiseFA = false) THEN (cr2.unitCost / (1 - COALESCE(cr2.subsidy, 0))) - --raise F&A only - WHEN (alias.aliasType.removeSubsidy = false AND (alias.aliasType.canRaiseFA = true AND p.chargeId.canRaiseFA = true)) THEN (cr2.unitCost * (CASE WHEN (alias.faRate IS NOT NULL AND alias.faRate < CAST(ir.indirectRate AS DOUBLE)) THEN (1 + (CAST(ir.IndirectRate AS DOUBLE) / (1 + alias.faRate))) ELSE 1 END)) - --the NIH rate - ELSE cr2.unitCost - END AS DOUBLE) as unitCost2, - cr2.unitCost as nihRate2, - - --for adjustments, this is the second lease charge - CAST(CASE - --order of priority for unit cost: - --project-level exemption: pay this value - WHEN (e3.unitCost IS NOT NULL) THEN e3.unitCost - --project-level multiplier: multiply NIH rate by this value - WHEN (pm.multiplier IS NOT NULL AND cr3.unitCost IS NOT NULL) THEN (cr3.unitCost * pm.multiplier) - --if there is not a known rate, we dont know what do to - WHEN (cr3.unitCost IS NULL) THEN null - --for non-OGA aliases, we always use the NIH rate - WHEN (alias.category IS NOT NULL AND alias.category != 'OGA') THEN cr3.unitCost - --if we dont know the aliasType, we also dont know what do to - WHEN (alias.aliasType.aliasType IS NULL) THEN null - --remove both subsidy and raise F&A if needed - WHEN (alias.aliasType.removeSubsidy = true AND (alias.aliasType.canRaiseFA = true AND p.chargeId.canRaiseFA = true)) THEN ((cr3.unitCost / (1 - COALESCE(cr3.subsidy, 0))) * (CASE WHEN (alias.faRate IS NOT NULL AND alias.faRate < CAST(ir.indirectRate AS DOUBLE)) THEN (1 + (CAST(ir.indirectRate AS DOUBLE) / ( 1 + alias.faRate))) ELSE 1 END)) - --remove subsidy only - WHEN (alias.aliasType.removeSubsidy = true AND alias.aliasType.canRaiseFA = false) THEN (cr3.unitCost / (1 - COALESCE(cr3.subsidy, 0))) - --raise F&A only - WHEN (alias.aliasType.removeSubsidy = false AND (alias.aliasType.canRaiseFA = true AND p.chargeId.canRaiseFA = true)) THEN (cr3.unitCost * (CASE WHEN (alias.faRate IS NOT NULL AND alias.faRate < CAST(ir.IndirectRate AS DOUBLE)) THEN (1 + (CAST(ir.IndirectRate AS DOUBLE) / (1 + alias.faRate))) ELSE 1 END)) - --the NIH rate - ELSE cr3.unitCost - END AS DOUBLE) as unitCost3, - cr3.unitCost as nihRate3, - - p.quantity, - cast(ce.account as varchar(200)) as creditAccount, - ce.rowid as creditAccountId, - null as comment, - coalesce(alias.investigatorId, p.project.investigatorId) as investigatorId, - CASE - WHEN (e.rowid IS NOT NULL OR e2.rowid IS NOT NULL OR e3.rowid IS NOT NULL) THEN 'Y' - WHEN (pm.multiplier IS NOT NULL) THEN ('Multiplier: ' || CAST(pm.multiplier AS varchar(100))) - ELSE null - END as isExemption, - CASE - WHEN (e.unitCost IS NOT NULL) THEN null --ignore project-level exemptions - WHEN (cr.unitCost IS NULL) THEN null --will be flagged for other reasons - WHEN (pm.multiplier IS NOT NULL) THEN null --also ignore project-level multipliers - WHEN (alias.aliasType.aliasType IS NULL) THEN null --unknown alias type, will be flagged elsewhere - WHEN (alias.aliasType.removeSubsidy = true AND COALESCE(cr.subsidy, 0) > 0) THEN 'Removed NIH Subsidy' - WHEN (alias.aliasType.canRaiseFA = true AND p.chargeId.canRaiseFA = true AND (alias.faRate IS NOT NULL AND alias.faRate < CAST(ir.IndirectRate AS DOUBLE))) THEN ('Reduced F&A: ' || CAST(alias.faRate as varchar(20))) - ELSE null - END as isNonStandardRate, - CASE WHEN (alias.alias IS NOT NULL AND alias.aliasType.aliasType IS NULL) THEN ('Unknown Type: ' || alias.aliasType) ELSE null END as isUnknownAliasType, - CASE - --handle adjustments and non-adjustments separately - WHEN (p.isAdjustment IS NULL AND coalesce(e.unitCost, cr.unitCost) is null) THEN 'Y' - WHEN (p.isAdjustment IS NOT NULL AND (coalesce(e3.unitCost, cr3.unitCost) IS NULL OR coalesce(e2.unitCost, cr2.unitCost) IS NULL)) THEN 'Y' - ELSE null - END as lacksRate, - CASE - WHEN (p.category = 'Lease Fees' or p.category = 'Lease Setup Fee' or p.category = 'Lease Setup Fees') AND e.rowid IS NULL THEN cr.rowId - ELSE null - END as rateId, - CASE - WHEN (p.category = 'Lease Fees' or p.category = 'Lease Setup Fee' or p.category = 'Lease Setup Fees') THEN e.rowid - ELSE null - END as exemptionId, - null as isMiscCharge, - p.isAdjustment, - CASE WHEN alias.alias IS NULL THEN 'Y' ELSE null END as isMissingAccount, - CASE WHEN alias.fiscalAuthority.faid IS NULL THEN 'Y' ELSE null END as isMissingFaid, - CASE - WHEN alias.aliasEnabled IS NULL THEN 'N' - WHEN alias.aliasEnabled != 'Y' THEN 'N' - ELSE null - END as isAcceptingCharges, - CASE - WHEN (alias.budgetStartDate IS NOT NULL AND CAST(alias.budgetStartDate as date) > CAST(p.date as date)) THEN 'Prior To Budget Start' - WHEN (alias.budgetEndDate IS NOT NULL AND CAST(alias.budgetEndDate as date) < CAST(p.date as date)) THEN 'After Budget End' - WHEN (alias.projectStatus IS NOT NULL AND alias.projectStatus != 'ACTIVE' AND alias.projectStatus != 'No Cost Ext' AND alias.projectStatus != 'Partial Setup') THEN 'Grant Project Not Active' - ELSE null - END as isExpiredAccount, - - CASE WHEN (TIMESTAMPDIFF('SQL_TSI_DAY', p.date, curdate()) > 45) THEN 'Y' ELSE null END as isOldCharge, - p.project.account as currentActiveAlias, - p.datefinalized, - p.enddatefinalized - -FROM onprc_billing.leaseFees p - ---the primary charge. this will be based on transaction date -LEFT JOIN onprc_billing_public.chargeRates cr ON ( - CAST(p.assignmentStart AS DATE) >= CAST(cr.startDate AS DATE) AND - (CAST(p.assignmentStart AS DATE) <= cr.enddateCoalesced OR cr.enddate IS NULL) AND - p.chargeId = cr.chargeId -) - -LEFT JOIN onprc_billing_public.chargeRateExemptions e ON ( - CAST(p.assignmentStart AS DATE) >= CAST(e.startDate AS DATE) AND - (CAST(p.assignmentStart AS DATE) <= e.enddateCoalesced OR e.enddate IS NULL) AND - p.chargeId = e.chargeId AND - p.project = e.project -) - ---the original charge (for adjustments) ---NOTE: the adjustment will use the lease end as the transaction date; however, we need to calculate this unit cost ---based on the original date of assignment -LEFT JOIN onprc_billing_public.chargeRates cr2 ON ( - CAST(p.assignmentStart AS DATE) >= CAST(cr2.startDate AS DATE) AND - (CAST(p.assignmentStart AS DATE) <= cr2.enddateCoalesced OR cr2.enddate IS NULL) AND - p.leaseCharge1 = cr2.chargeId -) - -LEFT JOIN onprc_billing_public.chargeRateExemptions e2 ON ( - CAST(p.assignmentStart AS DATE) >= CAST(e2.startDate AS DATE) AND - (CAST(p.assignmentStart AS DATE) <= e2.enddateCoalesced OR e2.enddate IS NULL) AND - p.leaseCharge1 = e2.chargeId AND - p.project = e2.project -) ---EO original charge - ---the final charge (for adjustments) ---this is the what we should have charges, based on the true release condition. ---this is also based on the date of assignment, which will differ from transaction date -LEFT JOIN onprc_billing_public.chargeRates cr3 ON ( - CAST(p.assignmentStart AS DATE) >= CAST(cr3.startDate AS DATE) AND - (CAST(p.assignmentStart AS DATE) <= cr3.enddateCoalesced OR cr3.enddate IS NULL) AND - p.leaseCharge2 = cr3.chargeId -) - -LEFT JOIN onprc_billing_public.chargeRateExemptions e3 ON ( - CAST(p.assignmentStart AS DATE) >= CAST(e3.startDate AS DATE) AND - (CAST(p.assignmentStart AS DATE) <= e3.enddateCoalesced OR e3.enddate IS NULL) AND - p.leaseCharge2 = e3.chargeId AND - p.project = e3.project -) - ---EO final charge - -LEFT JOIN onprc_billing_public.creditAccount ce ON ( - CAST(p.date AS DATE) >= CAST(ce.startDate AS DATE) AND - (CAST(p.date AS DATE) <= ce.enddateCoalesced OR ce.enddate IS NULL) AND - p.chargeId = ce.chargeId -) - -LEFT JOIN onprc_billing_public.projectAccountHistory aliasAtTime ON ( - aliasAtTime.project = p.project AND - aliasAtTime.startDate <= cast(p.date as date) AND - aliasAtTime.endDate >= cast(p.date as date) -) - -LEFT JOIN onprc_billing_public.aliases alias ON ( - aliasAtTime.account = alias.alias -) - -LEFT JOIN onprc_billing_public.projectMultipliers pm ON ( - CAST(p.date AS DATE) >= CASt(pm.startDate AS DATE) AND - (CAST(p.date AS DATE) <= pm.enddateCoalesced OR pm.enddate IS NULL) AND - alias.alias = pm.account -) -LEFT JOIN onprc_billing.ogaSynchIR ir - on ir.alias = alias.alias - -) t -where t.id.demographics.species Not IN ('Rabbit','Guinea Pig') -UNION ALL - ---add misc charges -SELECT - mc.id, - mc.date, - null as enddate, - mc.project, - mc.account, - null as projectedReleaseCondition, - null as releaseCondition, - null as assignCondition, - null as releaseType, - null as ageAtTime, - mc.category, - mc.chargeId, - mc.serviceCenter, - mc.item, - - null as leaseCharge1, - null as leaseCharge2, - mc.sourceRecord, - mc.chargeCategory, - - mc.unitcost, - mc.nihRate, - - null as unitCost1, - null as nihRate1, - null as unitCost2, - null as nihRate2, - null as unitCost3, - null as nihRate3, - - mc.quantity, - - mc.creditAccount, - mc.creditAccountId, - mc.comment, - mc.investigatorId, - mc.isExemption, - mc.isNonStandardRate, - mc.lacksRate, - mc.rateId, - mc.exemptionId, - 'Y' as isMiscCharge, - mc.isAdjustment, - mc.isMissingAccount, - mc.isMissingFaid, - mc.isAcceptingCharges, - mc.isExpiredAccount, - mc.isOldCharge, - mc.currentActiveAlias, - null as datefinalized, - null as enddatefinalized - -FROM onprc_billing.miscChargesFeeRateData mc -WHERE cast(mc.billingDate as date) >= CAST(StartDate as date) AND cast(mc.billingDate as date) <= CAST(EndDate as date) -AND mc.category IN ('Lease Fees', 'Lease Setup Fee', 'Lease Setup Fees') diff --git a/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql b/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql new file mode 100644 index 000000000..f87c23c3f --- /dev/null +++ b/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql @@ -0,0 +1,294 @@ +PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP) + +WITH +-- ===================================================================================== +-- 1) Base Lease Fees +-- ===================================================================================== +base_lease AS ( + SELECT + p.id, + p.date, + p.enddate, + p.assignmentStart, + p.project, + p.project.displayName AS projectDisplayName, + p.projectedReleaseCondition, + p.releaseCondition, + p.assignCondition, + p.releaseType, + p.ageAtTime, + p.category, + p.chargeId, + p.leaseCharge1, + p.leaseCharge2, + p.sourceRecord, + p.chargeCategory, + p.quantity, + p.isAdjustment, + p.datefinalized, + p.enddatefinalized + FROM onprc_billing.leaseFees p + WHERE CAST(p.date AS DATE) + BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE) + AND p.category = 'Lease Fees' +), + +-- ===================================================================================== +-- 2) Alias / Account Context +-- ===================================================================================== +alias_context AS ( + SELECT + bl.*, + a.alias AS account, + a.category AS aliasCategory, + a.faRate AS faRate, + a.aliasType.aliasType AS aliasType, + a.aliasType.removeSubsidy AS removeSubsidy, + a.aliasType.canRaiseFA AS canRaiseFA, + a.fiscalAuthority.faid AS faid, + a.aliasEnabled AS aliasEnabled, + a.budgetStartDate AS budgetStartDate, + a.budgetEndDate AS budgetEndDate, + a.projectStatus AS projectStatus, + COALESCE(a.investigatorId, + bl.project.investigatorId) AS investigatorId + FROM base_lease bl + LEFT JOIN onprc_billing_public.projectAccountHistory pah + ON pah.project = bl.project + AND pah.startDate <= CAST(bl.date AS DATE) + AND pah.endDate >= CAST(bl.date AS DATE) + LEFT JOIN onprc_billing_public.aliases a + ON pah.account = a.alias +), + +-- ===================================================================================== +-- 3) Rates / Exemptions / Multipliers / Indirect Rate +-- ===================================================================================== +rate_context AS ( + SELECT + ac.*, + + cr.unitCost AS nihRate1, + cr.subsidy AS subsidy1, + e.unitCost AS exemptRate1, + e.rowid AS exemptionId1, + + cr2.unitCost AS nihRate2, + cr2.subsidy AS subsidy2, + e2.unitCost AS exemptRate2, + e2.rowid AS exemptionId2, + + cr3.unitCost AS nihRate3, + cr3.subsidy AS subsidy3, + e3.unitCost AS exemptRate3, + e3.rowid AS exemptionId3, + + pm.multiplier AS multiplier, + ir.indirectRate AS indirectRate + FROM alias_context ac + + LEFT JOIN onprc_billing_public.chargeRates cr + ON ac.assignmentStart >= cr.startDate + AND (ac.assignmentStart <= cr.enddateCoalesced OR cr.enddate IS NULL) + AND ac.chargeId = cr.chargeId + + LEFT JOIN onprc_billing_public.chargeRateExemptions e + ON ac.assignmentStart >= e.startDate + AND (ac.assignmentStart <= e.enddateCoalesced OR e.enddate IS NULL) + AND ac.chargeId = e.chargeId + AND ac.project = e.project + + LEFT JOIN onprc_billing_public.chargeRates cr2 + ON ac.assignmentStart >= cr2.startDate + AND (ac.assignmentStart <= cr2.enddateCoalesced OR cr2.enddate IS NULL) + AND ac.leaseCharge1 = cr2.chargeId + + LEFT JOIN onprc_billing_public.chargeRateExemptions e2 + ON ac.assignmentStart >= e2.startDate + AND (ac.assignmentStart <= e2.enddateCoalesced OR e2.enddate IS NULL) + AND ac.leaseCharge1 = e2.chargeId + AND ac.project = e2.project + + LEFT JOIN onprc_billing_public.chargeRates cr3 + ON ac.assignmentStart >= cr3.startDate + AND (ac.assignmentStart <= cr3.enddateCoalesced OR cr3.enddate IS NULL) + AND ac.leaseCharge2 = cr3.chargeId + + LEFT JOIN onprc_billing_public.chargeRateExemptions e3 + ON ac.assignmentStart >= e3.startDate + AND (ac.assignmentStart <= e3.enddateCoalesced OR e3.enddate IS NULL) + AND ac.leaseCharge2 = e3.chargeId + AND ac.project = e3.project + + LEFT JOIN onprc_billing_public.projectMultipliers pm + ON ac.date >= pm.startDate + AND (ac.date <= pm.enddateCoalesced OR pm.enddate IS NULL) + AND ac.account = pm.account + + LEFT JOIN onprc_billing.ogaSynchIR ir + ON ir.alias = ac.account +), + +-- ===================================================================================== +-- 4) Unit Cost Calculation (Parser-Safe) +-- ===================================================================================== +calculated_costs AS ( + SELECT + rc.*, + + CAST( + CASE + WHEN exemptRate1 IS NOT NULL THEN exemptRate1 + + WHEN multiplier IS NOT NULL AND nihRate1 IS NOT NULL + THEN (CAST(nihRate1 AS DOUBLE) * CAST(multiplier AS DOUBLE)) + + WHEN nihRate1 IS NULL THEN NULL + + WHEN aliasCategory IS NOT NULL AND aliasCategory <> 'OGA' + THEN nihRate1 + + WHEN aliasType IS NULL THEN NULL + + WHEN removeSubsidy = TRUE + AND canRaiseFA = TRUE + AND chargeId.canRaiseFA = TRUE + THEN ( + (CAST(nihRate1 AS DOUBLE) / NULLIF((1 - COALESCE(subsidy1, 0)), 0)) + * + ( + 1 + + CASE + WHEN faRate IS NOT NULL + AND indirectRate IS NOT NULL + AND faRate < CAST(indirectRate AS DOUBLE) + THEN (CAST(indirectRate AS DOUBLE) / (1 + faRate)) + ELSE 0 + END + ) + ) + + WHEN removeSubsidy = TRUE AND canRaiseFA = FALSE + THEN (CAST(nihRate1 AS DOUBLE) / NULLIF((1 - COALESCE(subsidy1, 0)), 0)) + + WHEN removeSubsidy = FALSE + AND canRaiseFA = TRUE + AND chargeId.canRaiseFA = TRUE + THEN ( + CAST(nihRate1 AS DOUBLE) + * + ( + 1 + + CASE + WHEN faRate IS NOT NULL + AND indirectRate IS NOT NULL + AND faRate < CAST(indirectRate AS DOUBLE) + THEN ((CAST(indirectRate AS DOUBLE) - faRate) / (1 + faRate)) + ELSE 0 + END + ) + ) + + ELSE nihRate1 + END + AS DOUBLE) AS unitCost + FROM rate_context rc +), + +-- ===================================================================================== +-- 5) Final Lease Charges (Billing Review) +-- ===================================================================================== +lease_final AS ( + SELECT + id, + date AS assignmentStartDate, + enddate AS assignmentEndDate, + datefinalized AS dateFinalized, + project, + account, + projectedReleaseCondition, + releaseCondition, + assignCondition, + releaseType, + ageAtTime, + category, + chargeId, + chargeId.departmentCode AS serviceCenter, + chargeId.name AS item, + leaseCharge1.name as InitialLEaseType, + leaseCharge2.name as FinalLEaseType, + sourceRecord, + chargeCategory, + + unitCost, + quantity, + + CASE + WHEN isAdjustment IS NOT NULL + THEN ROUND(CAST(unitCost AS DOUBLE), 2) + ELSE + ROUND( + CAST(unitCost AS DOUBLE) + * COALESCE(CAST(quantity AS DOUBLE), 1), + 2 + ) + END AS totalCost, + + investigatorId, + + NULL AS isMiscCharge, + isAdjustment + FROM calculated_costs + WHERE id.demographics.species NOT IN ('Rabbit','Guinea Pig') +), + +-- ===================================================================================== +-- 6) Misc Charges (Billing Review) +-- ===================================================================================== +misc_charges AS ( + SELECT + mc.id, + mc.billingDate AS assignmentStartDate, + NULL AS assignmentEndDate, + NULL AS dateFinalized, + mc.project, + mc.account, + NULL AS projectedReleaseCondition, + NULL AS releaseCondition, + NULL AS assignCondition, + NULL AS releaseType, + NULL AS ageAtTime, + mc.category, + mc.chargeId, + mc.serviceCenter, + mc.item, + NULL AS leaseCharge1, + NULL AS leaseCharge2, + mc.sourceRecord, + mc.chargeCategory, + + mc.unitCost, + mc.quantity, + + ROUND( + CAST(mc.unitCost AS DOUBLE) + * COALESCE(CAST(mc.quantity AS DOUBLE), 1), + 2 + ) AS totalCost, + + mc.investigatorId, + + 'Y' AS isMiscCharge, + mc.isAdjustment + FROM onprc_billing.miscChargesFeeRateData mc + WHERE CAST(mc.billingDate AS DATE) + BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE) + AND mc.category = 'Lease Fees' +) + +-- ===================================================================================== +-- FINAL BILLING-REVIEW OUTPUT +-- ===================================================================================== +SELECT * FROM lease_final +UNION ALL +SELECT * FROM misc_charges; From 5fd36548bf6068a2972c04b432afe172d4a03bf6 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Fri, 19 Dec 2025 10:45:18 -0700 Subject: [PATCH 17/26] Update of Lease Fee Rates to using CTEs and eliminate the mutltiple queries that were needed in original query --- .../resources/queries/onprc_billing/leasefeerates.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql b/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql index f87c23c3f..e78040931 100644 --- a/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql +++ b/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql @@ -130,7 +130,7 @@ rate_context AS ( ), -- ===================================================================================== --- 4) Unit Cost Calculation (Parser-Safe) +-- 4) Unit Cost Calculation -- ===================================================================================== calculated_costs AS ( SELECT @@ -287,7 +287,7 @@ misc_charges AS ( ) -- ===================================================================================== --- FINAL BILLING-REVIEW OUTPUT +-- FINAL BILLING-REVIEW OUTPUT Missing 8 Fields needded for Daily Notification -- ===================================================================================== SELECT * FROM lease_final UNION ALL From 99d1afc8dae069e9fbaf564f19acc73c71b146fb Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 22 Dec 2025 15:04:34 -0700 Subject: [PATCH 18/26] Update removing use of Select * as it is not a recommended method --- .../queries/onprc_billing/leasefeerates.sql | 198 +++++++++++++----- 1 file changed, 147 insertions(+), 51 deletions(-) diff --git a/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql b/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql index e78040931..10d072660 100644 --- a/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql +++ b/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql @@ -28,8 +28,7 @@ base_lease AS ( p.datefinalized, p.enddatefinalized FROM onprc_billing.leaseFees p - WHERE CAST(p.date AS DATE) - BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE) + WHERE CAST(p.date AS DATE) BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE) AND p.category = 'Lease Fees' ), @@ -50,8 +49,7 @@ alias_context AS ( a.budgetStartDate AS budgetStartDate, a.budgetEndDate AS budgetEndDate, a.projectStatus AS projectStatus, - COALESCE(a.investigatorId, - bl.project.investigatorId) AS investigatorId + COALESCE(a.investigatorId, bl.project.investigatorId) AS investigatorId FROM base_lease bl LEFT JOIN onprc_billing_public.projectAccountHistory pah ON pah.project = bl.project @@ -68,23 +66,23 @@ rate_context AS ( SELECT ac.*, - cr.unitCost AS nihRate1, - cr.subsidy AS subsidy1, - e.unitCost AS exemptRate1, - e.rowid AS exemptionId1, + cr.unitCost AS nihRate1, + cr.subsidy AS subsidy1, + e.unitCost AS exemptRate1, + e.rowid AS exemptionId1, - cr2.unitCost AS nihRate2, - cr2.subsidy AS subsidy2, - e2.unitCost AS exemptRate2, - e2.rowid AS exemptionId2, + cr2.unitCost AS nihRate2, + cr2.subsidy AS subsidy2, + e2.unitCost AS exemptRate2, + e2.rowid AS exemptionId2, - cr3.unitCost AS nihRate3, - cr3.subsidy AS subsidy3, - e3.unitCost AS exemptRate3, - e3.rowid AS exemptionId3, + cr3.unitCost AS nihRate3, + cr3.subsidy AS subsidy3, + e3.unitCost AS exemptRate3, + e3.rowid AS exemptionId3, - pm.multiplier AS multiplier, - ir.indirectRate AS indirectRate + pm.multiplier AS multiplier, + ir.indirectRate AS indirectRate FROM alias_context ac LEFT JOIN onprc_billing_public.chargeRates cr @@ -135,7 +133,6 @@ rate_context AS ( calculated_costs AS ( SELECT rc.*, - CAST( CASE WHEN exemptRate1 IS NOT NULL THEN exemptRate1 @@ -196,31 +193,39 @@ calculated_costs AS ( ), -- ===================================================================================== --- 5) Final Lease Charges (Billing Review) +-- 5) Final Lease Charges -- ===================================================================================== lease_final AS ( SELECT id, - date AS assignmentStartDate, - enddate AS assignmentEndDate, - datefinalized AS dateFinalized, + date, + enddate, + datefinalized AS dateFinalized, + project, account, + projectedReleaseCondition, releaseCondition, assignCondition, releaseType, ageAtTime, + category, chargeId, - chargeId.departmentCode AS serviceCenter, - chargeId.name AS item, - leaseCharge1.name as InitialLEaseType, - leaseCharge2.name as FinalLEaseType, + chargeId.departmentCode AS serviceCenter, + chargeId.name AS item, + + leaseCharge1, + leaseCharge2, + leaseCharge1.name AS InitialLeaseType, + leaseCharge2.name AS FinalLeaseType, + sourceRecord, chargeCategory, unitCost, + nihRate1 AS nihRate, quantity, CASE @@ -228,67 +233,158 @@ lease_final AS ( THEN ROUND(CAST(unitCost AS DOUBLE), 2) ELSE ROUND( - CAST(unitCost AS DOUBLE) - * COALESCE(CAST(quantity AS DOUBLE), 1), + CAST(unitCost AS DOUBLE) * COALESCE(CAST(quantity AS DOUBLE), 1), 2 ) END AS totalCost, investigatorId, - NULL AS isMiscCharge, + CAST(NULL AS VARCHAR(200)) AS creditAccount, + CAST(NULL AS VARCHAR(200)) AS creditAccountType, + CAST(NULL AS VARCHAR(4000)) AS comment, + CAST(NULL AS INTEGER) AS creditAccountId, + + CAST(NULL AS INTEGER) AS rateId, + exemptionId1 AS exemptionId, + + CAST(NULL AS VARCHAR(1)) AS isMiscCharge, isAdjustment FROM calculated_costs - WHERE id.demographics.species NOT IN ('Rabbit','Guinea Pig') + WHERE id.demographics.species NOT IN ('Rabbit', 'Guinea Pig') ), -- ===================================================================================== --- 6) Misc Charges (Billing Review) +-- 6) Misc Charges (Lease Fees category) -- ===================================================================================== misc_charges AS ( SELECT mc.id, - mc.billingDate AS assignmentStartDate, - NULL AS assignmentEndDate, - NULL AS dateFinalized, + mc.billingDate AS date, + CAST(NULL AS TIMESTAMP) AS enddate, + CAST(NULL AS TIMESTAMP) AS dateFinalized, + mc.project, mc.account, - NULL AS projectedReleaseCondition, - NULL AS releaseCondition, - NULL AS assignCondition, - NULL AS releaseType, - NULL AS ageAtTime, + + CAST(NULL AS VARCHAR(200)) AS projectedReleaseCondition, + CAST(NULL AS VARCHAR(200)) AS releaseCondition, + CAST(NULL AS VARCHAR(200)) AS assignCondition, + CAST(NULL AS VARCHAR(200)) AS releaseType, + CAST(NULL AS DOUBLE) AS ageAtTime, + mc.category, mc.chargeId, mc.serviceCenter, mc.item, - NULL AS leaseCharge1, - NULL AS leaseCharge2, + + CAST(NULL AS INTEGER) AS leaseCharge1, + CAST(NULL AS INTEGER) AS leaseCharge2, + CAST(NULL AS VARCHAR(200)) AS InitialLeaseType, + CAST(NULL AS VARCHAR(200)) AS FinalLeaseType, + mc.sourceRecord, mc.chargeCategory, mc.unitCost, + mc.nihRate AS nihRate, mc.quantity, ROUND( - CAST(mc.unitCost AS DOUBLE) - * COALESCE(CAST(mc.quantity AS DOUBLE), 1), + CAST(mc.unitCost AS DOUBLE) * COALESCE(CAST(mc.quantity AS DOUBLE), 1), 2 ) AS totalCost, mc.investigatorId, - 'Y' AS isMiscCharge, - mc.isAdjustment + mc.creditAccount AS creditAccount, + mc.creditAccountType AS creditAccountType, + mc.comment AS comment, + mc.creditAccountId AS creditAccountId, + + mc.rateId AS rateId, + mc.exemptionId AS exemptionId, + + 'Y' AS isMiscCharge, + mc.isAdjustment AS isAdjustment FROM onprc_billing.miscChargesFeeRateData mc - WHERE CAST(mc.billingDate AS DATE) - BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE) + WHERE CAST(mc.billingDate AS DATE) BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE) AND mc.category = 'Lease Fees' ) -- ===================================================================================== --- FINAL BILLING-REVIEW OUTPUT Missing 8 Fields needded for Daily Notification +-- FINAL OUTPUT (explicit column list; no SELECT *) -- ===================================================================================== -SELECT * FROM lease_final +SELECT + id, + date, + enddate, + dateFinalized, + project, + account, + projectedReleaseCondition, + releaseCondition, + assignCondition, + releaseType, + ageAtTime, + category, + chargeId, + serviceCenter, + item, + leaseCharge1, + leaseCharge2, + InitialLeaseType, + FinalLeaseType, + sourceRecord, + chargeCategory, + unitCost, + nihRate, + quantity, + totalCost, + investigatorId, + creditAccount, + creditAccountType, + comment, + creditAccountId, + rateId, + exemptionId, + isMiscCharge, + isAdjustment +FROM lease_final UNION ALL -SELECT * FROM misc_charges; +SELECT + id, + date, + enddate, + dateFinalized, + project, + account, + projectedReleaseCondition, + releaseCondition, + assignCondition, + releaseType, + ageAtTime, + category, + chargeId, + serviceCenter, + item, + leaseCharge1, + leaseCharge2, + InitialLeaseType, + FinalLeaseType, + sourceRecord, + chargeCategory, + unitCost, + nihRate, + quantity, + totalCost, + investigatorId, + creditAccount, + creditAccountType, + comment, + creditAccountId, + rateId, + exemptionId, + isMiscCharge, + isAdjustment +FROM misc_charges; From 05ab92f8a129a4da74b75f0d5d3ca49332bfe61d Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Wed, 24 Dec 2025 10:49:21 -0700 Subject: [PATCH 19/26] Updated to incldue a credit account field in the final output. WIll need to add the logic for populating this field --- .../queries/onprc_billing/leasefeerates.sql | 220 ++++++------------ 1 file changed, 70 insertions(+), 150 deletions(-) diff --git a/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql b/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql index 10d072660..e307d7ec2 100644 --- a/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql +++ b/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql @@ -28,7 +28,8 @@ base_lease AS ( p.datefinalized, p.enddatefinalized FROM onprc_billing.leaseFees p - WHERE CAST(p.date AS DATE) BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE) + WHERE CAST(p.date AS DATE) + BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE) AND p.category = 'Lease Fees' ), @@ -38,6 +39,7 @@ base_lease AS ( alias_context AS ( SELECT bl.*, + da.DualProjectCategory, a.alias AS account, a.category AS aliasCategory, a.faRate AS faRate, @@ -49,7 +51,8 @@ alias_context AS ( a.budgetStartDate AS budgetStartDate, a.budgetEndDate AS budgetEndDate, a.projectStatus AS projectStatus, - COALESCE(a.investigatorId, bl.project.investigatorId) AS investigatorId + COALESCE(a.investigatorId, + bl.project.investigatorId) AS investigatorId FROM base_lease bl LEFT JOIN onprc_billing_public.projectAccountHistory pah ON pah.project = bl.project @@ -57,6 +60,13 @@ alias_context AS ( AND pah.endDate >= CAST(bl.date AS DATE) LEFT JOIN onprc_billing_public.aliases a ON pah.account = a.alias + Left JOIN study.dualAssigned da + on bl.id = da.id + AND da.dualendDate >= CAST(bl.date AS DATE) + AND da.dualstartDate <= CAST(bl.date AS DATE) + and da.DualProjectCategory in ('U42','JMAC Colony(P51)') + + ), -- ===================================================================================== @@ -66,23 +76,23 @@ rate_context AS ( SELECT ac.*, - cr.unitCost AS nihRate1, - cr.subsidy AS subsidy1, - e.unitCost AS exemptRate1, - e.rowid AS exemptionId1, + cr.unitCost AS nihRate1, + cr.subsidy AS subsidy1, + e.unitCost AS exemptRate1, + e.rowid AS exemptionId1, - cr2.unitCost AS nihRate2, - cr2.subsidy AS subsidy2, - e2.unitCost AS exemptRate2, - e2.rowid AS exemptionId2, + cr2.unitCost AS nihRate2, + cr2.subsidy AS subsidy2, + e2.unitCost AS exemptRate2, + e2.rowid AS exemptionId2, - cr3.unitCost AS nihRate3, - cr3.subsidy AS subsidy3, - e3.unitCost AS exemptRate3, - e3.rowid AS exemptionId3, + cr3.unitCost AS nihRate3, + cr3.subsidy AS subsidy3, + e3.unitCost AS exemptRate3, + e3.rowid AS exemptionId3, - pm.multiplier AS multiplier, - ir.indirectRate AS indirectRate + pm.multiplier AS multiplier, + ir.indirectRate AS indirectRate FROM alias_context ac LEFT JOIN onprc_billing_public.chargeRates cr @@ -128,11 +138,12 @@ rate_context AS ( ), -- ===================================================================================== --- 4) Unit Cost Calculation +-- 4) Unit Cost Calculation (Parser-Safe) -- ===================================================================================== calculated_costs AS ( SELECT rc.*, + CAST( CASE WHEN exemptRate1 IS NOT NULL THEN exemptRate1 @@ -193,39 +204,36 @@ calculated_costs AS ( ), -- ===================================================================================== --- 5) Final Lease Charges +-- 5) Final Lease Charges (Billing Review) -- ===================================================================================== +-- ... existing code ... + lease_final AS ( SELECT id, - date, - enddate, - datefinalized AS dateFinalized, - + date AS assignmentStartDate, + enddate AS assignmentEndDate, + datefinalized AS dateFinalized, project, account, - projectedReleaseCondition, releaseCondition, assignCondition, releaseType, ageAtTime, - category, chargeId, - chargeId.departmentCode AS serviceCenter, - chargeId.name AS item, - - leaseCharge1, - leaseCharge2, - leaseCharge1.name AS InitialLeaseType, - leaseCharge2.name AS FinalLeaseType, - + chargeId.departmentCode AS serviceCenter, + chargeId.name AS item, + leaseCharge1.name as InitialLEaseType, + leaseCharge2.name as FinalLEaseType, sourceRecord, chargeCategory, + CAST(NULL AS VARCHAR) AS creditAccount, + CAST(NULL AS INTEGER) AS creditAccountId, + unitCost, - nihRate1 AS nihRate, quantity, CASE @@ -233,158 +241,70 @@ lease_final AS ( THEN ROUND(CAST(unitCost AS DOUBLE), 2) ELSE ROUND( - CAST(unitCost AS DOUBLE) * COALESCE(CAST(quantity AS DOUBLE), 1), + CAST(unitCost AS DOUBLE) + * COALESCE(CAST(quantity AS DOUBLE), 1), 2 ) END AS totalCost, investigatorId, - CAST(NULL AS VARCHAR(200)) AS creditAccount, - CAST(NULL AS VARCHAR(200)) AS creditAccountType, - CAST(NULL AS VARCHAR(4000)) AS comment, - CAST(NULL AS INTEGER) AS creditAccountId, - - CAST(NULL AS INTEGER) AS rateId, - exemptionId1 AS exemptionId, - - CAST(NULL AS VARCHAR(1)) AS isMiscCharge, + NULL AS isMiscCharge, isAdjustment FROM calculated_costs - WHERE id.demographics.species NOT IN ('Rabbit', 'Guinea Pig') + WHERE id.demographics.species NOT IN ('Rabbit','Guinea Pig') ), --- ===================================================================================== --- 6) Misc Charges (Lease Fees category) --- ===================================================================================== +-- ... existing code ... + misc_charges AS ( SELECT mc.id, - mc.billingDate AS date, - CAST(NULL AS TIMESTAMP) AS enddate, - CAST(NULL AS TIMESTAMP) AS dateFinalized, - + mc.billingDate AS assignmentStartDate, + NULL AS assignmentEndDate, + NULL AS dateFinalized, mc.project, mc.account, - - CAST(NULL AS VARCHAR(200)) AS projectedReleaseCondition, - CAST(NULL AS VARCHAR(200)) AS releaseCondition, - CAST(NULL AS VARCHAR(200)) AS assignCondition, - CAST(NULL AS VARCHAR(200)) AS releaseType, - CAST(NULL AS DOUBLE) AS ageAtTime, - + NULL AS projectedReleaseCondition, + NULL AS releaseCondition, + NULL AS assignCondition, + NULL AS releaseType, + NULL AS ageAtTime, mc.category, mc.chargeId, mc.serviceCenter, mc.item, - - CAST(NULL AS INTEGER) AS leaseCharge1, - CAST(NULL AS INTEGER) AS leaseCharge2, - CAST(NULL AS VARCHAR(200)) AS InitialLeaseType, - CAST(NULL AS VARCHAR(200)) AS FinalLeaseType, - + NULL AS leaseCharge1, + NULL AS leaseCharge2, mc.sourceRecord, mc.chargeCategory, + mc.creditAccount AS creditAccount, + mc.creditAccountId AS creditAccountId, + mc.unitCost, - mc.nihRate AS nihRate, mc.quantity, ROUND( - CAST(mc.unitCost AS DOUBLE) * COALESCE(CAST(mc.quantity AS DOUBLE), 1), + CAST(mc.unitCost AS DOUBLE) + * COALESCE(CAST(mc.quantity AS DOUBLE), 1), 2 ) AS totalCost, mc.investigatorId, - mc.creditAccount AS creditAccount, - mc.creditAccountType AS creditAccountType, - mc.comment AS comment, - mc.creditAccountId AS creditAccountId, - - mc.rateId AS rateId, - mc.exemptionId AS exemptionId, - - 'Y' AS isMiscCharge, - mc.isAdjustment AS isAdjustment + 'Y' AS isMiscCharge, + mc.isAdjustment FROM onprc_billing.miscChargesFeeRateData mc - WHERE CAST(mc.billingDate AS DATE) BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE) + WHERE CAST(mc.billingDate AS DATE) + BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE) AND mc.category = 'Lease Fees' ) +-- ... existing code ... -- ===================================================================================== --- FINAL OUTPUT (explicit column list; no SELECT *) +-- FINAL BILLING-REVIEW OUTPUT -- ===================================================================================== -SELECT - id, - date, - enddate, - dateFinalized, - project, - account, - projectedReleaseCondition, - releaseCondition, - assignCondition, - releaseType, - ageAtTime, - category, - chargeId, - serviceCenter, - item, - leaseCharge1, - leaseCharge2, - InitialLeaseType, - FinalLeaseType, - sourceRecord, - chargeCategory, - unitCost, - nihRate, - quantity, - totalCost, - investigatorId, - creditAccount, - creditAccountType, - comment, - creditAccountId, - rateId, - exemptionId, - isMiscCharge, - isAdjustment -FROM lease_final +SELECT * FROM lease_final UNION ALL -SELECT - id, - date, - enddate, - dateFinalized, - project, - account, - projectedReleaseCondition, - releaseCondition, - assignCondition, - releaseType, - ageAtTime, - category, - chargeId, - serviceCenter, - item, - leaseCharge1, - leaseCharge2, - InitialLeaseType, - FinalLeaseType, - sourceRecord, - chargeCategory, - unitCost, - nihRate, - quantity, - totalCost, - investigatorId, - creditAccount, - creditAccountType, - comment, - creditAccountId, - rateId, - exemptionId, - isMiscCharge, - isAdjustment -FROM misc_charges; +SELECT * FROM misc_charges; From 53095e9b47e29aacb539bcea9db96bc9e4785ffc Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Wed, 24 Dec 2025 12:59:26 -0700 Subject: [PATCH 20/26] Update to Finance to include table for projects receiving credit for leases and addition to Finance Main Board to allow administration --- .../sqlserver/onprc_billing-25.006-25.007.sql | 8 +++++++ .../resources/schemas/onprc_billing.xml | 23 +++++++++++++++++++ .../resources/views/financeManagement.html | 2 +- .../onprc_billing/ONPRC_BillingModule.java | 2 +- 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 onprc_billing/resources/schemas/dbscripts/sqlserver/onprc_billing-25.006-25.007.sql diff --git a/onprc_billing/resources/schemas/dbscripts/sqlserver/onprc_billing-25.006-25.007.sql b/onprc_billing/resources/schemas/dbscripts/sqlserver/onprc_billing-25.006-25.007.sql new file mode 100644 index 000000000..33347b754 --- /dev/null +++ b/onprc_billing/resources/schemas/dbscripts/sqlserver/onprc_billing-25.006-25.007.sql @@ -0,0 +1,8 @@ +CREATE TABLE onprc_billing.ProjectLeaseIncomeEligibility ( + rowId INT IDENTITY(1,1) NOT NULL, + project INT NOT NULL, + startDate DATE NOT NULL, + endDate DATE NULL, + comment NVARCHAR(4000) NULL, + CONSTRAINT PK_ProjectLeaseIncomeEligibility PRIMARY KEY (rowId) +); \ No newline at end of file diff --git a/onprc_billing/resources/schemas/onprc_billing.xml b/onprc_billing/resources/schemas/onprc_billing.xml index 2bb09f73b..20e462d92 100644 --- a/onprc_billing/resources/schemas/onprc_billing.xml +++ b/onprc_billing/resources/schemas/onprc_billing.xml @@ -1641,4 +1641,27 @@ + + + + + + ehr + project + project_id + display_name + + + + + + + + + + + + +
\ No newline at end of file diff --git a/onprc_billing/resources/views/financeManagement.html b/onprc_billing/resources/views/financeManagement.html index c7d05adbc..ef8293e6b 100644 --- a/onprc_billing/resources/views/financeManagement.html +++ b/onprc_billing/resources/views/financeManagement.html @@ -81,7 +81,7 @@ {name: 'SLA Per Diem Fee Structure', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'slaPerDiemFeeDefinition'})}, {name: 'ONPRC Annual Rate Change', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'AnnualRateChange',showImport: true})}, {name: 'ONPRC Indirect Rate', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'IndirectRates',showImport: true})}, /*Added to provide access to indirect Rate*/ - + {name: 'Project receive Credit for Lease', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'leaseIncomeEligibleProjects',showImport: true})}, /*Added to provide access to indirect Rate*/ ] }] }] diff --git a/onprc_billing/src/org/labkey/onprc_billing/ONPRC_BillingModule.java b/onprc_billing/src/org/labkey/onprc_billing/ONPRC_BillingModule.java index d46499053..ca944815f 100644 --- a/onprc_billing/src/org/labkey/onprc_billing/ONPRC_BillingModule.java +++ b/onprc_billing/src/org/labkey/onprc_billing/ONPRC_BillingModule.java @@ -83,7 +83,7 @@ public String getName() @Override public @Nullable Double getSchemaVersion() { - return 25.006; + return 25.007; } @Override From 97f80c9eac57878580595b5aa87e6decf1fd8a62 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Fri, 26 Dec 2025 13:29:21 -0700 Subject: [PATCH 21/26] Update to Finance Management html to point to the correct tbale for Project Lease Income Eligibility --- onprc_billing/resources/views/financeManagement.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onprc_billing/resources/views/financeManagement.html b/onprc_billing/resources/views/financeManagement.html index ef8293e6b..26fbed34c 100644 --- a/onprc_billing/resources/views/financeManagement.html +++ b/onprc_billing/resources/views/financeManagement.html @@ -81,7 +81,7 @@ {name: 'SLA Per Diem Fee Structure', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'slaPerDiemFeeDefinition'})}, {name: 'ONPRC Annual Rate Change', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'AnnualRateChange',showImport: true})}, {name: 'ONPRC Indirect Rate', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'IndirectRates',showImport: true})}, /*Added to provide access to indirect Rate*/ - {name: 'Project receive Credit for Lease', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'leaseIncomeEligibleProjects',showImport: true})}, /*Added to provide access to indirect Rate*/ + {name: 'Project receive Credit for Lease', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'ProjectLeaseIncomeEligibility',showImport: true})}, /*update to onpen for defining projects*/ ] }] }] From 77d015d259669c598edcca2aea9a33d6e708924f Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 29 Dec 2025 08:10:37 -0700 Subject: [PATCH 22/26] Correction of issue in onprc_billing.xml and finance managment --- .../queries/onprc_billing/leasefeerates.sql | 25 +++++++++++++------ .../resources/schemas/onprc_billing.xml | 2 +- .../resources/views/financeManagement.html | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql b/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql index e307d7ec2..2c2c1bb74 100644 --- a/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql +++ b/onprc_billing/resources/queries/onprc_billing/leasefeerates.sql @@ -35,11 +35,13 @@ base_lease AS ( -- ===================================================================================== -- 2) Alias / Account Context + -- ===================================================================================== alias_context AS ( SELECT bl.*, da.DualProjectCategory, + da.project1 as InitialProject, a.alias AS account, a.category AS aliasCategory, a.faRate AS faRate, @@ -54,6 +56,7 @@ alias_context AS ( COALESCE(a.investigatorId, bl.project.investigatorId) AS investigatorId FROM base_lease bl + LEFT JOIN onprc_billing_public.projectAccountHistory pah ON pah.project = bl.project AND pah.startDate <= CAST(bl.date AS DATE) @@ -64,17 +67,21 @@ alias_context AS ( on bl.id = da.id AND da.dualendDate >= CAST(bl.date AS DATE) AND da.dualstartDate <= CAST(bl.date AS DATE) - and da.DualProjectCategory in ('U42','JMAC Colony(P51)') +--Select * from alias_context ), - -- ===================================================================================== +-- 2a lookup to determine credit to for each lease fee +-- when an assignment creates a dual assignment and the original assignment is an + -- 3) Rates / Exemptions / Multipliers / Indirect Rate -- ===================================================================================== + rate_context AS ( SELECT ac.*, + LI.project as CreditTo, cr.unitCost AS nihRate1, cr.subsidy AS subsidy1, @@ -135,9 +142,12 @@ rate_context AS ( LEFT JOIN onprc_billing.ogaSynchIR ir ON ir.alias = ac.account + Left Join onprc_Billing.ProjectLeaseIncomeEligibility Li + on ac.InitialProject = Li.Project ), --- ===================================================================================== + + -- ===================================================================================== -- 4) Unit Cost Calculation (Parser-Safe) -- ===================================================================================== calculated_costs AS ( @@ -202,11 +212,11 @@ calculated_costs AS ( AS DOUBLE) AS unitCost FROM rate_context rc ), - +--Select * from calculated_costs -- ===================================================================================== -- 5) Final Lease Charges (Billing Review) -- ===================================================================================== --- ... existing code ... + lease_final AS ( SELECT @@ -229,8 +239,9 @@ lease_final AS ( leaseCharge2.name as FinalLEaseType, sourceRecord, chargeCategory, - - CAST(NULL AS VARCHAR) AS creditAccount, +-- Add the action to get the correct alias for the credit account +-- This is based on whether there is a value in Credit to field + CAST(creditto as varchar) AS creditAccount, CAST(NULL AS INTEGER) AS creditAccountId, unitCost, diff --git a/onprc_billing/resources/schemas/onprc_billing.xml b/onprc_billing/resources/schemas/onprc_billing.xml index 20e462d92..4d8e21a75 100644 --- a/onprc_billing/resources/schemas/onprc_billing.xml +++ b/onprc_billing/resources/schemas/onprc_billing.xml @@ -1662,6 +1662,6 @@ - \ No newline at end of file diff --git a/onprc_billing/resources/views/financeManagement.html b/onprc_billing/resources/views/financeManagement.html index 26fbed34c..821ac06ac 100644 --- a/onprc_billing/resources/views/financeManagement.html +++ b/onprc_billing/resources/views/financeManagement.html @@ -81,7 +81,7 @@ {name: 'SLA Per Diem Fee Structure', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'slaPerDiemFeeDefinition'})}, {name: 'ONPRC Annual Rate Change', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'AnnualRateChange',showImport: true})}, {name: 'ONPRC Indirect Rate', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'IndirectRates',showImport: true})}, /*Added to provide access to indirect Rate*/ - {name: 'Project receive Credit for Lease', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'ProjectLeaseIncomeEligibility',showImport: true})}, /*update to onpen for defining projects*/ + {name: 'Project receive Credit for Lease', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'ProjectLeaseIncomeEligibility',showImport: true})}, /*update to Define who receives lease fee credit*/ ] }] }] From 44d74d501dafd987f59b87be643b344295f6a8f6 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 29 Dec 2025 09:04:01 -0700 Subject: [PATCH 23/26] Removed undefined fields from onprc_billing.xml --- onprc_billing/resources/schemas/onprc_billing.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/onprc_billing/resources/schemas/onprc_billing.xml b/onprc_billing/resources/schemas/onprc_billing.xml index 4d8e21a75..3ce878907 100644 --- a/onprc_billing/resources/schemas/onprc_billing.xml +++ b/onprc_billing/resources/schemas/onprc_billing.xml @@ -1655,9 +1655,6 @@ - - - From 431cff6d71afac05d22e6656fa52c977369b90c7 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 29 Dec 2025 10:13:34 -0700 Subject: [PATCH 24/26] Removed undefined fields from onprc_billing.xml --- onprc_billing/resources/schemas/onprc_billing.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onprc_billing/resources/schemas/onprc_billing.xml b/onprc_billing/resources/schemas/onprc_billing.xml index 3ce878907..62afb780a 100644 --- a/onprc_billing/resources/schemas/onprc_billing.xml +++ b/onprc_billing/resources/schemas/onprc_billing.xml @@ -1655,7 +1655,7 @@ - + From 49112d4b73bd06ecda34f33aa8a4bf94d6d27f7c Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 29 Dec 2025 10:17:43 -0700 Subject: [PATCH 25/26] Removed undefined fields from onprc_billing.xml --- onprc_billing/resources/schemas/onprc_billing.xml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/onprc_billing/resources/schemas/onprc_billing.xml b/onprc_billing/resources/schemas/onprc_billing.xml index 62afb780a..d425c2afa 100644 --- a/onprc_billing/resources/schemas/onprc_billing.xml +++ b/onprc_billing/resources/schemas/onprc_billing.xml @@ -1641,7 +1641,7 @@ - +
@@ -1655,9 +1655,6 @@ - - -
From 7fdd4193fdcebcb08388bafcaa8f311bc1eb1aba Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Wed, 31 Dec 2025 13:17:00 -0700 Subject: [PATCH 26/26] Added a linked schema to allow Finacne to view LEase Fee Eligibility --- .../resources/schemas/pf_publicFinance.template.xml | 5 +++++ onprc_billing/resources/views/financeManagement.html | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/onprc_billing/resources/schemas/pf_publicFinance.template.xml b/onprc_billing/resources/schemas/pf_publicFinance.template.xml index e3c842a20..4a3b73524 100644 --- a/onprc_billing/resources/schemas/pf_publicFinance.template.xml +++ b/onprc_billing/resources/schemas/pf_publicFinance.template.xml @@ -4,6 +4,7 @@ aliases dataAccess projectAccountHistory + ProjectLeaseIncomeEligibility @@ -20,6 +21,10 @@ Project Account History + + + Project Lease Income Eligibility +
\ No newline at end of file diff --git a/onprc_billing/resources/views/financeManagement.html b/onprc_billing/resources/views/financeManagement.html index 821ac06ac..f0b75d53d 100644 --- a/onprc_billing/resources/views/financeManagement.html +++ b/onprc_billing/resources/views/financeManagement.html @@ -81,7 +81,7 @@ {name: 'SLA Per Diem Fee Structure', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'slaPerDiemFeeDefinition'})}, {name: 'ONPRC Annual Rate Change', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'AnnualRateChange',showImport: true})}, {name: 'ONPRC Indirect Rate', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'IndirectRates',showImport: true})}, /*Added to provide access to indirect Rate*/ - {name: 'Project receive Credit for Lease', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'onprc_billing', 'query.queryName': 'ProjectLeaseIncomeEligibility',showImport: true})}, /*update to Define who receives lease fee credit*/ + {name: 'Project receive Credit for Lease', url: LABKEY.ActionURL.buildURL(queryController, queryAction, null, {schemaName: 'pf_publicFinance', 'query.queryName': 'ProjectLeaseIncomeEligibility',showImport: true})}, /*update to Define who receives lease fee credit*/ ] }] }]