Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 43 additions & 32 deletions onprc_billing/resources/queries/onprc_billing/labworkFees.sql
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
PARAMETERS(StartDate TIMESTAMP, EndDate TIMESTAMP)

SELECT
e.Id,
e.date,
e.billingDate,
e.project,
e.servicerequested,
p.chargeId,
e.sourceRecord,
null as chargeCategory,
e.taskid
PARAMETERS (StartDate TIMESTAMP, EndDate TIMESTAMP)

-- Direct charges (internal labwork) by evaluation date
SELECT
e.Id,
e.date,
e.billingDate,
e.project,
e.serviceRequested,
p.chargeId,
e.sourceRecord,
NULL AS chargeCategory,
e.taskId
FROM onprc_billing.clinPathRun_ExemptCharges e
JOIN onprc_billing.labworkFeeDefinition p ON (p.servicename = e.servicerequested AND p.active = true)

WHERE CAST(e.billingdate as date) >= CAST(StartDate as date) AND CAST(e.billingdate as date) <= CAST(EndDate as date)
AND (e.chargetype not in ('Not Billable', 'No Charge', 'Research Staff') or e.chargetype is null)
AND e.qcstate.publicdata = true
JOIN onprc_billing.labworkFeeDefinition p
ON p.serviceName = e.serviceRequested
AND p.active = TRUE
WHERE CAST(e.date AS DATE) BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE)
AND e.qcState = 18
AND (e.chargeType NOT IN ('Not Billable', 'No Charge', 'Research Staff') OR e.chargeType IS NULL)

UNION ALL

--for any service sent to an outside lab, we have 1 processing charge per distinct sample
-- Processing fee: 1 per distinct sample sent to outside labs
SELECT
e.Id,
e.date,
e.billingDate,
e.project,
group_concat(e.servicerequested) as servicerequested,
(SELECT c.rowid FROM onprc_billing_public.chargeableItems c WHERE c.name = 'Lab Processing Fee') as chargeId,
null as sourceRecord,
null as chargeCategory,
e.taskid

e.Id,
e.date,
e.billingDate,
e.project,
GROUP_CONCAT(DISTINCT e.serviceRequested) AS serviceRequested,
(
SELECT c.rowId
FROM onprc_billing_public.chargeableItems c
WHERE c.name = 'Lab Processing Fee'
) AS chargeId,
NULL AS sourceRecord,
NULL AS chargeCategory,
e.taskId
FROM onprc_billing.clinPathRun_ExemptCharges e
WHERE CAST(e.billingDate as date) >= CAST(StartDate as date) AND CAST(e.billingDate as date) <= CAST(EndDate as date)
AND e.qcstate.publicdata = true
AND (e.chargetype not in ('Not Billable', 'No Charge', 'Research Staff') or e.chargetype is null)
AND e.servicerequested.outsidelab = true
GROUP BY e.Id, e.date, e.billingdate, e.project, e.tissue, e.taskid
WHERE CAST(e.date AS DATE) BETWEEN CAST(StartDate AS DATE) AND CAST(EndDate AS DATE)
AND e.qcState = 18
AND (e.chargeType NOT IN ('Not Billable', 'No Charge', 'Research Staff') OR e.chargeType IS NULL)
AND e.serviceRequested.outsideLab = TRUE
GROUP BY
e.Id,
e.date,
e.billingDate,
e.project,
e.tissue,
e.taskId;