feature/RVRNEXT-5-main-currency-of-community-should-be-a-general-currency #34
@ -48,6 +48,8 @@ class CommunityController implements IAuthenticationRequired
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
\Container::$persistentDataManager->loadRelationsFromDb($community, false);
|
||||||
|
|
||||||
return new HtmlContent('communities/community', [
|
return new HtmlContent('communities/community', [
|
||||||
'community' => $community,
|
'community' => $community,
|
||||||
'members' => $this->getMembers($community),
|
'members' => $this->getMembers($community),
|
||||||
@ -185,6 +187,10 @@ class CommunityController implements IAuthenticationRequired
|
|||||||
}
|
}
|
||||||
|
|
||||||
$currency = $this->currencyRepository->getById(\Container::$request->query('currency_id'));
|
$currency = $this->currencyRepository->getById(\Container::$request->query('currency_id'));
|
||||||
|
if ($currency->getId() === $community->getMainCurrencyId()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
\Container::$persistentDataManager->deleteFromDb($currency);
|
\Container::$persistentDataManager->deleteFromDb($currency);
|
||||||
|
|
||||||
return new JsonContent(['success' => true]);
|
return new JsonContent(['success' => true]);
|
||||||
@ -197,7 +203,7 @@ class CommunityController implements IAuthenticationRequired
|
|||||||
}
|
}
|
||||||
|
|
||||||
$currency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, \Container::$request->query('code'));
|
$currency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, \Container::$request->query('code'));
|
||||||
if ($currency === null) {
|
if ($currency === null || $currency->getId() === $community->getMainCurrencyId()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,8 +270,7 @@ class CommunityController implements IAuthenticationRequired
|
|||||||
public function saveCommunity(): ?IContent
|
public function saveCommunity(): ?IContent
|
||||||
{
|
{
|
||||||
$name = \Container::$request->post('name');
|
$name = \Container::$request->post('name');
|
||||||
$currency = \Container::$request->post('currency');
|
if (strlen($name) === 0) {
|
||||||
if (strlen($name) === 0 || strlen($currency) === 0 || strlen($currency) > 3) {
|
|
||||||
return new JsonContent([
|
return new JsonContent([
|
||||||
'error' => ['errorText' => 'Please fill all required fields!']
|
'error' => ['errorText' => 'Please fill all required fields!']
|
||||||
]);
|
]);
|
||||||
@ -277,12 +282,19 @@ class CommunityController implements IAuthenticationRequired
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
$mainCurrencyCode = \Container::$request->post('main_currency_code');
|
||||||
|
$mainCurrencyRoundDigits = \Container::$request->post('main_currency_round_digits');
|
||||||
|
if (strlen($mainCurrencyCode) === 0 || strlen($mainCurrencyCode) > 3 || $mainCurrencyRoundDigits < 0 || $mainCurrencyRoundDigits > 9) {
|
||||||
|
return new JsonContent([
|
||||||
|
'error' => ['errorText' => 'Please fill all required fields!']
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
$community = new Community();
|
$community = new Community();
|
||||||
$community->setCreatedDate(new DateTime());
|
$community->setCreatedDate(new DateTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
$community->setName($name);
|
$community->setName($name);
|
||||||
$community->setCurrency($currency);
|
|
||||||
\Container::$persistentDataManager->saveToDb($community);
|
\Container::$persistentDataManager->saveToDb($community);
|
||||||
|
|
||||||
if (!$communityId) {
|
if (!$communityId) {
|
||||||
@ -296,6 +308,15 @@ class CommunityController implements IAuthenticationRequired
|
|||||||
$communityMember->setUser($user);
|
$communityMember->setUser($user);
|
||||||
$communityMember->setOwner(true);
|
$communityMember->setOwner(true);
|
||||||
\Container::$persistentDataManager->saveToDb($communityMember);
|
\Container::$persistentDataManager->saveToDb($communityMember);
|
||||||
|
|
||||||
|
$mainCurrency = new Currency();
|
||||||
|
$mainCurrency->setCommunity($community);
|
||||||
|
$mainCurrency->setCode($mainCurrencyCode);
|
||||||
|
$mainCurrency->setRoundDigits($mainCurrencyRoundDigits);
|
||||||
|
\Container::$persistentDataManager->saveToDb($mainCurrency);
|
||||||
|
|
||||||
|
$community->setMainCurrency($mainCurrency);
|
||||||
|
\Container::$persistentDataManager->saveToDb($community);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JsonContent([
|
return new JsonContent([
|
||||||
@ -305,7 +326,7 @@ class CommunityController implements IAuthenticationRequired
|
|||||||
|
|
||||||
private function getMembers(Community $community): array
|
private function getMembers(Community $community): array
|
||||||
{
|
{
|
||||||
$members = iterator_to_array($this->communityMemberRepository->getAllByCommunity($community, true));
|
$members = iterator_to_array($this->communityMemberRepository->getAllByCommunity($community, true, [User::class]));
|
||||||
usort($members, function($a, $b) {
|
usort($members, function($a, $b) {
|
||||||
return strnatcmp($a->getUser()->getDisplayName(), $b->getUser()->getDisplayName());
|
return strnatcmp($a->getUser()->getDisplayName(), $b->getUser()->getDisplayName());
|
||||||
});
|
});
|
||||||
@ -318,6 +339,9 @@ class CommunityController implements IAuthenticationRequired
|
|||||||
usort($currencies, function($a, $b) {
|
usort($currencies, function($a, $b) {
|
||||||
return strnatcmp($a->getCode(), $b->getCode());
|
return strnatcmp($a->getCode(), $b->getCode());
|
||||||
});
|
});
|
||||||
|
usort($currencies, function($a, $b) use ($community) {
|
||||||
|
return (int)($b->getId() === $community->getMainCurrencyId()) - (int)($a->getId() === $community->getMainCurrencyId());
|
||||||
|
});
|
||||||
return $currencies;
|
return $currencies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php namespace RVR\Controller;
|
<?php namespace RVR\Controller;
|
||||||
|
|
||||||
|
use RVR\PersistentData\Model\Community;
|
||||||
use RVR\PersistentData\Model\User;
|
use RVR\PersistentData\Model\User;
|
||||||
use RVR\Repository\CommunityMemberRepository;
|
use RVR\Repository\CommunityMemberRepository;
|
||||||
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
||||||
@ -27,7 +28,7 @@ class HomeController implements IAuthenticationRequired
|
|||||||
*/
|
*/
|
||||||
$user = \Container::$request->user();
|
$user = \Container::$request->user();
|
||||||
|
|
||||||
$ownCommunityMembers = $this->communityMemberRepository->getAllByUser($user, true);
|
$ownCommunityMembers = $this->communityMemberRepository->getAllByUser($user, true, [Community::class]);
|
||||||
$communities = [];
|
$communities = [];
|
||||||
foreach ($ownCommunityMembers as $ownCommunityMember) {
|
foreach ($ownCommunityMembers as $ownCommunityMember) {
|
||||||
$communities[] = $ownCommunityMember->getCommunity();
|
$communities[] = $ownCommunityMember->getCommunity();
|
||||||
|
@ -7,12 +7,18 @@ class Community extends Model
|
|||||||
{
|
{
|
||||||
protected static string $table = 'communities';
|
protected static string $table = 'communities';
|
||||||
|
|
||||||
protected static array $fields = ['name', 'currency', 'created'];
|
protected static array $fields = ['name', 'currency', 'main_currency_id', 'created'];
|
||||||
|
|
||||||
|
protected static array $relations = ['main_currency' => Currency::class];
|
||||||
|
|
||||||
private string $name = '';
|
private string $name = '';
|
||||||
|
|
||||||
private string $currency = '';
|
private string $currency = '';
|
||||||
|
|
||||||
|
private ?Currency $mainCurrency = null;
|
||||||
|
|
||||||
|
private ?int $mainCurrencyId = null;
|
||||||
|
|
||||||
private DateTime $created;
|
private DateTime $created;
|
||||||
|
|
||||||
public function setName(string $name): void
|
public function setName(string $name): void
|
||||||
@ -25,6 +31,16 @@ class Community extends Model
|
|||||||
$this->currency = $currency;
|
$this->currency = $currency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setMainCurrency(Currency $mainCurrency): void
|
||||||
|
{
|
||||||
|
$this->mainCurrency = $mainCurrency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setMainCurrencyId(int $mainCurrencyId): void
|
||||||
|
{
|
||||||
|
$this->mainCurrencyId = $mainCurrencyId;
|
||||||
|
}
|
||||||
|
|
||||||
public function setCreatedDate(DateTime $created): void
|
public function setCreatedDate(DateTime $created): void
|
||||||
{
|
{
|
||||||
$this->created = $created;
|
$this->created = $created;
|
||||||
@ -45,6 +61,16 @@ class Community extends Model
|
|||||||
return $this->currency;
|
return $this->currency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getMainCurrency(): ?Currency
|
||||||
|
{
|
||||||
|
return $this->mainCurrency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMainCurrencyId(): ?int
|
||||||
|
{
|
||||||
|
return $this->mainCurrencyId;
|
||||||
|
}
|
||||||
|
|
||||||
public function getCreatedDate(): DateTime
|
public function getCreatedDate(): DateTime
|
||||||
{
|
{
|
||||||
return $this->created;
|
return $this->created;
|
||||||
|
@ -16,14 +16,15 @@
|
|||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<h3 class="marginBottom">Currencies</h3>
|
<h3 class="marginBottom">Currencies</h3>
|
||||||
<p>Main currency: <b><?= $community->getCurrency() ?></b></p>
|
<?php foreach ($currencies as $currency): ?>
|
||||||
<?php if (count($currencies) > 0): ?>
|
<p>
|
||||||
<p>Further currencies: <b>
|
<?php if ($currency->getId() === $community->getMainCurrencyId()): ?>
|
||||||
<?php foreach ($currencies as $currency): ?>
|
<b><?= $currency->getCode() ?></b>
|
||||||
|
<?php else: ?>
|
||||||
<a href="<?= Container::$routeCollection->getRoute('community-currency-exchange-rates')->generateLink(['communityId' => $community->getId(), 'code' => $currency->getCode()]) ?>"><?= $currency->getCode() ?></a>
|
<a href="<?= Container::$routeCollection->getRoute('community-currency-exchange-rates')->generateLink(['communityId' => $community->getId(), 'code' => $currency->getCode()]) ?>"><?= $currency->getCode() ?></a>
|
||||||
<?php endforeach; ?>
|
<?php endif; ?>
|
||||||
</b></p>
|
</p>
|
||||||
<?php endif; ?>
|
<?php endforeach; ?>
|
||||||
<?php if ($editPermission): ?>
|
<?php if ($editPermission): ?>
|
||||||
<hr>
|
<hr>
|
||||||
<p><a href="<?= Container::$routeCollection->getRoute('community-currencies')->generateLink(['communityId' => $community->getId()]) ?>">Edit currencies</a></p>
|
<p><a href="<?= Container::$routeCollection->getRoute('community-currencies')->generateLink(['communityId' => $community->getId()]) ?>">Edit currencies</a></p>
|
||||||
@ -40,19 +41,23 @@
|
|||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
|
<?php
|
||||||
|
$mainCurrencyCode = $community->getMainCurrency()->getCode();
|
||||||
|
$mainCurrencyRoundDigits = $community->getMainCurrency()->getRoundDigits();
|
||||||
|
?>
|
||||||
<h3 class="marginBottom">Finances</h3>
|
<h3 class="marginBottom">Finances</h3>
|
||||||
<table class="fullWidth">
|
<table class="fullWidth">
|
||||||
<tr>
|
<tr>
|
||||||
<td>You owe</td>
|
<td>You owe</td>
|
||||||
<td style="text-align: right; color: red;">0 <?= $community->getCurrency() ?></td>
|
<td style="text-align: right; color: red;"><?= number_format(0, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>You're owed</td>
|
<td>You're owed</td>
|
||||||
<td style="text-align: right; color: green;">0 <?= $community->getCurrency() ?></td>
|
<td style="text-align: right; color: green;"><?= number_format(0, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Your balance</td>
|
<td>Your balance</td>
|
||||||
<td style="text-align: right;">0 <?= $community->getCurrency() ?></td>
|
<td style="text-align: right;"><?= number_format(0, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
@ -21,9 +21,11 @@
|
|||||||
<td>
|
<td>
|
||||||
<input type="number" form="editCurrency_<?= $currency->getId() ?>" class="text fullWidth" name="round_digits" value="<?= $currency->getRoundDigits() ?>" min="0" max="9" required>
|
<input type="number" form="editCurrency_<?= $currency->getId() ?>" class="text fullWidth" name="round_digits" value="<?= $currency->getRoundDigits() ?>" min="0" max="9" required>
|
||||||
</td>
|
</td>
|
||||||
<td style="text-align: right;">
|
<td style="text-align: right; font-size: 0;">
|
||||||
<button type="submit" form="editCurrency_<?= $currency->getId() ?>" name="submit" class="small marginRight" disabled>Save</button><!--
|
<button type="submit" form="editCurrency_<?= $currency->getId() ?>" name="submit" class="small" disabled>Save</button>
|
||||||
--><button type="submit" form="deleteCurrency_<?= $currency->getId() ?>" class="small red">Delete</button>
|
<?php if ($currency->getId() !== $community->getMainCurrencyId()): ?>
|
||||||
|
<button type="submit" form="deleteCurrency_<?= $currency->getId() ?>" class="small red marginLeft">Delete</button>
|
||||||
|
<?php endif; ?>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
|
@ -16,7 +16,10 @@
|
|||||||
?>
|
?>
|
||||||
<form id="communityForm" action="<?= $formAction ?>" method="post" data-redirect-on-success="true">
|
<form id="communityForm" action="<?= $formAction ?>" method="post" data-redirect-on-success="true">
|
||||||
<input type="text" class="text big fullWidth" name="name" placeholder="Name" value="<?= isset($community) ? $community->getName() : '' ?>" required>
|
<input type="text" class="text big fullWidth" name="name" placeholder="Name" value="<?= isset($community) ? $community->getName() : '' ?>" required>
|
||||||
<input type="text" class="text big fullWidth marginTop" name="currency" value="<?= isset($community) ? $community->getCurrency() : '' ?>" placeholder="Default currency" maxlength="3" required>
|
<?php if (!isset($community)): ?>
|
||||||
|
<input type="text" class="text big fullWidth marginTop" name="main_currency_code" placeholder="Main currency" maxlength="3" required>
|
||||||
|
<input type="number" class="text big fullWidth marginTop" name="main_currency_round_digits" placeholder="Main currency round digits" min="0" max="9" required>
|
||||||
|
<?php endif; ?>
|
||||||
<p id="communityFormError" class="formError justify marginTop"></p>
|
<p id="communityFormError" class="formError justify marginTop"></p>
|
||||||
<div class="right marginTop">
|
<div class="right marginTop">
|
||||||
<button type="submit" name="submit"><?= isset($community) ? 'Save' : 'Create' ?></button>
|
<button type="submit" name="submit"><?= isset($community) ? 'Save' : 'Create' ?></button>
|
||||||
|
Loading…
Reference in New Issue
Block a user