Browse Source

Add old price to action, xml generation and structs

Vova Tkach 5 years ago
parent
commit
c0e0d14cf7
5 changed files with 92 additions and 33 deletions
  1. 24 0
      engine/fetdata/shop_product.go
  2. 36 16
      modules/module_shop.go
  3. 10 0
      modules/module_shop_act_modify.go
  4. 16 15
      utils/mysql_struct_shop_product.go
  5. 6 2
      xml.go

+ 24 - 0
engine/fetdata/shop_product.go

@@ -237,6 +237,30 @@ func (this *ShopProduct) Price() float64 {
 	}
 }
 
+func (this *ShopProduct) PriceOld() float64 {
+	if this == nil {
+		return 0
+	}
+	if this.Currency() == nil {
+		return this.object.A_price_old
+	}
+	if this.wrap.ShopGetCurrentCurrency() == nil {
+		return this.object.A_price_old
+	}
+	if this.wrap.ShopGetCurrentCurrency().A_id == this.Currency().Id() {
+		return this.object.A_price_old
+	}
+	if this.Currency().Id() == 1 {
+		return this.object.A_price_old * this.wrap.ShopGetCurrentCurrency().A_coefficient
+	} else {
+		if c, ok := (*this.wrap.ShopGetAllCurrencies())[this.Currency().Id()]; ok == true {
+			return this.object.A_price_old / c.A_coefficient
+		} else {
+			return this.object.A_price_old
+		}
+	}
+}
+
 func (this *ShopProduct) PriceNice() string {
 	return utils.FormatProductPrice(
 		this.Price(),

+ 36 - 16
modules/module_shop.go

@@ -527,6 +527,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 					shop_products.user,
 					shop_products.currency,
 					shop_products.price,
+					shop_products.price_old,
 					shop_products.name,
 					shop_products.alias,
 					shop_products.vendor,
@@ -556,6 +557,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				&row.A_user,
 				&row.A_currency,
 				&row.A_price,
+				&row.A_price_old,
 				&row.A_name,
 				&row.A_alias,
 				&row.A_vendor,
@@ -657,7 +659,11 @@ func (this *Modules) RegisterModule_Shop() *Module {
 						NameInTable: "Price",
 						Classes:     "d-none d-md-table-cell",
 						CallBack: func(values *[]string) string {
-							return `<div>` + utils.Float64ToStr(utils.StrToFloat64((*values)[4])) + `</div>` +
+							price_old := ""
+							if utils.StrToFloat64((*values)[9]) > 0 {
+								price_old = `<div><strike>` + utils.Float64ToStr(utils.StrToFloat64((*values)[9])) + `</strike></div>`
+							}
+							return price_old + `<div>` + utils.Float64ToStr(utils.StrToFloat64((*values)[4])) + `</div>` +
 								`<div><small>` + currencies[utils.StrToInt((*values)[3])] + `</small></div>`
 						},
 					},
@@ -687,6 +693,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 						DBField: "pname",
 						DBExp:   "spp.name",
 					},
+					{
+						DBField: "price_old",
+					},
 				},
 				func(values *[]string) string {
 					return builder.DataTableAction(&[]builder.DataTableActionRow{
@@ -728,7 +737,8 @@ func (this *Modules) RegisterModule_Shop() *Module {
 							UNIX_TIMESTAMP(`+"`shop_products`.`datetime`"+`) AS datetime,
 							shop_products.active,
 							shop_products.parent_id,
-							spp.name AS pname
+							spp.name AS pname,
+							shop_products.price_old
 						FROM
 							shop_products
 							LEFT JOIN shop_products AS spp ON spp.id = shop_products.parent_id
@@ -1105,20 +1115,21 @@ func (this *Modules) RegisterModule_Shop() *Module {
 			}
 
 			data := utils.MySql_shop_product{
-				A_id:       0,
-				A_user:     0,
-				A_currency: 0,
-				A_price:    0,
-				A_gname:    "",
-				A_name:     "",
-				A_alias:    "",
-				A_vendor:   "",
-				A_quantity: 0,
-				A_category: 0,
-				A_briefly:  "",
-				A_content:  "",
-				A_datetime: 0,
-				A_active:   0,
+				A_id:        0,
+				A_user:      0,
+				A_currency:  0,
+				A_price:     0,
+				A_price_old: 0,
+				A_gname:     "",
+				A_name:      "",
+				A_alias:     "",
+				A_vendor:    "",
+				A_quantity:  0,
+				A_category:  0,
+				A_briefly:   "",
+				A_content:   "",
+				A_datetime:  0,
+				A_active:    0,
 			}
 
 			if wrap.CurrSubModule == "modify" {
@@ -1135,6 +1146,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 						user,
 						currency,
 						price,
+						price_old,
 						gname,
 						name,
 						alias,
@@ -1156,6 +1168,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 					&data.A_user,
 					&data.A_currency,
 					&data.A_price,
+					&data.A_price_old,
 					&data.A_gname,
 					&data.A_name,
 					&data.A_alias,
@@ -1300,7 +1313,14 @@ func (this *Modules) RegisterModule_Shop() *Module {
 							`<div>` +
 							`<div class="row">` +
 							`<div class="col-md-8">` +
+							`<div class="row">` +
+							`<div class="col-md-6">` +
 							`<div><input class="form-control" type="number" step="0.01" id="lbl_price" name="price" value="` + utils.Float64ToStr(data.A_price) + `" placeholder="" autocomplete="off" required></div>` +
+							`</div>` +
+							`<div class="col-md-6">` +
+							`<div><input class="form-control" type="number" id="lbl_price_old" name="price_old" value="` + utils.Float64ToStr(data.A_price_old) + `" placeholder="" autocomplete="off"></div>` +
+							`</div>` +
+							`</div>` +
 							`<div class="d-md-none mb-3"></div>` +
 							`</div>` +
 							`<div class="col-md-4">` +

+ 10 - 0
modules/module_shop_act_modify.go

@@ -18,6 +18,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 		pf_gname := wrap.R.FormValue("gname")
 		pf_name := wrap.R.FormValue("name")
 		pf_price := wrap.R.FormValue("price")
+		pf_price_old := wrap.R.FormValue("price_old")
 		pf_currency := wrap.R.FormValue("currency")
 		pf_alias := wrap.R.FormValue("alias")
 		pf_vendor := wrap.R.FormValue("vendor")
@@ -41,6 +42,11 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 			return
 		}
 
+		if !utils.IsFloat(pf_price_old) {
+			wrap.MsgError(`Inner system error`)
+			return
+		}
+
 		if !utils.IsNumeric(pf_currency) {
 			wrap.MsgError(`Inner system error`)
 			return
@@ -96,6 +102,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 						user = ?,
 						currency = ?,
 						price = ?,
+						price_old = ?,
 						gname = ?,
 						name = ?,
 						alias = ?,
@@ -110,6 +117,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 					wrap.User.A_id,
 					utils.StrToInt(pf_currency),
 					utils.StrToFloat64(pf_price),
+					utils.StrToFloat64(pf_price_old),
 					pf_gname,
 					pf_name,
 					pf_alias,
@@ -213,6 +221,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 					`UPDATE shop_products SET
 						currency = ?,
 						price = ?,
+						price_old = ?,
 						gname = ?,
 						name = ?,
 						alias = ?,
@@ -227,6 +236,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 					;`,
 					utils.StrToInt(pf_currency),
 					utils.StrToFloat64(pf_price),
+					utils.StrToFloat64(pf_price_old),
 					pf_gname,
 					pf_name,
 					pf_alias,

+ 16 - 15
utils/mysql_struct_shop_product.go

@@ -5,21 +5,22 @@ import (
 )
 
 type MySql_shop_product struct {
-	A_id       int
-	A_parent   sql.NullInt64
-	A_user     int
-	A_currency int
-	A_price    float64
-	A_gname    string
-	A_name     string
-	A_alias    string
-	A_vendor   string
-	A_quantity int
-	A_category int
-	A_briefly  string
-	A_content  string
-	A_datetime int
-	A_active   int
+	A_id        int
+	A_parent    sql.NullInt64
+	A_user      int
+	A_currency  int
+	A_price     float64
+	A_price_old float64
+	A_gname     string
+	A_name      string
+	A_alias     string
+	A_vendor    string
+	A_quantity  int
+	A_category  int
+	A_briefly   string
+	A_content   string
+	A_datetime  int
+	A_active    int
 }
 
 func (this *MySql_shop_product) A_parent_id() int {

+ 6 - 2
xml.go

@@ -223,7 +223,8 @@ func xml_gen_offers(db *sqlw.DB, conf *config.Config) string {
 			shop_products.quantity,
 			shop_products.category,
 			shop_products.content,
-			IFNULL(shop_products.parent_id, 0)
+			IFNULL(shop_products.parent_id, 0),
+			shop_products.price_old
 		FROM
 			shop_products
 			LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
@@ -236,7 +237,7 @@ func xml_gen_offers(db *sqlw.DB, conf *config.Config) string {
 	)
 	if err == nil {
 		defer rows.Close()
-		values := make([]string, 10)
+		values := make([]string, 11)
 		scan := make([]interface{}, len(values))
 		for i := range values {
 			scan[i] = &values[i]
@@ -247,6 +248,9 @@ func xml_gen_offers(db *sqlw.DB, conf *config.Config) string {
 				result += `<offer id="` + html.EscapeString(string(values[0])) + `" available="true">`
 				result += `<url>` + html.EscapeString((*conf).API.XML.Url) + `shop/` + html.EscapeString(string(values[4])) + `/</url>`
 				result += `<price>` + utils.Float64ToStrF(utils.StrToFloat64(string(values[2])), "%.2f") + `</price>`
+				if utils.StrToFloat64(string(values[10])) > 0 {
+					result += `<price_old>` + utils.Float64ToStrF(utils.StrToFloat64(string(values[10])), "%.2f") + `</price_old>`
+				}
 				result += `<currencyId>` + html.EscapeString(string(values[1])) + `</currencyId>`
 				result += `<categoryId>` + html.EscapeString(string(values[7])) + `</categoryId>`
 				result += xml_gen_offer_pictures(db, conf, utils.StrToInt(string(values[0])), utils.StrToInt(string(values[9])))