|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Finance\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Finance\Models\Contact; |
| 7 | +use App\Modules\Finance\Models\CustomerGroup; |
| 8 | +use Illuminate\Http\RedirectResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Inertia\Inertia; |
| 11 | +use Inertia\Response; |
| 12 | + |
| 13 | +class CustomerGroupController extends Controller |
| 14 | +{ |
| 15 | + public function index(Request $request): Response |
| 16 | + { |
| 17 | + $this->authorize('viewAny', CustomerGroup::class); |
| 18 | + |
| 19 | + $customerGroups = CustomerGroup::latest() |
| 20 | + ->paginate(20) |
| 21 | + ->withQueryString(); |
| 22 | + |
| 23 | + return Inertia::render('Finance/CustomerGroups/Index', [ |
| 24 | + 'customerGroups' => $customerGroups, |
| 25 | + ]); |
| 26 | + } |
| 27 | + |
| 28 | + public function store(Request $request): RedirectResponse |
| 29 | + { |
| 30 | + $this->authorize('create', CustomerGroup::class); |
| 31 | + |
| 32 | + $validated = $request->validate([ |
| 33 | + 'name' => 'required|string|max:255', |
| 34 | + 'description' => 'nullable|string', |
| 35 | + 'discount_percent' => 'nullable|numeric|min:0|max:100', |
| 36 | + 'credit_limit' => 'nullable|numeric|min:0', |
| 37 | + 'payment_term_id' => 'nullable|exists:payment_terms,id', |
| 38 | + 'currency' => 'nullable|string|max:3', |
| 39 | + 'is_active' => 'boolean', |
| 40 | + ]); |
| 41 | + |
| 42 | + $customerGroup = CustomerGroup::create([ |
| 43 | + ...$validated, |
| 44 | + 'tenant_id' => auth()->user()->tenant_id, |
| 45 | + ]); |
| 46 | + |
| 47 | + return redirect()->route('finance.customer-groups.show', $customerGroup) |
| 48 | + ->with('success', 'Customer group created successfully.'); |
| 49 | + } |
| 50 | + |
| 51 | + public function show(CustomerGroup $customerGroup): Response |
| 52 | + { |
| 53 | + $this->authorize('view', $customerGroup); |
| 54 | + |
| 55 | + $customerGroup->load('paymentTerm'); |
| 56 | + $members = $customerGroup->members()->paginate(10); |
| 57 | + |
| 58 | + return Inertia::render('Finance/CustomerGroups/Show', [ |
| 59 | + 'customerGroup' => $customerGroup, |
| 60 | + 'members' => $members, |
| 61 | + ]); |
| 62 | + } |
| 63 | + |
| 64 | + public function update(Request $request, CustomerGroup $customerGroup): RedirectResponse |
| 65 | + { |
| 66 | + $this->authorize('update', $customerGroup); |
| 67 | + |
| 68 | + $validated = $request->validate([ |
| 69 | + 'name' => 'required|string|max:255', |
| 70 | + 'description' => 'nullable|string', |
| 71 | + 'discount_percent' => 'nullable|numeric|min:0|max:100', |
| 72 | + 'credit_limit' => 'nullable|numeric|min:0', |
| 73 | + 'payment_term_id' => 'nullable|exists:payment_terms,id', |
| 74 | + 'currency' => 'nullable|string|max:3', |
| 75 | + 'is_active' => 'boolean', |
| 76 | + ]); |
| 77 | + |
| 78 | + $customerGroup->update($validated); |
| 79 | + |
| 80 | + return redirect()->back()->with('success', 'Customer group updated successfully.'); |
| 81 | + } |
| 82 | + |
| 83 | + public function addMember(Request $request, CustomerGroup $customerGroup): RedirectResponse |
| 84 | + { |
| 85 | + $this->authorize('update', $customerGroup); |
| 86 | + |
| 87 | + $validated = $request->validate([ |
| 88 | + 'contact_id' => 'required|exists:contacts,id', |
| 89 | + ]); |
| 90 | + |
| 91 | + $customerGroup->members()->syncWithoutDetaching([$validated['contact_id']]); |
| 92 | + |
| 93 | + return redirect()->back()->with('success', 'Contact added to group successfully.'); |
| 94 | + } |
| 95 | + |
| 96 | + public function removeMember(CustomerGroup $customerGroup, Contact $contact): RedirectResponse |
| 97 | + { |
| 98 | + $this->authorize('update', $customerGroup); |
| 99 | + |
| 100 | + $customerGroup->members()->detach($contact->id); |
| 101 | + |
| 102 | + return redirect()->back()->with('success', 'Contact removed from group successfully.'); |
| 103 | + } |
| 104 | + |
| 105 | + public function destroy(CustomerGroup $customerGroup): RedirectResponse |
| 106 | + { |
| 107 | + $this->authorize('delete', $customerGroup); |
| 108 | + |
| 109 | + $customerGroup->delete(); |
| 110 | + |
| 111 | + return redirect()->route('finance.customer-groups.index') |
| 112 | + ->with('success', 'Customer group deleted successfully.'); |
| 113 | + } |
| 114 | +} |
0 commit comments