chore: add point pool
This commit is contained in:
@@ -867,36 +867,25 @@ func (d *Database) CalculateAndAddPointsForCommandTx(tx *sql.Tx, commandID int,
|
||||
settings = DefaultSettings()
|
||||
}
|
||||
|
||||
// Construire les sets de catégories par pool
|
||||
weedCats := make(map[string]bool)
|
||||
for _, cat := range settings.PointsCategoriesWeed {
|
||||
weedCats[strings.ToLower(cat)] = true
|
||||
}
|
||||
zipetteCats := make(map[string]bool)
|
||||
for _, cat := range settings.PointsCategoriesZipette {
|
||||
zipetteCats[strings.ToLower(cat)] = true
|
||||
}
|
||||
// pool "total" → toujours compté dans le pool weed (point)
|
||||
totalCats := make(map[string]bool)
|
||||
for _, cat := range settings.PointsCategoriesTotal {
|
||||
totalCats[strings.ToLower(cat)] = true
|
||||
pools := settings.PointsPools
|
||||
if len(pools) == 0 {
|
||||
log.Printf("ℹ️ [CalcPointsTx] Aucun pool configuré → 0 points")
|
||||
return 0, "", nil
|
||||
}
|
||||
|
||||
// Si aucune catégorie configurée → pas de points
|
||||
if !settings.PointsSeparated {
|
||||
// Mode non-séparé : seul le pool Total est actif
|
||||
if len(weedCats) == 0 && len(zipetteCats) == 0 && len(totalCats) == 0 {
|
||||
log.Printf("ℹ️ [CalcPointsTx] Mode non-séparé : aucune catégorie configurée → 0 points")
|
||||
return 0, "", nil
|
||||
}
|
||||
} else {
|
||||
// Mode séparé : seuls W et Z sont actifs (T ignoré)
|
||||
if len(weedCats) == 0 && len(zipetteCats) == 0 {
|
||||
log.Printf("ℹ️ [CalcPointsTx] Mode séparé : aucune catégorie W/Z configurée → 0 points")
|
||||
return 0, "", nil
|
||||
// Construire la map catégorie → index de pool
|
||||
catToPool := make(map[string]int)
|
||||
for i, pool := range pools {
|
||||
for _, cat := range pool.Categories {
|
||||
catToPool[strings.ToLower(cat)] = i
|
||||
}
|
||||
}
|
||||
|
||||
if len(catToPool) == 0 {
|
||||
log.Printf("ℹ️ [CalcPointsTx] Aucune catégorie assignée aux pools → 0 points")
|
||||
return 0, "", nil
|
||||
}
|
||||
|
||||
// ✅ ÉTAPE 1: Récupérer tous les items de la commande avec leurs catégories
|
||||
rows, err := tx.Query(`
|
||||
SELECT ci.quantite, ci.prix, COALESCE(p.category, '') as category
|
||||
@@ -911,9 +900,7 @@ func (d *Database) CalculateAndAddPointsForCommandTx(tx *sql.Tx, commandID int,
|
||||
defer rows.Close()
|
||||
|
||||
var itemCount int
|
||||
totalPrixWeed := 0.0
|
||||
totalPrixZipette := 0.0
|
||||
totalPrixTotal := 0.0
|
||||
poolTotals := make([]float64, len(pools))
|
||||
|
||||
for rows.Next() {
|
||||
var quantite, prix float64
|
||||
@@ -924,12 +911,8 @@ func (d *Database) CalculateAndAddPointsForCommandTx(tx *sql.Tx, commandID int,
|
||||
}
|
||||
itemCount++
|
||||
catLower := strings.ToLower(category)
|
||||
if weedCats[catLower] {
|
||||
totalPrixWeed += prix
|
||||
} else if zipetteCats[catLower] {
|
||||
totalPrixZipette += prix
|
||||
} else if totalCats[catLower] {
|
||||
totalPrixTotal += prix
|
||||
if poolIdx, ok := catToPool[catLower]; ok {
|
||||
poolTotals[poolIdx] += prix
|
||||
}
|
||||
}
|
||||
|
||||
@@ -943,58 +926,49 @@ func (d *Database) CalculateAndAddPointsForCommandTx(tx *sql.Tx, commandID int,
|
||||
return 0, "", nil
|
||||
}
|
||||
|
||||
log.Printf("📊 [CalcPointsTx] %d items - weed: %.2f€, zipette: %.2f€, total: %.2f€",
|
||||
itemCount, totalPrixWeed, totalPrixZipette, totalPrixTotal)
|
||||
for i, t := range poolTotals {
|
||||
log.Printf("📊 [CalcPointsTx] Pool[%d] (%s): %.2f€", i, pools[i].Name, t)
|
||||
}
|
||||
|
||||
// ✅ ÉTAPE 2: Calculer les points
|
||||
// ✅ ÉTAPE 2: Calculer les points par pool et mettre à jour les colonnes DB
|
||||
// Pool[0] → colonne `point`, Pool[1] → colonne `point_zipette`
|
||||
var totalPoints int
|
||||
var pointCategory string
|
||||
var result sql.Result
|
||||
|
||||
if !settings.PointsSeparated {
|
||||
// ── Mode non-séparé : Barème Total appliqué sur W + Z + T ──────────────
|
||||
allTotal := totalPrixWeed + totalPrixZipette + totalPrixTotal
|
||||
totalPoints = CalcPointsFromTiers(allTotal, settings.PointsTotalTiers)
|
||||
pointCategory = "total"
|
||||
pts0 := CalcPointsFromTiers(poolTotals[0], pools[0].Tiers)
|
||||
pts1 := 0
|
||||
if len(pools) >= 2 {
|
||||
pts1 = CalcPointsFromTiers(poolTotals[1], pools[1].Tiers)
|
||||
}
|
||||
totalPoints = pts0 + pts1
|
||||
|
||||
log.Printf("💰 [CalcPointsTx] Mode non-séparé - total: %.2f€ → %d pts (barème Total)",
|
||||
allTotal, totalPoints)
|
||||
log.Printf("💰 [CalcPointsTx] pool[0]=%d pts, pool[1]=%d pts", pts0, pts1)
|
||||
|
||||
if totalPoints == 0 {
|
||||
return 0, "", nil
|
||||
}
|
||||
if totalPoints == 0 {
|
||||
return 0, "", nil
|
||||
}
|
||||
|
||||
if pts0 > 0 && pts1 > 0 {
|
||||
pointCategory = pools[0].Name + " & " + pools[1].Name
|
||||
} else if pts1 > 0 {
|
||||
pointCategory = pools[1].Name
|
||||
} else {
|
||||
pointCategory = pools[0].Name
|
||||
}
|
||||
|
||||
if len(pools) == 1 || pts1 == 0 {
|
||||
result, err = tx.Exec(`
|
||||
UPDATE clients
|
||||
SET point = point + $1, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE username = $2
|
||||
`, totalPoints, username)
|
||||
`, pts0, username)
|
||||
} else {
|
||||
// ── Mode séparé : Barèmes W et Z, pool T ignoré ────────────────────────
|
||||
pointsWeed := CalcPointsFromTiers(totalPrixWeed, settings.PointsWeedTiers)
|
||||
pointsZipette := CalcPointsFromTiers(totalPrixZipette, settings.PointsZipetteTiers)
|
||||
totalPoints = pointsWeed + pointsZipette
|
||||
|
||||
log.Printf("💰 [CalcPointsTx] Mode séparé - weed: %d pts, zipette: %d pts",
|
||||
pointsWeed, pointsZipette)
|
||||
|
||||
if totalPoints == 0 {
|
||||
return 0, "", nil
|
||||
}
|
||||
|
||||
if pointsWeed > 0 && pointsZipette > 0 {
|
||||
pointCategory = "mixed"
|
||||
} else if pointsZipette > 0 {
|
||||
pointCategory = "zipette&co"
|
||||
} else {
|
||||
pointCategory = "weed&hash"
|
||||
}
|
||||
|
||||
result, err = tx.Exec(`
|
||||
UPDATE clients
|
||||
SET point = point + $1, point_zipette = point_zipette + $2, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE username = $3
|
||||
`, pointsWeed, pointsZipette, username)
|
||||
`, pts0, pts1, username)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user