77 lines
2.1 KiB
PHP
77 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\ContractorManagement\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use Modules\ContractorManagement\Models\Contractor;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ContractorManager extends Component
|
|
{
|
|
public $contractors;
|
|
public $company_name;
|
|
public $type = 'main';
|
|
public $user_limit = 10;
|
|
public $parent_id = null;
|
|
|
|
protected $rules = [
|
|
'company_name' => 'required|string|max:255',
|
|
'type' => 'required|in:main,sub',
|
|
'user_limit' => 'required|integer|min:0',
|
|
'parent_id' => 'nullable|exists:contractors,id'
|
|
];
|
|
|
|
public function mount()
|
|
{
|
|
$this->loadContractors();
|
|
}
|
|
|
|
public function loadContractors()
|
|
{
|
|
$user = Auth::user();
|
|
if ($user->hasRole('admin')) {
|
|
$this->contractors = Contractor::all();
|
|
} elseif ($user->hasRole('Main Contractor Admin') && $user->contractor) {
|
|
$this->contractors = Contractor::where('id', $user->contractor_id)
|
|
->orWhere('parent_id', $user->contractor_id)
|
|
->get();
|
|
} else {
|
|
$this->contractors = collect();
|
|
}
|
|
}
|
|
|
|
public function createContractor()
|
|
{
|
|
$this->validate();
|
|
|
|
$user = Auth::user();
|
|
|
|
// Enforce hierarchy rules
|
|
if (!$user->hasRole('admin')) {
|
|
if ($this->type === 'main') {
|
|
session()->flash('error', 'Only admins can create Main Contractors.');
|
|
return;
|
|
}
|
|
if ($user->hasRole('Main Contractor Admin')) {
|
|
$this->parent_id = $user->contractor_id; // force parent to be themselves
|
|
}
|
|
}
|
|
|
|
Contractor::create([
|
|
'company_name' => $this->company_name,
|
|
'type' => $this->type,
|
|
'user_limit' => $this->user_limit,
|
|
'parent_id' => $this->parent_id,
|
|
]);
|
|
|
|
$this->reset(['company_name', 'type', 'user_limit', 'parent_id']);
|
|
$this->loadContractors();
|
|
session()->flash('message', 'Contractor created successfully.');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('contractormanagement::livewire.contractor-manager');
|
|
}
|
|
}
|