Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
BranchController
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 5
210
0.00% covered (danger)
0.00%
0 / 1
 index
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 store
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 update
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 destroy
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 validated
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Http\Controllers\admin;
4
5use App\Http\Controllers\Controller;
6use App\Models\Branch;
7use Illuminate\Http\RedirectResponse;
8use Illuminate\Http\Request;
9use Illuminate\Validation\Rule;
10
11class BranchController extends Controller
12{
13    public function index()
14    {
15        return view('admin.locations.branches', ['branches' => Branch::withCount('warehouses')->orderByDesc('is_default')->get()]);
16    }
17
18    public function store(Request $request): RedirectResponse
19    {
20        $data = $this->validated($request);
21        $data['is_default'] = $data['is_default'] || ! Branch::where('is_default', true)->exists();
22        if ($data['is_default']) {
23            $data['status'] = true;
24        }
25        if ($data['is_default']) {
26            Branch::where('is_default', true)->update(['is_default' => false]);
27        }
28        Branch::create($data);
29
30        return back()->with('success', 'Branch created.');
31    }
32
33    public function update(Request $request, Branch $branch): RedirectResponse
34    {
35        $data = $this->validated($request, $branch);
36        if ($data['is_default']) {
37            Branch::whereKeyNot($branch->id)->update(['is_default' => false]);
38        }
39        if ($branch->is_default && (! $data['status'] || ! $data['is_default'])) {
40            return back()->with('error', 'Make another branch the default before changing this one.');
41        }
42        $branch->update($data);
43
44        return back()->with('success', 'Branch updated.');
45    }
46
47    public function destroy(Branch $branch): RedirectResponse
48    {
49        if ($branch->is_default || $branch->warehouses()->exists()) {
50            return back()->with('error', 'A default branch or a branch with warehouses cannot be deleted.');
51        }
52        $branch->delete();
53
54        return back()->with('success', 'Branch deleted.');
55    }
56
57    private function validated(Request $request, ?Branch $branch = null): array
58    {
59        $data = $request->validate([
60            'name' => ['required', 'string', 'max:150'], 'code' => ['required', 'string', 'max:30', Rule::unique('branches')->ignore($branch)],
61            'phone' => ['nullable', 'string', 'max:40'], 'address' => ['nullable', 'string', 'max:1000'],
62            'is_default' => ['nullable', 'boolean'], 'status' => ['nullable', 'boolean'],
63        ]);
64        $data['code'] = strtoupper($data['code']);
65        $data['is_default'] = $request->boolean('is_default');
66        $data['status'] = $request->boolean('status');
67
68        return $data;
69    }
70}