84 lines
3.3 KiB
PHP
84 lines
3.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Livewire\Projects;
|
||
|
|
|
||
|
|
use App\Models\Inspection;
|
||
|
|
use App\Models\Project;
|
||
|
|
use Illuminate\Database\Eloquent\Builder;
|
||
|
|
use Illuminate\Support\Facades\Auth;
|
||
|
|
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||
|
|
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||
|
|
|
||
|
|
class InspectionTable extends DataTableComponent
|
||
|
|
{
|
||
|
|
protected $model = Inspection::class;
|
||
|
|
|
||
|
|
public int $projectId;
|
||
|
|
|
||
|
|
public function configure(): void
|
||
|
|
{
|
||
|
|
$this->setPrimaryKey('id')
|
||
|
|
->setDefaultSort('inspections.created_at', 'desc')
|
||
|
|
->setSortingPillsEnabled(false)
|
||
|
|
->setAdditionalSelects([
|
||
|
|
'inspections.id as id',
|
||
|
|
'inspections.created_at as created_at',
|
||
|
|
'inspections.feature_id as feature_id',
|
||
|
|
'inspections.template_id as template_id',
|
||
|
|
'inspections.user_id as user_id',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function builder(): Builder
|
||
|
|
{
|
||
|
|
$user = Auth::user();
|
||
|
|
abort_unless(
|
||
|
|
$user->can('manage all') ||
|
||
|
|
Project::whereKey($this->projectId)->whereHas('users', fn ($q) => $q->where('user_id', $user->id))->exists(),
|
||
|
|
403
|
||
|
|
);
|
||
|
|
|
||
|
|
return Inspection::query()
|
||
|
|
->where('inspections.project_id', $this->projectId)
|
||
|
|
->with(['feature', 'template', 'user']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function columns(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
Column::make('Fecha', 'created_at')
|
||
|
|
->sortable()
|
||
|
|
->format(fn ($value, $row) => $row->created_at?->format('d/m/Y') ?? '—'),
|
||
|
|
|
||
|
|
Column::make('Elemento')
|
||
|
|
->label(fn ($row) => $row->feature?->name
|
||
|
|
? '<span class="font-medium">' . e($row->feature->name) . '</span>'
|
||
|
|
: '<span class="text-base-content/30 text-xs">—</span>')
|
||
|
|
->html(),
|
||
|
|
|
||
|
|
Column::make('Plantilla')
|
||
|
|
->label(fn ($row) => e($row->template?->name ?? '—')),
|
||
|
|
|
||
|
|
Column::make('Resultado', 'result')
|
||
|
|
->sortable()
|
||
|
|
->format(fn ($value) => $value
|
||
|
|
? '<span class="badge badge-sm badge-outline">' . e($value) . '</span>'
|
||
|
|
: '<span class="text-base-content/30 text-xs">—</span>')
|
||
|
|
->html(),
|
||
|
|
|
||
|
|
Column::make('Usuario')
|
||
|
|
->label(fn ($row) => e($row->user?->name ?? '—')),
|
||
|
|
|
||
|
|
Column::make('Acciones')
|
||
|
|
->label(fn ($row) =>
|
||
|
|
'<div class="flex justify-end">
|
||
|
|
<button wire:click="$dispatch(\'map-view-inspection\', { id: ' . $row->id . ' })"
|
||
|
|
class="btn btn-xs btn-ghost" title="Ver inspección">
|
||
|
|
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
|
||
|
|
</button>
|
||
|
|
</div>')
|
||
|
|
->html(),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|