RVRNEXT-43 adapt balance calculations to multiple payees

This commit is contained in:
Bence Pőcze 2023-06-17 13:47:05 +02:00
parent 1886257b63
commit 1d0a2b09d5
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D

View File

@ -1,5 +1,6 @@
<?php namespace RVR\Finance; <?php namespace RVR\Finance;
use Container;
use RVR\PersistentData\Model\Community; use RVR\PersistentData\Model\Community;
use RVR\Repository\CommunityMemberRepository; use RVR\Repository\CommunityMemberRepository;
use RVR\Repository\TransactionRepository; use RVR\Repository\TransactionRepository;
@ -56,13 +57,18 @@ class BalanceCalculator
private function sumTransactions(): void private function sumTransactions(): void
{ {
$membersCount = count($this->members); $membersCount = count($this->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) { foreach ($transactions as $transaction) {
$sum = $this->exchangeRateCalculator->calculate($transaction->getSum(), $transaction->getCurrency(), $transaction->getTimeDate()); $sum = $this->exchangeRateCalculator->calculate($transaction->getSum(), $transaction->getCurrency(), $transaction->getTimeDate());
$payees = $transaction->getPayees();
$payeeCount = count($payees);
if ($transaction->getPayeeUserId()) { if ($payeeCount > 0) {
$this->payments[$transaction->getPayerUserId()][$transaction->getPayeeUserId()] += $sum; foreach ($payees as $payee) {
$this->payments[$transaction->getPayerUserId()][$payee->getUserId()] += $sum / $payeeCount;
}
} else { } else {
foreach ($this->members as $payeeUserId => $member) { foreach ($this->members as $payeeUserId => $member) {
$this->payments[$transaction->getPayerUserId()][$payeeUserId] += $sum / $membersCount; $this->payments[$transaction->getPayerUserId()][$payeeUserId] += $sum / $membersCount;