update calculations
Some checks failed
Build and Publish / BuildAndDeploy (push) Successful in 3m14s
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled

This commit is contained in:
matst80
2025-05-05 13:46:22 +02:00
parent 08a4a5c216
commit ebd54b1476

View File

@@ -43,6 +43,8 @@ type CartItem struct {
Sku string `json:"sku"` Sku string `json:"sku"`
Name string `json:"name"` Name string `json:"name"`
Price int64 `json:"price"` Price int64 `json:"price"`
TotalPrice int64 `json:"totalPrice"`
TotalTax int64 `json:"totalTax"`
OrgPrice int64 `json:"orgPrice"` OrgPrice int64 `json:"orgPrice"`
Stock StockStatus `json:"stock"` Stock StockStatus `json:"stock"`
Quantity int `json:"qty"` Quantity int `json:"qty"`
@@ -249,9 +251,9 @@ func (c *CartGrain) FindItemWithSku(sku string) (*CartItem, bool) {
return nil, false return nil, false
} }
func GetTaxAmount(total int, tax int) int { func GetTaxAmount(total int64, tax int) int64 {
taxD := 10000 / float64(tax) taxD := 10000 / float64(tax)
return int(float64(total) / float64((1 + taxD))) return int64(float64(total) / float64((1 + taxD)))
} }
func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPayload, error) { func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPayload, error) {
@@ -312,7 +314,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
tax = int(msg.Tax) tax = int(msg.Tax)
} }
taxAmount := GetTaxAmount(int(msg.Price), tax) taxAmount := GetTaxAmount(msg.Price, tax)
c.Items = append(c.Items, &CartItem{ c.Items = append(c.Items, &CartItem{
Id: c.lastItemId, Id: c.lastItemId,
@@ -321,6 +323,8 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
Sku: msg.Sku, Sku: msg.Sku,
Name: msg.Name, Name: msg.Name,
Price: msg.Price, Price: msg.Price,
TotalPrice: msg.Price * int64(msg.Quantity),
TotalTax: int64(taxAmount * int64(msg.Quantity)),
Image: msg.Image, Image: msg.Image,
Stock: StockStatus(msg.Stock), Stock: StockStatus(msg.Stock),
Disclaimer: msg.Disclaimer, Disclaimer: msg.Disclaimer,
@@ -333,7 +337,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
OrgPrice: msg.OrgPrice, OrgPrice: msg.OrgPrice,
ArticleType: msg.ArticleType, ArticleType: msg.ArticleType,
Outlet: msg.Outlet, Outlet: msg.Outlet,
Tax: taxAmount, Tax: int(taxAmount),
}) })
c.UpdateTotals() c.UpdateTotals()
c.mu.Unlock() c.mu.Unlock()
@@ -561,8 +565,12 @@ func (c *CartGrain) UpdateTotals() {
c.TotalTax = 0 c.TotalTax = 0
c.TotalDiscount = 0 c.TotalDiscount = 0
for _, item := range c.Items { for _, item := range c.Items {
c.TotalPrice += item.Price * int64(item.Quantity) rowTotal := item.Price * int64(item.Quantity)
c.TotalTax += int64(item.Tax) * int64(item.Quantity) rowTax := int64(item.Tax) * int64(item.Quantity)
item.TotalPrice = rowTotal
item.TotalTax = rowTax
c.TotalPrice += rowTotal
c.TotalTax += rowTax
itemDiff := max(0, item.OrgPrice-item.Price) itemDiff := max(0, item.OrgPrice-item.Price)
c.TotalDiscount += itemDiff * int64(item.Quantity) c.TotalDiscount += itemDiff * int64(item.Quantity)
} }