This fixes what turns out to be an incredibly inaccurate chart.

This commit is contained in:
James Cole 2016-02-10 07:34:04 +01:00
parent f9696287a4
commit 5974bdcc2a
3 changed files with 7 additions and 55 deletions

View File

@ -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);
}

View File

@ -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
*

View File

@ -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
*