feat(templates): exponer importación desde otro proyecto y desde CSV/Excel

El TemplateManager ya implementaba ambos flujos pero la vista no los exponía.
Añadidos a la cabecera los botones "Importar de proyecto" e "Importar CSV/Excel" y
sus modales:
- CSV/Excel: nombre + fichero, vista previa de campos detectados, crear; con enlace
  para descargar CSV de ejemplo.
- Otro proyecto: selección de proyecto accesible + checklist de plantillas a importar.

Tests: TemplateImportTest (importar de proyecto y desde CSV). Suite 81 passing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-25 14:18:21 +02:00
co-authored by Claude Opus 4.8
parent 9fc5854bbb
commit 1decb19bba
2 changed files with 213 additions and 1 deletions
@@ -2,7 +2,15 @@
<div class="bg-base-100 p-4 rounded shadow">
<div class="flex justify-between items-center mb-4">
<h2 class="text-xl font-bold">📋 {{ __('Inspection templates') }}</h2>
<div>
<div class="flex items-center gap-2">
<button wire:click="openImportProjectModal" class="btn btn-outline btn-sm gap-1" title="{{ __('Import from another project') }}">
<x-heroicon-o-arrow-down-on-square-stack class="w-4 h-4" />
{{ __('Import from project') }}
</button>
<button wire:click="openImportFileModal" class="btn btn-outline btn-sm gap-1" title="{{ __('Import from CSV/Excel') }}">
<x-heroicon-o-document-arrow-up class="w-4 h-4" />
{{ __('Import CSV/Excel') }}
</button>
<button wire:click="newTemplate" class="btn btn-primary btn-sm">
{{ __('New template') }}
</button>
@@ -159,4 +167,129 @@
</table>
</div>
</div>
{{-- ════════════════════════════════════════════════════════════
MODAL: Importar desde CSV/Excel
════════════════════════════════════════════════════════════ --}}
@if($showImportFileModal)
<div class="fixed inset-0 z-40 bg-black/50" wire:click="$set('showImportFileModal', false)"></div>
<div class="fixed inset-0 z-50 flex items-center justify-center p-4">
<div class="bg-base-100 rounded-box shadow-2xl w-full max-w-2xl max-h-[90vh] overflow-y-auto">
<div class="flex items-center justify-between p-4 border-b border-base-300">
<h3 class="font-bold">{{ __('Import from CSV/Excel') }}</h3>
<button wire:click="$set('showImportFileModal', false)" class="btn btn-sm btn-ghost btn-circle">
<x-heroicon-o-x-mark class="w-4 h-4" />
</button>
</div>
<div class="p-4 space-y-3">
<p class="text-xs text-base-content/60">
{{ __('Columns: name, label, type, required, options, min, max, step') }}.
<button type="button" wire:click="downloadExampleCsv" class="link link-primary">{{ __('Download example CSV') }}</button>
</p>
<div class="form-control">
<label class="label"><span class="label-text font-medium">{{ __('Template name') }} <span class="text-error">*</span></span></label>
<input type="text" wire:model="importTemplateName" class="input input-bordered w-full"
placeholder="{{ __('e.g. Concrete reception') }}" />
@error('importTemplateName')<span class="text-xs text-error">{{ $message }}</span>@enderror
</div>
<div class="form-control">
<label class="label"><span class="label-text font-medium">{{ __('File (CSV / Excel)') }} <span class="text-error">*</span></span></label>
<input type="file" wire:model="importFile" accept=".csv,.txt,.xlsx,.xls" class="file-input file-input-bordered w-full" />
<div wire:loading wire:target="importFile" class="text-xs text-base-content/50 mt-1">{{ __('Uploading…') }}</div>
@error('importFile')<span class="text-xs text-error">{{ $message }}</span>@enderror
</div>
@if($importError)
<div class="alert alert-error text-sm py-2">{{ $importError }}</div>
@endif
<div class="flex justify-end">
<button wire:click="parseImportFile" class="btn btn-sm btn-secondary gap-1" wire:loading.attr="disabled" wire:target="parseImportFile,importFile">
<x-heroicon-o-eye class="w-4 h-4" /> {{ __('Preview') }}
</button>
</div>
{{-- Vista previa de campos detectados --}}
@if(!empty($importPreviewFields))
<div class="border border-base-300 rounded-box p-2">
<p class="text-sm font-medium mb-2">{{ count($importPreviewFields) }} {{ __('fields detected') }}:</p>
<div class="overflow-x-auto">
<table class="table table-xs">
<thead><tr><th>{{ __('Label') }}</th><th>{{ __('Name') }}</th><th>{{ __('Type') }}</th><th>{{ __('Required') }}</th></tr></thead>
<tbody>
@foreach($importPreviewFields as $f)
<tr>
<td>{{ $f['label'] }}</td>
<td class="font-mono text-xs">{{ $f['name'] }}</td>
<td><span class="badge badge-ghost badge-sm">{{ $f['type'] }}</span></td>
<td>{{ !empty($f['required']) ? '✓' : '' }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endif
</div>
<div class="flex justify-end gap-2 p-4 border-t border-base-300">
<button wire:click="$set('showImportFileModal', false)" class="btn btn-ghost btn-sm">{{ __('Cancel') }}</button>
<button wire:click="confirmImportFile" class="btn btn-primary btn-sm" @disabled(empty($importPreviewFields))>
{{ __('Create template') }}
</button>
</div>
</div>
</div>
@endif
{{-- ════════════════════════════════════════════════════════════
MODAL: Importar desde otro proyecto
════════════════════════════════════════════════════════════ --}}
@if($showImportProjectModal)
<div class="fixed inset-0 z-40 bg-black/50" wire:click="$set('showImportProjectModal', false)"></div>
<div class="fixed inset-0 z-50 flex items-center justify-center p-4">
<div class="bg-base-100 rounded-box shadow-2xl w-full max-w-lg max-h-[90vh] overflow-y-auto">
<div class="flex items-center justify-between p-4 border-b border-base-300">
<h3 class="font-bold">{{ __('Import from another project') }}</h3>
<button wire:click="$set('showImportProjectModal', false)" class="btn btn-sm btn-ghost btn-circle">
<x-heroicon-o-x-mark class="w-4 h-4" />
</button>
</div>
<div class="p-4 space-y-3">
<div class="form-control">
<label class="label"><span class="label-text font-medium">{{ __('Project') }}</span></label>
<select wire:model.live="importProjectId" class="select select-bordered w-full">
<option value="">{{ __('Select project...') }}</option>
@foreach($availableProjects as $p)
<option value="{{ $p->id }}">{{ $p->name }}</option>
@endforeach
</select>
</div>
@if($importProjectId)
@if(count($importableTemplates))
<div class="border border-base-300 rounded-box p-2 space-y-1 max-h-64 overflow-y-auto">
@foreach($importableTemplates as $t)
<label class="flex items-center gap-2 p-1 hover:bg-base-200 rounded cursor-pointer">
<input type="checkbox" wire:model="selectedImportTemplateIds" value="{{ $t->id }}" class="checkbox checkbox-sm" />
<span class="text-sm">{{ $t->name }}</span>
<span class="text-xs text-base-content/50">({{ count($t->fields ?? []) }} {{ __('fields') }})</span>
</label>
@endforeach
</div>
@else
<p class="text-sm text-base-content/40 py-2">{{ __('This project has no templates.') }}</p>
@endif
@endif
</div>
<div class="flex justify-end gap-2 p-4 border-t border-base-300">
<button wire:click="$set('showImportProjectModal', false)" class="btn btn-ghost btn-sm">{{ __('Cancel') }}</button>
<button wire:click="importFromProject" class="btn btn-primary btn-sm" @disabled(empty($selectedImportTemplateIds))>
{{ __('Import selected') }}
</button>
</div>
</div>
</div>
@endif
</div>
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace Tests\Feature;
use App\Livewire\Inspections\TemplateManager;
use App\Models\InspectionTemplate;
use App\Models\Project;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Livewire\Livewire;
use Tests\TestCase;
class TemplateImportTest extends TestCase
{
use RefreshDatabase;
private function project(User $member, string $ref): Project
{
$p = Project::create([
'reference' => $ref, 'name' => 'Proyecto ' . $ref, 'address' => 'x',
'lat' => 40.0, 'lng' => -3.0, 'start_date' => now()->toDateString(),
'end_date_estimated' => now()->addMonth()->toDateString(),
'status' => 'in_progress', 'created_by' => $member->id,
]);
$p->users()->attach($member->id, ['role_in_project' => 'supervisor']);
return $p;
}
public function test_import_templates_from_another_project(): void
{
$user = User::factory()->create();
$source = $this->project($user, 'SRC');
$target = $this->project($user, 'DST');
$tpl = InspectionTemplate::create([
'project_id' => $source->id, 'name' => 'Recepción acero',
'fields' => [['name' => 'ok', 'label' => 'OK', 'type' => 'boolean']],
]);
Livewire::actingAs($user)
->test(TemplateManager::class, ['project' => $target])
->call('openImportProjectModal')
->set('importProjectId', $source->id)
->set('selectedImportTemplateIds', [$tpl->id])
->call('importFromProject')
->assertHasNoErrors();
$this->assertDatabaseHas('inspection_templates', [
'project_id' => $target->id, 'name' => 'Recepción acero',
]);
}
public function test_import_template_from_csv(): void
{
$user = User::factory()->create();
$project = $this->project($user, 'CSV');
$csv = "name,label,type,required,options,min,max,step\n"
. "resistencia,Resistencia,integer,1,,,,\n"
. "acabado,Acabado,select,0,bueno;regular;malo,,,\n";
$file = UploadedFile::fake()->createWithContent('campos.csv', $csv);
Livewire::actingAs($user)
->test(TemplateManager::class, ['project' => $project])
->call('openImportFileModal')
->set('importTemplateName', 'Plantilla CSV')
->set('importFile', $file)
->call('parseImportFile')
->assertHasNoErrors()
->call('confirmImportFile');
$this->assertDatabaseHas('inspection_templates', [
'project_id' => $project->id, 'name' => 'Plantilla CSV',
]);
$tpl = InspectionTemplate::where('name', 'Plantilla CSV')->first();
$this->assertCount(2, $tpl->fields);
}
}