IMMREX7
<?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 AttendanceController 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)
->whereNull('idClass')
->whereDate('fromDate', '<=', $jdate)
->whereDate('toDate', '>=', $jdate)
->first();
$hd = '';
if($school->idCountry == 1){
if ($holiday != null) {
if($holiday->type != "Other Event")
$hd = $holiday->holidayName;
} else if ($date->dayOfWeek == '0') {
$hd = 'SUNDAY';
}
}else{
if ($holiday != null) {
if($holiday->type != "Other Event")
$hd = $holiday->holidayName;
} else if ($date->dayOfWeek == '5') {
$hd = 'Friday';
}
}
$days[] = array($jdate, $hd);
}
}
$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;
$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 = [];
}
if(Auth::guard('school')->user()->idSchool == 82){
return view('schools.attendance.view_attendance_ssm', compact('todaydate', 'classes', 'days', 'students', 'current_month', 'school', 'months', 'total_present', 'total_absent'));
}
return view('schools.attendance.view_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) {
$school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
$classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$students = [];
$idClass = $request->idClass;
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();
if(Auth::guard('school')->user()->idSchool == 82){
return view('schools.attendance.manual_attendance_ssm', compact('classes', 'students', 'month', 'current_month','school'));
}
return view('schools.attendance.manual_attendance', compact('classes', 'students', 'month', 'current_month','idClass','school'));
}
/**
* 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\Attendance::whereDate('date', '=', $jdate)
// ->where('idType', '=', 'M')
// ->get()->pluck('Enrollment_Number')->toArray();
// $newscr = $var;
// $detach = array_diff($attendancemarked, $newscr);
// \App\Attendance::whereDate('date', '=', $jdate)
// ->whereIn('Enrollment_Number', $detach)
// ->where('idType', '=', 'M')
// ->delete();
foreach ($var as $key1 => $var1) {
// dd($var1);
$a_exist = \App\Attendance::where('Enrollment_Number', '=', $var1['ecNo'])
->whereDate('date', '=', $jdate);
if(isset($request->routine)){
$a_exist= $a_exist->where('routine', '=', $request->routine);
}
$a_exist = $a_exist->where('idType', '=', 'M')
->first();
if (isset($var1['present']) && !$a_exist) {
$attendance = new \App\Attendance();
$attendance->fill($request->all());
$attendance->Enrollment_Number = $var1['ecNo'];
$attendance->idSchool = Auth::guard('school')->user()->idSchool;
$attendance->date = $key;
$attendance->status = 'P';
$attendance->idType = 'M';
if(isset($request->routine)){
$attendance->routine = $request->routine;
}
$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('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 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('attandance')
->join('students', 'attandance.Enrollment_Number', '=', 'students.ecNo')
->where('attandance.idMonth', '=', $monthid)
->where('students.idSection', '=', $sectionid)
->whereDate('attandance.date', '=', 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('attandance')
->join('students', 'attandance.Enrollment_Number', '=', 'students.ecNo')
->where('attandance.idMonth', '=', $monthid)
->where('students.idSection', '=', $sectionid)
->whereDate('attandance.date', '=', 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) {
if(isset($request->students)){
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();
$days[] = [];
$total_present = 0;
$total_absent = 0;
//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)
->whereNull('idClass')
->whereDate('fromDate', '<=', $jdate)
->whereDate('toDate', '>=', $jdate)
->where('idFinancialYear', '=', Session::get('idFinancialYear'))
->first();
$hd = '';
if($school->idCountry == 1){
if ($holiday != null) {
if($holiday->type != "Other Event")
$hd = $holiday->holidayName;
} else if ($tdate->dayOfWeek == '0') {
$hd = 'SUNDAY';
}
}else{
if ($holiday != null) {
if($holiday->type != "Other Event")
$hd = $holiday->holidayName;
} else if ($tdate->dayOfWeek == '5') {
$hd = 'Friday';
}
}
$days[] = array($dt, $hd);
}
if(Auth::guard('school')->user()->idSchool == 82){
return view('schools.attendance.monthly_attendance_ssm', compact('todaydate', 'classes', 'current_month', 'school', 'months', 'total_present', 'total_absent', 'days', 'month', 'students'));
}else
return view('schools.attendance.monthly_attendance', compact('todaydate', 'classes', 'current_month', 'school', 'months', 'total_present', 'total_absent', 'days', 'month', 'students'));
}
if(Auth::guard('school')->user()->idSchool == 82){
return view('schools.attendance.monthly_attendance_ssm', compact('todaydate', 'classes', 'current_month', 'school', 'months', 'total_present', 'total_absent', 'days'));
}else
return view('schools.attendance.monthly_attendance', compact('todaydate', 'classes', 'current_month', 'school', 'months', 'total_present', 'total_absent', 'days'));
}
}
Copyright © 2021 -