Add chart report for expenses grouped by account
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package de.financer.controller;
|
||||
|
||||
import de.financer.ResponseReason;
|
||||
import de.financer.dto.AccountExpense;
|
||||
import de.financer.model.Account;
|
||||
import de.financer.service.AccountService;
|
||||
import org.slf4j.Logger;
|
||||
@@ -101,4 +102,13 @@ public class AccountController {
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@RequestMapping("getAccountExpenses")
|
||||
public Iterable<AccountExpense> getAccountExpenses(String periodStart, String periodEnd) {
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug(String.format("/accounts/getAccountExpenses got parameters: %s|%s", periodStart, periodEnd));
|
||||
}
|
||||
|
||||
return this.accountService.getAccountExpenses(periodStart, periodEnd);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.financer.dba;
|
||||
|
||||
import de.financer.dto.AccountExpense;
|
||||
import de.financer.model.Account;
|
||||
import de.financer.model.AccountType;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
@@ -7,10 +8,15 @@ import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public interface AccountRepository extends CrudRepository<Account, Long> {
|
||||
Account findByKey(String key);
|
||||
|
||||
@Query("SELECT SUM(a.currentBalance) FROM Account a WHERE a.type IN :accountTypes")
|
||||
Long getCurrentAssets(AccountType... accountTypes);
|
||||
|
||||
@Query("SELECT new de.financer.dto.AccountExpense(a, SUM(t.amount)) FROM Transaction t JOIN t.toAccount a WHERE a.type in :expenseTypes AND t.date BETWEEN :startDate AND :startEnd GROUP BY a")
|
||||
Iterable<AccountExpense> getAccountExpenses(LocalDate startDate, LocalDate startEnd, AccountType... expenseTypes);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package de.financer.service;
|
||||
|
||||
import de.financer.ResponseReason;
|
||||
import de.financer.config.FinancerConfig;
|
||||
import de.financer.dba.AccountRepository;
|
||||
import de.financer.dto.AccountExpense;
|
||||
import de.financer.model.Account;
|
||||
import de.financer.model.AccountGroup;
|
||||
import de.financer.model.AccountStatus;
|
||||
@@ -15,8 +17,10 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Collections;
|
||||
|
||||
@Service
|
||||
public class AccountService {
|
||||
@@ -24,9 +28,13 @@ public class AccountService {
|
||||
|
||||
@Autowired
|
||||
private AccountRepository accountRepository;
|
||||
|
||||
@Autowired
|
||||
private AccountGroupService accountGroupService;
|
||||
|
||||
@Autowired
|
||||
private FinancerConfig financerConfig;
|
||||
|
||||
/**
|
||||
* This method returns the account identified by the given key.
|
||||
*
|
||||
@@ -152,4 +160,22 @@ public class AccountService {
|
||||
public Long getCurrentAssets() {
|
||||
return this.accountRepository.getCurrentAssets(AccountType.BANK, AccountType.CASH);
|
||||
}
|
||||
|
||||
public Iterable<AccountExpense> getAccountExpenses(String periodStart, String periodEnd) {
|
||||
LocalDate startDate;
|
||||
LocalDate endDate;
|
||||
|
||||
try {
|
||||
startDate = LocalDate.parse(periodStart, DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat()));
|
||||
endDate = LocalDate.parse(periodEnd, DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat()));
|
||||
} catch (DateTimeParseException dtpe) {
|
||||
LOGGER.error(String
|
||||
.format("Could not parse periodStart (%s) or periodEnd (%s): %s", periodStart, periodEnd, dtpe
|
||||
.getMessage()));
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return this.accountRepository.getAccountExpenses(startDate, endDate, AccountType.LIABILITY, AccountType.EXPENSE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user