mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Optimised budget year chart.
This commit is contained in:
@@ -283,9 +283,6 @@ class BudgetController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function year(BudgetRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts)
|
public function year(BudgetRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts)
|
||||||
{
|
{
|
||||||
$allBudgets = $repository->getBudgets();
|
|
||||||
$budgets = new Collection;
|
|
||||||
|
|
||||||
// chart properties for cache:
|
// chart properties for cache:
|
||||||
$cache = new CacheProperties();
|
$cache = new CacheProperties();
|
||||||
$cache->addProperty($start);
|
$cache->addProperty($start);
|
||||||
@@ -298,26 +295,30 @@ class BudgetController extends Controller
|
|||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter empty budgets:
|
$budgetInformation = $repository->getBudgetsAndExpenses($start, $end);
|
||||||
foreach ($allBudgets as $budget) {
|
$budgets = new Collection;
|
||||||
$spent = $repository->balanceInPeriod($budget, $start, $end, $accounts);
|
$entries = new Collection;
|
||||||
if ($spent != 0) {
|
|
||||||
$budgets->push($budget);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$entries = new Collection;
|
/** @var array $row */
|
||||||
|
foreach ($budgetInformation as $row) {
|
||||||
|
$budgets->push($row['budget']);
|
||||||
|
}
|
||||||
|
|
||||||
while ($start < $end) {
|
while ($start < $end) {
|
||||||
// month is the current end of the period:
|
// month is the current end of the period:
|
||||||
$month = clone $start;
|
$month = clone $start;
|
||||||
$month->endOfMonth();
|
$month->endOfMonth();
|
||||||
$row = [clone $start];
|
$row = [clone $start];
|
||||||
|
$dateFormatted = $start->format('Y-m');
|
||||||
|
|
||||||
// each budget, fill the row:
|
// each budget, check if there is an entry for this month:
|
||||||
foreach ($budgets as $budget) {
|
/** @var array $row */
|
||||||
$spent = $repository->balanceInPeriod($budget, $start, $month, $accounts);
|
foreach ($budgetInformation as $budgetRow) {
|
||||||
$row[] = $spent * -1;
|
$spent = 0; // nothing spent.
|
||||||
|
if (isset($budgetRow['entries'][$dateFormatted])) {
|
||||||
|
$spent = $budgetRow['entries'][$dateFormatted] * -1; // to fit array
|
||||||
|
}
|
||||||
|
$row[] = $spent;
|
||||||
}
|
}
|
||||||
$entries->push($row);
|
$entries->push($row);
|
||||||
$start->endOfMonth()->addDay();
|
$start->endOfMonth()->addDay();
|
||||||
|
@@ -210,6 +210,59 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
|||||||
return Carbon::now()->startOfYear();
|
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
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
|
@@ -27,6 +27,17 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function destroy(Budget $budget);
|
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.
|
* Takes tags into account.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user