2022 CDC Clinical Practice Guideline for Prescribing Opioids Implementation Guide
2022.1.0 - CI Build

2022 CDC Clinical Practice Guideline for Prescribing Opioids Implementation Guide, published by Centers for Disease Control and Prevention (CDC). This guide is not an authorized publication; it is the continuous build for version 2022.1.0 built by the FHIR (HL7® FHIR® Standard) CI Build. This version is based on the current content of https://github.com/cqframework/opioid-cds-r4/ and changes regularly. See the Directory of published versions

Library: OpioidCDSREC10CommonLibrary

Official URL: http://fhir.org/guides/cdc/opioid-cds/Library/OpioidCDSREC10Common Version: 2022.1.0
Draft as of 2024-04-22 Computable Name: OpioidCDSREC10Common
Id: OpioidCDSREC10Common
Url: OpioidCDSREC10Common
Version: 2022.1.0
Name: OpioidCDSREC10Common
Status: draft
Type:

system: LibraryType

code: logic-library

Date: 2024-04-22 23:06:49+0000
Publisher: Centers for Disease Control and Prevention (CDC)
Related Artifacts:

Dependencies

Parameters:
NameTypeMinMaxIn/Out
PatientPatient01out
UDS Lookback PeriodPeriod01out
Rx Lookback PeriodPeriod01out
Amphetamine Simple Identifierstring01out
Barbiturate Simple Identifierstring01out
Benzodiazepine Simple Identifierstring01out
Buprenorphine Simple Identifierstring01out
Cannabinoid Simple Identifierstring01out
Cocaine Simple Identifierstring01out
Fentanyl Simple Identifierstring01out
Heroin Simple Identifierstring01out
Methadone Simple Identifierstring01out
Opiate Simple Identifierstring01out
Oxycodone Simple Identifierstring01out
PCP Simple Identifierstring01out
All Opioid or Non-opioid UDS in Lookback PeriodObservation0*out
Inclusion Criteria For UDS Recommendationboolean01out
Amphetamine Screenings In Lookback PeriodObservation0*out
Positive Amphetamine Screenings In Lookback PeriodObservation0*out
Negative Amphetamine Screenings In Lookback PeriodObservation0*out
Normalize Rx In Lookback PeriodMedicationRequest0*out
Amphetamine Rx In Lookback PeriodMedicationRequest0*out
Barbiturate Screenings In Lookback PeriodObservation0*out
Positive Barbiturate Screenings In Lookback PeriodObservation0*out
Negative Barbiturate Screenings In Lookback PeriodObservation0*out
Barbiturate Rx In Lookback PeriodMedicationRequest0*out
Benzodiazepine Screenings In Lookback PeriodObservation0*out
Positive Benzodiazepine Screenings In Lookback PeriodObservation0*out
Negative Benzodiazepine Screenings In Lookback PeriodObservation0*out
Benzodiazepine Rx In Lookback PeriodMedicationRequest0*out
Buprenorphine Screenings In Lookback PeriodObservation0*out
Positive Buprenorphine Screenings In Lookback PeriodObservation0*out
Negative Buprenorphine Screenings In Lookback PeriodObservation0*out
Buprenorphine Rx In Lookback PeriodMedicationRequest0*out
Cannabinoid Screenings In Lookback PeriodObservation0*out
Positive Cannabinoid Screenings In Lookback PeriodObservation0*out
Negative Cannabinoid Screenings In Lookback PeriodObservation0*out
Cocaine Screenings In Lookback PeriodObservation0*out
Positive Cocaine Screenings In Lookback PeriodObservation0*out
Negative Cocaine Screenings In Lookback PeriodObservation0*out
Fentanyl Screenings In Lookback PeriodObservation0*out
Positive Fentanyl Screenings In Lookback PeriodObservation0*out
Negative Fentanyl Screenings In Lookback PeriodObservation0*out
Fentanyl Rx In Lookback PeriodMedicationRequest0*out
Heroin Screenings In Lookback PeriodObservation0*out
Positive Heroin Screenings In Lookback PeriodObservation0*out
Negative Heroin Screenings In Lookback PeriodObservation0*out
Methadone Screenings In Lookback PeriodObservation0*out
Positive Methadone Screenings In Lookback PeriodObservation0*out
Negative Methadone Screenings In Lookback PeriodObservation0*out
Methadone Rx In Lookback PeriodMedicationRequest0*out
Opiate Screenings In Lookback PeriodObservation0*out
Positive Opiate Screenings In Lookback PeriodObservation0*out
Negative Opiate Screenings In Lookback PeriodObservation0*out
Opiate Rx In Lookback PeriodMedicationRequest0*out
Oxycodone Screenings In Lookback PeriodObservation0*out
Positive Oxycodone Screenings In Lookback PeriodObservation0*out
Negative Oxycodone Screenings In Lookback PeriodObservation0*out
Oxycodone Rx In Lookback PeriodMedicationRequest0*out
PCP Screenings In Lookback PeriodObservation0*out
Positive PCP Screenings In Lookback PeriodObservation0*out
Negative PCP Screenings In Lookback PeriodObservation0*out
Unexpected DetailsResource0*out
Inclusion Criteria For Unexpected Resultsboolean01out
Exclusion Criteriaboolean01out
All Positive Labs In Lookback PeriodObservation0*out
All Lab Dates in Lookback Perioddate0*out
Positive Lab Dates In Lookback Perioddate0*out
Unexpected Div Startstring01out
Table Startstring01out
Table Headersstring0*out
Generate Table Rowsstring0*out
Generate Footer Rowsstring0*out
Table Endstring01out
Details Tablestring01out
Details More Infostring01out
Div Endstring01out
Get HTML Detailsstring01out
Data Requirements:
TypeProfileMSCode Filter
Patient http://hl7.org/fhir/StructureDefinition/Patient
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Condition http://hl7.org/fhir/StructureDefinition/Condition ; code filter:
path: category
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/condition-encounter-diagnosis-category
Condition http://hl7.org/fhir/StructureDefinition/Condition ; code filter:
path: category
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/condition-problem-list-category
Condition http://hl7.org/fhir/StructureDefinition/Condition ; code filter:
path: category
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/condition-us-core-health-concern-category
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Medication http://hl7.org/fhir/StructureDefinition/Medication ; code filter:
path: id
MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Encounter http://hl7.org/fhir/StructureDefinition/Encounter ;;;;
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;; code filter:
path: code
value set: http://fhir.org/guides/cdc/opioid-cds/ValueSet/all-urine-drug-screening-tests
MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

MedicationRequest http://hl7.org/fhir/StructureDefinition/MedicationRequest ;;;;;; code filter:
path: status

code: active

code: completed

code: stopped

Content: text/cql
library OpioidCDSREC10Common version '2022.1.0'

using FHIR version '4.0.1'

include FHIRHelpers version '4.0.1' called FHIRHelpers
include OpioidCDSCommon version '2022.1.0' called Common
include OpioidCDSRoutines version '2022.1.0' called Routines
include OpioidCDSCommonConfig version '2022.1.0' called Config

context Patient

// Constants

define "UDS Lookback Period":
  Interval[Today() - 12 months - 1 days, Today()]

define "Rx Lookback Period":
  Interval[Today() - 12 months - 31 days, Today()]

define "Amphetamine Simple Identifier": 'Amphetamine'
define "Barbiturate Simple Identifier": 'Barbiturate'
define "Benzodiazepine Simple Identifier": 'Benzodiazepine'
define "Buprenorphine Simple Identifier": 'Buprenorphine'
define "Cannabinoid Simple Identifier": 'Cannabinoid'
define "Cocaine Simple Identifier": 'Cocaine'
define "Fentanyl Simple Identifier": 'Fentanyl'
define "Heroin Simple Identifier": 'Heroin (6-AM)'
define "Methadone Simple Identifier": 'Methadone'
define "Opiate Simple Identifier": 'Opiate'
define "Oxycodone Simple Identifier": 'Oxycodone'
define "PCP Simple Identifier": 'Phencyclidine (PCP)'

/*
@description: Determines whether an opioid review is appropriate for the patient and whether they had a UDS performed in the past year
*/
define "Inclusion Criteria For UDS Recommendation":
  Routines."Is Opioid Review Useful?"
    and not exists ("All Opioid or Non-opioid UDS in Lookback Period")

define "Inclusion Criteria For Unexpected Results":
  AnyTrue(
    "Unexpected Details" Details
      return (exists Details.unexpectedPosDetails) or (exists Details.unexpectedNegDetails)
  )

define "Exclusion Criteria":
  Common."End of Life Assessment"

define "All Opioid or Non-opioid UDS in Lookback Period":
  (
    [Observation: Common."All Drug Urine Screening"] Lab
      where date from Lab.effective in day of "UDS Lookback Period"
        and not (Lab.status.value in { 'unknown', 'entered-in-error', 'cancelled' })
  ) X sort by date from FHIRHelpers.ToDateTime(effective as FHIR.dateTime) desc

// Amphetamines
define "Amphetamine Screenings In Lookback Period":
  "All Opioid or Non-opioid UDS in Lookback Period" Lab
    where Lab.code in Common."Amphetamine Urine Tests"

define "Positive Amphetamine Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Amphetamine Screenings In Lookback Period", true)

define "Negative Amphetamine Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Amphetamine Screenings In Lookback Period", true)

define "Amphetamine Rx In Lookback Period":
  "Normalize Rx In Lookback Period" Rx
      where Rx.medication in Common."Amphetamine Medications"

// Barbiturates
define "Barbiturate Screenings In Lookback Period":
  "All Opioid or Non-opioid UDS in Lookback Period" Lab
    where Lab.code in Common."Barbiturate Urine Tests"

define "Positive Barbiturate Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Barbiturate Screenings In Lookback Period", true)

define "Negative Barbiturate Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Barbiturate Screenings In Lookback Period", true)

define "Barbiturate Rx In Lookback Period":
  "Normalize Rx In Lookback Period" Rx
      where Rx.medication in Common."Barbiturate Medications"

// Benzodiazepines
define "Benzodiazepine Screenings In Lookback Period":
  "All Opioid or Non-opioid UDS in Lookback Period" Lab
    where Lab.code in Common."Benzodiazepine Urine Tests"

define "Positive Benzodiazepine Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Benzodiazepine Screenings In Lookback Period", true)

define "Negative Benzodiazepine Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Benzodiazepine Screenings In Lookback Period", true)

define "Benzodiazepine Rx In Lookback Period":
  "Normalize Rx In Lookback Period" Rx
      where Rx.medication in Common."Benzodiazepine medications"

// Buprenorphine
define "Buprenorphine Screenings In Lookback Period":
  "All Opioid or Non-opioid UDS in Lookback Period" Lab
    where Lab.code in Common."Buprenorphine Urine Tests"

define "Positive Buprenorphine Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Buprenorphine Screenings In Lookback Period", true)

define "Negative Buprenorphine Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Buprenorphine Screenings In Lookback Period", true)

define "Buprenorphine Rx In Lookback Period":
  "Normalize Rx In Lookback Period" Rx
      where Rx.medication in Common."Buprenorphine Medications"

// Cannabinoids
define "Cannabinoid Screenings In Lookback Period":
  if Config."Cannabinoid Urine Screening Check Enabled"
    then (
      "All Opioid or Non-opioid UDS in Lookback Period" Lab
        where Lab.code in Common."Cannabinoid Urine Tests"
    ) else null

define "Positive Cannabinoid Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Cannabinoid Screenings In Lookback Period", Config."Cannabinoid Urine Screening Check Enabled")

define "Negative Cannabinoid Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Cannabinoid Screenings In Lookback Period", Config."Cannabinoid Urine Screening Check Enabled")

// Cocaine
define "Cocaine Screenings In Lookback Period":
  "All Opioid or Non-opioid UDS in Lookback Period" Lab
    where Lab.code in Common."Cocaine Urine Tests"

define "Positive Cocaine Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Cocaine Screenings In Lookback Period", true)

define "Negative Cocaine Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Cocaine Screenings In Lookback Period", true)

// Fentanyl
define "Fentanyl Screenings In Lookback Period":
  if Config."Opiate Urine Screening Check Enabled"
  then (
    "All Opioid or Non-opioid UDS in Lookback Period" AllLabs
      where AllLabs.code in Common."Fentanyl Urine Tests"
  ) else null

define "Positive Fentanyl Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Fentanyl Screenings In Lookback Period", Config."Opiate Urine Screening Check Enabled")

define "Negative Fentanyl Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Fentanyl Screenings In Lookback Period", Config."Opiate Urine Screening Check Enabled")

define "Fentanyl Rx In Lookback Period":
  "Normalize Rx In Lookback Period" Rx
    where Rx.medication in Common."Fentanyl Medications"

// Heroin
define "Heroin Screenings In Lookback Period":
  "All Opioid or Non-opioid UDS in Lookback Period" Lab
    where Lab.code in Common."Heroin Urine Tests"

define "Positive Heroin Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Heroin Screenings In Lookback Period", true)

define "Negative Heroin Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Heroin Screenings In Lookback Period", true)

// Methadone
define "Methadone Screenings In Lookback Period":
  "All Opioid or Non-opioid UDS in Lookback Period" Lab
    where Lab.code in Common."Methadone Urine Tests"

define "Positive Methadone Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Methadone Screenings In Lookback Period", true)

define "Negative Methadone Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Methadone Screenings In Lookback Period", true)

define "Methadone Rx In Lookback Period":
  "Normalize Rx In Lookback Period" Rx
      where Rx.medication in Common."Methadone Medications"

// Opiates
define "Opiate Screenings In Lookback Period":
  if Config."Opiate Urine Screening Check Enabled"
    then (
      "All Opioid or Non-opioid UDS in Lookback Period" Lab
        where Lab.code in Common."General Opiate Urine Tests"
    ) else null

define "Positive Opiate Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Opiate Screenings In Lookback Period", Config."Opiate Urine Screening Check Enabled")

define "Negative Opiate Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Opiate Screenings In Lookback Period", Config."Opiate Urine Screening Check Enabled")

define "Opiate Rx In Lookback Period":
  "Normalize Rx In Lookback Period" Rx
      where Rx.medication in Common."Opiate Medications"

// Oxycodone
define "Oxycodone Screenings In Lookback Period":
  if Config."Opiate Urine Screening Check Enabled"
    then (
      "All Opioid or Non-opioid UDS in Lookback Period" Lab
        where Lab.code in Common."Oxycodone Urine Tests"
    ) else null

define "Positive Oxycodone Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("Oxycodone Screenings In Lookback Period", Config."Opiate Urine Screening Check Enabled")

define "Negative Oxycodone Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("Oxycodone Screenings In Lookback Period", Config."Opiate Urine Screening Check Enabled")

define "Oxycodone Rx In Lookback Period":
  "Normalize Rx In Lookback Period" Rx
      where Rx.medication in Common."Oxycodone Medications"

// PCP
define "PCP Screenings In Lookback Period":
  "All Opioid or Non-opioid UDS in Lookback Period" AllLabs
    where AllLabs.code in Common."PCP Urine Tests"

define "Positive PCP Screenings In Lookback Period":
  GetPositiveLabsInLookbackPeriod("PCP Screenings In Lookback Period", true)

define "Negative PCP Screenings In Lookback Period":
  GetNegativeLabsInLookbackPeriod("PCP Screenings In Lookback Period", true)

// Helpers  

define "All Positive Labs In Lookback Period":
  "Amphetamine Screenings In Lookback Period" union "Barbiturate Screenings In Lookback Period" 
    union "Benzodiazepine Screenings In Lookback Period" union "Buprenorphine Screenings In Lookback Period" 
    union "Cannabinoid Screenings In Lookback Period" union "Cocaine Screenings In Lookback Period" 
    union "Fentanyl Screenings In Lookback Period" union "Heroin Screenings In Lookback Period" 
    union "Methadone Screenings In Lookback Period" union "Opiate Screenings In Lookback Period"
    union "Oxycodone Screenings In Lookback Period" union "PCP Screenings In Lookback Period" 

define "All Lab Dates in Lookback Period":
  (
    "All Opioid or Non-opioid UDS in Lookback Period" Lab
      return date from FHIRHelpers.ToDateTime(Lab.effective as FHIR.dateTime)
  ) X sort desc

define "Positive Lab Dates In Lookback Period":
  (
    "All Positive Labs In Lookback Period" Lab
      return date from FHIRHelpers.ToDateTime(Lab.effective as FHIR.dateTime)
  ) X sort desc

define "Normalize Rx In Lookback Period":
  Common."Get MedicationRequest Medication as Code"(
    [MedicationRequest: status in { 'active', 'completed', 'stopped' }] Rx
      where Rx.category in Common."Community"
        and Rx.intent = 'order'
        and Rx.authoredOn.value in day of "Rx Lookback Period"
  )

// Details Logic

define "Unexpected Details":
  {
    CreateDetailWithPossibleUnexpectedNegative(
      "Amphetamine Simple Identifier", "Positive Amphetamine Screenings In Lookback Period", 
      "Negative Amphetamine Screenings In Lookback Period", "Amphetamine Rx In Lookback Period"
    ),
    CreateDetailWithPossibleUnexpectedNegative(
      "Barbiturate Simple Identifier", "Positive Barbiturate Screenings In Lookback Period", 
      "Negative Barbiturate Screenings In Lookback Period", "Barbiturate Rx In Lookback Period"
    ),
    CreateDetailWithPossibleUnexpectedNegative(
      "Benzodiazepine Simple Identifier", "Positive Benzodiazepine Screenings In Lookback Period", 
      "Negative Benzodiazepine Screenings In Lookback Period", "Benzodiazepine Rx In Lookback Period"
    ),
    CreateDetailWithPossibleUnexpectedNegative(
      "Buprenorphine Simple Identifier", "Positive Buprenorphine Screenings In Lookback Period", 
      "Negative Buprenorphine Screenings In Lookback Period", "Buprenorphine Rx In Lookback Period"
    ),
    CreateDetailWithoutPossibleUnexpectedNegative(
      "Cannabinoid Simple Identifier", "Positive Cannabinoid Screenings In Lookback Period", 
      "Negative Cannabinoid Screenings In Lookback Period"
    ),
    CreateDetailWithoutPossibleUnexpectedNegative(
      "Cocaine Simple Identifier", "Positive Cocaine Screenings In Lookback Period", 
      "Negative Cocaine Screenings In Lookback Period"
    ),
    CreateDetailWithPossibleUnexpectedNegative(
      "Fentanyl Simple Identifier", "Positive Fentanyl Screenings In Lookback Period", 
      "Negative Fentanyl Screenings In Lookback Period", "Fentanyl Rx In Lookback Period"
    ),
    CreateDetailWithoutPossibleUnexpectedNegative(
      "Heroin Simple Identifier", "Positive Heroin Screenings In Lookback Period", 
      "Negative Heroin Screenings In Lookback Period"
    ),
    CreateDetailWithPossibleUnexpectedNegative(
      "Methadone Simple Identifier", "Positive Methadone Screenings In Lookback Period", 
      "Negative Methadone Screenings In Lookback Period", "Methadone Rx In Lookback Period"
    ),
    CreateDetailWithPossibleUnexpectedNegative(
      "Opiate Simple Identifier", "Positive Opiate Screenings In Lookback Period", 
      "Negative Opiate Screenings In Lookback Period", "Opiate Rx In Lookback Period"
    ),
    CreateDetailWithPossibleUnexpectedNegative(
      "Oxycodone Simple Identifier", "Positive Oxycodone Screenings In Lookback Period", 
      "Negative Oxycodone Screenings In Lookback Period", "Oxycodone Rx In Lookback Period"
    ),
    CreateDetailWithoutPossibleUnexpectedNegative(
      "PCP Simple Identifier", "Positive PCP Screenings In Lookback Period", 
      "Negative PCP Screenings In Lookback Period"
    )
  }

// HTML Generation

define "Get HTML Details":
  Common."Style Header" + "Unexpected Div Start" + "Details Table" + "Details More Info" + "Div End"

define "Unexpected Div Start":
  '<div class=\"alert-body\"><div><p>Patient may have <strong>unexpected urine toxicology test results<\/strong> in the past year.<\/p><\/div>'

define "Details Table": 
  "Table Start" + Combine("Table Headers") 
    + '<tbody>' + Combine("Generate Table Rows") + '</tbody>' 
    + '<tfoot>' + Combine("Generate Footer Rows") + '<\/tfoot>' 
  + "Table End"

define "Table Start":
  '<div><table class=\"details-table\"><caption class=\"details-caption\">Urine Toxicology Results<\/caption><tr><th class=\"details-table-header\">Substance<\/th>'

define "Table Headers":
  "All Lab Dates in Lookback Period" LabDate
    return '<th class=\"details-table-header\">' + ToString(LabDate) + '<\/th>'

define "Generate Table Rows":
  "Unexpected Details" BOT
    let row: ToString(IndexOf("Unexpected Details", BOT) + 1)
    return '<tr>' + GetExpectedColumn(BOT.ingredient) + Combine(
      "All Lab Dates in Lookback Period" LabDate
        let col: ToString(IndexOf("All Lab Dates in Lookback Period", LabDate) + 2)
        return all (
          if BOT.unexpectedPosDetails.date contains LabDate
            then GetUnexpectedColumn('pos', row + ':' + col)
          else if BOT.unexpectedNegDetails.udsDate contains LabDate
            then GetUnexpectedColumn('neg', row + ':' + col)
          else if BOT.posDates contains LabDate
            then GetExpectedColumn('pos')
          else if BOT.negDates contains LabDate
            then GetExpectedColumn('neg')
          else GetExpectedColumn('n/a')
        )
    ) + '<\/tr>'

define "Generate Footer Rows":
  "Unexpected Details" BOT
      let row: ToString(IndexOf("Unexpected Details", BOT) + 1),
      colspan: Length("All Lab Dates in Lookback Period") + 1
      return Combine(
        "All Lab Dates in Lookback Period" LabDate
          let col: ToString(IndexOf("All Lab Dates in Lookback Period", LabDate) + 2)
          return all (
            if BOT.unexpectedPosDetails.date contains LabDate
              then singleton from (
                BOT.unexpectedPosDetails PosDetail
                  where PosDetail.date = LabDate
                    return '<tr>' + GetFooterColumn(colspan, row + ':' + col + ' - Possible unprescribed substance found: ' + PosDetail.udsDetail) + '<\/tr>'
              )
            else if BOT.unexpectedNegDetails.udsDate contains LabDate
              then singleton from (
                BOT.unexpectedNegDetails NegDetail
                  where NegDetail.udsDate = LabDate
                    return '<tr>' + GetFooterColumn(colspan, row + ':' + col + ' - Possible unexpected negative result found: prescribed ' + NegDetail.rxDetail + ' on ' + ToString(NegDetail.rxDate)) + '<\/tr>'
              )
            else ''
          )
      )

define "Table End":
  '<\/table><\/div>'

define "Details More Info":
  '<div id=\"box\">\r\n  <p>Note on false positives and unexpected negative results...<\/p>\r\n  <input type=\"checkbox\" class=\"show-more\"> Show more\r\n  <div class=\"more\">\r\n  <p>Positive results may be a false positives or could represent occasional use or possible substance use disorder.<br\/><br\/>For a review regarding interpreting possible false positive urine toxicology results, see <a target=\"_blank\" href=\"https:\/\/pubmed.ncbi.nlm.nih.gov\/24986836\">http:\/\/pubmed.ncbi.nlm.nih.gov\/24986836<\/a>. It is unknown if the findings reported in this article can be extrapolated to other laboratory analyzers that were not used in the referenced studies.<br\/><br\/>For guidance regarding evaluating and addressing unexpected toxicology tests results, see <a target=\"_blank\" href=\"https:\/\/www.cdc.gov\/mmwr\/volumes\/71\/rr\/rr7103a1.htm#Recommendation10\">Recommendation 10 of the 22 CDC Clinical Practice Guideiline<\/a>.<\/p>\r\n  <p>An unexpectedly negative test may be due to diversion, intermittent use, aberrant drug use behavior, or other factors, including a false negative result. Clinicians should interpret this result in the context of the overall treatment plan.<\/p>\r\n  <\/div>\r\n<\/div>'

define "Div End": '<\/div>'

// Functions

define function GetUnexpectedColumn(Result String, Superscript String):
  '<td class=\"details-table-cell-unexpected\">' + Result + '<sup>' + Superscript + '<\/sup><\/td>'

define function GetExpectedColumn(Content String):
  '<td class=\"details-table-cell\">' + Content + '<\/td>'

define function GetFooterColumn(colspan Integer, message String):
  '<td class=\"details-table-cell\"' + ' colspan=' + ToString(colspan) + '>' + message + '<\/td>'

define function CreateDetailWithPossibleUnexpectedNegative(TheSubstance String, ThePosLabs List<Observation>, TheNegLabs List<Observation>, TheMeds List<MedicationRequest>):
  {
    ingredient: TheSubstance,
    posDates: (
      ThePosLabs PosLab
        return date from FHIRHelpers.ToDateTime(PosLab.effective as FHIR.dateTime)
    ),
    negDates: (
      TheNegLabs NegLab
        return date from FHIRHelpers.ToDateTime(NegLab.effective as FHIR.dateTime)
    ),
    unexpectedPosDetails: (
      ThePosLabs PosLab
        with TheMeds Rx
          such that not (Rx.authoredOn in day of Interval[PosLab.effective - 31 days, PosLab.effective - 1 days])
            return {
              date: date from FHIRHelpers.ToDateTime(PosLab.effective as FHIR.dateTime),
              udsDetail: if exists (PosLab.code.coding) then PosLab.code.coding[0].display.value else 'Unable to determine a detailed identifier'
            }
    ),
    unexpectedNegDetails: (
      from TheNegLabs NegLab,
        TheMeds Rx
          where (Rx.authoredOn in day of Interval[NegLab.effective - 31 days, NegLab.effective - 1 days])
            return {
              udsDate: date from FHIRHelpers.ToDateTime(NegLab.effective as FHIR.dateTime),
              udsDetail: if exists (NegLab.code.coding) then NegLab.code.coding[0].display.value else 'Unable to determine a detailed identifier',
              rxDate: date from FHIRHelpers.ToDateTime(Rx.authoredOn as FHIR.dateTime),
              rxDetail: if exists (Rx.medication.coding) then Rx.medication.coding[0].display.value else 'Unable to determine a detailed identifier'
            }
    )
  }

define function CreateDetailWithoutPossibleUnexpectedNegative(TheSubstance String, ThePosLabs List<Observation>, TheNegLabs List<Observation>):
  {
    ingredient: TheSubstance,
    posDates: (
      ThePosLabs PosLab
        return date from FHIRHelpers.ToDateTime(PosLab.effective as FHIR.dateTime)
    ),
    negDates: (
      TheNegLabs NegLab
        return date from FHIRHelpers.ToDateTime(NegLab.effective as FHIR.dateTime)
    ),
    unexpectedPosDetails: (
      ThePosLabs PosLab
        return {
          date: date from FHIRHelpers.ToDateTime(PosLab.effective as FHIR.dateTime),
          udsDetail: if exists (PosLab.code.coding) then PosLab.code.coding[0].display.value else 'Unable to determine a detailed identifier'
        }
    ),
    unexpectedNegDetails: {
      null as Tuple{udsDate System.Date, udsDetail System.String, rxDate System.Date, rxDetail System.String}
    }
  }

define function GetPositiveLabsInLookbackPeriod(Labs List<FHIR.Observation>, Enabled Boolean):
  if Enabled
    then Labs Lab
     where StartsWith(Lower(Lab.value as FHIR.string), 'pos')
  else null

define function GetNegativeLabsInLookbackPeriod(Labs List<FHIR.Observation>, Enabled Boolean):
  if Enabled
    then Labs Lab
     where StartsWith(Lower(Lab.value as FHIR.string), 'neg')
  else null
Content: application/elm+xml
Encoded data (458208 characters)
Content: application/elm+json
Encoded data (832252 characters)