Fix some issues in multi-currency budget reports.

This commit is contained in:
James Cole
2019-09-02 21:21:29 +02:00
parent 771cf73171
commit 206dae87ba
3 changed files with 44 additions and 61 deletions

View File

@@ -41,6 +41,26 @@ class BudgetController extends Controller
{ {
use BasicDataSupport; use BasicDataSupport;
/** @var OperationsRepositoryInterface */
private $opsRepository;
/**
* ExpenseReportController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->opsRepository = app(OperationsRepositoryInterface::class);
return $next($request);
}
);
}
/** /**
* @param Collection $accounts * @param Collection $accounts
* @param Collection $budgets * @param Collection $budgets
@@ -49,10 +69,8 @@ class BudgetController extends Controller
*/ */
public function accountPerBudget(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) public function accountPerBudget(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end)
{ {
// get all journals. $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets);
$opsRepository = app(OperationsRepositoryInterface::class); $report = [];
$spent = $opsRepository->listExpenses($start, $end, $accounts, $budgets);
$report = [];
/** @var Account $account */ /** @var Account $account */
foreach ($accounts as $account) { foreach ($accounts as $account) {
$accountId = $account->id; $accountId = $account->id;
@@ -101,11 +119,9 @@ class BudgetController extends Controller
*/ */
public function accounts(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) public function accounts(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end)
{ {
// get all journals. $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets);
$opsRepository = app(OperationsRepositoryInterface::class); $report = [];
$spent = $opsRepository->listExpenses($start, $end, $accounts, $budgets); $sums = [];
$report = [];
$sums = [];
/** @var Account $account */ /** @var Account $account */
foreach ($accounts as $account) { foreach ($accounts as $account) {
$accountId = $account->id; $accountId = $account->id;
@@ -156,10 +172,8 @@ class BudgetController extends Controller
*/ */
public function avgExpenses(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) public function avgExpenses(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end)
{ {
// get all journals. $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets);
$opsRepository = app(OperationsRepositoryInterface::class); $result = [];
$spent = $opsRepository->listExpenses($start, $end, $accounts, $budgets);
$result = [];
foreach ($spent as $currency) { foreach ($spent as $currency) {
$currencyId = $currency['currency_id']; $currencyId = $currency['currency_id'];
foreach ($currency['budgets'] as $budget) { foreach ($currency['budgets'] as $budget) {
@@ -210,11 +224,9 @@ class BudgetController extends Controller
*/ */
public function budgets(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) public function budgets(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end)
{ {
// get all journals. $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets);
$opsRepository = app(OperationsRepositoryInterface::class); $sums = [];
$spent = $opsRepository->listExpenses($start, $end, $accounts, $budgets); $report = [];
$sums = [];
$report = [];
/** @var Budget $budget */ /** @var Budget $budget */
foreach ($budgets as $budget) { foreach ($budgets as $budget) {
$budgetId = $budget->id; $budgetId = $budget->id;
@@ -257,6 +269,7 @@ class BudgetController extends Controller
/** /**
* Show partial overview of budgets. * Show partial overview of budgets.
* TODO can be replaced I think.
* *
* @param Collection $accounts * @param Collection $accounts
* @param Carbon $start * @param Carbon $start
@@ -310,14 +323,12 @@ class BudgetController extends Controller
return $cache->get(); // @codeCoverageIgnore return $cache->get(); // @codeCoverageIgnore
} }
/** @var OperationsRepositoryInterface $opsRepository */ $periods = app('navigation')->listOfPeriods($start, $end);
$opsRepository = app(OperationsRepositoryInterface::class); $keyFormat = app('navigation')->preferredCarbonFormat($start, $end);
$periods = app('navigation')->listOfPeriods($start, $end);
$keyFormat = app('navigation')->preferredCarbonFormat($start, $end);
// list expenses for budgets in account(s) // list expenses for budgets in account(s)
$expenses = $opsRepository->listExpenses($start, $end, $accounts); $expenses = $this->opsRepository->listExpenses($start, $end, $accounts);
$report = []; $report = [];
foreach ($expenses as $currency) { foreach ($expenses as $currency) {
@@ -363,10 +374,8 @@ class BudgetController extends Controller
*/ */
public function topExpenses(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) public function topExpenses(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end)
{ {
// get all journals. $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets);
$opsRepository = app(OperationsRepositoryInterface::class); $result = [];
$spent = $opsRepository->listExpenses($start, $end, $accounts, $budgets);
$result = [];
foreach ($spent as $currency) { foreach ($spent as $currency) {
$currencyId = $currency['currency_id']; $currencyId = $currency['currency_id'];
foreach ($currency['budgets'] as $budget) { foreach ($currency['budgets'] as $budget) {

View File

@@ -125,8 +125,8 @@ class ReportController extends Controller
trans( trans(
'firefly.report_audit', 'firefly.report_audit',
[ [
'start' => $start->formatLocalized($this->monthFormat), 'start' => $start->formatLocalized($this->monthAndDayFormat),
'end' => $end->formatLocalized($this->monthFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat),
] ]
) )
); );
@@ -162,8 +162,8 @@ class ReportController extends Controller
trans( trans(
'firefly.report_budget', 'firefly.report_budget',
[ [
'start' => $start->formatLocalized($this->monthFormat), 'start' => $start->formatLocalized($this->monthAndDayFormat),
'end' => $end->formatLocalized($this->monthFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat),
] ]
) )
); );
@@ -200,8 +200,8 @@ class ReportController extends Controller
trans( trans(
'firefly.report_category', 'firefly.report_category',
[ [
'start' => $start->formatLocalized($this->monthFormat), 'start' => $start->formatLocalized($this->monthAndDayFormat),
'end' => $end->formatLocalized($this->monthFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat),
] ]
) )
); );
@@ -406,8 +406,8 @@ class ReportController extends Controller
trans( trans(
'firefly.report_tag', 'firefly.report_tag',
[ [
'start' => $start->formatLocalized($this->monthFormat), 'start' => $start->formatLocalized($this->monthAndDayFormat),
'end' => $end->formatLocalized($this->monthFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat),
] ]
) )
); );

View File

@@ -30,32 +30,6 @@ namespace FireflyIII\Support\Http\Controllers;
trait BasicDataSupport trait BasicDataSupport
{ {
/**
* Filters empty results from getBudgetPeriodReport.
*
* @param array $data
*
* @return array
*/
protected function filterPeriodReport(array $data): array // helper function for period overview.
{
/**
* @var int $entryId
* @var array $set
*/
foreach ($data as $entryId => $set) {
$sum = '0';
$entries = $set['entries'] ?? [];
foreach ($entries as $amount) {
$sum = bcadd($amount, $sum);
}
$data[$entryId]['sum'] = $sum;
if (0 === bccomp('0', $sum)) {
unset($data[$entryId]);
}
}
return $data;
}
/** /**
* Find the ID in a given array. Return '0' of not there (amount). * Find the ID in a given array. Return '0' of not there (amount).