Compare commits
No commits in common. "afead7fd610e1d975a3b1d3603cdf9aaac04ce6f" and "a5978393798ea3a482fd84c07bf0ae2ea1dc9048" have entirely different histories.
afead7fd61
...
a597839379
@ -59,21 +59,32 @@ class CommunityController implements IAuthenticationRequired
|
|||||||
|
|
||||||
\Container::$persistentDataManager->loadRelationsFromDb($community, false, ['main_currency']);
|
\Container::$persistentDataManager->loadRelationsFromDb($community, false, ['main_currency']);
|
||||||
|
|
||||||
/**
|
$balanceCalculator = new BalanceCalculator($community);
|
||||||
* @var User $user
|
$debts = $balanceCalculator->calculate();
|
||||||
*/
|
$debtItems = [];
|
||||||
$user = \Container::$request->user();
|
$debtBalance = 0.0;
|
||||||
$balanceCalculator = new BalanceCalculator($community, $user);
|
$outstandingItems = [];
|
||||||
$balance = $balanceCalculator->calculate();
|
$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;
|
||||||
|
|
||||||
return new HtmlContent('communities/community', [
|
return new HtmlContent('communities/community', [
|
||||||
'community' => $community,
|
'community' => $community,
|
||||||
'upcomingAndRecentEvents' => iterator_to_array($this->eventRepository->getUpcomingAndRecentByCommunity($community, new DateTime(), 30, 3)),
|
'upcomingAndRecentEvents' => iterator_to_array($this->eventRepository->getUpcomingAndRecentByCommunity($community, new DateTime(), 30, 3)),
|
||||||
'debtItems' => $balance['debtItems'],
|
'debtItems' => $debtItems,
|
||||||
'debtBalance' => $balance['debtBalance'],
|
'debtBalance' => $debtBalance,
|
||||||
'outstandingItems' => $balance['outstandingItems'],
|
'outstandingItems' => $outstandingItems,
|
||||||
'outstandingBalance' => $balance['outstandingBalance'],
|
'outstandingBalance' => $outstandingBalance,
|
||||||
'balance' => $balance['absoluteBalance'],
|
'balance' => $balance,
|
||||||
'editPermission' => $ownCommunityMember->getOwner()
|
'editPermission' => $ownCommunityMember->getOwner()
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
use Container;
|
use Container;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use RVR\Finance\BalanceCalculator;
|
|
||||||
use RVR\Finance\ExchangeRateCalculator;
|
use RVR\Finance\ExchangeRateCalculator;
|
||||||
use RVR\PersistentData\Model\Community;
|
use RVR\PersistentData\Model\Community;
|
||||||
use RVR\PersistentData\Model\CommunityMember;
|
use RVR\PersistentData\Model\CommunityMember;
|
||||||
@ -106,22 +105,10 @@ class EventController implements IAuthenticationRequired, ISecured
|
|||||||
}
|
}
|
||||||
Container::$persistentDataManager->loadRelationsFromDb($this->community, true, ['main_currency']);
|
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', [
|
return new HtmlContent('events/event', [
|
||||||
'community' => $this->community,
|
'community' => $this->community,
|
||||||
'event' => $event,
|
'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'],
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
use Container;
|
use Container;
|
||||||
use RVR\PersistentData\Model\Community;
|
use RVR\PersistentData\Model\Community;
|
||||||
use RVR\PersistentData\Model\Event;
|
|
||||||
use RVR\PersistentData\Model\User;
|
|
||||||
use RVR\Repository\CommunityMemberRepository;
|
use RVR\Repository\CommunityMemberRepository;
|
||||||
use RVR\Repository\TransactionRepository;
|
use RVR\Repository\TransactionRepository;
|
||||||
|
|
||||||
@ -11,10 +9,6 @@ class BalanceCalculator
|
|||||||
{
|
{
|
||||||
private Community $community;
|
private Community $community;
|
||||||
|
|
||||||
private User $user;
|
|
||||||
|
|
||||||
private ?Event $event;
|
|
||||||
|
|
||||||
private TransactionRepository $transactionRepository;
|
private TransactionRepository $transactionRepository;
|
||||||
|
|
||||||
private CommunityMemberRepository $communityMemberRepository;
|
private CommunityMemberRepository $communityMemberRepository;
|
||||||
@ -25,13 +19,9 @@ class BalanceCalculator
|
|||||||
|
|
||||||
private array $payments;
|
private array $payments;
|
||||||
|
|
||||||
private array $actualDebts;
|
public function __construct(Community $community)
|
||||||
|
|
||||||
public function __construct(Community $community, User $user, ?Event $event = null)
|
|
||||||
{
|
{
|
||||||
$this->community = $community;
|
$this->community = $community;
|
||||||
$this->user = $user;
|
|
||||||
$this->event = $event;
|
|
||||||
$this->transactionRepository = new TransactionRepository();
|
$this->transactionRepository = new TransactionRepository();
|
||||||
$this->communityMemberRepository = new CommunityMemberRepository();
|
$this->communityMemberRepository = new CommunityMemberRepository();
|
||||||
$this->exchangeRateCalculator = new ExchangeRateCalculator($this->community->getMainCurrency());
|
$this->exchangeRateCalculator = new ExchangeRateCalculator($this->community->getMainCurrency());
|
||||||
@ -42,8 +32,7 @@ class BalanceCalculator
|
|||||||
$this->collectMembers();
|
$this->collectMembers();
|
||||||
$this->createPaymentsMatrix();
|
$this->createPaymentsMatrix();
|
||||||
$this->sumTransactions();
|
$this->sumTransactions();
|
||||||
$this->calculateActualDebts();
|
return $this->calculateActualDebts();
|
||||||
return $this->calculateBalanceForUser();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function collectMembers(): void
|
private function collectMembers(): void
|
||||||
@ -68,11 +57,7 @@ class BalanceCalculator
|
|||||||
private function sumTransactions(): void
|
private function sumTransactions(): void
|
||||||
{
|
{
|
||||||
$membersCount = count($this->members);
|
$membersCount = count($this->members);
|
||||||
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']));
|
$transactions = iterator_to_array($this->transactionRepository->getAllByCommunity($this->community, true, ['currency']));
|
||||||
}
|
|
||||||
Container::$persistentDataManager->loadMultiRelationsFromDb($transactions, 'payees');
|
Container::$persistentDataManager->loadMultiRelationsFromDb($transactions, 'payees');
|
||||||
|
|
||||||
foreach ($transactions as $transaction) {
|
foreach ($transactions as $transaction) {
|
||||||
@ -92,45 +77,20 @@ class BalanceCalculator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculateActualDebts(): void
|
private function calculateActualDebts(): array
|
||||||
{
|
{
|
||||||
$this->actualDebts = [];
|
$actualDebts = [];
|
||||||
|
|
||||||
foreach ($this->payments as $payerUserId => $paymentsOfPayer) {
|
foreach ($this->payments as $payerUserId => $paymentsOfPayer) {
|
||||||
foreach ($paymentsOfPayer as $payeeUserId => $sum) {
|
foreach ($paymentsOfPayer as $payeeUserId => $sum) {
|
||||||
$actualDebt = $this->payments[$payeeUserId][$payerUserId] - $sum;
|
$actualDebt = $this->payments[$payeeUserId][$payerUserId] - $sum;
|
||||||
|
|
||||||
if (round($actualDebt, $this->community->getMainCurrency()->getRoundDigits()) > 0.0) {
|
if (round($actualDebt, $this->community->getMainCurrency()->getRoundDigits()) > 0.0) {
|
||||||
$this->actualDebts[] = ['payer' => $this->members[$payerUserId], 'payee' => $this->members[$payeeUserId], 'amount' => $actualDebt];
|
$actualDebts[] = ['payer' => $this->members[$payerUserId], 'payee' => $this->members[$payeeUserId], 'amount' => $actualDebt];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculateBalanceForUser(): array
|
return $actualDebts;
|
||||||
{
|
|
||||||
$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
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,33 +22,6 @@
|
|||||||
<td class="mono" style="text-align: right;"><?= number_format($totalCost, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
<td class="mono" style="text-align: right;"><?= number_format($totalCost, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
<table class="fullWidth marginTop">
|
|
||||||
<tr>
|
|
||||||
<td class="bold">You owe*</td>
|
|
||||||
<td class="mono red" style="text-align: right;"><?= number_format($debtBalance, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
|
||||||
</tr>
|
|
||||||
<?php foreach ($debtItems as $item): ?>
|
|
||||||
<tr>
|
|
||||||
<td class="small"><?= $item['payee']->getUser()->getDisplayName() ?></td>
|
|
||||||
<td class="small mono red" style="text-align: right;"><?= number_format($item['amount'], $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
|
||||||
</tr>
|
|
||||||
<?php endforeach; ?>
|
|
||||||
<tr>
|
|
||||||
<td class="bold">You're owed*</td>
|
|
||||||
<td class="mono green" style="text-align: right;"><?= number_format($outstandingBalance, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
|
||||||
</tr>
|
|
||||||
<?php foreach ($outstandingItems as $item): ?>
|
|
||||||
<tr>
|
|
||||||
<td class="small"><?= $item['payer']->getUser()->getDisplayName() ?></td>
|
|
||||||
<td class="small mono green" style="text-align: right;"><?= number_format($item['amount'], $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
|
||||||
</tr>
|
|
||||||
<?php endforeach; ?>
|
|
||||||
<tr>
|
|
||||||
<td class="bold">Your balance*</td>
|
|
||||||
<td class="mono <?= $balance < 0 ? 'red' : ($balance > 0 ? 'green' : '') ?>" style="text-align: right;;"><?= number_format($balance, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
<p class="small right">* Virtual balance only for this event. Check <a href="<?= Container::$routeCollection->getRoute('community')->generateLink(['communitySlug' => $community->getSlug()]) ?>">community</a> finances for your real balance.</p>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@endsection
|
@endsection
|
||||||
|
Loading…
Reference in New Issue
Block a user