49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
package db
|
|
|
|
import (
|
|
"gestion/models"
|
|
"math"
|
|
)
|
|
|
|
// ResolvePromotionDiscount retourne le pourcentage de réduction actif pour un
|
|
// produit, sa catégorie catalogue et une quantité donnés, si une promotion
|
|
// configurée dans les settings couvre exactement ce couple (produit,
|
|
// quantité) — contrairement aux récompenses, aucun seuil de points n'entre
|
|
// en jeu : la promotion s'applique à toute commande de cette quantité.
|
|
func ResolvePromotionDiscount(settings *models.AppSettings, productID int, category string, quantity float64) (float64, bool) {
|
|
if settings == nil || !settings.PromotionsEnabled {
|
|
return 0, false
|
|
}
|
|
for _, promo := range settings.Promotions {
|
|
if promo.Category != category || promo.DiscountPercent <= 0 {
|
|
continue
|
|
}
|
|
if promo.AllProducts {
|
|
if promo.Quantity == quantity {
|
|
return promo.DiscountPercent, true
|
|
}
|
|
continue
|
|
}
|
|
for _, pq := range promo.Products {
|
|
if pq.ProductID == productID && pq.Quantity == quantity {
|
|
return promo.DiscountPercent, true
|
|
}
|
|
}
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
// ApplyPromotionToPrice applique la réduction (si une promotion couvre ce
|
|
// produit/quantité/catégorie) au prix catalogue donné, arrondi au centime.
|
|
func (d *Database) ApplyPromotionToPrice(productID int, category string, quantity, price float64) (float64, bool) {
|
|
settings, err := d.GetSettings()
|
|
if err != nil {
|
|
return price, false
|
|
}
|
|
discount, ok := ResolvePromotionDiscount(&settings, productID, category, quantity)
|
|
if !ok {
|
|
return price, false
|
|
}
|
|
return math.Round(price*(1-discount/100)*100) / 100, true
|
|
}
|