IMMREX7

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

<?php

namespace App\Http\Controllers\School;

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

class BusAttendanceController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        $todaydate = Carbon::today()->format('Y-m-d');
        if ($request->has('fromDate') && $request->fromDate != null || $request->has('toDate') && $request->toDate != null) {
            $from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
            $to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
            $period = \Carbon\CarbonPeriod::create($from_date, $to_date);

            foreach ($period as $date) {

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

                if($school->idCountry == 1){
                    if ($holiday != null) {
                        $hd = $holiday->holidayName;
                    } else if ($date->dayOfWeek == '0') {
                        $hd = 'SUNDAY';
                    }
                }else{
                    if ($holiday != null) {
                        $hd = $holiday->holidayName;
                    } else if ($date->dayOfWeek == '5') {
                        $hd = 'FRIDAY';
                    }
                }
                $days[] = array($jdate, $hd);
            }
        }


        $days = [];
        $months = \App\Month::get()->pluck('monthName', 'idMonth')->toArray();
        $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_present = 0;
        $total_absent = 0;

        if ($request->has('students') && count($request->students) > 0) {
            $students = \App\AdmEntry::whereIn('idStudent', $request->students)
                    ->where('idSchool', '=', $school->idSchool)
                    ->where('isActive', '=', 'Y')
                    ->where('idFinancialYear', '=', Session::get('idFinancialYear'))
                    ->get();
        } elseif ($request->has('sections') && count($request->sections) > 0) {
            $students = \App\AdmEntry::whereIn('idSection', $request->sections)
                    ->where('idSchool', '=', $school->idSchool)
                    ->where('isActive', '=', 'Y')
                    ->where('idFinancialYear', '=', Session::get('idFinancialYear'))
                    ->get();
        } else if ($request->has('classes') && count($request->classes) > 0) {
            $students = \App\AdmEntry::whereIn('idClass', $request->classes)
                    ->where('idSchool', '=', $school->idSchool)
                    ->where('isActive', '=', 'Y')
                    ->where('idFinancialYear', '=', Session::get('idFinancialYear'))
                    ->get();
        } else {
            $students = [];
        }

        return view('schools.attendance.view_bus_attendance', compact('todaydate', 'classes', 'days', 'students', 'current_month', 'school', 'months', 'total_present', 'total_absent'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request) {
        $classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $students = [];
        if ($request->has('idSection') && $request->idSection != null) {
            $students = \App\AdmEntry::where('idSection', '=', $request->idSection)->where('idFinancialYear', '=', Session::get('idFinancialYear'))->where('isActive', '=', 'Y')->get();
        } else {
            $students = \App\AdmEntry::where('idClass', '=', $request->idClass)->where('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();
        $bus = true;
        return view('schools.attendance.manual_attendance', compact('classes', 'students', 'month', 'current_month','bus'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
//        dd($request->all());
        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\BusAttendance::where('enrollmentNo', '=', $var1['ecNo'])
                            ->whereDate('created_on', '=', $jdate)
                            ->where('isAutomatic', '=', 'M')
                            ->first();
                    if (isset($var1['present']) && !$a_exist) {
                        $attendance = new \App\BusAttendance();
                        $attendance->enrollmentNo = $var1['ecNo'];
                        $attendance->idSchool = Auth::guard('school')->user()->idSchool;
                        $attendance->created_on = $jdate;
                        $attendance->create_month = $request->idMonth;
                        $attendance->timestamp = Carbon::now()->timestamp;
                        $attendance->status = 'P';
                        $attendance->isAutomatic = '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('BusAttendance 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 getStdAttendance($idMonth, $idsection) {
        $students = \App\AdmEntry::where('idSection', '=', $idsection)->get();
        $month = \App\Month::where('idMonth', '=', $idMonth)->first();
        return json_encode($students);
    }

    public function smstoAbsentStudent($monthid, $sectionid) {
        $present = DB::table('attendance_bus')
                ->join('students', 'attendance_bus.enrollmentNo', '=', 'students.ecNo')
                ->where('attendance_bus.create_month', '=', $monthid)
                ->where('students.idSection', '=', $sectionid)
                ->whereDate('attendance_bus.created_on', '=', Carbon::today()->toDateString())
                ->where('students.isActive', '=', 'Y')
                ->where('students.idFinancialYear', '=', Session::get('idFinancialYear'))
                ->select('idStudent')
                ->get()
                ->pluck('idStudent')
                ->toArray();
        $absentstudent = \App\AdmEntry::where('idSection', '=', $sectionid)
                ->whereNotIn('idStudent', $present)
                ->where('students.isActive', '=', 'Y')
                ->where('students.idFinancialYear', '=', Session::get('idFinancialYear'))
                ->select('idStudent', 'ecNo', 'firstName', 'middleName', 'lastName')
                ->get();
        return view('schools.attendance.sms-to-absent-students', compact('absentstudent'));
    }

    public function sendsmstoabsent(Request $request) {
//        dd($request->all());
        foreach ($request->students as $var) {
            $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
            $student = \App\AdmEntry::where('idStudent', '=', $var)->where('idSchool', '=', $school->idSchool)->first();
            $father_mobile = $student->father_mobile;
            $mother_mobile = $student->mpther_mobile;
            $phone_number = implode(',', array($father_mobile, $mother_mobile));
            $message = 'Dear Parent, Your Child - ' . $student->firstName . ' Absent Today - ' . $school->schoolName . ' Regards ' . $school->sms_regard_text . '.';
            $template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'manual_absent')->first();
            if (!empty($template)) {
                $tempid = $template->template_id;
                \App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
            }
        }
//        flash('Sms has been Sent Successfully !!');
        return redirect('school/attendance/create');
    }

    public function smstoPresentStudent($monthid, $sectionid) {
        $present = DB::table('attendance_bus')
                ->join('students', 'attendance_bus.enrollmentNo', '=', 'students.ecNo')
                ->where('attendance_bus.create_month', '=', $monthid)
                ->where('students.idSection', '=', $sectionid)
                ->whereDate('attendance_bus.created_on', '=', Carbon::today()->toDateString())
                ->where('students.isActive', '=', 'Y')
                ->where('students.idFinancialYear', '=', Session::get('idFinancialYear'))
                ->select('idStudent')
                ->get();
        return view('schools.attendance.sms-to-present-students', compact('present'));
    }

    public function sendsmstopresent(Request $request) {
        foreach ($request->students as $var) {
            $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
            $student = \App\AdmEntry::where('idStudent', '=', $var)->where('idSchool', '=', $school->idSchool)->first();
            $father_mobile = $student->father_mobile;
            $mother_mobile = $student->mpther_mobile;
            $phone_number = implode(',', array($father_mobile, $mother_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);
            }
        }
        flash('Sms has been Sent Successfully !!');
        return redirect('school/attendance/create');
    }

    public function monthlyAttendance(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 = $request->year;
            $days = [];

            if ($request->has('students') && count($request->students) > 0) {
                $students = \App\AdmEntry::whereIn('idStudent', $request->students)
                        ->where('idSchool', '=', $school->idSchool)
                        ->where('isActive', '=', 'Y')
                        ->where('idFinancialYear', '=', Session::get('idFinancialYear'))
                        ->get();
            } elseif ($request->has('sections') && count($request->sections) > 0) {
                $students = \App\AdmEntry::whereIn('idSection', $request->sections)
                        ->where('idSchool', '=', $school->idSchool)
                        ->where('isActive', '=', 'Y')
                        ->where('idFinancialYear', '=', Session::get('idFinancialYear'))
                        ->get();
            } else if ($request->has('classes') && count($request->classes) > 0) {
                $students = \App\AdmEntry::whereIn('idClass', $request->classes)
                        ->where('idSchool', '=', $school->idSchool)
                        ->where('isActive', '=', 'Y')
                        ->where('idFinancialYear', '=', Session::get('idFinancialYear'))
                        ->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)
                        ->where('idFinancialYear', '=', Session::get('idFinancialYear'))
                        ->first();
                $hd = '';

                if($school->idCountry == 1){
                    if ($holiday != null) {
                        $hd = $holiday->holidayName;
                    } else if ($tdate->dayOfWeek == '0') {
                        $hd = 'SUNDAY';
                    }
                }else{
                    if ($holiday != null) {
                        $hd = $holiday->holidayName;
                    } else if ($tdate->dayOfWeek == '5') {
                        $hd = 'FRIDAY';
                    }
                }
               
                $days[] = array($dt, $hd);
            }
        }

        $total_present = 0;
        $total_absent = 0;

        if ($request->has('idMonth'))
        return view('schools.attendance.monthly_bus_attendance', compact('todaydate', 'classes', 'current_month', 'school', 'months', 'total_present', 'total_absent', 'days', 'month', 'students'));
        else return view('schools.attendance.monthly_bus_attendance', compact('todaydate', 'classes', 'current_month', 'school', 'months', 'total_present', 'total_absent'));
    }

}

Copyright © 2021 - 2025 IMMREX7