138 lines
5.2 KiB
PHP
138 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Resources\ArchiveHistoryResource;
|
|
use App\Http\Resources\ArchiveInfoResource;
|
|
use App\Models\ArchiveHistory;
|
|
use App\Models\ArchiveInfo;
|
|
use App\Rules\DateTimeOrStringOrNumber;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class ArchiveHistoryController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
|
|
}
|
|
|
|
public function show($id, Request $request)
|
|
{
|
|
$archiveHistory = ArchiveHistory::find($id);
|
|
|
|
return response()->json($archiveHistory);
|
|
}
|
|
|
|
public function moveStore(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'issue_at' => ['nullable', new DateTimeOrStringOrNumber],
|
|
'return_at' => ['nullable', new DateTimeOrStringOrNumber],
|
|
'org_id' => 'required|numeric',
|
|
'employee_name' => 'nullable|string',
|
|
'employee_post' => 'nullable|string',
|
|
'comment' => 'nullable|string',
|
|
'has_lost' => 'boolean',
|
|
'historyable_id' => 'nullable|numeric',
|
|
'historyable_type' => 'required|string',
|
|
]);
|
|
|
|
// Преобразуем timestamp в дату, если пришли числа
|
|
if (isset($data['issue_at']) && is_numeric($data['issue_at'])) {
|
|
$data['issue_at'] = Carbon::createFromTimestampMs($data['issue_at'])->format('Y-m-d');
|
|
}
|
|
|
|
if (isset($data['return_at']) && is_numeric($data['return_at'])) {
|
|
$data['return_at'] = Carbon::createFromTimestampMs($data['return_at'])->format('Y-m-d');
|
|
}
|
|
|
|
$archiveHistory = ArchiveHistory::create($data);
|
|
|
|
// Если переданы данные для полиморфной связи
|
|
if ($request->filled('historyable_id') && $request->filled('historyable_type')) {
|
|
// Найти связанную модель
|
|
$historyableClass = $request->input('historyable_type');
|
|
|
|
// Проверяем, существует ли класс модели
|
|
if (class_exists($historyableClass)) {
|
|
$historyableModel = $historyableClass::find($request->input('historyable_id'));
|
|
|
|
if ($historyableModel) {
|
|
// Связываем модели
|
|
$archiveHistory->historyable()->associate($historyableModel);
|
|
$archiveHistory->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
return response()->json(ArchiveHistoryResource::make($archiveHistory));
|
|
}
|
|
|
|
public function moveUpdate($id, Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'issue_at' => ['nullable', new DateTimeOrStringOrNumber],
|
|
'return_at' => ['nullable', new DateTimeOrStringOrNumber],
|
|
'org_id' => 'required|numeric',
|
|
'employee_name' => 'nullable|string',
|
|
'employee_post' => 'nullable|string',
|
|
'comment' => 'nullable|string',
|
|
'has_lost' => 'boolean',
|
|
'historyable_id' => 'nullable|numeric',
|
|
'historyable_type' => 'required|string',
|
|
]);
|
|
|
|
// Преобразуем timestamp в дату, если пришли числа
|
|
if (isset($data['issue_at']) && is_numeric($data['issue_at'])) {
|
|
$data['issue_at'] = Carbon::createFromTimestampMs($data['issue_at'])->format('Y-m-d');
|
|
}
|
|
|
|
if (isset($data['return_at']) && is_numeric($data['return_at'])) {
|
|
$data['return_at'] = Carbon::createFromTimestampMs($data['return_at'])->format('Y-m-d');
|
|
}
|
|
|
|
$archiveHistory = ArchiveHistory::find($id);
|
|
|
|
$hasUpdated = $archiveHistory->update($data);
|
|
|
|
return response()->json(ArchiveHistoryResource::make($archiveHistory));
|
|
}
|
|
|
|
public function infoUpdate($patientId, Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'id' => 'required|numeric',
|
|
'num' => 'nullable|string',
|
|
'post_in' => ['nullable', new DateTimeOrStringOrNumber],
|
|
'historyable_type' => 'required|string',
|
|
]);
|
|
|
|
// Преобразуем timestamp в дату, если пришли числа
|
|
if (isset($data['post_in']) && is_numeric($data['post_in'])) {
|
|
$data['post_in'] = Carbon::createFromTimestampMs($data['post_in'])->format('Y-m-d');
|
|
}
|
|
|
|
if ($patientId && $request->filled('historyable_type')) {
|
|
// Найти связанную модель
|
|
$historyableClass = $request->input('historyable_type');
|
|
|
|
// Проверяем, существует ли класс модели
|
|
if (class_exists($historyableClass)) {
|
|
$historyableModel = $historyableClass::find($patientId);
|
|
|
|
if ($historyableModel) {
|
|
// Связываем модели
|
|
$historyableModel->archiveInfo()->updateOrCreate([
|
|
'historyable_type' => $historyableClass,
|
|
'historyable_id' => $patientId,
|
|
], $data);
|
|
return response()->json(ArchiveInfoResource::make($historyableModel->archiveInfo));
|
|
}
|
|
}
|
|
}
|
|
|
|
return response()->json()->setStatusCode(500);
|
|
}
|
|
}
|