From 3dcdacc3b801f90e97a7098f52793a17d879008c Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 31 Dec 2015 17:46:34 +0100 Subject: [PATCH] Cleared lots of queries. In some cases, from 1400 back to 300. And those 300 have a different cause which is next. --- app/Http/Controllers/BudgetController.php | 38 +++++++++-- app/Repositories/Budget/BudgetRepository.php | 65 +++++++++++++++++- .../Budget/BudgetRepositoryInterface.php | 32 +++++++++ resources/twig/budgets/show.twig | 68 +++++++++---------- 4 files changed, 159 insertions(+), 44 deletions(-) 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: + * + * Where yyyy-mm-dd is the date and 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: + * + * Where yyyy-mm-dd is the date and 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) %} -
- -
-
-
- {{ 'amount'|_ }}: {{ rep.amount|formatAmount }} -
-
- {{ 'spent'|_ }}: {{ spentInRep|formatAmount }} -
+
+ +
+
+
+ {{ 'amount'|_ }}: {{ limit.amount|formatAmount }}
-
-
- {% set overspent = rep.amount + spentInRep < 0 %} +
+ {{ 'spent'|_ }}: {{ limit.spent|formatAmount }} +
+
+
+
+ {% set overspent = limit.amount + limit.spent < 0 %} - {% if overspent %} - {% set pct = (spentInRep != 0 ? (rep.amount / (spentInRep*-1))*100 : 0) %} -
-
-
-
- {% else %} - {% set amount = rep.amount %} - {% set pct = (amount != 0 ? (((spentInRep*-1) / amount)*100) : 0) %} -
-
-
- {% endif %} -
+ {% if overspent %} + {% set pct = (limit.spent != 0 ? (limit.amount / (limit.spent*-1))*100 : 0) %} +
+
+
+
+ {% else %} + {% set pct = (limit.amount != 0 ? (((limit.spent*-1) / limit.amount)*100) : 0) %} +
+
+
+ {% endif %}
- {% endfor %} +
{% endfor %} {% if limits|length == 1 %}