diff --git a/src/main/java/com/application/munera/repositories/ExpenseRepository.java b/src/main/java/com/application/munera/repositories/ExpenseRepository.java index ba7f0d4..da13977 100644 --- a/src/main/java/com/application/munera/repositories/ExpenseRepository.java +++ b/src/main/java/com/application/munera/repositories/ExpenseRepository.java @@ -21,5 +21,11 @@ public interface ExpenseRepository extends JpaRepository, JpaSpec @Query("SELECT e FROM Expense e WHERE YEAR(e.date) = :year") List findAllByYear(@Param("year") int year); + @Query("SELECT e FROM Expense e JOIN e.creditors c WHERE c.id = :personId AND e.isResolved = false") + Set findUnpaidCreditorsExpensesByPersonId(@Param("personId") Long personId); + + @Query("SELECT e FROM Expense e JOIN e.debtors d WHERE d.id = :personId AND e.isResolved = false") + Set findUnpaidDebtorsExpensesByPersonId(@Param("personId") Long personId); + boolean existsByIdAndIsResolvedTrue(Long id); } \ No newline at end of file diff --git a/src/main/java/com/application/munera/services/ExpenseService.java b/src/main/java/com/application/munera/services/ExpenseService.java index 74964b1..534454d 100644 --- a/src/main/java/com/application/munera/services/ExpenseService.java +++ b/src/main/java/com/application/munera/services/ExpenseService.java @@ -32,6 +32,14 @@ public class ExpenseService { public Collection findCreditByUser(final Person person) { return repository.findCreditorsExpensesByPersonId(person.getId()); } + + public Collection findUnpaidDebtByUser(final Person person) { + return repository.findUnpaidDebtorsExpensesByPersonId(person.getId()); + } + + public Collection findUnpaidCreditByUser(final Person person) { + return repository.findUnpaidCreditorsExpensesByPersonId(person.getId()); + } public List findAll() {return repository.findAll();} public void update(Expense entity) { diff --git a/src/main/java/com/application/munera/services/PersonService.java b/src/main/java/com/application/munera/services/PersonService.java index eff44fc..3de2629 100644 --- a/src/main/java/com/application/munera/services/PersonService.java +++ b/src/main/java/com/application/munera/services/PersonService.java @@ -60,8 +60,8 @@ public class PersonService { } 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); + final var credit = this.expenseService.findUnpaidCreditByUser(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); } }