Add end balance to account statistic

This commit is contained in:
2020-03-16 20:35:56 +01:00
parent eee891a534
commit 442f2d6087
7 changed files with 51 additions and 15 deletions

View File

@@ -16,4 +16,7 @@ public interface AccountStatisticRepository extends CrudRepository<AccountStatis
@Query("SELECT a FROM Account a WHERE a NOT IN (SELECT accStat.account FROM AccountStatistic accStat WHERE accStat.period = :period) AND a.status = :accountStatus")
Iterable<Account> getAccountsWithoutStatisticInPeriod(Period period, AccountStatus accountStatus);
@Query("SELECT accStat FROM AccountStatistic accStat WHERE accStat.period = :period")
Iterable<AccountStatistic> getAllForPeriod(Period period);
}

View File

@@ -63,8 +63,8 @@ public class AccountStatisticService {
}
/**
* This method creates an {@link AccountStatistic}s entry with from and to spending <code>0</code>,
* 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 <code>0</code>, 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<AccountStatistic> allForPeriod = this.accountStatisticRepository.getAllForPeriod(period);
this.accountStatisticRepository.saveAll(IterableUtils.toList(allForPeriod)
.stream()
.map(as -> {
as.setEndBalance(as.getAccount().getCurrentBalance());
return as;
}
)
.collect(Collectors.toList())
);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,2 @@
ALTER TABLE account_statistic
ADD COLUMN end_balance BIGINT;