Skip to content

Commit

Permalink
COM-675 Add treatment indicator 7c
Browse files Browse the repository at this point in the history
Implements treatment indicator 7c
  • Loading branch information
tmvumbi2 authored and lauravignoli committed Mar 30, 2020
1 parent fd4fa70 commit 38977bb
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 5 deletions.
106 changes: 106 additions & 0 deletions metadata/reportssql/treatment_report_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,112 @@ WHERE
END$$
DELIMITER ;

DROP FUNCTION IF EXISTS TREATMENT_Indicator7c;

DELIMITER $$
CREATE FUNCTION TREATMENT_Indicator7c(
p_startDate DATE,
p_endDate DATE,
p_startAge INT(11),
p_endAge INT (11),
p_includeEndAge TINYINT(1),
p_gender VARCHAR(1)) RETURNS INT(11)
DETERMINISTIC
BEGIN
DECLARE result INT(11) DEFAULT 0;

SELECT
COUNT(DISTINCT pat.patient_id) INTO result
FROM
patient pat
WHERE
patientGenderIs(pat.patient_id, p_gender) AND
patientHasEnrolledIntoHivProgram(pat.patient_id) = "Yes" AND
patientHasStartedARVTreatmentDuringReportingPeriod(pat.patient_id, p_startDate, p_endDate) AND
patientOrderedARVDuringReportingPeriod(pat.patient_id, p_startDate, p_endDate) AND
patientScreenedPositiveForTBDuringReportingPeriod(pat.patient_id, p_startDate, p_endDate) AND
patientAgeWhenRegisteredForHivProgramIsBetween(pat.patient_id, p_startAge, p_endAge, p_includeEndAge);

RETURN (result);
END$$
DELIMITER ;

-- patientScreenedPositiveForTBDuringReportingPeriod

DROP FUNCTION IF EXISTS patientScreenedPositiveForTBDuringReportingPeriod;

DELIMITER $$
CREATE FUNCTION patientScreenedPositiveForTBDuringReportingPeriod(
p_patientId INT(11),
p_startDate DATE,
p_endDate DATE) RETURNS TINYINT(1)
DETERMINISTIC
BEGIN

DECLARE result TINYINT(1) DEFAULT 0;
DECLARE tbTestDate DATE;
DECLARE tbResultUuid VARCHAR(38) DEFAULT "7a4899dc-68ff-444a-9c7e-7fbae547e326";
DECLARE tbTestDateUuid VARCHAR(38) DEFAULT "55185e73-e634-4dfc-8ec0-02086e8c54d0";
DECLARE tbPositiveUuid VARCHAR(38) DEFAULT "7acfafa4-f19b-485e-97a7-c9e002dbe37a";
DECLARE encounterIdOfTestDateObservation INT(11);

SELECT
o.value_datetime, o.encounter_id
INTO tbTestDate, encounterIdOfTestDateObservation
FROM obs o
JOIN concept c ON o.concept_id = c.concept_id AND c.retired = 0
WHERE o.voided = 0
AND o.value_datetime IS NOT NULL
AND o.value_datetime BETWEEN p_startDate AND p_endDate
AND o.person_id = p_patientId
AND c.uuid = tbTestDateUuid
ORDER BY o.value_datetime DESC
LIMIT 1;

SELECT TRUE INTO result
FROM obs o
JOIN concept c ON o.concept_id = c.concept_id AND c.retired = 0
WHERE o.voided = 0
AND o.encounter_id = encounterIdOfTestDateObservation
AND o.value_coded IS NOT NULL
AND o.value_coded = (SELECT concept.concept_id FROM concept WHERE concept.uuid = tbPositiveUuid)
AND o.person_id = p_patientId
AND c.uuid = tbResultUuid
ORDER BY o.obs_id DESC
LIMIT 1;

RETURN (result);
END$$
DELIMITER ;

-- patientOrderedARVDuringReportingPeriod

DROP FUNCTION IF EXISTS patientOrderedARVDuringReportingPeriod;

DELIMITER $$
CREATE FUNCTION patientOrderedARVDuringReportingPeriod(
p_patientId INT(11),
p_startDate DATE,
p_endDate DATE) RETURNS TINYINT(1)
DETERMINISTIC
BEGIN

DECLARE result TINYINT(1) DEFAULT 0;

SELECT TRUE INTO result
FROM orders o
JOIN drug_order do ON do.order_id = o.order_id
JOIN concept c ON do.duration_units = c.concept_id AND c.retired = 0
JOIN drug d ON d.drug_id = do.drug_inventory_id AND d.retired = 0
WHERE o.patient_id = p_patientId AND o.voided = 0
AND drugIsARV(d.concept_id)
AND o.scheduled_date BETWEEN p_startDate AND p_endDate
GROUP BY o.patient_id;

RETURN (result );
END$$
DELIMITER ;

-- getPatientDateOfEnrolmentInProgram

DROP FUNCTION IF EXISTS getPatientDateOfEnrolmentInProgram;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SELECT 'Not available' AS 'Not available',
0 AS '<15 M',
0 AS '<15 F',
0 AS '>= 15 M',
0 AS '>= 15 F',
SELECT 'New on ART/Screen Positive' AS 'Title',
TREATMENT_Indicator7c('#startDate#','#endDate#', 0, 15, 0, 'M') AS '<15 M',
TREATMENT_Indicator7c('#startDate#','#endDate#', 0, 15, 0, 'F') AS '<15 F',
TREATMENT_Indicator7c('#startDate#','#endDate#', 15, 200, 1, 'M') AS '>=15 M',
TREATMENT_Indicator7c('#startDate#','#endDate#', 15, 200, 1, 'F') AS '>=15 F',
0 AS 'Unknown M',
0 AS 'Unknown F';

0 comments on commit 38977bb

Please sign in to comment.