first&last
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Requests;
|
||||
|
||||
use App\Enums\StatusSlug;
|
||||
use App\Models\Equipment;
|
||||
use App\Models\Priority;
|
||||
use App\Models\RequestType;
|
||||
use App\Models\ServiceRequest;
|
||||
use App\Models\Status;
|
||||
use App\Models\User;
|
||||
use App\Services\RequestService;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Component;
|
||||
use Livewire\Features\SupportFileUploads\WithFileUploads;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class RequestFormPage extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public ?ServiceRequest $serviceRequest = null;
|
||||
|
||||
public ?int $serviceRequestId = null;
|
||||
|
||||
public string $title = '';
|
||||
|
||||
public string $description = '';
|
||||
|
||||
public string $typeId = '';
|
||||
|
||||
public string $priorityId = '';
|
||||
|
||||
public string $statusId = '';
|
||||
|
||||
public string $clientId = '';
|
||||
|
||||
public string $assignedTo = '';
|
||||
|
||||
public string $equipmentId = '';
|
||||
|
||||
public string $location = '';
|
||||
|
||||
public string $plannedAt = '';
|
||||
|
||||
public string $deadlineAt = '';
|
||||
|
||||
public string $resolutionNotes = '';
|
||||
|
||||
public ?int $clientRating = null;
|
||||
|
||||
public string $clientComment = '';
|
||||
|
||||
public array $attachments = [];
|
||||
|
||||
public function mount(?ServiceRequest $request = null): void
|
||||
{
|
||||
abort_unless(auth()->user()->isAdmin() || auth()->user()->isManager(), 403);
|
||||
|
||||
if ($request && $request->exists) {
|
||||
$this->serviceRequest = $request;
|
||||
$this->serviceRequestId = $request->id;
|
||||
$this->title = $request->title;
|
||||
$this->description = $request->description;
|
||||
$this->typeId = (string) $request->type_id;
|
||||
$this->priorityId = (string) $request->priority_id;
|
||||
$this->statusId = (string) $request->status_id;
|
||||
$this->clientId = (string) $request->client_id;
|
||||
$this->assignedTo = (string) $request->assigned_to;
|
||||
$this->equipmentId = (string) $request->equipment_id;
|
||||
$this->location = (string) $request->location;
|
||||
$this->plannedAt = (string) $request->planned_at?->format('Y-m-d\TH:i');
|
||||
$this->deadlineAt = (string) $request->deadline_at?->format('Y-m-d\TH:i');
|
||||
$this->resolutionNotes = (string) $request->resolution_notes;
|
||||
$this->clientRating = $request->client_rating;
|
||||
$this->clientComment = (string) $request->client_comment;
|
||||
} else {
|
||||
$this->statusId = (string) Status::query()->where('slug', StatusSlug::NEW->value)->value('id');
|
||||
}
|
||||
}
|
||||
|
||||
public function updatedTypeId(): void
|
||||
{
|
||||
if (!$this->typeId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$type = RequestType::query()->find($this->typeId);
|
||||
|
||||
if ($type) {
|
||||
$base = $this->plannedAt !== '' ? Carbon::parse($this->plannedAt) : now();
|
||||
$this->deadlineAt = $base->addHours($type->sla_hours)->format('Y-m-d\TH:i');
|
||||
}
|
||||
}
|
||||
|
||||
public function save(RequestService $requestService)
|
||||
{
|
||||
$this->validate([
|
||||
'title' => ['required', 'string', 'max:255'],
|
||||
'description' => ['required', 'string'],
|
||||
'typeId' => ['required', 'exists:request_types,id'],
|
||||
'priorityId' => ['required', 'exists:priorities,id'],
|
||||
'statusId' => ['nullable', 'exists:statuses,id'],
|
||||
'clientId' => ['required', 'exists:users,id'],
|
||||
'assignedTo' => ['nullable', 'exists:users,id'],
|
||||
'equipmentId' => ['nullable', 'exists:equipment,id'],
|
||||
'location' => ['nullable', 'string', 'max:255'],
|
||||
'plannedAt' => ['nullable', 'date'],
|
||||
'deadlineAt' => ['nullable', 'date'],
|
||||
'resolutionNotes' => ['nullable', 'string'],
|
||||
'clientRating' => ['nullable', 'integer', 'between:1,5'],
|
||||
'clientComment' => ['nullable', 'string', 'max:2000'],
|
||||
'attachments' => ['nullable', 'array', 'max:5'],
|
||||
'attachments.*' => ['file', 'max:10240'],
|
||||
]);
|
||||
|
||||
if (!$this->serviceRequestId && empty($this->statusId)) {
|
||||
throw ValidationException::withMessages([
|
||||
'statusId' => 'Не удалось определить статус заявки.',
|
||||
]);
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'title' => $this->title,
|
||||
'description' => $this->description,
|
||||
'type_id' => $this->typeId,
|
||||
'priority_id' => $this->priorityId,
|
||||
'status_id' => $this->statusId,
|
||||
'client_id' => $this->clientId,
|
||||
'assigned_to' => $this->assignedTo ?: null,
|
||||
'equipment_id' => $this->equipmentId ?: null,
|
||||
'location' => $this->location,
|
||||
'planned_at' => $this->plannedAt ?: null,
|
||||
'deadline_at' => $this->deadlineAt ?: null,
|
||||
'resolution_notes' => $this->resolutionNotes ?: null,
|
||||
'client_rating' => $this->clientRating,
|
||||
'client_comment' => $this->clientComment ?: null,
|
||||
'attachments' => $this->attachments,
|
||||
];
|
||||
|
||||
if ($this->serviceRequestId && $this->serviceRequest) {
|
||||
$request = $requestService->update($this->serviceRequest, $payload, auth()->user());
|
||||
session()->flash('success', 'Заявка успешно обновлена.');
|
||||
|
||||
return redirect()->route('requests.show', $request);
|
||||
}
|
||||
|
||||
$request = $requestService->create($payload, auth()->user());
|
||||
session()->flash('success', 'Заявка успешно создана.');
|
||||
|
||||
return redirect()->route('requests.show', $request);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$clientId = $this->clientId !== '' ? (int) $this->clientId : null;
|
||||
|
||||
return view('livewire.requests.request-form-page', [
|
||||
'types' => RequestType::query()->orderBy('name')->get(),
|
||||
'priorities' => Priority::query()->orderBy('level')->get(),
|
||||
'statuses' => Status::query()->orderBy('sort_order')->get(),
|
||||
'clients' => User::query()->where('role', 'client')->orderBy('name')->get(),
|
||||
'technicians' => User::query()->where('role', 'technician')->orderBy('name')->get(),
|
||||
'equipmentItems' => Equipment::query()
|
||||
->when($clientId, fn ($q) => $q->where('client_id', $clientId))
|
||||
->orderBy('name')
|
||||
->get(),
|
||||
])->title($this->serviceRequestId ? 'Редактирование заявки' : 'Создание заявки');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user