first(); } public function searchPatients(Request $request) { $search = $request->search; return MedicalHistory::whereLike('full_name', $search . '%') ->orderBy('recipient_date', 'desc') ->get()->map(function ($item) { return [ 'label' => "$item->medical_card_number - $item->full_name", 'value' => $item->id ]; }); } public function storePatient(Request $request) { $user = auth()->user(); $department = auth()->user()->department; $misUserId = $user->rf_lpudoctor_id; $data = $request->validate([ 'source_type' => 'nullable', 'medical_card_number' => 'nullable', 'full_name' => 'required', 'birth_date' => 'required', 'recipient_date' => 'required', 'extract_date' => 'nullable', 'death_date' => 'nullable', 'male' => 'nullable', 'urgency_id' => 'required', 'hospital_result_id' => 'nullable', 'visit_result_id' => 'required', 'mis_user_id' => 'nullable', 'comment' => 'nullable', ]); $data['user_id'] = $user->id; $branch = MisStationarBranch::where('rf_DepartmentID', $department->rf_mis_department_id) ->first(); $migrationData = [ 'ingoing_date' => $data['recipient_date'], 'out_date' => $data['extract_date'], 'department_id' => $department->rf_mis_department_id, 'stationar_branch_id' => $branch->StationarBranchID, 'visit_result_id' => $data['visit_result_id'], 'user_id' => $data['user_id'], 'mis_user_id' => $misUserId ]; DB::beginTransaction(); $historyNurse = MedicalHistoryNurse::create($data); $migrationData['medical_history_id'] = $historyNurse->id; $migrationNurse = MigrationPatientNurse::create($migrationData); $historyNurse->update([ 'latest_migration_id' => $migrationNurse->id ]); if ($historyNurse && $migrationNurse) { DB::commit(); } else { DB::rollBack(); } return response()->json([ 'data' => $historyNurse, ], 201); } public function storeCorrection($id, Request $request) { $sourceType = $request->patient_source; $originalId = $request->original_id; $data = $request->validate([ 'medical_card_number' => 'nullable', 'full_name' => 'nullable', 'birth_date' => 'nullable', 'recipient_date' => 'nullable', 'extract_date' => 'nullable', 'death_date' => 'nullable', 'male' => 'nullable', 'urgency_id' => 'nullable', 'hospital_result_id' => 'nullable', 'visit_result_id' => 'nullable', 'mis_user_id' => 'nullable', 'comment' => 'nullable', ]); $data['medical_history_id'] = $id; $data['user_id'] = auth()->user()->id; $departmentId = auth()->user()->department->rf_mis_department_id; if ($sourceType === 'mis') $currentMigration = MigrationPatient::currentMigration($id, $departmentId)->first(); else $currentMigration = MigrationPatientNurse::currentMigration($originalId, $departmentId)->first(); $migrationData = [ 'migration_patient_id' => $currentMigration->id, 'medical_history_id' => $id, 'ingoing_date' => $data['recipient_date'], 'out_date' => $data['extract_date'], 'visit_result_id' => $data['visit_result_id'], 'user_id' => $data['user_id'], ]; DB::beginTransaction(); if ($sourceType === 'mis') { $historyCorrection = MedicalHistoryCorrection::create($data); $migrationCorrection = MigrationPatientCorrection::create($migrationData); } else if ($sourceType === 'manual') { unset($data['medical_history_id']); unset($migrationData['migration_patient_id']); unset($migrationData['medical_history_id']); $historyCorrection = MedicalHistoryNurse::find($originalId)->update($data); $migrationCorrection = MigrationPatientNurse::where('medical_history_id', $originalId)->update($migrationData); } if ($historyCorrection && $migrationCorrection) { DB::commit(); // Синхронизируем снапшот, чтобы правки были видны в МИС-вкладке $reportNurseId = $request->input('report_nurse_id'); $this->syncNurseSnapshot($originalId, $data, $migrationData, $departmentId, $reportNurseId); return response()->json([ 'data' => $historyCorrection, ], 201); } else { DB::rollBack(); return response()->json([ 'data' => 'Something went wrong', ], 400); } } private function syncNurseSnapshot( mixed $originalId, array $data, array $migrationData, int $departmentId, ?int $reportNurseId ): void { $patientUpdate = collect($data) ->only(['recipient_date', 'extract_date', 'death_date', 'urgency_id', 'visit_result_id', 'hospital_result_id', 'full_name', 'birth_date']) ->toArray(); if (empty($patientUpdate)) return; // Если известен отчёт — обновляем только его и все последующие по этому отделению $reportNurseQuery = ReportNursePatient::where('original_id', $originalId); if ($reportNurseId) { $fromReport = ReportNurse::find($reportNurseId); if ($fromReport) { $affectedReportIds = ReportNurse::where('rf_department_id', $fromReport->rf_department_id) ->where('period_start', '>=', $fromReport->period_start) ->pluck('id'); $reportNurseQuery->whereIn('report_nurse_id', $affectedReportIds); } } $snapshotPatientIds = $reportNurseQuery->pluck('id'); if ($snapshotPatientIds->isEmpty()) return; ReportNursePatient::whereIn('id', $snapshotPatientIds)->update($patientUpdate); $migrationUpdate = array_filter([ 'ingoing_date' => $migrationData['ingoing_date'] ?? null, 'out_date' => $migrationData['out_date'] ?? null, 'visit_result_id' => $migrationData['visit_result_id'] ?? null, ]); if (!empty($migrationUpdate)) { ReportNurseMigrationPatient::whereIn('medical_history_id', $snapshotPatientIds) ->where('department_id', $departmentId) ->update($migrationUpdate); } } public function deleteHistory($id, Request $request) { $medicalHistory = UnifiedMedicalHistory::where('id', $id)->first(); $originalMedicalHistoryId = $medicalHistory->original_id; $nurseMedicalHistory = ReportNursePatient::where('original_id', $originalMedicalHistoryId)->first(); $migrationPatientsIds = $medicalHistory->migrations()->pluck('original_id'); foreach ($nurseMedicalHistory->migrations() as $nurseMigrationPatient) { $nurseMigrationPatient->delete(); } $nurseMedicalHistory->delete(); return response()->json([]); } }