/** * @param int $id The equipment you want to check out on * @return \Illuminate\Http\RedirectResponse|string */ public function checkout($id) { // FindOrFail because we don't want anyone checking out on nonexistant equipment $equipment = Equipment::findOrFail($id); $user = \Auth::id(); // Get today's range $now = new Carbon('now'); $start = clone $now->hour(0)->minute(0)->second(0); $end = clone $now->hour(23)->minute(59)->second(59); // Not checked out, checked in, and checkin was today $checkin = Checkin::where('user_id', $user)->where('equipment_id', $id)->whereNotNull('checkin')->whereNull('checkout')->whereBetween('checkin', [$start, $end])->first(); // No checkin? No dice. if ($checkin === null) { return "Error: You're not checked in."; } // All is well, write checkout to database $checkin->update(['checkout' => new \DateTime()]); return redirect()->back(); // "You have been checked out"; }
/** * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function stats() { $user = \Auth::id(); $now = new Carbon(); $currentFirstDay = clone $now; $currentFirstDay = $currentFirstDay->firstOfMonth(); $currentLastDay = clone $now; $currentLastDay = $currentLastDay->lastOfMonth(); $thisMonth = Checkin::where('user_id', $user)->whereBetween('checkin', [$currentFirstDay, $currentLastDay])->whereBetween('checkout', [$currentFirstDay, $currentLastDay])->orderBy('checkout')->get(); $stats['current']['total'] = $thisMonth->sum('burned'); $stats['current']['min'] = $thisMonth->min('burned'); $stats['current']['max'] = $thisMonth->max('burned'); $previousLastDay = clone $currentFirstDay; $previousLastDay = $previousLastDay->modify('-1 day'); $previousFirstDay = clone $previousLastDay; $previousFirstDay = $previousFirstDay->firstOfMonth(); $previousMonth = Checkin::where('user_id', $user)->whereBetween('checkin', [$previousFirstDay, $previousLastDay])->whereBetween('checkout', [$previousFirstDay, $previousLastDay])->orderBy('checkout')->get(); $stats['previous']['total'] = $previousMonth->sum('burned'); $stats['previous']['min'] = $previousMonth->min('burned'); $stats['previous']['max'] = $previousMonth->max('burned'); if ($stats['current']['total'] < $stats['previous']['total']) { $stats['difference'] = $stats['previous']['total'] - $stats['current']['total']; $stats['suggestion'] = Equipment::orderBy('calories_pm', 'desc')->first(); } return view('equipment.progress', $stats); }