Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
First new display of piggy bank reminders.
James Cole3f716cc369a0
diff --git a/app/Http/Middleware/Reminders.php b/app/Http/Middleware/Reminders.phpindex 556cc8f6b7..d63b3109de 100644--- a/app/Http/Middleware/Reminders.php+++ b/app/Http/Middleware/Reminders.php@@ -1,10 +1,4 @@<?php-/**- * Created by PhpStorm.- * User: sander- * Date: 06/03/15- * Time: 15:04- */namespace FireflyIII\Http\Middleware;@@ -14,8 +8,7 @@ use Closure;use FireflyIII\Models\PiggyBank;use FireflyIII\Models\Reminder;use Illuminate\Contracts\Auth\Guard;-use Log;-use Navigation;+use View;/*** Class Reminders@@ -61,69 +54,28 @@ class Reminders/** @var PiggyBank $piggyBank */foreach ($piggyBanks as $piggyBank) {- $startDate = is_null($piggyBank->startdate) ? 'null' : $piggyBank->startdate->format('d M Y');- $targetDate = is_null($piggyBank->targetdate) ? 'null' : $piggyBank->targetdate->format('d M Y');- Log::debug('PiggyBank: #' . $piggyBank->id . ', name: ' . $piggyBank->name);- Log::debug('Startdate: ' . $startDate . ', target date: ' . $targetDate);-- if (!is_null($piggyBank->targetdate)) {- // count back until now.- // echo 'Count back!<br>';- $start = $piggyBank->targetdate;- $end = $piggyBank->startdate;-- while ($start >= $end) {- $currentEnd = clone $start;- $start = Navigation::subtractPeriod($start, $piggyBank->reminder, 1);- $currentStart = clone $start;- Log::debug('Now range: [' . $currentStart->format('d M Y') . '] to [' . $currentEnd->format('d M Y') . ']');-- // for today?- if ($today < $currentEnd && $today > $currentStart) {- Log::debug('Today!');+ $ranges = $repository->getReminderRanges($piggyBank);- // find a reminder first?- $reminders = $this->auth->user()->reminders()- ->where('remindersable_id', $piggyBank->id)- ->onDates($currentStart, $currentEnd)- ->count();- Log::debug('Found ' . $reminders . ' reminders');-- if ($reminders == 0) {- // create a reminder here!- Log::debug('create reminder!');- $repository->createReminder($piggyBank, $currentStart, $currentEnd);- }- // stop looping, we're done.- break;- }+ foreach ($ranges as $range) {+ if ($today < $range['end'] && $today > $range['start']) {+ // create a reminder here!+ $repository->createReminder($piggyBank, $range['start'], $range['end']);+ break;}- } else {- $start = clone $piggyBank->startdate;- while ($start < $today) {- $currentStart = clone $start;- $start = Navigation::addPeriod($start, $piggyBank->reminder, 0);- $currentEnd = clone $start;- Log::debug('Now range: [' . $currentStart->format('d M Y') . '] to [' . $currentEnd->format('d M Y') . ']');+ // stop looping, we're done.- // for today?- if ($today < $currentEnd && $today > $currentStart) {- $reminders = $this->auth->user()->reminders()- ->where('remindersable_id', $piggyBank->id)- ->onDates($currentStart, $currentEnd)- ->count();- Log::debug('Found ' . $reminders . ' reminders');+ }+ }- if ($reminders == 0) {- // create a reminder here!- Log::debug('create reminder!');- $repository->createReminder($piggyBank, $currentStart, $currentEnd);- }- }- }+ // get and list active reminders:+ $reminders = $this->auth->user()->reminders()->today()->get();+ $reminders->each(+ function (Reminder $reminder) use ($repository) {+ $reminder->description = $repository->getReminderText($reminder);}- }+ );+ View::share('reminders', $reminders);}return $next($request);diff --git a/app/Models/Reminder.php b/app/Models/Reminder.phpindex a09eeb73ff..002566ae1e 100644--- a/app/Models/Reminder.php+++ b/app/Models/Reminder.php@@ -33,15 +33,21 @@ class Reminder extends Model/*** @param EloquentBuilder $query- * @param Carbon $date+ * @param Carbon $start+ * @param Carbon $end*- * @return mixed+ * @return $this*/public function scopeOnDates(EloquentBuilder $query, Carbon $start, Carbon $end){return $query->where('reminders.startdate', '=', $start->format('Y-m-d 00:00:00'))->where('reminders.enddate', '=', $end->format('Y-m-d 00:00:00'));}+ public function scopeToday(EloquentBuilder $query) {+ $today = new Carbon;+ return $query->where('startdate','<=',$today->format('Y-m-d 00:00:00'))->where('enddate','>=',$today->format('Y-m-d 00:00:00'))->where('active',1);+ }+/*** @return \Illuminate\Database\Eloquent\Relations\BelongsTo*/diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.phpindex e851949083..63fcb00443 100644--- a/app/Repositories/PiggyBank/PiggyBankRepository.php+++ b/app/Repositories/PiggyBank/PiggyBankRepository.php@@ -2,6 +2,7 @@namespace FireflyIII\Repositories\PiggyBank;+use Amount;use Auth;use Carbon\Carbon;use FireflyIII\Models\PiggyBank;@@ -102,17 +103,15 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface->first();if (is_null($reminder)) {// create one:- $reminder = Reminder::create(- [- 'user_id' => Auth::user()->id,- 'startdate' => $currentStart->format('Y-m-d'),- 'enddate' => $currentEnd->format('Y-m-d'),- 'active' => '1',- 'notnow' => '0',- 'remindersable_id' => $piggyBank->id,- 'remindersable_type' => 'PiggyBank',- ]- );+ $reminder = new Reminder;+ $reminder->user()->associate(Auth::user());+ $reminder->startdate = $currentStart;+ $reminder->enddate = $currentEnd;+ $reminder->active = true;+ $reminder->notnow = false;+ $reminder->remindersable()->associate($piggyBank);+ $reminder->save();+return $reminder;} else {@@ -122,6 +121,84 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface}+ /**+ * This routine will return an array consisting of two dates which indicate the start+ * and end date for each reminder that this piggy bank will have, if the piggy bank has+ * any reminders. For example:+ *+ * [12 mar - 15 mar]+ * [15 mar - 18 mar]+ *+ * etcetera.+ *+ * Array is filled with tiny arrays with Carbon objects in them.+ *+ * @param PiggyBank $piggyBank+ *+ * @return array+ */+ public function getReminderRanges(PiggyBank $piggyBank)+ {+ $ranges = [];+ $today = new Carbon;+ if ($piggyBank->remind_me === false) {+ return $ranges;+ }++ if (!is_null($piggyBank->targetdate)) {+ // count back until now.+ // echo 'Count back!<br>';+ $start = $piggyBank->targetdate;+ $end = $piggyBank->startdate;++ while ($start >= $end) {+ $currentEnd = clone $start;+ $start = Navigation::subtractPeriod($start, $piggyBank->reminder, 1);+ $currentStart = clone $start;+ $ranges[] = ['start' => clone $currentStart, 'end' => clone $currentEnd];+ }+ } else {+ $start = clone $piggyBank->startdate;+ while ($start < $today) {+ $currentStart = clone $start;+ $start = Navigation::addPeriod($start, $piggyBank->reminder, 0);+ $currentEnd = clone $start;+ $ranges[] = ['start' => clone $currentStart, 'end' => clone $currentEnd];+ }+ }++ return $ranges;+ }++ /**+ * Takes a reminder, finds the piggy bank and tells you what to do now.+ * Aka how much money to put in.+ *+ *+ * @param Reminder $reminder+ *+ * @return string+ */+ public function getReminderText(Reminder $reminder)+ {+ /** @var PiggyBank $piggyBank */+ $piggyBank = $reminder->remindersable;++ if (is_null($piggyBank->targetdate)) {+ return 'Add money to this piggy bank to reach your target of ' . Amount::format($piggyBank->targetamount);+ }++ $currentRep = $piggyBank->currentRelevantRep();+++ $ranges = $this->getReminderRanges($piggyBank);+ // calculate number of reminders:+ $left = $piggyBank->targetamount - $currentRep->currentamount;+ $perReminder = $left / count($ranges);++ return 'Add '.Amount::format($perReminder).' to fill this piggy bank on '.$piggyBank->targetdate->format('jS F Y');+ }+/*** @param array $data*diff --git a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.phpindex 656683ff50..33e71be7a9 100644--- a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php+++ b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php@@ -60,4 +60,37 @@ interface PiggyBankRepositoryInterface* @return PiggyBank*/public function update(PiggyBank $piggyBank, array $data);++ /**+ * Takes a reminder, finds the piggy bank and tells you what to do now.+ * Aka how much money to put in.+ *+ * TODO the routine to calculate the number of reminders is probably the same+ * routine as is used in the Reminders-middle ware and can be used again.+ *+ *+ *+ * @param Reminder $reminder+ *+ * @return string+ */+ public function getReminderText(Reminder $reminder);++ /**+ * This routine will return an array consisting of two dates which indicate the start+ * and end date for each reminder that this piggy bank will have, if the piggy bank has+ * any reminders. For example:+ *+ * [12 mar - 15 mar]+ * [15 mar - 18 mar]+ *+ * etcetera.+ *+ * Array is filled with tiny arrays with Carbon objects in them.+ *+ * @param PiggyBank $piggyBank+ *+ * @return array+ */+ public function getReminderRanges(PiggyBank $piggyBank);}\ No newline at end of filediff --git a/resources/views/partials/menu.blade.php b/resources/views/partials/menu.blade.phpindex cbd0d0a1dd..45ac8e55f0 100644--- a/resources/views/partials/menu.blade.php+++ b/resources/views/partials/menu.blade.php@@ -15,7 +15,41 @@<ul class="nav navbar-top-links navbar-right">+ <!-- reminders -->+ @if($reminders->count() > 0)+ <li class="dropdown">+ <a class="dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="false">+ <i class="fa fa-envelope fa-fw"></i> <i class="fa fa-caret-down"></i>+ </a>+ <ul class="dropdown-menu dropdown-messages">+ @foreach($reminders as $reminder)+ <li>+ <a href="#">+ <div>+ <strong>+ {{$reminder->remindersable->name}}+ </strong>+ <span class="pull-right text-muted">+ <em>{{$reminder->startdate->diffForHumans()}}</em>+ </span>+ </div>+ <div>{!! $reminder->description !!}</div>+ </a>+ </li>+ <li class="divider"></li>+ @endforeach+ <li>+ <a class="text-center" href="#">+ <strong>See all reminders</strong>+ <i class="fa fa-angle-right"></i>+ </a>+ </li>+ </ul>+ <!-- /.dropdown-messages -->+ </li>+ @endif+ <!-- menu --><li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#"><i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i>