Files
kartoteka/app/Http/Resources/Mis/SttMedicalHistoryResource.php
2025-12-25 17:30:50 +09:00

129 lines
5.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Resources\Mis;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class SttMedicalHistoryResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
// Определяем источник данных
$isFromArchive = $this->resource['in_archive'] ?? false;
$isTemporary = $this->resource['is_temporary'] ?? false;
$historyType = $this->resource['history_type'] ?? 'mis';
// Формируем ФИО
$family = $this->resource['family'] ?? '';
$name = $this->resource['name'] ?? '';
$ot = $this->resource['ot'] ?? '';
// Для временных записей (не в архиве) используем данные из MIS
if ($isTemporary) {
// Данные из stt_medicalhistory (не в архиве)
$fullName = trim("{$family} {$name} {$ot}");
$birthDate = $this->resource['birth_date'] ?? null;
$dateExtract = $this->resource['date_extract'] ?? null;
$dateRecipient = null; // Для MIS записей не в архиве может не быть
$cardNumber = $this->resource['card_number'] ?? null;
$archiveNum = null;
$postIn = null;
$status = $this->resource['status_text'] ?? 'Не в архиве';
} else {
// Данные из archive_infos (в архиве)
$fullName = trim("{$family} {$name} {$ot}");
$birthDate = $this->resource['birth_date'] ?? null;
// Для архивных записей date_extract может быть из MIS или FoxPro
$dateExtract = $this->resource['date_extract'] ?? null;
// Для MIS записей в архиве
if ($historyType === 'mis') {
$dateRecipient = $this->resource['date_recipient'] ?? null;
} else {
// Для FoxPro записей в архиве
$dateRecipient = $this->resource['mpostdate'] ?? null;
}
$cardNumber = $this->resource['card_number'] ?? null;
$archiveNum = $this->resource['archive_num'] ?? null;
$postIn = $this->resource['post_in'] ?? null;
$status = $this->resource['status_text'] ?? 'Неизвестно';
}
// Форматирование дат
$formattedBirthDate = $birthDate ? Carbon::parse($birthDate)->format('d.m.Y') : null;
$formattedDateRecipient = $dateRecipient ? Carbon::parse($dateRecipient)->format('d.m.Y') : null;
$formattedDateExtract = $dateExtract ? Carbon::parse($dateExtract)->format('d.m.Y') : null;
$formattedPostIn = $postIn ? Carbon::parse($postIn)->format('d.m.Y') : null;
return [
'id' => $this->resource['id'],
'history_type' => $historyType,
'in_archive' => $isFromArchive,
'is_temporary' => $isTemporary,
'source' => $this->resource['source'] ?? 'archive',
// Основные данные
'fullname' => $fullName,
'family' => $family,
'name' => $name,
'ot' => $ot,
'dr' => $formattedBirthDate,
'daterecipient' => $formattedDateRecipient,
'dateextract' => $formattedDateExtract,
// Номера карт
'medcardnum' => $cardNumber, // MIS номер или FoxPro номер
'mis_card_number' => $this->resource['mis_card_number'] ?? null, // Оригинальный MIS номер
'foxpro_card_number' => $this->resource['foxpro_card_number'] ?? null, // Оригинальный FoxPro номер
'card_num' => $archiveNum, // Архивный номер
'datearhiv' => $formattedPostIn,
// Статус и возможности
'status' => $status,
'status_id' => $this->resource['status_id'] ?? 0,
'can_be_issued' => $this->resource['can_be_issued'] ?? false,
'can_add_to_archive' => $this->resource['can_add_to_archive'] ?? false,
// Дополнительные идентификаторы
'mis_history_id' => $this->resource['mis_history_id'] ?? null,
'foxpro_history_id' => $this->resource['foxpro_history_id'] ?? null,
// Дополнительные данные
'snils' => $this->resource['snils'] ?? null,
'enp' => $this->resource['enp'] ?? null,
// 'created_at' => $this->resource->created_at ? Carbon::parse($this->resource->created_at)->format('d.m.Y H:i') : null,
// 'updated_at' => $this->resource->updated_at ? Carbon::parse($this->resource->updated_at)->format('d.m.Y H:i') : null,
// Стили для фронта
'row_class' => $this->resource['row_class'] ?? '',
'status_color' => $this->getStatusColor($this->resource['status_id'] ?? 0, $isFromArchive),
];
}
/**
* Получение цвета статуса для фронта
*/
private function getStatusColor(int $statusId, bool $inArchive): string
{
if (!$inArchive) {
return 'warning'; // Желтый для не в архиве
}
return match($statusId) {
1 => 'success', // Зеленый для в архиве
2 => 'info', // Синий для выдано
3, 4 => 'danger', // Красный для утрачено/списано
default => 'secondary',
};
}
}