From 5974bdcc2a949081e28d6cc6ee1692276a7f71bb Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 10 Feb 2016 07:34:04 +0100 Subject: [PATCH] This fixes what turns out to be an incredibly inaccurate chart. --- .../Controllers/Chart/BudgetController.php | 17 ++++------ app/Repositories/Budget/BudgetRepository.php | 33 ------------------- .../Budget/BudgetRepositoryInterface.php | 12 ------- 3 files changed, 7 insertions(+), 55 deletions(-) diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 84c2095cb4..e08a67bc72 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -65,20 +65,17 @@ class BudgetController extends Controller $last = Navigation::endOfX($last, $range, $final); $entries = new Collection; // get all expenses: - $set = $repository->getExpensesPerMonth($budget, $first, $last); + $spentArray = $repository->spentPerDay($budget, $first, $last); while ($first < $last) { - $monthFormatted = $first->format('Y-m'); - $filtered = $set->filter( - function (Budget $obj) use ($monthFormatted) { - return $obj->dateFormatted == $monthFormatted; - } - ); - $spent = is_null($filtered->first()) ? '0' : $filtered->first()->monthlyAmount; - - $entries->push([$first, round(($spent * -1), 2)]); + // periodspecific dates: + $currentStart = Navigation::startOfPeriod($first, $range); + $currentEnd = Navigation::endOfPeriod($first, $range); + $spent = $this->getSumOfRange($currentStart, $currentEnd, $spentArray); + $entry = [$first, ($spent * -1)]; + $entries->push($entry); $first = Navigation::addPeriod($first, $range, 0); } diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index e01b726071..51ed3fef26 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -370,39 +370,6 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn return $set; } - /** - * Returns the expenses for this budget grouped per month, with the date - * in "dateFormatted" (a string, not a Carbon) and the amount in "dailyAmount". - * - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getExpensesPerMonth(Budget $budget, Carbon $start, Carbon $end) - { - $set = Auth::user()->budgets() - ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.budget_id', '=', 'budgets.id') - ->leftJoin('transaction_journals', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - ->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') - ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - ->whereNull('transaction_journals.deleted_at') - ->where('budgets.id', $budget->id) - ->where('transactions.amount', '<', 0) - ->groupBy('dateFormatted') - ->orderBy('transaction_journals.date') - ->get( - [ - DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y-%m") AS `dateFormatted`'), - DB::raw('SUM(`transactions`.`amount`) as `monthlyAmount`'), - ] - ); - - return $set; - } - /** * @param Budget $budget * diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index c9d4826e46..e3f00681b5 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -136,18 +136,6 @@ interface BudgetRepositoryInterface */ public function getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end); - /** - * Returns the expenses for this budget grouped per month, with the date - * in "date" (a string, not a Carbon) and the amount in "dailyAmount". - * - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getExpensesPerMonth(Budget $budget, Carbon $start, Carbon $end); - /** * @param Budget $budget *