IMMREX7

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

<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use \App\Http\SendNotificationApi;
use Carbon\Carbon;
use PDF;
use App\Http\SendSmsApi;

class AttendanceController extends Controller {

    public $successStatus = 200;

    public function manageBusTransaction(){
        
        $totalStudent = 0;
        $total = DB::table('bus_transaction')->where('idSchool',140)->where('idFinancialYear',271)->get();
        foreach($total as $tel){
            $students = DB::table('students')->where('idStudent',$tel->idStudent)->first();
            if($students->idFinancialYear != $tel->idFinancialYear){
                $totalStudent++;
            }
        }
        echo "Total -> ".$totalStudent.'<br>';
        return ;
        $transaction = DB::table('bus_transaction')->where('idSchool',140)->where('idFinancialYear',271)->get();
        foreach($transaction as $var){
            $students = DB::table('students')->where('idStudent',$var->idStudent)->first();
            if($students  != null){
                if($students->idFinancialYear != $var->idFinancialYear){
                    echo $var->idBusTransaction.'->EStudent->'.$students->idStudent.' fy->'.$students->idFinancialYear.' ecNo->'.$students->ecNo;
                    $ecNo = $students->ecNo;
                    $studentOld = DB::table('students')->where('idSchool',140)->where('idFinancialYear',271)->where('ecNo',$ecNo)->first();
                    if($studentOld != null){
                        echo 'UStudent->'.$studentOld->idStudent.' fy->'.$studentOld->idFinancialYear.' ecNo->'.$studentOld->ecNo.'<br>';
                        if($studentOld->idFinancialYear ==  $var->idFinancialYear){
                            //update the new student id in 
                            //bus transaction and bus transaction details
                            //bus transport
                            echo 'Update Data'.'<br>';
                            $idBusTransaction = DB::table('bus_transaction')
                            ->where('idStudent', $students->idStudent)
                            ->where('idSchool',140)
                            ->where('idFinancialYear',271)->get()->pluck('idBusTransaction')->toArray();

                            DB::table('bus_transaction')
                            ->where('idStudent', $students->idStudent)
                            ->where('idSchool',140)
                            ->where('idFinancialYear',271)
                            ->update(array('idStudent' => $studentOld->idStudent));

                            DB::table('student_transport')
                            ->where('idStudent', $students->idStudent)
                            ->where('idSchool',140)
                            ->where('idFinancialYear',271)
                            ->update(array('idStudent' => $studentOld->idStudent));

                            DB::table('bus_transaction_details')
                            ->where('idStudent', $students->idStudent)
                            ->whereIn('idBusTransaction',  $idBusTransaction)
                            ->update(array('idStudent' => $studentOld->idStudent));

                            echo 'Update Done'.'<br>';
                        }
                    }
                }
            }
        }
    }

    public function printQuotations($str){
        $idQ = explode("QO1201",$str);
        $quotation = \App\Quotation::where('idQuotation',$idQ[1])->first();
        if($quotation == null) return redirect('/');
        $school = \App\School::where('idSchool', '=',$quotation->idSchool)->first();
        $pdf = PDF::loadView('schools.stock.print_quotation_order', ['margin_top' => 20], compact('quotation', 'school'));
        return $pdf->stream('quotation.pdf');
    }

    public function testNotification($regId){
        $id = [];
        array_push($id,$regId);
        //return SendNotificationApi::subscribeToTopic($id);
        return SendNotificationApi::testSingleNotification($id,"Visitors - Test came to visit you for the purpose of test");
        //return json_encode(['result' => 1]);
    }

    public function fixDiscount(Request $request){
        if($request->get('id') != "" && $request->get('fy') != ""){
            $idSchool = $request->get('id');
            $fy = $request->get('fy');
            $discounts = DB::table('student_discounts')->select('idStdDiscount','idStudent')->where('idSchool',$idSchool)->where('idFinancialYear',$fy)->get();
            foreach($discounts as $var){
                $student = DB::table('students')->where('idStudent',$var->idStudent)->first();
                if($student != null){
                    $promotions = \App\AdmEntry::where('idSchool', '=', $student->idSchool)
                                    ->where('father_mobile','=',$student->father_mobile)
                                    ->where('father_fname','=',$student->father_fname)
                                    ->where('ecNo','=',$student->ecNo)
                                    ->where('idFinancialYear','=',$fy)
                                    ->where('isActive','Y')
                                    ->first();
                    if($promotions != null){
                        if($var->idStudent != $promotions->idStudent)
                        echo 'Student id changed from'.$var->idStudent.' to '.$promotions->idStudent.'<br>';
                    }                
                }
            }
        }
    }

    public function printPo($str){
        $idQ = explode("PO201",$str);
        $po = \App\PurchaseOrder::where('idPurchaseOrder',$idQ[1])->first();
        if($po == null) return redirect('/');
        $school = \App\School::where('idSchool', '=',$po->idSchool)->first();
        $pdf = PDF::loadView('schools.stock.print_purchase_order', ['margin_top' => 20], compact('po', 'school'));
        return $pdf->stream('purchase_order.pdf');
    }

    public function setAttendanceData(Request $request){
        $ecno = $request->input('ecno');
        if ($ecno) {
            $timestamp = Carbon::now()->timestamp;
            $url = "https://online-login.online/window/attendance";
            $deviceId = "161";
            $status = $request->input('status');
            $arr = $request->input('ecno');
            foreach($arr as $key=>$value){
                if($value != ""){
                    $this->sendCurlPostRequest($url, $deviceId,$value,$status,$timestamp);
                    echo "Inserting .. ".$value.'<br>';
                }
                
            }
            echo 'To return <a href="https://online-login.online/window/attendance">Return back to form</a> <br>';
        }else{
            $student = DB::table('students')->where('idSchool',192)->get();
            return view('mt',compact('student'));
        }
    }
    private function sendCurlPostRequest($url, $deviceId,$ecNo,$status,$timestamp) {
        $postData = array(
            'Device_ID' => $deviceId,
            "TimeStamp" => $timestamp,
            "status" => $status,
            'Enrollment_Number' => $ecNo
        );

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            $errorMessage = 'cURL Error: ' . curl_error($ch);
            curl_close($ch);
            return $errorMessage;
        } else {
            curl_close($ch);
            return $response;
        }
    }

    public function insertBusAttandance(Request $request){
        date_default_timezone_set("Asia/Kolkata");
        $school = \App\School::where('Device_ID', '=', $request->Device_ID)->first();
        $student = \App\AdmEntry::where('ecNo', '=', $request->Enrollment_Number)->where('idSchool', '=', $school->idSchool)->first();
        if($student != null){
            $attendance = new \App\BusAttendance();
            $attendance->create_month = Carbon::createFromFormat('Y-m-d H:i:s', Carbon::createFromTimestamp($request->TimeStamp)->format('Y-m-d H:i:s'))->month;
            $attendance->idSchool = $school->idSchool;
            $attendance->idDevice = $request->Device_ID;
            $attendance->enrollmentNo = $request->Enrollment_Number;
            $attendance->timestamp = $request->TimeStamp;
            $attendance->status = $request->status;
            $attendance->isAutomatic = "Y";
            $attendance->created_on = Carbon::createFromTimestamp($request->TimeStamp)->format('Y-m-d');
            $attendance->save();

            $reachAt = Carbon::createFromTimestamp($request->TimeStamp);
            $now = Carbon::now();
            $totalDuration = $now->diffInSeconds($reachAt);
            if($totalDuration < 3600){
                if($school->notificationmode == "auto"){   
                    $reg_ids = DB::table('students')
                                            ->join('parents', 'students.father_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();
                
                    SendNotificationApi::sendSingleNotification($reg_ids, "Dear Parent, Your Child " . $student->firstName . " is " . $request->status);

                    $reg_ids = DB::table('students')
                                            ->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();

                    SendNotificationApi::sendSingleNotification($reg_ids, "Dear Parent, Your Child " . $student->firstName . " is " . $request->status);
                }
            }                    
           
            return response()->json(['success' => 'Data Inserted Successfully'], $this->successStatus);
        }else{
            return response()->json(['error' => 'Error in inserting data'], 401);
        }
    }


    private function getCurrentYear($idSchool){
        $finyear = \App\FinancialYear::where('idSchool', '=', $idSchool)->get();
        $now = Carbon::now();
        foreach ($finyear as $fy) {
            $start_date = Carbon::parse($fy->startDate);
            $end_date = Carbon::parse($fy->endDate);
            if ($now->between($start_date, $end_date) == true || $end_date->diffInDays($now) == 0) {
                return $fy->idFinancialYear;
            }
        }
        return 0;
    }


    public function insertInternationData(Request $request){
        //date_default_timezone_set("Asia/Qatar");

        $school = \App\School::where('Device_ID', '=', $request->Device_ID)->first();
            //return json_encode($school);
        if($school == null){
            return response()->json(['error' => 'Error in inserting data'], 401);
        }
        $idFinancialYear = $this->getCurrentYear($school->idSchool);           
        $student = \App\AdmEntry::where('ecNo', '=', $request->Enrollment_Number)->where('idSchool', '=', $school->idSchool)->where('idFinancialYear', $idFinancialYear)->orderBy('idStudent','DESC')->first();
        $employee = \App\Employee::where('enrollmentNo', '=', $request->Enrollment_Number)->where('idSchool', '=', $school->idSchool)->first();
        
        if ($student) {
            if($student->isActive == "N") return response()->json(['success' => 'Data Inserted Successfully','result' => 0], $this->successStatus);
            $attendance = new \App\Attendance();
            $attendance->idRequest = json_encode($request->getContent());
            $attendance->fill($request->all());
            $attendance->idMonth = Carbon::createFromFormat('Y-m-d H:i:s', Carbon::createFromTimestamp($request->TimeStamp)->format('Y-m-d H:i:s'))->month;
            $attendance->idSchool = $school->idSchool;
            $attendance->date = Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y');
            $attendance->save();
            $father = \App\Parents::where('mobile', '=', $student->father_mobile)->first();
            $father_mobile = $father->mobile;
            $phone_number = implode(',', array($father_mobile));

            if ($request->status == 'IN') {
                if($school->smsmode == "auto"){
                    $message = 'Dear Parent, Your Child ' . $student->firstName . ' has reached ' .$school->schoolName.' at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards '.$school->sms_regard_text.'.';
                        if($school->notificationmode == "auto"){
                            $reg_ids = DB::table('students')
                                            ->join('parents', 'students.father_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                            $reg_ids = DB::table('students')
                                                    ->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
                                                    ->select('parents.idFirebase')
                                                    ->where('students.idStudent', $student->idStudent)
                                                    ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                        }
                        $template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'school_reached')->first();
                        if (!empty($template)) {
                            $tempid = $template->template_id;
                            if($template->status == "Y")
                            \App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
                        }
                        
                }
            } else {
                if($school->smsmode == "auto"){
                    $message = 'Dear Parent, Your Child ' . $student->firstName . ' has left School ' .$school->schoolName. ' at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards '.$school->sms_regard_text.'.';
                        if($school->notificationmode == "auto"){                        
                            $reg_ids = DB::table('students')
                                            ->join('parents', 'students.father_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                            $reg_ids = DB::table('students')
                                            ->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                        }

                        $template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'school_left')->first();
                        if (!empty($template)) {
                            $tempid = $template->template_id;
                            if($template->status == "Y")
                            \App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
                        }
                }
                
            }            
            return response()->json(['success' => 'Data Inserted Successfully'], $this->successStatus);
        } elseif($employee) {
            $empattendance = new \App\EmpAttendance();
            $empattendance->idRequest = json_encode($request->getContent());
            $empattendance->fill($request->all());
            $empattendance->idMonth = Carbon::createFromFormat('Y-m-d H:i:s', Carbon::createFromTimestamp($request->TimeStamp)->format('Y-m-d H:i:s'))->month;
            $empattendance->idSchool = $school->idSchool;
            $empattendance->date = Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y');
            $empattendance->save();
            //
            $employee = \App\Employee::where('enrollmentNo', '=', $request->Enrollment_Number)->where('idSchool', '=', $school->idSchool)->first();
            $phone_number = $employee->mobile;
            if ($request->status == 'IN') {
                if($school->smsmode == "auto"){
                        if($school->idSchool == 73 || $school->idSchool == 79){
                            $message = 'Dear Admin, ' . $employee->firstName . ' has reached at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards '.$school->sms_regard_text.'.';
                        }else
                        $message = 'Dear Admin, ' . $employee->firstName . ' has reached at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards, Regards JIJAU ENTP.';
                        $template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'emp_attendance')->first();
                        if (!empty($template)) {
                            $tempid = $template->template_id;
                            if($template->status == "Y")
                            \App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
                        }
                }
            }
            if ($request->status == 'OUT') {
                if($school->smsmode == "auto"){
                    if($school->idSchool == 73 || $school->idSchool == 79){
                        $message = 'Dear Admin, ' . $employee->firstName . ' left at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards '.$school->sms_regard_text.'.';
                    }else $message = 'Dear Admin, ' . $employee->firstName . ' left at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards, Regards JIJAU ENTP.';
                        $template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'emp_attendance_left')->first();
                        if (!empty($template)) {
                            $tempid = $template->template_id;
                            if($template->status == "Y")
                            \App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
                        }
                }
            }
            //

            return response()->json(['success' => 'Data Inserted Successfully'], $this->successStatus);
        } else {
            return response()->json(['error' => 'Error in inserting data'], 401);
        }
    
    }

    public function insertAttandanceData(Request $request) {
        \Log::info($request->Enrollment_Number);

        if($request->Device_ID == 177){
            return $this->insertInternationData($request);
        }
        
        date_default_timezone_set("Asia/Kolkata");       

        if($request->Device_ID == 161){
            $school = \App\School::where('idSchool', '=', 135)->first();
            $idFinancialYear = $this->getCurrentYear($school->idSchool);       
            $student = \App\AdmEntry::where('ecNo', '=', $request->Enrollment_Number)->where('idFinancialYear', $idFinancialYear)->where('idSchool', '=', 135)->orderBy('idStudent','DESC')->first();
            $employee = \App\Employee::where('enrollmentNo', '=', $request->Enrollment_Number)->where('idSchool', '=', 135)->first();
            if($student == null &&  $employee == null){
                $school = \App\School::where('idSchool', '=', 159)->first();
                $idFinancialYear = $this->getCurrentYear($school->idSchool);    
                $student = \App\AdmEntry::where('ecNo', '=', $request->Enrollment_Number)->where('idFinancialYear', 328)->where('idSchool', '=', 159)->orderBy('idStudent','DESC')->first();
                if($student == null)
                    $student = \App\AdmEntry::where('ecNo', '=', $request->Enrollment_Number)->where('idFinancialYear', 295)->where('idSchool', '=', 159)->orderBy('idStudent','DESC')->first();
                
                $employee = \App\Employee::where('enrollmentNo', '=', $request->Enrollment_Number)->where('idSchool', '=', 159)->first();


                \Log::info($this->getCurrentYear($school->idSchool));
                if($student == null &&  $employee == null){
                    $school = \App\School::where('idSchool', '=', 183)->first();
                    $idFinancialYear = $this->getCurrentYear($school->idSchool);    
                    $student = \App\AdmEntry::where('ecNo', '=', $request->Enrollment_Number)->where('idFinancialYear', $idFinancialYear)->where('idSchool', '=', 183)->orderBy('idStudent','DESC')->first();
                    $employee = \App\Employee::where('enrollmentNo', '=', $request->Enrollment_Number)->where('idSchool', '=', 183)->first();
                }
                if($student == null &&  $employee == null){
                    $school = \App\School::where('idSchool', '=', 181)->first();
                    $idFinancialYear = $this->getCurrentYear($school->idSchool);    
                    $student = \App\AdmEntry::where('ecNo', '=', $request->Enrollment_Number)->where('idFinancialYear', $idFinancialYear)->where('idSchool', '=', 181)->orderBy('idStudent','DESC')->first();
                    $employee = \App\Employee::where('enrollmentNo', '=', $request->Enrollment_Number)->where('idSchool', '=', 181)->first();
                }
            }

            \Log::info(json_encode( $student));
            \Log::info(json_encode( $school));
        }else if($request->Device_ID == 183){
            $school = \App\School::where('idSchool', '=', 140)->first();
            $idFinancialYear = $this->getCurrentYear($school->idSchool);       
            $student = \App\AdmEntry::where('ecNo', '=', $request->Enrollment_Number)->where('idFinancialYear', $idFinancialYear)->where('idSchool', '=', 140)->orderBy('idStudent','DESC')->first();
            $employee = \App\Employee::where('enrollmentNo', '=', $request->Enrollment_Number)->where('idSchool', '=', 140)->first();
            if($student == null &&  $employee == null){
                $school = \App\School::where('idSchool', '=', 160)->first();
                $idFinancialYear = $this->getCurrentYear($school->idSchool);    
                $student = \App\AdmEntry::where('ecNo', '=', $request->Enrollment_Number)->where('idFinancialYear', $idFinancialYear)->where('idSchool', '=', 160)->orderBy('idStudent','DESC')->first();
                $employee = \App\Employee::where('enrollmentNo', '=', $request->Enrollment_Number)->where('idSchool', '=', 160)->first();
            }

            \Log::info(json_encode( $student));
            \Log::info(json_encode( $school));
        }else{
            $school = \App\School::where('Device_ID', '=', $request->Device_ID)->first();
            //return json_encode($school);
            if($school == null){
                return response()->json(['error' => 'Error in inserting data'], 401);
            }
            $idFinancialYear = $this->getCurrentYear($school->idSchool);           
            $student = \App\AdmEntry::where('ecNo', '=', $request->Enrollment_Number)->where('idSchool', '=', $school->idSchool)->where('idFinancialYear', $idFinancialYear)->orderBy('idStudent','DESC')->first();
            $employee = \App\Employee::where('enrollmentNo', '=', $request->Enrollment_Number)->where('idSchool', '=', $school->idSchool)->first();
        }
            
        if ($student) {
            if($student->isActive == "N") return response()->json(['success' => 'Data Inserted Successfully','result' => 0], $this->successStatus);
            $attendance = new \App\Attendance();
            $attendance->idRequest = json_encode($request->getContent());
            $attendance->fill($request->all());
            $attendance->idMonth = Carbon::createFromFormat('Y-m-d H:i:s', Carbon::createFromTimestamp($request->TimeStamp)->format('Y-m-d H:i:s'))->month;
            $attendance->idSchool = $school->idSchool;
            $attendance->date = Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y');
            $attendance->save();
            $father = \App\Parents::where('mobile', '=', $student->father_mobile)->first();
            if($father == null) return response()->json(['success' => 'Data Inserted Successfully','result' => 0], $this->successStatus);
            $father_mobile = $father->mobile;
//            $mother = \App\Parents::where('mobile', '=', $student->mother_mobile)->first();
//            $mother_mobile ='';
//            if($mother)
//            $mother_mobile = $mother->mobile;
//            else  $mother_mobile = $school->mobile;
            $phone_number = implode(',', array($father_mobile));

            if ($request->status == 'IN') {
                if($school->smsmode == "auto"){
                    //check time for notification
                    $reachAt = Carbon::createFromTimestamp($request->TimeStamp);
                    $now = Carbon::now();
                    $totalDuration = $now->diffInSeconds($reachAt);
                    if($totalDuration < 3600){
                        $message = 'Dear Parent, Your Child ' . $student->firstName . ' has reached ' .$school->schoolName.' at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards '.$school->sms_regard_text.'.';
                        $template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'school_reached')->first();
                        if (!empty($template)) {
                            $tempid = $template->template_id;
                                if($template->status == "Y")
                                \App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
                        }
                        DB::table('student_attendance_notification')->insert([
                            'idStudent' => $student->idStudent,
                            'notification' => $message,
                            'created_at' => date('Y-m-d'),
                        ]);
                        if($school->notificationmode == "auto"){
                            $reg_ids = DB::table('students')
                                            ->join('parents', 'students.father_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                            $reg_ids = DB::table('students')
                                                    ->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
                                                    ->select('parents.idFirebase')
                                                    ->where('students.idStudent', $student->idStudent)
                                                    ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                        } 
                    }else{
                        return response()->json(['success' => 'Data Inserted Successfully','result' => 0], $this->successStatus);
                    }
                }
                if($school->idSchool == 117){
                    $message = 'Dear Parent, Your Child ' . $student->firstName . ' has reached ' .$school->schoolName.' at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s');
                    if($school->notificationmode == "auto"){ 
                        $reachAt = Carbon::createFromTimestamp($request->TimeStamp);
                        $now = Carbon::now();
                        $totalDuration = $now->diffInSeconds($reachAt);
                        if($totalDuration < 3600){
                            $reg_ids = DB::table('students')
                                ->join('parents', 'students.father_mobile', '=', 'parents.mobile')
                                ->select('parents.idFirebase')
                                ->where('students.idStudent', $student->idStudent)
                                ->get()->pluck('idFirebase')->toArray();
                        
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                            $reg_ids = DB::table('students')
                                            ->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                        }
                    }
                }
            } else {
                if($school->smsmode == "auto"){
                    $reachAt = Carbon::createFromTimestamp($request->TimeStamp);
                    $now = Carbon::now();
                    $totalDuration = $now->diffInSeconds($reachAt);
                        if($totalDuration < 3600){
                        $message = 'Dear Parent, Your Child ' . $student->firstName . ' has left School ' .$school->schoolName. ' at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards '.$school->sms_regard_text.'.';
                        $template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'school_left')->first();
                        if (!empty($template)) {
                            $tempid = $template->template_id;
                            if($template->status == "Y")
                            \App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
                        } 
                        DB::table('student_attendance_notification')->insert([
                            'idStudent' => $student->idStudent,
                            'notification' => $message,
                            'created_at' => date('Y-m-d'),
                        ]);
                        if($school->notificationmode == "auto"){                        
                            $reg_ids = DB::table('students')
                                            ->join('parents', 'students.father_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                            $reg_ids = DB::table('students')
                                            ->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                        }
                    }
                }
                if($school->idSchool == 117){
                    $message = 'Dear Parent, Your Child ' . $student->firstName . ' has left School ' .$school->schoolName. ' at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s');
                    $reachAt = Carbon::createFromTimestamp($request->TimeStamp);
                    $now = Carbon::now();
                    $totalDuration = $now->diffInSeconds($reachAt);
                    if($totalDuration < 3600){ 
                        if($school->notificationmode == "auto"){                          
                            $reg_ids = DB::table('students')
                                            ->join('parents', 'students.father_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                            $reg_ids = DB::table('students')
                                            ->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
                                            ->select('parents.idFirebase')
                                            ->where('students.idStudent', $student->idStudent)
                                            ->get()->pluck('idFirebase')->toArray();
                            SendNotificationApi::sendSingleNotification($reg_ids, $message);
                        }
                    }
                }
                
            }
            
            return response()->json(['success' => 'Data Inserted Successfully'], $this->successStatus);
        } elseif($employee) {
            $empattendance = new \App\EmpAttendance();
            $empattendance->idRequest = json_encode($request->getContent());
            $empattendance->fill($request->all());
            $empattendance->idMonth = Carbon::createFromFormat('Y-m-d H:i:s', Carbon::createFromTimestamp($request->TimeStamp)->format('Y-m-d H:i:s'))->month;
            $empattendance->idSchool = $school->idSchool;
            $empattendance->date = Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y');
            $empattendance->save();
            //
            $employee = \App\Employee::where('enrollmentNo', '=', $request->Enrollment_Number)->where('idSchool', '=', $school->idSchool)->first();
            if ($request->status == 'IN') {
                if($school->smsmode == "auto"){
                    $reachAt = Carbon::createFromTimestamp($request->TimeStamp);
                    $now = Carbon::now();
                    $totalDuration = $now->diffInSeconds($reachAt);
                    $phone_number = $employee->mobile;
                    if($totalDuration < 3600){
                        if($school->idSchool == 73 || $school->idSchool == 79){
                            $message = 'Dear Admin, ' . $employee->firstName . ' has reached at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards '.$school->sms_regard_text.'.';
                        }else
                        $message = 'Dear Admin, ' . $employee->firstName . ' has reached at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards, Regards JIJAU ENTP.';
                        $template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'emp_attendance')->first();
                        if (!empty($template)) {
                            $tempid = $template->template_id;
                            if($template->status == "Y")
                            \App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
                        }
                    }
                }
            }
            if ($request->status == 'OUT') {
                if($school->smsmode == "auto"){
                    $reachAt = Carbon::createFromTimestamp($request->TimeStamp);
                    $now = Carbon::now();
                    $totalDuration = $now->diffInSeconds($reachAt);
                    $phone_number = $employee->mobile;
                    if($totalDuration < 3600){
                        if($school->idSchool == 73 || $school->idSchool == 79){
                            $message = 'Dear Admin, ' . $employee->firstName . ' left at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards '.$school->sms_regard_text.'.';
                        }else $message = 'Dear Admin, ' . $employee->firstName . ' left at ' . Carbon::createFromTimestamp($request->TimeStamp)->format('d-m-Y H:i:s').' Regards, Regards JIJAU ENTP.';
                        $template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'emp_attendance_left')->first();
                        if (!empty($template)) {
                            $tempid = $template->template_id;
                            if($template->status == "Y")
                            \App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
                        }
                    }
                }
            }
            //

            return response()->json(['success' => 'Data Inserted Successfully'], $this->successStatus);
        } else {
            return response()->json(['error' => 'Error in inserting data'], 401);
        }
    }


    public function insertReversalData(){
        $stdcheque = \App\StudentTransaction::where('idSchool', '=', 25)->where('status', '=', 'Bounced')->get();
        foreach($stdcheque as $vars){
            //\App\RollbackTransaction::insert($vars->toArray());
            $stdtransaction_dets = \App\StudentExtraTransactionDetail::where('idTransaction', '=', $vars->idTransaction)->get();
                    foreach ($stdtransaction_dets as $var) {
                        $det = \App\StudentExtraTransactionDetail::where('idTransactionDetail', '=', $var->idTransactionDetail)->first();
                         //fill details in rollback details
                        unset($det->idTransactionDetail);
                        //\App\RollbackTransactionDetails::insert($det->toArray());
                    }
        }
        $lstdcheque = \App\LesserTransaction::where('idSchool', '=', 25)->where('status', '=', 'Bounced')->get();
        foreach($lstdcheque as $vars){
            //\App\RollbackTransaction::insert($vars->toArray());
            $stdtransaction_dets = \App\LesserTransactionDetail::where('idLesserTransaction', '=', $vars->idLesserTransaction)->get();
                foreach ($stdtransaction_dets as $var) {
                    $det = \App\LesserTransactionDetail::where('idLessTransactionDet', '=', $var->idLessTransactionDet)->first();
                    //fill details in rollback details
                    unset($det->idLessTransactionDet);
                    //\App\RollbackTransactionDetails::insert($det->toArray());
                }
        }
        return json_encode($lstdcheque);
    }

}

Copyright © 2021 - 2025 IMMREX7