|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Core\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Core\Models\Company; |
| 7 | +use Illuminate\Http\RedirectResponse; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Inertia\Inertia; |
| 10 | +use Inertia\Response; |
| 11 | + |
| 12 | +class CompanyController extends Controller |
| 13 | +{ |
| 14 | + public function index(): Response |
| 15 | + { |
| 16 | + $this->authorize('viewAny', Company::class); |
| 17 | + |
| 18 | + $companies = Company::with('parent') |
| 19 | + ->withCount('subsidiaries') |
| 20 | + ->latest() |
| 21 | + ->paginate(20) |
| 22 | + ->withQueryString(); |
| 23 | + |
| 24 | + return Inertia::render('Core/Companies/Index', [ |
| 25 | + 'companies' => $companies, |
| 26 | + ]); |
| 27 | + } |
| 28 | + |
| 29 | + public function create(): Response |
| 30 | + { |
| 31 | + $this->authorize('create', Company::class); |
| 32 | + |
| 33 | + $parents = Company::select('id', 'name', 'code')->get(); |
| 34 | + |
| 35 | + return Inertia::render('Core/Companies/Create', [ |
| 36 | + 'parents' => $parents, |
| 37 | + ]); |
| 38 | + } |
| 39 | + |
| 40 | + public function store(Request $request): RedirectResponse |
| 41 | + { |
| 42 | + $this->authorize('create', Company::class); |
| 43 | + |
| 44 | + $validated = $request->validate([ |
| 45 | + 'name' => 'required|string|max:255', |
| 46 | + 'code' => 'nullable|string|max:20', |
| 47 | + 'tax_id' => 'nullable|string|max:100', |
| 48 | + 'currency_code' => 'nullable|string|size:3', |
| 49 | + 'fiscal_year_start' => 'nullable|integer|min:1|max:12', |
| 50 | + 'address' => 'nullable|string', |
| 51 | + 'phone' => 'nullable|string|max:50', |
| 52 | + 'email' => 'nullable|email|max:255', |
| 53 | + 'website' => 'nullable|string|max:255', |
| 54 | + 'industry' => 'nullable|string|max:100', |
| 55 | + 'is_active' => 'boolean', |
| 56 | + 'parent_company_id' => 'nullable|exists:companies,id', |
| 57 | + ]); |
| 58 | + |
| 59 | + $validated['created_by'] = auth()->id(); |
| 60 | + |
| 61 | + Company::create($validated); |
| 62 | + |
| 63 | + return redirect()->route('core.companies.index') |
| 64 | + ->with('success', 'Company created successfully.'); |
| 65 | + } |
| 66 | + |
| 67 | + public function show(Company $company): Response |
| 68 | + { |
| 69 | + $this->authorize('view', $company); |
| 70 | + |
| 71 | + $company->load(['parent', 'subsidiaries', 'users']); |
| 72 | + |
| 73 | + return Inertia::render('Core/Companies/Show', [ |
| 74 | + 'company' => $company, |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + public function edit(Company $company): Response |
| 79 | + { |
| 80 | + $this->authorize('update', $company); |
| 81 | + |
| 82 | + $parents = Company::select('id', 'name', 'code') |
| 83 | + ->where('id', '!=', $company->id) |
| 84 | + ->get(); |
| 85 | + |
| 86 | + return Inertia::render('Core/Companies/Edit', [ |
| 87 | + 'company' => $company, |
| 88 | + 'parents' => $parents, |
| 89 | + ]); |
| 90 | + } |
| 91 | + |
| 92 | + public function update(Request $request, Company $company): RedirectResponse |
| 93 | + { |
| 94 | + $this->authorize('update', $company); |
| 95 | + |
| 96 | + $validated = $request->validate([ |
| 97 | + 'name' => 'required|string|max:255', |
| 98 | + 'code' => 'nullable|string|max:20', |
| 99 | + 'tax_id' => 'nullable|string|max:100', |
| 100 | + 'currency_code' => 'nullable|string|size:3', |
| 101 | + 'fiscal_year_start' => 'nullable|integer|min:1|max:12', |
| 102 | + 'address' => 'nullable|string', |
| 103 | + 'phone' => 'nullable|string|max:50', |
| 104 | + 'email' => 'nullable|email|max:255', |
| 105 | + 'website' => 'nullable|string|max:255', |
| 106 | + 'industry' => 'nullable|string|max:100', |
| 107 | + 'is_active' => 'boolean', |
| 108 | + 'parent_company_id' => 'nullable|exists:companies,id', |
| 109 | + ]); |
| 110 | + |
| 111 | + $company->update($validated); |
| 112 | + |
| 113 | + return redirect()->route('core.companies.show', $company) |
| 114 | + ->with('success', 'Company updated successfully.'); |
| 115 | + } |
| 116 | + |
| 117 | + public function destroy(Company $company): RedirectResponse |
| 118 | + { |
| 119 | + $this->authorize('delete', $company); |
| 120 | + |
| 121 | + $company->delete(); |
| 122 | + |
| 123 | + return redirect()->route('core.companies.index') |
| 124 | + ->with('success', 'Company deleted successfully.'); |
| 125 | + } |
| 126 | +} |
0 commit comments