feat: total expense by user calculations

This commit is contained in:
Filippo Ferrari 2024-06-04 14:34:08 +01:00
parent 0f3f70be72
commit 5112c83379
4 changed files with 8 additions and 4 deletions

View file

@ -58,4 +58,10 @@ public class PersonService {
public BigDecimal calculateCredit(final Person person) { public BigDecimal calculateCredit(final Person person) {
return this.expenseService.findCreditByUser(person).stream().map(Expense::getCost).reduce(BigDecimal.ZERO, BigDecimal::add); return this.expenseService.findCreditByUser(person).stream().map(Expense::getCost).reduce(BigDecimal.ZERO, BigDecimal::add);
} }
public BigDecimal calculateTotalExpenses(final Person person) {
final var credit = this.expenseService.findCreditByUser(person).stream().map(Expense::getCost).reduce(BigDecimal.ZERO, BigDecimal::add);
final var debit = this.expenseService.findDebtByUser(person).stream().map(Expense::getCost).reduce(BigDecimal.ZERO, BigDecimal::add);
return credit.add(debit);
}
} }

View file

@ -196,6 +196,5 @@ public class CategoriesView extends Div implements BeforeEnterObserver {
private void populateForm(Category value) { private void populateForm(Category value) {
this.category = value; this.category = value;
binder.readBean(this.category); binder.readBean(this.category);
} }
} }

View file

@ -87,9 +87,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
// Configure Grid // Configure Grid
grid.addColumn(Expense::getName).setHeader("Name").setSortable(true); grid.addColumn(Expense::getName).setHeader("Name").setSortable(true);
grid.addColumn(Expense::getCost).setHeader("Amount").setSortable(true); grid.addColumn(Expense::getCost).setHeader("Amount").setSortable(true);
grid.addColumn(expenseCategory -> expenseCategory.getCategory().getName()) grid.addColumn(expenseCategory -> expenseCategory.getCategory().getName()).setHeader("Category").setSortable(true);
.setHeader("Category")
.setSortable(true);
grid.addColumn(Expense::getPeriodInterval).setHeader("Period Interval").setSortable(true); grid.addColumn(Expense::getPeriodInterval).setHeader("Period Interval").setSortable(true);
grid.addColumn(Expense::getPeriodUnit).setHeader("Period Unit").setSortable(true); grid.addColumn(Expense::getPeriodUnit).setHeader("Period Unit").setSortable(true);
grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true); grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true);

View file

@ -71,6 +71,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
grid.addColumn(Person::getEmail).setHeader("Email").setSortable(true); grid.addColumn(Person::getEmail).setHeader("Email").setSortable(true);
grid.addColumn(personService::calculateDebt).setHeader("Debt").setSortable(true); grid.addColumn(personService::calculateDebt).setHeader("Debt").setSortable(true);
grid.addColumn(personService::calculateCredit).setHeader("Credit").setSortable(true); grid.addColumn(personService::calculateCredit).setHeader("Credit").setSortable(true);
grid.addColumn(personService::calculateTotalExpenses).setHeader("Total Expenses value").setSortable(true);
grid.getColumns().forEach(col -> col.setAutoWidth(true)); grid.getColumns().forEach(col -> col.setAutoWidth(true));
grid.setItems(query -> personService.list( grid.setItems(query -> personService.list(
PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query))) PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query)))