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 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 273
UserRequestDiscussController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 13
14042
0.00% covered (danger)
0.00%
0 / 273
 index
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 userLeaveDiscuss
0.00% covered (danger)
0.00%
0 / 1
992
0.00% covered (danger)
0.00%
0 / 63
 userCompOffBalanceDiscuss
0.00% covered (danger)
0.00%
0 / 1
992
0.00% covered (danger)
0.00%
0 / 63
 userRequestAddCollaborator
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 33
 getUsersWithoutCollaborators
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 31
 removeCollaborator
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 13
 userAttendanceDiscuss
0.00% covered (danger)
0.00%
0 / 1
992
0.00% covered (danger)
0.00%
0 / 63
 create
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 store
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 show
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 edit
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 update
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 destroy
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
<?php
namespace App\Http\Controllers\Dashboard;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Auth\UserRequestDiscuss;
use App\Models\Auth\userRequestCollaborator;
use DB;
use Carbon\Carbon;
use App\Models\Auth\User;
use App\Models\Leave\ApplyLeave;
use App\Models\Leave\CompOffLeave;
use Illuminate\Support\Facades\Auth;
use App\Notifications\AppNotifications;
use GetShiftDetailHelper;
use App\Models\AttendanceRequests\UserAttendanceRequests;
class UserRequestDiscussController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function userLeaveDiscuss(Request $request, $id)
    {
        $leaveId = $id;
        $message = $request->message;
        $userId = Auth::user()->id;
        // store message
        $userDiscussCreate = UserRequestDiscuss::create([
            'module' => 'leave',
            'context_id' => $leaveId,
            'user_id' =>  $userId,
            'message' => $message
        ]);
        //get leave
        $leave = ApplyLeave::where('id', $leaveId)->first();
        //get user, who applied leave 
        $user = User::where('id', $leave->user_id)->first();
        //get user who send message
        $sender = User::where('id', $userId)->first();
        //primary reporting manager
        $primaryReportingManagerId =  $user->userdetail->primary_reporting_manager_id;
        //notification title
        $title = "Leave Discussion";
        //notification message
        $arr = [$title, $sender->name . ' ' . $sender->lastName . ' has send message.', 'home#leave-' . $leaveId];
     
        //get collaborators
        $collaborators = userRequestCollaborator::where('module', 'leave')
            ->where('context_id', $id)
            ->pluck('user_id')->toArray();
        //primary reporting manager
        $primaryReportingManagerId = $user->userdetail->primary_reporting_manager_id;
        
        //secondary reporting managers 
        $secondaryManagers = $user->userdetail->secondaryManager;
        
        //get secondary managers in array
        $secondaryManagersArray =  $secondaryManagers->pluck('id')->unique()->toArray();
        if($userId == $leave->user_id){
            if (!empty($collaborators)) {
                foreach ($collaborators as $collaborator) {
                    if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL || !in_array($collaborator,(array) $secondaryManagersArray)) {
                    $collaboratorUser = User::where('id', $collaborator)->first();
                    //send notification to all collaborators
                    $collaboratorUser->notify(new AppNotifications($arr));
                    }
                    
                }
            }
            //check if user has primary reporting manager
            if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL) {
                $primary_manger = User::where('id', $primaryReportingManagerId)->first();
                $primary_manger->notify(new AppNotifications($arr));
            } else {
 
                foreach ($secondaryManagers as $secondaryManager) {
                    if ($primaryReportingManagerId != $secondaryManager->id) {
                        $secondaryManager->notify(new AppNotifications($arr));
                    }
                }
            }
        
        
        }else if (!empty($collaborators) && in_array(auth()->user()->id, (array) $collaborators)) {
               foreach ($collaborators as $collaborator) {
                    $collaboratorUser = User::where('id', $collaborator)->first();
                    if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL || !in_array($collaborator, (array) $secondaryManagersArray)) {
                    //send notification to all collaborators
                    if(auth()->user()->id != $collaborator){
                    $collaboratorUser->notify(new AppNotifications($arr));
                    }
                    }
                }
                //check if user has primary reporting manager
                if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL) {
                    $primary_manger = User::where('id', $primaryReportingManagerId)->first();
                    $primary_manger->notify(new AppNotifications($arr));
                } else {
                    foreach ($secondaryManagers as $secondaryManager) {
                        if ($primaryReportingManagerId != $secondaryManager->id) {
                            $secondaryManager->notify(new AppNotifications($arr));
                        }
                    }
                }
                //send notification to user
                $user->notify(new AppNotifications($arr));
            
        }else{
            if (!empty($collaborators)) {
            foreach ($collaborators as $collaborator) {
                if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL || !in_array($collaborator, (array) $secondaryManagersArray)) {
                $collaboratorUser = User::where('id', $collaborator)->first();
                //send notification to all collaborators
                $collaboratorUser->notify(new AppNotifications($arr));
                }
                
            }
            }
            //send notification to user
            $user->notify(new AppNotifications($arr));
            if ($primaryReportingManagerId == 0 || $primaryReportingManagerId == NULL) {
            foreach ($secondaryManagers as $secondaryManager) {
                if ($sender->id != $secondaryManager->id) {
                    $secondaryManager->notify(new AppNotifications($arr));
                }
            }
        }
        }
        $leaveDiscuss = UserRequestDiscuss::with(['user', 'user.userdetail'])
            ->where('module', 'leave')
            ->where('context_id', $leaveId)
            ->orderBy('created_at', 'DESC')
            ->take(1)
            ->first();
        $response = array(
            'leaveDiscuss' => $leaveDiscuss
         );
        return response()->json($response);
    }
    public function userCompOffBalanceDiscuss(Request $request, $id)
    {
        $leaveId = $id;
        $message = $request->message;
        $userId = Auth::user()->id;
        // store message
        $userDiscussCreate = UserRequestDiscuss::create([
            'module' => 'leave',
            'context_id' => $leaveId,
            'user_id' =>  $userId,
            'message' => $message
        ]);
        //get leave
        $leave = CompOffLeave::where('id', $leaveId)->first();
        //get user, who applied leave 
        $user = User::where('id', $leave->user_id)->first();
        //get user who send message
        $sender = User::where('id', $userId)->first();
        //primary reporting manager
        $primaryReportingManagerId =  $user->userdetail->primary_reporting_manager_id;
        //notification title
        $title = "Request Discussion";
        //notification message
        $arr = [$title, $sender->name . ' ' . $sender->lastName . ' has send message.', 'home#leave-' . $leaveId];
     
        //get collaborators
        $collaborators = userRequestCollaborator::where('module', 'leave')
            ->where('context_id', $id)
            ->pluck('user_id')->toArray();
        //primary reporting manager
        $primaryReportingManagerId = $user->userdetail->primary_reporting_manager_id;
        
        //secondary reporting managers 
        $secondaryManagers = $user->userdetail->secondaryManager;
        
        //get secondary managers in array
        $secondaryManagersArray =  $secondaryManagers->pluck('id')->unique()->toArray();
        if($userId == $leave->user_id){
            if (!empty($collaborators)) {
                foreach ($collaborators as $collaborator) {
                    if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL || !in_array($collaborator,(array) $secondaryManagersArray)) {
                    $collaboratorUser = User::where('id', $collaborator)->first();
                    //send notification to all collaborators
                    $collaboratorUser->notify(new AppNotifications($arr));
                    }
                    
                }
            }
            //check if user has primary reporting manager
            if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL) {
                $primary_manger = User::where('id', $primaryReportingManagerId)->first();
                $primary_manger->notify(new AppNotifications($arr));
            } else {
 
                foreach ($secondaryManagers as $secondaryManager) {
                    if ($primaryReportingManagerId != $secondaryManager->id) {
                        $secondaryManager->notify(new AppNotifications($arr));
                    }
                }
            }
        
        
        }else if (!empty($collaborators) && in_array(auth()->user()->id, (array) $collaborators)) {
               foreach ($collaborators as $collaborator) {
                    $collaboratorUser = User::where('id', $collaborator)->first();
                    if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL || !in_array($collaborator, (array) $secondaryManagersArray)) {
                    //send notification to all collaborators
                    if(auth()->user()->id != $collaborator){
                    $collaboratorUser->notify(new AppNotifications($arr));
                    }
                    }
                }
                //check if user has primary reporting manager
                if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL) {
                    $primary_manger = User::where('id', $primaryReportingManagerId)->first();
                    $primary_manger->notify(new AppNotifications($arr));
                } else {
                    foreach ($secondaryManagers as $secondaryManager) {
                        if ($primaryReportingManagerId != $secondaryManager->id) {
                            $secondaryManager->notify(new AppNotifications($arr));
                        }
                    }
                }
                //send notification to user
                $user->notify(new AppNotifications($arr));
            
        }else{
            if (!empty($collaborators)) {
            foreach ($collaborators as $collaborator) {
                if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL || !in_array($collaborator, (array) $secondaryManagersArray)) {
                $collaboratorUser = User::where('id', $collaborator)->first();
                //send notification to all collaborators
                $collaboratorUser->notify(new AppNotifications($arr));
                }
                
            }
            }
            //send notification to user
            $user->notify(new AppNotifications($arr));
            if ($primaryReportingManagerId == 0 || $primaryReportingManagerId == NULL) {
            foreach ($secondaryManagers as $secondaryManager) {
                if ($sender->id != $secondaryManager->id) {
                    $secondaryManager->notify(new AppNotifications($arr));
                }
            }
        }
        }
        $leaveDiscuss = UserRequestDiscuss::with(['user', 'user.userdetail'])
            ->where('module', 'leave')
            ->where('context_id', $leaveId)
            ->orderBy('created_at', 'DESC')
            ->take(1)
            ->first();
        $response = array(
            'leaveDiscuss' => $leaveDiscuss
         );
        return response()->json($response);
    }
    //add collaborator
    public function userRequestAddCollaborator(Request $request)
    {
        $collaborator = $request->collaborator;
        $context_id =  $request->context_id;
        $module = $request->module;
        $checkCollaborator = userRequestCollaborator::where('module', $module)
            ->where('context_id', $context_id)
            ->where('user_id', $collaborator)->get();
        if (count($checkCollaborator) == 0) {
            $addCollaborator = userRequestCollaborator::create([
                'module' => $module,
                'context_id' => $context_id,
                'user_id' => $collaborator
            ]);
            $data  = '';
            if ($addCollaborator) {
                $user = User::where('id', $collaborator)->first();
                //send notification to collaborator
                $title = ucfirst($module) . " Discussion";
                if ($module == 'leave') {
                    $div = 'home#leave-' . $context_id;
                } else {
                    $div = 'home#attendance-' . $context_id;
                }
                $arr = [$title, auth()->user()->name . ' ' . auth()->user()->lastName . ' added you as collaborator.', $div];
                $user->notify(new AppNotifications($arr));
                if ($user->profile_pic) {
                    $user_image = url('storage/' . $user->profile_pic);
                } else {
                    if ($user->userdetail->gender == 'Male') {
                        $user_image = url("global_assets/images/favicon_io/default-avatar-boy.png");
                    } else {
                        $user_image = url("global_assets/images/favicon_io/default-avatar-girl.jpg");
                    }
                }
                $text = "'" . $module . "'";
                $data  .= '<div class="col-md-1 remove-collaborator-' . $addCollaborator->id . '" id="remove-collaborator-' . $addCollaborator->id . '" style="cursor: pointer;">';
                $data .= '<a class="boxclose" onclick="removeCollaborator(' . $addCollaborator->id . ',' . $context_id . ',' . $text . ')"> <i class="icon-close2"></i></a>';
                $data .= ' <img src="' . $user_image . '" title="' . $user->name . ' ' . $user->lastName . '" class="rounded-circle  rounded-border" width="32" height="32" alt="">';
                $data .= '</div>';
            }
            $response = array(
                'collaborator' => $data,
            );
            return response()->json($response);
        }
        return response()->json('');
    }
    //get users without collaborators
    public function getUsersWithoutCollaborators(Request $request)
    {
        $context_id =  $request->context_id;
        $module = $request->module;
        $user_id  = $request->user_id;
        $user = User::where('id', $user_id)->first();
        $primaryReportingManager = $user->userdetail->primary_reporting_manager_id;
        $secondaryManagers = $user->userdetail->secondaryManager;
        $managers = [];
        foreach ($secondaryManagers as $secondaryManager) {
            $managers[] = $secondaryManager->id;
        }
        $collaboratorsArray = userRequestCollaborator::where('module', $module)
            ->where('context_id', $context_id)->pluck('user_id')->unique()->toArray();
        $getUsers = User::where('id', '!=', auth()->user()->id)
            ->whereNotIn('id', $collaboratorsArray)
            ->get();
        if ($module == 'leave') {
            $viewAll = 'View All leave requests';
            $viewTeam = 'View All leave requests of team';
        } else if ($module == 'attendance') {
            $viewAll = 'View All Attendance request';
            $viewTeam = 'View All Attendance requests of team';
        }
        $usersText =  '';
        $usersText .= '<div class="form-group div-collaborator-names">';
        $usersText .= '<select  class="form-control  '.$module.'-collaborator-name-' . $context_id . '" id="'.$module.'-collaborator-name-' . $context_id . '">';
        $usersText .= '<option></option>';
        foreach ($getUsers as $getuser) {
            if ($getuser->id != $user_id && !empty($getuser->userdetail) && (in_array($getuser->id, $managers) || $getuser->can($viewAll) ||  $getuser->can($viewTeam))) {
                $usersText .= '<option value="' . $getuser->id . '">' . $getuser->name . ' ' . $getuser->lastName . '</option>';
            }
        }
        $usersText .= '</select>';
        $usersText .= '</div>';
        $response = array(
            'usersText' => $usersText
        );
        return response()->json($response);
    }
    public function removeCollaborator(Request $request)
    {
        $collaborator  = $request->userRequestCollaboratorId;
        $contextId =  $request->contextId;
        $module = $request->module;
        $userRequestCollaborator = userRequestCollaborator::where('id', $collaborator)
            ->first();
        //get user
        $user = User::where('id', $userRequestCollaborator->user_id)->first();
        //send notification to collaborator
        $title = ucfirst($module) . " Discussion";
        
        if ($userRequestCollaborator->delete()) {
            $arr = [$title, auth()->user()->name . ' ' . auth()->user()->lastName . ' removed you as collaborator.', null];
            $user->notify(new AppNotifications($arr));
            $response = array(
                'success' => true
            );
            return response()->json($response);
        }
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function userAttendanceDiscuss(Request $request, $id)
    {
        $attendanceId = $id;
        $message = $request->message;
        $userId = Auth::user()->id;
        // store message
        $userDiscussCreate = UserRequestDiscuss::create([
            'module' => 'attendance',
            'context_id' => $attendanceId,
            'user_id' =>  $userId,
            'message' => $message
        ]);
        //get attendance request
        $attendance = UserAttendanceRequests::where('id', $attendanceId)->first();
        //get user, who send attendance request
        $user = User::where('id', $attendance->user_id)->first();
        //get user who send message
        $sender = User::where('id', $userId)->first();
        //primary reporting manager
        $primaryReportingManagerId =  $user->userdetail->primary_reporting_manager_id;
        //notification title
        $title = "Attendance Discussion";
        //notification message
        $arr = [$title, $sender->name . ' ' . $sender->lastName . ' has send message.', 'home#attendance-' . $attendanceId];
        
        //get collaborators
        $collaborators = userRequestCollaborator::where('module', 'attendance')
            ->where('context_id', $id)
            ->pluck('user_id')->toArray();
        //primary reporting manager
        $primaryReportingManagerId = $user->userdetail->primary_reporting_manager_id;
        
        //secondary reporting managers 
        $secondaryManagers = $user->userdetail->secondaryManager;
        
        //get secondary managers in array
        $secondaryManagersArray =  $secondaryManagers->pluck('id')->unique()->toArray();
        if($userId == $attendance->user_id){
            if (!empty($collaborators)) {
                foreach ($collaborators as $collaborator) {
                    if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL || !in_array($collaborator,(array) $secondaryManagersArray)) {
                    $collaboratorUser = User::where('id', $collaborator)->first();
                    //send notification to all collaborators
                    $collaboratorUser->notify(new AppNotifications($arr));
                    }
                    
                }
            }
            //check if user has primary reporting manager
            if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL) {
                $primary_manger = User::where('id', $primaryReportingManagerId)->first();
                $primary_manger->notify(new AppNotifications($arr));
            } else {
 
                foreach ($secondaryManagers as $secondaryManager) {
                    if ($primaryReportingManagerId != $secondaryManager->id) {
                        $secondaryManager->notify(new AppNotifications($arr));
                    }
                }
            }
        
        
        }else if (!empty($collaborators) && in_array(auth()->user()->id, (array) $collaborators)) {
               foreach ($collaborators as $collaborator) {
                    $collaboratorUser = User::where('id', $collaborator)->first();
                    if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL || !in_array($collaborator, (array) $secondaryManagersArray)) {
                    //send notification to all collaborators
                    if(auth()->user()->id != $collaborator){
                    $collaboratorUser->notify(new AppNotifications($arr));
                    }
                    }
                }
                //check if user has primary reporting manager
                if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL) {
                    $primary_manger = User::where('id', $primaryReportingManagerId)->first();
                    $primary_manger->notify(new AppNotifications($arr));
                } else {
                    foreach ($secondaryManagers as $secondaryManager) {
                        if ($primaryReportingManagerId != $secondaryManager->id) {
                            $secondaryManager->notify(new AppNotifications($arr));
                        }
                    }
                }
                //send notification to user
                $user->notify(new AppNotifications($arr));
            
        }else{
            if (!empty($collaborators)) {
            foreach ($collaborators as $collaborator) {
                if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL || !in_array($collaborator, (array) $secondaryManagersArray)) {
                $collaboratorUser = User::where('id', $collaborator)->first();
                //send notification to all collaborators
                $collaboratorUser->notify(new AppNotifications($arr));
                }
                
            }
            }
            //send notification to user
            $user->notify(new AppNotifications($arr));
            if ($primaryReportingManagerId == 0 || $primaryReportingManagerId == NULL) {
            foreach ($secondaryManagers as $secondaryManager) {
                if ($sender->id != $secondaryManager->id) {
                    $secondaryManager->notify(new AppNotifications($arr));
                }
            }
        }
        }
        $attendanceDiscuss = UserRequestDiscuss::with(['user', 'user.userdetail'])
            ->where('module', 'attendance')
            ->where('context_id', $attendanceId)
            ->orderBy('created_at', 'DESC')
            ->take(1)
            ->first();
        $response = array(
            'attendanceDiscuss' => $attendanceDiscuss
        );
        return response()->json($response);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
    }
    /**
     * 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)
    {
    }
}