first change
This commit is contained in:
0
Modules/Employee/app/Http/Controllers/.gitkeep
Normal file
0
Modules/Employee/app/Http/Controllers/.gitkeep
Normal file
163
Modules/Employee/app/Http/Controllers/DepartmentController.php
Normal file
163
Modules/Employee/app/Http/Controllers/DepartmentController.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Employee\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Employee\Models\Department;
|
||||
use Modules\Employee\Services\DepartmentService;
|
||||
use Modules\User\Services\UserService;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class DepartmentController extends Controller
|
||||
{
|
||||
protected $departmentService;
|
||||
protected $userService;
|
||||
|
||||
public function __construct(DepartmentService $departmentService, UserService $userService)
|
||||
{
|
||||
$this->departmentService = $departmentService;
|
||||
$this->userService = $userService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(?int $id = null)
|
||||
{
|
||||
$isEditing = !is_null($id);
|
||||
$department = $isEditing ? $this->departmentService->getDepartmentById($id) : null;
|
||||
|
||||
if (request()->ajax()) {
|
||||
$model = Department::query()->orderBy('order');
|
||||
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->editColumn('status', function (Department $department) {
|
||||
$status = $department->status ? 'Published' : 'Draft';
|
||||
$color = $department->status ? 'text-success' : 'text-danger';
|
||||
return "<p class='{$color}'>{$status}</p>";
|
||||
})
|
||||
->addColumn('head', function (Department $department) {
|
||||
return $department->departmentHead?->name ?? '-';
|
||||
})
|
||||
->addColumn('action', 'employee::department.datatable.action')
|
||||
->rawColumns(['action', 'status', 'head'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
$userOptions = $this->userService->pluck();
|
||||
|
||||
return view('employee::department.index', [
|
||||
'department' => $department,
|
||||
'userOptions' => $userOptions,
|
||||
'title' => $isEditing ? 'Edit Department' : 'Add Department',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$isEditing = $request->has('id');
|
||||
|
||||
$request->merge([
|
||||
'slug' => Str::slug($request->title),
|
||||
]);
|
||||
|
||||
if ($isEditing) {
|
||||
$validated = $request->validate([
|
||||
'title' => ['required', 'string', 'max:255', 'unique:departments,title,' . $request->id],
|
||||
'slug' => ['required', 'string'],
|
||||
'user_id' => ['nullable', 'integer'],
|
||||
]);
|
||||
|
||||
$department = $this->departmentService->updateDepartment($request->id, $validated);
|
||||
flash()->success("Department for {$department->title} has been updated.");
|
||||
return to_route('department.index');
|
||||
}
|
||||
|
||||
$maxOrder = Department::max('order');
|
||||
$order = $maxOrder ? ++$maxOrder : 1;
|
||||
|
||||
$request->mergeIfMissing([
|
||||
'order' => $order
|
||||
]);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => ['required', 'string', 'unique:departments,title'],
|
||||
'slug' => ['required', 'string'],
|
||||
'order' => ['integer'],
|
||||
'user_id' => ['nullable', 'integer'],
|
||||
]);
|
||||
|
||||
$department = $this->departmentService->storeDepartment($validated);
|
||||
flash()->success("Department for {$department->title} has been created.");
|
||||
return to_route('department.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$department = $this->departmentService->deleteDepartment($id);
|
||||
return response()->json(['status' => 200, 'message' => "Department has been deleted."], 200);
|
||||
}
|
||||
|
||||
public function reorder(Request $request)
|
||||
{
|
||||
$departments = $this->departmentService->getAllDepartments();
|
||||
|
||||
foreach ($departments as $department) {
|
||||
foreach ($request->order as $order) {
|
||||
if ($order['id'] == $department->id) {
|
||||
$department->update(['order' => $order['position']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return response(['status' => true, 'message' => 'Reordered successfully'], 200);
|
||||
}
|
||||
|
||||
public function toggle($id)
|
||||
{
|
||||
$department = Department::findOrFail($id);
|
||||
$department->update(['status' => !$department->status]);
|
||||
return response(['status' => 200, 'message' => 'Toggled successfully'], 200);
|
||||
}
|
||||
}
|
163
Modules/Employee/app/Http/Controllers/DesignationController.php
Normal file
163
Modules/Employee/app/Http/Controllers/DesignationController.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Employee\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Employee\Models\Designation;
|
||||
use Modules\Employee\Services\DepartmentService;
|
||||
use Modules\Employee\Services\DesignationService;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class DesignationController extends Controller
|
||||
{
|
||||
protected $designationService;
|
||||
protected $departmentService;
|
||||
|
||||
public function __construct(DesignationService $designationService, DepartmentService $departmentService)
|
||||
{
|
||||
$this->designationService = $designationService;
|
||||
$this->departmentService = $departmentService;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(?int $id = null)
|
||||
{
|
||||
$isEditing = !is_null($id);
|
||||
$designation = $isEditing ? $this->designationService->getDesignationById($id) : null;
|
||||
|
||||
if (request()->ajax()) {
|
||||
$model = Designation::query()->orderBy('order');
|
||||
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->editColumn('status', function (Designation $designation) {
|
||||
$status = $designation->status ? 'Published' : 'Draft';
|
||||
$color = $designation->status ? 'text-success' : 'text-danger';
|
||||
return "<p class='{$color}'>{$status}</p>";
|
||||
})
|
||||
->editColumn('department_id', function (Designation $designation) {
|
||||
return $designation->department?->title ?? "-";
|
||||
})
|
||||
->addColumn('action', 'employee::designation.datatable.action')
|
||||
->rawColumns(['action', 'status'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
$departmentOptions = $this->departmentService->pluck();
|
||||
return view('employee::designation.index', [
|
||||
'designation' => $designation,
|
||||
'departmentOptions'=> $departmentOptions,
|
||||
'title' => $isEditing ? 'Edit Designation' : 'Add Designation',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$isEditing = $request->has('id');
|
||||
|
||||
$request->merge([
|
||||
'slug' => Str::slug($request->title),
|
||||
]);
|
||||
|
||||
if ($isEditing) {
|
||||
$validated = $request->validate([
|
||||
'title' => ['required', 'string', 'max:255', 'unique:designations,title,' . $request->id],
|
||||
'slug' => ['required', 'string'],
|
||||
'department_id' => ['nullable', 'integer'],
|
||||
]);
|
||||
|
||||
$designation = $this->designationService->updateDesignation($request->id, $validated);
|
||||
flash()->success("Designation for {$designation->title} has been updated.");
|
||||
return to_route('designation.index');
|
||||
}
|
||||
|
||||
$maxOrder = Designation::max('order');
|
||||
$order = $maxOrder ? ++$maxOrder : 1;
|
||||
|
||||
$request->mergeIfMissing([
|
||||
'order' => $order
|
||||
]);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => ['required', 'string', 'unique:designations,title'],
|
||||
'slug' => ['required', 'string'],
|
||||
'order' => ['integer'],
|
||||
'department_id' => ['nullable', 'integer'],
|
||||
]);
|
||||
|
||||
$designation = $this->designationService->storeDesignation($validated);
|
||||
flash()->success("Designation for {$designation->title} has been created.");
|
||||
return to_route('designation.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$designation = $this->designationService->deleteDesignation($id);
|
||||
return response()->json(['status' => 200, 'message' => "Designation has been deleted."], 200);
|
||||
}
|
||||
|
||||
public function reorder(Request $request)
|
||||
{
|
||||
$designations = $this->designationService->getAllDesignations();
|
||||
|
||||
foreach ($designations as $designation) {
|
||||
foreach ($request->order as $order) {
|
||||
if ($order['id'] == $designation->id) {
|
||||
$designation->update(['order' => $order['position']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return response(['status' => true, 'message' => 'Reordered successfully'], 200);
|
||||
}
|
||||
|
||||
public function toggle($id)
|
||||
{
|
||||
$designation = Designation::findOrFail($id);
|
||||
$designation->update(['status' => !$designation->status]);
|
||||
return response(['status' => 200, 'message' => 'Toggled successfully'], 200);
|
||||
}
|
||||
}
|
258
Modules/Employee/app/Http/Controllers/EmployeeController.php
Normal file
258
Modules/Employee/app/Http/Controllers/EmployeeController.php
Normal file
@@ -0,0 +1,258 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Employee\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\CCMS\Models\Branch;
|
||||
use Modules\Employee\Interfaces\EmployeeInterface;
|
||||
use Modules\Employee\Models\Employee;
|
||||
use Modules\Employee\Services\DepartmentService;
|
||||
use Modules\Employee\Services\DesignationService;
|
||||
use Modules\User\Services\UserService;
|
||||
|
||||
class EmployeeController extends Controller
|
||||
{
|
||||
private $employee;
|
||||
private $userService;
|
||||
private $branchService;
|
||||
private $designationService;
|
||||
private $departmentService;
|
||||
|
||||
|
||||
public function __construct(EmployeeInterface $employee, UserService $userService, DesignationService $designationService, DepartmentService $departmentService)
|
||||
{
|
||||
$this->employee = $employee;
|
||||
$this->userService = $userService;
|
||||
$this->designationService = $designationService;
|
||||
$this->departmentService = $departmentService;
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$data['title'] = "Employee List";
|
||||
|
||||
if ($request->ajax()) {
|
||||
$data['employees'] = $this->employee->findAll(request: $request, query: function ($query) {
|
||||
$query->where('status', "!=", "terminated")->with('user')->latest();
|
||||
}, paginate:true, limit:12);
|
||||
|
||||
$view = view('employee::employee.partials.employee-list', $data)->render();
|
||||
|
||||
return response()->json(['status', 200, 'html' => $view], 200);
|
||||
}
|
||||
return view('employee::employee.index', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data['title'] = 'Create Employee';
|
||||
$data['editable'] = false;
|
||||
$data['genderOptions'] = config("constants.gender_options");
|
||||
$data['statusOptions'] = config("constants.employee_status_options");
|
||||
$data['branchOptions'] = Branch::pluck('title', 'id');
|
||||
$data['departmentOptions'] = $this->departmentService->pluck();
|
||||
$data['designationOptions'] = $this->designationService->pluck();
|
||||
return view('employee::employee.create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validatedData = $request->validate([
|
||||
'mobile' => [
|
||||
'required',
|
||||
'regex:/^(98|97)[0-9]{8}$/',
|
||||
],
|
||||
'photo' => 'nullable',
|
||||
'email' => 'required|email|unique:employees,email',
|
||||
], [
|
||||
'mobile.required' => 'Mobile number is required.',
|
||||
'mobile.regex' => 'Mobile number must start with 98 or 97 and be 10 digits long.',
|
||||
'email.required' => 'Email is required.',
|
||||
'email.email' => 'Invalid email format.',
|
||||
'email.unique' => 'The email has already been taken.',
|
||||
]);
|
||||
|
||||
$request->merge([
|
||||
'employee_code' => 'EMP' . mt_rand(1000, 9999),
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($validatedData, $request) {
|
||||
$employeeInput = $request->only(Employee::getFillableFields());
|
||||
$userInput = $request->only(User::getFillableFields());
|
||||
|
||||
$employee = $this->employee->create($employeeInput);
|
||||
|
||||
$userInput['name'] = $employee->full_name;
|
||||
$userInput['order'] = ($maxOrder = User::max('order')) !== null ? ++$maxOrder : 1;
|
||||
|
||||
$user = $this->userService->storeUser($userInput);
|
||||
|
||||
$employee->update(['user_id' => $user->id]);
|
||||
});
|
||||
|
||||
flash()->success('Employee has been created successfully!');
|
||||
return redirect()->route('employee.index');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
flash()->error($e->getMessage());
|
||||
return redirect()->back()->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$data['employee'] = $this->employee->findById($id);
|
||||
|
||||
// $data['leaves'] = $this->leave->getLeaveByEmployeeId(id: $id);
|
||||
// $data['leaveTypeList'] = $this->leaveType->pluck();
|
||||
// $data['status'] = Leave::PROGRESS_STATUS;
|
||||
// $data['duration'] = Leave::DURATION;
|
||||
// $data['attends'] = $this->attendance->getAttendanceByEmployeeId(id: $id);
|
||||
// $data['roleList'] = $this->role->pluck();
|
||||
// $data['desgination'] = $data['employee']->designation->name;
|
||||
// $data['department'] = $data['employee']->department_id ? optional($this->dropDown->getDropdownById($data['employee']->department_id))->title : null;
|
||||
// $data['leaveBalance'] = $this->leaveBalance->where([
|
||||
// 'employee_id' => $id,
|
||||
// ])->with(['leaveType:leave_type_id,name'])->get()
|
||||
// ->transform(function ($leaveBalance) {
|
||||
// return [
|
||||
// 'leave_type_id' => $leaveBalance->leave_type_id,
|
||||
// 'leave_type' => $leaveBalance->leaveType?->name,
|
||||
// 'total' => $leaveBalance->total,
|
||||
// 'remain' => $leaveBalance->remain,
|
||||
// ];
|
||||
// });
|
||||
|
||||
return view('employee::show', $data);
|
||||
}
|
||||
|
||||
/**ss
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$data['title'] = 'Edit Employee';
|
||||
$data['editable'] = true;
|
||||
$data['genderOptions'] = config("constants.gender_options");
|
||||
$data['statusOptions'] = config("constants.employee_status_options");
|
||||
$data['branchOptions'] = Branch::pluck('title', 'id');
|
||||
$data['departmentOptions'] = $this->departmentService->pluck();
|
||||
$data['designationOptions'] = $this->designationService->pluck();
|
||||
$data['employee'] = $this->employee->findById($id);
|
||||
return view('employee::employee.edit', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'mobile' => [
|
||||
'required',
|
||||
'regex:/^(98|97)[0-9]{8}$/',
|
||||
],
|
||||
'email' => 'required|email|unique:employees,email,' . $id,
|
||||
], [
|
||||
'contact.required' => 'The contact number is required.',
|
||||
'contact.regex' => 'The contact number must start with 98 or 97 and be 10 digits long.',
|
||||
'email.required' => 'The email is required.',
|
||||
'email.email' => 'The email must be a valid email address.',
|
||||
'email.unique' => 'The email has already been taken.',
|
||||
]);
|
||||
|
||||
try {
|
||||
|
||||
DB::transaction(function () use ($request, $id) {
|
||||
$input = $request->only(Employee::getFillableFields());
|
||||
$employee = $this->employee->update($id, $input);
|
||||
|
||||
$userData = [
|
||||
'name' => $employee->full_name,
|
||||
'email' => $employee->email,
|
||||
];
|
||||
|
||||
if ($employee->isDirty("email")) {
|
||||
$userData["can_login"] = false;
|
||||
}
|
||||
|
||||
$employee->user->update($userData);
|
||||
});
|
||||
|
||||
flash()->success('Employee has been updated!');
|
||||
|
||||
return redirect()->route('employee.index');
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
flash()->error($th->getMessage());
|
||||
return redirect()->back()->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
|
||||
DB::transaction(function () use ($id) {
|
||||
$employee = $this->employee->delete($id);
|
||||
$employee->user->delete();
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'status' => 200,
|
||||
'message' => 'Employee has been deleted!',
|
||||
], 200);
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
return response()->json([
|
||||
'status' => 500,
|
||||
'message' => 'Failed to delete employee!',
|
||||
'error' => $th->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function terminate($id)
|
||||
{
|
||||
try {
|
||||
|
||||
DB::transaction(function () use ($id) {
|
||||
$employee = $this->employee->terminate($id);
|
||||
$employee->user->update([
|
||||
"can_login" => false,
|
||||
]);
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'status' => 200,
|
||||
'message' => 'Employee has been terminate!',
|
||||
], 200);
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
return response()->json([
|
||||
'status' => 500,
|
||||
'message' => 'Failed to terminate employee!',
|
||||
'error' => $th->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user