Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 304
NotificationsApiController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 9
3782
0.00% covered (danger)
0.00%
0 / 304
 checkinCheckout
0.00% covered (danger)
0.00%
0 / 1
132
0.00% covered (danger)
0.00%
0 / 76
 checkincheckoutDetail
0.00% covered (danger)
0.00%
0 / 1
182
0.00% covered (danger)
0.00%
0 / 49
 getcheckincheckouttime
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 18
 getcheckincheckouttimeapi
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 24
 getNotification
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 52
 updateReadAt
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 9
 markAllAsRead
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 5
 sendnotification
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 15
 getNotificationsJson
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 56
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\NotificationModel\Notification;
use Carbon\Carbon;
use App\User;
use App\Models\Dashboard\AttendanceDetail;
use Illuminate\Support\Facades\Validator;
use GetShiftDetailHelper;
use DB;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Route;
class NotificationsApiController extends Controller
{
    private $check_in;  
    public function checkinCheckout(Request $request)
    {
       
        //get user
        $user = Auth::user();
        //current date
        $currentDate = Carbon::now()->toDateString();
        $startDate = Carbon::parse(Carbon::now(config('settings.time_zone'))->toDateString().'00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone'));
        $endDate = Carbon::parse(Carbon::now(config('settings.time_zone'))->addDay()->toDateString().'00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone'));
        /* start of no checkin */
        $title = "No Checkin";
        $noCheckinMessage =  'You have missed check-in.';
        $url = 'home';
        $arr = null;
        $arr = [$title, $noCheckinMessage, $url];
        $check_data = [
            'notification' =>  $arr[0],
            'message' =>  $arr[1],
            'actionUrl' =>  $arr[2]
        ];
        $notifications  = Notification::where('notifiable_id', $user->id)
                                        ->where('data', json_encode($check_data))
                                        ->whereDate('created_at', $currentDate)
                                        ->first();
          
        if (!empty($notifications)) {
            $noCheckin = ['noCheckin' => true,
                         'message' => 'You have not checked in for the day! Mark your attendance'];
       
        }else{
            $noCheckin = ['noCheckin' => false];
        }
       
        /* end of no checkin */
        /* start of no check out */
        $noCheckoutMessage =  'You have missed check-out.';
        $noCheckoutArr = null;
        $noCheckoutArr = [$title, $noCheckoutMessage, $url];
        $noCheckout_data = [
            'notification' =>  $noCheckoutArr[0],
            'message' =>  $noCheckoutArr[1],
            'actionUrl' =>  $noCheckoutArr[2]
        ];
        $notifications  = Notification::where('notifiable_id', $user->id)
            ->where('data', json_encode($noCheckout_data))
            ->whereDate('created_at', $currentDate)
            ->first();
        
        if (!empty($notifications)) {
            $noCheckout = ['noCheckout' => true,
                            'message' => 'You have not checked out for the day! Mark your attendance'];
        
        }else{
            $noCheckout = ['noCheckout' => false];
        }
        /* end of no check out */
        /* start of late checkin  and early checkout*/
        
        //late checkin based on shift timing
        $currentTime = Carbon::now(config('settings.time_zone'))->toTimeString();
        $lateCheckin = null;
        $earlyCheckout = null;
        $getEarlyCheckout = null;
        if(!empty($user->userdetail)){
           //get shift details    
        $shift = GetShiftDetailHelper::getShiftDetail($user->id,$startDate,$endDate);
            
        if($shift  != null){ 
            
            $shiftStartTime =  $shift['shiftStartTime'];
            $shiftEndTime =  $shift['shiftEndTime'];
            $bufferCheckinTime = $shift['bufferCheckinTime'];
            $bufferCheckoutTime =$shift['bufferCheckoutTime'];
            $bufferCheckinSecs = strtotime($bufferCheckinTime) - strtotime("00:00:00");
            $bufferCheckoutSecs = strtotime($bufferCheckoutTime) - strtotime("00:00:00");
            $timeWithBuffer = date("H:i:s", strtotime($shiftStartTime) + $bufferCheckinSecs);
            $shiftEndTimeWithBuffer = date("H:i:s", strtotime($shiftEndTime));
        
        //late checkin
        if($currentTime > $timeWithBuffer){
            $lateCheckin = ['lateCheckin' => true,
                         'message' => 'You are checking in late for the day! Mark your attendance'];
        }else{
            $lateCheckin = ['lateCheckin' => false];
        }
        //early chekout
        if ($shiftStartTime > $shiftEndTime) // to check if the shift ends the next day
        {
            $time = Carbon::now(config('settings.time_zone'));
           
            $day = $time->day+1;
            //set time to shift start time
            $setShiftStartTime = Carbon::createFromFormat('Y-m-d H:i:s',$time->year.'-'.$time->month.'-'.$time->day.' '. $shiftStartTime,config('settings.time_zone'));
            
            //set time to next day 12am
            $set12am = Carbon::createFromFormat('Y-m-d H:i:s',$time->year.'-'.$time->month.'-'.$day.' 00:00:00',config('settings.time_zone')); 
    
            //current time is between shift start time and morning 00:00
            if($time->between($setShiftStartTime, $set12am, true)) {
                $getEarlyCheckout = true;
            }else{
                if($currentTime < $shiftEndTimeWithBuffer) { //current time is after 12am or before shift end time
                    $getEarlyCheckout = true;
                }else{
                    $getEarlyCheckout = false;
                }
            }   
        }else{
            if($currentTime < $shiftEndTimeWithBuffer){
                $getEarlyCheckout = true;
            }else{
                $getEarlyCheckout = false;
            }
        }
        
        if($getEarlyCheckout == true){
            $earlyCheckout = ['earlyCheckout' => true,
                             'message' => 'You are checking out early for the day! Mark your attendance' ];
        }else{
            $earlyCheckout = ['earlyCheckout' => false];
        }
        /* end of late checkin  and early checkout*/
        /* start of get checkin value */
        /*$this->checkincheckoutDetail();
        $dcheck_in = null;
        $dcheck_out = null;
        if ($this->check_in != null) {
            $dcheck_in = $this->check_in;
        }
        if ($this->check_out != null) {
            $dcheck_out = $this->check_out;
        }
*/
        $timearr = $this->getcheckincheckouttime($request);
        /* end of get checkin value */
      
        return response()->json([
            'status' => '200',
            'checkin' => $timearr[0],
            'checkout' => $timearr[1],
            'noCheckin' => $noCheckin,
            'noCheckout' => $noCheckout,
            'lateCheckin' => $lateCheckin,
            'earlyCheckout' => $earlyCheckout
        ]);
    }
}
    
}
    public function checkincheckoutDetail()
    {
        $user = auth()->user();
        $currentDateTime = Carbon::now();
        $startDate = Carbon::parse(Carbon::now(config('settings.time_zone'))->toDateString().'00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone'));
        $endDate = Carbon::parse(Carbon::now(config('settings.time_zone'))->addDay()->toDateString().'00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone'));
        if (isset($user->userdetail)) {
            //get shift details    
            $shift = GetShiftDetailHelper::getShiftDetail($user->id,$startDate,$endDate);
                
            if($shift  != null){ 
              
                $shiftStartTime =  $shift['shiftStartTime'];
                $shiftEndTime =  $shift['shiftEndTime'];
                $bufferTime =  $shift['bufferTime'];
                
                $query = AttendanceDetail::query();
                $query->where('user_id', '=', $user->id);
                if ($shiftStartTime > $shiftEndTime) // to check if the shift ends the next day
                {
                    $user_id = $user->id;
                    $today_date_time  = Carbon::now();
                    $time_now = $today_date_time->toTimeString(config('settings.time_zone'));
                    
                    $query->whereDate('check_in', '=', $today_date_time);
                    $attendanceDetail = $query->get();
                    $offsetStartTime = Carbon::parse($shiftStartTime);
                    $offsetStartTime = $offsetStartTime->subHours(4)->toTimeString();
                    $offsetEndTime = Carbon::parse($shiftEndTime);
                    $offsetEndTime = $offsetEndTime->addHours(4)->toTimeString();
                    if ($attendanceDetail->isEmpty())      //if no attendance is marked for the user that particular day
                    {
                       
                        if (($time_now < $shiftStartTime && $time_now > $offsetStartTime) ||
                            ($time_now == $shiftStartTime) || ($time_now > $shiftStartTime && ($offsetEndTime < $time_now))
                        ) {
                            $attendanceDetail = AttendanceDetail::where('user_id', '=', $user->id)
                                                                ->whereDate('check_in', '=', $currentDateTime->toDateString())
                                                                ->get();
                         
                        } else {
                           
                            $subday_date_time = $today_date_time->subday()->toDateString();
                            $attendanceDetail = AttendanceDetail::where('user_id', '=', $user->id)
                                                                ->whereDate('check_in', '=', $subday_date_time)
                                                                ->get();
                           
                        }
                    }
                } else {
                    $query->whereDate('check_in', '=', $currentDateTime->toDateString());
                    $query->orwhere('user_id', '=', $user->id);
                    $query->whereDate('check_out', '=', $currentDateTime->toDateString());
                    $attendanceDetail = $query->get();
                }
            }
            if(!empty($attendanceDetail)){
            
            foreach ($attendanceDetail as $value) {
                if (isset($value->user_id)) {
                    $this->check_in = $value->check_in;
                    $dcheck_in = Carbon::parse($this->check_in);
                    $dcheck_in->setTimezone(config('settings.time_zone'));
                    $this->check_in =  $dcheck_in->format(config('settings.php_date_format') .' '. config('settings.time_format'));
                    $this->check_out = $value->check_out;
                    $dcheck_out = Carbon::parse($this->check_out);
                    $dcheck_out->setTimezone(config('settings.time_zone'));
                    $this->check_out =  $dcheck_out->format(config('settings.php_date_format') .' '. config('settings.time_format'));
                }
            }
           }
        } else {
            return;    // user/userdetail not entered in AMS
        }
    }
    public function getcheckincheckouttime(Request $request)
    {
        $user = Auth::user();
        $today_date_time  = Carbon::now();
        $attendanceDetail = AttendanceDetail::where('user_id', '=', $user->id)
                            ->whereDate('check_in', '=', $today_date_time)
                            ->first();
        $dcheck_in = '';
        $dcheck_out = '';
        if(isset($attendanceDetail) && !empty($attendanceDetail))
        {
            if ($attendanceDetail->check_in != null) {
                $dcheck_in = Carbon::parse($attendanceDetail->check_in);
                    $dcheck_in->setTimezone(config('settings.time_zone'));
                    $dcheck_in =  $dcheck_in->format(config('settings.php_date_format') .' '. config('settings.time_format'));
            
            }
            if ($attendanceDetail->check_out != null) {
                $dcheck_out = Carbon::parse($attendanceDetail->check_out);
                    $dcheck_out->setTimezone(config('settings.time_zone'));
                    $dcheck_out =  $dcheck_out->format(config('settings.php_date_format') .' '. config('settings.time_format'));
            }
        }
        $timearr = array($dcheck_in,$dcheck_out);
        
        
        /* end of get checkin value */
       
        return $timearr;
    }
    public function getcheckincheckouttimeapi(Request $request)
    {
        $user = Auth::user();
        $today_date_time  = Carbon::now();
        $attendanceDetail = AttendanceDetail::where('user_id', '=', $user->id)
                            ->whereDate('check_in', '=', $today_date_time)
                            ->first();
        $dcheck_in = '';
        $dcheck_out = '';
        if(isset($attendanceDetail) && !empty($attendanceDetail))
        {
            if ($attendanceDetail->check_in != null) {
                $dcheck_in = Carbon::parse($attendanceDetail->check_in);
                    $dcheck_in->setTimezone(config('settings.time_zone'));
                    $dcheck_in =  $dcheck_in->format(config('settings.php_date_format') .' '. config('settings.time_format'));
            
            }
            if ($attendanceDetail->check_out != null) {
                $dcheck_out = Carbon::parse($attendanceDetail->check_out);
                    $dcheck_out->setTimezone(config('settings.time_zone'));
                    $dcheck_out =  $dcheck_out->format(config('settings.php_date_format') .' '. config('settings.time_format'));
            }
        }
        if(Auth::user()->profile_pic)
            $imageurl = url('storage/'. Auth::user()->profile_pic);
        else
            $imageurl = url('global_assets/images/placeholders/placeholder.jpg');
       return response()->json([
            'checkin'=> $dcheck_in,
            'checkout' => $dcheck_out,
            'username'     => $user->name,
            'imageurl' => $imageurl
        ]);     
    }
    /*public function getNotification()
    {   
        $user = auth()->user();
        $notifications  = $user->notifications()->get();
        return response()->json(['status' => '200', 'notifications' => $notifications ]);
    }*/
    public function getNotification(Request $request){
        $user = auth()->user();
        if(isset($request->pageno))
        {
            $pageno = $request->pageno;
        }
        else
        {
            $pageno = 1;
        }
        /*echo $pageno;
        exit;*/
        $itms = DB::table('users')->where('users.id', $user->id)
                                    ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
                                    ->orderBy('notifications.created_at','DESC')
                                    ->offset( intval($request->input('pageno')))
                                    ->limit(10)->get();
        $text = '';
        $count = 0;
        if(!empty($itms)){
        foreach($itms as $itm){
        if(isset(json_decode($itm->data)->notification ) ){
            if(isset($itm->read_at ) ){
                $bold = 'style="font-weight:500;color:#2196f3;"';
                $count = 1;
            }else{
                $bold = 'style="font-weight:bold;"';
            }    
                if(json_decode($itm->data)->actionUrl != null){ 
                 if(json_decode($itm->data)->notification == 'Absent'){ 
                    $url =   url(json_decode($itm->data)->actionUrl.'?').urlencode(base64_encode(Carbon::parse($itm->created_at)->format('Y-m-d'))) ;
                 }else{
                    $url =   url(json_decode($itm->data)->actionUrl) ;
                 } 
                 }else{
                   $url =  url('particular-notification', $itm->id);
                  }      
            $created_at = Carbon::parse($itm->created_at);
            $created_at = Carbon::parse($created_at);
            $created_at->setTimezone(config('settings.time_zone'));
            $created_at =  $created_at->format(config('settings.php_date_format') .' '. config('settings.time_format'));
            
            $text .= '<a  href="'.$url.'">';
            $text .= '<div class="media-title" id="show-message" '.$bold.'>';
                         
            $text .= '<span class="font-weight-semibold">'.json_decode($itm->data)->notification.'</span>';
            $text .= '<span class="text-muted float-right font-size-sm" style="margin-right:5px;">';
                    
            $text .= $created_at.'</span>';
            $text .= '</div>';
            $text .=  '<span class="text-muted" '.$bold.'>'.json_decode($itm->data)->message.'</span>';
            $text .= '</a>';
        }else{
            if( ! isset($itm->read_at ) ){
                $bold = 'style="font-weight:bold"';
                $count = 1;
            }else{
                $bold = '';
            }
                
            /*$url = url('particular-notification', $itm->id);*/
            $text .= '<a  href="#">';
            $text .= '<div class="media-title" id="show-message" '.$bold.'>';
            $text .= '<span class="font-weight-semibold">'.$itm->name.'</span>';
            $text .= '<span class="text-muted float-right font-size-sm">'.$itm->data.'</span>';
            $text .= '</div>';
            $text .= '</a>'; 
                
        }
        }
        }   
        $text.= '<span class="text-muted"></span>';  
        $count = DB::table('users')->where('users.id', $user->id)
        ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
        ->whereNull('notifications.read_at')->count();                   
        return response()->json([
            'notificationsText'=> $text,
            'notificationsCount' => $count
        ]);                            
      }
    public function updateReadAt(Request $request)
    {   
        $validator = Validator::make($request->all(), [
            'id' =>  'required',
            'read_at' => 'required'
        ]);
        if ($validator->fails()) {
            return response()->json($validator->errors());
        }
        $user = auth()->user();
        $query = array(
            'id' => $request->id,
            'notifiable_id' => $user->id
        );
        $notifications  = Notification::where($query)->update(['read_at' => $request->read_at]);
        return response()->json(['status' => '200', 'message' => "read at updated successfully" ]);
    }
    public function markAllAsRead(Request $request){
        $user = auth()->user();
        $notifications = DB::table('users')->where('users.id', $user->id)
        ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
        ->update(['notifications.read_at'=>Carbon::now()]);
       
        return response()->json(['status' => '200', 'message' => "read all notification updated successfully" ]);
      }
      public function sendnotification(Request $request)
      {
        $user = auth()->user();
        $notifications = DB::table('users')->where('users.id', $user->id)
                        ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
                        ->where('notifications.is_sent','=',2)
                        ->whereNull('notifications.read_at')
                        ->orderBy('notifications.created_at','DESC')->first();
        $count = DB::table('users')->where('users.id', $user->id)
                        ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
                        ->whereNull('notifications.read_at')->count();
        /*$update = Notification::where('id','=',$notifications->id)->update(['is_sent'=>1]);*/
        if($count <= 0)
        {
            $count = "";
        }
        if(isset($notifications) && !empty($notifications))
        {
            $update = Notification::where('notifiable_id','=',$user->id)->update(['is_sent'=>1]);
            return response()->json(['status' => '200', 'msgtitle' =>  json_decode($notifications->data)->notification,'message' => json_decode($notifications->data)->message,'count' => ''.$count]);
        }
        else
        {
            return response()->json(['status' => '100', 'message' => "", 'msgtitle'=> '','count' => ''.$count]);
        }
      }
      public function getNotificationsJson(Request $request)
    {
        $pageno = 1;
        if(isset($request->pageno))
        {
            $pageno = $request->pageno;
        }
        $itms = DB::table('users')->where('users.id', auth()->id())
            ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
            ->orderBy('notifications.created_at','DESC')
            ->skip(($pageno-1) * 10)
            ->limit(10)->get();
        $text = '';
        $count = DB::table('users')->where('users.id', auth()->id())
            ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
            ->orderBy('notifications.created_at','DESC')->
            count();
        $notification_array = [];
        if(!empty($itms)){
            foreach($itms as $itm){
                if(isset(json_decode($itm->data)->notification ) ){
                    if(json_decode($itm->data)->actionUrl != null){
                        if(json_decode($itm->data)->notification == 'Absent'){
                            $url =   route(json_decode($itm->data)->actionUrl, urlencode(base64_encode(Carbon::parse($itm->created_at)->format('Y-m-d')))) ;
                        }else{
                            $contains = Str::contains(json_decode($itm->data)->actionUrl,'#');
                            if($contains){
                                $url_explode = explode('#',json_decode($itm->data)->actionUrl);
                                $url =   Route::has($url_explode[0]) ? route($url_explode[0],'#'.$url_explode[1])  : '';
                            }else{
                                $url =   Route::has(json_decode($itm->data)->actionUrl) ? route(json_decode($itm->data)->actionUrl)  : '';
                            }
                        }
                    }else{
                        $url =  url('particular-notification', $itm->id);
                    }
                    $created_at = Carbon::parse($itm->created_at);
                    $created_at = Carbon::parse($created_at);
                    $created_at->setTimezone(config('settings.time_zone'));
                    $created_at =  $created_at->format(config('settings.php_date_format') .' '. config('settings.time_format'));
                    $notification = json_decode($itm->data)->notification;
                    $message = json_decode($itm->data)->message;
                    $read_at = $itm->read_at;
                    $dataArr = [];
                    $dataArr['created_at'] = $created_at;
                    $dataArr['notification'] = $notification;
                    $dataArr['message'] = $message;
                    $dataArr['read_at'] = $read_at;
                    $notification_array[] = $dataArr;
                }else{
                    $created_at = Carbon::parse($itm->created_at);
                    $created_at = Carbon::parse($created_at);
                    $created_at->setTimezone(config('settings.time_zone'));
                    $created_at =  $created_at->format(config('settings.php_date_format') .' '. config('settings.time_format'));
                    $notification = json_decode($itm->data)->notification;
                    $message = json_decode($itm->data)->message;
                    $read_at = $itm->read_at;
                    $dataArr = [];
                    $dataArr['created_at'] = $created_at;
                    $dataArr['notification'] = $notification;
                    $dataArr['message'] = $message;
                    $dataArr['read_at'] = $read_at;
                    $notification_array[] = $dataArr;
                }
            }
        }
        return response()->json([
            'status' => 200,
            'notifications'=> json_encode($notification_array),
            'notificationsCount' => $count
        ]);
    }
}