/** * Return the price for the project * This method needs to be called getFieldAttribute * @return string */ public function getPriceAttribute() { // @TODO Try to abstract this logic, since you are using it in multiple places // $rate = $this->rate_per_hour; // $time = $this->total_time; // $price = 0; // // if ($time['seconds'] > 30) { // $time['minutes'] = $time['minutes'] + 1; // } // // $price+= $rate * $time['hours']; // $price+= $rate / 60 * $time['minutes']; //Get the ids of the timers that belong to the project $timer_ids = Timer::where('project_id', $this->id)->lists('id'); //Get the sum of all the timers that belong to the project $price = (double) Timer::whereIn('id', $timer_ids)->sum('price'); /** * @VP: * Why is sum returning a string here? I want it to be a decimal. */ return $price; }
/** * Stop the timer (update it). * Return all the projects, * as well as the project that is currently displaying in the project popup * @param Request $request * @return mixed */ public function stopProjectTimer(Request $request) { // Fetch the required data $project = Project::find($request->get('project_id')); $last_timer_id = Timer::where('project_id', $project->id)->max('id'); $timer = Timer::find($last_timer_id); // Update the data (Price will be zero if time is less than 30 seconds) $timer->finish = Carbon::now()->toDateTimeString(); $timer->save(); $timer->calculatePrice(); $message = Auth::user()->name . ' has stopped the timer on the project ' . $project->description . '.'; //Create a notification in the database for the payer, in case they are not currently logged in $notification = new Notification(['message' => $message]); $notification->user()->associate($project->payer->id); $notification->save(); //Pusher $pusher = new Pusher(env('PUSHER_PUBLIC_KEY'), env('PUSHER_SECRET_KEY'), env('PUSHER_APP_ID')); $data = ['payer_id' => $project->payer_id, 'notification' => $notification]; $pusher->trigger('channel', 'stopTimer', $data); // Create the fractal manager // @TODO: You could extract the next two lines to a ServiceProvider $fractal = new Manager(); $fractal->setSerializer(new ArraySerializer()); // Create an item with fractal $resource = new Item($timer, new StopProjectTimerTransformer()); // $this->createItem(Model, transofrmer) // $this->createCollection(Model, transofrmer) // Send a response return response()->json($fractal->createData($resource)->toArray()); // return $this->responseOk($timer); }