This commit is contained in:
4 files changed
+99
-30
No files matched your search
@@ -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
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user