DDD
This commit is contained in:
parent
31c91c51ea
commit
4ef3dcaf08
@ -2,6 +2,7 @@ package renders
|
||||
|
||||
import (
|
||||
"cangui/whatsapp/backend/models"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"text/template"
|
||||
@ -538,13 +539,17 @@ func ClientConsumptionPage(db *gorm.DB) http.HandlerFunc {
|
||||
http.Error(w, "Non authentifié", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
fmt.Println("🔐 SSOID reçu :", ssoid)
|
||||
|
||||
var currentUser models.User
|
||||
if err := db.Where("sso_id = ?", ssoid).First(¤tUser).Error; err != nil {
|
||||
http.Error(w, "Utilisateur introuvable", http.StatusUnauthorized)
|
||||
fmt.Println("❌ Utilisateur introuvable en base pour ce SSOID")
|
||||
return
|
||||
}
|
||||
fmt.Printf("👤 Utilisateur connecté : %s (ID %d - rôle %s)\n", currentUser.Email, currentUser.ID, currentUser.Role)
|
||||
|
||||
// Récupérer les filtres
|
||||
period := r.URL.Query().Get("period")
|
||||
clientIDStr := r.URL.Query().Get("client")
|
||||
var clientID uint
|
||||
@ -552,7 +557,12 @@ func ClientConsumptionPage(db *gorm.DB) http.HandlerFunc {
|
||||
idParsed, _ := strconv.Atoi(clientIDStr)
|
||||
clientID = uint(idParsed)
|
||||
}
|
||||
fmt.Println("📅 Filtre période :", period)
|
||||
if clientID > 0 {
|
||||
fmt.Println("🎯 Filtre client ID :", clientID)
|
||||
}
|
||||
|
||||
// Calcul de la date de début
|
||||
var fromDate time.Time
|
||||
now := time.Now()
|
||||
switch period {
|
||||
@ -567,8 +577,9 @@ func ClientConsumptionPage(db *gorm.DB) http.HandlerFunc {
|
||||
default:
|
||||
fromDate = time.Time{}
|
||||
}
|
||||
fmt.Println("📆 Date de début utilisée :", fromDate)
|
||||
|
||||
// Préparation des filtres
|
||||
// Requête de base
|
||||
query := db.Model(&models.Consumption{})
|
||||
if currentUser.Role == models.ROLE_ADMIN {
|
||||
if clientID > 0 {
|
||||
@ -581,17 +592,22 @@ func ClientConsumptionPage(db *gorm.DB) http.HandlerFunc {
|
||||
query = query.Where("created_at >= ?", fromDate)
|
||||
}
|
||||
|
||||
// Regroupement par date
|
||||
// Résumé journalier
|
||||
type DailySummary struct {
|
||||
Date time.Time
|
||||
TotalCredits uint
|
||||
}
|
||||
var summaries []DailySummary
|
||||
query.Select("DATE(created_at) as date, SUM(credits_used) as total_credits").
|
||||
result := query.Select("DATE(created_at) as date, SUM(credits_used) as total_credits").
|
||||
Group("DATE(created_at)").Order("date desc").
|
||||
Scan(&summaries)
|
||||
|
||||
// Détail par jour
|
||||
if result.Error != nil {
|
||||
fmt.Println("❌ Erreur lors de la récupération du résumé :", result.Error)
|
||||
}
|
||||
fmt.Printf("📊 Résumés trouvés : %d\n", len(summaries))
|
||||
|
||||
// Détails par date
|
||||
details := make(map[string][]models.Consumption)
|
||||
for _, s := range summaries {
|
||||
var daily []models.Consumption
|
||||
@ -608,6 +624,7 @@ func ClientConsumptionPage(db *gorm.DB) http.HandlerFunc {
|
||||
}
|
||||
detailQuery.Order("created_at desc").Find(&daily)
|
||||
|
||||
fmt.Printf("📅 Détails du %s : %d enregistrements\n", s.Date.Format("2006-01-02"), len(daily))
|
||||
details[s.Date.Format("2006-01-02")] = daily
|
||||
}
|
||||
|
||||
@ -622,6 +639,7 @@ func ClientConsumptionPage(db *gorm.DB) http.HandlerFunc {
|
||||
if currentUser.Role == models.ROLE_ADMIN {
|
||||
var clients []models.User
|
||||
db.Where("role = ?", "CLIENT").Find(&clients)
|
||||
fmt.Printf("👥 Nombre de clients trouvés : %d\n", len(clients))
|
||||
data["Clients"] = clients
|
||||
}
|
||||
|
||||
@ -631,6 +649,7 @@ func ClientConsumptionPage(db *gorm.DB) http.HandlerFunc {
|
||||
|
||||
|
||||
|
||||
|
||||
// pour un rendu complet de la page
|
||||
func renderTemplate(w http.ResponseWriter, templ string, data map[string]interface{}) {
|
||||
t, err := template.ParseFiles(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user