first&last
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Directories;
|
||||
|
||||
use App\Models\Equipment;
|
||||
use App\Models\EquipmentCategory;
|
||||
use App\Models\User;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class EquipmentPage extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public string $name = '';
|
||||
|
||||
public string $serialNumber = '';
|
||||
|
||||
public string $inventoryNumber = '';
|
||||
|
||||
public string $categoryId = '';
|
||||
|
||||
public string $clientId = '';
|
||||
|
||||
public string $purchaseDate = '';
|
||||
|
||||
public string $warrantyUntil = '';
|
||||
|
||||
public string $condition = 'good';
|
||||
|
||||
public string $notes = '';
|
||||
|
||||
public ?int $editingId = null;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
abort_unless(auth()->user()->isAdmin(), 403);
|
||||
}
|
||||
|
||||
public function edit(int $id): void
|
||||
{
|
||||
$model = Equipment::query()->findOrFail($id);
|
||||
$this->editingId = $model->id;
|
||||
$this->name = $model->name;
|
||||
$this->serialNumber = $model->serial_number;
|
||||
$this->inventoryNumber = $model->inventory_number;
|
||||
$this->categoryId = (string) $model->category_id;
|
||||
$this->clientId = (string) $model->client_id;
|
||||
$this->purchaseDate = (string) $model->purchase_date;
|
||||
$this->warrantyUntil = (string) $model->warranty_until;
|
||||
$this->condition = $model->condition->value;
|
||||
$this->notes = (string) $model->notes;
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$serialRule = Rule::unique('equipment', 'serial_number');
|
||||
$inventoryRule = Rule::unique('equipment', 'inventory_number');
|
||||
|
||||
if ($this->editingId) {
|
||||
$serialRule = $serialRule->ignore($this->editingId);
|
||||
$inventoryRule = $inventoryRule->ignore($this->editingId);
|
||||
}
|
||||
|
||||
$this->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'serialNumber' => ['required', 'string', $serialRule],
|
||||
'inventoryNumber' => ['required', 'string', $inventoryRule],
|
||||
'categoryId' => ['required', 'exists:equipment_categories,id'],
|
||||
'clientId' => ['nullable', 'exists:users,id'],
|
||||
'purchaseDate' => ['nullable', 'date'],
|
||||
'warrantyUntil' => ['nullable', 'date'],
|
||||
'condition' => ['required', Rule::in(['good', 'fair', 'poor'])],
|
||||
'notes' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
Equipment::query()->updateOrCreate(
|
||||
['id' => $this->editingId],
|
||||
[
|
||||
'name' => $this->name,
|
||||
'serial_number' => $this->serialNumber,
|
||||
'inventory_number' => $this->inventoryNumber,
|
||||
'category_id' => $this->categoryId,
|
||||
'client_id' => $this->clientId ?: null,
|
||||
'purchase_date' => $this->purchaseDate ?: null,
|
||||
'warranty_until' => $this->warrantyUntil ?: null,
|
||||
'condition' => $this->condition,
|
||||
'notes' => $this->notes,
|
||||
]
|
||||
);
|
||||
|
||||
$this->reset(['name', 'serialNumber', 'inventoryNumber', 'categoryId', 'clientId', 'purchaseDate', 'warrantyUntil', 'notes', 'editingId']);
|
||||
$this->condition = 'good';
|
||||
session()->flash('success', 'Оборудование сохранено.');
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
Equipment::query()->findOrFail($id)->delete();
|
||||
session()->flash('success', 'Оборудование удалено.');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.directories.equipment-page', [
|
||||
'items' => Equipment::query()->with(['category', 'client'])->latest()->paginate(15),
|
||||
'categories' => EquipmentCategory::query()->orderBy('name')->get(),
|
||||
'clients' => User::query()->where('role', 'client')->orderBy('name')->get(),
|
||||
])->title('Справочник: Оборудование');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user