* добавлены операции и услуги операций

* добавлена выборка и подсчет по датам для роли зав.
* переключатель ролей
* выбор отделений для роли зав.
This commit is contained in:
brusnitsyn
2026-01-22 17:58:27 +09:00
parent 8a0fdf9470
commit cb43c74a72
28 changed files with 961 additions and 143 deletions

View File

@@ -7,13 +7,16 @@ use App\Http\Resources\Mis\FormattedPatientResource;
use App\Models\MetrikaGroup;
use App\Models\MetrikaResult;
use App\Models\MisMedicalHistory;
use App\Models\MisMigrationPatient;
use App\Models\MisStationarBranch;
use App\Models\ObservationPatient;
use App\Models\Report;
use App\Models\UnwantedEvent;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
use Inertia\Inertia;
@@ -21,9 +24,26 @@ class ReportController extends Controller
{
public function index(Request $request)
{
$user = $request->user();
$user = Auth::user();
$department = $user->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')
@@ -35,10 +55,103 @@ class ReportController extends Controller
$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
]);
@@ -50,6 +163,7 @@ class ReportController extends Controller
'metrics' => 'required',
'observationPatients' => 'nullable',
'departmentId' => 'required|integer',
'unwantedEvent' => 'nullable'
]);
$metrics = $data['metrics'];
@@ -74,6 +188,13 @@ class ReportController extends Controller
'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();
@@ -97,59 +218,215 @@ class ReportController extends Controller
public function getPatients(Request $request)
{
$user = Auth::user();
$data = $request->validate([
'status' => 'required|string', // plan emergency
'startAt' => 'nullable',
'endAt' => 'nullable',
]);
$status = $data['status'];
$startDate = Carbon::now()->addDays(-1)->format('Y-m-d');
$endDate = Carbon::now()->format('Y-m-d');
$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 ($status === 'plan') {
$patients = MisMedicalHistory::select(
[
...$model->getFillable(),
DB::raw('ROW_NUMBER() OVER (ORDER BY "DateRecipient" DESC) as num')
])
->plan()
->inDepartment($misDepartmentId, $startDate, $endDate)
->orderBy('DateRecipient', 'DESC')
->get()
->map(function ($item, $index) {
$item->num = $index + 1;
return $item;
});;
} else if ($status === 'emergency') {
$patients = MisMedicalHistory::select(
[
...$model->getFillable(),
DB::raw('ROW_NUMBER() OVER (ORDER BY "DateRecipient" DESC) as num')
])
->emergency()
->inDepartment($misDepartmentId, $startDate, $endDate)
->orderBy('DateRecipient', 'DESC')
->get()
->map(function ($item, $index) {
$item->num = $index + 1;
return $item;
});
} else if ($status === 'observation') {
$patients = ObservationPatient::with(['history'])
->where('rf_department_id', $misDepartmentId)
->history;
} 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;
});
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) {
@@ -164,29 +441,97 @@ class ReportController extends Controller
public function getPatientsCount(Request $request)
{
$user = Auth::user();
$data = $request->validate([
'status' => 'required|string', // plan emergency
'startAt' => 'nullable',
'endAt' => 'nullable',
]);
$status = $data['status'];
$startDate = Carbon::now()->addDays(-1)->format('Y-m-d');
$endDate = Carbon::now()->format('Y-m-d');
$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');
}
$model = new MisMedicalHistory();
$misDepartmentId = $request->user()->department->rf_mis_department_id;
if ($status === 'plan') {
$count = MisMedicalHistory::select($model->getFillable())
->plan()
->inDepartment($misDepartmentId, $startDate, $endDate)
->orderBy('DateRecipient', 'DESC')
->count();
} else if ($status === 'emergency') {
$count = MisMedicalHistory::select($model->getFillable())
->emergency()
->inDepartment($misDepartmentId, $startDate, $endDate)
->orderBy('DateRecipient', 'DESC')
->count();
// Получаем 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);

View File

@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
use Inertia\Inertia;
@@ -53,4 +54,22 @@ class AuthController extends Controller
return Inertia::location(route('start'));
}
public function changeRole(Request $request)
{
$user = Auth::user();
if (!$user) return null;
$data = $request->validate([
'role_id' => 'required|integer|exists:roles,role_id'
]);
$sessionKey = 'user_' . $user->id . '_current_role';
$user->current_role_id = $data['role_id'];
$user->save();
return redirect()->route('start')->setStatusCode(302);
}
}

View File

@@ -45,6 +45,7 @@ class HandleInertiaRequests extends Middleware
'token' => Session::get('token'),
'permissions' => $user->permissions(),
'role' => $user->currentRole(),
'available_roles' => $user->roles,
'available_departments' => $user->availableDepartments(),
'current_department' => $user->department
] : null,

View File

@@ -2,6 +2,7 @@
namespace App\Http\Resources\Mis;
use App\Models\MisSurgicalOperation;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;
@@ -25,6 +26,15 @@ class FormattedPatientResource extends JsonResource
'name' => $this->migrations()->first()->diagnosis()->first()?->mkb()->first()->NAME ?? null,
];
}),
'operations' => $this->whenLoaded('surgicalOperations', function () {
return $this->surgicalOperations()->where('rf_StationarBranchID', $this->misStationarBranchId)
->get()
->map(function (MisSurgicalOperation $operation) {
return [
'code' => $operation->serviceMedical->ServiceMedicalCode ?? null,
];
});
}),
'fullname' => Str::ucwords(Str::lower("$this->FAMILY $this->Name $this->OT")),
'age' => Carbon::parse($this->BD)->diff(Carbon::now())->format('%y'),
'birth_date' => Carbon::parse($this->BD)->format('d.m.Y'),