public function store() { // $rules = array('unit-of-issue' => 'required|unique:metrics,name'); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { return Redirect::route('metric.index')->withErrors($validator); } else { // store $metric = new Metric(); $metric->name = Input::get('unit-of-issue'); $metric->description = Input::get('description'); try { $metric->save(); $url = Session::get('SOURCE_URL'); return Redirect::route('metric.index')->with('message', trans('messages.metric-succesfully-added')); } catch (QueryException $e) { Log::error($e); } } }
/** * calculate and save metrics on connect * @param object - user */ public static function firstTime($user) { $metrics = Metric::where('date', Carbon::now()->format('Y-m-d'))->where('user', $user->id)->first(); $customers = self::getCustomers($user); $subscriptions = self::getSubscriptions($user); if ($metrics) { // we already have data for the metric, update it $metrics->mrr = self::getMRR($user, $subscriptions, $metrics->mrr); $metrics->au = self::getAU($user, $customers, $metrics->au); $metrics->arr = $metrics->mrr * 12; $metrics->arpu = $metrics->au != 0 ? round($metrics->mrr / $metrics->au) : null; } else { // we dont have any data yet $metrics = new Metric(); $metrics->user = $user->id; $metrics->date = Carbon::now()->format('Y-m-d'); $metrics->mrr = self::getMRR($user, $subscriptions); $metrics->au = self::getAU($user, $customers); $metrics->arr = $metrics->mrr * 12; $metrics->arpu = $metrics->au != 0 ? round($metrics->mrr / $metrics->au) : null; $metrics->uc = null; $metrics->cancellations = 0; $metrics->monthlyCancellations = null; } $metrics->save(); }