/** * * @return \Illuminate\View\View */ public function payer() { //I got an error with the package if //I didn't put $pusher_public_key into a variable first $pusher_public_key = env('PUSHER_PUBLIC_KEY'); $payer = Payer::find(Auth::user()->id); JavaScript::put(['projects' => $payer->confirmedProjects->toArray(), 'payees' => $payer->payees->toArray(), 'me' => Auth::user(), 'notifications' => $payer->notifications, 'project_requests' => $payer->projectRequests, 'pusher_public_key' => $pusher_public_key]); return view('payer'); }
public static function addPayer($payer_email) { // @TODO This step could be improved to remove the double-query effect $user = Auth::user(); $payee = Payee::findOrFail($user->id); $payer = Payer::whereEmail($payer_email)->firstOrFail(); $payee->payers()->attach($payer->id); $payee->save(); return $payee->payers; }
/** * Run the database seeds. * * @return void */ public function run() { Project::truncate(); Timer::truncate(); $faker = Faker::create(); $john = User::where('name', 'John')->first(); $payee_john = Payee::find($john->id); $payer_john = Payer::find($john->id); $jenny = User::where('name', 'Jenny')->first(); $jane = User::where('name', 'Jane')->first(); $bob = User::where('name', 'Bob')->first(); $payee_bob = Payee::find($bob->id); /** * John is payee */ // dd($faker->randomElement([1,2,3])); // dd($faker->randomElement($payee_john->payers()->lists('id'))); // dd($payee_john->payers()->lists('id')->all()); foreach (range(0, 2) as $index) { $project = Project::create(['payee_id' => $john->id, 'payer_id' => $faker->randomElement($payee_john->payers()->lists('id')->all()), 'description' => $faker->word, 'rate_per_hour' => 40, 'status' => 'confirmed']); $this->createTimersForProject($project); } //Create a project with Jenny as payer $project = Project::create(['payee_id' => $john->id, 'payer_id' => $jenny->id, 'description' => $faker->word, 'rate_per_hour' => 40, 'status' => 'confirmed']); $this->createTimersForProject($project); /** * John is payer */ foreach (range(0, 2) as $index) { $project = Project::create(['payee_id' => $faker->randomElement($payer_john->payees()->lists('id')->all()), 'payer_id' => $john->id, 'description' => $faker->word, 'rate_per_hour' => 1, 'status' => 'confirmed']); $this->createTimersForProject($project); } /** * Bob is payee */ foreach (range(0, 4) as $index) { $project = Project::create(['payee_id' => $bob->id, 'payer_id' => $jenny->id, 'description' => $faker->word, 'rate_per_hour' => 40, 'status' => 'confirmed']); $this->createTimersForProject($project); } /** * Jenny is payee */ foreach (range(0, 2) as $index) { $project = Project::create(['payee_id' => $jenny->id, 'payer_id' => $john->id, 'description' => $faker->word, 'rate_per_hour' => 10, 'status' => 'confirmed']); $this->createTimersForProject($project); } }
/** * 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 }
/** * Remove a relationship between a payee and a payer, * and all associated projects * @TODO Should be a DELETE method to /users/{user}/payers/{payer} * @param Request $request * @return mixed */ public function removePayer(Request $request) { $payer = Payer::findOrFail($request->get('payer_id')); $payee = Payee::find(Auth::user()->id); //Remove the relationship between the payee and the payer //from the payee_payer table $payee->payers()->detach($payer->id); $payee->save(); //Remove projects the payee had with the payer /** * @VP: * Is there some way I could do this instead in the migrations file, * like with cascade on delete? * @JS: * Nope. */ $payee->projects()->where('payer_id', $payer->id)->delete(); }
/** * 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; }