diff --git a/app/Factory/TransactionGroupFactory.php b/app/Factory/TransactionGroupFactory.php index 5a6135c01e..f99278a20a 100644 --- a/app/Factory/TransactionGroupFactory.php +++ b/app/Factory/TransactionGroupFactory.php @@ -23,9 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Factory; -use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\TransactionGroup; -use FireflyIII\Models\TransactionJournal; use FireflyIII\User; /** @@ -60,10 +58,8 @@ class TransactionGroupFactory $collection = $this->journalFactory->create($data); $title = $data['group_title'] ?? null; $title = '' === $title ? null : $title; - /** @var TransactionJournal $first */ - $first = $collection->first(); - $group = new TransactionGroup; - $group->user()->associate($first->user); + $group = new TransactionGroup; + $group->user()->associate($this->user); $group->title = $title; $group->save(); diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php index 03ee0fed19..83fbbb03ab 100644 --- a/app/Factory/TransactionJournalFactory.php +++ b/app/Factory/TransactionJournalFactory.php @@ -143,6 +143,9 @@ class TransactionJournalFactory if (null !== $journal) { $collection->push($journal); } + if(null === $journal) { + Log::error('The createJournal() method returned NULL. This may indicate an error.'); + } } return $collection; @@ -247,6 +250,7 @@ class TransactionJournalFactory $destinationAccount = $this->getAccount($type->type, 'destination', (int)$row['destination_id'], $row['destination_name']); // @codeCoverageIgnoreStart } catch (FireflyException $e) { + Log::error('Could not validate source or destination.'); Log::error($e->getMessage()); return null; diff --git a/app/Helpers/Report/BalanceReportHelper.php b/app/Helpers/Report/BalanceReportHelper.php index 82c87fb459..64d049983f 100644 --- a/app/Helpers/Report/BalanceReportHelper.php +++ b/app/Helpers/Report/BalanceReportHelper.php @@ -27,7 +27,11 @@ use FireflyIII\Helpers\Collection\Balance; use FireflyIII\Helpers\Collection\BalanceEntry; use FireflyIII\Helpers\Collection\BalanceHeader; use FireflyIII\Helpers\Collection\BalanceLine; +use FireflyIII\Helpers\Collector\GroupCollectorInterface; +use FireflyIII\Models\Account; +use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; +use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use Illuminate\Support\Collection; use Log; @@ -65,11 +69,91 @@ class BalanceReportHelper implements BalanceReportHelperInterface * @param Carbon $start * @param Carbon $end * - * @return Balance + * @return array */ - public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): Balance + public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): array { Log::debug('Start of balance report'); + $report = [ + 'budgets' => [], + 'accounts' => [], + ]; + /** @var Account $account */ + foreach ($accounts as $account) { + $report['accounts'][$account->id] = [ + 'id' => $account->id, + 'name' => $account->name, + 'iban' => $account->iban, + 'sum' => '0', + ]; + } + + $budgets = $this->budgetRepository->getBudgets(); + // per budget, dan per balance line + // of als het in een balance line valt dan daaronder en anders niet + // kruistabel vullen? + + /** @var Budget $budget */ + foreach ($budgets as $budget) { + $budgetId = $budget->id; + $report['budgets'][$budgetId] = [ + 'budget_id' => $budgetId, + 'budget_name' => $budget->name, + 'spent' => [], // per account + 'sums' => [], // per currency + ]; + $spent = []; + /** @var GroupCollectorInterface $collector */ + $collector = app(GroupCollectorInterface::class); + $journals = $collector->setRange($start, $end)->setSourceAccounts($accounts)->setTypes([TransactionType::WITHDRAWAL])->setBudget($budget) + ->getExtractedJournals(); + /** @var array $journal */ + foreach ($journals as $journal) { + $sourceAccount = $journal['source_account_id']; + $currencyId = $journal['currency_id']; + $spent[$sourceAccount] = $spent[$sourceAccount] ?? [ + 'source_account_id' => $sourceAccount, + 'currency_id' => $journal['currency_id'], + 'currency_code' => $journal['currency_code'], + 'currency_name' => $journal['currency_name'], + 'currency_symbol' => $journal['currency_symbol'], + 'currency_decimal_places' => $journal['currency_decimal_places'], + 'spent' => '0', + ]; + $spent[$sourceAccount]['spent'] = bcadd($spent[$sourceAccount]['spent'], $journal['amount']); + + // also fix sum: + $report['sums'][$budgetId][$currencyId] = $report['sums'][$budgetId][$currencyId] ?? [ + 'sum' => '0', + 'currency_id' => $journal['currency_id'], + 'currency_code' => $journal['currency_code'], + 'currency_name' => $journal['currency_name'], + 'currency_symbol' => $journal['currency_symbol'], + 'currency_decimal_places' => $journal['currency_decimal_places'], + ]; + $report['sums'][$budgetId][$currencyId]['sum'] = bcadd($report['sums'][$budgetId][$currencyId]['sum'], $journal['amount']); + $report['accounts'][$sourceAccount]['sum'] = bcadd($report['accounts'][$sourceAccount]['sum'], $journal['amount']); + + // add currency info for account sum + $report['accounts'][$sourceAccount]['currency_id'] = $journal['currency_id']; + $report['accounts'][$sourceAccount]['currency_code'] = $journal['currency_code']; + $report['accounts'][$sourceAccount]['currency_name'] = $journal['currency_name']; + $report['accounts'][$sourceAccount]['currency_symbol'] = $journal['currency_symbol']; + $report['accounts'][$sourceAccount]['currency_decimal_places'] = $journal['currency_decimal_places']; + } + $report['budgets'][$budgetId]['spent'] = $spent; + // get transactions in budget + } + + return $report; + // do sums: + + + echo '
';
+ print_r($report);
+ exit;
+
+
$balance = new Balance;
$header = new BalanceHeader;
$budgetLimits = $this->budgetRepository->getAllBudgetLimits($start, $end);
diff --git a/app/Helpers/Report/BalanceReportHelperInterface.php b/app/Helpers/Report/BalanceReportHelperInterface.php
index 2a18d6abe9..332d898cce 100644
--- a/app/Helpers/Report/BalanceReportHelperInterface.php
+++ b/app/Helpers/Report/BalanceReportHelperInterface.php
@@ -38,7 +38,7 @@ interface BalanceReportHelperInterface
* @param Carbon $start
* @param Carbon $end
*
- * @return Balance
+ * @return array
*/
- public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): Balance;
+ public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): array;
}
diff --git a/app/Http/Controllers/Report/BalanceController.php b/app/Http/Controllers/Report/BalanceController.php
index 0993cf4541..49a1b0748f 100644
--- a/app/Http/Controllers/Report/BalanceController.php
+++ b/app/Http/Controllers/Report/BalanceController.php
@@ -47,7 +47,6 @@ class BalanceController extends Controller
*/
public function general(Collection $accounts, Carbon $start, Carbon $end)
{
-
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
@@ -55,17 +54,19 @@ class BalanceController extends Controller
$cache->addProperty('balance-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
- return $cache->get(); // @codeCoverageIgnore
+ //return $cache->get(); // @codeCoverageIgnore
}
$helper = app(BalanceReportHelperInterface::class);
- $balance = $helper->getBalanceReport($accounts, $start, $end);
- try {
- $result = view('reports.partials.balance', compact('balance'))->render();
+ $report = $helper->getBalanceReport($accounts, $start, $end);
+ // TODO no budget.
+ // TODO sum over account.
+// try {
+ $result = view('reports.partials.balance', compact('report'))->render();
// @codeCoverageIgnoreStart
- } catch (Throwable $e) {
- Log::debug(sprintf('Could not render reports.partials.balance: %s', $e->getMessage()));
- $result = 'Could not render view.';
- }
+// } catch (Throwable $e) {
+// Log::debug(sprintf('Could not render reports.partials.balance: %s', $e->getMessage()));
+// $result = 'Could not render view.';
+// }
// @codeCoverageIgnoreEnd
$cache->store($result);
diff --git a/app/Http/Controllers/Report/CategoryController.php b/app/Http/Controllers/Report/CategoryController.php
index 1d9d5629db..67339b4cb6 100644
--- a/app/Http/Controllers/Report/CategoryController.php
+++ b/app/Http/Controllers/Report/CategoryController.php
@@ -171,31 +171,60 @@ class CategoryController extends Controller
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
- $report = [];
+ $report = [
+ 'categories' => [],
+ 'sums' => [],
+ ];
/** @var Category $category */
foreach ($categories as $category) {
$spent = $repository->spentInPeriod($category, $accounts, $start, $end);
$earned = $repository->earnedInPeriod($category, $accounts, $start, $end);
- $currencies = array_keys($spent) + array_keys($earned);
+ if (0 === count($spent) && 0 === count($earned)) {
+ continue;
+ }
+ $currencies = array_unique(array_merge(array_keys($spent), array_keys($earned)));
foreach ($currencies as $code) {
- $currencyInfo = $spent[$code] ?? $earned[$code];
- $report[$category->id] = [
- 'name' => sprintf('%s (%s)', $category->name, $code),
- 'spent' => round($spent[$code]['spent'] ?? '0', $currencyInfo['currency_decimal_places']),
- 'earned' => round($earned[$code]['earned'] ?? '0', $currencyInfo['currency_decimal_places']),
+ $currencyInfo = $spent[$code] ?? $earned[$code];
+ $key = sprintf('%s-%s', $category->id, $code);
+ $report['categories'][$key] = [
+ 'name' => $category->name,
+ 'spent' => $spent[$code]['spent'] ?? '0',
+ 'earned' => $earned[$code]['earned'] ?? '0',
'id' => $category->id,
'currency_id' => $currencyInfo['currency_id'],
'currency_code' => $currencyInfo['currency_code'],
'currency_symbol' => $currencyInfo['currency_symbol'],
- 'cyrrency_decimal_places' => $currencyInfo['currency_decimal_places'],
+ 'currency_name' => $currencyInfo['currency_name'],
+ 'currency_decimal_places' => $currencyInfo['currency_decimal_places'],
];
+
}
}
$sum = [];
- foreach ($report as $categoryId => $row) {
- $sum[$categoryId] = (float)$row['spent'];
+ /**
+ * @var string $categoryId
+ * @var array $row
+ */
+ foreach ($report['categories'] as $categoryId => $row) {
+ $sum[$categoryId] = (float)$row['spent'];
+ }
+ array_multisort($sum, SORT_ASC, $report['categories']);
+
+ // get sums:
+ foreach ($report['categories'] as $entry) {
+ $currencyId = $entry['currency_id'];
+ $report['sums'][$currencyId] = $report['sums'][$currencyId] ?? [
+ 'spent' => '0',
+ 'earned' => '0',
+ 'currency_id' => $entry['currency_id'],
+ 'currency_code' => $entry['currency_code'],
+ 'currency_symbol' => $entry['currency_symbol'],
+ 'currency_name' => $entry['currency_name'],
+ 'cyrrency_decimal_places' => $entry['currency_decimal_places'],
+ ];
+ $report['sums'][$currencyId]['spent'] = bcadd($report['sums'][$currencyId]['spent'], $entry['spent']);
+ $report['sums'][$currencyId]['earned'] = bcadd($report['sums'][$currencyId]['earned'], $entry['earned']);
}
- array_multisort($sum, SORT_ASC, $report);
// @codeCoverageIgnoreStart
try {
diff --git a/app/Http/Controllers/Report/ExpenseController.php b/app/Http/Controllers/Report/ExpenseController.php
index ef8e3d4d2a..75dfcdeb70 100644
--- a/app/Http/Controllers/Report/ExpenseController.php
+++ b/app/Http/Controllers/Report/ExpenseController.php
@@ -74,7 +74,6 @@ class ExpenseController extends Controller
* @param Carbon $end
*
* @return string
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function budget(Collection $accounts, Collection $expense, Carbon $start, Carbon $end): string
{
diff --git a/resources/views/v1/reports/partials/balance.twig b/resources/views/v1/reports/partials/balance.twig
index 3bed737ebe..bb2217c1ae 100644
--- a/resources/views/v1/reports/partials/balance.twig
+++ b/resources/views/v1/reports/partials/balance.twig
@@ -1,59 +1,45 @@
| {{ 'budgets'|_ }} | - {% for account in balance.getBalanceHeader.getAccounts %} -{{ account.name }} | +{{ 'budgets'|_ }} | + {% for account in report.accounts %} +{{ account.name }} | {% endfor %} -- {{ 'leftInBudget'|_ }} - | +{{ 'sum'|_ }} | |
|---|---|---|---|---|---|---|
|
- {{ balanceLine.getTitle }}
- {% if balanceLine.getStartdate and balanceLine.getEnddate %}
- - {{ balanceLine.getStartdate.formatLocalized(monthAndDayFormat) }} - — - {{ balanceLine.getEnddate.formatLocalized(monthAndDayFormat) }} - - {% endif %} - |
- - {% if(balanceLine.getBudgetLimit.amount) %} - {{ balanceLine.getBudgetLimit.amount|formatAmount }} - {% else %} - {{ '0'|formatAmount }} - {% endif %} - | - {% else %} -{{ balanceLine.getTitle }} | - {% endif %} - - {% for balanceEntry in balanceLine.getBalanceEntries %} -- {% if balanceEntry.getSpent != 0 %} - {{ (balanceEntry.getSpent)|formatAmountPlain }} - - {% endif %} - {% if balanceEntry.getLeft != 0 %} - {{ (balanceEntry.getLeft)|formatAmountPlain }} - {% endif %} - | - {% endfor %} + {% for budget in report.budgets %} +|||
| + {{ budget.budget_name }} + | + {% for account in report.accounts %} + {% if budget.spent[account.id] %}- {{ balanceLine.leftOfRepetition|formatAmount }} + {{ formatAmountBySymbol(budget.spent[account.id].spent, budget.spent[account.id].currency_symbol, budget.spent[account.id].currency_decimal_places) }} | -+ + | + {% endif %} + {% endfor %} +
+ {% for sum in report.sums[budget.budget_id] %}
+ {{ formatAmountBySymbol(sum.sum, sum.currency_symbol, sum.currency_decimal_places) }} + {% endfor %} + |
+
{% endfor %}
+
+ |||
| {{ 'sum'|_ }} | + {% for account in report.accounts %} +{{ formatAmountBySymbol(account.sum, account.currency_symbol, account.currency_decimal_places) }} | + {% endfor %} +|||||