mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-08-05 18:57:26 -05:00
Finish multi-currency budget and account report.
This commit is contained in:
@@ -25,6 +25,7 @@ namespace FireflyIII\Http\Controllers\Report;
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||
use FireflyIII\Repositories\Category\NoCategoryRepositoryInterface;
|
||||
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
|
||||
@@ -62,6 +63,105 @@ class CategoryController extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function accountPerCategory(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
|
||||
{
|
||||
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
|
||||
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
|
||||
$report = [];
|
||||
/** @var Account $account */
|
||||
foreach ($accounts as $account) {
|
||||
$accountId = $account->id;
|
||||
$report[$accountId] = $report[$accountId] ?? [
|
||||
'name' => $account->name,
|
||||
'id' => $account->id,
|
||||
'iban' => $account->iban,
|
||||
'currencies' => [],
|
||||
];
|
||||
}
|
||||
|
||||
// loop expenses.
|
||||
foreach ($spent as $currency) {
|
||||
$currencyId = $currency['currency_id'];
|
||||
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$sourceAccountId = $journal['source_account_id'];
|
||||
$report[$sourceAccountId]['currencies'][$currencyId] = $report[$sourceAccountId]['currencies'][$currencyId] ?? [
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_name' => $currency['currency_name'],
|
||||
'currency_decimal_places' => $currency['currency_decimal_places'],
|
||||
'categories' => [],
|
||||
];
|
||||
|
||||
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]
|
||||
= $report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]
|
||||
??
|
||||
[
|
||||
'spent' => '0',
|
||||
'earned' => '0',
|
||||
'sum' => '0',
|
||||
];
|
||||
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]['spent'] = bcadd(
|
||||
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]['spent'], $journal['amount']
|
||||
);
|
||||
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]['sum'] = bcadd(
|
||||
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]['sum'], $journal['amount']
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// loop income.
|
||||
foreach ($earned as $currency) {
|
||||
$currencyId = $currency['currency_id'];
|
||||
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$destinationId = $journal['destination_account_id'];
|
||||
$report[$destinationId]['currencies'][$currencyId]
|
||||
= $report[$destinationId]['currencies'][$currencyId]
|
||||
?? [
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_name' => $currency['currency_name'],
|
||||
'currency_decimal_places' => $currency['currency_decimal_places'],
|
||||
'categories' => [],
|
||||
];
|
||||
|
||||
|
||||
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]
|
||||
= $report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]
|
||||
??
|
||||
[
|
||||
'spent' => '0',
|
||||
'earned' => '0',
|
||||
'sum' => '0',
|
||||
];
|
||||
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]['earned'] = bcadd(
|
||||
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]['earned'], $journal['amount']
|
||||
);
|
||||
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]['sum'] = bcadd(
|
||||
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]['sum'], $journal['amount']
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return view('reports.category.partials.account-per-category', compact('report', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
@@ -153,8 +253,8 @@ class CategoryController extends Controller
|
||||
$report[$destinationAccountId]['currencies'][$currencyId]['sum'] = bcadd(
|
||||
$report[$destinationAccountId]['currencies'][$currencyId]['sum'], $journal['amount']
|
||||
);
|
||||
$sums[$currencyId]['earned_sum'] = bcadd($sums[$currencyId]['earned_sum'], $journal['amount']);
|
||||
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], $journal['amount']);
|
||||
$sums[$currencyId]['earned_sum'] = bcadd($sums[$currencyId]['earned_sum'], $journal['amount']);
|
||||
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], $journal['amount']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,6 +262,110 @@ class CategoryController extends Controller
|
||||
return view('reports.category.partials.accounts', compact('sums', 'report'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function categories(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
|
||||
{
|
||||
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
|
||||
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
|
||||
$sums = [];
|
||||
$report = [];
|
||||
/** @var Category $category */
|
||||
foreach ($categories as $category) {
|
||||
$categoryId = $category->id;
|
||||
$report[$categoryId] = $report[$categoryId] ?? [
|
||||
'name' => $category->name,
|
||||
'id' => $category->id,
|
||||
'currencies' => [],
|
||||
];
|
||||
}
|
||||
foreach ($spent as $currency) {
|
||||
$currencyId = $currency['currency_id'];
|
||||
$sums[$currencyId] = $sums[$currencyId] ?? [
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_name' => $currency['currency_name'],
|
||||
'currency_decimal_places' => $currency['currency_decimal_places'],
|
||||
'earned_sum' => '0',
|
||||
'spent_sum' => '0',
|
||||
'total_sum' => '0',
|
||||
];
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
$categoryId = $category['id'];
|
||||
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
// add currency info to report array:
|
||||
$report[$categoryId]['currencies'][$currencyId] = $report[$categoryId]['currencies'][$currencyId] ?? [
|
||||
'spent' => '0',
|
||||
'earned' => '0',
|
||||
'sum' => '0',
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_name' => $currency['currency_name'],
|
||||
'currency_decimal_places' => $currency['currency_decimal_places'],
|
||||
];
|
||||
$report[$categoryId]['currencies'][$currencyId]['spent'] = bcadd(
|
||||
$report[$categoryId]['currencies'][$currencyId]['spent'], $journal['amount']
|
||||
);
|
||||
$report[$categoryId]['currencies'][$currencyId]['sum'] = bcadd(
|
||||
$report[$categoryId]['currencies'][$currencyId]['sum'], $journal['amount']
|
||||
);
|
||||
|
||||
$sums[$currencyId]['spent_sum'] = bcadd($sums[$currencyId]['spent_sum'], $journal['amount']);
|
||||
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], $journal['amount']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($earned as $currency) {
|
||||
$currencyId = $currency['currency_id'];
|
||||
$sums[$currencyId] = $sums[$currencyId] ?? [
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_name' => $currency['currency_name'],
|
||||
'currency_decimal_places' => $currency['currency_decimal_places'],
|
||||
'earned_sum' => '0',
|
||||
'spent_sum' => '0',
|
||||
'total_sum' => '0',
|
||||
];
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
$categoryId = $category['id'];
|
||||
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
// add currency info to report array:
|
||||
$report[$categoryId]['currencies'][$currencyId] = $report[$categoryId]['currencies'][$currencyId] ?? [
|
||||
'spent' => '0',
|
||||
'earned' => '0',
|
||||
'sum' => '0',
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_name' => $currency['currency_name'],
|
||||
'currency_decimal_places' => $currency['currency_decimal_places'],
|
||||
];
|
||||
$report[$categoryId]['currencies'][$currencyId]['earned'] = bcadd(
|
||||
$report[$categoryId]['currencies'][$currencyId]['earned'], $journal['amount']
|
||||
);
|
||||
$report[$categoryId]['currencies'][$currencyId]['sum'] = bcadd(
|
||||
$report[$categoryId]['currencies'][$currencyId]['sum'], $journal['amount']
|
||||
);
|
||||
|
||||
$sums[$currencyId]['earned_sum'] = bcadd($sums[$currencyId]['earned_sum'], $journal['amount']);
|
||||
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], $journal['amount']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return view('reports.category.partials.categories', compact('sums', 'report'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show overview of expenses in category.
|
||||
*
|
||||
@@ -371,7 +575,6 @@ class CategoryController extends Controller
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show overview of operations.
|
||||
*
|
||||
@@ -538,6 +741,212 @@ class CategoryController extends Controller
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
public function topExpenses(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
|
||||
{
|
||||
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
|
||||
$result = [];
|
||||
foreach ($spent as $currency) {
|
||||
$currencyId = $currency['currency_id'];
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$result[] = [
|
||||
'description' => $journal['description'],
|
||||
'transaction_group_id' => $journal['transaction_group_id'],
|
||||
'amount_float' => (float)$journal['amount'],
|
||||
'amount' => $journal['amount'],
|
||||
'date' => $journal['date']->formatLocalized($this->monthAndDayFormat),
|
||||
'destination_account_name' => $journal['destination_account_name'],
|
||||
'destination_account_id' => $journal['destination_account_id'],
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'currency_name' => $currency['currency_name'],
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_decimal_places' => $currency['currency_decimal_places'],
|
||||
'category_id' => $category['id'],
|
||||
'category_name' => $category['name'],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
// sort by amount_float
|
||||
// sort temp array by amount.
|
||||
$amounts = array_column($result, 'amount_float');
|
||||
array_multisort($amounts, SORT_ASC, $result);
|
||||
|
||||
try {
|
||||
$result = view('reports.category.partials.top-expenses', compact('result'))->render();
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (Throwable $e) {
|
||||
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
|
||||
$result = sprintf('Could not render view: %s', $e->getMessage());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
public function topIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
|
||||
{
|
||||
$spent = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
|
||||
$result = [];
|
||||
foreach ($spent as $currency) {
|
||||
$currencyId = $currency['currency_id'];
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$result[] = [
|
||||
'description' => $journal['description'],
|
||||
'transaction_group_id' => $journal['transaction_group_id'],
|
||||
'amount_float' => (float)$journal['amount'],
|
||||
'amount' => $journal['amount'],
|
||||
'date' => $journal['date']->formatLocalized($this->monthAndDayFormat),
|
||||
'source_account_name' => $journal['source_account_name'],
|
||||
'source_account_id' => $journal['source_account_id'],
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'currency_name' => $currency['currency_name'],
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_decimal_places' => $currency['currency_decimal_places'],
|
||||
'category_id' => $category['id'],
|
||||
'category_name' => $category['name'],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
// sort by amount_float
|
||||
// sort temp array by amount.
|
||||
$amounts = array_column($result, 'amount_float');
|
||||
array_multisort($amounts, SORT_DESC, $result);
|
||||
|
||||
try {
|
||||
$result = view('reports.category.partials.top-income', compact('result'))->render();
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (Throwable $e) {
|
||||
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
|
||||
$result = sprintf('Could not render view: %s', $e->getMessage());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
public function avgExpenses(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
|
||||
{
|
||||
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
|
||||
$result = [];
|
||||
foreach ($spent as $currency) {
|
||||
$currencyId = $currency['currency_id'];
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$destinationId = $journal['destination_account_id'];
|
||||
$key = sprintf('%d-%d', $destinationId, $currency['currency_id']);
|
||||
$result[$key] = $result[$key] ?? [
|
||||
'transactions' => 0,
|
||||
'sum' => '0',
|
||||
'avg' => '0',
|
||||
'avg_float' => 0,
|
||||
'destination_account_name' => $journal['destination_account_name'],
|
||||
'destination_account_id' => $journal['destination_account_id'],
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'currency_name' => $currency['currency_name'],
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_decimal_places' => $currency['currency_decimal_places'],
|
||||
];
|
||||
$result[$key]['transactions']++;
|
||||
$result[$key]['sum'] = bcadd($journal['amount'], $result[$key]['sum']);
|
||||
$result[$key]['avg'] = bcdiv($result[$key]['sum'], (string)$result[$key]['transactions']);
|
||||
$result[$key]['avg_float'] = (float)$result[$key]['avg'];
|
||||
}
|
||||
}
|
||||
}
|
||||
// sort by amount_float
|
||||
// sort temp array by amount.
|
||||
$amounts = array_column($result, 'avg_float');
|
||||
array_multisort($amounts, SORT_ASC, $result);
|
||||
|
||||
try {
|
||||
$result = view('reports.category.partials.avg-expenses', compact('result'))->render();
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (Throwable $e) {
|
||||
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
|
||||
$result = sprintf('Could not render view: %s', $e->getMessage());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
public function avgIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
|
||||
{
|
||||
$spent = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
|
||||
$result = [];
|
||||
foreach ($spent as $currency) {
|
||||
$currencyId = $currency['currency_id'];
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$sourceId = $journal['source_account_id'];
|
||||
$key = sprintf('%d-%d', $sourceId, $currency['currency_id']);
|
||||
$result[$key] = $result[$key] ?? [
|
||||
'transactions' => 0,
|
||||
'sum' => '0',
|
||||
'avg' => '0',
|
||||
'avg_float' => 0,
|
||||
'source_account_name' => $journal['source_account_name'],
|
||||
'source_account_id' => $journal['source_account_id'],
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'currency_name' => $currency['currency_name'],
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_decimal_places' => $currency['currency_decimal_places'],
|
||||
];
|
||||
$result[$key]['transactions']++;
|
||||
$result[$key]['sum'] = bcadd($journal['amount'], $result[$key]['sum']);
|
||||
$result[$key]['avg'] = bcdiv($result[$key]['sum'], (string)$result[$key]['transactions']);
|
||||
$result[$key]['avg_float'] = (float)$result[$key]['avg'];
|
||||
}
|
||||
}
|
||||
}
|
||||
// sort by amount_float
|
||||
// sort temp array by amount.
|
||||
$amounts = array_column($result, 'avg_float');
|
||||
array_multisort($amounts, SORT_DESC, $result);
|
||||
|
||||
try {
|
||||
$result = view('reports.category.partials.avg-income', compact('result'))->render();
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (Throwable $e) {
|
||||
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
|
||||
$result = sprintf('Could not render view: %s', $e->getMessage());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user