feat: Add company association to projects with role management

- Created Company model and migration with fields: name, tax_id, address, phone, email, website, type, notes
- Created company_project pivot table with role_in_project field
- Added relationships: Project.companies() and Company.projects()
- Created Livewire component ProjectCompanies for managing company assignments
- Added 'Companies' tab to project edit interface alongside Phases and Users tabs
- Implemented assign/remove company functionality with role selection
- Applied same permissions logic as user assignment (assign users permission or Admin role)
This commit is contained in:
2026-05-13 11:20:33 +02:00
parent 69e6c7889a
commit a9000d453e
11 changed files with 964 additions and 0 deletions
@@ -0,0 +1,76 @@
<div>
@if(session()->has('message'))
<div class="alert alert-success mb-2 text-sm">{{ session('message') }}</div>
@endif
@if(session()->has('error'))
<div class="alert alert-error mb-2 text-sm">{{ session('error') }}</div>
@endif
{{-- Asignar compañía --}}
@can('assign users')
<form wire:submit.prevent="assignCompany" class="flex items-end gap-2 mb-4">
<div class="flex-1">
<label class="label-text text-xs">{{ __('Companies') }}</label>
<select wire:model="selectedCompanyId" class="select select-bordered select-sm w-full">
<option value="">{{ __('Select') }}...</option>
@foreach($allCompanies as $company)
<option value="{{ $company->id }}">{{ $company->name }} @if($company->tax_id) ({{ $company->tax_id }}) @endif</option>
@endforeach
</select>
</div>
<div class="w-32">
<label class="label-text text-xs">{{ __('Role') }}</label>
<select wire:model="selectedRole" class="select select-bordered select-sm w-full">
<option value="owner">{{ __('Owner') }}</option>
<option value="constructor">{{ __('Constructor') }}</option>
<option value="subcontractor">{{ __('Subcontractor') }}</option>
<option value="consultant">{{ __('Consultant') }}</option>
<option value="supplier">{{ __('Supplier') }}</option>
<option value="other">{{ __('Other') }}</option>
</select>
</div>
<button type="submit" class="btn btn-primary btn-sm">{{ __('Assign') }}</button>
</form>
@endcan
{{-- Lista de compañías asignadas --}}
@if($assignedCompanies->isNotEmpty())
<div class="space-y-1">
@foreach($assignedCompanies as $company)
<div class="flex items-center justify-between p-2 border rounded text-sm">
<div class="flex items-center gap-2">
<span class="w-6 h-6 rounded-full bg-primary text-white flex items-center justify-center text-xs font-bold">
{{ strtoupper(substr($company->name, 0, 1)) }}
</span>
<div>
<span class="font-medium">{{ $company->name }}</span>
@if($company->tax_id)
<span class="text-xs text-gray-400 ml-1">{{ $company->tax_id }}</span>
@endif
</div>
</div>
<div class="flex items-center gap-1">
@can('assign users')
<select wire:change="changeRole({{ $company->id }}, $event.target.value)"
class="select select-bordered select-xs">
<option value="owner" @selected($company->pivot->role_in_project == 'owner')>{{ __('Owner') }}</option>
<option value="constructor" @selected($company->pivot->role_in_project == 'constructor')>{{ __('Constructor') }}</option>
<option value="subcontractor" @selected($company->pivot->role_in_project == 'subcontractor')>{{ __('Subcontractor') }}</option>
<option value="consultant" @selected($company->pivot->role_in_project == 'consultant')>{{ __('Consultant') }}</option>
<option value="supplier" @selected($company->pivot->role_in_project == 'supplier')>{{ __('Supplier') }}</option>
<option value="other" @selected($company->pivot->role_in_project == 'other')>{{ __('Other') }}</option>
</select>
<button wire:click="removeCompany({{ $company->id }})"
class="btn btn-xs btn-ghost text-error"
onclick="return confirm('{{ __('Remove') }} {{ $company->name }}?')"></button>
@else
<span class="badge badge-sm">{{ ucfirst($company->pivot->role_in_project) }}</span>
@endcan
</div>
</div>
@endforeach
</div>
@else
<p class="text-sm text-gray-400 text-center py-4">{{ __('No companies assigned yet') }}</p>
@endif
</div>
@@ -18,6 +18,12 @@
<label for="tab-users-{{ $project->id }}" class="tab {{ $activeTab === 'users' ? 'tab-active' : '' }}">
{{ __('Users') }}
</label>
<input type="radio" name="tabs-project-edit-{{ $project->id }}" id="tab-companies-{{ $project->id }}"
{{ $activeTab === 'companies' ? 'checked' : '' }} class="tab-toggle" />
<label for="tab-companies-{{ $project->id }}" class="tab {{ $activeTab === 'companies' ? 'tab-active' : '' }}">
{{ __('Companies') }}
</label>
</div>
<!-- Tab Content -->
@@ -0,0 +1,3 @@
<div>
{{-- Nothing in the world is as soft and yielding as water. --}}
</div>
@@ -0,0 +1,160 @@
<div>
<div class="bg-base-100 p-4 rounded shadow">
<div class="flex justify-between items-center mb-4">
<h2 class="text-xl font-bold">📋 Templates de inspección</h2>
<div>
<button wire:click="newTemplate" class="btn btn-primary btn-sm">
Nuevo template
</button>
</div>
</div>
@if(session()->has('message'))
<div class="alert alert-success mb-4">{{ session('message') }}</div>
@endif
{{-- Formulario de creación/edición con diseño de dos columnas --}}
@if($showForm)
<form wire:submit.prevent="saveTemplate" class="border p-4 rounded mb-6 bg-base-200">
<table class="w-full mb-8">
<tbody>
{{-- Nombre del template --}}
<tr>
<td class="w-1/4 py-3 pr-4 align-top">
{{__('Nombre del template')}}
</td>
<td class="py-3">
<input type="text" wire:model="form.name"
class="input w-full"
required>
</td>
</tr>
{{-- Descripción --}}
<tr>
<td class="w-1/4 py-3 pr-4 align-top">
{{__('Descripción')}}
</td>
<td class="py-3">
<textarea wire:model="form.description" class="textarea textarea-bordered w-full" rows="2"></textarea>
</td>
</tr>
{{-- Fase asociada (opcional) --}}
<tr>
<td class="w-1/4 py-3 pr-4 align-top">
{{__('Fase asociada (opcional)')}}
</td>
<td class="py-3">
<select wire:model="form.phase_id" class="select select-bordered w-full">
<option value="">Ninguna (global para el proyecto)</option>
@foreach($phases as $phase)
<option value="{{ $phase->id }}" {{ old('form.phase_id') == $phase->id ? 'selected' : '' }}>
{{ $phase->name }}
</option>
@endforeach
</select>
</td>
</tr>
</tbody>
</table>
{{-- Campos dinámicos --}}
<div class="border-t pt-4 mt-2">
<h3 class="font-bold mb-3">Campos del formulario</h3>
@foreach($form['fields'] as $index => $field)
<div class="border p-3 rounded mb-3 bg-base-100">
{{-- Fila: nombre interno --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">Nombre interno</div>
<div><input type="text" wire:model="form.fields.{{ $index }}.name" placeholder="ej: altura_medida" class="input input-sm w-full"></div>
</div>
{{-- Fila: etiqueta --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">Etiqueta visible</div>
<div><input type="text" wire:model="form.fields.{{ $index }}.label" placeholder="ej: Altura medida (m)" class="input input-sm w-full"></div>
</div>
{{-- Fila: tipo --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">Tipo de campo</div>
<div>
<select wire:model="form.fields.{{ $index }}.type" class="select select-sm w-full">
@foreach($fieldTypes as $typeValue => $typeLabel)
<option value="{{ $typeValue }}">{{ $typeLabel }}</option>
@endforeach
</select>
</div>
</div>
{{-- Fila: requerido y botón eliminar --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">Requerido</div>
<div class="flex justify-between items-center">
<input type="checkbox" wire:model="form.fields.{{ $index }}.required" class="checkbox checkbox-sm">
<button type="button" wire:click="removeField({{ $index }})" class="btn btn-xs btn-error">Eliminar campo</button>
</div>
</div>
{{-- Campos adicionales según tipo --}}
@if(in_array($field['type'], ['integer', 'decimal', 'percentage']))
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">Mínimo / Máximo / Paso</div>
<div class="flex gap-2">
<input type="number" wire:model="form.fields.{{ $index }}.min" placeholder="Mín" class="input input-xs w-20">
<input type="number" wire:model="form.fields.{{ $index }}.max" placeholder="Máx" class="input input-xs w-20">
<input type="number" step="any" wire:model="form.fields.{{ $index }}.step" placeholder="Paso" class="input input-xs w-20">
</div>
</div>
@elseif($field['type'] === 'select')
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">Opciones (separadas por coma)</div>
<div><input type="text" wire:model="form.fields.{{ $index }}.options" placeholder="ej: Bueno,Regular,Malo" class="input input-sm w-full"></div>
</div>
@endif
</div>
@endforeach
<button type="button" wire:click="addField" class="btn btn-sm btn-secondary mt-2">+ Agregar campo</button>
</div>
<div class="flex gap-2 mt-4">
<button type="submit" class="btn btn-primary">Guardar template</button>
<button type="button" wire:click="cancelForm" class="btn">Cancelar</button>
</div>
</form>
@endif
{{-- Tabla de templates existentes --}}
<div class="overflow-x-auto">
<table class="table table-zebra">
<thead>
<tr>
<th>Nombre</th>
<th>Descripción</th>
<th>Fase</th>
<th>Campos</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
@forelse($templates as $template)
<tr>
<td>{{ $template->name }}</td>
<td>{{ $template->description ?? '-' }}</td>
<td>{{ $template->phase ? $template->phase->name : 'Global' }}</td>
<td>{{ count($template->fields) }}</td>
<td>
<button wire:click="editTemplate({{ $template->id }})" class="btn btn-xs btn-warning">
Editar
</button>
<button wire:click="deleteTemplate({{ $template->id }})" class="btn btn-xs btn-error" onclick="return confirm('¿Eliminar template?')">Eliminar</button>
</td>
</tr>
@empty
<tr>
<td colspan="5" class="text-center">No hay templates creados. Presiona "Nuevo template" para comenzar.</td>
</tr>
@endforelse
</tbody>
</table>
</div>
</div>
</div>