mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 10:33:30 +00:00
Optimised chart.
This commit is contained in:
@@ -190,15 +190,24 @@ class BudgetController extends Controller
|
|||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$set = $repository->getExpensesPerDay($budget, $start, $end);
|
||||||
$entries = new Collection;
|
$entries = new Collection;
|
||||||
$amount = $repetition->amount;
|
$amount = $repetition->amount;
|
||||||
|
|
||||||
|
// get sum (har har)!
|
||||||
while ($start <= $end) {
|
while ($start <= $end) {
|
||||||
|
$formatted = $start->format('Y-m-d');
|
||||||
|
$filtered = $set->filter(
|
||||||
|
function (Budget $obj) use ($formatted) {
|
||||||
|
return $obj->date == $formatted;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$sum = is_null($filtered->first()) ? '0' : $filtered->first()->dailyAmount;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Sum of expenses on this day:
|
* Sum of expenses on this day:
|
||||||
*/
|
*/
|
||||||
$sum = $repository->expensesOnDay($budget, $start);
|
$amount = round(bcadd($amount, $sum), 2);
|
||||||
$amount = bcadd($amount, $sum);
|
|
||||||
$entries->push([clone $start, $amount]);
|
$entries->push([clone $start, $amount]);
|
||||||
$start->addDay();
|
$start->addDay();
|
||||||
}
|
}
|
||||||
|
@@ -37,6 +37,49 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the expenses for this budget grouped per day, 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 getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* select transaction_journals.date, SUM(transactions.amount) as dailyAmount from budgets
|
||||||
|
left join budget_transaction_journal ON budget_transaction_journal.budget_id = budgets.id
|
||||||
|
left join transaction_journals ON budget_transaction_journal.transaction_journal_id = transaction_journals.id
|
||||||
|
left join transactions ON transaction_journals.id = transactions.transaction_journal_id
|
||||||
|
where
|
||||||
|
|
||||||
|
transaction_journals.date >= "2015-12-01"
|
||||||
|
and transaction_journals.date <= "2015-12-31"
|
||||||
|
and budgets.id = 1
|
||||||
|
and transactions.amount < 0
|
||||||
|
group by transaction_journals.date
|
||||||
|
order by transaction_journals.date
|
||||||
|
*/
|
||||||
|
|
||||||
|
$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'))
|
||||||
|
->where('budgets.id', $budget->id)
|
||||||
|
->where('transactions.amount', '<', 0)
|
||||||
|
->groupBy('transaction_journals.date')
|
||||||
|
->orderBy('transaction_journals.date')
|
||||||
|
->get(['transaction_journals.date', DB::Raw('SUM(`transactions`.`amount`) as `dailyAmount`')]);
|
||||||
|
|
||||||
|
return $set;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
*
|
*
|
||||||
@@ -534,20 +577,21 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
|||||||
}
|
}
|
||||||
|
|
||||||
$set = Auth::user()->budgets()
|
$set = Auth::user()->budgets()
|
||||||
->leftJoin('budget_limits', 'budgets.id', '=', 'budget_limits.budget_id')
|
->leftJoin('budget_limits', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||||
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||||
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d'))
|
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d'))
|
||||||
->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d'))
|
->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d'))
|
||||||
->groupBy('budgets.id')
|
->groupBy('budgets.id')
|
||||||
->groupBy('dateFormatted')
|
->groupBy('dateFormatted')
|
||||||
->whereIn('budgets.id', $budgetIds)
|
->whereIn('budgets.id', $budgetIds)
|
||||||
->get(
|
->get(
|
||||||
[
|
[
|
||||||
'budgets.*',
|
'budgets.*',
|
||||||
DB::Raw('DATE_FORMAT(`limit_repetitions`.`startdate`,"%Y") as `dateFormatted`'),
|
DB::Raw('DATE_FORMAT(`limit_repetitions`.`startdate`,"%Y") as `dateFormatted`'),
|
||||||
DB::Raw('SUM(`limit_repetitions`.`amount`) as `budgeted`')
|
DB::Raw('SUM(`limit_repetitions`.`amount`) as `budgeted`')
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
return $set;
|
return $set;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,6 +20,18 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function cleanupBudgets();
|
public function cleanupBudgets();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the expenses for this budget grouped per day, 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 getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user