feat(dashboard): cotisations tout temps + synthèse membre
- CotisationRepository: calculerTotalCotisationsPayeesToutTemps(membreId) - MembreDashboardService: envoi totalCotisationsPayeesToutTemps dans la synthèse Made-with: Cursor
This commit is contained in:
@@ -12,6 +12,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -39,7 +40,7 @@ public class CotisationRepository extends BaseRepository<Cotisation> {
|
||||
"SELECT c FROM Cotisation c WHERE c.numeroReference = :numeroReference",
|
||||
Cotisation.class);
|
||||
query.setParameter("numeroReference", numeroReference);
|
||||
return query.getResultStream().findFirst();
|
||||
return query.getResultList().stream().findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,6 +62,93 @@ public class CotisationRepository extends BaseRepository<Cotisation> {
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
/** Compte les cotisations d'un membre (pour dashboard). */
|
||||
public long countByMembreId(UUID membreId) {
|
||||
if (membreId == null) return 0L;
|
||||
TypedQuery<Long> query = entityManager.createQuery(
|
||||
"SELECT COUNT(c) FROM Cotisation c WHERE c.membre.id = :membreId", Long.class);
|
||||
query.setParameter("membreId", membreId);
|
||||
Long result = query.getSingleResult();
|
||||
return result != null ? result : 0L;
|
||||
}
|
||||
|
||||
/** Compte les cotisations payées d'un membre (statut PAYEE, pour dashboard). */
|
||||
public long countPayeesByMembreId(UUID membreId) {
|
||||
if (membreId == null) return 0L;
|
||||
TypedQuery<Long> query = entityManager.createQuery(
|
||||
"SELECT COUNT(c) FROM Cotisation c WHERE c.membre.id = :membreId AND (c.statut = 'PAYEE' OR c.statut = 'PARTIELLEMENT_PAYEE')",
|
||||
Long.class);
|
||||
query.setParameter("membreId", membreId);
|
||||
Long result = query.getSingleResult();
|
||||
return result != null ? result : 0L;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trouve les cotisations dont l'organisation fait partie de la liste (pour admin / admin org).
|
||||
*
|
||||
* @param organisationIds ensemble des UUID d'organisations
|
||||
* @param page pagination
|
||||
* @param sort tri
|
||||
* @return liste paginée des cotisations
|
||||
*/
|
||||
public List<Cotisation> findByOrganisationIdIn(Set<UUID> organisationIds, Page page, Sort sort) {
|
||||
if (organisationIds == null || organisationIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
String orderBy = sort != null ? " ORDER BY " + buildOrderBy(sort) : " ORDER BY c.dateEcheance DESC";
|
||||
TypedQuery<Cotisation> query = entityManager.createQuery(
|
||||
"SELECT c FROM Cotisation c WHERE c.organisation.id IN :organisationIds" + orderBy,
|
||||
Cotisation.class);
|
||||
query.setParameter("organisationIds", organisationIds);
|
||||
query.setFirstResult(page.index * page.size);
|
||||
query.setMaxResults(page.size);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
/** Compte les cotisations d'une organisation (pour dashboard par org). */
|
||||
public long countByOrganisationId(UUID organisationId) {
|
||||
if (organisationId == null) return 0L;
|
||||
TypedQuery<Long> query = entityManager.createQuery(
|
||||
"SELECT COUNT(c) FROM Cotisation c WHERE c.organisation.id = :organisationId", Long.class);
|
||||
query.setParameter("organisationId", organisationId);
|
||||
Long result = query.getSingleResult();
|
||||
return result != null ? result : 0L;
|
||||
}
|
||||
|
||||
/** Compte les cotisations d'une organisation avec date de paiement dans une période (pour graphiques). */
|
||||
public long countByOrganisationIdAndDatePaiementBetween(UUID organisationId, java.time.LocalDateTime start, java.time.LocalDateTime end) {
|
||||
if (organisationId == null) return 0L;
|
||||
TypedQuery<Long> query = entityManager.createQuery(
|
||||
"SELECT COUNT(c) FROM Cotisation c WHERE c.organisation.id = :organisationId AND c.datePaiement >= :start AND c.datePaiement <= :end",
|
||||
Long.class);
|
||||
query.setParameter("organisationId", organisationId);
|
||||
query.setParameter("start", start);
|
||||
query.setParameter("end", end);
|
||||
Long result = query.getSingleResult();
|
||||
return result != null ? result : 0L;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trouve les cotisations en attente pour les organisations données (pour admin / admin org).
|
||||
*
|
||||
* @param organisationIds ensemble des UUID d'organisations
|
||||
* @return liste des cotisations en attente, triées par date d'échéance
|
||||
*/
|
||||
public List<Cotisation> findEnAttenteByOrganisationIdIn(Set<UUID> organisationIds) {
|
||||
if (organisationIds == null || organisationIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
int anneeEnCours = LocalDate.now().getYear();
|
||||
TypedQuery<Cotisation> query = entityManager.createQuery(
|
||||
"SELECT c FROM Cotisation c WHERE c.organisation.id IN :organisationIds "
|
||||
+ "AND c.statut = 'EN_ATTENTE' AND EXTRACT(YEAR FROM c.dateEcheance) = :annee "
|
||||
+ "ORDER BY c.dateEcheance ASC",
|
||||
Cotisation.class);
|
||||
query.setParameter("organisationIds", organisationIds);
|
||||
query.setParameter("annee", anneeEnCours);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Trouve les cotisations par statut
|
||||
*
|
||||
@@ -143,13 +231,112 @@ public class CotisationRepository extends BaseRepository<Cotisation> {
|
||||
* Recherche avancée avec filtres multiples
|
||||
*
|
||||
* @param membreId UUID du membre (optionnel)
|
||||
* @param statut statut (optionnel)
|
||||
* @param typeCotisation type (optionnel)
|
||||
* @param annee année (optionnel)
|
||||
* @param mois mois (optionnel)
|
||||
* @param page pagination
|
||||
* @return liste filtrée des cotisations
|
||||
* @param statut Statut de la cotisation (optionnel)
|
||||
* @param typeCotisation Type de cotisation (optionnel)
|
||||
* @param annee Année (optionnel)
|
||||
* @param page Pagination
|
||||
* @param sort Tri
|
||||
* @return liste paginée des cotisations filtrées
|
||||
*/
|
||||
public List<Cotisation> searchAdvanced(UUID membreId, String statut, String typeCotisation, Integer annee,
|
||||
Page page, Sort sort) {
|
||||
StringBuilder queryStr = new StringBuilder("SELECT c FROM Cotisation c WHERE 1=1 ");
|
||||
Map<String, Object> params = new java.util.HashMap<>();
|
||||
|
||||
if (membreId != null) {
|
||||
queryStr.append("AND c.membre.id = :membreId ");
|
||||
params.put("membreId", membreId);
|
||||
}
|
||||
if (statut != null && !statut.isBlank()) {
|
||||
queryStr.append("AND c.statut = :statut ");
|
||||
params.put("statut", statut);
|
||||
}
|
||||
if (typeCotisation != null && !typeCotisation.isBlank()) {
|
||||
queryStr.append("AND c.typeCotisation = :typeCotisation ");
|
||||
params.put("typeCotisation", typeCotisation);
|
||||
}
|
||||
if (annee != null) {
|
||||
queryStr.append("AND c.annee = :annee ");
|
||||
params.put("annee", annee);
|
||||
}
|
||||
|
||||
if (sort != null) {
|
||||
queryStr.append(" ORDER BY ").append(buildOrderBy(sort));
|
||||
} else {
|
||||
queryStr.append(" ORDER BY c.dateEcheance DESC");
|
||||
}
|
||||
|
||||
TypedQuery<Cotisation> query = entityManager.createQuery(queryStr.toString(), Cotisation.class);
|
||||
params.forEach(query::setParameter);
|
||||
|
||||
query.setFirstResult(page.index * page.size);
|
||||
query.setMaxResults(page.size);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
// --- Méthodes d'agrégation pour le dashboard membre --- //
|
||||
|
||||
public BigDecimal calculerTotalCotisationsPayeesCeMois(UUID membreId) {
|
||||
LocalDate startOfMonth = LocalDate.now().withDayOfMonth(1);
|
||||
LocalDate endOfMonth = LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth());
|
||||
|
||||
BigDecimal total = entityManager.createQuery(
|
||||
"SELECT SUM(c.montantPaye) FROM Cotisation c WHERE c.membre.id = :membreId AND c.statut = 'PAYEE' AND c.datePaiement >= :start AND c.datePaiement <= :end",
|
||||
BigDecimal.class)
|
||||
.setParameter("membreId", membreId)
|
||||
.setParameter("start", startOfMonth.atStartOfDay())
|
||||
.setParameter("end", endOfMonth.atTime(23, 59, 59))
|
||||
.getSingleResult();
|
||||
return total != null ? total : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
public long countRetardByMembreId(UUID membreId) {
|
||||
Long count = entityManager.createQuery(
|
||||
"SELECT COUNT(c) FROM Cotisation c WHERE c.membre.id = :membreId AND c.dateEcheance < :today AND c.statut != 'PAYEE' AND c.statut != 'ANNULEE'",
|
||||
Long.class)
|
||||
.setParameter("membreId", membreId)
|
||||
.setParameter("today", LocalDate.now())
|
||||
.getSingleResult();
|
||||
return count != null ? count : 0L;
|
||||
}
|
||||
|
||||
public BigDecimal calculerTotalCotisationsAnneeEnCours(UUID membreId) {
|
||||
int anneeCourante = LocalDate.now().getYear();
|
||||
BigDecimal total = entityManager.createQuery(
|
||||
"SELECT SUM(c.montantDu) FROM Cotisation c WHERE c.membre.id = :membreId AND c.annee = :annee",
|
||||
BigDecimal.class)
|
||||
.setParameter("membreId", membreId)
|
||||
.setParameter("annee", anneeCourante)
|
||||
.getSingleResult();
|
||||
return total != null ? total : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
public BigDecimal calculerTotalCotisationsPayeesAnneeEnCours(UUID membreId) {
|
||||
int anneeCourante = LocalDate.now().getYear();
|
||||
BigDecimal total = entityManager.createQuery(
|
||||
"SELECT SUM(c.montantPaye) FROM Cotisation c WHERE c.membre.id = :membreId AND c.annee = :annee AND (c.statut = 'PAYEE' OR c.statut = 'PARTIELLEMENT_PAYEE')",
|
||||
BigDecimal.class)
|
||||
.setParameter("membreId", membreId)
|
||||
.setParameter("annee", anneeCourante)
|
||||
.getSingleResult();
|
||||
return total != null ? total : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
/** Total des cotisations payées par le membre (toutes années) pour le KPI « Contribution Totale ». */
|
||||
public BigDecimal calculerTotalCotisationsPayeesToutTemps(UUID membreId) {
|
||||
BigDecimal total = entityManager.createQuery(
|
||||
"SELECT SUM(c.montantPaye) FROM Cotisation c WHERE c.membre.id = :membreId AND (c.statut = 'PAYEE' OR c.statut = 'PARTIELLEMENT_PAYEE')",
|
||||
BigDecimal.class)
|
||||
.setParameter("membreId", membreId)
|
||||
.getSingleResult();
|
||||
return total != null ? total : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recherche avancee
|
||||
*/
|
||||
|
||||
public List<Cotisation> rechercheAvancee(
|
||||
UUID membreId, String statut, String typeCotisation, Integer annee, Integer mois, Page page) {
|
||||
StringBuilder jpql = new StringBuilder("SELECT c FROM Cotisation c WHERE 1=1");
|
||||
@@ -234,6 +421,29 @@ public class CotisationRepository extends BaseRepository<Cotisation> {
|
||||
return query.getSingleResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Somme des montants payés pour un statut donné (ex. PAYEE pour revenus
|
||||
* perçus).
|
||||
*/
|
||||
public BigDecimal sommeMontantPayeParStatut(String statut) {
|
||||
TypedQuery<BigDecimal> query = entityManager.createQuery(
|
||||
"SELECT COALESCE(SUM(c.montantPaye), 0) FROM Cotisation c WHERE c.statut = :statut",
|
||||
BigDecimal.class);
|
||||
query.setParameter("statut", statut);
|
||||
BigDecimal result = query.getSingleResult();
|
||||
return result != null ? result : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
/**
|
||||
* Somme de tous les montants dus.
|
||||
*/
|
||||
public BigDecimal sommeMontantDu() {
|
||||
TypedQuery<BigDecimal> query = entityManager.createQuery(
|
||||
"SELECT COALESCE(SUM(c.montantDu), 0) FROM Cotisation c",
|
||||
BigDecimal.class);
|
||||
return query.getSingleResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Trouve les cotisations nécessitant un rappel
|
||||
*
|
||||
@@ -347,7 +557,7 @@ public class CotisationRepository extends BaseRepository<Cotisation> {
|
||||
public BigDecimal sumMontantsPayes(
|
||||
UUID organisationId, LocalDateTime debut, LocalDateTime fin) {
|
||||
TypedQuery<BigDecimal> query = entityManager.createQuery(
|
||||
"SELECT COALESCE(SUM(c.montantPaye), 0) FROM Cotisation c WHERE c.membre.organisation.id = :organisationId AND c.statut = 'PAYEE' AND c.datePaiement BETWEEN :debut AND :fin",
|
||||
"SELECT COALESCE(SUM(c.montantPaye), 0) FROM Cotisation c WHERE c.organisation.id = :organisationId AND c.statut = 'PAYEE' AND c.datePaiement BETWEEN :debut AND :fin",
|
||||
BigDecimal.class);
|
||||
query.setParameter("organisationId", organisationId);
|
||||
query.setParameter("debut", debut);
|
||||
@@ -360,7 +570,7 @@ public class CotisationRepository extends BaseRepository<Cotisation> {
|
||||
public BigDecimal sumMontantsEnAttente(
|
||||
UUID organisationId, LocalDateTime debut, LocalDateTime fin) {
|
||||
TypedQuery<BigDecimal> query = entityManager.createQuery(
|
||||
"SELECT COALESCE(SUM(c.montantDu), 0) FROM Cotisation c WHERE c.membre.organisation.id = :organisationId AND c.statut = 'EN_ATTENTE' AND c.dateCreation BETWEEN :debut AND :fin",
|
||||
"SELECT COALESCE(SUM(c.montantDu), 0) FROM Cotisation c WHERE c.organisation.id = :organisationId AND c.statut = 'EN_ATTENTE' AND c.dateCreation BETWEEN :debut AND :fin",
|
||||
BigDecimal.class);
|
||||
query.setParameter("organisationId", organisationId);
|
||||
query.setParameter("debut", debut);
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
package dev.lions.unionflow.server.service;
|
||||
|
||||
import dev.lions.unionflow.server.api.dto.dashboard.MembreDashboardSyntheseResponse;
|
||||
import dev.lions.unionflow.server.api.enums.solidarite.StatutAide;
|
||||
import dev.lions.unionflow.server.entity.Membre;
|
||||
import dev.lions.unionflow.server.repository.CotisationRepository;
|
||||
import dev.lions.unionflow.server.repository.DemandeAideRepository;
|
||||
import dev.lions.unionflow.server.repository.MembreRepository;
|
||||
import dev.lions.unionflow.server.repository.mutuelle.epargne.CompteEpargneRepository;
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.NotFoundException;
|
||||
import org.eclipse.microprofile.jwt.JsonWebToken;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@ApplicationScoped
|
||||
public class MembreDashboardService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(MembreDashboardService.class);
|
||||
|
||||
@Inject
|
||||
SecurityIdentity securityIdentity;
|
||||
|
||||
@Inject
|
||||
MembreRepository membreRepository;
|
||||
|
||||
@Inject
|
||||
CotisationRepository cotisationRepository;
|
||||
|
||||
@Inject
|
||||
CompteEpargneRepository compteEpargneRepository;
|
||||
|
||||
@Inject
|
||||
DemandeAideRepository demandeAideRepository;
|
||||
|
||||
/**
|
||||
* Récupère l'email du principal : d'abord la claim JWT "email" (Keycloak envoie souvent
|
||||
* preferred_username comme getName()), puis fallback sur getName().
|
||||
*/
|
||||
private String getEmailFromPrincipal() {
|
||||
if (securityIdentity == null || securityIdentity.getPrincipal() == null) return null;
|
||||
if (securityIdentity.getPrincipal() instanceof JsonWebToken jwt) {
|
||||
try {
|
||||
String email = jwt.getClaim("email");
|
||||
if (email != null && !email.isBlank()) return email;
|
||||
} catch (Exception e) {
|
||||
LOG.debugf("Claim email non disponible: %s", e.getMessage());
|
||||
}
|
||||
}
|
||||
return securityIdentity.getPrincipal().getName();
|
||||
}
|
||||
|
||||
public MembreDashboardSyntheseResponse getDashboardData() {
|
||||
String email = getEmailFromPrincipal();
|
||||
if (email == null || email.isBlank()) {
|
||||
throw new NotFoundException("Identité non disponible pour le dashboard membre.");
|
||||
}
|
||||
LOG.infof("Génération du dashboard pour le membre: %s", email);
|
||||
|
||||
Membre membre = membreRepository.findByEmail(email.trim())
|
||||
.or(() -> membreRepository.findByEmail(email.trim().toLowerCase()))
|
||||
.filter(m -> m.getActif() == null || m.getActif())
|
||||
.orElseThrow(() -> new NotFoundException("Membre non trouvé pour l'email: " + email));
|
||||
|
||||
UUID membreId = membre.getId();
|
||||
LocalDate now = LocalDate.now();
|
||||
|
||||
// 1. Infos membre
|
||||
String prenom = membre.getPrenom();
|
||||
String nom = membre.getNom();
|
||||
LocalDate dateInscription = null;
|
||||
if (membre.getDateCreation() != null) {
|
||||
dateInscription = membre.getDateCreation().toLocalDate();
|
||||
}
|
||||
|
||||
// 2. Cotisations
|
||||
// Approximations : on somme les cotisations payées ce mois, on regarde s'il y a
|
||||
// des cotisations en retard, etc.
|
||||
// On utilisera des méthodes custom sur le repository si besoin, ou on le fait
|
||||
// en Java (sur les petits sets test)
|
||||
BigDecimal paiementsMois = cotisationRepository.calculerTotalCotisationsPayeesCeMois(membreId);
|
||||
|
||||
// Statut : "En retard", "À jour", "En attente"
|
||||
long enRetardCount = cotisationRepository.countRetardByMembreId(membreId);
|
||||
String statutCotisations = enRetardCount > 0 ? "En retard" : "À jour";
|
||||
|
||||
// Taux de cotisation (payé vs dû pour l'année en cours)
|
||||
BigDecimal totalAnneeDu = cotisationRepository.calculerTotalCotisationsAnneeEnCours(membreId);
|
||||
BigDecimal totalAnneePaye = cotisationRepository.calculerTotalCotisationsPayeesAnneeEnCours(membreId);
|
||||
|
||||
Integer tauxCotisations = null;
|
||||
if (totalAnneeDu != null && totalAnneeDu.compareTo(BigDecimal.ZERO) > 0) {
|
||||
if (totalAnneePaye == null)
|
||||
totalAnneePaye = BigDecimal.ZERO;
|
||||
tauxCotisations = totalAnneePaye.multiply(new BigDecimal("100"))
|
||||
.divide(totalAnneeDu, 0, java.math.RoundingMode.HALF_UP)
|
||||
.intValue();
|
||||
}
|
||||
BigDecimal totalCotisationsPayeesAnnee = (totalAnneePaye != null ? totalAnneePaye : BigDecimal.ZERO);
|
||||
BigDecimal totalCotisationsPayeesToutTemps = cotisationRepository.calculerTotalCotisationsPayeesToutTemps(membreId);
|
||||
int nombreCotisationsPayees = (int) cotisationRepository.countPayeesByMembreId(membreId);
|
||||
|
||||
// 3. Epargne (somme des soldes des comptes actifs du membre)
|
||||
BigDecimal soldeEpargne = compteEpargneRepository.sumSoldeActuelByMembreId(membreId);
|
||||
BigDecimal evolutionEpargneNb = BigDecimal.ZERO;
|
||||
String evolutionEpargneTxt = soldeEpargne.compareTo(BigDecimal.ZERO) > 0 ? "+0%" : "0%";
|
||||
Integer objectifEpargne = 0;
|
||||
|
||||
// 4. Événements (pas de repository InscriptionEvenement dédié pour l'instant)
|
||||
Integer mesEvenements = 0;
|
||||
Integer evenementsAVenir = 0;
|
||||
Integer tauxParticipation = null;
|
||||
|
||||
// 5. Aides (demandes du membre)
|
||||
List<dev.lions.unionflow.server.entity.DemandeAide> demandes = demandeAideRepository.findByDemandeurId(membreId);
|
||||
int mesDemandes = demandes != null ? demandes.size() : 0;
|
||||
int aidesCours = demandes != null ? (int) demandes.stream()
|
||||
.filter(d -> d.getStatut() != null && d.getStatut() != StatutAide.APPROUVEE && d.getStatut() != StatutAide.REJETEE && d.getStatut() != StatutAide.ANNULEE)
|
||||
.count() : 0;
|
||||
Integer tauxAidesApprouvees = null;
|
||||
if (mesDemandes > 0 && demandes != null) {
|
||||
long acceptees = demandes.stream().filter(d -> d.getStatut() == StatutAide.APPROUVEE).count();
|
||||
tauxAidesApprouvees = (int) (acceptees * 100 / mesDemandes);
|
||||
}
|
||||
|
||||
return new MembreDashboardSyntheseResponse(
|
||||
prenom,
|
||||
nom,
|
||||
dateInscription,
|
||||
|
||||
paiementsMois != null ? paiementsMois : BigDecimal.ZERO,
|
||||
totalCotisationsPayeesAnnee,
|
||||
totalCotisationsPayeesToutTemps != null ? totalCotisationsPayeesToutTemps : BigDecimal.ZERO,
|
||||
Integer.valueOf(nombreCotisationsPayees),
|
||||
statutCotisations,
|
||||
tauxCotisations,
|
||||
|
||||
soldeEpargne,
|
||||
evolutionEpargneNb,
|
||||
evolutionEpargneTxt,
|
||||
objectifEpargne,
|
||||
|
||||
mesEvenements,
|
||||
evenementsAVenir,
|
||||
tauxParticipation,
|
||||
|
||||
Integer.valueOf(mesDemandes),
|
||||
Integer.valueOf(aidesCours),
|
||||
tauxAidesApprouvees);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user