Files
onboard/app/Http/Controllers/Api/NurseController.php
brusnitsyn 2026a1ca9f * добавил удаление карты, если она была добавлена не из МИС
* добавил диалог при удалении карты
* добавил сохранение движения
* добавил вывод сохраненного отчета
* изменил логику сохранения отчета
2026-05-06 17:03:41 +09:00

175 lines
5.9 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\MedicalHistory;
use App\Models\MedicalHistoryCorrection;
use App\Models\MedicalHistoryNurse;
use App\Models\MigrationPatient;
use App\Models\MigrationPatientCorrection;
use App\Models\MigrationPatientNurse;
use App\Models\MisStationarBranch;
use App\Models\UnifiedMedicalHistory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class NurseController extends Controller
{
public function getPatient($id, Request $request)
{
return UnifiedMedicalHistory::where('id', $id)->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();
return response()->json([
'data' => $historyCorrection,
], 201);
} else {
DB::rollBack();
return response()->json([
'data' => 'Something went wrong',
], 400);
}
}
public function deleteHistory($id, Request $request)
{
$medicalHistory = UnifiedMedicalHistory::where('id', $id)->first();
$medicalHistoryId = $medicalHistory->original_id;
$migrationPatientsIds = $medicalHistory->migrations()->pluck('original_id');
foreach ($migrationPatientsIds as $migrationPatientsId) {
MigrationPatientNurse::query()->where('id', $migrationPatientsId)->delete();
}
MedicalHistoryNurse::where('id', $medicalHistoryId)->delete();
return response()->json([]);
}
}