feat: filtering by status for better math

This commit is contained in:
filippo-ferrari 2024-07-07 17:34:40 +02:00
parent 23b9e09aab
commit 239a08a326
3 changed files with 16 additions and 2 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);
} }
} }