finalize detail page except NDS and squencing

This commit is contained in:
2026-03-20 15:50:30 +07:00
parent aab4bd25dc
commit d4a8c9ded6
8 changed files with 212665 additions and 105432 deletions

View File

@@ -25,27 +25,32 @@ class DashboardService
|--------------------------------------------------------------------------
*/
public function summaryCards($dateFrom, $dateTo)
public function summaryCards()
{
$programs = Surveillance::orderBy('id')->get();
$results = [];
$today = date('Y-m-d');
$currentFrom = date('Y-m-d', strtotime('-6 days'));
$currentTo = $today;
$prevFrom = date('Y-m-d', strtotime('-13 days'));
$prevTo = date('Y-m-d', strtotime('-7 days'));
foreach ($programs as $program) {
$current = SurveillanceCase::where('surveillance_id', $program->id)
->whereBetween('case_date', [$dateFrom, $dateTo])
->whereBetween('case_date', [$currentFrom, $currentTo])
->count();
$previous = SurveillanceCase::where('surveillance_id', $program->id)
->whereBetween('case_date', [
date('Y-m-d', strtotime($dateFrom . ' -7 days')),
date('Y-m-d', strtotime($dateFrom . ' -1 day'))
])
->whereBetween('case_date', [$prevFrom, $prevTo])
->count();
$percentChange = $previous > 0
? round((($current - $previous) / $previous) * 100, 1)
: 0;
: ($current > 0 ? 100 : 0);
$results[] = [
'surveillance_id' => $program->id,
@@ -59,7 +64,6 @@ class DashboardService
return $results;
}
/*
|--------------------------------------------------------------------------
| Fast SARI Summary (single query)
@@ -80,31 +84,31 @@ class DashboardService
->where('surveillance_cases.week_data', $week)
->selectRaw("
COUNT(DISTINCT surveillance_cases.lab_code) as total_cases,
COUNT(DISTINCT surveillance_cases.lab_code) as total_cases,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
THEN surveillance_cases.lab_code
END) as overall_positive,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
THEN surveillance_cases.lab_code
END) as overall_positive,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
AND (
LOWER(case_lab_results.pathogen_name) LIKE '%influenza%'
OR LOWER(case_lab_results.pathogen_name) LIKE '%influzena%'
)
THEN surveillance_cases.lab_code
END) as influenza_positive,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
AND (
LOWER(case_lab_results.pathogen_name) LIKE '%influenza%'
OR LOWER(case_lab_results.pathogen_name) LIKE '%influzena%'
)
THEN surveillance_cases.lab_code
END) as influenza_positive,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
AND (
LOWER(case_lab_results.pathogen_name) LIKE '%covid%'
OR LOWER(case_lab_results.pathogen_name) LIKE '%sars%'
)
THEN surveillance_cases.lab_code
END) as covid_positive
")
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
AND (
LOWER(case_lab_results.pathogen_name) LIKE '%covid%'
OR LOWER(case_lab_results.pathogen_name) LIKE '%sars%'
)
THEN surveillance_cases.lab_code
END) as covid_positive
")
->first();
@@ -136,7 +140,51 @@ class DashboardService
| Program Summary
|--------------------------------------------------------------------------
*/
public function afiTrend($surveillanceId, $startYear, $startWeek, $endYear, $endWeek)
{
return CaseLabResult::join(
'surveillance_cases',
'case_lab_results.lab_code',
'=',
'surveillance_cases.lab_code'
)
->where('surveillance_cases.surveillance_id', $surveillanceId)
->where(function ($q) use ($startYear, $startWeek, $endYear, $endWeek) {
$q->whereRaw("(year_data > ? OR (year_data = ? AND week_data >= ?))", [$startYear, $startYear, $startWeek])
->whereRaw("(year_data < ? OR (year_data = ? AND week_data <= ?))", [$endYear, $endYear, $endWeek]);
})
->where('case_lab_results.is_positive', 1)
->whereNotNull('case_lab_results.pathogen_name')
->where('case_lab_results.pathogen_name', '!=', '')
->where('case_lab_results.pathogen_name', '!=', 'Positive')
->selectRaw("
surveillance_cases.year_data as year,
surveillance_cases.week_data as period,
CASE
WHEN LOWER(case_lab_results.pathogen_name) LIKE '%influenza%' THEN 'Influenza'
ELSE case_lab_results.pathogen_name
END as pathogen,
COUNT(DISTINCT surveillance_cases.lab_code) as total
")
->groupBy(
'surveillance_cases.year_data',
'surveillance_cases.week_data',
'pathogen'
)
->orderBy('surveillance_cases.year_data')
->orderBy('surveillance_cases.week_data')
->get();
}
public function programSummary($surveillanceId, $startYear, $startWeek, $endYear, $endWeek)
{
@@ -308,6 +356,13 @@ class DashboardService
$endYear,
$endWeek
),
'afi_trend' => $this->afiTrend(
$surveillanceId,
$startYear,
$startWeek,
$endYear,
$endWeek
),
'province_distribution' => $this->provinceProgram(
$surveillanceId,
@@ -520,22 +575,55 @@ class DashboardService
*/
public function provinceCircles($startYear, $startWeek, $endYear, $endWeek)
{
return SurveillanceCase::selectRaw(" surveillance_cases.site_province_name, surveillance_cases.surveillance_id, COUNT(*) as total ")->join('case_lab_results', 'surveillance_cases.lab_code', '=', 'case_lab_results.lab_code')->where('case_lab_results.is_positive', 1)->where(function ($q) use ($startYear, $startWeek, $endYear, $endWeek) {
$q->whereRaw("(surveillance_cases.year_data > ? OR (surveillance_cases.year_data = ? AND surveillance_cases.week_data >= ?))", [$startYear, $startYear, $startWeek])->whereRaw("(surveillance_cases.year_data < ? OR (surveillance_cases.year_data = ? AND surveillance_cases.week_data <= ?))", [$endYear, $endYear, $endWeek]);
})->groupBy('surveillance_cases.site_province_name', 'surveillance_cases.surveillance_id')->get();
return SurveillanceCase::leftJoin(
'case_lab_results',
'surveillance_cases.lab_code',
'=',
'case_lab_results.lab_code'
)
->where(function ($q) use ($startYear, $startWeek, $endYear, $endWeek) {
$q->whereRaw(
"(surveillance_cases.year_data > ? OR (surveillance_cases.year_data = ? AND surveillance_cases.week_data >= ?))",
[$startYear, $startYear, $startWeek]
)
->whereRaw(
"(surveillance_cases.year_data < ? OR (surveillance_cases.year_data = ? AND surveillance_cases.week_data <= ?))",
[$endYear, $endYear, $endWeek]
);
})
->selectRaw("
surveillance_cases.patient_province,
surveillance_cases.surveillance_id,
COUNT(DISTINCT surveillance_cases.lab_code) as total,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
THEN surveillance_cases.lab_code
END) as positive
")
->groupBy(
'surveillance_cases.patient_province',
'surveillance_cases.surveillance_id'
)
->get();
}
public function provinceProgram($surveillanceId, $startYear, $startWeek, $endYear, $endWeek)
{
return SurveillanceCase::selectRaw("
surveillance_cases.site_province_name,
surveillance_cases.patient_province,
COUNT(DISTINCT surveillance_cases.lab_code) as total,
COUNT(DISTINCT surveillance_cases.lab_code) as total,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
THEN surveillance_cases.lab_code
END) as positive
")
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
THEN surveillance_cases.lab_code
END) as positive
")
->join(
'case_lab_results',
@@ -560,7 +648,7 @@ class DashboardService
})
->groupBy('surveillance_cases.site_province_name')
->groupBy('surveillance_cases.patient_province')
->get();
}
@@ -583,7 +671,7 @@ class DashboardService
})
->groupBy('sentinel_site_name')
->orderByDesc('total') // nice for chart
->orderByDesc('total')
->get();
}
@@ -597,16 +685,13 @@ class DashboardService
{
$total = $this->totalTested($surveillanceId, $startYear, $startWeek, $endYear, $endWeek);
$rows = CaseLabResult::join(
'surveillance_cases',
'case_lab_results.lab_code',
'=',
'surveillance_cases.lab_code'
)
->where('surveillance_cases.surveillance_id', $surveillanceId)
$rows = CaseLabResult::where('case_lab_results.surveillance_id', $surveillanceId)
->where(function ($q) use ($startYear, $startWeek, $endYear, $endWeek) {
$q->whereRaw("(year_data > ? OR (year_data = ? AND week_data >= ?))", [$startYear, $startYear, $startWeek])
->whereIn('lab_code', function ($q) use ($surveillanceId, $startYear, $startWeek, $endYear, $endWeek) {
$q->select('lab_code')
->from('surveillance_cases')
->where('surveillance_id', $surveillanceId)
->whereRaw("(year_data > ? OR (year_data = ? AND week_data >= ?))", [$startYear, $startYear, $startWeek])
->whereRaw("(year_data < ? OR (year_data = ? AND week_data <= ?))", [$endYear, $endYear, $endWeek]);
})
@@ -614,18 +699,12 @@ class DashboardService
->selectRaw("
CASE
WHEN LOWER(case_lab_results.pathogen_name) LIKE '%influenza%'
OR LOWER(case_lab_results.pathogen_name) LIKE '%influzena%'
WHEN LOWER(case_lab_results.pathogen_name) LIKE '%influenza%'
THEN 'Influenza'
WHEN case_lab_results.pathogen_name = 'Positive'
AND case_lab_results.indicator LIKE '%Covid%'
THEN 'SARS-CoV-2'
ELSE case_lab_results.pathogen_name
END as pathogen,
COUNT(DISTINCT surveillance_cases.lab_code) as total
COUNT(DISTINCT case_lab_results.lab_code) as total
")
->groupBy('pathogen')
@@ -642,29 +721,36 @@ class DashboardService
{
$total = $this->totalTested($surveillanceId, $startYear, $startWeek, $endYear, $endWeek);
$rows = CaseLabResult::join(
'surveillance_cases',
'case_lab_results.lab_code',
'=',
'surveillance_cases.lab_code'
)
->where('surveillance_cases.surveillance_id', $surveillanceId)
->where('case_lab_results.is_positive', 1)
$rows = CaseLabResult::where('case_lab_results.surveillance_id', $surveillanceId)
->where(function ($q) use ($startYear, $startWeek, $endYear, $endWeek) {
$q->whereRaw("(year_data > ? OR (year_data = ? AND week_data >= ?))", [$startYear, $startYear, $startWeek])
->whereRaw("(year_data < ? OR (year_data = ? AND week_data <= ?))", [$endYear, $endYear, $endWeek]);
->whereIn('case_lab_results.lab_code', function ($q) use ($surveillanceId, $startYear, $startWeek, $endYear, $endWeek) {
$q->select('lab_code')
->from('surveillance_cases')
->where('surveillance_id', $surveillanceId)
->whereRaw(
"(year_data > ? OR (year_data = ? AND week_data >= ?))",
[$startYear, $startYear, $startWeek]
)
->whereRaw(
"(year_data < ? OR (year_data = ? AND week_data <= ?))",
[$endYear, $endYear, $endWeek]
);
})
->where('case_lab_results.is_positive', 1)
->selectRaw("
subtype,
COUNT(DISTINCT surveillance_cases.lab_code) as total
case_lab_results.subtype,
COUNT(DISTINCT case_lab_results.lab_code) as total
")
->groupBy('subtype')
->havingRaw("subtype IS NOT NULL AND subtype != 'Positive' AND subtype != ''")
->orderByDesc('total')
->get();
->whereNotNull('case_lab_results.subtype')
->where('case_lab_results.subtype', '!=', '')
->where('case_lab_results.subtype', '!=', 'Positive')
->groupBy('case_lab_results.subtype')
->orderByDesc('total')
->get();
return $rows->map(function ($r) use ($total) {
$r->rate = $total > 0 ? round(($r->total / $total) * 100, 1) : 0;
@@ -672,7 +758,6 @@ class DashboardService
});
}
/*
|--------------------------------------------------------------------------
| Age Distribution
@@ -681,18 +766,31 @@ class DashboardService
public function ageDistribution($surveillanceId, $startYear, $startWeek, $endYear, $endWeek)
{
return SurveillanceCase::selectRaw("
CASE
WHEN patient_age_inday < 365 THEN '<1 year'
WHEN patient_age_inday < 1825 THEN '14 years'
WHEN patient_age_inday < 6570 THEN '517 years'
WHEN patient_age_inday < 16425 THEN '1844 years'
WHEN patient_age_inday < 23725 THEN '4564 years'
WHEN patient_age_inday <= 28 THEN '028 days'
WHEN patient_age_inday <= 364 THEN '29 days11 months'
WHEN patient_age_inday <= 1460 THEN '14 years'
WHEN patient_age_inday <= 5110 THEN '514 years'
WHEN patient_age_inday <= 8765 THEN '1524 years'
WHEN patient_age_inday <= 18250 THEN '2549 years'
WHEN patient_age_inday <= 23725 THEN '5064 years'
ELSE '65+ years'
END as age_group,
CASE
WHEN patient_age_inday <= 28 THEN 1
WHEN patient_age_inday <= 364 THEN 2
WHEN patient_age_inday <= 1460 THEN 3
WHEN patient_age_inday <= 5110 THEN 4
WHEN patient_age_inday <= 8765 THEN 5
WHEN patient_age_inday <= 18250 THEN 6
WHEN patient_age_inday <= 23725 THEN 7
ELSE 8
END as age_order,
COUNT(*) as total
")
")
->where('surveillance_id', $surveillanceId)
@@ -702,7 +800,6 @@ class DashboardService
"(year_data > ? OR (year_data = ? AND week_data >= ?))",
[$startYear, $startYear, $startWeek]
)
->whereRaw(
"(year_data < ? OR (year_data = ? AND week_data <= ?))",
[$endYear, $endYear, $endWeek]
@@ -710,13 +807,11 @@ class DashboardService
})
->groupBy('age_group')
->groupBy('age_group', 'age_order')
->orderBy('age_order')
->get();
}
/*
|--------------------------------------------------------------------------
| Sex Distribution