Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
10 / 20
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
CustomerGroupController
50.00% covered (danger)
50.00%
10 / 20
28.57% covered (danger)
28.57%
2 / 7
19.12
0.00% covered (danger)
0.00%
0 / 1
 index
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 store
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 edit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 destroy
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 validated
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Http\Controllers\admin;
4
5use App\Http\Controllers\Controller;
6use App\Models\CustomerGroup;
7use Illuminate\Http\RedirectResponse;
8use Illuminate\Http\Request;
9use Illuminate\Support\Str;
10use Illuminate\Validation\Rule;
11use Illuminate\View\View;
12
13/**
14 * Sales → Customer Groups. Pricing tiers (Retail, Wholesale, Dealer…) a
15 * customer can be assigned to — see PriceListController for the actual
16 * price rules per group. Gated by the `wholesale_pricing` feature.
17 */
18class CustomerGroupController extends Controller
19{
20    public function index(): View
21    {
22        $groups = CustomerGroup::query()->withCount(['customers', 'priceLists'])->orderBy('sort_order')->orderBy('name')->get();
23
24        return view('admin.customer-groups.index', compact('groups'));
25    }
26
27    public function create(): View
28    {
29        return view('admin.customer-groups.form', ['group' => new CustomerGroup]);
30    }
31
32    public function store(Request $request): RedirectResponse
33    {
34        CustomerGroup::create($this->validated($request));
35
36        return redirect()->route('admin.customer-groups.index')->with('success', 'Customer group created.');
37    }
38
39    public function edit(CustomerGroup $customerGroup): View
40    {
41        return view('admin.customer-groups.form', ['group' => $customerGroup]);
42    }
43
44    public function update(Request $request, CustomerGroup $customerGroup): RedirectResponse
45    {
46        $customerGroup->update($this->validated($request, $customerGroup));
47
48        return redirect()->route('admin.customer-groups.index')->with('success', 'Customer group updated.');
49    }
50
51    public function destroy(CustomerGroup $customerGroup): RedirectResponse
52    {
53        if ($customerGroup->customers()->exists()) {
54            return redirect()->back()->with('error', 'This group still has customers assigned to it — move them to another group first.');
55        }
56
57        $customerGroup->delete();
58
59        return redirect()->route('admin.customer-groups.index')->with('success', 'Customer group deleted.');
60    }
61
62    private function validated(Request $request, ?CustomerGroup $group = null): array
63    {
64        $data = $request->validate([
65            'name' => ['required', 'string', 'max:120'],
66            'slug' => ['nullable', 'string', 'max:120', Rule::unique('customer_groups', 'slug')->ignore($group)],
67            'sort_order' => ['nullable', 'integer'],
68        ]);
69
70        $data['slug'] = ($data['slug'] ?? null) ?: Str::slug($data['name']);
71        $data['sort_order'] = $data['sort_order'] ?? 0;
72
73        return $data;
74    }
75}