mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-26 02:40:43 -06:00
Optimised budget year chart.
This commit is contained in:
parent
ad3defb071
commit
83de3482ce
@ -283,9 +283,6 @@ class BudgetController extends Controller
|
||||
*/
|
||||
public function year(BudgetRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts)
|
||||
{
|
||||
$allBudgets = $repository->getBudgets();
|
||||
$budgets = new Collection;
|
||||
|
||||
// chart properties for cache:
|
||||
$cache = new CacheProperties();
|
||||
$cache->addProperty($start);
|
||||
@ -298,26 +295,30 @@ class BudgetController extends Controller
|
||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
// filter empty budgets:
|
||||
foreach ($allBudgets as $budget) {
|
||||
$spent = $repository->balanceInPeriod($budget, $start, $end, $accounts);
|
||||
if ($spent != 0) {
|
||||
$budgets->push($budget);
|
||||
}
|
||||
}
|
||||
$budgetInformation = $repository->getBudgetsAndExpenses($start, $end);
|
||||
$budgets = new Collection;
|
||||
$entries = new Collection;
|
||||
|
||||
$entries = new Collection;
|
||||
/** @var array $row */
|
||||
foreach ($budgetInformation as $row) {
|
||||
$budgets->push($row['budget']);
|
||||
}
|
||||
|
||||
while ($start < $end) {
|
||||
// month is the current end of the period:
|
||||
$month = clone $start;
|
||||
$month->endOfMonth();
|
||||
$row = [clone $start];
|
||||
$row = [clone $start];
|
||||
$dateFormatted = $start->format('Y-m');
|
||||
|
||||
// each budget, fill the row:
|
||||
foreach ($budgets as $budget) {
|
||||
$spent = $repository->balanceInPeriod($budget, $start, $month, $accounts);
|
||||
$row[] = $spent * -1;
|
||||
// each budget, check if there is an entry for this month:
|
||||
/** @var array $row */
|
||||
foreach ($budgetInformation as $budgetRow) {
|
||||
$spent = 0; // nothing spent.
|
||||
if (isset($budgetRow['entries'][$dateFormatted])) {
|
||||
$spent = $budgetRow['entries'][$dateFormatted] * -1; // to fit array
|
||||
}
|
||||
$row[] = $spent;
|
||||
}
|
||||
$entries->push($row);
|
||||
$start->endOfMonth()->addDay();
|
||||
|
@ -210,6 +210,59 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
return Carbon::now()->startOfYear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with every budget in it and the expenses for each budget
|
||||
* per month.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBudgetsAndExpenses(Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()
|
||||
->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id')
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0);
|
||||
}
|
||||
)
|
||||
->groupBy('budgets.id')
|
||||
->groupBy('dateFormatted')
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->get(
|
||||
[
|
||||
'budgets.*',
|
||||
DB::Raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y-%m") AS `dateFormatted`'),
|
||||
DB::Raw('SUM(`transactions`.`amount`) AS `sumAmount`')
|
||||
]
|
||||
);
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
$return = [];
|
||||
foreach ($set as $budget) {
|
||||
$id = $budget->id;
|
||||
if (!isset($return[$id])) {
|
||||
$return[$id] = [
|
||||
'budget' => $budget,
|
||||
'entries' => [],
|
||||
];
|
||||
}
|
||||
// store each entry:
|
||||
$return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
|
@ -27,6 +27,17 @@ interface BudgetRepositoryInterface
|
||||
*/
|
||||
public function destroy(Budget $budget);
|
||||
|
||||
/**
|
||||
* Returns an array with every budget in it and the expenses for each budget
|
||||
* per month.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBudgetsAndExpenses(Carbon $start, Carbon $end);
|
||||
|
||||
/**
|
||||
* Takes tags into account.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user