department; $startDateCarbon = Carbon::now()->firstOfMonth(); $startDate = $request->query('startAt', $startDateCarbon->format('Y-m-d')); $endDateCarbon = Carbon::now(); $endDate = $request->query('endAt', $endDateCarbon->format('Y-m-d')); $doctorStartDate = Carbon::now()->addDays(-1)->format('Y-m-d'); $doctorEndDate = Carbon::now()->format('Y-m-d'); if (is_numeric($startDate)) { $startDateCarbon = Carbon::createFromTimestampMs($startDate); $startDate = Carbon::createFromTimestampMs($startDate)->setTimezone('Asia/Yakutsk')->format('Y-m-d'); } if (is_numeric($endDate)) { $endDateCarbon = Carbon::createFromTimestampMs($endDate); $endDate = Carbon::createFromTimestampMs($endDate)->setTimezone('Asia/Yakutsk')->format('Y-m-d'); } $beds = (int)$department->metrikaDefault()->where('rf_metrika_item_id', 1)->first()->value; $occupiedBeds = optional(Report::where('rf_department_id', $user->rf_department_id) ->join('metrika_results', 'reports.report_id', '=', 'metrika_results.rf_report_id') ->where('metrika_results.rf_metrika_item_id', 8) ->orderBy('sent_at', 'desc')->first())->value ?? 0; $percentLoadedBeds = intval($occupiedBeds) * 100 / $beds; $metrikaGroup = MetrikaGroup::whereMetrikaGroupId(2)->first(); $metrikaItems = $metrikaGroup->metrikaItems; $misDepartmentId = $request->user()->department->rf_mis_department_id; $branchId = MisStationarBranch::where('rf_DepartmentID', $misDepartmentId) ->value('StationarBranchID'); if ($user->isHeadOfDepartment()) { $medicalHistoryIds = MisMigrationPatient::whereInDepartment($branchId) ->pluck('rf_MedicalHistoryID') ->unique() ->toArray(); $extractedMedicalHistoryIds = MisMigrationPatient::extractedToday($branchId, $startDate, $endDate) ->pluck('rf_MedicalHistoryID') ->unique() ->toArray(); $recipientCount = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds) ->when($user->isHeadOfDepartment(), function($query) use ($startDate, $endDate) { return $query->whereBetween('DateRecipient', [$startDate, $endDate]); }) ->when(!$user->isHeadOfDepartment(), function($query) use ($doctorStartDate, $doctorEndDate) { return $query->whereBetween('DateRecipient', [$doctorStartDate, $doctorEndDate]); }) ->orderBy('DateRecipient', 'DESC') ->count(); $extractedCount = MisMedicalHistory::whereIn('MedicalHistoryID', $extractedMedicalHistoryIds) ->orderBy('DateRecipient', 'DESC') ->count(); $recipientIds = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds) ->when($doctorStartDate, function($query) use ($doctorStartDate, $doctorEndDate) { return $query->whereBetween('DateRecipient', [$doctorStartDate, $doctorEndDate]); }) ->orderBy('DateRecipient', 'DESC') ->pluck('MedicalHistoryID') ->values(); $currentCount = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds) ->currentlyHospitalized() ->orderBy('DateRecipient', 'DESC') ->count(); } else { $medicalHistoryIds = MisMigrationPatient::currentlyInTreatment($branchId) ->pluck('rf_MedicalHistoryID') ->unique() ->toArray(); $extractedMedicalHistoryIds = MisMigrationPatient::extractedToday($branchId) ->pluck('rf_MedicalHistoryID') ->unique() ->toArray(); $recipientCount = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds) ->when($user->isHeadOfDepartment(), function($query) use ($startDate, $endDate) { return $query->whereBetween('DateRecipient', [$startDate, $endDate]); }) ->when(!$user->isHeadOfDepartment(), function($query) use ($doctorStartDate, $doctorEndDate) { return $query->whereBetween('DateRecipient', [$doctorStartDate, $doctorEndDate]); }) ->orderBy('DateRecipient', 'DESC') ->count(); $extractedCount = MisMedicalHistory::whereIn('MedicalHistoryID', $extractedMedicalHistoryIds) ->orderBy('DateRecipient', 'DESC') ->count(); $recipientIds = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds) ->when($user->isHeadOfDepartment(), function($query) use ($startDate, $endDate) { return $query->whereBetween('DateRecipient', [$startDate, $endDate]); }) ->when(!$user->isHeadOfDepartment(), function($query) use ($doctorStartDate, $doctorEndDate) { return $query->whereBetween('DateRecipient', [$doctorStartDate, $doctorEndDate]); }) ->orderBy('DateRecipient', 'DESC') ->pluck('MedicalHistoryID') ->values(); $currentCount = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds) ->currentlyHospitalized() ->orderBy('DateRecipient', 'DESC') ->count(); } return response()->json([ 'department' => [ 'beds' => $beds, 'percentLoadedBeds' => $percentLoadedBeds, 'recipientCount' => $recipientCount, 'extractCount' => $extractedCount, 'currentCount' => $currentCount, 'recipientIds' => $recipientIds ], 'dates' => [ 'startAt' => $startDateCarbon->getTimestampMs(), 'endAt' => $endDateCarbon->getTimestampMs() ], 'metrikaItems' => $metrikaItems ]); } public function store(Request $request) { $data = $request->validate([ 'metrics' => 'required', 'observationPatients' => 'nullable', 'departmentId' => 'required|integer', 'unwantedEvent' => 'nullable' ]); $metrics = $data['metrics']; $observationPatients = $data['observationPatients']; $metriks = []; foreach ($metrics as $key => $value) { $metrika = new MetrikaResult; $metrikaId = (int)Str::replace('metrika_item_', '', $key); $metrika->rf_metrika_item_id = $metrikaId; $metrika->value = $value; $metriks[] = $metrika; } \DB::beginTransaction(); $report = Report::create([ 'rf_department_id' => $data['departmentId'], 'rf_user_id' => Auth::user()->id, 'created_at' => now(), 'sent_at' => now() ]); if (in_array('unwantedEvent', $data)) { $unwantedEvent = UnwantedEvent::create([ 'rf_report_id' => $report->id, 'comment' => $data['unwantedEvent']['comment'] ?? '', ]); } foreach ($metriks as $metrika) { $metrika->rf_report_id = $report->report_id; $metrika->save(); } foreach ($observationPatients as $observationPatient) { ObservationPatient::create([ 'rf_department_id' => $data['departmentId'], 'rf_report_id' => $report->report_id, 'rf_medicalhistory_id' => $observationPatient['id'], 'rf_mkab_id' => null ]); } \DB::commit(); return response()->json([ 'message' => 'success' ]); } public function getPatients(Request $request) { $user = Auth::user(); $data = $request->validate([ 'status' => 'required|string', // plan emergency 'startAt' => 'nullable', 'endAt' => 'nullable', ]); $status = $data['status']; $startDateCarbon = Carbon::now()->firstOfMonth(); $startDate = $data['startAt'] ?? $startDateCarbon->format('Y-m-d'); $endDateCarbon = Carbon::now(); $endDate = $data['endAt'] ?? $startDateCarbon->format('Y-m-d'); $doctorStartDate = Carbon::now()->addDays(-1)->format('Y-m-d'); $doctorEndDate = Carbon::now()->format('Y-m-d'); if (is_numeric($startDate)) { $startDateCarbon = Carbon::createFromTimestampMs($startDate); $startDate = Carbon::createFromTimestampMs($startDate)->setTimezone('Asia/Yakutsk')->format('Y-m-d'); } if (is_numeric($endDate)) { $endDateCarbon = Carbon::createFromTimestampMs($endDate); $endDate = Carbon::createFromTimestampMs($endDate)->setTimezone('Asia/Yakutsk')->format('Y-m-d'); } $model = new MisMedicalHistory(); $misDepartmentId = $request->user()->department->rf_mis_department_id; $userDepartmentId = $request->user()->department->department_id; $misStationarBranchId = MisStationarBranch::where('rf_DepartmentID', $misDepartmentId)->first()->StationarBranchID; if ($user->isHeadOfDepartment()) { if ($status === 'plan') { // Сначала получаем ID локально $branchId = MisStationarBranch::where('rf_DepartmentID', $misDepartmentId) ->value('StationarBranchID'); if (!$branchId) { return collect(); } $medicalHistoryIds = MisMigrationPatient::whereInDepartment($branchId) ->when($startDate && $endDate, function($query) use ($startDate, $endDate) { return $query->whereBetween('DateIngoing', [$startDate, $endDate]); }) ->pluck('rf_MedicalHistoryID') ->unique() ->toArray(); if (empty($medicalHistoryIds)) { return collect(); } // Получаем истории $patients = MisMedicalHistory::select($model->getFillable()) ->plan() ->whereIn('MedicalHistoryID', $medicalHistoryIds) ->orderBy('DateRecipient', 'DESC') ->get() ->map(function ($item, $index) use ($misStationarBranchId) { $item->num = $index + 1; $item->misStationarBranchId = $misStationarBranchId; return $item; }); } else if ($status === 'emergency') { // Сначала получаем ID локально $branchId = MisStationarBranch::where('rf_DepartmentID', $misDepartmentId) ->value('StationarBranchID'); if (!$branchId) { return collect(); } $medicalHistoryIds = MisMigrationPatient::whereInDepartment($branchId) ->when($startDate && $endDate, function($query) use ($startDate, $endDate) { return $query->whereBetween('DateIngoing', [$startDate, $endDate]); }) ->pluck('rf_MedicalHistoryID') ->unique() ->toArray(); if (empty($medicalHistoryIds)) { return collect(); } // Получаем истории $patients = MisMedicalHistory::select($model->getFillable()) ->emergency() ->whereIn('MedicalHistoryID', $medicalHistoryIds) ->with(['surgicalOperations']) ->orderBy('DateRecipient', 'DESC') ->get() ->map(function ($item, $index) use ($misStationarBranchId) { $item->num = $index + 1; $item->misStationarBranchId = $misStationarBranchId; return $item; }); } else if ($status === 'observation') { $medicalHistoryIds = ObservationPatient::where('rf_department_id', $userDepartmentId) ->pluck('rf_medicalhistory_id') ->toArray(); $patients = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds) ->get()->map(function ($item, $index) use ($misStationarBranchId) { $item->num = $index + 1; $item->misStationarBranchId = $misStationarBranchId; return $item; }); } else if ($status === 'deceased') { $patients = MisMedicalHistory::select(...$model->getFillable()) ->deceased() ->inDepartment($misDepartmentId, $startDate, $endDate) ->get() ->map(function ($item, $index) { $item->num = $index + 1; return $item; }); } } else { if ($status === 'plan') { // Сначала получаем ID локально $branchId = MisStationarBranch::where('rf_DepartmentID', $misDepartmentId) ->value('StationarBranchID'); if (!$branchId) { return collect(); } $medicalHistoryIds = MisMigrationPatient::currentlyInTreatment($branchId) // ->when($startDate && $endDate, function($query) use ($startDate, $endDate) { // return $query->whereBetween('DateIngoing', [$startDate, $endDate]); // }) ->pluck('rf_MedicalHistoryID') ->unique() ->toArray(); if (empty($medicalHistoryIds)) { return collect(); } // Получаем истории $patients = MisMedicalHistory::select($model->getFillable()) ->plan() ->whereIn('MedicalHistoryID', $medicalHistoryIds) ->currentlyHospitalized() ->orderBy('DateRecipient', 'DESC') ->get() ->map(function ($item, $index) use ($misStationarBranchId) { $item->num = $index + 1; $item->misStationarBranchId = $misStationarBranchId; return $item; }); } else if ($status === 'emergency') { // Сначала получаем ID локально $branchId = MisStationarBranch::where('rf_DepartmentID', $misDepartmentId) ->value('StationarBranchID'); if (!$branchId) { return collect(); } $medicalHistoryIds = MisMigrationPatient::currentlyInTreatment($branchId) // ->when($startDate && $endDate, function($query) use ($startDate, $endDate) { // return $query->whereBetween('DateIngoing', [$startDate, $endDate]); // }) ->pluck('rf_MedicalHistoryID') ->unique() ->toArray(); if (empty($medicalHistoryIds)) { return collect(); } // Получаем истории $patients = MisMedicalHistory::select($model->getFillable()) ->emergency() ->whereIn('MedicalHistoryID', $medicalHistoryIds) ->currentlyHospitalized() ->with(['surgicalOperations']) ->orderBy('DateRecipient', 'DESC') ->get() ->map(function ($item, $index) use ($misStationarBranchId) { $item->num = $index + 1; $item->misStationarBranchId = $misStationarBranchId; return $item; }); } else if ($status === 'observation') { $medicalHistoryIds = ObservationPatient::where('rf_department_id', $userDepartmentId) ->pluck('rf_medicalhistory_id') ->toArray(); $patients = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds) ->get()->map(function ($item, $index) use ($misStationarBranchId) { $item->num = $index + 1; $item->misStationarBranchId = $misStationarBranchId; return $item; }); } else if ($status === 'deceased') { $patients = MisMedicalHistory::select(...$model->getFillable()) ->deceased() ->inDepartment($misDepartmentId, $startDate, $endDate) ->get() ->map(function ($item, $index) { $item->num = $index + 1; return $item; }); } } $patients->load(['migrations' => function ($query) use ($startDate, $endDate, $misStationarBranchId) { $query->whereHas('diagnosis', function ($query) { $query->where('rf_DiagnosTypeID', 3); })->with('diagnosis.mkb') ->where('rf_StationarBranchID', $misStationarBranchId); }]); return response()->json(FormattedPatientResource::collection($patients)); } public function getPatientsCount(Request $request) { $user = Auth::user(); $data = $request->validate([ 'status' => 'required|string', // plan emergency 'startAt' => 'nullable', 'endAt' => 'nullable', ]); $status = $data['status']; $startDateCarbon = Carbon::now()->firstOfMonth(); $startDate = $data['startAt'] ?? $startDateCarbon->format('Y-m-d'); $endDateCarbon = Carbon::now(); $endDate = $data['endAt'] ?? $endDateCarbon->format('Y-m-d'); $doctorStartDate = Carbon::now()->addDays(-1)->format('Y-m-d'); $doctorEndDate = Carbon::now()->format('Y-m-d'); if (is_numeric($startDate)) { $startDateCarbon = Carbon::createFromTimestampMs($startDate); $startDate = Carbon::createFromTimestampMs($startDate)->setTimezone('Asia/Yakutsk')->format('Y-m-d'); } if (is_numeric($endDate)) { $endDateCarbon = Carbon::createFromTimestampMs($endDate); $endDate = Carbon::createFromTimestampMs($endDate)->setTimezone('Asia/Yakutsk')->format('Y-m-d'); } $misDepartmentId = $request->user()->department->rf_mis_department_id; // Получаем ID отделения $branchId = MisStationarBranch::where('rf_DepartmentID', $misDepartmentId) ->value('StationarBranchID'); if (!$branchId) { return response()->json(0); } if ($user->isHeadOfDepartment()) { // Получаем ID медицинских историй по миграциям $medicalHistoryIds = MisMigrationPatient::whereInDepartment($branchId) ->when($startDate && $endDate, function($query) use ($startDate, $endDate) { return $query->whereBetween('DateIngoing', [$startDate, $endDate]); }) ->pluck('rf_MedicalHistoryID') ->unique() ->toArray(); if (empty($medicalHistoryIds)) { return response()->json(0); } // Подсчет в зависимости от статуса if ($status === 'plan') { $count = MisMedicalHistory::plan() ->whereIn('MedicalHistoryID', $medicalHistoryIds) ->orderBy('DateRecipient', 'DESC') ->count(); } else if ($status === 'emergency') { $count = MisMedicalHistory::emergency() ->whereIn('MedicalHistoryID', $medicalHistoryIds) ->orderBy('DateRecipient', 'DESC') ->count(); } else { $count = 0; } } else { // Получаем ID медицинских историй по миграциям $medicalHistoryIds = MisMigrationPatient::currentlyInTreatment($branchId) ->pluck('rf_MedicalHistoryID') ->unique() ->toArray(); if (empty($medicalHistoryIds)) { return response()->json(0); } // Подсчет в зависимости от статуса if ($status === 'plan') { $count = MisMedicalHistory::plan() ->whereIn('MedicalHistoryID', $medicalHistoryIds) ->currentlyHospitalized() ->orderBy('DateRecipient', 'DESC') ->count(); } else if ($status === 'emergency') { $count = MisMedicalHistory::emergency() ->whereIn('MedicalHistoryID', $medicalHistoryIds) ->currentlyHospitalized() ->orderBy('DateRecipient', 'DESC') ->count(); } else { $count = 0; } } return response()->json($count); } }