/** * Mark all timers that belong to the user (payee), * and are with a certain payer, as paid * * WARNING: Be careful, method not Restful! (Should be PUT /timers/{timer}) * * @param Request $request */ public function markAsPaid(Request $request) { $payer = Payer::findOrFail($request->get('payer_id')); $payee = Payee::find(Auth::user()->id); $project_ids = $payee->projects()->where('payer_id', $payer->id)->lists('id'); Timer::whereIn('project_id', $project_ids)->where('paid', 0)->update(['paid' => 1, 'time_of_payment' => Carbon::now()]); $message = Auth::user()->name . ' has marked all timers as paid.'; //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($payer->id); $notification->save(); //Pusher $pusher = new Pusher(env('PUSHER_PUBLIC_KEY'), env('PUSHER_SECRET_KEY'), env('PUSHER_APP_ID')); $data = ['payer_id' => $payer->id, 'payee_id' => $payee->id, 'notification' => $notification]; $pusher->trigger('channel', 'markAsPaid', $data); // @TODO Return collection of timers that have been modified }
/** * 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; }
/** * Get the total amount the current user owes the user * @return mixed */ public function getOwedByUserAttribute() { $payer = Payer::find(Auth::user()->id); //Find the projects belonging to the current user and $this user $projects_with_payee = Project::where('payer_id', $payer->id)->where('payee_id', $this->id)->lists('id'); //Find the timers belonging to those projects, //but only those that have not been paid for $timers_with_payee = Timer::whereIn('project_id', $projects_with_payee)->where('paid', 0)->lists('id'); //Find the amount owed $owed = Timer::whereIn('id', $timers_with_payee)->sum('price'); return (double) $owed; }