From 5112c83379e147facce1092087d6b83884ba6b86 Mon Sep 17 00:00:00 2001 From: Filippo Ferrari Date: Tue, 4 Jun 2024 14:34:08 +0100 Subject: [PATCH] feat: total expense by user calculations --- .../java/com/application/munera/services/PersonService.java | 6 ++++++ .../application/munera/views/expenses/CategoriesView.java | 1 - .../com/application/munera/views/expenses/ExpensesView.java | 4 +--- .../com/application/munera/views/expenses/PeopleView.java | 1 + 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/application/munera/services/PersonService.java b/src/main/java/com/application/munera/services/PersonService.java index 097840a..be2685e 100644 --- a/src/main/java/com/application/munera/services/PersonService.java +++ b/src/main/java/com/application/munera/services/PersonService.java @@ -58,4 +58,10 @@ public class PersonService { public BigDecimal calculateCredit(final Person person) { 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); + } } diff --git a/src/main/java/com/application/munera/views/expenses/CategoriesView.java b/src/main/java/com/application/munera/views/expenses/CategoriesView.java index 7b82b3c..f7ba8e3 100644 --- a/src/main/java/com/application/munera/views/expenses/CategoriesView.java +++ b/src/main/java/com/application/munera/views/expenses/CategoriesView.java @@ -196,6 +196,5 @@ public class CategoriesView extends Div implements BeforeEnterObserver { private void populateForm(Category value) { this.category = value; binder.readBean(this.category); - } } diff --git a/src/main/java/com/application/munera/views/expenses/ExpensesView.java b/src/main/java/com/application/munera/views/expenses/ExpensesView.java index 251acb9..8f6c1ee 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -87,9 +87,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { // Configure Grid grid.addColumn(Expense::getName).setHeader("Name").setSortable(true); grid.addColumn(Expense::getCost).setHeader("Amount").setSortable(true); - grid.addColumn(expenseCategory -> expenseCategory.getCategory().getName()) - .setHeader("Category") - .setSortable(true); + grid.addColumn(expenseCategory -> expenseCategory.getCategory().getName()).setHeader("Category").setSortable(true); grid.addColumn(Expense::getPeriodInterval).setHeader("Period Interval").setSortable(true); grid.addColumn(Expense::getPeriodUnit).setHeader("Period Unit").setSortable(true); grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true); diff --git a/src/main/java/com/application/munera/views/expenses/PeopleView.java b/src/main/java/com/application/munera/views/expenses/PeopleView.java index 9ca9255..8ceb100 100644 --- a/src/main/java/com/application/munera/views/expenses/PeopleView.java +++ b/src/main/java/com/application/munera/views/expenses/PeopleView.java @@ -71,6 +71,7 @@ public class PeopleView extends Div implements BeforeEnterObserver { grid.addColumn(Person::getEmail).setHeader("Email").setSortable(true); grid.addColumn(personService::calculateDebt).setHeader("Debt").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.setItems(query -> personService.list( PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query)))