/**
  * 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++) {
         $this->validate($request, [$i . '.productivity' => 'required|numeric', $i . '.quality' => 'required|numeric', $i . '.experience' => 'required|string', $i . '.position_id' => 'required|numeric', $i . '.project_id' => 'required|numeric', $i . '.department_id' => 'required|numeric']);
         $target = Target::where('id', $request->input($i . '.id'))->delete();
         // $target->active = false;
         // $target->save();
         $new_target = new Target();
         // $new_target->value = $request->input($i.'.value');
         $new_target->productivity = $request->input($i . '.productivity');
         $new_target->quality = $request->input($i . '.quality');
         // $new_target->type = $request->input($i.'.type');
         $new_target->experience = $request->input($i . '.experience');
         $new_target->position_id = $request->input($i . '.position_id');
         $new_target->project_id = $request->input($i . '.project_id');
         $new_target->department_id = $request->input($i . '.department_id');
         // $new_target->active = true;
         $new_target->save();
     }
 }
 public function downloadWeeklyDepartment($department_id, $date_start, $date_end, $daily_work_hours)
 {
     $user = Auth::user();
     if ($user->department_id != $department_id || !$user) {
         abort(403);
     }
     $this->projects = DB::table('projects')->where('department_id', $department_id)->whereNull('deleted_at')->get();
     foreach ($this->projects as $project_key => $project) {
         $project->reports = Report::with(['performances' => function ($query) {
             $query->with(['member' => function ($query) {
                 $query->withTrashed();
             }])->with('position');
         }])->with(['project' => function ($query) {
             $query->with('positions');
         }])->where('project_id', $project->id)->where('date_start', Carbon::parse($date_start))->where('date_end', Carbon::parse($date_end))->where('daily_work_hours', 'like', $daily_work_hours . '%')->orderBy('date_start', 'desc')->get();
         if (count($project->reports)) {
             $project->department = DB::table('departments')->where('id', $department_id)->first();
             $project->department->beginner = array();
             $project->department->moderately_experienced = array();
             $project->department->experienced = array();
             $project->department->quality = array();
             foreach ($project->reports[0]->project->positions as $position_key => $position) {
                 // Beginner
                 $previous_beginner_target = Target::withTrashed()->where('position_id', $position->id)->where('experience', 'Beginner')->where('created_at', '<', $project->reports[0]->date_start)->orderBy('created_at', 'desc')->first();
                 $beginner_productivity = count($previous_beginner_target) ? $previous_beginner_target : Target::where('position_id', $position->id)->where('experience', 'Beginner')->first();
                 // Moderately Experienced
                 $previous_moderately_experienced_target = Target::withTrashed()->where('position_id', $position->id)->where('experience', 'Moderately Experienced')->where('created_at', '<', $project->reports[0]->date_start)->orderBy('created_at', 'desc')->first();
                 $moderately_experienced_productivity = count($previous_moderately_experienced_target) ? $previous_moderately_experienced_target : Target::where('position_id', $position->id)->where('experience', 'Moderately Experienced')->first();
                 // Experienced
                 $previous_experienced_target = Target::withTrashed()->where('position_id', $position->id)->where('experience', 'Experienced')->where('created_at', '<', $project->reports[0]->date_start)->orderBy('created_at', 'desc')->first();
                 $experienced_productivity = count($previous_experienced_target) ? $previous_experienced_target : Target::where('position_id', $position->id)->where('experience', 'Experienced')->first();
                 // Quality
                 $previous_experienced_target = Target::withTrashed()->where('position_id', $position->id)->where('experience', 'Experienced')->where('created_at', '<', $project->reports[0]->date_start)->orderBy('created_at', 'desc')->first();
                 $quality = count($previous_experienced_target) ? $previous_experienced_target : Target::where('position_id', $position->id)->where('experience', 'Experienced')->first();
                 array_push($project->department->beginner, $beginner_productivity);
                 array_push($project->department->moderately_experienced, $moderately_experienced_productivity);
                 array_push($project->department->experienced, $experienced_productivity);
                 array_push($project->department->quality, $quality);
             }
             foreach ($project->reports as $report_key => $report) {
                 foreach ($report->performances as $performance_key => $performance) {
                     $performance->experience = Experience::where('member_id', $performance->member_id)->where('project_id', $performance->project_id)->first()->experience;
                 }
             }
         }
     }
     // return response()->json('$this->projects');
     Excel::create('PQR ' . $this->projects[0]->department->name . ' Weekly Summary ' . Carbon::parse($date_start)->toFormattedDateString() . ' to ' . Carbon::parse($date_end)->toFormattedDateString(), function ($excel) {
         foreach ($this->projects as $project_key => $project) {
             $this->project = $project;
             if (count($this->project->reports)) {
                 $excel->sheet($this->project->name, function ($sheet) {
                     $sheet->loadView('excel.weekly')->with('project', $this->project);
                 });
             }
         }
     })->download('xls');
 }