diff --git a/financer-common/src/main/java/de/financer/model/AccountStatistic.java b/financer-common/src/main/java/de/financer/model/AccountStatistic.java index 1a4d253..ab0e604 100644 --- a/financer-common/src/main/java/de/financer/model/AccountStatistic.java +++ b/financer-common/src/main/java/de/financer/model/AccountStatistic.java @@ -21,6 +21,7 @@ public class AccountStatistic { private long transactionCountFrom; private long spendingTotalTo; private long transactionCountTo; + private long endBalance; public Long getId() { return id; @@ -73,4 +74,12 @@ public class AccountStatistic { public void setTransactionCountTo(long transactionCountTo) { this.transactionCountTo = transactionCountTo; } + + public long getEndBalance() { + return endBalance; + } + + public void setEndBalance(long endBalance) { + this.endBalance = endBalance; + } } diff --git a/financer-server/pom.xml b/financer-server/pom.xml index 88af9aa..4ef1ecb 100644 --- a/financer-server/pom.xml +++ b/financer-server/pom.xml @@ -17,7 +17,7 @@ jar - postgres,dev + hsqldb,dev mk diff --git a/financer-server/src/main/java/de/financer/dba/AccountStatisticRepository.java b/financer-server/src/main/java/de/financer/dba/AccountStatisticRepository.java index 078b20e..142ef61 100644 --- a/financer-server/src/main/java/de/financer/dba/AccountStatisticRepository.java +++ b/financer-server/src/main/java/de/financer/dba/AccountStatisticRepository.java @@ -16,4 +16,7 @@ public interface AccountStatisticRepository extends CrudRepository getAccountsWithoutStatisticInPeriod(Period period, AccountStatus accountStatus); + + @Query("SELECT accStat FROM AccountStatistic accStat WHERE accStat.period = :period") + Iterable getAllForPeriod(Period period); } diff --git a/financer-server/src/main/java/de/financer/service/AccountStatisticService.java b/financer-server/src/main/java/de/financer/service/AccountStatisticService.java index 22bf82d..2c59930 100644 --- a/financer-server/src/main/java/de/financer/service/AccountStatisticService.java +++ b/financer-server/src/main/java/de/financer/service/AccountStatisticService.java @@ -63,8 +63,8 @@ public class AccountStatisticService { } /** - * This method creates an {@link AccountStatistic}s entry with from and to spending 0, - * for every account not used in a booking in the given period. + * This method creates an {@link AccountStatistic}s entry with from and to spending 0, for every + * account not used in a booking in the given period. * * @param period to generate the null statistic entries for */ @@ -74,20 +74,20 @@ public class AccountStatisticService { .getAccountsWithoutStatisticInPeriod(period, AccountStatus.OPEN); this.accountStatisticRepository.saveAll(IterableUtils.toList(accountsWithoutStat) - .stream() - .map(a -> { - final AccountStatistic as = new AccountStatistic(); + .stream() + .map(a -> { + final AccountStatistic as = new AccountStatistic(); - as.setTransactionCountTo(0); - as.setSpendingTotalTo(0); - as.setTransactionCountFrom(0); - as.setSpendingTotalFrom(0); - as.setAccount(a); - as.setPeriod(period); + as.setTransactionCountTo(0); + as.setSpendingTotalTo(0); + as.setTransactionCountFrom(0); + as.setSpendingTotalFrom(0); + as.setAccount(a); + as.setPeriod(period); - return as; - }) - .collect(Collectors.toList())); + return as; + }) + .collect(Collectors.toList())); } private AccountStatistic calculateInternal(Account account, Period period, long amount, boolean from, int multiplier) { @@ -111,4 +111,19 @@ public class AccountStatisticService { return accountStatistic; } + + @Transactional(propagation = Propagation.REQUIRED) + public void takeOverEndBalances(Period period) { + final Iterable allForPeriod = this.accountStatisticRepository.getAllForPeriod(period); + + this.accountStatisticRepository.saveAll(IterableUtils.toList(allForPeriod) + .stream() + .map(as -> { + as.setEndBalance(as.getAccount().getCurrentBalance()); + return as; + } + ) + .collect(Collectors.toList()) + ); + } } diff --git a/financer-server/src/main/java/de/financer/service/PeriodService.java b/financer-server/src/main/java/de/financer/service/PeriodService.java index a0a9619..aa3c103 100644 --- a/financer-server/src/main/java/de/financer/service/PeriodService.java +++ b/financer-server/src/main/java/de/financer/service/PeriodService.java @@ -66,6 +66,8 @@ public class PeriodService { this.accountStatisticService.generateNullStatisticsForUnusedAccounts(currentPeriod); + this.accountStatisticService.takeOverEndBalances(currentPeriod); + response = ResponseReason.OK; } catch (Exception e) { LOGGER.error("Could not close current expense period!", e); @@ -90,6 +92,8 @@ public class PeriodService { this.periodRepository.save(expenseYearPeriod); this.periodRepository.save(nextExpenseYearPeriod); + + this.accountStatisticService.takeOverEndBalances(expenseYearPeriod); } } diff --git a/financer-server/src/main/resources/database/common/V28_0_0__endBalance.sql b/financer-server/src/main/resources/database/common/V28_0_0__endBalance.sql new file mode 100644 index 0000000..f978267 --- /dev/null +++ b/financer-server/src/main/resources/database/common/V28_0_0__endBalance.sql @@ -0,0 +1,2 @@ +ALTER TABLE account_statistic + ADD COLUMN end_balance BIGINT; \ No newline at end of file diff --git a/financer-web-client/src/main/resources/static/changelog.txt b/financer-web-client/src/main/resources/static/changelog.txt index 4930007..e5348f5 100644 --- a/financer-web-client/src/main/resources/static/changelog.txt +++ b/financer-web-client/src/main/resources/static/changelog.txt @@ -1,3 +1,6 @@ +v27 -> v28: +- Add account end balance to account statistics for closed expense periods. This is currently not visible in the UI + v26 -> v27: - Changed sort order of accounts in overview page. The accounts are now sorted by the account type first (BCILES), then by the account group name and then by the account ID, leading to an overall more organic order of accounts