127 lines
3.9 KiB
PHP
127 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\HrmAddon\App\Http\Controllers\Api;
|
|
|
|
use App\Helpers\HasUploader;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\HrmAddon\App\Models\Employee;
|
|
|
|
class EmployeeController extends Controller
|
|
{
|
|
use HasUploader;
|
|
|
|
public function index()
|
|
{
|
|
$data = Employee::with('department:id,name', 'designation:id,name', 'shift:id,name', 'branch:id,name')
|
|
->where('business_id', auth()->user()->business_id)
|
|
->when(request('status'), function ($query) {
|
|
$status = request('status');
|
|
if (in_array($status, ['active', 'terminated', 'suspended'])) {
|
|
$query->where('status', $status);
|
|
}
|
|
})
|
|
->latest()
|
|
->get();
|
|
|
|
return response()->json([
|
|
'message' => __('Data fetched successfully.'),
|
|
'data' => $data,
|
|
]);
|
|
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate($this->validationRules());
|
|
|
|
$data = Employee::create($request->except('business_id', 'image') + [
|
|
'business_id' => auth()->user()->business_id,
|
|
'image' => $request->image ? $this->upload($request, 'image') : NULL
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => __('Data saved successfully.'),
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$data = Employee::with('department:id,name', 'designation:id,name', 'shift:id,name', 'branch:id,name')
|
|
->where('business_id', auth()->user()->business_id)
|
|
->findOrFail($id);
|
|
|
|
return response()->json([
|
|
'message' => __('Data fetched successfully.'),
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$employee = Employee::where('business_id', auth()->user()->business_id)
|
|
->find($id);
|
|
|
|
if (!$employee) {
|
|
return response()->json([
|
|
'message' => __('Data not found.'),
|
|
], 404);
|
|
}
|
|
|
|
$request->validate($this->validationRules($id));
|
|
|
|
$data = $employee->update($request->except('business_id', 'image') + [
|
|
'image' => $request->image ? $this->upload($request, 'image', $employee->image) : $employee->image
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => __('Data saved successfully.'),
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$employee = Employee::where('business_id', auth()->user()->business_id)
|
|
->find($id);
|
|
|
|
if (!$employee) {
|
|
return response()->json([
|
|
'message' => __('Data not found.'),
|
|
], 404);
|
|
}
|
|
|
|
if (file_exists($employee->image)) {
|
|
Storage::delete($employee->image);
|
|
}
|
|
|
|
$employee->delete();
|
|
|
|
return response()->json([
|
|
'message' => __('Data deleted successfully.'),
|
|
]);
|
|
}
|
|
|
|
// Common validation for store/update.
|
|
protected function validationRules($id = null)
|
|
{
|
|
return [
|
|
'name' => 'required|string|max:255',
|
|
'designation_id' => 'required|exists:designations,id',
|
|
'department_id' => 'required|exists:departments,id',
|
|
'shift_id' => 'required|exists:shifts,id',
|
|
'amount' => 'required|numeric',
|
|
'phone' => 'required|string|max:20',
|
|
'email' => 'nullable|email|max:255',
|
|
'gender' => 'required|in:male,female,others',
|
|
'country' => 'nullable|string|max:100',
|
|
'birth_date' => 'nullable|date',
|
|
'join_date' => 'nullable|date',
|
|
'image' => 'nullable|image|mimes:jpg,png,jpeg,svg',
|
|
'status' => 'required|in:active,terminated,suspended',
|
|
];
|
|
}
|
|
}
|