Browse Source

PriceNice in basket (formating)

Vova Tkach 5 years ago
parent
commit
be097583fa
4 changed files with 44 additions and 18 deletions
  1. 7 5
      engine/basket/basket.go
  2. 35 13
      engine/basket/session.go
  3. 1 0
      engine/fetdata/fetdata.go
  4. 1 0
      modules/module_shop.go

+ 7 - 5
engine/basket/basket.go

@@ -5,12 +5,14 @@ import (
 	"sync"
 
 	"golang-fave/engine/sqlw"
+	"golang-fave/engine/wrapper/config"
 )
 
 type SBParam struct {
 	R         *http.Request
 	DB        *sqlw.DB
 	Host      string
+	Config    *config.Config
 	SessionId string
 }
 
@@ -36,7 +38,7 @@ func (this *Basket) Info(p *SBParam) string {
 	if h, ok := this.hosts[p.Host]; ok == true {
 		if s, ok := h.sessions[p.SessionId]; ok == true {
 			s.Preload(p.R, p.DB)
-			return s.String(p.DB)
+			return s.String(p.DB, (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
 		} else {
 			return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
 		}
@@ -56,14 +58,14 @@ func (this *Basket) Plus(p *SBParam, product_id int) string {
 	if h, ok := this.hosts[p.Host]; ok == true {
 		if s, ok := h.sessions[p.SessionId]; ok == true {
 			s.Preload(p.R, p.DB)
-			s.Plus(p.DB, product_id)
+			s.Plus(p.DB, product_id, (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
 		}
 	} else {
 		s := &session{}
 		s.listCurrencies = map[int]*currency{}
 		s.Products = map[int]*product{}
 		s.Preload(p.R, p.DB)
-		s.Plus(p.DB, product_id)
+		s.Plus(p.DB, product_id, (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
 		h := &onehost{}
 		h.sessions = map[string]*session{}
 		h.sessions[p.SessionId] = s
@@ -84,7 +86,7 @@ func (this *Basket) Minus(p *SBParam, product_id int) string {
 	if h, ok := this.hosts[p.Host]; ok == true {
 		if s, ok := h.sessions[p.SessionId]; ok == true {
 			s.Preload(p.R, p.DB)
-			s.Minus(p.DB, product_id)
+			s.Minus(p.DB, product_id, (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
 		}
 	}
 
@@ -102,7 +104,7 @@ func (this *Basket) Remove(p *SBParam, product_id int) string {
 	if h, ok := this.hosts[p.Host]; ok == true {
 		if s, ok := h.sessions[p.SessionId]; ok == true {
 			s.Preload(p.R, p.DB)
-			s.Remove(p.DB, product_id)
+			s.Remove(p.DB, product_id, (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
 		}
 	}
 

+ 35 - 13
engine/basket/session.go

@@ -3,6 +3,7 @@ package basket
 import (
 	"encoding/json"
 	"html"
+	"math"
 	"net/http"
 	"strings"
 
@@ -20,6 +21,27 @@ type session struct {
 	TotalCount int              `json:"total_count"`
 }
 
+func (this *session) priceFormat(product_price float64, format, round int) string {
+	price := product_price
+	if round == 1 {
+		price = math.Ceil(price)
+	} else if round == 2 {
+		price = math.Floor(price)
+	}
+
+	if format == 1 {
+		return utils.Float64ToStrF(price, "%.1f")
+	} else if format == 2 {
+		return utils.Float64ToStrF(price, "%.2f")
+	} else if format == 3 {
+		return utils.Float64ToStrF(price, "%.3f")
+	} else if format == 4 {
+		return utils.Float64ToStrF(price, "%.4f")
+	}
+
+	return utils.Float64ToStrF(price, "%.0f")
+}
+
 func (this *session) makePrice(product_price float64, product_currency_id int) float64 {
 	if this.Currency == nil {
 		return product_price
@@ -145,16 +167,16 @@ func (this *session) updateProducts(db *sqlw.DB) {
 	}
 }
 
-func (this *session) updateTotals() {
+func (this *session) updateTotals(format, round int) {
 	this.totalSum = 0
 	this.TotalCount = 0
 	for _, product := range this.Products {
-		product.Price = utils.Float64ToStrF(this.makePrice(product.price, product.currency.Id), "%.2f")
-		product.Sum = utils.Float64ToStrF(this.makePrice(product.price*float64(product.Quantity), product.currency.Id), "%.2f")
+		product.Price = this.priceFormat(this.makePrice(product.price, product.currency.Id), format, round)
+		product.Sum = this.priceFormat(this.makePrice(product.price*float64(product.Quantity), product.currency.Id), format, round)
 		this.totalSum += this.makePrice(product.price, product.currency.Id) * float64(product.Quantity)
 		this.TotalCount += product.Quantity
 	}
-	this.TotalSum = utils.Float64ToStrF(this.totalSum, "%.2f")
+	this.TotalSum = this.priceFormat(this.totalSum, format, round)
 }
 
 // Info, Plus, Minus
@@ -220,9 +242,9 @@ func (this *session) Preload(r *http.Request, db *sqlw.DB) {
 	}
 }
 
-func (this *session) String(db *sqlw.DB) string {
+func (this *session) String(db *sqlw.DB, format, round int) string {
 	this.updateProducts(db)
-	this.updateTotals()
+	this.updateTotals(format, round)
 
 	json, err := json.Marshal(this)
 	if err != nil {
@@ -232,11 +254,11 @@ func (this *session) String(db *sqlw.DB) string {
 	return string(json)
 }
 
-func (this *session) Plus(db *sqlw.DB, product_id int) {
+func (this *session) Plus(db *sqlw.DB, product_id, format, round int) {
 	if p, ok := this.Products[product_id]; ok == true {
 		p.Quantity++
 		this.updateProducts(db)
-		this.updateTotals()
+		this.updateTotals(format, round)
 		return
 	}
 	row := &utils.MySql_shop_product{}
@@ -332,11 +354,11 @@ func (this *session) Plus(db *sqlw.DB, product_id int) {
 			Quantity: 1,
 		}
 		this.updateProducts(db)
-		this.updateTotals()
+		this.updateTotals(format, round)
 	}
 }
 
-func (this *session) Minus(db *sqlw.DB, product_id int) {
+func (this *session) Minus(db *sqlw.DB, product_id, format, round int) {
 	if p, ok := this.Products[product_id]; ok == true {
 		if p.Quantity > 1 {
 			p.Quantity--
@@ -344,15 +366,15 @@ func (this *session) Minus(db *sqlw.DB, product_id int) {
 			delete(this.Products, product_id)
 		}
 		this.updateProducts(db)
-		this.updateTotals()
+		this.updateTotals(format, round)
 	}
 }
 
-func (this *session) Remove(db *sqlw.DB, product_id int) {
+func (this *session) Remove(db *sqlw.DB, product_id, format, round int) {
 	if _, ok := this.Products[product_id]; ok == true {
 		delete(this.Products, product_id)
 		this.updateProducts(db)
-		this.updateTotals()
+		this.updateTotals(format, round)
 	}
 }
 

+ 1 - 0
engine/fetdata/fetdata.go

@@ -204,6 +204,7 @@ func (this *FERData) ShopBasketProductsCount() int {
 		R:         this.wrap.R,
 		DB:        this.wrap.DB,
 		Host:      this.wrap.CurrHost,
+		Config:    this.wrap.Config,
 		SessionId: this.wrap.GetSessionId(),
 	})
 }

+ 1 - 0
modules/module_shop.go

@@ -451,6 +451,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				R:         wrap.R,
 				DB:        wrap.DB,
 				Host:      wrap.CurrHost,
+				Config:    wrap.Config,
 				SessionId: wrap.GetSessionId(),
 			}
 			if wrap.UrlArgs[2] == "info" {