diff --git a/cart-grain.go b/cart-grain.go index 9d9fecf..35847e0 100644 --- a/cart-grain.go +++ b/cart-grain.go @@ -43,6 +43,8 @@ type CartItem struct { Sku string `json:"sku"` Name string `json:"name"` Price int64 `json:"price"` + TotalPrice int64 `json:"totalPrice"` + TotalTax int64 `json:"totalTax"` OrgPrice int64 `json:"orgPrice"` Stock StockStatus `json:"stock"` Quantity int `json:"qty"` @@ -249,9 +251,9 @@ func (c *CartGrain) FindItemWithSku(sku string) (*CartItem, bool) { return nil, false } -func GetTaxAmount(total int, tax int) int { +func GetTaxAmount(total int64, tax int) int64 { 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) { @@ -312,7 +314,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa tax = int(msg.Tax) } - taxAmount := GetTaxAmount(int(msg.Price), tax) + taxAmount := GetTaxAmount(msg.Price, tax) c.Items = append(c.Items, &CartItem{ Id: c.lastItemId, @@ -321,6 +323,8 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa Sku: msg.Sku, Name: msg.Name, Price: msg.Price, + TotalPrice: msg.Price * int64(msg.Quantity), + TotalTax: int64(taxAmount * int64(msg.Quantity)), Image: msg.Image, Stock: StockStatus(msg.Stock), Disclaimer: msg.Disclaimer, @@ -333,7 +337,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa OrgPrice: msg.OrgPrice, ArticleType: msg.ArticleType, Outlet: msg.Outlet, - Tax: taxAmount, + Tax: int(taxAmount), }) c.UpdateTotals() c.mu.Unlock() @@ -561,8 +565,12 @@ func (c *CartGrain) UpdateTotals() { c.TotalTax = 0 c.TotalDiscount = 0 for _, item := range c.Items { - c.TotalPrice += item.Price * int64(item.Quantity) - c.TotalTax += int64(item.Tax) * int64(item.Quantity) + rowTotal := item.Price * 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) c.TotalDiscount += itemDiff * int64(item.Quantity) }