Compare commits

...

3 commits

Author SHA1 Message Date
filippo-ferrari
8e5ba1bb2b feat: horizontal layout for checkboxes 2024-07-07 17:50:18 +02:00
filippo-ferrari
bc1093b736 feat: is Paid checkbox added 2024-07-07 17:41:14 +02:00
filippo-ferrari
239a08a326 feat: filtering by status for better math 2024-07-07 17:34:40 +02:00
4 changed files with 25 additions and 12 deletions

View file

@ -21,5 +21,11 @@ public interface ExpenseRepository extends JpaRepository<Expense, Long>, JpaSpec
@Query("SELECT e FROM Expense e WHERE YEAR(e.date) = :year") @Query("SELECT e FROM Expense e WHERE YEAR(e.date) = :year")
List<Expense> findAllByYear(@Param("year") int year); List<Expense> findAllByYear(@Param("year") int year);
@Query("SELECT e FROM Expense e JOIN e.creditors c WHERE c.id = :personId AND e.isResolved = false")
Set<Expense> findUnpaidCreditorsExpensesByPersonId(@Param("personId") Long personId);
@Query("SELECT e FROM Expense e JOIN e.debtors d WHERE d.id = :personId AND e.isResolved = false")
Set<Expense> findUnpaidDebtorsExpensesByPersonId(@Param("personId") Long personId);
boolean existsByIdAndIsResolvedTrue(Long id); boolean existsByIdAndIsResolvedTrue(Long id);
} }

View file

@ -32,6 +32,14 @@ public class ExpenseService {
public Collection<Expense> findCreditByUser(final Person person) { public Collection<Expense> findCreditByUser(final Person person) {
return repository.findCreditorsExpensesByPersonId(person.getId()); return repository.findCreditorsExpensesByPersonId(person.getId());
} }
public Collection<Expense> findUnpaidDebtByUser(final Person person) {
return repository.findUnpaidDebtorsExpensesByPersonId(person.getId());
}
public Collection<Expense> findUnpaidCreditByUser(final Person person) {
return repository.findUnpaidCreditorsExpensesByPersonId(person.getId());
}
public List<Expense> findAll() {return repository.findAll();} public List<Expense> findAll() {return repository.findAll();}
public void update(Expense entity) { public void update(Expense entity) {

View file

@ -60,8 +60,8 @@ public class PersonService {
} }
public BigDecimal calculateNetBalance(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 credit = this.expenseService.findUnpaidCreditByUser(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); final var debit = this.expenseService.findUnpaidDebtByUser(person).stream().map(Expense::getCost).reduce(BigDecimal.ZERO, BigDecimal::add);
return credit.subtract(debit); return credit.subtract(debit);
} }
} }

View file

@ -30,7 +30,6 @@ import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.BeanValidationBinder; import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.ValidationException; import com.vaadin.flow.data.binder.ValidationException;
import com.vaadin.flow.data.renderer.ComponentRenderer; import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.data.renderer.LitRenderer;
import com.vaadin.flow.router.*; import com.vaadin.flow.router.*;
import com.vaadin.flow.spring.data.VaadinSpringDataHelpers; import com.vaadin.flow.spring.data.VaadinSpringDataHelpers;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@ -68,6 +67,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
private ComboBox<Category> category; private ComboBox<Category> category;
private TextArea description; private TextArea description;
private Checkbox isPeriodic; private Checkbox isPeriodic;
private Checkbox isResolved;
private ComboBox<PeriodUnit> periodUnit; private ComboBox<PeriodUnit> periodUnit;
private TextField periodInterval; private TextField periodInterval;
private DatePicker date; private DatePicker date;
@ -124,6 +124,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
// We set initial value of isPeriodic to true and show period fields // We set initial value of isPeriodic to true and show period fields
isPeriodic.setValue(false); isPeriodic.setValue(false);
isResolved.setValue(false);
periodUnit.setVisible(false); periodUnit.setVisible(false);
periodInterval.setVisible(false); periodInterval.setVisible(false);
@ -232,7 +233,6 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
category.setItems(categoryService.findAll()); category.setItems(categoryService.findAll());
category.setItemLabelGenerator(Category::getName); category.setItemLabelGenerator(Category::getName);
description = new TextArea("Description"); description = new TextArea("Description");
isPeriodic = new Checkbox("Is Periodic");
periodUnit = new ComboBox<>("Period Unit"); periodUnit = new ComboBox<>("Period Unit");
periodUnit.setItems(PeriodUnit.values()); periodUnit.setItems(PeriodUnit.values());
periodInterval = new TextField("Period Interval"); periodInterval = new TextField("Period Interval");
@ -246,15 +246,14 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
debtors.setItems(personService.findAll()); debtors.setItems(personService.findAll());
debtors.setItemLabelGenerator(Person::getFirstName); debtors.setItemLabelGenerator(Person::getFirstName);
date = new DatePicker("Date"); date = new DatePicker("Date");
LitRenderer<Expense> isPeriodicRenderer = LitRenderer.<Expense>of(
"<vaadin-icon icon='vaadin:${item.icon}' style='width: var(--lumo-icon-size-s); height: var(--lumo-icon-size-s); color: ${item.color};'></vaadin-icon>")
.withProperty("icon", important -> important.getIsPeriodic() ? "check" : "minus").withProperty("color",
important -> important.getIsPeriodic()
? "var(--lumo-primary-text-color)"
: "var(--lumo-disabled-text-color)");
formLayout.add(name, cost, category, description, isPeriodic, periodUnit, periodInterval, date, creditors, debtors, event); // Horizontal layout for checkboxes
grid.addColumn(isPeriodicRenderer).setHeader("Periodic").setAutoWidth(true); HorizontalLayout checkboxLayout = new HorizontalLayout();
isPeriodic = new Checkbox("Is Periodic");
isResolved = new Checkbox("Paid");
checkboxLayout.add(isPeriodic, isResolved);
formLayout.add(name, cost, category, description, checkboxLayout, periodUnit, periodInterval, date, creditors, debtors, event);
editorDiv.add(formLayout); editorDiv.add(formLayout);
createButtonLayout(editorLayoutDiv); createButtonLayout(editorLayoutDiv);