feat: net balance labels

This commit is contained in:
filippo-ferrari 2024-06-22 13:29:53 +02:00
parent 02e81b4b63
commit f1650ba542
2 changed files with 27 additions and 4 deletions

View file

@ -59,9 +59,9 @@ public class PersonService {
return this.expenseService.findCreditByUser(person).stream().map(Expense::getCost).reduce(BigDecimal.ZERO, BigDecimal::add);
}
public BigDecimal calculateTotalExpenses(final Person person) {
public BigDecimal calculateNetBalance(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);
return credit.subtract(debit);
}
}

View file

@ -11,6 +11,7 @@ import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.Notification.Position;
@ -21,6 +22,7 @@ import com.vaadin.flow.component.textfield.EmailField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.ValidationException;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.PageTitle;
@ -29,6 +31,7 @@ import com.vaadin.flow.spring.data.VaadinSpringDataHelpers;
import org.springframework.data.domain.PageRequest;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import java.math.BigDecimal;
import java.util.Optional;
@PageTitle("People")
@ -71,7 +74,13 @@ 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.addColumn(personService::calculateNetBalance).setHeader("Total Expenses value").setSortable(true);
grid.addColumn(new ComponentRenderer<>(persona -> {
final var netBalance = personService.calculateNetBalance(persona);
return createBadge(netBalance);
})).setHeader("Balance Status").setSortable(true);
grid.getColumns().forEach(col -> col.setAutoWidth(true));
grid.setItems(query -> personService.list(
PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query)))
@ -207,4 +216,18 @@ public class PeopleView extends Div implements BeforeEnterObserver {
binder.readBean(this.person);
}
}
private Span createBadge(BigDecimal netBalance) {
Span badge = new Span();
if (netBalance.compareTo(BigDecimal.ZERO) < 0) {
badge.setText("Credit");
badge.getElement().getThemeList().add("badge success");
} else if (netBalance.compareTo(BigDecimal.ZERO) > 0) {
badge.setText("Debit");
badge.getElement().getThemeList().add("badge error");
} else {
badge.setText("Nothing");
badge.getElement().getThemeList().add("badge contrast");
}
return badge;
}}