/** * Run the database seeds. * * @return void */ public function run() { User::truncate(); DB::table('payee_payer')->truncate(); $jenny = User::create(['name' => 'Jenny', 'email' => '*****@*****.**', 'password' => bcrypt('abcdefg')]); $jane = User::create(['name' => 'Jane', 'email' => '*****@*****.**', 'password' => bcrypt('abcdefg')]); $bob = User::create(['name' => 'Bob', 'email' => '*****@*****.**', 'password' => bcrypt('abcdefg')]); $john = User::create(['name' => 'John', 'email' => '*****@*****.**', 'password' => bcrypt('abcdefg')]); /** * Create payers for John */ $payee_john = Payee::find($john->id); $payee_john->payers()->attach($jenny->id); $payee_john->payers()->attach($jane->id); $payee_john->payers()->attach($bob->id); $payee_john->save(); /** * Create payers for Bob */ $payee_bob = Payee::find($bob->id); $payee_bob->payers()->attach($jenny->id); $payee_bob->payers()->attach($john->id); $payee_bob->save(); /** * Create payers for Jenny */ $payee_bob = Payee::find($bob->id); $payee_bob->payers()->attach($john->id); $payee_bob->save(); }
/** * * @return \Illuminate\View\View */ public function payee() { //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'); $payee = Payee::find(Auth::user()->id); JavaScript::put(['projects' => $payee->confirmedProjects->toArray(), 'payers' => $payee->payers->toArray(), 'me' => Auth::user(), 'notifications' => $payee->notifications, 'declined_projects' => $payee->declinedProjects->toArray(), 'pusher_public_key' => $pusher_public_key]); return view('payee'); }
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; }
public function payers(Request $request) { $typing = '%' . $request->get('typing') . '%'; $payee = Payee::find(Auth::user()->id); if ($typing === '%%') { //The input has been focused but nothing has been typed. Return all the payers. return $payee->payers; } return $payee->payers()->where('name', 'LIKE', $typing)->get(); }
/** * Get the total amount the user owes the current user * @return mixed */ public function getOwedToUserAttribute() { $payee = Payee::find(Auth::user()->id); //Find the projects belonging to the current user and $this user $projects_with_payer = Project::where('payee_id', $payee->id)->where('payer_id', $this->id)->lists('id'); //Find the timers belonging to those projects, //but only those that have not been paid for $timers_with_payer = Timer::whereIn('project_id', $projects_with_payer)->where('paid', 0)->lists('id'); //Find the amount owed $owed = Timer::whereIn('id', $timers_with_payer)->sum('price'); $owed = number_format($owed, 2); return $owed; }
/** * Insert a new project * Return projects * @param Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function store(CreateProjectRequest $request) { $payer_email = $request->get('payer_email'); //Check if the email is for a new payer, rather than a previous payer of the payee. //If it is, add the row to the payee_payer pivot table before creating the project. //Todo: If it is a new payer, do the appropriate validation errors //todo: (different for if the new payer email field is blank vs the previous payer input) if ($request->get('new_payer')) { Payee::addPayer($payer_email); } //Create the project return $this->projectsRepository->createProject($payer_email, $request->get('description'), $request->get('rate')); }
/** * 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(); }