From cfc21915826b3474a8f2cde760a586aee7b193e4 Mon Sep 17 00:00:00 2001 From: said-moj <45761276+said-moj@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:23:19 +0100 Subject: [PATCH] Add Mi scope report --- cla_backend/apps/reports/forms.py | 32 ++++++++++ .../apps/reports/sql/MiScopeReport.sql | 61 +++++++++++++++++++ cla_backend/apps/reports/urls.py | 1 + cla_backend/apps/reports/views.py | 7 +++ 4 files changed, 101 insertions(+) create mode 100644 cla_backend/apps/reports/sql/MiScopeReport.sql diff --git a/cla_backend/apps/reports/forms.py b/cla_backend/apps/reports/forms.py index 276e697b5..15bf7485c 100644 --- a/cla_backend/apps/reports/forms.py +++ b/cla_backend/apps/reports/forms.py @@ -1008,6 +1008,38 @@ def get_headers(self): ] +class MIScopeReport(SQLFileDateRangeReport): + QUERY_FILE = "MIScopeReport.sql" + + def get_sql_params(self): + from_date, to_date = self.date_range + return {"from_date": from_date, "to_date": to_date} + + def get_headers(self): + return [ + "Person ID", + "Case ID", + "Created", + "Modified", + "Case source", + "CHS scope state", + "Web scope state", + "Means eligibility state", + "Workflow status", + "CHS case outcome code", + "Provider Notes", + "Operator Notes", + "Client notes", + "Category code", + "Category name", + "Matter Type 1 code", + "Matter Type 1 description", + "Matter Type 2 code", + "Matter Type 2 description", + "Web diagnosis categories", + ] + + class CallbackTimeSlotReport(DateRangeReportForm): def get_queryset(self): from_date, to_date = self.date_range diff --git a/cla_backend/apps/reports/sql/MiScopeReport.sql b/cla_backend/apps/reports/sql/MiScopeReport.sql new file mode 100644 index 000000000..b1c48d6a5 --- /dev/null +++ b/cla_backend/apps/reports/sql/MiScopeReport.sql @@ -0,0 +1,61 @@ +WITH log_changed_category as ( + SELECT log_created.case_id, (log_changed_category.patch #>> '{}')::json#>>'{backwards,0,value}' as "diagnosis_category" + FROM cla_eventlog_log as log_created + JOIN cla_eventlog_log as log_changed_category ON log_created.case_id=log_changed_category.case_id + AND log_created.notes = 'Case created digitally' AND log_changed_category.notes LIKE 'Changed category to%%' +), +log_mi_oos_outcome_code as ( + SELECT case_id, code + FROM cla_eventlog_log + WHERE code = 'MIS-OOS' + GROUP BY case_id, code +) + +SELECT + c.personal_details_id as "Person ID" + ,c.reference as "Case Id" + ,to_char(c.created, 'YYYY-MM-DD') as "Created" + ,to_char(c.modified, 'YYYY-MM-DD') as "Modified" + ,c.source as "Case source" + ,CASE diagnosis.state + when 'INSCOPE' then 'yes' + else NULL + END as "CHS scope state" + ,COALESCE(log_changed_category.diagnosis_category, category.code) as "Web scope state" + ,CASE ec.state + when 'yes' then 'yes' + when 'no' then 'no' + else 'unknown' + END AS "Means eligibility state" + ,CASE + WHEN ec.state NOT IN('yes', 'no') THEN 'Pending' + WHEN ec.state IS NOT NULL AND provider_assigned_at IS NULL THEN 'Operator' + WHEN c.provider_viewed IS NOT NULL AND log_mi_oos_outcome_code.code IS NULL THEN 'Read and approved by SP' + WHEN c.provider_viewed IS NOT NULL AND log_mi_oos_outcome_code.code IS NOT NULL THEN 'Read and NOT approved by SP' + ELSE 'Provider has not viewed' + END as "Workflow status" + ,c.outcome_code as "CHS case outcome code" + ,c.provider_notes as "Provider Notes" + ,c.notes as "Operator Notes" + ,ec.notes as "Client notes" + ,category.code as "Category code" + ,category.name as "Category name" + ,mt1.code as "Matter Type 1 code" + ,mt1.description as "Matter Type 1 description" + ,mt2.code as "Matter Type 2 code" + ,mt2.description as "Matter Type 2 description" + ,ec.notes as "Web diagnosis categories" + + +FROM legalaid_case as c +LEFT OUTER JOIN legalaid_eligibilitycheck as ec on c.eligibility_check_id = ec.id +LEFT OUTER JOIN legalaid_category as category on ec.category_id = category.id +LEFT OUTER JOIN legalaid_adaptationdetails as adapt on c.adaptation_details_id = adapt.id +LEFT OUTER JOIN log_changed_category ON log_changed_category.case_id = c.id +LEFT OUTER JOIN legalaid_mattertype as mt1 on mt1.id = c.matter_type1_id +LEFT OUTER JOIN legalaid_mattertype as mt2 on mt2.id = c.matter_type2_id +LEFT OUTER JOIN diagnosis_diagnosistraversal as diagnosis on c.diagnosis_id = diagnosis.id +LEFT OUTER JOIN log_mi_oos_outcome_code ON log_mi_oos_outcome_code.case_id = c.id +WHERE source IN ('WEB') +AND c.modified >= %(from_date)s AND c.modified < %(to_date)s +ORDER BY c.modified DESC diff --git a/cla_backend/apps/reports/urls.py b/cla_backend/apps/reports/urls.py index 3043e197b..d62cdca46 100644 --- a/cla_backend/apps/reports/urls.py +++ b/cla_backend/apps/reports/urls.py @@ -41,6 +41,7 @@ url(r"^mi-comlpaints/$", views.mi_complaints, name="mi_complaints"), url(r"^mi-obiee-extract/$", views.mi_obiee_extract, name="mi_obiee_extract"), url(r"^mi-problem-categorisation/$", views.mi_problem_categorisation, name="mi_problem_categorisation"), + url(r"^mi-scope-report/$", views.mi_scope_report, name="mi_scope_report"), url(r"^metrics-report/$", views.metrics_report, name="metrics_report"), url(r"^all-knowledgebase-articles/$", views.all_knowledgebase_articles, name="all_knowledgebase_articles"), url(r"^reasons-for-contacting/$", views.reasons_for_contacting, name="reasons_for_contacting"), diff --git a/cla_backend/apps/reports/views.py b/cla_backend/apps/reports/views.py index c65ff950d..97b5a1fdd 100644 --- a/cla_backend/apps/reports/views.py +++ b/cla_backend/apps/reports/views.py @@ -34,6 +34,7 @@ ReasonsForContactingReport, ReasonsForContactingDisaggregated, MIProblemCategorisation, + MIScopeReport, MIDemographicReport, CallbackTimeSlotReport, ) @@ -271,6 +272,12 @@ def mi_problem_categorisation(request): return report_view(request, MIProblemCategorisation, "MI Problem Categorisation") +@staff_member_required +@permission_required("legalaid.run_reports") +def mi_scope_report(request): + return report_view(request, MIScopeReport, "MI Scope report") + + @staff_member_required @permission_required("legalaid.run_reports") def callback_time_slot_report(request):