Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| EmployeeController | |
0.00% |
0 / 40 |
|
0.00% |
0 / 7 |
210 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| store | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
56 | |||
| show | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| edit | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| update | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| destroy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\admin; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Requests\EmployeeRequest; |
| 7 | use App\Models\admin\Employee; |
| 8 | use App\Models\Branch; |
| 9 | use App\Models\User; |
| 10 | use App\Service\PosService; |
| 11 | use App\Traits\PosTrait; |
| 12 | use Illuminate\Http\Request; |
| 13 | use Illuminate\Http\Response; |
| 14 | use Illuminate\Support\Facades\DB; |
| 15 | use Illuminate\Support\Facades\Log; |
| 16 | |
| 17 | class EmployeeController extends Controller |
| 18 | { |
| 19 | use PosTrait; |
| 20 | |
| 21 | /** |
| 22 | * Display a listing of the resource. |
| 23 | * |
| 24 | * @return Response |
| 25 | */ |
| 26 | public function index() |
| 27 | { |
| 28 | $data['employees'] = Employee::getAll(true); |
| 29 | |
| 30 | return view('admin.employee.index')->with($data); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Show the form for creating a new resource. |
| 35 | * |
| 36 | * @return Response |
| 37 | */ |
| 38 | public function create() |
| 39 | { |
| 40 | return view('admin.employee.create', [ |
| 41 | 'branches' => feature('branch_management') ? Branch::where('status', true)->pluck('name', 'id') : collect(), |
| 42 | ]); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Store a newly created resource in storage. |
| 47 | * |
| 48 | * @param Request $request |
| 49 | * @return Response |
| 50 | */ |
| 51 | public function store(EmployeeRequest $request) |
| 52 | { |
| 53 | $request->validate([ |
| 54 | 'branch_id' => feature('branch_management') ? ['required', 'exists:branches,id'] : ['nullable'], |
| 55 | ]); |
| 56 | try { |
| 57 | DB::beginTransaction(); |
| 58 | $userData = $request->only(['name', 'email', 'password']); |
| 59 | $user = User::store($userData); |
| 60 | if (feature('branch_management')) { |
| 61 | $user->update(['branch_id' => $request->branch_id]); |
| 62 | } |
| 63 | $employeeData = $request->only([ |
| 64 | 'phone', |
| 65 | 'nid', |
| 66 | 'present_address', |
| 67 | 'permanent_address', |
| 68 | 'joining_date', |
| 69 | 'joining_salary', |
| 70 | ]); |
| 71 | $employee = Employee::store($employeeData, $user); |
| 72 | |
| 73 | if ($request->hasFile('image')) { |
| 74 | $data['image'] = $this->FileProcessing($request->file('image'), PosService::EMPLOYEE_IMAGE, 429, 500); |
| 75 | $employee->update(['image' => $data['image']]); |
| 76 | } elseif ($request->filled('image')) { |
| 77 | // Handle image from Media Library (URL) |
| 78 | $imageUrl = $request->input('image'); |
| 79 | // Optional: remove base URL to store relative path if needed, or just store the full URL |
| 80 | if (str_starts_with($imageUrl, url('/'))) { |
| 81 | $imageUrl = str_replace(url('/') . '/', '', $imageUrl); |
| 82 | } |
| 83 | $employee->update(['image' => $imageUrl]); |
| 84 | } |
| 85 | DB::commit(); |
| 86 | |
| 87 | return redirect()->back()->with('success', 'Data successfully inserted'); |
| 88 | } catch (\Throwable $e) { |
| 89 | DB::rollBack(); |
| 90 | Log::error('Employee store failed: ' . $e->getMessage(), ['exception' => $e]); |
| 91 | |
| 92 | return redirect()->back()->withInput()->with('error', 'Something went wrong while saving the employee. Please try again.'); |
| 93 | } |
| 94 | |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Display the specified resource. |
| 99 | * |
| 100 | * @return Response |
| 101 | */ |
| 102 | public function show(Employee $employee) |
| 103 | { |
| 104 | // |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Show the form for editing the specified resource. |
| 109 | * |
| 110 | * @return Response |
| 111 | */ |
| 112 | public function edit(Employee $employee) |
| 113 | { |
| 114 | // |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Update the specified resource in storage. |
| 119 | * |
| 120 | * @return Response |
| 121 | */ |
| 122 | public function update(Request $request, Employee $employee) |
| 123 | { |
| 124 | // |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Remove the specified resource from storage. |
| 129 | * |
| 130 | * @return Response |
| 131 | */ |
| 132 | public function destroy(Employee $employee) |
| 133 | { |
| 134 | // |
| 135 | } |
| 136 | } |