From 6ec073d08e84d21ea3bf739498b8e8c7e6736822 Mon Sep 17 00:00:00 2001 From: MK13 Date: Mon, 24 Jun 2019 21:11:36 +0200 Subject: [PATCH] Add current assets to status on overview page --- .../controller/AccountController.java | 15 ++++++ .../de/financer/dba/AccountRepository.java | 5 ++ .../de/financer/service/AccountService.java | 44 +++++++++++------- .../controller/AccountController.java | 2 + .../java/de/financer/controller/Function.java | 1 + .../template/GetCurrentAssetsTemplate.java | 15 ++++++ .../src/main/resources/favicon.ico | Bin 0 -> 318 bytes .../main/resources/i18n/message.properties | 1 + .../resources/i18n/message_de_DE.properties | 1 + .../templates/account/accountOverview.html | 4 ++ 10 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 financer-web-client/src/main/java/de/financer/controller/template/GetCurrentAssetsTemplate.java create mode 100644 financer-web-client/src/main/resources/favicon.ico diff --git a/financer-server/src/main/java/de/financer/controller/AccountController.java b/financer-server/src/main/java/de/financer/controller/AccountController.java index 46b0b04..8488c4f 100644 --- a/financer-server/src/main/java/de/financer/controller/AccountController.java +++ b/financer-server/src/main/java/de/financer/controller/AccountController.java @@ -86,4 +86,19 @@ public class AccountController { return responseReason.toResponseEntity(); } + + @RequestMapping("getCurrentAssets") + public Long getCurrentAssets() { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("/accounts/getCurrentAssets called"); + } + + final Long response = this.accountService.getCurrentAssets(); + + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(String.format("/accounts/getCurrentAssets returns with %s", response)); + } + + return response; + } } diff --git a/financer-server/src/main/java/de/financer/dba/AccountRepository.java b/financer-server/src/main/java/de/financer/dba/AccountRepository.java index 13cdf21..0a0a072 100644 --- a/financer-server/src/main/java/de/financer/dba/AccountRepository.java +++ b/financer-server/src/main/java/de/financer/dba/AccountRepository.java @@ -1,6 +1,8 @@ package de.financer.dba; import de.financer.model.Account; +import de.financer.model.AccountType; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @@ -8,4 +10,7 @@ import org.springframework.transaction.annotation.Transactional; @Transactional(propagation = Propagation.REQUIRED) public interface AccountRepository extends CrudRepository { Account findByKey(String key); + + @Query("SELECT SUM(a.currentBalance) FROM Account a WHERE a.type IN :accountTypes") + Long getCurrentAssets(AccountType... accountTypes); } diff --git a/financer-server/src/main/java/de/financer/service/AccountService.java b/financer-server/src/main/java/de/financer/service/AccountService.java index 9e4559a..183ff25 100644 --- a/financer-server/src/main/java/de/financer/service/AccountService.java +++ b/financer-server/src/main/java/de/financer/service/AccountService.java @@ -31,6 +31,7 @@ public class AccountService { * This method returns the account identified by the given key. * * @param key the key to get the account for + * * @return the account or null if no account with the given key can be found */ public Account getAccountByKey(String key) { @@ -38,15 +39,16 @@ public class AccountService { } /** - * @return all existing accounts, regardless of their type or status. This explicitly covers accounts in {@link AccountStatus#CLOSED CLOSED} as well. + * @return all existing accounts, regardless of their type or status. This explicitly covers accounts in {@link + * AccountStatus#CLOSED CLOSED} as well. */ public Iterable getAll() { return this.accountRepository.findAll(); } /** - * This method saves the given account. It either updates the account if it already exists or inserts - * it if it's new. + * This method saves the given account. It either updates the account if it already exists or inserts it if it's + * new. * * @param account the account to save */ @@ -56,18 +58,18 @@ public class AccountService { } /** - * This method creates new account with the given key and type. The account has status {@link AccountStatus#OPEN OPEN} - * and a current balance of 0. + * This method creates new account with the given key and type. The account has status {@link AccountStatus#OPEN + * OPEN} and a current balance of 0. * * @param key the key of the new account * @param type the type of the new account. Must be one of {@link AccountType}. * @param accountGroupName the name of the account group to use, can be null - * @return {@link ResponseReason#INVALID_ACCOUNT_TYPE} if the given type is not a valid {@link AccountType}, - * {@link ResponseReason#UNKNOWN_ERROR} if an unexpected error occurs, - * {@link ResponseReason#OK} if the operation completed successfully, - * {@link ResponseReason#DUPLICATE_ACCOUNT_KEY} if an account with the given key - * already exists and {@link ResponseReason#ACCOUNT_GROUP_NOT_FOUND} if the optional parameter - * accountGroupName does not identify a valid account group. Never returns null. + * + * @return {@link ResponseReason#INVALID_ACCOUNT_TYPE} if the given type is not a valid {@link AccountType}, {@link + * ResponseReason#UNKNOWN_ERROR} if an unexpected error occurs, {@link ResponseReason#OK} if the operation completed + * successfully, {@link ResponseReason#DUPLICATE_ACCOUNT_KEY} if an account with the given key already exists and + * {@link ResponseReason#ACCOUNT_GROUP_NOT_FOUND} if the optional parameter + * accountGroupName does not identify a valid account group. Never returns null. */ @Transactional(propagation = Propagation.SUPPORTS) public ResponseReason createAccount(String key, String type, String accountGroupName) { @@ -96,13 +98,11 @@ public class AccountService { try { this.accountRepository.save(account); - } - catch (DataIntegrityViolationException dive) { + } catch (DataIntegrityViolationException dive) { LOGGER.error(String.format("Duplicate key! %s|%s|%s", key, type, accountGroupName), dive); return ResponseReason.DUPLICATE_ACCOUNT_KEY; - } - catch (Exception e) { + } catch (Exception e) { LOGGER.error(String.format("Could not save account %s|%s|%s", key, type, accountGroupName), e); return ResponseReason.UNKNOWN_ERROR; @@ -133,8 +133,7 @@ public class AccountService { try { this.accountRepository.save(account); - } - catch (Exception e) { + } catch (Exception e) { LOGGER.error(String.format("Could not update account status %s|%s", key, accountStatus.name()), e); return ResponseReason.UNKNOWN_ERROR; @@ -142,4 +141,15 @@ public class AccountService { return ResponseReason.OK; } + + /** + * Get the current assets for the specified account types, basically the sum of all the current balances of all + * accounts with the given types. We are specifying BANK and CASH here as these are virtually always available on a + * very short notice. So this is the amount of money we have at disposal right now. + * + * @return the current assets + */ + public Long getCurrentAssets() { + return this.accountRepository.getCurrentAssets(AccountType.BANK, AccountType.CASH); + } } diff --git a/financer-web-client/src/main/java/de/financer/controller/AccountController.java b/financer-web-client/src/main/java/de/financer/controller/AccountController.java index 8c69a8f..fb4971e 100644 --- a/financer-web-client/src/main/java/de/financer/controller/AccountController.java +++ b/financer-web-client/src/main/java/de/financer/controller/AccountController.java @@ -32,11 +32,13 @@ public class AccountController { .exchange(this.financerConfig); final ResponseEntity> rtAllActRes = new GetAllActiveRecurringTransactionsTemplate() .exchange(this.financerConfig); + final ResponseEntity currentAssets = new GetCurrentAssetsTemplate().exchange(this.financerConfig); final boolean showClosedBoolean = BooleanUtils.toBoolean(showClosed); model.addAttribute("accounts", ControllerUtils.filterAndSortAccounts(response.getBody(), showClosedBoolean)); model.addAttribute("rtDueTodayCount", IterableUtils.size(rtDtRes.getBody())); model.addAttribute("rtAllActiveCount", IterableUtils.size(rtAllActRes.getBody())); + model.addAttribute("currentAssets", currentAssets.getBody()); model.addAttribute("showClosed", showClosedBoolean); ControllerUtils.addVersionAttribute(model, this.financerConfig); diff --git a/financer-web-client/src/main/java/de/financer/controller/Function.java b/financer-web-client/src/main/java/de/financer/controller/Function.java index 602a4fd..009fe1f 100644 --- a/financer-web-client/src/main/java/de/financer/controller/Function.java +++ b/financer-web-client/src/main/java/de/financer/controller/Function.java @@ -6,6 +6,7 @@ public enum Function { ACC_CREATE_ACCOUNT("accounts/createAccount"), ACC_CLOSE_ACCOUNT("accounts/closeAccount"), ACC_OPEN_ACCOUNT("accounts/openAccount"), + ACC_CURRENT_ASSETS("accounts/getCurrentAssets"), ACC_GP_CREATE_ACCOUNT_GROUP("accountGroups/createAccountGroup"), ACC_GP_GET_ALL("accountGroups/getAll"), diff --git a/financer-web-client/src/main/java/de/financer/controller/template/GetCurrentAssetsTemplate.java b/financer-web-client/src/main/java/de/financer/controller/template/GetCurrentAssetsTemplate.java new file mode 100644 index 0000000..732bf53 --- /dev/null +++ b/financer-web-client/src/main/java/de/financer/controller/template/GetCurrentAssetsTemplate.java @@ -0,0 +1,15 @@ +package de.financer.controller.template; + +import de.financer.config.FinancerConfig; +import de.financer.controller.Function; +import de.financer.util.ControllerUtils; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.ResponseEntity; + +public class GetCurrentAssetsTemplate { + public ResponseEntity exchange(FinancerConfig financerConfig) { + return new FinancerRestTemplate().exchange(ControllerUtils + .buildUrl(financerConfig, Function.ACC_CURRENT_ASSETS), new ParameterizedTypeReference() { + }); + } +} diff --git a/financer-web-client/src/main/resources/favicon.ico b/financer-web-client/src/main/resources/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..ecd593e26bb9e29daddf1369656591d9032f2b2b GIT binary patch literal 318 zcmb78F%rNa2n%<8Kv+6=^qKv+4Imxdgo6kq0$_5I$`^(KH-Mp_;!#9D?9E&$W^HrM pejcOiBl9pI!C7S^SFQ7E{_|FYR)bB~`oJU*i_r@i3y%-p1RvdF3l;zX literal 0 HcmV?d00001 diff --git a/financer-web-client/src/main/resources/i18n/message.properties b/financer-web-client/src/main/resources/i18n/message.properties index 9d7b159..7eb5b38 100644 --- a/financer-web-client/src/main/resources/i18n/message.properties +++ b/financer-web-client/src/main/resources/i18n/message.properties @@ -10,6 +10,7 @@ financer.account-overview.available-actions.create-account-group=Create new acco financer.account-overview.status=Status\: financer.account-overview.status.recurring-transaction-due-today=Recurring transactions due today\: financer.account-overview.status.recurring-transaction-active=Active recurring transactions\: +financer.account-overview.status.current-assets=Current assets\: financer.account-overview.table-header.id=ID financer.account-overview.table-header.key=Account financer.account-overview.table-header.group=Group diff --git a/financer-web-client/src/main/resources/i18n/message_de_DE.properties b/financer-web-client/src/main/resources/i18n/message_de_DE.properties index ddb1924..64b6eef 100644 --- a/financer-web-client/src/main/resources/i18n/message_de_DE.properties +++ b/financer-web-client/src/main/resources/i18n/message_de_DE.properties @@ -10,6 +10,7 @@ financer.account-overview.available-actions.create-account-group=Neue Konto-Grup financer.account-overview.status=Status\: financer.account-overview.status.recurring-transaction-due-today=Wiederkehrende Buchungen heute f\u00E4llig\: financer.account-overview.status.recurring-transaction-active=Aktive wiederkehrende Buchungen\: +financer.account-overview.status.current-assets=Umlaufverm\u00F6gen\: financer.account-overview.table-header.id=ID financer.account-overview.table-header.key=Konto financer.account-overview.table-header.group=Gruppe diff --git a/financer-web-client/src/main/resources/templates/account/accountOverview.html b/financer-web-client/src/main/resources/templates/account/accountOverview.html index 3abcf48..a633f54 100644 --- a/financer-web-client/src/main/resources/templates/account/accountOverview.html +++ b/financer-web-client/src/main/resources/templates/account/accountOverview.html @@ -10,6 +10,10 @@

+
+ + +