From 751c1895ca0ee1e079b69629ed9521fb6aa96d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 1 May 2023 22:00:32 +0200 Subject: [PATCH] RVRNEXT-7 calculate balances in CommunityController --- src/Controller/CommunityController.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index 1a853e9..30b90be 100644 --- a/src/Controller/CommunityController.php +++ b/src/Controller/CommunityController.php @@ -1,6 +1,7 @@ loadRelationsFromDb($community, false, ['main_currency']); + $balanceCalculator = new BalanceCalculator($community); + $debts = $balanceCalculator->calculate(); + $debtUsers = []; + $debtBalance = 0.0; + $outstandingUsers = []; + $outstandingBalance = 0.0; + foreach ($debts as $debt) { + if ($debt['payer']->getId() === \Container::$request->user()->getUniqueId()) { + $debtBalance += $debt['amount']; + $debtUsers[] = $debt; + } + if ($debt['payee']->getId() === \Container::$request->user()->getUniqueId()) { + $outstandingBalance += $debt['amount']; + $outstandingUsers[] = $debt; + } + } + $balance = $outstandingBalance - $debtBalance; + return new HtmlContent('communities/community', [ 'community' => $community, 'members' => $this->getMembers($community), 'currencies' => $this->getCurrencies($community), 'upcomingEvents' => [], + 'debtUsers' => $debtUsers, + 'debtBalance' => $debtBalance, + 'outstandingUsers' => $outstandingUsers, + 'outstandingBalance' => $outstandingBalance, + 'balance' => $balance, 'editPermission' => $ownCommunityMember->getOwner() ]); }