Skip to content

Firefly III: a personal finances manager

Cleared lots of queries. In some cases, from 1400 back to 300. And those 300 have a different cause which is next.

James Cole3dcdacc3b801
diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php
index 1cf1e15225..fb07d200b3 100644
--- a/app/Http/Controllers/BudgetController.php
+++ b/app/Http/Controllers/BudgetController.php
@@ -8,6 +8,7 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
+use Illuminate\Support\Collection;
use Input;
use Navigation;
use Preferences;
@@ -132,9 +133,9 @@ class BudgetController extends Controller
}
/**
- * @param BudgetRepositoryInterface $repository
+ * @param BudgetRepositoryInterface $repository
*
- * @param ARI $accountRepository
+ * @param ARI $accountRepository
*
* @return \Illuminate\View\View
*/
@@ -232,13 +233,38 @@ class BudgetController extends Controller
$journals = $repository->getJournals($budget, $repetition);
if (is_null($repetition->id)) {
- $limits = $repository->getBudgetLimits($budget);
- $subTitle = e($budget->name);
+ $start = $repository->firstActivity($budget);
+ $end = new Carbon;
+ $set = $budget->limitrepetitions()->orderBy('startdate','DESC')->get();
+ //$set = $repository->getBudgetLimits($budget);
+ //$subTitle = e($budget->name);
} else {
- $limits = [$repetition->budgetLimit];
- $subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]);
+ $start = $repetition->startdate;
+ $end = $repetition->enddate;
+ $set = new Collection([$repetition]);
+ // $spentArray = $repository->spentPerDay($budget, $start, $end);
+ // $budgetLimit = $repetition->budgetLimit;
+ // $budgetLimit->spent = $this->getSumOfRange($start, $end, $spentArray); // bit weird but it works.
+ // $limits = new Collection([$budgetLimit]);
+ //$subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]);
}
+ $spentArray = $repository->spentPerDay($budget, $start, $end);
+ $limits = new Collection();
+
+ /** @var LimitRepetition $entry */
+ foreach ($set as $entry) {
+ $entry->spent = $this->getSumOfRange($entry->startdate, $entry->enddate, $spentArray);
+ $limits->push($entry);
+ }
+
+ // loop limits and set the amount spent for each limit.
+ // /** @var BudgetLimit $budgetLimit */
+ // foreach ($set as $budgetLimit) {
+ // $start = $budgetLimit->startdate;
+ // $end = $budgetLimit->lim
+ // }
+
$journals->setPath('/budgets/show/' . $budget->id);
return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle'));
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index c5a3e857c4..cfcbf75359 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -5,7 +5,6 @@ namespace FireflyIII\Repositories\Budget;
use Auth;
use Carbon\Carbon;
use DB;
-use FireflyIII\Models\Account;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\LimitRepetition;
@@ -185,6 +184,33 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
}
+ /**
+ * @param Budget $budget
+ *
+ * @return Carbon
+ */
+ public function firstActivity(Budget $budget)
+ {
+ $first = $budget->transactionjournals()->orderBy('date', 'ASC')->first();
+ if ($first) {
+ return $first->date;
+ }
+
+ return new Carbon;
+ }
+
+ /**
+ * Get a collection of all the limit repetitions belonging to this $budget.
+ *
+ * @param Budget $budget
+ *
+ * @return Collection
+ */
+ public function getBudgetReps(Budget $budget) {
+ $set = $budget->limitrepetitions()->count();
+ var_dump($set);
+ }
+
/**
* @param Budget $budget
* @param Carbon $start
@@ -213,6 +239,41 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
return $budget->budgetLimits()->orderBy('startdate', 'DESC')->get();
}
+
+ /**
+ * Returns an array with the following key:value pairs:
+ *
+ * yyyy-mm-dd:<amount>
+ *
+ * Where yyyy-mm-dd is the date and <amount> is the money spent using DEPOSITS in the $budget
+ * from all the users accounts.
+ *
+ * @param Budget $budget
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ */
+ public function spentPerDay(Budget $budget, Carbon $start, Carbon $end)
+ {
+ /** @var Collection $query */
+ $query = $budget->transactionJournals()
+ ->transactionTypes([TransactionType::WITHDRAWAL])
+ ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
+ ->where('transactions.amount', '<', 0)
+ ->before($end)
+ ->after($start)
+ ->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
+
+ $return = [];
+ foreach ($query->toArray() as $entry) {
+ $return[$entry['dateFormatted']] = $entry['sum'];
+ }
+
+ return $return;
+ }
+
+
/**
* @return Collection
*/
@@ -617,7 +678,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
*/
public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end)
{
- $ids = $accounts->pluck('id')->toArray();
+ $ids = $accounts->pluck('id')->toArray();
$budgetIds = $budgets->pluck('id')->toArray();
/** @var Collection $set */
diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php
index c1109188f7..c228df2ce3 100644
--- a/app/Repositories/Budget/BudgetRepositoryInterface.php
+++ b/app/Repositories/Budget/BudgetRepositoryInterface.php
@@ -32,6 +32,22 @@ interface BudgetRepositoryInterface
*/
public function getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end);
+ /**
+ * @param Budget $budget
+ *
+ * @return Carbon
+ */
+ public function firstActivity(Budget $budget);
+
+ /**
+ * Get a collection of all the limit repetitions belonging to this $budget.
+ *
+ * @param Budget $budget
+ *
+ * @return Collection
+ */
+ public function getBudgetReps(Budget $budget);
+
/**
* Returns the expenses for this budget grouped per month, with the date
* in "date" (a string, not a Carbon) and the amount in "dailyAmount".
@@ -44,6 +60,22 @@ interface BudgetRepositoryInterface
*/
public function getExpensesPerMonth(Budget $budget, Carbon $start, Carbon $end);
+ /**
+ * Returns an array with the following key:value pairs:
+ *
+ * yyyy-mm-dd:<amount>
+ *
+ * Where yyyy-mm-dd is the date and <amount> is the money spent using WITHDRAWALS in the $budget
+ * from all the users accounts.
+ *
+ * @param Budget $budget
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ */
+ public function spentPerDay(Budget $budget, Carbon $start, Carbon $end);
+
/**
* @param Budget $budget
*
diff --git a/resources/twig/budgets/show.twig b/resources/twig/budgets/show.twig
index 173b9a1d4e..666b874300 100644
--- a/resources/twig/budgets/show.twig
+++ b/resources/twig/budgets/show.twig
@@ -44,47 +44,43 @@
{% endif %}
{% for limit in limits %}
- {% for rep in limit.limitRepetitions %}
- {% set spentInRep = spentInRepetition(rep) %}
- <div class="box">
- <div class="box-header with-border">
- <h3 class="box-title"><a href="{{ route('budgets.show',[budget.id,rep.id]) }}">{{ rep.startdate.formatLocalized(monthFormat) }}</a>
- </h3>
- </div>
- <div class="box-body">
- <div class="row">
- <div class="col-lg-6 col-md-6 col-sm-6">
- {{ 'amount'|_ }}: {{ rep.amount|formatAmount }}
- </div>
- <div class="col-lg-6 col-md-6 col-sm-6">
- {{ 'spent'|_ }}: {{ spentInRep|formatAmount }}
- </div>
+ <div class="box">
+ <div class="box-header with-border">
+ <h3 class="box-title"><a href="{{ route('budgets.show',[budget.id,limit.id]) }}">{{ limit.startdate.formatLocalized(monthFormat) }}</a>
+ </h3>
+ </div>
+ <div class="box-body">
+ <div class="row">
+ <div class="col-lg-6 col-md-6 col-sm-6">
+ {{ 'amount'|_ }}: {{ limit.amount|formatAmount }}
+ </div>
+ <div class="col-lg-6 col-md-6 col-sm-6">
+ {{ 'spent'|_ }}: {{ limit.spent|formatAmount }}
</div>
- <div class="row">
- <div class="col-lg-12 col-md-12 col-sm-12">
- {% set overspent = rep.amount + spentInRep < 0 %}
+ </div>
+ <div class="row">
+ <div class="col-lg-12 col-md-12 col-sm-12">
+ {% set overspent = limit.amount + limit.spent < 0 %}
- {% if overspent %}
- {% set pct = (spentInRep != 0 ? (rep.amount / (spentInRep*-1))*100 : 0) %} <!-- must have -1 here -->
- <div class="progress progress-striped">
- <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="{{ pct|round }}" aria-valuemin="0"
- aria-valuemax="100" style="width: {{ pct|round }}%;"></div>
- <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="{{ (100-pct)|round }}"
- aria-valuemin="0" aria-valuemax="100" style="width: {{ (100-pct)|round }}%;"></div>
- </div>
- {% else %}
- {% set amount = rep.amount %}
- {% set pct = (amount != 0 ? (((spentInRep*-1) / amount)*100) : 0) %} <!-- must have -1 here -->
- <div class="progress progress-striped">
- <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="{{ pct|round }}" aria-valuemin="0"
- aria-valuemax="100" style="width: {{ pct|round }}%;"></div>
- </div>
- {% endif %}
- </div>
+ {% if overspent %}
+ {% set pct = (limit.spent != 0 ? (limit.amount / (limit.spent*-1))*100 : 0) %} <!-- must have -1 here -->
+ <div class="progress progress-striped">
+ <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="{{ pct|round }}" aria-valuemin="0"
+ aria-valuemax="100" style="width: {{ pct|round }}%;"></div>
+ <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="{{ (100-pct)|round }}"
+ aria-valuemin="0" aria-valuemax="100" style="width: {{ (100-pct)|round }}%;"></div>
+ </div>
+ {% else %}
+ {% set pct = (limit.amount != 0 ? (((limit.spent*-1) / limit.amount)*100) : 0) %} <!-- must have -1 here -->
+ <div class="progress progress-striped">
+ <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="{{ pct|round }}" aria-valuemin="0"
+ aria-valuemax="100" style="width: {{ pct|round }}%;"></div>
+ </div>
+ {% endif %}
</div>
</div>
</div>
- {% endfor %}
+ </div>
{% endfor %}
{% if limits|length == 1 %}