/**
  * 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();
         }
     }
 }
 public function reportDelete(Request $request, $id)
 {
     $activity_type = ActivityType::where('action', 'delete')->first();
     $activity = new Activity();
     $activity->report_id = $id;
     $activity->user_id = $request->user()->id;
     $activity->activity_type_id = $activity_type->id;
     $activity->save();
     $admin = User::where('email', '*****@*****.**')->first();
     $notification = new Notification();
     $notification->receiver_user_id = $admin->id;
     $notification->sender_user_id = $request->user()->id;
     $notification->subscriber = 'admin';
     $notification->message = 'deletes ';
     $notification->state = 'main.activity';
     $notification->event_id = $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();
     event(new ReportSubmittedBroadCast($notify));
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $create_report = false;
     for ($i = 0; $i < count($request->all()); $i++) {
         if ($request->input($i . '.include')) {
             $this->validate($request, [$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 (!$create_report) {
                 $admin = User::where('role', 'admin')->first();
                 $report = new Report();
                 $report->user_id = $request->user()->id;
                 $report->department_id = $request->user()->department_id;
                 $report->project_id = $request->input($i . '.project_id');
                 $report->daily_work_hours = $request->input($i . '.daily_work_hours');
                 $report->date_start = $request->input($i . '.date_start');
                 $report->date_end = $request->input($i . '.date_end');
                 $report->save();
                 // create a notification
                 $notification = new Notification();
                 $notification->message = 'submitted 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', 'create')->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;
             }
             // $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 = new Performance();
             $performance->report_id = $report->id;
             $performance->member_id = $request->input($i . '.member.id');
             $performance->position_id = $request->input($i . '.position_id');
             $performance->department_id = $request->user()->department_id;
             $performance->project_id = $request->input($i . '.project_id');
             $performance->target_id = $target->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);
             // Quadrant
             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->save();
             // fetch target
             // $productivity = Target::where('type', 'Productivity')->where('position_id', $request->input($i.'.position_id'))->where('experience', $request->input($i.'.experience'))->where('created_at', '<=', $request->input($i.'.date_end'))->orderBy('created_at', 'desc')->first();
             // if(!$productivity)
             // {
             //      $productivity = Target::where('type', 'Productivity')->where('position_id', $request->input($i.'.position_id'))->where('experience', $request->input($i.'.experience'))->first();
             // }
             // $quality = Target::where('type', 'Quality')->where('position_id', $request->input($i.'.position_id'))->where('experience', $request->input($i.'.experience'))->where('created_at', '<=', $request->input($i.'.date_end'))->orderBy('created_at', 'desc')->first();
             // if(!$quality)
             // {
             //      $quality = Target::where('type', 'Quality')->where('position_id', $request->input($i.'.position_id'))->where('experience', $request->input($i.'.experience'))->first();
             // }
             // $performance->productivity_id = $productivity->id;
             // $performance->quality_id = $quality->id;
         }
     }
 }