diff --git a/composer.json b/composer.json index 1f4500f..45d6329 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "esoko/soko-web": "0.7", + "esoko/soko-web": "0.8", "firebase/php-jwt": "^6.4" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 27d420f..e3bd9cf 100644 --- a/composer.lock +++ b/composer.lock @@ -4,15 +4,15 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "71f3adc97b2d83ac1d57112ecce65fac", + "content-hash": "a89a42e04596ab159fc41abbd9390068", "packages": [ { "name": "esoko/soko-web", - "version": "v0.7", + "version": "v0.8", "source": { "type": "git", "url": "https://git.esoko.eu/esoko/soko-web.git", - "reference": "88a2a99527b51dfb240ec78ac7070dc36a1022b6" + "reference": "219b42f995b8e34432da4dde77e53e24b75d78dd" }, "require": { "phpmailer/phpmailer": "^6.8", @@ -33,7 +33,7 @@ "GNU GPL 3.0" ], "description": "Lightweight web framework", - "time": "2023-04-30T18:20:27+00:00" + "time": "2023-05-01T17:08:22+00:00" }, { "name": "firebase/php-jwt", diff --git a/database/migrations/structure/20230428_2150_transactions.sql b/database/migrations/structure/20230428_2150_transactions.sql new file mode 100644 index 0000000..d6f0645 --- /dev/null +++ b/database/migrations/structure/20230428_2150_transactions.sql @@ -0,0 +1,19 @@ +CREATE TABLE `transactions` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `community_id` int(10) unsigned NOT NULL, + `currency_id` int(10) unsigned NOT NULL, + `payer_user_id` int(10) unsigned NOT NULL, + `payee_user_id` int(10) unsigned NULL, + `description` varchar(255) NOT NULL, + `sum` decimal(19,9) NOT NULL, + `time` timestamp NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (`id`), + KEY `community_id` (`community_id`), + KEY `currency_id` (`currency_id`), + KEY `payer_user_id` (`payer_user_id`), + KEY `payee_user_id` (`payee_user_id`), + CONSTRAINT `transactions_community_id` FOREIGN KEY (`community_id`) REFERENCES `communities` (`id`), + CONSTRAINT `transactions_currency_id` FOREIGN KEY (`currency_id`) REFERENCES `currencies` (`id`), + CONSTRAINT `transactions_payer_user_id` FOREIGN KEY (`payer_user_id`) REFERENCES `users` (`id`), + CONSTRAINT `transactions_payee_user_id` FOREIGN KEY (`payee_user_id`) REFERENCES `users` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; diff --git a/public/static/css/rvr.css b/public/static/css/rvr.css index ec9c086..b1bb9a9 100644 --- a/public/static/css/rvr.css +++ b/public/static/css/rvr.css @@ -31,6 +31,10 @@ main { color: #ffffff; } +::placeholder, select > option[value=""] { + color: #8e8e8e; +} + p, h1, h2, h3, input, textarea, select, button, a, table, label { font-family: 'Oxygen', sans-serif; } @@ -150,6 +154,12 @@ a:hover, a:focus { text-decoration: underline; } +a.block { + color: initial; + font-weight: initial; + text-decoration: initial; +} + button, a.button { cursor: pointer; font-size: 16px; @@ -421,7 +431,6 @@ div.buttonContainer>button { } div.box { - width: 576px; background-color: #eeeef4; border-radius: 3px; margin: 10px auto; @@ -429,6 +438,15 @@ div.box { box-sizing: border-box; } +div.compactBox { + width: 576px; +} + +div.transaction { + display: grid; + grid-template-columns: auto auto; +} + div.gridContainer { display: grid; grid-template-columns: repeat(auto-fit, minmax(400px, 1fr)); @@ -451,7 +469,7 @@ table.fullWidth { } table th { - font-weight: bold; + font-weight: 700; } table th, table td { @@ -467,6 +485,34 @@ table th:not(:last-child), table td:not(:last-child) { padding-right: 3px; } +p.paginateContainer { + font-size: 0; +} + +p.paginateContainer > * { + font-size: initial; + background-color: #5e77aa; + border: solid #5e77aa 1px; + color: #ffffff; + font-weight: 700; + padding: 3px 6px; + text-align: center; + display: inline-block; + height: 25px; + line-height: 25px; + width: 25px; +} + +p.paginateContainer > a:hover, p.paginateContainer > .selected { + background-color: #3b5998; + border: solid #29457f 1px; + text-decoration: none; +} + +p.paginateContainer > *:not(:last-child) { + border-right: solid #869ab9 1px; +} + .choices__inner { box-sizing: border-box; } @@ -501,7 +547,7 @@ table th:not(:last-child), table td:not(:last-child) { padding-left: 15px; padding-right: 15px; } - div.box { + div.compactBox { width: initial; } } diff --git a/src/Controller/TransactionController.php b/src/Controller/TransactionController.php new file mode 100644 index 0000000..c6b484d --- /dev/null +++ b/src/Controller/TransactionController.php @@ -0,0 +1,162 @@ +communityRepository = new CommunityRepository(); + $this->communityMemberRepository = new CommunityMemberRepository(); + $this->currencyRepository = new CurrencyRepository(); + $this->transactionRepository = new TransactionRepository(); + } + + public function isAuthenticationRequired(): bool + { + return true; + } + + public function authorize(): bool + { + $communityId = \Container::$request->query('communityId'); + $this->community = $this->communityRepository->getById($communityId); + if ($this->community === null) { + return false; + } + + /** + * @var User $user + */ + $user = \Container::$request->user(); + $this->ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($this->community, $user); + if ($this->ownCommunityMember === null) { + return false; + } + + return true; + } + + public function getTransactions(): IContent + { + Container::$persistentDataManager->loadRelationsFromDb($this->community, true, ['main_currency']); + $exchangeRateCalculator = new ExchangeRateCalculator($this->community->getMainCurrency()); + + $itemsPerPage = 50; + $numberOfTransactions = $this->transactionRepository->countAllByCommunity($this->community); + $pages = ceil($numberOfTransactions / $itemsPerPage); + $currentPage = Container::$request->query('page') ?: 0; + $transactions = $this->transactionRepository->getPagedByCommunity( + $this->community, + $currentPage * $itemsPerPage, + $itemsPerPage, + true, + ['currency', 'payer_user', 'payee_user'] + ); + + return new HtmlContent('communities/transactions', [ + 'community' => $this->community, + 'exchangeRateCalculator' => $exchangeRateCalculator, + 'pages' => $pages, + 'currentPage' => $currentPage, + 'transactions' => $transactions + ]); + } + + public function getTransactionEdit(): ?IContent + { + $transactionId = Container::$request->query('transactionId'); + if ($transactionId) { + $transaction = $this->transactionRepository->getById($transactionId); + if ($transaction === null) { + return null; + } + } else { + $transaction = null; + } + + return new HtmlContent('communities/transaction_edit', [ + 'community' => $this->community, + 'transaction' => $transaction, + 'members' => $this->getMembers($this->community), + 'currencies' => $this->getCurrencies($this->community) + ]); + } + + public function saveTransaction(): ?IContent + { + $transactionId = Container::$request->query('transactionId'); + if ($transactionId) { + $transaction = $this->transactionRepository->getById($transactionId); + } else { + $transaction = new Transaction(); + $transaction->setCommunity($this->community); + } + + $transaction->setCurrencyId(Container::$request->post('currency_id')); + $transaction->setPayerUserId(Container::$request->post('payer_user_id')); + $transaction->setPayeeUserId(Container::$request->post('payee_user_id') ?: null); + $transaction->setDescription(Container::$request->post('description')); + $transaction->setSum(Container::$request->post('sum')); + $transaction->setTimeDate(new DateTime(Container::$request->post('time'))); + Container::$persistentDataManager->saveToDb($transaction); + + return new JsonContent(['success' => true]); + } + + public function deleteTransaction(): IContent + { + $transaction = $this->transactionRepository->getById(Container::$request->query('transactionId')); + Container::$persistentDataManager->deleteFromDb($transaction); + + return new JsonContent(['success' => true]); + } + + private function getMembers(Community $community): array + { + $members = iterator_to_array($this->communityMemberRepository->getAllByCommunity($community, true, ['user'])); + usort($members, function($a, $b) { + return strnatcmp($a->getUser()->getDisplayName(), $b->getUser()->getDisplayName()); + }); + return $members; + } + + private function getCurrencies(Community $community): array + { + $currencies = iterator_to_array($this->currencyRepository->getAllByCommunity($community)); + 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; + } +} diff --git a/src/Finance/ExchangeRateCalculator.php b/src/Finance/ExchangeRateCalculator.php new file mode 100644 index 0000000..1b170b0 --- /dev/null +++ b/src/Finance/ExchangeRateCalculator.php @@ -0,0 +1,52 @@ +mainCurrency = $mainCurrency; + $this->currencyExchangeRateRepository = new CurrencyExchangeRateRepository(); + } + + public function calculate(float $sumInCurrency, Currency $currency, DateTime $time): float + { + if ($currency->getId() === $this->mainCurrency->getId()) { + return $sumInCurrency; + } + + $currentExchangeRate = $this->getCurrentExchangeRate($currency, $time); + if ($currentExchangeRate === null) { + return 0.0; + } + + return $sumInCurrency * $currentExchangeRate->getExchangeRate(); + } + + private function getCurrentExchangeRate(Currency $currency, DateTime $time): ?CurrencyExchangeRate + { + if (!isset($this->exchangeRates[$currency->getId()])) { + $this->exchangeRates[$currency->getId()] = iterator_to_array($this->currencyExchangeRateRepository->getAllByCurrency($currency)); + } + + $currentExchangeRate = null; + foreach ($this->exchangeRates[$currency->getId()] as $exchangeRate) { + if ($exchangeRate->getValidFrom() > $time) { + break; + } + $currentExchangeRate = $exchangeRate; + } + + return $currentExchangeRate; + } +} diff --git a/src/PersistentData/Model/Transaction.php b/src/PersistentData/Model/Transaction.php new file mode 100644 index 0000000..a4a9b48 --- /dev/null +++ b/src/PersistentData/Model/Transaction.php @@ -0,0 +1,160 @@ + Community::class, + 'currency' => Currency::class, + 'payer_user' => User::class, + 'payee_user' => User::class + ]; + + private ?Community $community = null; + + private int $communityId; + + private ?Currency $currency = null; + + private int $currencyId; + + private ?User $payerUser = null; + + private int $payerUserId; + + private ?User $payeeUser = null; + + private ?int $payeeUserId = null; + + private string $description = ''; + + private float $sum = 0.0; + + private DateTime $time; + + public function setCommunity(Community $community): void + { + $this->community = $community; + } + + public function setCommunityId(int $communityId): void + { + $this->communityId = $communityId; + } + + public function setCurrency(Currency $currency): void + { + $this->currency = $currency; + } + + public function setCurrencyId(int $currencyId): void + { + $this->currencyId = $currencyId; + } + + public function setPayerUser(User $payerUser): void + { + $this->payerUser = $payerUser; + } + + public function setPayerUserId(int $payerUserId): void + { + $this->payerUserId = $payerUserId; + } + + public function setPayeeUser(?User $payeeUser): void + { + $this->payeeUser = $payeeUser; + } + + public function setPayeeUserId(?int $payeeUserId): void + { + $this->payeeUserId = $payeeUserId; + } + + public function setDescription(string $description): void + { + $this->description = $description; + } + + public function setSum(float $sum): void + { + $this->sum = $sum; + } + + public function setTimeDate(DateTime $time): void + { + $this->time = $time; + } + + public function setTime(string $time): void + { + $this->time = new DateTime($time); + } + + public function getCommunity(): ?Community + { + return $this->community; + } + + public function getCommunityId(): int + { + return $this->communityId; + } + + public function getCurrency(): ?Currency + { + return $this->currency; + } + + public function getCurrencyId(): int + { + return $this->currencyId; + } + + public function getPayerUser(): ?User + { + return $this->payerUser; + } + + public function getPayerUserId(): int + { + return $this->payerUserId; + } + + public function getPayeeUser(): ?User + { + return $this->payeeUser; + } + + public function getPayeeUserId(): ?int + { + return $this->payeeUserId; + } + + public function getDescription(): string + { + return $this->description; + } + + public function getSum(): float + { + return $this->sum; + } + + public function getTimeDate(): DateTime + { + return $this->time; + } + + public function getTime(): string + { + return $this->time->format('Y-m-d H:i:s'); + } +} diff --git a/src/Repository/TransactionRepository.php b/src/Repository/TransactionRepository.php new file mode 100644 index 0000000..4ef85bc --- /dev/null +++ b/src/Repository/TransactionRepository.php @@ -0,0 +1,44 @@ +selectFromDbById($id, Transaction::class); + } + + public function getAllByCommunity(Community $community, bool $useRelations = false, array $withRelations = []): Generator + { + $select = $this->selectAllByCommunity($community); + + yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations); + } + + public function countAllByCommunity(Community $community): int + { + return $this->selectAllByCommunity($community)->count(); + } + + public function getPagedByCommunity(Community $community, int $start, int $limit, bool $useRelations = false, array $withRelations = []): Generator + { + $select = new Select(Container::$dbConnection); + $select->where('community_id', '=', $community->getId()); + $select->orderBy('time', 'DESC'); + $select->limit($limit, $start); + + yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations); + } + + private function selectAllByCommunity(Community $community) + { + $select = new Select(Container::$dbConnection, Transaction::getTable()); + $select->where('community_id', '=', $community->getId()); + return $select; + } +} diff --git a/views/account/account.php b/views/account/account.php index f414387..10eb87a 100644 --- a/views/account/account.php +++ b/views/account/account.php @@ -4,7 +4,7 @@ @section(main)