/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $notify_report = false;
     for ($i = 0; $i < count($request->all()); $i++) {
         if ($request->input($i . '.include')) {
             $this->validate($request, [$i . '.id' => 'required', $i . '.member.id' => 'required|numeric', $i . '.position_id' => 'required|numeric', $i . '.project_id' => 'required|numeric', $i . '.target_id' => 'required|numeric', $i . '.output' => 'required|numeric', $i . '.date_start' => 'required|date', $i . '.date_end' => 'required|date', $i . '.hours_worked' => 'required|numeric', $i . '.daily_work_hours' => 'required|numeric', $i . '.output_error' => 'required|numeric']);
             // check if a report is already created
             if (!$notify_report) {
                 $admin = User::where('role', 'admin')->first();
                 $report = Report::where('id', $request->input($i . '.report_id'))->first();
                 // create a notification
                 $notification = new Notification();
                 $notification->message = 'updated a ';
                 $notification->sender_user_id = $request->user()->id;
                 $notification->receiver_user_id = $admin->id;
                 $notification->subscriber = 'admin';
                 $notification->state = 'main.weekly-report';
                 $notification->event_id = $report->id;
                 $notification->event_id_type = 'report_id';
                 $notification->seen = false;
                 $notification->save();
                 $notify = DB::table('reports')->join('users', 'users.id', '=', 'reports.user_id')->join('projects', 'projects.id', '=', 'reports.project_id')->join('notifications', 'notifications.event_id', '=', 'reports.id')->select('reports.*', 'users.*', DB::raw('LEFT(users.first_name, 1) as first_letter'), 'projects.*', 'notifications.*')->where('notifications.id', $notification->id)->first();
                 // foreach ($query as $key => $value) {
                 //     $notify = $value;
                 // }
                 event(new ReportSubmittedBroadCast($notify));
                 $activity_type = ActivityType::where('action', 'update')->first();
                 $activity = new Activity();
                 $activity->report_id = $report->id;
                 $activity->user_id = $request->user()->id;
                 $activity->activity_type_id = $activity_type->id;
                 $activity->save();
                 // report
                 $create_report = true;
             }
             $old_performance = Performance::where('id', $request->input($i . '.id'))->first();
             // record history of the performance
             $performance_history = new PerformanceHistory();
             $performance_history->activity_id = $activity->id;
             $performance_history->performance_id = $old_performance->id;
             $performance_history->report_id = $old_performance->report_id;
             $performance_history->member_id = $old_performance->member_id;
             $performance_history->position_id = $old_performance->position_id;
             $performance_history->department_id = $old_performance->department_id;
             $performance_history->project_id = $old_performance->project_id;
             $performance_history->target_id = $old_performance->target_id;
             $performance_history->date_start = $old_performance->date_start;
             $performance_history->date_end = $old_performance->date_end;
             $performance_history->daily_work_hours = $old_performance->daily_work_hours;
             $performance_history->output = $old_performance->output;
             $performance_history->hours_worked = $old_performance->hours_worked;
             $performance_history->output_error = $old_performance->output_error;
             $performance_history->average_output = $old_performance->average_output;
             $performance_history->productivity = $old_performance->productivity;
             $performance_history->quality = $old_performance->quality;
             $performance_history->quadrant = $old_performance->quadrant;
             $performance_history->save();
         }
     }
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     for ($i = 0; $i < count($request->all()); $i++) {
         if ($request->input($i . '.include')) {
             $this->validate($request, [$i . '.id' => 'required|numeric', $i . '.position_id' => 'required', $i . '.target_id' => 'required|numeric', $i . '.output' => 'required|numeric', $i . '.hours_worked' => 'required|numeric', $i . '.daily_work_hours' => 'required|numeric', $i . '.output_error' => 'required|numeric']);
             // $target = Target::where('position_id', $request->input($i.'.position_id'))->where('experience', $request->input($i.'.experience'))->first();
             $target = Target::withTrashed()->where('id', $request->input($i . '.target_id'))->first();
             $performance = Performance::where('id', $request->input($i . '.id'))->first();
             $performance->position_id = $request->input($i . '.position_id');
             // $performance->project_id = $request->input($i.'.project_id');
             $performance->output = round($request->input($i . '.output'), 2);
             // $performance->date_start = $request->input($i.'.date_start');
             // $performance->date_end = $request->input($i.'.date_end');
             $performance->hours_worked = round($request->input($i . '.hours_worked'), 2);
             // $performance->daily_work_hours = $request->input($i.'.daily_work_hours');
             $performance->output_error = round($request->input($i . '.output_error'), 2);
             // Round((Output / Hours Worked) * Daily Work Hours)
             // store the rounded value
             $performance->average_output = round($request->input($i . '.output') / $request->input($i . '.hours_worked') * $request->input($i . '.daily_work_hours'), 2);
             // average output / target output * 100 to convert to percentage
             $performance->productivity = round($performance->average_output / $target->productivity * 100, 2);
             // 1 - output w/error / output * 100 to convert to percentage
             $performance->quality = round((1 - $performance->output_error / $performance->output) * 100, 2);
             if ($performance->productivity < 100 && $performance->quality >= $target->quality) {
                 $performance->quadrant = 'Quadrant 1';
             } else {
                 if ($performance->productivity >= 100 && $performance->quality >= $target->quality) {
                     $performance->quadrant = 'Quadrant 2';
                 } else {
                     if ($performance->productivity >= 100 && $performance->quality < $target->quality) {
                         $performance->quadrant = 'Quadrant 3';
                     } else {
                         if ($performance->productivity < 100 && $performance->quality < $target->quality) {
                             $performance->quadrant = 'Quadrant 4';
                         }
                     }
                 }
             }
             // $performance->type = "weekly";
             // $performance->performance_id = $request->input($i.'.performance_id');
             // save performance to database
             $performance->save();
         }
     }
 }
 public function approve(Request $request)
 {
     $create_notification = false;
     $pending_count = count($request->all());
     for ($i = 0; $i < count($request->all()); $i++) {
         if ($request->input($i . '.include')) {
             $pending_count--;
             $this->validate($request, [$i . '.approval_id' => 'required|numeric', $i . '.performance_approval_id' => 'required|numeric', $i . '.performance_id' => 'required|numeric']);
             if (!$create_notification) {
                 $admin = User::where('email', '*****@*****.**')->first();
                 $report = Report::where('id', $request->input($i . '.report_id'))->first();
                 $notification = new Notification();
                 $notification->receiver_user_id = $report->user_id;
                 $notification->sender_user_id = $admin->id;
                 $notification->subscriber = 'team-leader';
                 $notification->message = 'approved changes on';
                 $notification->state = 'main.approvals';
                 // $notification->event_id = $request->input($i.'.approval_id');
                 // $notification->event_id_type = 'approval_id';
                 $notification->event_id = $report->id;
                 $notification->event_id_type = 'report_id';
                 $notification->seen = false;
                 $notification->save();
                 $notify = DB::table('notifications')->join('approvals', 'approvals.report_id', '=', 'notifications.event_id')->join('reports', 'reports.id', '=', 'approvals.report_id')->join('projects', 'projects.id', '=', 'reports.project_id')->join('users', 'users.id', '=', 'notifications.sender_user_id')->select('*', DB::raw('LEFT(users.first_name, 1) as first_letter'))->where('notifications.id', $notification->id)->first();
                 event(new ApprovalNotificationBroadCast($notify));
                 // report
                 $create_notification = true;
             }
             $old_performance = Performance::where('id', $request->input($i . '.performance_id'))->first();
             // record history of the performance
             $performance_history = new PerformanceHistory();
             $performance_history->performance_id = $old_performance->id;
             $performance_history->report_id = $old_performance->report_id;
             $performance_history->member_id = $old_performance->member_id;
             $performance_history->position_id = $old_performance->position_id;
             $performance_history->department_id = $old_performance->department_id;
             $performance_history->project_id = $old_performance->project_id;
             $performance_history->target_id = $old_performance->target_id;
             $performance_history->date_start = $old_performance->date_start;
             $performance_history->date_end = $old_performance->date_end;
             $performance_history->daily_work_hours = $old_performance->daily_work_hours;
             $performance_history->output = $old_performance->output;
             $performance_history->hours_worked = $old_performance->hours_worked;
             $performance_history->output_error = $old_performance->output_error;
             $performance_history->average_output = $old_performance->average_output;
             $performance_history->productivity = $old_performance->productivity;
             $performance_history->quality = $old_performance->quality;
             $performance_history->quadrant = $old_performance->quadrant;
             $performance_history->save();
             $this->performance_approval_approved = PerformanceApproval::where('id', $request->input($i . '.performance_approval_id'))->first();
             $this->performance_approval_approved->status = 'approved';
             $this->performance_approval_approved->save();
             // fetch the changes
             // $performance_approval = DB::table('performance_approvals')
             //     ->join('members', 'members.id', '=', 'performance_approvals.member_id')
             //     ->where('performance_approvals.id', $request->input($i.'.performance_approval_id'))
             //     ->first();
             $performance_approval = PerformanceApproval::with(['member' => function ($query) {
                 $query->with(['experiences' => function ($query) {
                     $query->where('project_id', $this->performance_approval_approved->project_id);
                 }]);
             }])->where('id', $request->input($i . '.performance_approval_id'))->first();
             $performance = Performance::where('id', $request->input($i . '.performance_id'))->first();
             // apply changes
             $performance->position_id = $performance_approval->position_id;
             $performance->hours_worked = $performance_approval->hours_worked;
             $performance->output = $performance_approval->output;
             $performance->output_error = $performance_approval->output_error;
             // Round((Output / Hours Worked) * Daily Work Hours)
             $performance->average_output = round($performance_approval->output / $performance_approval->hours_worked * $performance_approval->daily_work_hours, 2);
             // fetch target
             $target = Target::withTrashed()->where('id', $performance_approval->target_id)->first();
             // recompute results
             // $result = Result::where('id', $performance_approval->result_id)->first();
             // average output / target output * 100 to convert to percentage
             $performance->productivity = round($performance_approval->average_output / $target->productivity * 100, 1);
             // 1 - output w/error / output * 100 to convert to percentage
             $performance->quality = round((1 - $performance_approval->output_error / $performance_approval->output) * 100, 1);
             $performance->save();
         }
     }
     if (!$pending_count) {
         $approval = Approval::where('id', $request->input('0.approval_id'))->first();
         $approval->status = 'done';
         $approval->save();
     }
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     Report::where('id', $id)->delete();
     Performance::where('report_id', $id)->delete();
     // Result::where('report_id', $id)->delete();
     DB::table('approvals')->where('report_id')->delete();
     DB::table('performance_approvals')->where('report_id')->delete();
     DB::table('performance_histories')->where('report_id')->delete();
 }