Files
construprogress/app/Livewire/ProjectCompanies.php
T

81 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Livewire;
use Livewire\Component;
use App\Models\Project;
use App\Models\Company;
use Illuminate\Support\Facades\Auth;
class ProjectCompanies extends Component
{
public Project $project;
public $assignedCompanies = [];
public $allCompanies = [];
public $selectedCompanyId = '';
public $selectedRole = 'other';
public function mount(Project $project)
{
$this->project = $project;
$this->loadCompanies();
}
public function loadCompanies()
{
$this->assignedCompanies = $this->project->companies()->withPivot('role_in_project')->get();
$assignedIds = $this->assignedCompanies->pluck('id')->toArray();
$this->allCompanies = Company::whereNotIn('id', $assignedIds)->orderBy('name')->get();
}
public function assignCompany()
{
$user = Auth::user();
if (!$user->can('assign users')) {
session()->flash('error', 'No tienes permisos para asignar compañías.');
return;
}
$this->validate([
'selectedCompanyId' => 'required|exists:companies,id',
'selectedRole' => 'required|in:owner,constructor,subcontractor,consultant,supplier,other',
]);
$this->project->companies()->attach($this->selectedCompanyId, [
'role_in_project' => $this->selectedRole
]);
$this->reset(['selectedCompanyId', 'selectedRole']);
$this->loadCompanies();
$this->dispatch('notify', 'Compañía asignada al proyecto.');
}
public function removeCompany($companyId)
{
$user = Auth::user();
if (!$user->can('assign users')) {
session()->flash('error', 'Sin permisos.');
return;
}
$this->project->companies()->detach($companyId);
$this->loadCompanies();
$this->dispatch('notify', 'Compañía eliminada del proyecto.');
}
public function changeRole($companyId, $role)
{
if (!in_array($role, ['owner', 'constructor', 'subcontractor', 'consultant', 'supplier', 'other'])) return;
$this->project->companies()->updateExistingPivot($companyId, [
'role_in_project' => $role
]);
$this->loadCompanies();
$this->dispatch('notify', 'Rol actualizado.');
}
public function render()
{
return view('livewire.project-companies');
}
}