-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompanyServiceController.php
More file actions
244 lines (195 loc) · 8.06 KB
/
CompanyServiceController.php
File metadata and controls
244 lines (195 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Modules\Nfse\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Modules\Nfse\Models\CompanyService;
use Modules\Nfse\Support\Lc116Catalog;
class CompanyServiceController extends Controller
{
/**
* Create a new CompanyService record
*/
public function create(): View
{
$catalog = new Lc116Catalog();
$items = $catalog->search(); // Get all items
return view('nfse::services.create', [
'items' => $items,
]);
}
/**
* Store a newly created CompanyService in storage
*/
public function store(Request $request): RedirectResponse
{
$validated = $request->validate([
'item_lista_servico' => ['required', 'string', 'max:10'],
'codigo_tributacao_nacional' => ['nullable', 'string', 'size:6'],
'aliquota' => ['required', 'numeric', 'min:0', 'max:100'],
'description' => ['nullable', 'string', 'max:255'],
'is_active' => ['boolean'],
]);
$validated['item_lista_servico'] = preg_replace('/\D+/', '', (string) $validated['item_lista_servico']) ?: '';
$validated['codigo_tributacao_nacional'] = preg_replace('/\D+/', '', (string) ($validated['codigo_tributacao_nacional'] ?? '')) ?: '';
$validated['codigo_tributacao_nacional'] = $validated['codigo_tributacao_nacional'] !== ''
? $validated['codigo_tributacao_nacional']
: null;
$companyId = $this->resolveCompanyId($request);
if ($companyId <= 0) {
abort(403);
}
// Check if this service already exists
$existing = CompanyService::where('company_id', $companyId)
->where('item_lista_servico', $validated['item_lista_servico'])
->first();
if ($existing) {
return back()->withInput()->with('error', trans('nfse::general.settings.services.service_duplicate'));
}
CompanyService::create([
'company_id' => $companyId,
...$validated,
'is_active' => $validated['is_active'] ?? true,
'is_default' => !CompanyService::where('company_id', $companyId)->where('is_default', true)->exists(),
]);
$createdService = CompanyService::where('company_id', $companyId)
->where('item_lista_servico', $validated['item_lista_servico'])
->latest('id')
->first();
if ($createdService !== null && $createdService->is_default) {
$this->setDefaultService($createdService);
}
return redirect()->route('nfse.settings.edit', ['tab' => 'services'])
->with('success', trans('nfse::general.settings.services.service_added'));
}
/**
* Show the form for editing a CompanyService
*/
public function edit(Request $request, CompanyService $service): View
{
$this->authorizeServiceOwnership($request, $service);
$catalog = new Lc116Catalog();
$items = $catalog->search();
return view('nfse::services.edit', [
'service' => $service,
'items' => $items,
]);
}
/**
* Update a CompanyService in storage
*/
public function update(Request $request, CompanyService $service): RedirectResponse
{
$this->authorizeServiceOwnership($request, $service);
$validated = $request->validate([
'codigo_tributacao_nacional' => ['nullable', 'string', 'size:6'],
'aliquota' => ['required', 'numeric', 'min:0', 'max:100'],
'description' => ['nullable', 'string', 'max:255'],
'is_active' => ['boolean'],
]);
$validated['codigo_tributacao_nacional'] = preg_replace('/\D+/', '', (string) ($validated['codigo_tributacao_nacional'] ?? '')) ?: '';
$validated['codigo_tributacao_nacional'] = $validated['codigo_tributacao_nacional'] !== ''
? $validated['codigo_tributacao_nacional']
: null;
$service->update($validated);
return redirect()->route('nfse.settings.edit', ['tab' => 'services'])
->with('success', trans('nfse::general.settings.services.service_updated'));
}
/**
* Delete a CompanyService
*/
public function destroy(Request $request, CompanyService $service): RedirectResponse
{
$this->authorizeServiceOwnership($request, $service);
// If this was the default, unset default flag
if ($service->is_default) {
$service->update(['is_default' => false]);
}
$service->delete();
return redirect()->route('nfse.settings.edit', ['tab' => 'services'])
->with('success', trans('nfse::general.settings.services.service_deleted'));
}
/**
* Make a service the default for this company
*/
public function makeDefault(Request $request, CompanyService $service): RedirectResponse
{
$this->authorizeServiceOwnership($request, $service);
if (! $service->is_active) {
$service->update(['is_active' => true]);
}
$this->setDefaultService($service);
return redirect()->route('nfse.settings.edit', ['tab' => 'services'])
->with('success', trans('nfse::general.settings.services.service_made_default'));
}
/**
* Toggle active status of a CompanyService.
*/
public function toggleActive(Request $request, CompanyService $service): RedirectResponse
{
$this->authorizeServiceOwnership($request, $service);
$newStatus = ! $service->is_active;
$wasDefault = $service->is_default;
$service->update([
'is_active' => $newStatus,
'is_default' => $newStatus ? $service->is_default : false,
]);
if (! $newStatus && $wasDefault) {
$fallback = CompanyService::where('company_id', $service->company_id)
->where('id', '!=', $service->id)
->where('is_active', true)
->orderBy('is_default', 'desc')
->orderBy('id')
->first();
if ($fallback !== null) {
$this->setDefaultService($fallback);
}
}
if ($newStatus) {
$hasDefault = CompanyService::where('company_id', $service->company_id)
->where('is_active', true)
->where('is_default', true)
->exists();
if (! $hasDefault) {
$this->setDefaultService($service);
}
}
return redirect()->route('nfse.settings.edit', ['tab' => 'services'])
->with('success', $newStatus
? trans('nfse::general.settings.services.service_activated')
: trans('nfse::general.settings.services.service_deactivated'));
}
private function resolveCompanyId(?Request $request = null): int
{
$companyId = 0;
if ($request !== null) {
$routeCompanyId = $request->route('company_id');
$companyId = is_numeric($routeCompanyId) ? (int) $routeCompanyId : 0;
}
if ($companyId <= 0 && function_exists('company_id')) {
$companyId = (int) (company_id() ?? 0);
}
if ($companyId <= 0) {
$user = auth()->user();
$companyId = $user !== null && isset($user->company_id) ? (int) $user->company_id : 0;
}
return $companyId;
}
private function authorizeServiceOwnership(?Request $request, CompanyService $service): void
{
$companyId = $this->resolveCompanyId($request);
if ($companyId <= 0 || (int) $service->company_id !== $companyId) {
abort(403);
}
}
private function setDefaultService(CompanyService $service): void
{
CompanyService::where('company_id', $service->company_id)
->where('id', '!=', $service->id)
->update(['is_default' => false]);
$service->update(['is_default' => true]);
}
}