RVRNEXT-5 make community main currency a general currency

This commit is contained in:
bence committed 2023-04-28 21:06:18 +02:00
1 parent 157f530ad5
commit 20a850f011
6 files changed
+82 -21

No files matched your search

+29 -5
View File
@@ -48,6 +48,8 @@ class CommunityController implements IAuthenticationRequired
return null;
}
\Container::$persistentDataManager->loadRelationsFromDb($community, false);
return new HtmlContent('communities/community', [
'community' => $community,
'members' => $this->getMembers($community),
@@ -185,6 +187,10 @@ class CommunityController implements IAuthenticationRequired
}
$currency = $this->currencyRepository->getById(\Container::$request->query('currency_id'));
if ($currency->getId() === $community->getMainCurrencyId()) {
return null;
}
\Container::$persistentDataManager->deleteFromDb($currency);
return new JsonContent(['success' => true]);
@@ -197,7 +203,7 @@ class CommunityController implements IAuthenticationRequired
}
$currency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, \Container::$request->query('code'));
if ($currency === null) {
if ($currency === null || $currency->getId() === $community->getMainCurrencyId()) {
return null;
}
@@ -264,8 +270,7 @@ class CommunityController implements IAuthenticationRequired
public function saveCommunity(): ?IContent
{
$name = \Container::$request->post('name');
$currency = \Container::$request->post('currency');
if (strlen($name) === 0 || strlen($currency) === 0 || strlen($currency) > 3) {
if (strlen($name) === 0) {
return new JsonContent([
'error' => ['errorText' => 'Please fill all required fields!']
]);
@@ -277,12 +282,19 @@ class CommunityController implements IAuthenticationRequired
return null;
}
} 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->setCreatedDate(new DateTime());
}
$community->setName($name);
$community->setCurrency($currency);
\Container::$persistentDataManager->saveToDb($community);
if (!$communityId) {
@@ -296,6 +308,15 @@ class CommunityController implements IAuthenticationRequired
$communityMember->setUser($user);
$communityMember->setOwner(true);
\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([
@@ -305,7 +326,7 @@ class CommunityController implements IAuthenticationRequired
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) {
return strnatcmp($a->getUser()->getDisplayName(), $b->getUser()->getDisplayName());
});
@@ -318,6 +339,9 @@ class CommunityController implements IAuthenticationRequired
usort($currencies, function($a, $b) {
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;
}