public function checkLimitEditAll(Request $request)
 {
     $performances_array = array();
     for ($i = 0; $i < count($request->all()); $i++) {
         $date_start = $request->input($i . '.date_start');
         $date_end = $request->input($i . '.date_end');
         // fetch all records with the same report details
         $performances = Performance::where('date_start', $date_start)->whereBetween('date_end', [$date_start, $date_end])->where('daily_work_hours', 'like', $request->input($i . '.daily_work_hours') . '%')->where('member_id', $request->input($i . '.member_id'))->orderBy('created_at', 'desc')->get();
         $hours_worked = 0;
         $performance = Performance::with(['member' => function ($query) {
             $query->withTrashed()->with('experiences');
         }])->with('project')->with(['target' => function ($query) {
             $query->withTrashed();
         }])->where('id', $request->input($i . '.id'))->first();
         // iterate every record to check the total of hours worked by the employee
         foreach ($performances as $key => $value) {
             $hours_worked += $value->hours_worked;
             if ($request->input($i . '.weekly_hours') == round($hours_worked, 1)) {
                 $performance->limit = $request->input($i . '.hours_worked');
             }
         }
         if (!$performance->limit) {
             $performance->limit = round($request->input($i . '.weekly_hours') - $hours_worked + $request->input($i . '.current_hours_worked'), 1);
         }
         $performance->date_started = $request->input($i . '.date_started');
         $performance->experience = $request->input($i . '.experience');
         $performance->include = true;
         $performance->weekly_hours = $request->input($i . '.weekly_hours');
         array_push($performances_array, $performance);
     }
     return response()->json($performances_array);
 }
 public function monthly()
 {
     $this->date_start = new Carbon('first Monday of this month');
     $this->date_end = new Carbon('last Monday of this month');
     $projects = Project::with('positions')->get();
     foreach ($projects as $project_key => $project) {
         $project->first_report = Report::where('project_id', $project->id)->whereBetween('date_start', [$this->date_start, $this->date_end])->first();
         if ($project->first_report) {
             $project->date_start = $this->date_start->toFormattedDateString();
             $project->date_end = $this->date_end->toFormattedDateString();
             $project->members = Experience::with(['member' => function ($query) {
                 $query->withTrashed();
             }])->where('project_id', $project->id)->get();
             $project->beginner_total_output = 0;
             $project->beginner_total_hours_worked = 0;
             $project->beginner_total_average_output = 0;
             $project->moderately_experienced_total_output = 0;
             $project->moderately_experienced_total_hours_worked = 0;
             $project->moderately_experienced_total_average_output = 0;
             $project->experienced_total_output = 0;
             $project->experienced_total_hours_worked = 0;
             $project->experienced_total_average_output = 0;
             foreach ($project->positions as $position_key => $position) {
                 $position->beginner = 0;
                 $position->moderately_experienced = 0;
                 $position->experienced = 0;
                 $position->beginner_total_output = 0;
                 $position->beginner_total_hours_worked = 0;
                 $position->beginner_total_average_output = 0;
                 $position->moderately_experienced_total_output = 0;
                 $position->moderately_experienced_total_hours_worked = 0;
                 $position->moderately_experienced_total_average_output = 0;
                 $position->experienced_total_output = 0;
                 $position->experienced_total_hours_worked = 0;
                 $position->experienced_total_average_output = 0;
             }
             foreach ($project->members as $member_key => $member) {
                 $member->positions = $project->positions;
                 $member->roles = 0;
                 $overall_monthly_productivity = 0;
                 $overall_monthly_quality = 0;
                 $overall_count = 0;
                 foreach ($member->positions as $position_key => $position) {
                     $performances = Performance::with('project', 'position')->with(['target' => function ($query) {
                         $query->withTrashed();
                     }])->with(['member' => function ($query) {
                         $query->withTrashed()->with(['experiences' => function ($query) {
                             $query->with('project');
                         }]);
                     }])->where('daily_work_hours', 'like', $project->first_report->daily_work_hours . '%')->where('position_id', $position->id)->where('member_id', $member->member_id)->whereBetween('date_start', [$this->date_start, $this->date_end])->get();
                     if (count($performances)) {
                         $member->roles++;
                         $position->total_hours_worked = 0;
                         $position->total_output = 0;
                         $position->total_output_error = 0;
                         $position->total_average_output = 0;
                         $position->monthly_productivity = 0;
                         $position->monthly_quality = 0;
                         foreach ($performances as $performance_key => $performance) {
                             $position->total_hours_worked += $performance->hours_worked;
                             $position->total_output += $performance->output;
                             $position->total_output_error += $performance->output_error;
                             if ($performance->target->experience == 'Beginner') {
                                 if ($performance_key === 0) {
                                     $project->positions[$position_key]->beginner += 1;
                                 }
                                 $project->positions[$position_key]->beginner_total_output += $performance->output;
                                 $project->positions[$position_key]->beginner_total_hours_worked += $performance->hours_worked;
                             } else {
                                 if ($performance->target->experience == 'Moderately Experienced') {
                                     if ($performance_key === 0) {
                                         $project->positions[$position_key]->moderately_experienced += 1;
                                     }
                                     $project->positions[$position_key]->moderately_experienced_total_output += $performance->output;
                                     $project->positions[$position_key]->moderately_experienced_total_hours_worked += $performance->hours_worked;
                                 } else {
                                     if ($performance->target->experience == 'Experienced') {
                                         if ($performance_key === 0) {
                                             $project->positions[$position_key]->experienced += 1;
                                         }
                                         $project->positions[$position_key]->experienced_total_output += $performance->output;
                                         $project->positions[$position_key]->experienced_total_hours_worked += $performance->hours_worked;
                                     }
                                 }
                             }
                         }
                         $project->positions[$position_key]->head_count = $project->positions[$position_key]->beginner + $project->positions[$position_key]->moderately_experienced + $project->positions[$position_key]->experienced;
                         $position->total_average_output = round($position->total_output / $position->total_hours_worked * $performances[0]->daily_work_hours, 2);
                         $position->monthly_productivity = round($position->total_average_output / $performances[0]->target->productivity * 100, 2);
                         $position->monthly_quality = round((1 - $position->total_output_error / $position->total_output) * 100, 2);
                         if ($position->monthly_productivity < 100 && $position->monthly_quality >= $performances[0]->target->quality) {
                             $position->quadrant = 'Quadrant 1';
                         } else {
                             if ($position->monthly_productivity >= 100 && $position->monthly_quality >= $performances[0]->target->quality) {
                                 $position->quadrant = 'Quadrant 2';
                             } else {
                                 if ($position->monthly_productivity >= 100 && $position->monthly_quality < $performances[0]->target->quality) {
                                     $position->quadrant = 'Quadrant 3';
                                 } else {
                                     if ($position->monthly_productivity < 100 && $position->monthly_quality < $performances[0]->target->quality) {
                                         $position->quadrant = 'Quadrant 4';
                                     }
                                 }
                             }
                         }
                         $overall_monthly_productivity += $position->monthly_productivity;
                         $overall_monthly_quality += $position->monthly_quality;
                         $overall_count++;
                     }
                 }
                 if ($overall_count) {
                     $member->average_productivity = $overall_monthly_productivity / $overall_count;
                     $member->average_quality = $overall_monthly_quality / $overall_count;
                 }
             }
             foreach ($project->positions as $position_key => $position) {
                 $position->beginner_total_average_output = $position->beginner_total_hours_worked ? round($position->beginner_total_output / $position->beginner_total_hours_worked * $project->first_report->daily_work_hours, 2) : 0;
                 $position->moderately_experienced_total_average_output = $position->moderately_experienced_total_hours_worked ? round($position->moderately_experienced_total_output / $position->moderately_experienced_total_hours_worked * $project->first_report->daily_work_hours, 2) : 0;
                 $position->experienced_total_average_output = $position->experienced_total_hours_worked ? round($position->experienced_total_output / $position->experienced_total_hours_worked * $project->first_report->daily_work_hours, 2) : 0;
                 $project->beginner_total_output += $position->beginner_total_output;
                 $project->beginner_total_hours_worked += $position->beginner_total_hours_worked;
                 $project->moderately_experienced_total_output += $position->moderately_experienced_total_output;
                 $project->moderately_experienced_total_hours_worked += $position->moderately_experienced_total_hours_worked;
                 $project->experienced_total_output += $position->experienced_total_output;
                 $project->experienced_total_hours_worked += $position->experienced_total_hours_worked;
             }
             $project->beginner_total_average_output = $project->beginner_total_hours_worked ? round($project->beginner_total_output / $project->beginner_total_hours_worked * $project->first_report->daily_work_hours, 2) : 0;
             $project->moderately_experienced_total_average_output = $project->moderately_experienced_total_hours_worked ? round($project->moderately_experienced_total_output / $project->moderately_experienced_total_hours_worked * $project->first_report->daily_work_hours, 2) : 0;
             $project->experienced_total_average_output = $project->experienced_total_hours_worked ? round($project->experienced_total_output / $project->experienced_total_hours_worked * $project->first_report->daily_work_hours, 2) : 0;
             $project->total_output = $project->beginner_total_output + $project->moderately_experienced_total_output + $project->experienced_total_output;
             $project->total_hours_worked = $project->beginner_total_hours_worked + $project->moderately_experienced_total_hours_worked + $project->experienced_total_hours_worked;
             $project->total_average_output = round($project->total_output / $project->total_hours_worked * $project->first_report->daily_work_hours, 2);
         }
     }
     return $projects;
 }