Skip to content

Commit 44f9df9

Browse files
author
Itamar Junior
committed
feat: implement service provider management system with CRUD operations
1 parent fc690db commit 44f9df9

26 files changed

+1237
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace App\Livewire\Company\Service;
4+
5+
use Livewire\Component;
6+
use App\Models\ServiceCategory;
7+
use Illuminate\Support\Facades\Auth;
8+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
9+
10+
class CategoryManager extends Component
11+
{
12+
use AuthorizesRequests;
13+
14+
public $categories;
15+
public string $name = '';
16+
public string $code = '';
17+
public string $description = '';
18+
19+
public bool $showCreateModal = false;
20+
21+
public ?ServiceCategory $confirmingDelete = null;
22+
public bool $showDeleteModal = false;
23+
24+
public bool $showEditModal = false;
25+
public ?ServiceCategory $editingCategory = null;
26+
public string $editName = '';
27+
public string $editCode = '';
28+
public string $editDescription = '';
29+
30+
public function mount()
31+
{
32+
$this->loadCategories();
33+
}
34+
35+
public function loadCategories()
36+
{
37+
$this->authorize('viewAny', ServiceCategory::class);
38+
$this->categories = ServiceCategory::where('business_id', Auth::user()->current_business_id)->get();
39+
}
40+
41+
public function openCreateModal()
42+
{
43+
$this->authorize('create', ServiceCategory::class);
44+
$this->reset(['name', 'code', 'description']);
45+
$this->resetValidation();
46+
$this->showCreateModal = true;
47+
}
48+
49+
public function create()
50+
{
51+
$this->authorize('create', ServiceCategory::class);
52+
$this->validate([
53+
'name' => 'required|string|max:255',
54+
'code' => 'required|string|max:50|unique:service_categories,code',
55+
]);
56+
57+
ServiceCategory::create([
58+
'business_id' => Auth::user()->current_business_id,
59+
'name' => $this->name,
60+
'code' => strtoupper($this->code),
61+
'description' => $this->description,
62+
]);
63+
64+
$this->reset(['name', 'code', 'description']);
65+
$this->showCreateModal = false;
66+
$this->loadCategories();
67+
68+
session()->flash('success', 'Service category created.');
69+
$this->dispatch('category-created');
70+
}
71+
72+
public function confirmDelete($id)
73+
{
74+
$this->confirmingDelete = ServiceCategory::findOrFail($id);
75+
$this->authorize('delete', $this->confirmingDelete);
76+
$this->showDeleteModal = true;
77+
}
78+
79+
public function delete()
80+
{
81+
if (! $this->confirmingDelete) return;
82+
83+
abort_unless(Auth::user()->ownsBusiness($this->confirmingDelete->business_id), 403);
84+
85+
$this->confirmingDelete->delete();
86+
$this->confirmingDelete = null;
87+
$this->showDeleteModal = false;
88+
session()->flash('success', 'Category deleted.');
89+
$this->dispatch('category-deleted');
90+
$this->loadCategories();
91+
}
92+
93+
public function edit($id)
94+
{
95+
$this->editingCategory = ServiceCategory::findOrFail($id);
96+
97+
$this->authorize('update', $this->editingCategory);
98+
abort_unless(Auth::user()->ownsBusiness($this->editingCategory->business_id), 403);
99+
100+
$this->editName = $this->editingCategory->name;
101+
$this->editCode = $this->editingCategory->code;
102+
$this->editDescription = $this->editingCategory->description;
103+
$this->showEditModal = true;
104+
}
105+
106+
public function update()
107+
{
108+
$this->validate([
109+
'editName' => 'required|string|max:255',
110+
'editCode' => 'required|string|max:50|unique:service_categories,code,' . $this->editingCategory->id,
111+
]);
112+
113+
if (! $this->editingCategory) return;
114+
115+
$this->authorize('update', $this->editingCategory);
116+
abort_unless(Auth::user()->ownsBusiness($this->editingCategory->business_id), 403);
117+
118+
$this->editingCategory->update([
119+
'name' => $this->editName,
120+
'code' => strtoupper($this->editCode),
121+
'description' => $this->editDescription,
122+
]);
123+
124+
$this->reset(['editingCategory', 'editName', 'editCode', 'editDescription', 'showEditModal']);
125+
$this->loadCategories();
126+
session()->flash('success', 'Category updated successfully.');
127+
$this->dispatch('category-updated');
128+
}
129+
130+
public function render()
131+
{
132+
return view('livewire.company.service.category-manager');
133+
}
134+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace App\Livewire\Company\Service;
4+
5+
use App\Models\Company;
6+
use Livewire\Component;
7+
use App\Models\ServiceCategory;
8+
use App\Models\ServiceProvider;
9+
use Illuminate\Support\Facades\Auth;
10+
use App\Models\CompanyServiceContract;
11+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
12+
13+
class ContractManager extends Component
14+
{
15+
use AuthorizesRequests;
16+
17+
public $companies;
18+
public $categories;
19+
public $providers;
20+
public $contracts;
21+
22+
public $company_id;
23+
public $service_category_id;
24+
public $service_provider_id;
25+
public $budget;
26+
public $start_date;
27+
public $next_due_date;
28+
public $status = 'active';
29+
public $notes;
30+
31+
public bool $showCreateModal = false;
32+
33+
public function mount()
34+
{
35+
$this->companies = Company::where('business_id', Auth::user()->current_business_id)->orderBy('name')->get();
36+
$this->categories = ServiceCategory::where('business_id', Auth::user()->current_business_id)->get();
37+
$this->providers = ServiceProvider::where('business_id', Auth::user()->current_business_id)->get();
38+
$this->loadContracts();
39+
}
40+
41+
public function loadContracts()
42+
{
43+
$this->contracts = CompanyServiceContract::with(['company', 'provider', 'category'])
44+
->whereHas('company', fn($q) => $q->where('business_id', Auth::user()->current_business_id))
45+
->latest()->get();
46+
}
47+
48+
public function openCreateModal()
49+
{
50+
$this->reset(['company_id', 'service_category_id', 'service_provider_id', 'budget', 'start_date', 'next_due_date', 'status', 'notes']);
51+
$this->resetValidation();
52+
$this->showCreateModal = true;
53+
}
54+
55+
public function create()
56+
{
57+
$this->validate([
58+
'company_id' => 'required|exists:companies,id',
59+
'service_category_id' => 'required|exists:service_categories,id',
60+
'service_provider_id' => 'required|exists:service_providers,id',
61+
'budget' => 'nullable|numeric',
62+
'start_date' => 'nullable|date',
63+
'next_due_date' => 'nullable|date',
64+
'status' => 'required|in:active,inactive,terminated',
65+
]);
66+
67+
CompanyServiceContract::create([
68+
'company_id' => $this->company_id,
69+
'service_category_id' => $this->service_category_id,
70+
'service_provider_id' => $this->service_provider_id,
71+
'budget' => $this->budget,
72+
'start_date' => $this->start_date,
73+
'next_due_date' => $this->next_due_date,
74+
'status' => $this->status,
75+
'notes' => $this->notes,
76+
]);
77+
78+
$this->reset(['showCreateModal']);
79+
$this->loadContracts();
80+
session()->flash('success', 'Service contract created.');
81+
}
82+
public function render()
83+
{
84+
return view('livewire.company.service.contract-manager');
85+
}
86+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
namespace App\Livewire\Company\Service\Provider;
4+
5+
use Livewire\Component;
6+
use App\Models\ServiceCategory;
7+
use App\Models\ServiceProvider;
8+
use Illuminate\Support\Facades\Auth;
9+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
10+
11+
class ProviderManager extends Component
12+
{
13+
use AuthorizesRequests;
14+
15+
public $providers;
16+
public $categories;
17+
18+
public string $name = '';
19+
public string $contact_name = '';
20+
public string $email = '';
21+
public string $phone = '';
22+
public string $website = '';
23+
public string $address = '';
24+
public string $notes = '';
25+
26+
public bool $showCreateModal = false;
27+
28+
public ?ServiceProvider $confirmingDelete = null;
29+
public bool $showDeleteModal = false;
30+
31+
public bool $showEditModal = false;
32+
public ?ServiceProvider $editingProvider = null;
33+
public string $editName = '';
34+
public string $editContactName = '';
35+
public string $editEmail = '';
36+
public string $editPhone = '';
37+
public string $editWebsite = '';
38+
public string $editAddress = '';
39+
public string $editNotes = '';
40+
41+
public function mount()
42+
{
43+
$this->loadProviders();
44+
$this->categories = ServiceCategory::where('business_id', Auth::user()->current_business_id)->get();
45+
}
46+
47+
public function loadProviders()
48+
{
49+
$this->authorize('viewAny', ServiceProvider::class);
50+
$this->providers = ServiceProvider::where('business_id', Auth::user()->current_business_id)->get();
51+
}
52+
53+
public function openCreateModal()
54+
{
55+
$this->authorize('create', ServiceProvider::class);
56+
$this->reset(['name', 'contact_name', 'email', 'phone', 'website', 'address', 'notes']);
57+
$this->resetValidation();
58+
$this->showCreateModal = true;
59+
}
60+
61+
public function create()
62+
{
63+
$this->authorize('create', ServiceProvider::class);
64+
$this->validate([
65+
'name' => 'required|string|max:255',
66+
'email' => 'nullable|email|max:255',
67+
'phone' => 'nullable|string|max:50',
68+
]);
69+
70+
ServiceProvider::create([
71+
'business_id' => Auth::user()->current_business_id,
72+
'name' => $this->name,
73+
'contact_name' => $this->contact_name,
74+
'email' => $this->email,
75+
'phone' => $this->phone,
76+
'website' => $this->website,
77+
'address' => $this->address,
78+
'notes' => $this->notes,
79+
]);
80+
81+
$this->reset(['name', 'contact_name', 'email', 'phone', 'website', 'address', 'notes']);
82+
$this->showCreateModal = false;
83+
$this->loadProviders();
84+
session()->flash('success', 'Service provider created.');
85+
$this->dispatch('provider-created');
86+
}
87+
88+
public function confirmDelete($id)
89+
{
90+
$this->confirmingDelete = ServiceProvider::findOrFail($id);
91+
$this->authorize('delete', $this->confirmingDelete);
92+
$this->showDeleteModal = true;
93+
}
94+
95+
public function delete()
96+
{
97+
if (! $this->confirmingDelete) return;
98+
99+
abort_unless(Auth::user()->ownsBusiness($this->confirmingDelete->business_id), 403);
100+
$this->confirmingDelete->delete();
101+
$this->reset('confirmingDelete', 'showDeleteModal');
102+
$this->loadProviders();
103+
session()->flash('success', 'Provider deleted.');
104+
}
105+
106+
public function edit($id)
107+
{
108+
$this->editingProvider = ServiceProvider::findOrFail($id);
109+
$this->authorize('update', $this->editingProvider);
110+
111+
$this->editName = $this->editingProvider->name;
112+
$this->editContactName = $this->editingProvider->contact_name;
113+
$this->editEmail = $this->editingProvider->email;
114+
$this->editPhone = $this->editingProvider->phone;
115+
$this->editWebsite = $this->editingProvider->website;
116+
$this->editAddress = $this->editingProvider->address;
117+
$this->editNotes = $this->editingProvider->notes;
118+
$this->showEditModal = true;
119+
}
120+
121+
public function update()
122+
{
123+
$this->validate([
124+
'editName' => 'required|string|max:255',
125+
'editEmail' => 'nullable|email|max:255',
126+
'editPhone' => 'nullable|string|max:50',
127+
]);
128+
129+
if (! $this->editingProvider) return;
130+
131+
$this->authorize('update', $this->editingProvider);
132+
133+
$this->editingProvider->update([
134+
'name' => $this->editName,
135+
'contact_name' => $this->editContactName,
136+
'email' => $this->editEmail,
137+
'phone' => $this->editPhone,
138+
'website' => $this->editWebsite,
139+
'address' => $this->editAddress,
140+
'notes' => $this->editNotes,
141+
]);
142+
143+
$this->reset([
144+
'editingProvider',
145+
'editName',
146+
'editContactName',
147+
'editEmail',
148+
'editPhone',
149+
'editWebsite',
150+
'editAddress',
151+
'editNotes',
152+
'showEditModal',
153+
]);
154+
155+
$this->loadProviders();
156+
session()->flash('success', 'Provider updated.');
157+
}
158+
159+
160+
public function render()
161+
{
162+
return view('livewire.company.service.provider.provider-manager');
163+
}
164+
}

0 commit comments

Comments
 (0)