RVRNEXT-11 add event for transactions

This commit is contained in:
Bence Pőcze 2023-05-28 03:45:36 +02:00
parent ddc85e06bb
commit 43b8ef78e7
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D
5 changed files with 94 additions and 5 deletions

View File

@ -0,0 +1,30 @@
(function () {
const element = document.getElementById('transactionForm').elements['event_id'];
const select = new TomSelect(element, {
valueField: 'value',
labelField: 'label',
searchField: 'label',
loadThrottle: 300,
load: function (query, callback) {
var self = this;
RVR.httpRequest('GET', searchEventUrl.replace('QUERY', encodeURIComponent(query)), function () {
self.clearOptions();
callback(this.response.results);
});
},
});
select.on('change', function (value) {
this.clearOptions();
});
select.on('blur', function (value) {
this.clearOptions();
});
select.on('type', function (value) {
if (value === '') {
this.clearOptions();
}
});
})();

View File

@ -11,6 +11,7 @@ use RVR\Repository\CommunityMemberRepository;
use RVR\Repository\CommunityRepository; use RVR\Repository\CommunityRepository;
use RVR\Repository\CurrencyRepository; use RVR\Repository\CurrencyRepository;
use RVR\Repository\TransactionRepository; use RVR\Repository\TransactionRepository;
use RVR\Repository\EventRepository;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired; use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Authorization\ISecured; use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Response\IContent; use SokoWeb\Interfaces\Response\IContent;
@ -27,9 +28,11 @@ class TransactionController implements IAuthenticationRequired, ISecured
private TransactionRepository $transactionRepository; private TransactionRepository $transactionRepository;
private Community $community; private EventRepository $eventRepository;
private CommunityMember $ownCommunityMember; private ?Community $community;
private ?CommunityMember $ownCommunityMember;
public function __construct() public function __construct()
{ {
@ -37,6 +40,7 @@ class TransactionController implements IAuthenticationRequired, ISecured
$this->communityMemberRepository = new CommunityMemberRepository(); $this->communityMemberRepository = new CommunityMemberRepository();
$this->currencyRepository = new CurrencyRepository(); $this->currencyRepository = new CurrencyRepository();
$this->transactionRepository = new TransactionRepository(); $this->transactionRepository = new TransactionRepository();
$this->eventRepository = new EventRepository();
} }
public function isAuthenticationRequired(): bool public function isAuthenticationRequired(): bool
@ -78,7 +82,7 @@ class TransactionController implements IAuthenticationRequired, ISecured
$currentPage * $itemsPerPage, $currentPage * $itemsPerPage,
$itemsPerPage, $itemsPerPage,
true, true,
['currency', 'payer_user', 'payee_user'] ['event', 'currency', 'payer_user', 'payee_user']
); );
return new HtmlContent('communities/transactions', [ return new HtmlContent('communities/transactions', [
@ -99,13 +103,22 @@ class TransactionController implements IAuthenticationRequired, ISecured
if ($transaction === null) { if ($transaction === null) {
return null; return null;
} }
Container::$persistentDataManager->loadRelationsFromDb($transaction, false, ['event']);
$event = $transaction->getEvent();
} else { } else {
$transaction = null; $transaction = null;
$eventSlug = Container::$request->query('event');
if ($eventSlug) {
$event = $this->eventRepository->getBySlug($eventSlug);
} else {
$event = null;
}
} }
return new HtmlContent('communities/transaction_edit', [ return new HtmlContent('communities/transaction_edit', [
'community' => $this->community, 'community' => $this->community,
'transaction' => $transaction, 'transaction' => $transaction,
'event' => $event,
'members' => $this->getMembers($this->community), 'members' => $this->getMembers($this->community),
'currencies' => $this->getCurrencies($this->community) 'currencies' => $this->getCurrencies($this->community)
]); ]);
@ -121,6 +134,7 @@ class TransactionController implements IAuthenticationRequired, ISecured
$transaction->setCommunity($this->community); $transaction->setCommunity($this->community);
} }
$transaction->setEventId(Container::$request->post('event_id') ?: null);
$transaction->setCurrencyId(Container::$request->post('currency_id')); $transaction->setCurrencyId(Container::$request->post('currency_id'));
$transaction->setPayerUserId(Container::$request->post('payer_user_id')); $transaction->setPayerUserId(Container::$request->post('payer_user_id'));
$transaction->setPayeeUserId(Container::$request->post('payee_user_id') ?: null); $transaction->setPayeeUserId(Container::$request->post('payee_user_id') ?: null);

View File

@ -7,10 +7,11 @@ class Transaction extends Model
{ {
protected static string $table = 'transactions'; protected static string $table = 'transactions';
protected static array $fields = ['community_id', 'currency_id', 'payer_user_id', 'payee_user_id', 'description', 'sum', 'time']; protected static array $fields = ['community_id', 'event_id', 'currency_id', 'payer_user_id', 'payee_user_id', 'description', 'sum', 'time'];
protected static array $relations = [ protected static array $relations = [
'community' => Community::class, 'community' => Community::class,
'event' => Event::class,
'currency' => Currency::class, 'currency' => Currency::class,
'payer_user' => User::class, 'payer_user' => User::class,
'payee_user' => User::class 'payee_user' => User::class
@ -20,6 +21,10 @@ class Transaction extends Model
private int $communityId; private int $communityId;
private ?Event $event = null;
private ?int $eventId = null;
private ?Currency $currency = null; private ?Currency $currency = null;
private int $currencyId; private int $currencyId;
@ -48,6 +53,16 @@ class Transaction extends Model
$this->communityId = $communityId; $this->communityId = $communityId;
} }
public function setEvent(?Event $event): void
{
$this->event = $event;
}
public function setEventId(?int $eventId): void
{
$this->eventId = $eventId;
}
public function setCurrency(Currency $currency): void public function setCurrency(Currency $currency): void
{ {
$this->currency = $currency; $this->currency = $currency;
@ -108,6 +123,16 @@ class Transaction extends Model
return $this->communityId; return $this->communityId;
} }
public function getEvent(): ?Event
{
return $this->event;
}
public function getEventId(): ?int
{
return $this->eventId;
}
public function getCurrency(): ?Currency public function getCurrency(): ?Currency
{ {
return $this->currency; return $this->currency;

View File

@ -1,3 +1,7 @@
@css(/static/node_modules/tom-select/dist/css/tom-select.min.css)
@js(/static/node_modules/tom-select/dist/js/tom-select.base.min.js)
@js(js/communities/transaction.js)
@extends(templates/layout_normal) @extends(templates/layout_normal)
@section(main) @section(main)
@ -17,7 +21,14 @@
Container::$routeCollection->getRoute('community.transactions.new-action')->generateLink(['communitySlug' => $community->getSlug()]); Container::$routeCollection->getRoute('community.transactions.new-action')->generateLink(['communitySlug' => $community->getSlug()]);
?> ?>
<form id="transactionForm" action="<?= $formAction ?>" method="post" data-redirect-on-success="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug()]) ?>"> <form id="transactionForm" action="<?= $formAction ?>" method="post" data-redirect-on-success="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug()]) ?>">
<p class="formLabel">Payer</p> <p class="formLabel">Event</p>
<select name="event_id">
<option value="">[none]</option>
<?php if (isset($event)): ?>
<option value="<?= $event->getId() ?>" selected><?= $event->getTitle() ?></option>
<?php endif; ?>
</select>
<p class="formLabel marginTop">Payer</p>
<select class="big fullWidth" name="payer_user_id" required> <select class="big fullWidth" name="payer_user_id" required>
<option value="" hidden></option> <option value="" hidden></option>
<?php foreach ($members as $member): ?> <?php foreach ($members as $member): ?>
@ -57,3 +68,9 @@
<?php endif; ?> <?php endif; ?>
</div> </div>
@endsection @endsection
@section(pageScript)
<script>
var searchEventUrl = '<?= Container::$routeCollection->getRoute('community.events.search')->generateLink(['communitySlug' => $community->getSlug(), 'q' => 'QUERY']) ?>';
</script>
@endsection

View File

@ -29,6 +29,9 @@
<a class="block" href="<?= Container::$routeCollection->getRoute('community.transactions.edit')->generateLink(['communitySlug' => $community->getSlug(), 'transactionId' => $transaction->getId()]) ?>"> <a class="block" href="<?= Container::$routeCollection->getRoute('community.transactions.edit')->generateLink(['communitySlug' => $community->getSlug(), 'transactionId' => $transaction->getId()]) ?>">
<div class="box transaction"> <div class="box transaction">
<div> <div>
<?php if ($transaction->getEvent()): ?>
<p><span class="label"><?= $transaction->getEvent()->getTitle() ?></span></p>
<?php endif; ?>
<p style="font-weight: bold;"><?= $transaction->getDescription() ?></p> <p style="font-weight: bold;"><?= $transaction->getDescription() ?></p>
<p class="small"><?= $transaction->getPayerUser()->getDisplayName() ?> <i class="fa-solid fa-caret-right"></i> <?= $transaction->getPayeeUser() ? $transaction->getPayeeUser()->getDisplayName() : '[common]' ?></p> <p class="small"><?= $transaction->getPayerUser()->getDisplayName() ?> <i class="fa-solid fa-caret-right"></i> <?= $transaction->getPayeeUser() ? $transaction->getPayeeUser()->getDisplayName() : '[common]' ?></p>
<p class="small"><?= $transaction->getTimeDate()->format('Y-m-d H:i') ?></p> <p class="small"><?= $transaction->getTimeDate()->format('Y-m-d H:i') ?></p>