From cf4adae604b617270970cee62ac88b8eb7843fc9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 1 Feb 2020 15:54:26 +0100 Subject: [PATCH] Refactor category chart code. --- .../Controllers/Chart/CategoryController.php | 90 ++++---- .../Controllers/Chart/CategoryController.php | 213 ++++++++---------- .../Controllers/Report/BudgetController.php | 16 +- .../Controllers/Report/CategoryController.php | 140 ++---------- .../Category/NoCategoryRepository.php | 30 ++- 5 files changed, 202 insertions(+), 287 deletions(-) diff --git a/app/Api/V1/Controllers/Chart/CategoryController.php b/app/Api/V1/Controllers/Chart/CategoryController.php index 01763fff93..ce26a0b469 100644 --- a/app/Api/V1/Controllers/Chart/CategoryController.php +++ b/app/Api/V1/Controllers/Chart/CategoryController.php @@ -90,12 +90,12 @@ class CategoryController extends Controller $tempData = []; $spentWith = $this->opsRepository->listExpenses($start, $end); $earnedWith = $this->opsRepository->listIncome($start, $end); - $spentWithout = $this->noCatRepository->listExpenses($start, $end); - $earnedWithout = $this->noCatRepository->listIncome($start, $end); + $spentWithout = $this->noCatRepository->listExpenses($start, $end); // refactored + $earnedWithout = $this->noCatRepository->listIncome($start, $end); // refactored $categories = []; - foreach ([$spentWith, $earnedWith] as $set) { + foreach ([$spentWith, $earnedWith, $spentWithout, $earnedWithout] as $set) { foreach ($set as $currency) { foreach ($currency['categories'] as $category) { $categories[] = $category['name']; @@ -141,48 +141,48 @@ class CategoryController extends Controller } } - foreach ([$spentWithout, $earnedWithout] as $set) { - foreach ($set as $currency) { - $inKey = sprintf('%d-i', $currency['currency_id']); - $outKey = sprintf('%d-e', $currency['currency_id']); - $categories[] = (string)trans('firefly.no_category'); - // make data arrays if not yet present. - $tempData[$inKey] = $tempData[$inKey] ?? [ - 'currency_id' => $currency['currency_id'], - 'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]), - 'currency_code' => $currency['currency_code'], - 'currency_symbol' => $currency['currency_symbol'], - 'currency_decimal_places' => $currency['currency_decimal_places'], - 'type' => 'bar', // line, area or bar - 'yAxisID' => 0, // 0, 1, 2 - 'entries' => [ - // per category: - // "category" => 5, - ], - ]; - $tempData[$outKey] = $tempData[$outKey] ?? [ - 'currency_id' => $currency['currency_id'], - 'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]), - 'currency_code' => $currency['currency_code'], - 'currency_symbol' => $currency['currency_symbol'], - 'currency_decimal_places' => $currency['currency_decimal_places'], - 'type' => 'bar', // line, area or bar - 'yAxisID' => 0, // 0, 1, 2 - 'entries' => [ - // per category: - // "category" => 5, - ], - ]; - foreach ($currency['transaction_journals'] as $journal) { - // is it expense or income? - $letter = -1 === bccomp($journal['amount'], '0') ? 'e' : 'i'; - $currentKey = sprintf('%d-%s', $currency['currency_id'], $letter); - $name = (string)trans('firefly.no_category'); - $tempData[$currentKey]['entries'][$name] = $tempData[$currentKey]['entries'][$name] ?? '0'; - $tempData[$currentKey]['entries'][$name] = bcadd($tempData[$currentKey]['entries'][$name], $journal['amount']); - } - } - } +// foreach ([] as $set) { +// foreach ($set as $currency) { +// $inKey = sprintf('%d-i', $currency['currency_id']); +// $outKey = sprintf('%d-e', $currency['currency_id']); +// $categories[] = (string)trans('firefly.no_category'); +// // make data arrays if not yet present. +// $tempData[$inKey] = $tempData[$inKey] ?? [ +// 'currency_id' => $currency['currency_id'], +// 'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]), +// 'currency_code' => $currency['currency_code'], +// 'currency_symbol' => $currency['currency_symbol'], +// 'currency_decimal_places' => $currency['currency_decimal_places'], +// 'type' => 'bar', // line, area or bar +// 'yAxisID' => 0, // 0, 1, 2 +// 'entries' => [ +// // per category: +// // "category" => 5, +// ], +// ]; +// $tempData[$outKey] = $tempData[$outKey] ?? [ +// 'currency_id' => $currency['currency_id'], +// 'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]), +// 'currency_code' => $currency['currency_code'], +// 'currency_symbol' => $currency['currency_symbol'], +// 'currency_decimal_places' => $currency['currency_decimal_places'], +// 'type' => 'bar', // line, area or bar +// 'yAxisID' => 0, // 0, 1, 2 +// 'entries' => [ +// // per category: +// // "category" => 5, +// ], +// ]; +// foreach ($currency['transaction_journals'] as $journal) { +// // is it expense or income? +// $letter = -1 === bccomp($journal['amount'], '0') ? 'e' : 'i'; +// $currentKey = sprintf('%d-%s', $currency['currency_id'], $letter); +// $name = (string)trans('firefly.no_category'); +// $tempData[$currentKey]['entries'][$name] = $tempData[$currentKey]['entries'][$name] ?? '0'; +// $tempData[$currentKey]['entries'][$name] = bcadd($tempData[$currentKey]['entries'][$name], $journal['amount']); +// } +// } +// } // re-sort every spent array and add 0 for missing entries. foreach ($tempData as $index => $set) { diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index d5e73c1ea1..21ae827a07 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -134,7 +134,9 @@ class CategoryController extends Controller $currencies = []; $tempData = []; $categories = $repository->getCategories(); - $accounts = $accountRepository->getAccountsByType([AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::ASSET, AccountType::DEFAULT]); + $accounts = $accountRepository->getAccountsByType( + [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::ASSET, AccountType::DEFAULT] + ); /** @var Category $category */ foreach ($categories as $category) { @@ -229,68 +231,8 @@ class CategoryController extends Controller if ($cache->has()) { return response()->json($cache->get());// @codeCoverageIgnore } + $data = $this->reportPeriodChart($accounts, $start, $end, $category); - /** @var OperationsRepositoryInterface $opsRepository */ - $opsRepository = app(OperationsRepositoryInterface::class); - - - // this gives us all currencies - $collection = new Collection([$category]); - $expenses = $opsRepository->listExpenses($start, $end, null, $collection); - $income = $opsRepository->listIncome($start, $end, null, $collection); - $currencies = array_unique(array_merge(array_keys($income), array_keys($expenses))); - $periods = app('navigation')->listOfPeriods($start, $end); - $format = app('navigation')->preferredCarbonLocalizedFormat($start, $end); - $chartData = []; - // make empty data array: - // double foreach (bad) to make empty array: - foreach ($currencies as $currencyId) { - $currencyInfo = $expenses[$currencyId] ?? $income[$currencyId]; - $outKey = sprintf('%d-out', $currencyId); - $inKey = sprintf('%d-in', $currencyId); - $chartData[$outKey] - = [ - 'label' => sprintf('%s (%s)', (string)trans('firefly.spent'), $currencyInfo['currency_name']), - 'entries' => [], - 'type' => 'bar', - 'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red - ]; - - $chartData[$inKey] - = [ - 'label' => sprintf('%s (%s)', (string)trans('firefly.earned'), $currencyInfo['currency_name']), - 'entries' => [], - 'type' => 'bar', - 'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green - ]; - - - // loop empty periods: - foreach (array_keys($periods) as $period) { - $label = $periods[$period]; - $chartData[$outKey]['entries'][$label] = '0'; - $chartData[$inKey]['entries'][$label] = '0'; - } - // loop income and expenses for this category.: - $outSet = $expenses[$currencyId]['categories'][$category->id] ?? ['transaction_journals' => []]; - foreach ($outSet['transaction_journals'] as $journal) { - $amount = app('steam')->positive($journal['amount']); - $date = $journal['date']->formatLocalized($format); - $chartData[$outKey]['entries'][$date] = $chartData[$outKey]['entries'][$date] ?? '0'; - - $chartData[$outKey]['entries'][$date] = bcadd($amount, $chartData[$outKey]['entries'][$date]); - } - - $inSet = $income[$currencyId]['categories'][$category->id] ?? ['transaction_journals' => []]; - foreach ($inSet['transaction_journals'] as $journal) { - $amount = app('steam')->positive($journal['amount']); - $date = $journal['date']->formatLocalized($format); - $chartData[$inKey]['entries'][$date] = $chartData[$inKey]['entries'][$date] ?? '0'; - $chartData[$inKey]['entries'][$date] = bcadd($amount, $chartData[$inKey]['entries'][$date]); - } - } - - $data = $this->generator->multiSet($chartData); $cache->store($data); return response()->json($data); @@ -317,64 +259,8 @@ class CategoryController extends Controller if ($cache->has()) { return response()->json($cache->get()); // @codeCoverageIgnore } + $data = $this->reportPeriodChart($accounts, $start, $end, null); - /** @var NoCategoryRepositoryInterface $noCatRepository */ - $noCatRepository = app(NoCategoryRepositoryInterface::class); - - // this gives us all currencies - $expenses = $noCatRepository->listExpenses($start, $end, $accounts); - $income = $noCatRepository->listIncome($start, $end, $accounts); - $currencies = array_unique(array_merge(array_keys($income), array_keys($expenses))); - $periods = app('navigation')->listOfPeriods($start, $end); - $format = app('navigation')->preferredCarbonLocalizedFormat($start, $end); - $chartData = []; - // make empty data array: - // double foreach (bad) to make empty array: - foreach ($currencies as $currencyId) { - $currencyInfo = $expenses[$currencyId] ?? $income[$currencyId]; - $outKey = sprintf('%d-out', $currencyId); - $inKey = sprintf('%d-in', $currencyId); - - $chartData[$outKey] - = [ - 'label' => sprintf('%s (%s)', (string)trans('firefly.spent'), $currencyInfo['currency_name']), - 'entries' => [], - 'type' => 'bar', - 'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red - ]; - - $chartData[$inKey] - = [ - 'label' => sprintf('%s (%s)', (string)trans('firefly.earned'), $currencyInfo['currency_name']), - 'entries' => [], - 'type' => 'bar', - 'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green - ]; - - // loop empty periods: - foreach (array_keys($periods) as $period) { - $label = $periods[$period]; - $chartData[$outKey]['entries'][$label] = '0'; - $chartData[$inKey]['entries'][$label] = '0'; - } - // loop income and expenses: - $outSet = $expenses[$currencyId] ?? ['transaction_journals' => []]; - foreach ($outSet['transaction_journals'] as $journal) { - $amount = app('steam')->positive($journal['amount']); - $date = $journal['date']->formatLocalized($format); - $chartData[$outKey]['entries'][$date] = $chartData[$outKey]['entries'][$date] ?? '0'; - $chartData[$outKey]['entries'][$date] = bcadd($amount, $chartData[$outKey]['entries'][$date]); - } - - $inSet = $income[$currencyId] ?? ['transaction_journals' => []]; - foreach ($inSet['transaction_journals'] as $journal) { - $amount = app('steam')->positive($journal['amount']); - $date = $journal['date']->formatLocalized($format); - $chartData[$inKey]['entries'][$date] = $chartData[$inKey]['entries'][$date] ?? '0'; - $chartData[$inKey]['entries'][$date] = bcadd($amount, $chartData[$inKey]['entries'][$date]); - } - } - $data = $this->generator->multiSet($chartData); $cache->store($data); return response()->json($data); @@ -436,4 +322,93 @@ class CategoryController extends Controller return $carbon; } + + /** + * Generate report chart for either with or without category. + * + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * @param Category $category + * + * @return array + */ + private function reportPeriodChart(Collection $accounts, Carbon $start, Carbon $end, ?Category $category): array + { + $income = []; + $expenses = []; + $categoryId = 0; + if (null === $category) { + /** @var NoCategoryRepositoryInterface $noCatRepository */ + $noCatRepository = app(NoCategoryRepositoryInterface::class); + + // this gives us all currencies + $expenses = $noCatRepository->listExpenses($start, $end, $accounts); + $income = $noCatRepository->listIncome($start, $end, $accounts); + } + + if (null !== $category) { + /** @var OperationsRepositoryInterface $opsRepository */ + $opsRepository = app(OperationsRepositoryInterface::class); + $categoryId = (int)$category->id; + // this gives us all currencies + $collection = new Collection([$category]); + $expenses = $opsRepository->listExpenses($start, $end, null, $collection); + $income = $opsRepository->listIncome($start, $end, null, $collection); + + } + $currencies = array_unique(array_merge(array_keys($income), array_keys($expenses))); + $periods = app('navigation')->listOfPeriods($start, $end); + $format = app('navigation')->preferredCarbonLocalizedFormat($start, $end); + $chartData = []; + // make empty data array: + // double foreach (bad) to make empty array: + foreach ($currencies as $currencyId) { + $currencyInfo = $expenses[$currencyId] ?? $income[$currencyId]; + $outKey = sprintf('%d-out', $currencyId); + $inKey = sprintf('%d-in', $currencyId); + $chartData[$outKey] + = [ + 'label' => sprintf('%s (%s)', (string)trans('firefly.spent'), $currencyInfo['currency_name']), + 'entries' => [], + 'type' => 'bar', + 'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red + ]; + + $chartData[$inKey] + = [ + 'label' => sprintf('%s (%s)', (string)trans('firefly.earned'), $currencyInfo['currency_name']), + 'entries' => [], + 'type' => 'bar', + 'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green + ]; + + + // loop empty periods: + foreach (array_keys($periods) as $period) { + $label = $periods[$period]; + $chartData[$outKey]['entries'][$label] = '0'; + $chartData[$inKey]['entries'][$label] = '0'; + } + // loop income and expenses for this category.: + $outSet = $expenses[$currencyId]['categories'][$categoryId] ?? ['transaction_journals' => []]; + foreach ($outSet['transaction_journals'] as $journal) { + $amount = app('steam')->positive($journal['amount']); + $date = $journal['date']->formatLocalized($format); + $chartData[$outKey]['entries'][$date] = $chartData[$outKey]['entries'][$date] ?? '0'; + + $chartData[$outKey]['entries'][$date] = bcadd($amount, $chartData[$outKey]['entries'][$date]); + } + + $inSet = $income[$currencyId]['categories'][$categoryId] ?? ['transaction_journals' => []]; + foreach ($inSet['transaction_journals'] as $journal) { + $amount = app('steam')->positive($journal['amount']); + $date = $journal['date']->formatLocalized($format); + $chartData[$inKey]['entries'][$date] = $chartData[$inKey]['entries'][$date] ?? '0'; + $chartData[$inKey]['entries'][$date] = bcadd($amount, $chartData[$inKey]['entries'][$date]); + } + } + + return $this->generator->multiSet($chartData); + } } diff --git a/app/Http/Controllers/Report/BudgetController.php b/app/Http/Controllers/Report/BudgetController.php index 94a8bcdcc7..7ed503d63a 100644 --- a/app/Http/Controllers/Report/BudgetController.php +++ b/app/Http/Controllers/Report/BudgetController.php @@ -300,7 +300,6 @@ class BudgetController extends Controller $report[$budgetId]['currencies'][$currencyId]['sum_pct'] = $pct; } } - return view('reports.budget.partials.budgets', compact('sums', 'report')); } @@ -381,6 +380,7 @@ class BudgetController extends Controller } } + // add no budget info. $report['budgets'][0] = $report['budgets'][0] ?? [ 'budget_id' => null, @@ -408,8 +408,19 @@ class BudgetController extends Controller ]; $report['sums'][$noBudgetEntry['currency_id']]['spent'] = bcadd($report['sums'][$noBudgetEntry['currency_id']]['spent'] ?? '0', $noBudgetEntry['sum']); - } + // append currency info because it may be missing: + $report['sums'][$noBudgetEntry['currency_id']]['currency_id'] = (int)($noBudgetEntry['currency_id'] ?? $defaultCurrency->id); + $report['sums'][$noBudgetEntry['currency_id']]['currency_code'] = $noBudgetEntry['currency_code'] ?? $defaultCurrency->code; + $report['sums'][$noBudgetEntry['currency_id']]['currency_name'] = $noBudgetEntry['currency_name'] ?? $defaultCurrency->name; + $report['sums'][$noBudgetEntry['currency_id']]['currency_symbol'] = $noBudgetEntry['currency_symbol'] ?? $defaultCurrency->symbol; + $report['sums'][$noBudgetEntry['currency_id']]['currency_decimal_places'] = $noBudgetEntry['currency_decimal_places'] ?? $defaultCurrency->decimal_places; + // append other sums because they might be missing: + $report['sums'][$noBudgetEntry['currency_id']]['overspent'] = $report['sums'][$noBudgetEntry['currency_id']]['overspent'] ?? '0'; + $report['sums'][$noBudgetEntry['currency_id']]['left'] = $report['sums'][$noBudgetEntry['currency_id']]['left'] ?? '0'; + $report['sums'][$noBudgetEntry['currency_id']]['budgeted'] = $report['sums'][$noBudgetEntry['currency_id']]['budgeted'] ?? '0'; + + } // make percentages based on total amount. foreach ($report['budgets'] as $budgetId => $data) { foreach ($data['budget_limits'] as $limitId => $entry) { @@ -434,6 +445,7 @@ class BudgetController extends Controller $report['budgets'][$budgetId]['budget_limits'][$limitId]['budgeted_pct'] = $budgetedPct; } } + //var_dump($report);exit; return view('reports.partials.budgets', compact('report'))->render(); } diff --git a/app/Http/Controllers/Report/CategoryController.php b/app/Http/Controllers/Report/CategoryController.php index 554a996465..96b7feb93a 100644 --- a/app/Http/Controllers/Report/CategoryController.php +++ b/app/Http/Controllers/Report/CategoryController.php @@ -517,8 +517,8 @@ class CategoryController extends Controller $data = []; $with = $opsRepository->listExpenses($start, $end, $accounts); $without = $noCatRepos->listExpenses($start, $end, $accounts); - - foreach ($with as $currencyId => $currencyRow) { + foreach([$with, $without] as $set) { + foreach ($set as $currencyId => $currencyRow) { foreach ($currencyRow['categories'] as $categoryId => $categoryRow) { $key = sprintf('%d-%d', $currencyId, $categoryId); $data[$key] = $data[$key] ?? [ @@ -541,26 +541,8 @@ class CategoryController extends Controller } } } - foreach ($without as $currencyId => $currencyRow) { - $key = sprintf('0-%d', $currencyId); - $data[$key] = $data[$key] ?? [ - 'id' => 0, - 'title' => sprintf('%s (%s)', trans('firefly.noCategory'), $currencyRow['currency_name']), - 'currency_id' => $currencyRow['currency_id'], - 'currency_symbol' => $currencyRow['currency_symbol'], - 'currency_name' => $currencyRow['currency_name'], - 'currency_code' => $currencyRow['currency_code'], - 'currency_decimal_places' => $currencyRow['currency_decimal_places'], - 'sum' => '0', - 'entries' => [], - ]; - foreach ($currencyRow['transaction_journals'] as $journalId => $journal) { - $date = $journal['date']->format($format); - $data[$key]['entries'][$date] = $data[$key]['entries'][$date] ?? '0'; - $data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date], $journal['amount']); - $data[$key]['sum'] = bcadd($data[$key]['sum'], $journal['amount']); - } } + $cache->store($data); $report = $data; @@ -621,50 +603,31 @@ class CategoryController extends Controller $data = []; $with = $opsRepository->listIncome($start, $end, $accounts); $without = $noCatRepos->listIncome($start, $end, $accounts); + foreach([$with, $without] as $set) { + foreach ($set as $currencyId => $currencyRow) { + foreach ($currencyRow['categories'] as $categoryId => $categoryRow) { + $key = sprintf('%d-%d', $currencyId, $categoryId); + $data[$key] = $data[$key] ?? [ + 'id' => $categoryRow['id'], + 'title' => sprintf('%s (%s)', $categoryRow['name'], $currencyRow['currency_name']), + 'currency_id' => $currencyRow['currency_id'], + 'currency_symbol' => $currencyRow['currency_symbol'], + 'currency_name' => $currencyRow['currency_name'], + 'currency_code' => $currencyRow['currency_code'], + 'currency_decimal_places' => $currencyRow['currency_decimal_places'], + 'sum' => '0', + 'entries' => [], - foreach ($with as $currencyId => $currencyRow) { - foreach ($currencyRow['categories'] as $categoryId => $categoryRow) { - $key = sprintf('%d-%d', $currencyId, $categoryId); - $data[$key] = $data[$key] ?? [ - 'id' => $categoryRow['id'], - 'title' => sprintf('%s (%s)', $categoryRow['name'], $currencyRow['currency_name']), - 'currency_id' => $currencyRow['currency_id'], - 'currency_symbol' => $currencyRow['currency_symbol'], - 'currency_name' => $currencyRow['currency_name'], - 'currency_code' => $currencyRow['currency_code'], - 'currency_decimal_places' => $currencyRow['currency_decimal_places'], - 'sum' => '0', - 'entries' => [], - - ]; - foreach ($categoryRow['transaction_journals'] as $journalId => $journal) { - $date = $journal['date']->format($format); - $data[$key]['entries'][$date] = $data[$key]['entries'][$date] ?? '0'; - $data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date], $journal['amount']); - $data[$key]['sum'] = bcadd($data[$key]['sum'], $journal['amount']); + ]; + foreach ($categoryRow['transaction_journals'] as $journalId => $journal) { + $date = $journal['date']->format($format); + $data[$key]['entries'][$date] = $data[$key]['entries'][$date] ?? '0'; + $data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date], $journal['amount']); + $data[$key]['sum'] = bcadd($data[$key]['sum'], $journal['amount']); + } } } } - foreach ($without as $currencyId => $currencyRow) { - $key = sprintf('0-%d', $currencyId); - $data[$key] = $data[$key] ?? [ - 'id' => 0, - 'title' => sprintf('%s (%s)', trans('firefly.noCategory'), $currencyRow['currency_name']), - 'currency_id' => $currencyRow['currency_id'], - 'currency_symbol' => $currencyRow['currency_symbol'], - 'currency_name' => $currencyRow['currency_name'], - 'currency_code' => $currencyRow['currency_code'], - 'currency_decimal_places' => $currencyRow['currency_decimal_places'], - 'sum' => '0', - 'entries' => [], - ]; - foreach ($currencyRow['transaction_journals'] as $journalId => $journal) { - $date = $journal['date']->format($format); - $data[$key]['entries'][$date] = $data[$key]['entries'][$date] ?? '0'; - $data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date], $journal['amount']); - $data[$key]['sum'] = bcadd($data[$key]['sum'], $journal['amount']); - } - } $cache->store($data); $report = $data; @@ -725,8 +688,7 @@ class CategoryController extends Controller ]; // needs four for-each loops. - // TODO improve this. - foreach ([$earnedWith, $spentWith] as $data) { + foreach ([$earnedWith, $spentWith, $earnedWithout, $spentWithout] as $data) { foreach ($data as $currencyId => $currencyRow) { $report['sums'][$currencyId] = $report['sums'][$currencyId] ?? [ 'spent' => '0', @@ -781,58 +743,6 @@ class CategoryController extends Controller } } } - foreach ([$earnedWithout, $spentWithout] as $data) { - foreach ($data as $currencyId => $currencyRow) { - $report['sums'][$currencyId] = $report['sums'][$currencyId] ?? [ - 'spent' => '0', - 'earned' => '0', - 'sum' => '0', - 'currency_id' => $currencyRow['currency_id'], - 'currency_symbol' => $currencyRow['currency_symbol'], - 'currency_name' => $currencyRow['currency_name'], - 'currency_code' => $currencyRow['currency_code'], - 'currency_decimal_places' => $currencyRow['currency_decimal_places'], - ]; - $key = sprintf('%s-0', $currencyId); - $report['categories'][$key] = $report['categories'][$key] ?? [ - 'id' => 0, - 'title' => sprintf('%s (%s)', trans('firefly.noCategory'), $currencyRow['currency_name']), - 'currency_id' => $currencyRow['currency_id'], - 'currency_symbol' => $currencyRow['currency_symbol'], - 'currency_name' => $currencyRow['currency_name'], - 'currency_code' => $currencyRow['currency_code'], - 'currency_decimal_places' => $currencyRow['currency_decimal_places'], - 'spent' => '0', - 'earned' => '0', - 'sum' => '0', - ]; - // loop journals: - foreach ($currencyRow['transaction_journals'] as $journal) { - // sum of all - $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']; - // sum of 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']); - // total spent in no category - $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 - $report['categories'][$key]['earned'] = 1 === bccomp($journal['amount'], '0') ? bcadd( - $report['categories'][$key]['earned'], $journal['amount'] - ) : $report['categories'][$key]['earned']; - } - } - } // @codeCoverageIgnoreStart try { diff --git a/app/Repositories/Category/NoCategoryRepository.php b/app/Repositories/Category/NoCategoryRepository.php index 4f0c1c9fb2..8fd1845fa6 100644 --- a/app/Repositories/Category/NoCategoryRepository.php +++ b/app/Repositories/Category/NoCategoryRepository.php @@ -76,17 +76,25 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface foreach ($journals as $journal) { $currencyId = (int)$journal['currency_id']; $array[$currencyId] = $array[$currencyId] ?? [ - 'transaction_journals' => [], + 'categories' => [], 'currency_id' => $currencyId, 'currency_name' => $journal['currency_name'], 'currency_symbol' => $journal['currency_symbol'], 'currency_code' => $journal['currency_code'], 'currency_decimal_places' => $journal['currency_decimal_places'], ]; + // info about the non-existent category: + $array[$currencyId]['categories'][0] = $array[$currencyId]['categories'][0] ?? [ + 'id' => 0, + 'name' => (string)trans('firefly.noCategory'), + 'transaction_journals' => [], + ]; + // add journal to array: // only a subset of the fields. - $journalId = (int)$journal['transaction_journal_id']; - $array[$currencyId]['transaction_journals'][$journalId] = [ + $journalId = (int)$journal['transaction_journal_id']; + $array[$currencyId]['categories'][0]['transaction_journals'][$journalId] + = [ 'amount' => app('steam')->negative($journal['amount']), 'date' => $journal['date'], ]; @@ -121,17 +129,27 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface foreach ($journals as $journal) { $currencyId = (int)$journal['currency_id']; $array[$currencyId] = $array[$currencyId] ?? [ - 'transaction_journals' => [], + 'categories' => [], 'currency_id' => $currencyId, 'currency_name' => $journal['currency_name'], 'currency_symbol' => $journal['currency_symbol'], 'currency_code' => $journal['currency_code'], 'currency_decimal_places' => $journal['currency_decimal_places'], ]; + + // info about the non-existent category: + $array[$currencyId]['categories'][0] = $array[$currencyId]['categories'][0] ?? [ + 'id' => 0, + 'name' => (string)trans('firefly.noCategory'), + 'transaction_journals' => [], + ]; + + // add journal to array: // only a subset of the fields. - $journalId = (int)$journal['transaction_journal_id']; - $array[$currencyId]['transaction_journals'][$journalId] = [ + $journalId = (int)$journal['transaction_journal_id']; + $array[$currencyId]['categories'][0]['transaction_journals'][$journalId] + = [ 'amount' => app('steam')->positive($journal['amount']), 'date' => $journal['date'], ];