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