IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/Teacher/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/Teacher/LeaveController.php

<?php

namespace App\Http\Controllers\Teacher;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use Session;
use Carbon\Carbon;

class LeaveController extends TeacherController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $employee = \App\Employee::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
        $school = \App\School::where('idSchool','=',$employee->idSchool)->first();
        $leaves = [''=>'----Select----'] + DB::table('leave_types')->get()->pluck('name','name')->toArray();
        $emp_leaves = \App\EmployeeLeave::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)->where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->get();
        $leaveManage = $this->fetchEmployeeLeaves();
        return view('teachers.leaves', compact('leaves', 'emp_leaves', 'leaves','school','leaveManage'));
    }

    public function leaveWindow($id){
        $leave = \App\EmployeeLeave::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)->where('idLeave', '=', $id)->first();
        $school = \App\School::where('idSchool','=',Auth::guard('teacher')->user()->idSchool)->first();
        $employee = \App\Employee::where('idEmployee', '=', $leave->idEmployee)->first();
        if($leave->status != null){
            return redirect('teacher/leave-management');
        }
        $checkLeave = DB::table('leave_approval_master')->where('idEmployee',Auth::guard('teacher')->user()->idEmployee)->first();
        if($checkLeave == null){
            return redirect('teacher/leave-management');
        }
        $designation = DB::table('leave_approval_details')->select('idDesignation')->where('idPermitUser',$checkLeave->id)->get()->pluck('idDesignation')->toArray();
        if (in_array($employee->idDesignation, $designation)){
            $leaveManage = $this->fetchEmployeeOnlyLeaves($employee);
            return view('teachers.leave_window', compact('leave','leaveManage','school'));
        }else{
            return redirect('teacher/leave-management');
        }
    }

    public function fetchEmployeeOnlyLeaves($user){
        $leaves = array();
        $designationWise = \App\LeaveMaster::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)
        ->where('idDesignation',$user->idDesignation)
        ->where('idFinancialYear',Session::get('idFinancialYear'))
        ->where('leaveType',"monthly")
        ->whereNull('idEmployee')
        ->sum('paidLeaves');
        $paidLeaves = \App\LeaveMaster::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)
        ->where('idDesignation',$user->idDesignation)
        ->where('idEmployee',$user->idEmployee)
        ->where('idFinancialYear',Session::get('idFinancialYear'))
        ->where('leaveType',"monthly")
        ->sum('paidLeaves');
        $leaves["totalMonthLeaves"] = $designationWise + $paidLeaves;

        $designationYearWise = \App\LeaveMaster::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)
        ->where('idDesignation',$user->idDesignation)
        ->where('idFinancialYear',Session::get('idFinancialYear'))
        ->where('leaveType',"yearly")
        ->whereNull('idEmployee')
        ->sum('paidLeaves');
        $paidYearLeaves = \App\LeaveMaster::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)
        ->where('idDesignation',$user->idDesignation)
        ->where('idEmployee',$user->idEmployee)
        ->where('idFinancialYear',Session::get('idFinancialYear'))
        ->where('leaveType',"yearly")
        ->sum('paidLeaves');
        
         //check for leave balance
         $leaveBalance = \App\LeaveBalance::where('idSchool', '=', $user->idSchool)
         ->where('idEmployee',$user->idEmployee)
         ->where('idFinancialYear',Session::get('idFinancialYear'))
         ->sum('balance');   
         $leaves["totalYearLeaves"] = $designationYearWise + $paidYearLeaves;

        if($leaves["totalMonthLeaves"] > 0){
            $now = Carbon::now();
            $leaves["emp_leaves_taken"] = DB::table('employee_leaves')->where('idFinancialYear',Session::get('idFinancialYear'))->where('idMonth',$now->format('m'))->where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)->where('idEmployee', '=', $user->idEmployee)->count();
            if($leaveBalance == 0)
            $leaves["left_leaves"] = ($leaves["totalMonthLeaves"] + $leaves["totalYearLeaves"]) - $leaves["emp_leaves_taken"]; 
            else $leaves["left_leaves"] = ($leaves["totalMonthLeaves"] + $leaves["totalYearLeaves"]) - $leaves["emp_leaves_taken"] - $leaveBalance; 
        }else{
            $leaves["emp_leaves_taken"] = DB::table('employee_leaves')->where('idFinancialYear',Session::get('idFinancialYear'))->where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)->where('idEmployee', '=', $user->idEmployee)->count();
            if($leaveBalance == 0)
            $leaves["left_leaves"] = ($leaves["totalMonthLeaves"] + $leaves["totalYearLeaves"]) - $leaves["emp_leaves_taken"]; 
            else $leaves["left_leaves"] = ($leaves["totalMonthLeaves"] + $leaves["totalYearLeaves"]) - $leaves["emp_leaves_taken"]  - $leaveBalance; 
        }
        return $leaves;
    }

    public function viewDoc($id) {
        $leaves = \App\EmployeeLeave::findOrFail($id);
        $path = storage_path('app/public/schools/' . $leaves->idSchool . '/leaves/' . $leaves->documents);
        return response()->file($path);
    }

    public function leaveAction(Request $request) {
        $empleave = \App\EmployeeLeave::where('idLeave', '=', $request->idLeave)->first();
        if($empleave->status != null){
            return redirect('teacher/leave-management');
        }
        $employee = \App\Employee::where('idEmployee', '=', $request->idEmployee)->first();
        $empleave->status = $request->status;
        if(isset($request->remarks))
        $empleave->remarks = $request->remarks;
        $empleave->status_date = Carbon::now()->format("Y-m-d");
        $empleave->updated_by = Auth::guard('teacher')->user()->idEmployee;

        if($request->status == "Approve with PL(Paid Leave)"){
            $date = Carbon::parse($empleave->leave_from);
            $now = Carbon::parse($empleave->leave_to);
            $diff = $date->diffInDays($now) + 1;
            //check max allowed
            $designationWise = \App\LeaveMaster::where('idSchool', '=', $employee->idSchool)
                ->where('idDesignation',$employee->idDesignation)
                ->where('idFinancialYear',Session::get('idFinancialYear'))
                ->whereNull('idEmployee')->first();
            if($designationWise != null){
                if($designationWise->max_allowed > 0){
                    if($diff > $designationWise->max_allowed){
                          return redirect()->back()->with('error',"You have surpassed the allowed max days per month.");  
                    }
                }
            }else{
                $paidLeaves = \App\LeaveMaster::where('idSchool', '=', $employee->idSchool)
                    ->where('idDesignation',$employee->idDesignation)
                    ->where('idEmployee',$employee->idEmployee)
                    ->where('idFinancialYear',Session::get('idFinancialYear'))->first();
                if($paidLeaves != null){
                    if($paidLeaves->max_allowed > 0){
                        if($diff > $paidLeaves->max_allowed){
                            return redirect()->back()->with('error',"You have surpassed the allowed max days per month.");  
                        }
                    }
                }    
            }    
            
            DB::table('employee_leaves')->insert([
                "idSchool" =>  Auth::guard('teacher')->user()->idSchool,
                "idEmployee" => $empleave->idEmployee,
                "idFinancialYear" =>  Session::get('idFinancialYear'),
                "idLeave" => $empleave->idLeave,
                "idMonth" => $date->format('m'),
                "leave_date" => $date->format("Y-m-d")
            ]);
            if($empleave->leave_from != $empleave->leave_to){
                for ($x = 1; $x < $diff; $x++) {
                    $currentDay = $date->addDays(1);
                    DB::table('employee_leaves')->insert([
                        "idSchool" =>  Auth::guard('teacher')->user()->idSchool,
                        "idEmployee" => $empleave->idEmployee,
                        "idFinancialYear" =>  Session::get('idFinancialYear'),
                        "idLeave" => $empleave->idLeave,
                        "idMonth" => $currentDay->format("m"),
                        "leave_date" => $currentDay->format("Y-m-d")
                    ]);
                } 
            }
            
        }

        $empleave->update();
        return redirect('teacher/leave-management');
    }

    public function manageLeave(Request $request){
        $checkLeave = DB::table('leave_approval_master')->where('idEmployee',Auth::guard('teacher')->user()->idEmployee)->first();
        $school = \App\School::where('idSchool','=',Auth::guard('teacher')->user()->idSchool)->first();
        if($checkLeave != null){
            $designation = DB::table('leave_approval_details')->select('idDesignation')->where('idPermitUser',$checkLeave->id)->get()->pluck('idDesignation')->toArray();
            $empleaves =  \App\EmployeeLeave::leftJoin('school_users', 'employee_leave.status_by', '=', 'school_users.idSchoolUser')
                        ->leftJoin('employees', 'employee_leave.idEmployee', '=', 'employees.idEmployee')
                        ->select('employee_leave.*','school_users.name')->where('employee_leave.idSchool', '=', Auth::guard('teacher')->user()->idSchool)
                        ->whereIn('idDesignation',$designation)
                        ->get();
            return view('teachers.manage-leave',compact('checkLeave','school','empleaves'));
        }
        $empleaves = [];
        return view('teachers.manage-leave',compact('checkLeave','school','empleaves'));
    }

    public function fetchEmployeeLeaves(){
        $leaves = array();
        $designationWise = \App\LeaveMaster::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)
        ->where('idDesignation',Auth::guard('teacher')->user()->idDesignation)
        ->where('idFinancialYear',Session::get('idFinancialYear'))
        ->where('leaveType',"monthly")
        ->whereNull('idEmployee')
        ->sum('paidLeaves');
        $paidLeaves = \App\LeaveMaster::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)
        ->where('idDesignation',Auth::guard('teacher')->user()->idDesignation)
        ->where('idEmployee',Auth::guard('teacher')->user()->idEmployee)
        ->where('idFinancialYear',Session::get('idFinancialYear'))
        ->where('leaveType',"monthly")
        ->sum('paidLeaves');
        $leaves["totalMonthLeaves"] = $designationWise + $paidLeaves;

        $designationYearWise = \App\LeaveMaster::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)
        ->where('idDesignation',Auth::guard('teacher')->user()->idDesignation)
        ->where('idFinancialYear',Session::get('idFinancialYear'))
        ->where('leaveType',"yearly")
        ->whereNull('idEmployee')
        ->sum('paidLeaves');
        $paidYearLeaves = \App\LeaveMaster::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)
        ->where('idDesignation',Auth::guard('teacher')->user()->idDesignation)
        ->where('idEmployee',Auth::guard('teacher')->user()->idEmployee)
        ->where('idFinancialYear',Session::get('idFinancialYear'))
        ->where('leaveType',"yearly")
        ->sum('paidLeaves');
        
        $leaves["totalYearLeaves"] = $designationYearWise + $paidYearLeaves;
        if($leaves["totalMonthLeaves"] > 0){
            $now = Carbon::now();
            $leaves["emp_leaves_taken"] = DB::table('employee_leaves')->where('idFinancialYear',Session::get('idFinancialYear'))->where('idMonth',$now->format('m'))->where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)->where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->count();
        }else
        $leaves["emp_leaves_taken"] = DB::table('employee_leaves')->where('idFinancialYear',Session::get('idFinancialYear'))->where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)->where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->count();
        return $leaves;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        $teacher = \App\Employee::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
        $rules = [
            'leave_type' => 'required',
            'fromDate' => 'required',
            'toDate' => 'required'
        ];
        $messages = [
            'leave_type.required' => 'Select Leave Type First.',
        ];
        $this->validate($request, $rules, $messages);
        $empleave = new \App\EmployeeLeave();
        $empleave->idSchool = Auth::guard('teacher')->user()->idSchool;
        $empleave->idEmployee = Auth::guard('teacher')->user()->idEmployee;
        $empleave->leave_type = $request->leave_type;
        $empleave->leave_from =Carbon::parse($request->fromDate)->format("Y-m-d");
        $empleave->leave_to = Carbon::parse($request->toDate)->format("Y-m-d");
        $empleave->leave_message = $request->adminMessage;
        $leaves = $this->fetchEmployeeLeaves();
        $empleave->paid_avialable = "Monthly : ".$leaves["totalMonthLeaves"]." Yearly :".$leaves["totalYearLeaves"];
        $empleave->idFinancialYear = Session::get('idFinancialYear');
        $empleave->save();
        if ($request->hasFile('leaveFile')) {
            $hw = 'leave_file_' . $empleave->idLeave . '.' . $request->file('leaveFile')->getClientOriginalExtension();
            $request->file('leaveFile')->storeAs('public/schools/' . $teacher->idSchool . '/leaves/', $hw);
            $empleave->documents = $hw;
            $empleave->update();
        }
        return redirect('teacher/leaves');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {
        $leave = \App\LeaveMaster::where('idLeave','=',$id)->select('paidLeaves')->first();
       // dd($leave);
        return json_encode($leave);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        $employee = \App\Employee::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
        $school = \App\School::where('idSchool','=',$employee->idSchool)->first();
        $leaves = [''=>'----Select----'] + DB::table('leave_types')->get()->pluck('name','name')->toArray();
        $emp_leaves = \App\EmployeeLeave::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)->where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->get();
        $leaveManage = $this->fetchEmployeeLeaves();
        $leave = \App\EmployeeLeave::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)->where('idLeave', '=', $id)->first();
        if($leave->status != null){
            return redirect('teacher/leaves');
        }
        return view('teachers.leaves', compact('leaves', 'emp_leaves', 'leaves', 'leave','leaveManage','school'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        $teacher = \App\Employee::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
        $rules = [
            'leave_type' => 'required',
            'fromDate' => 'required',
            'toDate' => 'required'
        ];
        $messages = [
            'leave_type.required' => 'Select Leave Type First.',
        ];
        $this->validate($request, $rules, $messages);
        $empleave = \App\EmployeeLeave::where('idSchool', '=', Auth::guard('teacher')->user()->idSchool)->where('idLeave', '=', $id)->first();
        $empleave->leave_type = $request->leave_type;
        $empleave->leave_from =Carbon::parse($request->fromDate)->format("Y-m-d");
        $empleave->leave_to = Carbon::parse($request->toDate)->format("Y-m-d");
        $empleave->leave_message = $request->adminMessage;
        if ($request->hasFile('leaveFile')) {
            $hw = 'leave_file_' . $empleave->idLeave . '.' . $request->file('leaveFile')->getClientOriginalExtension();
            $request->file('leaveFile')->storeAs('public/schools/' . $teacher->idSchool . '/leaves/', $hw);
            $empleave->documents = $hw;
        }
        $leaves = $this->fetchEmployeeLeaves();
        $empleave->paid_avialable = "Monthly : ".$leaves["totalMonthLeaves"]." Yearly :".$leaves["totalYearLeaves"];
        $empleave->update();
        return redirect('teacher/leaves');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        //
    }

}

Copyright © 2021 - 2025 IMMREX7