fix: things
This commit is contained in:
parent
56138cd320
commit
c243913b74
5 changed files with 76 additions and 8 deletions
|
@ -13,14 +13,29 @@ import java.util.Set;
|
||||||
|
|
||||||
public interface ExpenseRepository extends JpaRepository<Expense, Long>, JpaSpecificationExecutor<Expense> {
|
public interface ExpenseRepository extends JpaRepository<Expense, Long>, JpaSpecificationExecutor<Expense> {
|
||||||
|
|
||||||
// Find expenses where the creditor is a specific person
|
// Find expenses where the payer is a specific person
|
||||||
@Query("SELECT e FROM Expense e WHERE e.payer.id = :personId")
|
@Query("SELECT e FROM Expense e WHERE e.payer.id = :personId")
|
||||||
Set<Expense> findExpensesByPayer(@Param("personId") Long personId);
|
List<Expense> findExpensesByPayer(@Param("personId") Long personId);
|
||||||
|
|
||||||
// Find expenses where the debtor is a specific person
|
// Find expenses where the beneficiary is a specific person
|
||||||
@Query("SELECT e FROM Expense e WHERE e.beneficiary.id = :personId")
|
@Query("SELECT e FROM Expense e WHERE e.beneficiary.id = :personId")
|
||||||
Set<Expense> findExpensesByBeneficiary(@Param("personId") Long personId);
|
List<Expense> findExpensesByBeneficiary(@Param("personId") Long personId);
|
||||||
|
|
||||||
|
// Find expenses where both payer and beneficiary are the same person
|
||||||
|
@Query("SELECT e FROM Expense e WHERE e.payer.id = :personId AND e.beneficiary.id = :personId")
|
||||||
|
List<Expense> findExpensesByPayerAndBeneficiary(@Param("personId") Long personId);
|
||||||
|
|
||||||
|
// Find expenses where the payer and beneficiary are the same person for a specific year
|
||||||
|
@Query("SELECT e FROM Expense e WHERE e.payer.id = :personId AND e.beneficiary.id = :personId AND YEAR(e.date) = :year")
|
||||||
|
List<Expense> findExpensesByPayerAndBeneficiaryAndYear(@Param("personId") Long personId, @Param("year") int year);
|
||||||
|
|
||||||
|
// Find expenses where the payer is a specific person for a specific year
|
||||||
|
@Query("SELECT e FROM Expense e WHERE e.payer.id = :personId AND YEAR(e.date) = :year")
|
||||||
|
List<Expense> findExpensesByPayerAndYear(@Param("personId") Long personId, @Param("year") int year);
|
||||||
|
|
||||||
|
// Find expenses where the beneficiary is a specific person for a specific year
|
||||||
|
@Query("SELECT e FROM Expense e WHERE e.beneficiary.id = :personId AND YEAR(e.date) = :year")
|
||||||
|
List<Expense> findExpensesByBeneficiaryAndYear(@Param("personId") Long personId, @Param("year") int year);
|
||||||
// Find all expenses for a given year
|
// Find all expenses for a given year
|
||||||
@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);
|
||||||
|
@ -31,7 +46,7 @@ public interface ExpenseRepository extends JpaRepository<Expense, Long>, JpaSpec
|
||||||
|
|
||||||
// Find unpaid expenses where the debtor is a specific person
|
// Find unpaid expenses where the debtor is a specific person
|
||||||
@Query("SELECT e FROM Expense e WHERE e.beneficiary.id = :personId AND e.isPaid = false")
|
@Query("SELECT e FROM Expense e WHERE e.beneficiary.id = :personId AND e.isPaid = false")
|
||||||
Set<Expense> findUnapidExpensesByBeneficiary(@Param("personId") Long personId);
|
Set<Expense> findUnpaidExpensesByBeneficiary(@Param("personId") Long personId);
|
||||||
|
|
||||||
// Find expenses for a given year and filter by expense type and paid status
|
// Find expenses for a given year and filter by expense type and paid status
|
||||||
@Query("SELECT e FROM Expense e WHERE YEAR(e.date) = :year AND NOT (e.expenseType = :expenseType AND e.isPaid = true)")
|
@Query("SELECT e FROM Expense e WHERE YEAR(e.date) = :year AND NOT (e.expenseType = :expenseType AND e.isPaid = true)")
|
||||||
|
|
|
@ -15,6 +15,8 @@ import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.Year;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -66,7 +68,7 @@ public class ExpenseService {
|
||||||
* @return the collection of unpaid expenses found
|
* @return the collection of unpaid expenses found
|
||||||
*/
|
*/
|
||||||
public Collection<Expense> findUnpaidExpensesWhereBeneficiary(final Person person) {
|
public Collection<Expense> findUnpaidExpensesWhereBeneficiary(final Person person) {
|
||||||
return expenseRepository.findUnapidExpensesByBeneficiary(person.getId());
|
return expenseRepository.findUnpaidExpensesByBeneficiary(person.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -181,6 +183,26 @@ public class ExpenseService {
|
||||||
return (int) expenseRepository.count();
|
return (int) expenseRepository.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Expense> fetchExpensesForDashboard(Person loggedInPerson, Year year) {
|
||||||
|
List<Expense> totalExpenses = new ArrayList<>();
|
||||||
|
final var yearValue = year.getValue();
|
||||||
|
|
||||||
|
// Fetch expenses where you are the payer and beneficiary (self-expenses) for the given year
|
||||||
|
List<Expense> bothExpenses = expenseRepository.findExpensesByPayerAndBeneficiaryAndYear(loggedInPerson.getId(), yearValue);
|
||||||
|
totalExpenses.addAll(bothExpenses); // Include these regardless of isPaid status
|
||||||
|
|
||||||
|
// Fetch expenses where you are the payer (you owe money), filtered by year
|
||||||
|
List<Expense> payerExpenses = expenseRepository.findExpensesByPayerAndYear(loggedInPerson.getId(), yearValue);
|
||||||
|
for (Expense expense : payerExpenses) {
|
||||||
|
if (!totalExpenses.contains(expense)) totalExpenses.add(expense);
|
||||||
|
}
|
||||||
|
// Fetch expenses where you are the beneficiary and not paid (amount owed to you), filtered by year
|
||||||
|
List<Expense> beneficiaryExpenses = expenseRepository.findExpensesByBeneficiaryAndYear(loggedInPerson.getId(), yearValue);
|
||||||
|
for (Expense expense : beneficiaryExpenses) {
|
||||||
|
if (Boolean.FALSE.equals(expense.getIsPaid()) && !totalExpenses.contains(expense)) totalExpenses.add(expense);
|
||||||
|
}
|
||||||
|
return totalExpenses;
|
||||||
|
}
|
||||||
// ================================
|
// ================================
|
||||||
// Private methods
|
// Private methods
|
||||||
// ================================
|
// ================================
|
||||||
|
|
|
@ -10,17 +10,20 @@ import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class PersonService {
|
public class PersonService {
|
||||||
|
|
||||||
|
private final UserService userService;
|
||||||
private final PersonRepository personRepository;
|
private final PersonRepository personRepository;
|
||||||
private final ExpenseService expenseService;
|
private final ExpenseService expenseService;
|
||||||
|
|
||||||
public PersonService(PersonRepository personRepository, ExpenseService expenseService) {
|
public PersonService(PersonRepository personRepository, ExpenseService expenseService, UserService userService) {
|
||||||
this.personRepository = personRepository;
|
this.personRepository = personRepository;
|
||||||
this.expenseService = expenseService;
|
this.expenseService = expenseService;
|
||||||
|
this.userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,6 +82,16 @@ public class PersonService {
|
||||||
return (int) this.personRepository.count();
|
return (int) this.personRepository.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the Person entity connected to the currently logged-in user.
|
||||||
|
*
|
||||||
|
* @return Person entity of the logged-in user, or null if not found.
|
||||||
|
*/
|
||||||
|
public Person getLoggedInPerson() {
|
||||||
|
final var user = userService.getLoggedInUser();
|
||||||
|
return Objects.requireNonNull(personRepository.findByUserId(user.getId()).orElse(null));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates a person in the repository.
|
* Updates a person in the repository.
|
||||||
* @param person the person to update
|
* @param person the person to update
|
||||||
|
|
|
@ -2,8 +2,11 @@ package com.application.munera.services;
|
||||||
|
|
||||||
import com.application.munera.data.User;
|
import com.application.munera.data.User;
|
||||||
import com.application.munera.repositories.UserRepository;
|
import com.application.munera.repositories.UserRepository;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import static com.application.munera.SecurityUtils.getLoggedInUserDetails;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
|
||||||
|
@ -16,4 +19,18 @@ public class UserService {
|
||||||
public User findByUsername (String username) {
|
public User findByUsername (String username) {
|
||||||
return this.userRepository.findByUsername(username);
|
return this.userRepository.findByUsername(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the logged-in User entity.
|
||||||
|
* @return User entity of the logged-in user, or null if not found.
|
||||||
|
*/
|
||||||
|
public User getLoggedInUser() {
|
||||||
|
UserDetails userDetails = getLoggedInUserDetails();
|
||||||
|
if (userDetails != null) {
|
||||||
|
String username = userDetails.getUsername();
|
||||||
|
return userRepository.findByUsername(username);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,7 +105,8 @@ public class DashboardView extends Div {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String generateBarChartScript() {
|
private String generateBarChartScript() {
|
||||||
List<Expense> expenses = expenseService.findExpensesByYearExcludingCreditPaid(Year.now().getValue());
|
final var loggedInPerson = this.personService.getLoggedInPerson();
|
||||||
|
List<Expense> expenses = expenseService.fetchExpensesForDashboard(loggedInPerson, Year.now());
|
||||||
|
|
||||||
// Prepare data for Highcharts
|
// Prepare data for Highcharts
|
||||||
Map<String, Double> monthlyData = new LinkedHashMap<>();
|
Map<String, Double> monthlyData = new LinkedHashMap<>();
|
||||||
|
|
Loading…
Reference in a new issue