Skip to content

Commit 6c6dafe

Browse files
authored
Fix PI-purchased NHP flag date check (#1684)
* Fix PI-purchased NHP flag date check * Conform better to existing style * Revert quantity calc for adjustment
1 parent 01f1e80 commit 6c6dafe

1 file changed

Lines changed: 29 additions & 21 deletions

File tree

onprc_billing/resources/queries/onprc_billing/leaseFees.sql

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ When a5.id is not Null
5656
then (Select c.rowid from Site.{substitutePath moduleProperty('ONPRC_Billing','BillingContainer')}.onprc_billing.chargeableItems c where c.itemCode = 'ONR41')
5757
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)
5858
then (Select c.rowid from Site.{substitutePath moduleProperty('ONPRC_Billing','BillingContainer')}.onprc_billing.chargeableItems c where c.itemCode = 'ONR44')
59-
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 rowid FROM onprc_billing_public.chargeableItems ci WHERE ci.active = true AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_NAME'))
60-
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 rowid FROM onprc_billing_public.chargeableItems ci WHERE ci.active = true AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_NAME'))
59+
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 rowid FROM onprc_billing_public.chargeableItems ci WHERE ci.active = true AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.DAY_LEASE_NAME'))
6160
WHEN a2.id IS NOT NULL THEN (SELECT rowid FROM onprc_billing_public.chargeableItems ci WHERE (ci.startDate <= a.date and ci.endDate >= a.date) AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.TMB_LEASE_NAME'))
6261
ELSE lf.chargeId
6362
END as chargeId,
@@ -81,7 +80,8 @@ CASE
8180
where b.id = a.id and a1.project.protocol = a2.project.protocol) > 0 THEN 0
8281

8382
WHEN (a.duration = 0 AND a.enddate IS NOT NULL AND a.assignCondition = a.releaseCondition) THEN 1
84-
WHEN (fl.id Is Not Null) THEN 0
83+
-- Exempt PI-purchased NHPs from lease fees
84+
WHEN (fl.id IS NOT NULL) THEN 0
8585
--This will check for infants born to resource moms and will not charge
8686

8787
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
@@ -139,21 +139,20 @@ LEFT JOIN onprc_billing.leaseFeeDefinition lf ON (
139139
AND lf.active = true
140140
)
141141

142-
143-
144-
--adds the reasearch owned animal exemption
145-
LEFT JOIN study.flags fl on
146-
(a.id = fl.id
147-
and fl.flag.code = 4034
148-
and (a.date >= fl.date and a.date <=COALESCE(fl.enddate,Now()) ))
149-
142+
-- Add PI-purchased NHP flag data
143+
LEFT JOIN study.flags fl
144+
ON (a.id = fl.id
145+
AND fl.flag.code = 4034
146+
AND a.date BETWEEN CAST(fl.date AS DATE) AND COALESCE(fl.enddate, now())
147+
)
150148

151149
WHERE CAST(a.datefinalized AS DATE) >= CAST(STARTDATE as DATE) AND CAST(a.datefinalized AS DATE) <= CAST(ENDDATE as DATE)
152150
AND a.qcstate.publicdata = true --and a.participantID.demographics.species.common not in ('Rabbit','Guinea Pigs')
153151

154152

155-
--add setup fees for all starts, except day leases aznd sla
153+
-- Add lease setup fees for all lease starts, except day leases, PI-purchased NHPs, and sla
156154
UNION ALL
155+
157156
SELECT
158157
a.id,
159158
a.date,
@@ -168,7 +167,7 @@ SELECT
168167
' ' as ESPFAnimal,
169168
'Lease Setup Fees' as category,
170169
(SELECT rowid FROM onprc_billing_public.chargeableItems ci WHERE ci.active = true AND ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.LEASE_SETUP_FEES')) as chargeId,
171-
1 as quantity,
170+
1 AS quantity,
172171
cast(null as integer) as leaseCharge1,
173172
cast(null as integer) as leaseCharge2,
174173
a.objectid as sourceRecord,
@@ -179,11 +178,19 @@ SELECT
179178

180179
FROM study.assignment a
181180

181+
-- Add PI-purchased NHP flag data
182+
LEFT JOIN study.flags fl
183+
ON (a.id = fl.id
184+
AND fl.flag.code = 4034
185+
AND a.date BETWEEN CAST(fl.date AS DATE) AND COALESCE(fl.enddate, now())
186+
)
187+
182188
WHERE CAST(a.datefinalized AS DATE) >= CAST(STARTDATE as DATE) AND CAST(a.datefinalized AS DATE) <= CAST(ENDDATE as DATE)
183189
AND a.qcstate.publicdata = true
184190
--only charge setup fee for leases >24H. note: duration assumes today as end, so exclude null enddates
185191
AND ((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))
186192
and a.id.demographics.species Not IN ('Rabbit','Guinea Pigs')
193+
AND fl.id IS NULL -- Exempt PI-purchased NHPs from lease setup fees
187194

188195
--add released animals that need adjustments
189196
UNION ALL
@@ -208,9 +215,9 @@ a5.id as ESPFAnimal,
208215
--////This selectes the charge ID to be used
209216
(SELECT max(rowid) as rowid FROM onprc_billing_public.chargeableItems ci WHERE ci.name = javaConstant('org.labkey.onprc_billing.ONPRC_BillingManager.LEASE_FEE_ADJUSTMENT') and ci.active = true) as chargeId,
210217
CASE
211-
when (fl.id Is Not Null) then 0
212-
else 1
213-
end as quantity,
218+
WHEN (fl.id IS NOT NULL) THEN 0 -- Exempt PI-purchased NHPs from lease fees
219+
ELSE 1
220+
END AS quantity,
214221
lf2.chargeId as leaseCharge1,
215222
lf.chargeId as leaseCharge2,
216223
a.objectid as sourceRecord,
@@ -248,11 +255,12 @@ LEFT join assignment_U42ESPF a5 on
248255
and a5.dateonly <=a.dateOnly
249256
AND a5.endDateCoalesced >= a.dateOnly)
250257

251-
--adds the reasearch owned animal exemption
252-
LEFT JOIN study.flags fl on
253-
(a.id = fl.id
254-
and fl.flag.code = 4034
255-
and (a.date >= fl.date and a.date <=COALESCE(fl.enddate,Now()) ))
258+
-- Add PI-purchased NHP flag data
259+
LEFT JOIN study.flags fl
260+
ON (a.id = fl.id
261+
AND fl.flag.code = 4034
262+
AND a.date BETWEEN CAST(fl.date AS DATE) AND COALESCE(fl.enddate, now())
263+
)
256264

257265
WHERE a.releaseCondition != a.projectedReleaseCondition
258266
and (A.id != A5.id or A5.id is Null)

0 commit comments

Comments
 (0)