diff --git a/cart-grain.go b/cart-grain.go index a8f9be9..082d2a9 100644 --- a/cart-grain.go +++ b/cart-grain.go @@ -75,6 +75,8 @@ type CartGrain struct { Id CartId `json:"id"` Items []*CartItem `json:"items"` TotalPrice int64 `json:"totalPrice"` + TotalTax int64 `json:"totalTax"` + TotalDiscount int64 `json:"totalDiscount"` Deliveries []*CartDelivery `json:"deliveries,omitempty"` Processing bool `json:"processing"` PaymentInProgress bool `json:"paymentInProgress"` @@ -301,7 +303,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa existingItem, found := c.FindItemWithSku(msg.Sku) if found { existingItem.Quantity += int(msg.Quantity) - c.TotalPrice += existingItem.Price * int64(msg.Quantity) + c.UpdateTotals() } else { c.mu.Lock() c.lastItemId++ @@ -309,6 +311,9 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa if msg.Tax > 0 { tax = int(msg.Tax) } + total := int(msg.Price) * int(msg.Quantity) + taxAmount := GetTaxAmount(total, tax) + c.Items = append(c.Items, &CartItem{ Id: c.lastItemId, ItemId: int(msg.ItemId), @@ -328,9 +333,9 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa OrgPrice: msg.OrgPrice, ArticleType: msg.ArticleType, Outlet: msg.Outlet, - Tax: tax, + Tax: taxAmount, }) - c.TotalPrice += msg.Price * int64(msg.Quantity) + c.UpdateTotals() c.mu.Unlock() } @@ -344,17 +349,18 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa for i, item := range c.Items { if item.Id == int(msg.Id) { if msg.Quantity <= 0 { - c.TotalPrice -= item.Price * int64(item.Quantity) + //c.TotalPrice -= item.Price * int64(item.Quantity) c.Items = append(c.Items[:i], c.Items[i+1:]...) } else { - diff := int(msg.Quantity) - item.Quantity + //diff := int(msg.Quantity) - item.Quantity item.Quantity = int(msg.Quantity) - c.TotalPrice += item.Price * int64(diff) + //c.TotalPrice += item.Price * int64(diff) } break } } + c.UpdateTotals() } case RemoveItemType: @@ -366,13 +372,13 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa items := make([]*CartItem, 0, len(c.Items)) for _, item := range c.Items { if item.Id == int(msg.Id) { - c.TotalPrice -= item.Price * int64(item.Quantity) + //c.TotalPrice -= item.Price * int64(item.Quantity) } else { items = append(items, item) } } c.Items = items - + c.UpdateTotals() } case SetDeliveryType: msg, ok := message.Content.(*messages.SetDelivery) @@ -407,6 +413,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa Items: items, }) c.Processing = true + c.UpdateTotals() go func() { time.Sleep(5 * time.Second) c.Processing = false @@ -429,7 +436,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa } } c.Deliveries = deliveries - + c.UpdateTotals() } case SetPickupPointType: msg, ok := message.Content.(*messages.SetPickupPoint) @@ -548,3 +555,19 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa msg := MakeFrameWithPayload(RemoteHandleMutationReply, 200, result) return &msg, err } + +func (c *CartGrain) UpdateTotals() { + c.TotalPrice = 0 + 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) + itemDiff := max(0, item.OrgPrice-item.Price) + c.TotalDiscount += itemDiff * int64(item.Quantity) + } + for _, delivery := range c.Deliveries { + c.TotalPrice += delivery.Price + } + +}