Add current expenses to the overview
The period start is configurable.
This commit is contained in:
@@ -21,6 +21,7 @@ public class FinancerConfig {
|
||||
private String dateFormat;
|
||||
private Collection<String> mailRecipients;
|
||||
private String fromAddress;
|
||||
private Integer monthPeriodStartDay;
|
||||
|
||||
/**
|
||||
* @return the raw country code, mostly an uppercase ISO 3166 2-letter code
|
||||
@@ -109,4 +110,32 @@ public class FinancerConfig {
|
||||
public void setFromAddress(String fromAddress) {
|
||||
this.fromAddress = fromAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* The day of month that indicates a start of an expense period. Valid values range from 1-28. There is no special
|
||||
* handling for months with more days.
|
||||
* If the value is 15 for example an expense period is always from the 15th of the current month to the 15th of the
|
||||
* next month:
|
||||
* <ul>
|
||||
* <li>15.01.2019 - 15.02.2019</li>
|
||||
* <li>15.02.2019 - 15.03.2019</li>
|
||||
* <li>...</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return the day of month indicating the start of an expense period
|
||||
*/
|
||||
public Integer getMonthPeriodStartDay() {
|
||||
return monthPeriodStartDay;
|
||||
}
|
||||
|
||||
public void setMonthPeriodStartDay(Integer monthPeriodStartDay) {
|
||||
if (monthPeriodStartDay == null) {
|
||||
throw new IllegalArgumentException("Parameter 'financer.monthPeriodStartDay' is not set!");
|
||||
}
|
||||
else if (monthPeriodStartDay < 1 || monthPeriodStartDay > 28) {
|
||||
throw new IllegalArgumentException("Parameter 'financer.monthPeriodStartDay' is out of range! Valid range from 1-28");
|
||||
}
|
||||
|
||||
this.monthPeriodStartDay = monthPeriodStartDay;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,4 +79,19 @@ public class TransactionController {
|
||||
|
||||
return responseReason.toResponseEntity();
|
||||
}
|
||||
|
||||
@RequestMapping("getExpensesCurrentPeriod")
|
||||
public Long getExpensesCurrentPeriod() {
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("/transactions/getExpensesCurrentPeriod called");
|
||||
}
|
||||
|
||||
final Long response = this.transactionService.getExpensesCurrentPeriod();
|
||||
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug(String.format("/transactions/getExpensesCurrentPeriod returns with %s", response));
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
package de.financer.dba;
|
||||
|
||||
import de.financer.model.Account;
|
||||
import de.financer.model.AccountType;
|
||||
import de.financer.model.Transaction;
|
||||
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;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public interface TransactionRepository extends CrudRepository<Transaction, Long> {
|
||||
Iterable<Transaction> findTransactionsByFromAccountOrToAccount(Account fromAccount, Account toAccount);
|
||||
|
||||
@Query("SELECT SUM(t.amount) FROM Transaction t JOIN t.toAccount a WHERE t.date BETWEEN :periodStart AND :periodEnd AND a.type IN :expenseTypes")
|
||||
Long getExpensesCurrentPeriod(LocalDate periodStart, LocalDate periodEnd, AccountType... expenseTypes);
|
||||
}
|
||||
|
||||
@@ -66,8 +66,8 @@ public class TransactionService {
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public ResponseReason createTransaction(String fromAccountKey, String toAccountKey, Long amount, String date,
|
||||
String description)
|
||||
{
|
||||
String description
|
||||
) {
|
||||
return this.createTransaction(fromAccountKey, toAccountKey, amount, date, description, null);
|
||||
}
|
||||
|
||||
@@ -95,8 +95,7 @@ public class TransactionService {
|
||||
if (AccountType.START.equals(fromAccount.getType()) && AccountType.LIABILITY.equals(toAccount.getType())) {
|
||||
toAccount.setCurrentBalance(toAccount.getCurrentBalance() + (this.ruleService
|
||||
.getMultiplierToAccount(toAccount) * amount * -1));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
toAccount.setCurrentBalance(toAccount.getCurrentBalance() + (this.ruleService
|
||||
.getMultiplierToAccount(toAccount) * amount));
|
||||
}
|
||||
@@ -216,8 +215,7 @@ public class TransactionService {
|
||||
|
||||
this.accountService.saveAccount(fromAccount);
|
||||
this.accountService.saveAccount(toAccount);
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Could not delete transaction!", e);
|
||||
|
||||
response = ResponseReason.UNKNOWN_ERROR;
|
||||
@@ -225,4 +223,14 @@ public class TransactionService {
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public Long getExpensesCurrentPeriod() {
|
||||
final LocalDate periodStart = LocalDate.now().withDayOfMonth(this.financerConfig.getMonthPeriodStartDay());
|
||||
final LocalDate periodEnd = periodStart.plusMonths(1);
|
||||
|
||||
final Long expensesCurrentPeriod = this.transactionRepository
|
||||
.getExpensesCurrentPeriod(periodStart, periodEnd, AccountType.EXPENSE, AccountType.LIABILITY);
|
||||
|
||||
return Optional.ofNullable(expensesCurrentPeriod).orElse(Long.valueOf(0l));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,4 +46,7 @@ spring.mail.host=localhost
|
||||
|
||||
# Disable JMX as we don't need it and it blocks parallel deployment on Tomcat
|
||||
# because the connection pool cannot shutdown properly
|
||||
spring.jmx.enabled=false
|
||||
spring.jmx.enabled=false
|
||||
|
||||
# The day of month indicating the start of an expense period. Valid values range from 1-28
|
||||
financer.monthPeriodStartDay=15
|
||||
Reference in New Issue
Block a user