Skip to content

Commit e2c44ef

Browse files
committed
Calculate taxes based on original price if taxes are included
1 parent fe0f2d7 commit e2c44ef

File tree

2 files changed

+61
-30
lines changed

2 files changed

+61
-30
lines changed

calculator/calculator.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,13 @@ func calculateTaxes(amountToTax uint64, item Item, params PriceParameters, setti
332332
subtotal = 0
333333
for _, tax := range taxAmounts {
334334
if includeTaxes {
335-
tax.price = rint(float64(tax.price) / (100 + float64(tax.percentage)) * 100)
335+
taxAmount := rint(float64(tax.price) / float64(100+tax.percentage) * 100 * (float64(tax.percentage) / 100))
336+
tax.price -= taxAmount
337+
taxes += taxAmount
338+
} else {
339+
taxes += rint(float64(tax.price) * float64(tax.percentage) / 100)
336340
}
337341
subtotal += tax.price
338-
taxes += rint(float64(tax.price) * float64(tax.percentage) / 100)
339342
}
340343

341344
return

calculator/calculator_test.go

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,6 @@ func TestMixedDiscounts(t *testing.T) {
349349
func TestRealWorldTaxCalculations(t *testing.T) {
350350
settings := &Settings{
351351
PricesIncludeTaxes: true,
352-
Taxes: []*Tax{&Tax{
353-
Percentage: 7,
354-
ProductTypes: []string{"Book"},
355-
Countries: []string{"USA"},
356-
}, &Tax{
357-
Percentage: 19,
358-
ProductTypes: []string{"E-Book"},
359-
Countries: []string{"USA"},
360-
}},
361352
}
362353

363354
item1 := &TestItem{
@@ -371,26 +362,63 @@ func TestRealWorldTaxCalculations(t *testing.T) {
371362
itemType: "E-Book",
372363
}},
373364
}
374-
item2 := &TestItem{
375-
price: 3490,
376-
itemType: "Book",
377-
items: []Item{&TestItem{
378-
price: 2300,
379-
itemType: "Book",
380-
}, &TestItem{
381-
price: 1190,
382-
itemType: "E-Book",
383-
}},
384-
}
385-
params := PriceParameters{"USA", "USD", nil, []Item{item1, item2}}
386-
price := CalculatePrice(settings, nil, params, testLogger)
387365

388-
validatePrice(t, price, Price{
389-
Subtotal: 5766,
390-
Discount: 0,
391-
NetTotal: 5766,
392-
Taxes: 625,
393-
Total: 6391,
366+
t.Run("Single items", func(t *testing.T) {
367+
settings.Taxes = []*Tax{&Tax{
368+
Percentage: 7,
369+
ProductTypes: []string{"Book"},
370+
Countries: []string{"USA"},
371+
}, &Tax{
372+
Percentage: 21,
373+
ProductTypes: []string{"E-Book"},
374+
Countries: []string{"USA"},
375+
}}
376+
377+
params := PriceParameters{"USA", "USD", nil, []Item{item1}}
378+
price := CalculatePrice(settings, nil, params, testLogger)
379+
380+
validatePrice(t, price, Price{
381+
Subtotal: 2602,
382+
Discount: 0,
383+
NetTotal: 2602,
384+
Taxes: 298,
385+
Total: 2900,
386+
})
387+
})
388+
389+
t.Run("Two items", func(t *testing.T) {
390+
settings.Taxes = []*Tax{&Tax{
391+
Percentage: 7,
392+
ProductTypes: []string{"Book"},
393+
Countries: []string{"USA"},
394+
}, &Tax{
395+
Percentage: 19,
396+
ProductTypes: []string{"E-Book"},
397+
Countries: []string{"USA"},
398+
}}
399+
400+
item2 := &TestItem{
401+
price: 3490,
402+
itemType: "Book",
403+
items: []Item{&TestItem{
404+
price: 2300,
405+
itemType: "Book",
406+
}, &TestItem{
407+
price: 1190,
408+
itemType: "E-Book",
409+
}},
410+
}
411+
412+
params := PriceParameters{"USA", "USD", nil, []Item{item1, item2}}
413+
price := CalculatePrice(settings, nil, params, testLogger)
414+
415+
validatePrice(t, price, Price{
416+
Subtotal: 5766,
417+
Discount: 0,
418+
NetTotal: 5766,
419+
Taxes: 624,
420+
Total: 6390,
421+
})
394422
})
395423
}
396424

0 commit comments

Comments
 (0)