public function performanceEdit(Request $request, $reportID) { $create_notification = false; for ($i = 0; $i < count($request->all()); $i++) { if ($request->input($i . '.include')) { $this->validate($request, [$i . '.id' => 'required|numeric', $i . '.position_id' => 'required|numeric', $i . '.department_id' => 'required|numeric', $i . '.project_id' => 'required|numeric', $i . '.output' => 'required|numeric', $i . '.target_id' => 'required', $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', $i . '.message' => 'required']); if (!$create_notification) { $admin = User::where('email', '*****@*****.**')->first(); $approval = new Approval(); $approval->action = 'update'; $approval->report_id = $reportID; $approval->status = 'pending'; $approval->save(); $report = Report::where('id', $request->input($i . '.report_id'))->first(); $report->user_id = $request->user()->id; $report->save(); // create a notification $notification = new Notification(); $notification->message = 'updates '; $notification->receiver_user_id = $admin->id; $notification->sender_user_id = $request->user()->id; $notification->subscriber = 'admin'; $notification->state = 'main.approvals'; $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(); event(new ReportSubmittedBroadCast($notify)); // report $create_notification = true; } $target = Target::withTrashed()->where('id', $request->input($i . '.target_id'))->first(); $performance_approval = new PerformanceApproval(); $performance_approval->approval_id = $approval->id; $performance_approval->report_id = $reportID; $performance_approval->message = $request->input($i . '.message'); // $performance_approval->result_id = $request->input($i.'.result_id'); $performance_approval->performance_id = $request->input($i . '.id'); $performance_approval->member_id = $request->input($i . '.member_id'); $performance_approval->position_id = $request->input($i . '.position_id'); $performance_approval->department_id = $request->input($i . '.department_id'); $performance_approval->project_id = $request->input($i . '.project_id'); $performance_approval->target_id = $target->id; $performance_approval->date_start = Carbon::parse($request->input($i . '.date_start')); $performance_approval->date_end = Carbon::parse($request->input($i . '.date_end')); $performance_approval->daily_work_hours = $request->input($i . '.daily_work_hours'); $performance_approval->output = $request->input($i . '.output'); $performance_approval->hours_worked = $request->input($i . '.hours_worked'); $performance_approval->output_error = $request->input($i . '.output_error'); // Round((Output / Hours Worked) * Daily Work Hours) // store the rounded value $performance_approval->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_approval->productivity = round($performance_approval->average_output / $target->productivity * 100, 1); // 1 - output w/error / output * 100 to convert to percentage $performance_approval->quality = round((1 - $performance_approval->output_error / $performance_approval->output) * 100, 1); // Quadrant if ($performance_approval->productivity < 100 && $performance_approval->quality >= $target->quality) { $performance_approval->quadrant = 'Quadrant 1'; } else { if ($performance_approval->productivity >= 100 && $performance_approval->quality >= $target->quality) { $performance_approval->quadrant = 'Quadrant 2'; } else { if ($performance_approval->productivity >= 100 && $performance_approval->quality < $target->quality) { $performance_approval->quadrant = 'Quadrant 3'; } else { if ($performance_approval->productivity < 100 && $performance_approval->quality < $target->quality) { $performance_approval->quadrant = 'Quadrant 4'; } } } } // save performance_approval request to database $performance_approval->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 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'); }