Exemplo n.º 1
0
 public static function absentEveryEmployee()
 {
     $employees = Employee::where('status', '=', 'active')->get();
     $absentess = [];
     foreach ($employees as $employee) {
         //Count the absent except half days
         foreach (Leavetype::where('leaveType', '<>', 'half day')->get() as $leave) {
             //$absentess[$employee->employeeID][$leave->leaveType] = 0;
             //      Half Day leaves are added to casual leaves.2 half days are equal to one Casual Leave
             $absentess[$employee->employeeID][$leave->leaveType] = Attendance::where('status', '=', 'absent')->where('employeeID', '=', $employee->employeeID)->where(function ($query) {
                 $query->where('application_status', '=', 'approved')->orWhere('application_status', '=', null);
             })->where('leaveType', '=', $leave->leaveType)->count();
         }
         // half days count
         foreach (Leavetype::where('leaveType', '=', 'half day')->get() as $leave) {
             $half_day = Attendance::select('halfDayType', DB::raw('count(*) as total'))->where('status', '=', 'absent')->where('employeeID', '=', $employee->employeeID)->where(function ($query) {
                 $query->where('application_status', '=', 'approved')->orWhere('application_status', '=', null);
             })->where('leaveType', '=', $leave->leaveType)->groupBy('halfDayType')->get();
             foreach ($half_day as $half) {
                 $absentess[$employee->employeeID][$half->halfDayType] += $half->total / 2;
             }
         }
         //  Total of All leaves
         $absentess[$employee->employeeID]['total'] = array_sum($absentess[$employee->employeeID]);
     }
     return $absentess;
 }
 /**
  * Show the form for creating a new resource.
  *
  * @return Response
  */
 public function create()
 {
     $rules = ['class' => 'required', 'section' => 'required', 'shift' => 'required', 'session' => 'required', 'regiNo' => 'required', 'date' => 'required', 'subject' => 'required'];
     $validator = \Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Redirect::to('/attendance/create')->withInput(Input::all())->withErrors($validator);
     } else {
         $exits = Attendance::select('date')->where('class', Input::get('class'))->where('section', Input::get('section'))->where('shift', Input::get('shift'))->where('session', trim(Input::get('session')))->where('subject', Input::get('subject'))->where('date', $this->parseAppDate(Input::get('date')))->get();
         if (count($exits) > 0) {
             $errorMessages = new Illuminate\Support\MessageBag();
             $errorMessages->add('Duplicate', 'Attendance already saved!!');
             return Redirect::to('/attendance/create')->withErrors($errorMessages);
         } else {
             $absentStudents = array();
             $students = Input::get('regiNo');
             $presents = Input::get('present');
             $all = false;
             if ($presents == null) {
                 $all = true;
             } else {
                 $ids = array_keys($presents);
             }
             $stpresent = array();
             foreach ($students as $student) {
                 $st = array();
                 $st['regiNo'] = $student;
                 if ($all) {
                     $st['status'] = 'No';
                 } else {
                     $st['status'] = $this->checkPresent($student, $ids);
                 }
                 if ($st['status'] == "No") {
                     array_push($absentStudents, $student);
                 }
                 array_push($stpresent, $st);
             }
             foreach ($stpresent as $stp) {
                 $attendance = new Attendance();
                 $attendance->class = Input::get('class');
                 $attendance->section = Input::get('section');
                 $attendance->shift = Input::get('shift');
                 $attendance->session = trim(Input::get('session'));
                 $attendance->subject = Input::get('subject');
                 $attendance->regiNo = $stp['regiNo'];
                 $attendance->status = $stp['status'];
                 $attendance->date = $this->parseAppDate(Input::get('date'));
                 $attendance->save();
             }
             //get sms format
             //loop absent student and get father's no and send sms
             $isSendSMS = Input::get('isSendSMS');
             if ($isSendSMS == null) {
                 return Redirect::to('/attendance/create')->with("success", "Students attendance save Succesfully.");
             } else {
                 if (count($absentStudents) > 0) {
                     foreach ($absentStudents as $absst) {
                         $student = DB::table('Student')->join('Class', 'Student.class', '=', 'Class.code')->select('Student.regiNo', 'Student.rollNo', 'Student.firstName', 'Student.middleName', 'Student.lastName', 'Student.fatherCellNo', 'Class.Name as class')->where('Student.regiNo', '=', $absst)->where('class', Input::get('class'))->first();
                         $msg = "Dear Parents your Child (Name-" . $student->firstName . " " . $student->middleName . " " . $student->lastName . ", Class- " . $student->class . " , Roll- " . $student->rollNo . " ) is Absent in School today.";
                         //  $fatherCellNo = Student::select('fatherCellNo','')->where('regiNo', $absst)->first();
                         $response = $this->sendSMS($student->fatherCellNo, "Supersoft", $msg);
                         $smsLog = new SMSLog();
                         $smsLog->type = "Attendance";
                         $smsLog->sender = "Supersoft";
                         $smsLog->message = $msg;
                         $smsLog->recipient = $student->fatherCellNo;
                         $smsLog->regiNo = $absst;
                         $smsLog->status = $response;
                         $smsLog->save();
                     }
                     return Redirect::to('/attendance/create')->with("success", "Students attendance saved and " . count($absentStudents) . " sms send to father numbers.");
                 } else {
                     return Redirect::to('/attendance/create')->with("success", "Students attendance save Succesfully.");
                 }
             }
         }
     }
 }