leave = $leaveRepository; $this->employee = $employeeRepository; $this->leaveType = $leaveTypeRepository; $this->leaveBalance = $leaveBalance; } /** * Display a listing of the resource. */ public function index(Request $request) { $filters = $request->all(); $data['title'] = "Leave List"; $data['leaves'] = $this->leave->findAll($filters); // dd($data['leaves']->toArray()); $data['employeeList'] = $this->employee->pluck(); $data['status'] = Leave::PROGRESS_STATUS; return view('leave::leave.index', $data); } /** * Show the form for creating a new resource. */ public function create() { $data['title'] = 'Create Leave'; $data['editable'] = false; $data['employeeList'] = $this->employee->pluck(); $data['leaveTypeList'] = $this->leaveType->pluck(); $data['status'] = Leave::PROGRESS_STATUS; $data['duration'] = Leave::DURATION; return view('leave::leave.create', $data); } /** * Store a newly created resource in storage. */ public function store(Request $request): RedirectResponse { $inputData = $request->all(); try { $dates = array_map('trim', explode(",", $request->start_date)); $inputData['total_days'] = $request->duration == '2' ? count($dates) / 2 : count($dates); $inputData['start_date'] = ['dates' => $dates]; $leave = $this->leave->create($inputData); if ($request->status == 2) { $leaveBalance = $this->leaveBalance->where([ 'employee_id' => $request->employee_id, 'leave_type_id' => $leave->leave_type_id, ])->first(); $leaveBalance->remain -= $leave->total_days; $leaveBalance->save(); $leave->leave_approved_by = Auth::id(); $leave->save(); } // $employeemodel = $this->employee->getEmployeeById($inputData['employee_id']); // sendNotification($employeemodel->user, [ // 'msg' => 'Leave Created']); flash()->success('Leave Created Succesfully'); } catch (\Throwable $th) { flash()->error($th->getMessage()); } return redirect()->route('leave.index'); } /** * Show the specified resource. */ public function show($id) { $data['title'] = 'View Leave'; $data['leave'] = $this->leave->getLeaveById($id); return view('leave::leave.show', $data); } /** * Show the form for editing the specified resource. */ public function edit($id) { $data['title'] = 'Edit Leave'; $data['editable'] = true; $data['leave'] = $this->leave->getLeaveById($id); $data['employeeList'] = $this->employee->pluck(); $data['leaveTypeList'] = $this->leaveType->pluck(); $data['status'] = Leave::PROGRESS_STATUS; $data['duration'] = Leave::DURATION; return view('leave::leave.edit', $data); } /** * Update the specified resource in storage. */ public function update(Request $request, $id): RedirectResponse { $inputData = $request->except(['_method', "_token"]); try { $this->leave->update($id, $inputData); flash()->success('Leave Updated Succesfully'); } catch (\Throwable $th) { flash()->error($th->getMessage()); } return redirect()->route('leave.index'); } /** * Remove the specified resource from storage. */ public function destroy($id) { try { $this->leave->delete($id); return response()->json(['status' => 200, 'message' => 'Leave has been deleted!'], 200); } catch (\Throwable $th) { return response()->json(['status' => 500, 'message' => 'Leave to delete!', 'error' => $th->getMessage()], 500); } } /** * Update the status of the specified resource in storage. */ public function updateStatus(Request $request) { try { $leave = $this->leave->getLeaveById($request->leave_id); $request->merge([ 'leave_approved_by' => Auth::id(), ]); if ($request->status == 2) { $leaveBalance = $this->leaveBalance->where([ 'employee_id' => $request->employee_id, 'leave_type_id' => $leave->leave_type_id, ])->first(); $leaveBalance->remain -= $leave->total_days; $leaveBalance->save(); } $this->leave->update($request->leave_id, $request->except(['_method', "_token"])); // $employeemodel = $this->employee->getEmployeeById($request->employee_id); // sendNotification($employeemodel->user, [ // 'msg' => 'Leave Status Changed']); flash()->success('Leave Status Updated Successfully'); } catch (\Throwable $th) { flash()->error($th->getMessage()); } return redirect()->route('leave.index'); } public function getRemainingEmpLeave(Request $request) { try { $leaveBalance = $this->leaveBalance->where([ 'employee_id' => $request->employee_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 response()->json([ 'view' => view('leave::leave.partials.remaining-leave', compact('leaveBalance'))->render(), ]); } catch (\Throwable $th) { return response()->json([ 'msg' => $th->getMessage(), ]); } // return redirect()->route('leave.index'); } public function checkEmpLeaveDates(Request $request) { $validator = \Validator::make($request->all(), [ 'employee_id' => 'required', 'leave_type_id' => 'required', 'duration' => 'required', ]); if ($validator->fails()) { return response()->json([ 'status' => false, 'errors' => $validator->errors(), ]); } try { $dates = array_map('trim', explode(",", $request->dates)); $leaves = $this->leave->where([ 'employee_id' => $request->employee_id, ]) ->when(true, function ($q) use ($dates) { $q->where(function ($query) use ($dates) { foreach ($dates as $date) { $query->orWhereJsonContains("start_date", ['dates' => $date]); } }); }) ->select('leave_id', 'employee_id', 'start_date', 'status') ->get() ->toArray(); $mergeLeaveDates = array_map(function ($leave) { return $leave['start_date']['dates']; }, $leaves); $previousLeaveDates = array_intersect($dates, array_merge(...$mergeLeaveDates)); $html = ''; $btnFlag = $request->filled('dates') ? true : false; if (!empty($previousLeaveDates)) { $btnFlag = false; $html .= '
  • Previous Leave Found on ' . implode(', ', $previousLeaveDates); } $leaveBalance = $this->leaveBalance->where([ 'employee_id' => $request->employee_id, 'leave_type_id' => $request->leave_type_id, ])->where('remain', '<=', count($dates)) ->first(); if ($leaveBalance) { $btnFlag = false; $html .= '
  • Remaining Leave Balance: ' . $leaveBalance->remain; } if ($btnFlag == true) { $html .= '
  • Leave Apply Dates: ' . $request->dates; } return response()->json(['html' => $html, 'btnFlag' => $btnFlag]); } catch (\Throwable $th) { return response()->json([ 'msg' => $th->getMessage(), ]); } } }