From 1d0a2b09d550d7e04333a3aca0d836a7d83b9771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sat, 17 Jun 2023 13:47:05 +0200 Subject: [PATCH] RVRNEXT-43 adapt balance calculations to multiple payees --- src/Finance/BalanceCalculator.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Finance/BalanceCalculator.php b/src/Finance/BalanceCalculator.php index 6842b0f..fa4db62 100644 --- a/src/Finance/BalanceCalculator.php +++ b/src/Finance/BalanceCalculator.php @@ -1,5 +1,6 @@ members); - $transactions = $this->transactionRepository->getAllByCommunity($this->community, true, ['currency']); + $transactions = iterator_to_array($this->transactionRepository->getAllByCommunity($this->community, true, ['currency'])); + Container::$persistentDataManager->loadMultiRelationsFromDb($transactions, 'payees'); foreach ($transactions as $transaction) { $sum = $this->exchangeRateCalculator->calculate($transaction->getSum(), $transaction->getCurrency(), $transaction->getTimeDate()); + $payees = $transaction->getPayees(); + $payeeCount = count($payees); - if ($transaction->getPayeeUserId()) { - $this->payments[$transaction->getPayerUserId()][$transaction->getPayeeUserId()] += $sum; + if ($payeeCount > 0) { + foreach ($payees as $payee) { + $this->payments[$transaction->getPayerUserId()][$payee->getUserId()] += $sum / $payeeCount; + } } else { foreach ($this->members as $payeeUserId => $member) { $this->payments[$transaction->getPayerUserId()][$payeeUserId] += $sum / $membersCount;