chore: build
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"gestion/utils"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -412,6 +413,7 @@ func GetAllProducts(c *gin.Context) {
|
||||
if role != "admin" && role != "cabine" {
|
||||
products = filterActivePrices(products)
|
||||
}
|
||||
products = applyPromotions(products, database)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": products,
|
||||
@@ -450,6 +452,7 @@ func GetProductsByCategory(c *gin.Context) {
|
||||
if roleCtx != "admin" && roleCtx != "cabine" {
|
||||
products = filterActivePrices(products)
|
||||
}
|
||||
products = applyPromotions(products, database)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": products,
|
||||
@@ -482,6 +485,7 @@ func GetProductByID(c *gin.Context) {
|
||||
if role != "admin" && role != "cabine" {
|
||||
filterActivepricesSingle(&product)
|
||||
}
|
||||
applyPromotionsSingle(&product, database)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
@@ -1002,3 +1006,32 @@ func filterActivepricesSingle(product *models.Product) {
|
||||
}
|
||||
product.Prices = activePrices
|
||||
}
|
||||
|
||||
// applyPromotions annote chaque palier de prix éligible avec le prix promo
|
||||
// (PromoPrice/PromoPercent) si une promotion couvre ce produit/quantité —
|
||||
// affichage seulement, le prix catalogue (Price) n'est jamais modifié ici ;
|
||||
// le prix réellement facturé est recalculé indépendamment dans AddToBasket.
|
||||
func applyPromotions(products []models.Product, database *db.Database) []models.Product {
|
||||
settings, err := database.GetSettings()
|
||||
if err != nil || !settings.PromotionsEnabled {
|
||||
return products
|
||||
}
|
||||
for i := range products {
|
||||
for j := range products[i].Prices {
|
||||
pr := &products[i].Prices[j]
|
||||
discount, ok := db.ResolvePromotionDiscount(&settings, products[i].ID, products[i].Category, pr.Quantity)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
promoPrice := math.Round(pr.Price*(1-discount/100)*100) / 100
|
||||
pr.PromoPrice = &promoPrice
|
||||
pr.PromoPercent = discount
|
||||
}
|
||||
}
|
||||
return products
|
||||
}
|
||||
|
||||
func applyPromotionsSingle(product *models.Product, database *db.Database) {
|
||||
products := applyPromotions([]models.Product{*product}, database)
|
||||
*product = products[0]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user