/**
  * Fulfills a scheduled pledge
  * @param Request $r
  * @param Project $project
  * @return (redirect()) to appropriate area with messages
  */
 function sendPledge(Request $r, Project $project)
 {
     $user = $r->user();
     $pledge = UserPledge::where('project_id', $project->id)->where('user_id', $user->id)->first();
     if ($pledge === null) {
         return redirect('account')->withErrors("You don't have a pledge for this project!");
     }
     if ($pledge->last_pledge->gte(Carbon::now()->subDays(31))) {
         return redirect('account')->withErrors("You aren't due a pledge yet. Come back later.");
     }
     $amount = $pledge->amount;
     $type = $pledge->type;
     $_this = $this;
     try {
         app('db')->transaction(function () use($amount, $user, $type, $project, $_this) {
             $_this->updateBalances($user, $amount, $project);
             $_this->storePledge($amount, $type, $project, $user);
         });
         return redirect('/projects/' . $project->id)->with('status', 'Thank you for your pledge. It has been taken from your balance');
     } catch (BalanceException $e) {
         return redirect('account')->withErrors('Not enough balance for this pledge. You need at least ' . $amount);
     } catch (\Exception $e) {
         return redirect('account')->withErrors('There was an error processing your pledge. Your balance should not be affected.');
     }
 }
示例#2
0
 function getMonthlyUsersAttribute()
 {
     $_this = $this;
     $cached = app('cache')->remember('monthly_users:' . $this->id, 3, function () use($_this) {
         return UserPledge::where('project_id', $_this->id)->count();
     });
     return $cached;
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     // for now, we only support monthly
     $pledges = UserPledge::where('frequency', 'monthly')->where('last_pledge', '<', Carbon::now()->subDay(31))->get();
     foreach ($pledges as $p) {
         // if we never sent an email before, the default is yesterday
         if ($p->last_email->year == -1) {
             $p->last_email = Carbon::now()->subDay();
             $p->save();
         }
         // only send emails every 3 days
         if ($p->last_email->gte(Carbon::now()->subDays(3))) {
             return;
         }
         $user = $p->user;
         // 7 days ago, we remove the pledge
         if ($p->last_pledge->lte(Carbon::now()->subDays(38))) {
             Mail::send('emails.next_pledge_7', ['user' => $p->user, 'pledge' => $p], function ($m) use($user) {
                 $m->from(env('FROM_EMAIL'), env('FROM_NAME'));
                 $m->to($user->email, $user->name)->subject("INACTIVITY - We've removed your pledge");
             });
             $p->delete();
             return;
         }
         // 4 days ago, another reminder
         if ($p->last_pledge->lte(Carbon::now()->subDays(35))) {
             Mail::send('emails.next_pledge_4', ['user' => $p->user, 'pledge' => $p], function ($m) use($user) {
                 $m->from(env('FROM_EMAIL'), env('FROM_NAME'));
                 $m->to($user->email, $user->name)->subject("You still haven't sent your PLEDGE");
             });
             $p->last_email = Carbon::now();
             $p->save();
             return;
         }
         // last email sent more than a day ago
         if ($p->last_pledge->lte(Carbon::now()->subDays(32))) {
             Mail::send('emails.next_pledge_1', ['user' => $p->user, 'pledge' => $p], function ($m) use($user) {
                 $m->from(env('FROM_EMAIL'), env('FROM_NAME'));
                 $m->to($user->email, $user->name)->subject("It's time for your next PLEDGE");
             });
             $p->last_email = Carbon::now();
             $p->save();
             return;
         }
     }
 }