diff --git a/app/Http/Controllers/Report/CategoryController.php b/app/Http/Controllers/Report/CategoryController.php index 045726a30c..e1bcec6fdc 100644 --- a/app/Http/Controllers/Report/CategoryController.php +++ b/app/Http/Controllers/Report/CategoryController.php @@ -25,7 +25,6 @@ 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; @@ -63,6 +62,106 @@ 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 accounts(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 = []; + $sums = []; + /** @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']; + $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'], + 'spent_sum' => '0', + 'earned_sum' => '0', + 'total_sum' => '0', + ]; + 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'], + 'spent' => '0', + 'earned' => '0', + 'sum' => '0', + ]; + $report[$sourceAccountId]['currencies'][$currencyId]['spent'] = bcadd( + $report[$sourceAccountId]['currencies'][$currencyId]['spent'], $journal['amount'] + ); + $report[$sourceAccountId]['currencies'][$currencyId]['sum'] = bcadd( + $report[$sourceAccountId]['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']); + } + } + } + + // loop income. + 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'], + 'spent_sum' => '0', + 'earned_sum' => '0', + 'total_sum' => '0', + ]; + foreach ($currency['categories'] as $category) { + foreach ($category['transaction_journals'] as $journal) { + $destinationAccountId = $journal['destination_account_id']; + $report[$destinationAccountId]['currencies'][$currencyId] = $report[$destinationAccountId]['currencies'][$currencyId] ?? [ + 'currency_id' => $currency['currency_id'], + 'currency_symbol' => $currency['currency_symbol'], + 'currency_name' => $currency['currency_name'], + 'currency_decimal_places' => $currency['currency_decimal_places'], + 'spent' => '0', + 'earned' => '0', + 'sum' => '0', + ]; + $report[$destinationAccountId]['currencies'][$currencyId]['earned'] = bcadd( + $report[$destinationAccountId]['currencies'][$currencyId]['earned'], $journal['amount'] + ); + $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']); + } + } + } + + return view('reports.category.partials.accounts', compact('sums', 'report')); + } + /** * Show overview of expenses in category. * @@ -168,62 +267,6 @@ class CategoryController extends Controller return $result; } - /** - * @param Collection $accounts - * @param Collection $categories - * @param Carbon $start - * @param Carbon $end - * - * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View - */ - public function accounts(Collection $accounts, Collection $categories, Carbon $start, Carbon $end) - { - $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories); - $report = []; - $sums = []; - /** @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']; - $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'], - 'sum' => '0', - ]; - 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'], - 'sum' => '0', - ]; - $report[$sourceAccountId]['currencies'][$currencyId]['sum'] = bcadd( - $report[$sourceAccountId]['currencies'][$currencyId]['sum'], $journal['amount'] - ); - $sums[$currencyId]['sum'] = bcadd($sums[$currencyId]['sum'], $journal['amount']); - } - } - } - - return view('reports.category.partials.accounts', compact('sums', 'report')); - } - - /** * Show overview of income in category. * @@ -242,7 +285,7 @@ class CategoryController extends Controller $cache->addProperty('category-period-income-report'); $cache->addProperty($accounts->pluck('id')->toArray()); if ($cache->has()) { - return $cache->get(); // @codeCoverageIgnore + return $cache->get(); // @codeCoverageIgnore } /** @var OperationsRepositoryInterface $opsRepository */ @@ -348,7 +391,7 @@ class CategoryController extends Controller $cache->addProperty('category-report'); $cache->addProperty($accounts->pluck('id')->toArray()); if ($cache->has()) { - return $cache->get(); // @codeCoverageIgnore + return $cache->get(); // @codeCoverageIgnore } /** @var CategoryRepositoryInterface $repository */ @@ -376,8 +419,8 @@ class CategoryController extends Controller foreach ([$earnedWith, $spentWith] as $data) { foreach ($data as $currencyId => $currencyRow) { $report['sums'][$currencyId] = $report['sums'][$currencyId] ?? [ - 'spent' => '0', - 'earned' => '0', + 'spent' => '0', + 'earned' => '0', 'sum' => '0', 'currency_id' => $currencyRow['currency_id'], 'currency_symbol' => $currencyRow['currency_symbol'], @@ -404,16 +447,20 @@ class CategoryController extends Controller // loop journals: foreach ($categoryRow['transaction_journals'] as $journal) { // sum of sums - $report['sums'][$currencyId]['sum'] = bcadd($report['sums'][$currencyId]['sum'], $journal['amount']); + $report['sums'][$currencyId]['sum'] = bcadd($report['sums'][$currencyId]['sum'], $journal['amount']); // sum of spent: - $report['sums'][$currencyId]['spent'] = -1 === bccomp($journal['amount'], '0') ? bcadd($report['sums'][$currencyId]['spent'], $journal['amount']): $report['sums'][$currencyId]['spent']; + $report['sums'][$currencyId]['spent'] = -1 === bccomp($journal['amount'], '0') ? bcadd( + $report['sums'][$currencyId]['spent'], $journal['amount'] + ) : $report['sums'][$currencyId]['spent']; // sum of earned - $report['sums'][$currencyId]['earned'] = 1 === bccomp($journal['amount'], '0') ? bcadd($report['sums'][$currencyId]['earned'], $journal['amount']): $report['sums'][$currencyId]['earned']; + $report['sums'][$currencyId]['earned'] = 1 === bccomp($journal['amount'], '0') ? bcadd( + $report['sums'][$currencyId]['earned'], $journal['amount'] + ) : $report['sums'][$currencyId]['earned']; // sum of category - $report['categories'][$key]['sum'] = bcadd($report['categories'][$key]['sum'], $journal['amount']); + $report['categories'][$key]['sum'] = bcadd($report['categories'][$key]['sum'], $journal['amount']); // total spent in category - $report['categories'][$key]['spent'] = -1 === bccomp($journal['amount'], '0') ? bcadd( + $report['categories'][$key]['spent'] = -1 === bccomp($journal['amount'], '0') ? bcadd( $report['categories'][$key]['spent'], $journal['amount'] ) : $report['categories'][$key]['spent']; // total earned in category @@ -452,17 +499,21 @@ class CategoryController extends Controller // loop journals: foreach ($currencyRow['transaction_journals'] as $journal) { // sum of all - $report['sums'][$currencyId]['sum'] = bcadd($report['sums'][$currencyId]['sum'], $journal['amount']); + $report['sums'][$currencyId]['sum'] = bcadd($report['sums'][$currencyId]['sum'], $journal['amount']); // sum of spent: - $report['sums'][$currencyId]['spent'] = -1 === bccomp($journal['amount'], '0') ? bcadd($report['sums'][$currencyId]['spent'], $journal['amount']): $report['sums'][$currencyId]['spent']; + $report['sums'][$currencyId]['spent'] = -1 === bccomp($journal['amount'], '0') ? bcadd( + $report['sums'][$currencyId]['spent'], $journal['amount'] + ) : $report['sums'][$currencyId]['spent']; // sum of earned - $report['sums'][$currencyId]['earned'] = 1 === bccomp($journal['amount'], '0') ? bcadd($report['sums'][$currencyId]['earned'], $journal['amount']): $report['sums'][$currencyId]['earned']; + $report['sums'][$currencyId]['earned'] = 1 === bccomp($journal['amount'], '0') ? bcadd( + $report['sums'][$currencyId]['earned'], $journal['amount'] + ) : $report['sums'][$currencyId]['earned']; // sum of category - $report['categories'][$key]['sum'] = bcadd($report['categories'][$key]['sum'], $journal['amount']); + $report['categories'][$key]['sum'] = bcadd($report['categories'][$key]['sum'], $journal['amount']); // total spent in no category - $report['categories'][$key]['spent'] = -1 === bccomp($journal['amount'], '0') ? bcadd( + $report['categories'][$key]['spent'] = -1 === bccomp($journal['amount'], '0') ? bcadd( $report['categories'][$key]['spent'], $journal['amount'] ) : $report['categories'][$key]['spent']; // total earned in no category diff --git a/app/Repositories/Category/OperationsRepository.php b/app/Repositories/Category/OperationsRepository.php index d6113215cf..a375fca03f 100644 --- a/app/Repositories/Category/OperationsRepository.php +++ b/app/Repositories/Category/OperationsRepository.php @@ -151,7 +151,7 @@ class OperationsRepository implements OperationsRepositoryInterface if (null === $categories || (null !== $categories && 0 === $categories->count())) { $collector->setCategories($this->getCategories()); } - $collector->withCategoryInformation(); + $collector->withCategoryInformation()->withAccountInformation(); $journals = $collector->getExtractedJournals(); $array = []; @@ -188,8 +188,10 @@ class OperationsRepository implements OperationsRepositoryInterface $array[$currencyId]['categories'][$categoryId]['transaction_journals'][$journalId] = [ - 'amount' => app('steam')->positive($journal['amount']), - 'date' => $journal['date'], + 'amount' => app('steam')->positive($journal['amount']), + 'date' => $journal['date'], + 'source_account_id' => $journal['source_account_id'], + 'destination_account_id' => $journal['destination_account_id'], ]; } diff --git a/public/v1/js/ff/reports/category/month.js b/public/v1/js/ff/reports/category/month.js index 7b12946e1e..413d93ff79 100644 --- a/public/v1/js/ff/reports/category/month.js +++ b/public/v1/js/ff/reports/category/month.js @@ -45,6 +45,7 @@ function drawChart() { "use strict"; loadAjaxPartial('accountsHolder', accountsUri); + loadAjaxPartial('categoriesHolder', categoriesUri); // month view: diff --git a/resources/views/v1/reports/category/month.twig b/resources/views/v1/reports/category/month.twig index 9f4d2d3b90..6a31812935 100644 --- a/resources/views/v1/reports/category/month.twig +++ b/resources/views/v1/reports/category/month.twig @@ -230,7 +230,7 @@ var accountsUri = '{{ route('report-data.category.accounts', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd')]) }}'; - {#var categoriesUri = '{{ route('report-data.category.categories', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd')]) }}';#} + var categoriesUri = '{{ route('report-data.category.categories', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd')]) }}'; {#var accountPerCategoryUri = '{{ route('report-data.category.account-per-category', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd')]) }}';#} {#var avgExpensesUri = '{{ route('report-data.category.avg-expenses', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd')]) }}';#} {#var topExpensesUri = '{{ route('report-data.category.top-expenses', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd')]) }}';#} diff --git a/resources/views/v1/reports/category/partials/accounts.twig b/resources/views/v1/reports/category/partials/accounts.twig index b85fe8cf11..5fd35138ef 100644 --- a/resources/views/v1/reports/category/partials/accounts.twig +++ b/resources/views/v1/reports/category/partials/accounts.twig @@ -3,6 +3,8 @@