* вывод данных из отчетов для ролей адм и зав * поправил ширину стобцов ввода * добавил календарь на страницу статистики * переделал календарь у заведующего на странице отчета * добавил и привязал метрики в статистику
121 lines
4.9 KiB
PHP
121 lines
4.9 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Web;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\MisStationarBranch;
|
||
use App\Models\Report;
|
||
use App\Services\DateRangeService;
|
||
use App\Services\MisPatientService;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Inertia\Inertia;
|
||
|
||
class ReportController extends Controller
|
||
{
|
||
protected DateRangeService $dateService;
|
||
protected MisPatientService $misPatientService;
|
||
|
||
public function __construct(MisPatientService $misPatientService, DateRangeService $dateRangeService)
|
||
{
|
||
$this->misPatientService = $misPatientService;
|
||
$this->dateService = $dateRangeService;
|
||
}
|
||
|
||
public function index(Request $request)
|
||
{
|
||
$user = \Auth::user();
|
||
$department = $user->department;
|
||
|
||
$queryStartDate = $request->query('startAt');
|
||
$queryEndDate = $request->query('endAt');
|
||
[$startDate, $endDate] = $this->dateService->getDateRangeForUser($user, $queryStartDate, $queryEndDate);
|
||
$isRangeOneDay = $this->dateService->isRangeOneDay($startDate, $endDate);
|
||
|
||
// Если диапазон содержит сутки
|
||
if ($isRangeOneDay) {
|
||
// Устанавливаем дату отчета, как последний день из выборки
|
||
$dateReport = $endDate;
|
||
} else {
|
||
// Устанавливаем дату отчета, как выборку
|
||
$dateReport = [$startDate, $endDate];
|
||
}
|
||
|
||
if ($isRangeOneDay) {
|
||
// Статистика выводится с нарастающим числом
|
||
$reports = $department->reports()
|
||
->whereDate('created_at', $dateReport)
|
||
->get();
|
||
} else {
|
||
$reports = $department->reports()
|
||
->whereBetween('created_at', $dateReport)
|
||
->get();
|
||
}
|
||
|
||
$isReports = $reports->count() > 0;
|
||
|
||
$allCount = 0; $outcomeCount = 0; $currentCount = 0; $occupiedBeds = 0; $planCount = 0;
|
||
$emergencyCount = 0; $planSurgical = 0; $emergencySurgical = 0; $transferredCount = 0;
|
||
$deceasedCount = 0;
|
||
if ($isReports) {
|
||
foreach ($reports as $report) {
|
||
$allCount += $this->getMetrikaResultFromReport($report, 3, $isRangeOneDay);
|
||
$currentCount += $this->getMetrikaResultFromReport($report, 8, false);
|
||
$occupiedBeds += $this->getMetrikaResultFromReport($report, 8, $isRangeOneDay);
|
||
$planCount += $this->getMetrikaResultFromReport($report, 4, $isRangeOneDay);
|
||
$emergencyCount += $this->getMetrikaResultFromReport($report, 12, $isRangeOneDay);
|
||
$planSurgical += $this->getMetrikaResultFromReport($report, 11, $isRangeOneDay);
|
||
$emergencySurgical += $this->getMetrikaResultFromReport($report, 10, $isRangeOneDay);
|
||
$transferredCount += $this->getMetrikaResultFromReport($report, 13, $isRangeOneDay);
|
||
$outcomeCount += $this->getMetrikaResultFromReport($report, 7, $isRangeOneDay);
|
||
$deceasedCount += $this->getMetrikaResultFromReport($report, 9, $isRangeOneDay);
|
||
}
|
||
} else {
|
||
$misDepartmentId = $request->user()->department->rf_mis_department_id;
|
||
|
||
$branchId = MisStationarBranch::where('rf_DepartmentID', $misDepartmentId)
|
||
->value('StationarBranchID');
|
||
|
||
$planCount = $this->misPatientService->getInStationarPatients('plan', $branchId, $dateReport)->count();
|
||
}
|
||
|
||
$bedsCount = $department->metrikaDefault()
|
||
->where('rf_metrika_item_id', 1)->value('value');
|
||
|
||
$percentLoadedBeds = $bedsCount > 0 ? round($occupiedBeds * 100 / $bedsCount) : 0;
|
||
|
||
return Inertia::render('Report/Index', [
|
||
'department' => [
|
||
'beds' => $bedsCount,
|
||
'recipients' => [
|
||
'all' => $allCount,
|
||
'plan' => $planCount,
|
||
'emergency' => $emergencyCount,
|
||
'transferred' => $transferredCount,
|
||
],
|
||
'outcome' => $outcomeCount,
|
||
'consist' => $currentCount,
|
||
'percentLoadedBeds' => $percentLoadedBeds,
|
||
'surgical' => [
|
||
'plan' => $planSurgical,
|
||
'emergency' => $emergencySurgical
|
||
],
|
||
'deceased' => $deceasedCount,
|
||
],
|
||
]);
|
||
}
|
||
|
||
private function getMetrikaResultFromReport(Report $report, int $metrikaItem, bool $sum = true)
|
||
{
|
||
if ($sum) {
|
||
return (int) ($report->metrikaResults()
|
||
->where('rf_metrika_item_id', $metrikaItem)
|
||
->sum(DB::raw('CAST(value AS INTEGER)')) ?: 0);
|
||
}
|
||
|
||
return (int) ($report->metrikaResults()
|
||
->where('rf_metrika_item_id', $metrikaItem)
|
||
->value('value') ?: 0);
|
||
}
|
||
}
|