IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/Hostel/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/Hostel/AttendanceController.php

<?php

namespace App\Http\Controllers\School\Hostel;

use Illuminate\Http\Request;
use App\Http\Controllers\School\SchoolController;
use Auth;
use DB;
use Carbon\Carbon;
use Session;
use App\Http\SendSmsApi;
use Illuminate\Support\Facades\Redirect;

class AttendanceController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
//        dd($request->all());
        if ($request->has('date') && $request->date != null) {
            $tdate = \Carbon\Carbon::parse($request->date)->format('Y-m-d');
        } else {
            $tdate = \Carbon\Carbon::parse(today_date())->format('Y-m-d');
        }
        $months = \App\Month::get()->pluck('monthName', 'idMonth')->toArray();
        $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        $classes = \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $now = \Carbon\Carbon::now();
        $month_name = $now->format('M');
        $current_month = \App\Month::where('monthName', 'like', '%' . $month_name . '%')->first();
        $total_attendace = 0;
        if ($request->has('students') && count($request->students) > 0) {
            $students = DB::table('hostel_students')
                    ->join('students', 'hostel_students.idStudent', '=', 'students.idStudent')
                    ->join('hostel_attendance', 'students.ecNo', '=', 'hostel_attendance.Enrollment_Number')
                    ->where('Device_ID', '=', $school->Device_ID)
                    ->whereDate('date', '=', $tdate)
                    ->whereIn('hostel_students.idStudent', $request->students)
                    ->where('hostel_students.idSchool', '=', $school->idSchool)
                    ->where('isActive', '=', 'Y')
                    ->get();
            $total_attendace = DB::table('hostel_attendance')
                            ->join('students', 'hostel_attendance.Enrollment_Number', '=', 'students.ecNo')
                            ->whereIn('idStudent', $request->students)
                            ->where('hostel_attendance.idSchool', '=', $school->idSchool)
                            ->where('Device_ID', '=', $school->Device_ID)
                            ->whereDate('date', '=', $tdate)
                            ->where(function ($query) {
                                $query->where('status', '=', 'IN')
                                ->orWhere('status', '=', 'P');
                            })
                            ->get()->count();
        } elseif ($request->has('sections') && count($request->sections) > 0) {

            $students = DB::table('hostel_students')
                    ->join('students', 'hostel_students.idStudent', '=', 'students.idStudent')
                    ->join('hostel_attendance', 'students.ecNo', '=', 'hostel_attendance.Enrollment_Number')
                    ->where('Device_ID', '=', $school->Device_ID)
                    ->whereDate('date', '=', $tdate)
                    ->whereIn('students.idSection', $request->sections)
                    ->where('hostel_students.idSchool', '=', $school->idSchool)
                    ->where('isActive', '=', 'Y')
                    ->get();
            $total_attendace = DB::table('hostel_attendance')
                            ->join('students', 'hostel_attendance.Enrollment_Number', '=', 'students.ecNo')
                            ->whereIn('idSection', $request->sections)
                            ->where('hostel_attendance.idSchool', '=', $school->idSchool)
                            ->where('Device_ID', '=', $school->Device_ID)
                            ->whereDate('date', '=', $tdate)
                            ->where(function ($query) {
                                $query->where('status', '=', 'IN')
                                ->orWhere('status', '=', 'P');
                            })
                            ->get()->count();
        } else if ($request->has('classes') && count($request->classes) > 0) {
            $students = DB::table('hostel_students')
                    ->rightjoin('students', 'hostel_students.idStudent', '=', 'students.idStudent')
                    ->rightjoin('hostel_attendance', 'students.ecNo', '=', 'hostel_attendance.Enrollment_Number')
                    ->where('Device_ID', '=', $school->Device_ID)
                    ->whereDate('date', '=', $tdate)
                    ->whereIn('students.idClass', $request->classes)
                    ->where('hostel_students.idSchool', '=', $school->idSchool)
                    ->where('isActive', '=', 'Y')
                    ->get();
//            dd($students);
            $total_attendace = DB::table('hostel_attendance')
                            ->join('students', 'hostel_attendance.Enrollment_Number', '=', 'students.ecNo')
                            ->whereIn('idClass', $request->classes)
                            ->where('hostel_attendance.idSchool', '=', $school->idSchool)
                            ->where('Device_ID', '=', $school->Device_ID)
                            ->whereDate('date', '=', $tdate)
                            ->where(function ($query) {
                                $query->where('status', '=', 'IN')
                                ->orWhere('status', '=', 'P');
                            })
                            ->get()->count();
        } else {
            $students = [];
        }
        //  dd($students);
        return view('schools.hostel.attendance', compact('total_attendace', 'tdate', 'classes', 'students', 'current_month', 'school', 'months'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request) {
        //
        $floor = ['' => '--Select--'] + \App\HostelFloor::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idFloor')->get()->pluck('floorName', 'idFloor')->toArray();
        $students = [];
        if ($request->has('idRoom') && $request->idRoom != null) {
            $students = \App\Hostelite::join('students', 'hostel_students.idStudent', '=', 'students.idStudent')->where('idRoom', '=', $request->idRoom)->where('hostel_students.idSchool', '=', Auth::guard('school')->user()->idSchool)->where('hostel_students.idFinancialYear', '=', Session::get('idFinancialYear'))->where('isActive', '=', 'Y')->get();
        } else if ($request->has('idFloor') && $request->idFloor != null){
            $students = \App\Hostelite::join('students', 'hostel_students.idStudent', '=', 'students.idStudent')->where('idFloor', '=', $request->idFloor)->where('hostel_students.idSchool', '=', Auth::guard('school')->user()->idSchool)->where('hostel_students.idFinancialYear', '=', Session::get('idFinancialYear'))->where('isActive', '=', 'Y')->get();
        }
        $month = \App\Month::where('idMonth', '=', $request->idMonth)->first();
        $now = \Carbon\Carbon::now();
        $month_name = $now->format('M');
        $current_month = \App\Month::where('monthName', 'like', '%' . $month_name . '%')->first();
        return view('schools.hostel.manual_attendance', compact('floor','students', 'month', 'current_month'));
    }

    public function fetchRooms($id){
        $room = \App\HostelRoom::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFloor',$id)
                        ->orderBy('idRoom')->get()->pluck('roomName', 'idRoom')->toArray();
        return json_encode($room);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        //
        if ($request->has('students')) {
            foreach ($request->students as $key => $var) {
                $jdate = Carbon::parse($key)->format('Y-m-d');
//                $attendancemarked = \App\BusAttendance::whereDate('date', '=', $jdate)
//                                ->where('isAutomatic', '=', 'M')
//                                ->get()->pluck('enrollmentNo')->toArray();
//                $newscr = $var;
//                $detach = array_diff($attendancemarked, $newscr);
//                \App\BusAttendance::whereDate('date', '=', $jdate)
//                        ->whereIn('enrollmentNo', $detach)
//                        ->where('isAutomatic', '=', 'M')
//                        ->delete();
                foreach ($var as $key1 => $var1) {
//                    dd($var1);
                    $a_exist = \App\HostelAttendance::where('Enrollment_Number', '=', $var1['ecNo'])
                            ->whereDate('date', '=', $jdate)
                            ->where('idType', '=', 'M')
                            ->first();
                    if (isset($var1['present']) && !$a_exist) {
                        $attendance = new \App\HostelAttendance();
                        $attendance->Enrollment_Number = $var1['ecNo'];
                        $attendance->idSchool = Auth::guard('school')->user()->idSchool;
                        $attendance->date = $jdate;
                        $attendance->idMonth = $request->idMonth;
                        $attendance->TimeStamp = Carbon::now()->timestamp;
                        $attendance->status = 'P';
                        $attendance->idType = 'M';
                        $attendance->save();

                        //SEND SMS 
                        $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
                        $student = \App\AdmEntry::where('ecNo', '=', $var1['ecNo'])->where('idSchool', '=', $school->idSchool)->first();
                        $father = \App\Parents::where('mobile', '=', $student->father_mobile)->first();

//                        $mother = \App\Parents::where('mobile', '=', $student->mother_mobile)->first();
//                        $mother_mobile = $mother->mobile;
                        if (!empty($father) && $school->smsmode == 'auto') {
                            $father_mobile = $father->mobile;
                            $phone_number = implode(',', array($father_mobile));
                            $message = 'Dear Parent, Your Child - ' . $student->firstName . ' Present Today - ' . $school->schoolName . ' Regards ' . $school->sms_regard_text . '.';
                            $template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'manual_present')->first();
                            if (!empty($template)) {
                                $tempid = $template->template_id;
                                //\App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
                            }
                        }
                    } else if(!isset($var1['present']) && !empty($a_exist)) {
                        $a_exist->delete();
                    }
                }
            }
            flash('Hostel Attendance saved successfully.');
            return redirect()->back();
        } else {
            flash('No Student has been selected for attendance');
            return redirect()->back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        //
    }

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

    public function getMonthlyAttendance(Request $request) {
        $todaydate = \Carbon\Carbon::parse(today_date())->format('Y-m-d');
        $months = \App\Month::get()->pluck('monthName', 'idMonth')->toArray();
        $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        $classes = \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $now = \Carbon\Carbon::now();
        $month_name = $now->format('M');
        $current_month = \App\Month::where('monthName', 'like', '%' . $month_name . '%')->first();

        //get selected month
        if ($request->has('idMonth')) {
            $month = \App\Month::where('idMonth', '=', $request->idMonth)->first();

            $noOfdays = $month->noOfDays;
            $m = $month->idMonth;
            $y = $now->year;
            $days = [];

            if ($request->has('students') && count($request->students) > 0) {
                $students = DB::table('hostel_students')
                        ->join('students', 'hostel_students.idStudent', '=', 'students.idStudent')
                        ->whereIn('hostel_students.idStudent', $request->students)
                        ->where('hostel_students.idSchool', '=', $school->idSchool)
                        ->where('isActive', '=', 'Y')
                        ->get();
            } elseif ($request->has('sections') && count($request->sections) > 0) {
                $students = DB::table('hostel_students')
                        ->join('students', 'hostel_students.idStudent', '=', 'students.idStudent')
                        ->whereIn('idSection', $request->sections)
                        ->where('hostel_students.idSchool', '=', $school->idSchool)
                        ->where('isActive', '=', 'Y')
                        ->get();
            } else if ($request->has('classes') && count($request->classes) > 0) {
                $students = DB::table('hostel_students')
                        ->join('students', 'hostel_students.idStudent', '=', 'students.idStudent')
                        ->whereIn('idClass', $request->classes)
                        ->where('hostel_students.idSchool', '=', $school->idSchool)
                        ->where('isActive', '=', 'Y')
                        ->get();
            } else {
                $students = [];
            }


            for ($i = 1; $i <= $noOfdays; $i++) {
                $dt = $i . '-' . $m . '-' . $y;

                $tdate = \Carbon\Carbon::parse($dt);
                $jdate = $tdate->format('Y-m-d');
                $holiday = \App\Holiday::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->whereDate('fromDate', '<=', $jdate)
                        ->whereDate('toDate', '>=', $jdate)
                        ->first();
                $hd = '';

                if ($holiday != null) {
                    $hd = $holiday->holidayName;
                } else if ($tdate->dayOfWeek == '0') {
                    $hd = 'SUNDAY';
                }
                $days[] = array($dt, $hd);
            }
        }
        if ($request->has('idMonth'))
        return view('schools.hostel.monthly_attendance', compact('todaydate',  'days', 'tdate', 'classes', 'month', 'students', 'current_month', 'school', 'months'));
        else return view('schools.hostel.monthly_attendance', compact('todaydate',   'classes','current_month', 'school', 'months'));
    }

}

Copyright © 2021 - 2025 IMMREX7