diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index 159dbde..af02add 100644 --- a/src/Controller/CommunityController.php +++ b/src/Controller/CommunityController.php @@ -59,32 +59,21 @@ class CommunityController implements IAuthenticationRequired \Container::$persistentDataManager->loadRelationsFromDb($community, false, ['main_currency']); - $balanceCalculator = new BalanceCalculator($community); - $debts = $balanceCalculator->calculate(); - $debtItems = []; - $debtBalance = 0.0; - $outstandingItems = []; - $outstandingBalance = 0.0; - foreach ($debts as $debt) { - if ($debt['payer']->getId() === \Container::$request->user()->getUniqueId()) { - $debtBalance += $debt['amount']; - $debtItems[] = $debt; - } - if ($debt['payee']->getId() === \Container::$request->user()->getUniqueId()) { - $outstandingBalance += $debt['amount']; - $outstandingItems[] = $debt; - } - } - $balance = $outstandingBalance - $debtBalance; + /** + * @var User $user + */ + $user = \Container::$request->user(); + $balanceCalculator = new BalanceCalculator($community, $user); + $balance = $balanceCalculator->calculate(); return new HtmlContent('communities/community', [ 'community' => $community, 'upcomingAndRecentEvents' => iterator_to_array($this->eventRepository->getUpcomingAndRecentByCommunity($community, new DateTime(), 30, 3)), - 'debtItems' => $debtItems, - 'debtBalance' => $debtBalance, - 'outstandingItems' => $outstandingItems, - 'outstandingBalance' => $outstandingBalance, - 'balance' => $balance, + 'debtItems' => $balance['debtItems'], + 'debtBalance' => $balance['debtBalance'], + 'outstandingItems' => $balance['outstandingItems'], + 'outstandingBalance' => $balance['outstandingBalance'], + 'balance' => $balance['absoluteBalance'], 'editPermission' => $ownCommunityMember->getOwner() ]); } diff --git a/src/Controller/EventController.php b/src/Controller/EventController.php index ab2aa5f..c2bd3f2 100644 --- a/src/Controller/EventController.php +++ b/src/Controller/EventController.php @@ -2,6 +2,7 @@ use Container; use DateTime; +use RVR\Finance\BalanceCalculator; use RVR\Finance\ExchangeRateCalculator; use RVR\PersistentData\Model\Community; use RVR\PersistentData\Model\CommunityMember; @@ -105,10 +106,22 @@ class EventController implements IAuthenticationRequired, ISecured } Container::$persistentDataManager->loadRelationsFromDb($this->community, true, ['main_currency']); + /** + * @var User $user + */ + $user = \Container::$request->user(); + $balanceCalculator = new BalanceCalculator($this->community, $user, $event); + $balance = $balanceCalculator->calculate(); + return new HtmlContent('events/event', [ 'community' => $this->community, 'event' => $event, - 'totalCost' => $this->sumTransactions($event) + 'totalCost' => $this->sumTransactions($event), + 'debtItems' => $balance['debtItems'], + 'debtBalance' => $balance['debtBalance'], + 'outstandingItems' => $balance['outstandingItems'], + 'outstandingBalance' => $balance['outstandingBalance'], + 'balance' => $balance['absoluteBalance'], ]); } diff --git a/src/Finance/BalanceCalculator.php b/src/Finance/BalanceCalculator.php index fa4db62..31d97c1 100644 --- a/src/Finance/BalanceCalculator.php +++ b/src/Finance/BalanceCalculator.php @@ -2,6 +2,8 @@ use Container; use RVR\PersistentData\Model\Community; +use RVR\PersistentData\Model\Event; +use RVR\PersistentData\Model\User; use RVR\Repository\CommunityMemberRepository; use RVR\Repository\TransactionRepository; @@ -9,6 +11,10 @@ class BalanceCalculator { private Community $community; + private User $user; + + private ?Event $event; + private TransactionRepository $transactionRepository; private CommunityMemberRepository $communityMemberRepository; @@ -19,9 +25,13 @@ class BalanceCalculator private array $payments; - public function __construct(Community $community) + private array $actualDebts; + + public function __construct(Community $community, User $user, ?Event $event = null) { $this->community = $community; + $this->user = $user; + $this->event = $event; $this->transactionRepository = new TransactionRepository(); $this->communityMemberRepository = new CommunityMemberRepository(); $this->exchangeRateCalculator = new ExchangeRateCalculator($this->community->getMainCurrency()); @@ -32,7 +42,8 @@ class BalanceCalculator $this->collectMembers(); $this->createPaymentsMatrix(); $this->sumTransactions(); - return $this->calculateActualDebts(); + $this->calculateActualDebts(); + return $this->calculateBalanceForUser(); } private function collectMembers(): void @@ -57,7 +68,11 @@ class BalanceCalculator private function sumTransactions(): void { $membersCount = count($this->members); - $transactions = iterator_to_array($this->transactionRepository->getAllByCommunity($this->community, true, ['currency'])); + if ($this->event !== null) { + $transactions = iterator_to_array($this->transactionRepository->getAllByEvent($this->event, true, ['currency'])); + } else { + $transactions = iterator_to_array($this->transactionRepository->getAllByCommunity($this->community, true, ['currency'])); + } Container::$persistentDataManager->loadMultiRelationsFromDb($transactions, 'payees'); foreach ($transactions as $transaction) { @@ -77,20 +92,45 @@ class BalanceCalculator } } - private function calculateActualDebts(): array + private function calculateActualDebts(): void { - $actualDebts = []; + $this->actualDebts = []; foreach ($this->payments as $payerUserId => $paymentsOfPayer) { foreach ($paymentsOfPayer as $payeeUserId => $sum) { $actualDebt = $this->payments[$payeeUserId][$payerUserId] - $sum; if (round($actualDebt, $this->community->getMainCurrency()->getRoundDigits()) > 0.0) { - $actualDebts[] = ['payer' => $this->members[$payerUserId], 'payee' => $this->members[$payeeUserId], 'amount' => $actualDebt]; + $this->actualDebts[] = ['payer' => $this->members[$payerUserId], 'payee' => $this->members[$payeeUserId], 'amount' => $actualDebt]; } } } + } - return $actualDebts; + private function calculateBalanceForUser(): array + { + $debtItems = []; + $debtBalance = 0.0; + $outstandingItems = []; + $outstandingBalance = 0.0; + foreach ($this->actualDebts as $debt) { + if ($debt['payer']->getId() === $this->user->getId()) { + $debtBalance += $debt['amount']; + $debtItems[] = $debt; + } + if ($debt['payee']->getId() === $this->user->getId()) { + $outstandingBalance += $debt['amount']; + $outstandingItems[] = $debt; + } + } + $absoluteBalance = $outstandingBalance - $debtBalance; + + return [ + 'absoluteBalance' => $absoluteBalance, + 'debtItems' => $debtItems, + 'debtBalance' => $debtBalance, + 'outstandingItems' => $outstandingItems, + 'outstandingBalance' => $outstandingBalance + ]; } } diff --git a/views/events/event.php b/views/events/event.php index 9c95adf..78c2fd9 100644 --- a/views/events/event.php +++ b/views/events/event.php @@ -22,6 +22,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +
You owe*
getUser()->getDisplayName() ?>
You're owed*
getUser()->getDisplayName() ?>
Your balance*
+

* Virtual balance only for this event. Check community finances for your real balance.

@endsection