Skip to content
Permalink
Browse files

Calculate taxes based on original price if taxes are included (#188)

  • Loading branch information...
mraerino committed Sep 16, 2019
1 parent ac25dbd commit aff8f9833f12d414bbb5ce499aba30deb5c6388a
Showing with 61 additions and 30 deletions.
  1. +5 −2 calculator/calculator.go
  2. +56 −28 calculator/calculator_test.go
@@ -332,10 +332,13 @@ func calculateTaxes(amountToTax uint64, item Item, params PriceParameters, setti
subtotal = 0
for _, tax := range taxAmounts {
if includeTaxes {
tax.price = rint(float64(tax.price) / (100 + float64(tax.percentage)) * 100)
taxAmount := rint(float64(tax.price) / float64(100+tax.percentage) * 100 * (float64(tax.percentage) / 100))
tax.price -= taxAmount
taxes += taxAmount
} else {
taxes += rint(float64(tax.price) * float64(tax.percentage) / 100)
}
subtotal += tax.price
taxes += rint(float64(tax.price) * float64(tax.percentage) / 100)
}

return
@@ -349,15 +349,6 @@ func TestMixedDiscounts(t *testing.T) {
func TestRealWorldTaxCalculations(t *testing.T) {
settings := &Settings{
PricesIncludeTaxes: true,
Taxes: []*Tax{&Tax{
Percentage: 7,
ProductTypes: []string{"Book"},
Countries: []string{"USA"},
}, &Tax{
Percentage: 19,
ProductTypes: []string{"E-Book"},
Countries: []string{"USA"},
}},
}

item1 := &TestItem{
@@ -371,26 +362,63 @@ func TestRealWorldTaxCalculations(t *testing.T) {
itemType: "E-Book",
}},
}
item2 := &TestItem{
price: 3490,
itemType: "Book",
items: []Item{&TestItem{
price: 2300,
itemType: "Book",
}, &TestItem{
price: 1190,
itemType: "E-Book",
}},
}
params := PriceParameters{"USA", "USD", nil, []Item{item1, item2}}
price := CalculatePrice(settings, nil, params, testLogger)

validatePrice(t, price, Price{
Subtotal: 5766,
Discount: 0,
NetTotal: 5766,
Taxes: 625,
Total: 6391,
t.Run("Single items", func(t *testing.T) {
settings.Taxes = []*Tax{&Tax{
Percentage: 7,
ProductTypes: []string{"Book"},
Countries: []string{"USA"},
}, &Tax{
Percentage: 21,
ProductTypes: []string{"E-Book"},
Countries: []string{"USA"},
}}

params := PriceParameters{"USA", "USD", nil, []Item{item1}}
price := CalculatePrice(settings, nil, params, testLogger)

validatePrice(t, price, Price{
Subtotal: 2602,
Discount: 0,
NetTotal: 2602,
Taxes: 298,
Total: 2900,
})
})

t.Run("Two items", func(t *testing.T) {
settings.Taxes = []*Tax{&Tax{
Percentage: 7,
ProductTypes: []string{"Book"},
Countries: []string{"USA"},
}, &Tax{
Percentage: 19,
ProductTypes: []string{"E-Book"},
Countries: []string{"USA"},
}}

item2 := &TestItem{
price: 3490,
itemType: "Book",
items: []Item{&TestItem{
price: 2300,
itemType: "Book",
}, &TestItem{
price: 1190,
itemType: "E-Book",
}},
}

params := PriceParameters{"USA", "USD", nil, []Item{item1, item2}}
price := CalculatePrice(settings, nil, params, testLogger)

validatePrice(t, price, Price{
Subtotal: 5766,
Discount: 0,
NetTotal: 5766,
Taxes: 624,
Total: 6390,
})
})
}

0 comments on commit aff8f98

Please sign in to comment.
You can’t perform that action at this time.