Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 0e85152

Browse files
committed
Fixes for calculations for taxes
1 parent 10aca03 commit 0e85152

File tree

2 files changed

+111
-21
lines changed

2 files changed

+111
-21
lines changed

core/order.go

Lines changed: 111 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ func (n *OpenBazaarNode) CheckoutBreakdown(data *repo.PurchaseData) (repo.Checko
896896
return emptyCheckoutBreakdown, err
897897
}
898898

899-
totalSurcharge, err := GetTotalSurchargeAmount(nrl, firstItem.Options)
899+
itemSurcharge, err := GetItemSurchargeAmount(nrl, firstItem.Options)
900900
if err != nil {
901901
return emptyCheckoutBreakdown, err
902902
}
@@ -932,8 +932,9 @@ func (n *OpenBazaarNode) CheckoutBreakdown(data *repo.PurchaseData) (repo.Checko
932932
if err != nil {
933933
return emptyCheckoutBreakdown, err
934934
}
935+
totalSurcharge := new(big.Int).Mul(itemSurcharge, totalQuantity)
935936
cv := &repo.CurrencyValue{
936-
Amount: totalSurcharge.Add(totalSurcharge, itemOriginAmt.Amount),
937+
Amount: new(big.Int).Add(itemSurcharge, itemOriginAmt.Amount),
937938
Currency: listingCurDef,
938939
}
939940
couponDiscount, err := GetTotalCouponCodeDiscount(nrl, firstItem.CouponCodes, cv)
@@ -966,28 +967,51 @@ func (n *OpenBazaarNode) CheckoutBreakdown(data *repo.PurchaseData) (repo.Checko
966967

967968
shippingTotal := new(big.Int).SetInt64(0)
968969
if isPhysicalGood {
969-
970-
shippingTotal, err = n.calculateShippingTotalForListings(contract, physicalGoods)
970+
shippingTotal, err = getPretaxShippingCost(v5Order, nrl)
971971
if err != nil {
972972
return emptyCheckoutBreakdown, err
973973
}
974974
}
975+
// Convert to final currency
976+
shippingCurrencyValue := repo.NewCurrencyValueFromBigInt(shippingTotal, listingCurDef)
977+
finalShippingTotal, _, err := shippingCurrencyValue.ConvertUsingProtobufDef(v5Order.Payment.AmountCurrency, cc)
978+
if err != nil {
979+
return emptyCheckoutBreakdown, err
980+
}
975981

976982
// Taxes
977983
taxesTotal := new(big.Int).SetInt64(0)
978984
for _, tax := range nrl.GetProtobuf().Taxes {
979985
for _, taxRegion := range tax.TaxRegions {
980986
if contract.BuyerOrder.Shipping.Country == taxRegion {
981987
factor := toHundredths(tax.Percentage)
982-
taxes, _ := new(big.Float).Mul(new(big.Float).SetInt(itemOriginAmt.Amount), factor).Int(nil)
988+
989+
var taxes *big.Int
990+
amountToTax := new(big.Int).SetInt64(0)
991+
992+
totalBasePrice := new(big.Int).Mul(itemOriginAmt.Amount, totalQuantity)
993+
amountToTax.Add(totalBasePrice, totalSurcharge)
994+
amountToTax.Sub(amountToTax, couponDiscount)
995+
996+
if tax.TaxShipping {
997+
amountToTax.Add(amountToTax, shippingTotal)
998+
}
999+
1000+
taxes, _ = new(big.Float).Mul(new(big.Float).SetInt(amountToTax), factor).Int(nil)
9831001
taxesTotal = new(big.Int).Add(taxesTotal, taxes)
1002+
9841003
break
9851004
}
9861005
}
9871006
}
1007+
taxesCurrencyValue := repo.NewCurrencyValueFromBigInt(taxesTotal, listingCurDef)
1008+
finalTaxesTotal, _, err := taxesCurrencyValue.ConvertUsingProtobufDef(v5Order.Payment.AmountCurrency, cc)
1009+
if err != nil {
1010+
return emptyCheckoutBreakdown, err
1011+
}
9881012

989-
checkoutBreakdown.Tax = taxesTotal.String()
990-
checkoutBreakdown.ShippingPrice = shippingTotal.String()
1013+
checkoutBreakdown.Tax = finalTaxesTotal.Amount.String()
1014+
checkoutBreakdown.ShippingPrice = finalShippingTotal.Amount.String()
9911015
checkoutBreakdown.Coupon = finalCouponDiscount.Amount.String()
9921016
checkoutBreakdown.OptionSurcharge = finalOptionSurcharge.Amount.String()
9931017
checkoutBreakdown.BasePrice = finalBasePrice.Amount.String()
@@ -997,6 +1021,60 @@ func (n *OpenBazaarNode) CheckoutBreakdown(data *repo.PurchaseData) (repo.Checko
9971021
return checkoutBreakdown, nil
9981022
}
9991023

1024+
func getPretaxShippingCost(v5Order *pb.Order, nrl *repo.Listing) (*big.Int, error) {
1025+
pretaxShippingCost := new(big.Int).SetInt64(0)
1026+
1027+
for _, item := range v5Order.Items {
1028+
//shippingOption := item.ShippingOption
1029+
1030+
itemQuantity, ok := new(big.Int).SetString(item.BigQuantity, 10)
1031+
if !ok {
1032+
return new(big.Int).SetInt64(0), errors.New("bad bigQuantity")
1033+
}
1034+
listingShippingOptions, err := nrl.GetShippingOptions()
1035+
if err != nil {
1036+
return new(big.Int).SetInt64(0), err
1037+
}
1038+
1039+
for _, listingShippingOption := range listingShippingOptions {
1040+
if inShippingRegions(v5Order.Shipping.Country, listingShippingOption.Regions) {
1041+
for _, listingService := range listingShippingOption.Services {
1042+
if item.ShippingOption.Service == listingService.Name {
1043+
servicePrice, _ := new(big.Int).SetString(listingService.BigPrice, 10)
1044+
if err != nil {
1045+
return new(big.Int).SetInt64(0), err
1046+
}
1047+
additionalItemPrice, _ := new(big.Int).SetString(listingService.BigAdditionalItemPrice, 10)
1048+
if err != nil {
1049+
return new(big.Int).SetInt64(0), err
1050+
}
1051+
1052+
pretaxShippingCost.Add(pretaxShippingCost, servicePrice)
1053+
1054+
if itemQuantity.Cmp(new(big.Int).SetInt64(1)) == 1 {
1055+
// Add additional item price for each quantity over 1
1056+
additionalCost := new(big.Int).Mul(additionalItemPrice, itemQuantity)
1057+
pretaxShippingCost.Add(pretaxShippingCost, additionalCost)
1058+
}
1059+
1060+
}
1061+
}
1062+
}
1063+
}
1064+
}
1065+
1066+
return pretaxShippingCost, nil
1067+
}
1068+
1069+
func inShippingRegions(needle pb.CountryCode, regions []pb.CountryCode) bool {
1070+
for _, region := range regions {
1071+
if needle == region || region == pb.CountryCode_ALL {
1072+
return true
1073+
}
1074+
}
1075+
return false
1076+
}
1077+
10001078
// CancelOfflineOrder - cancel order
10011079
func (n *OpenBazaarNode) CancelOfflineOrder(contract *pb.RicardianContract, records []*wallet.TransactionRecord) error {
10021080
v5Order, err := repo.ToV5Order(contract.BuyerOrder, nil)
@@ -1125,11 +1203,11 @@ func (n *OpenBazaarNode) CalculateOrderTotal(contract *pb.RicardianContract) (*b
11251203
}
11261204

11271205
// apply surcharges
1128-
totalSurcharge, err := GetTotalSurchargeAmount(nrl, item.Options)
1206+
itemSurcharge, err := GetItemSurchargeAmount(nrl, item.Options)
11291207
if err != nil {
11301208
return big.NewInt(0), err
11311209
}
1132-
itemOriginAmt = itemOriginAmt.AddBigInt(totalSurcharge)
1210+
itemOriginAmt = itemOriginAmt.AddBigInt(itemSurcharge)
11331211

11341212
// apply coupon discounts
11351213
totalDiscount, err := GetTotalCouponCodeDiscount(nrl, item.CouponCodes, itemOriginAmt)
@@ -1207,8 +1285,8 @@ func GetTotalCouponCodeDiscount(nrl *repo.Listing, couponCodes []string, itemAmo
12071285
return totalCouponCodeDiscount, nil
12081286
}
12091287

1210-
func GetTotalSurchargeAmount(nrl *repo.Listing, options []*pb.Order_Item_Option) (*big.Int, error) {
1211-
totalSurchargeAmount := big.NewInt(0)
1288+
func GetItemSurchargeAmount(nrl *repo.Listing, options []*pb.Order_Item_Option) (*big.Int, error) {
1289+
itemSurchargeAmount := big.NewInt(0)
12121290

12131291
selectedSku, err := GetSelectedSku(nrl.GetProtobuf(), options)
12141292
if err != nil {
@@ -1223,12 +1301,12 @@ func GetTotalSurchargeAmount(nrl *repo.Listing, options []*pb.Order_Item_Option)
12231301
// surcharge may be positive or negative
12241302
surcharge, ok := new(big.Int).SetString(sku.BigSurcharge, 10)
12251303
if ok && surcharge.Cmp(big.NewInt(0)) != 0 {
1226-
totalSurchargeAmount.Add(totalSurchargeAmount, surcharge)
1304+
itemSurchargeAmount.Add(itemSurchargeAmount, surcharge)
12271305
}
12281306
break
12291307
}
12301308
}
1231-
return totalSurchargeAmount, nil
1309+
return itemSurchargeAmount, nil
12321310
}
12331311
func toHundredths(f float32) *big.Float {
12341312
return new(big.Float).Mul(big.NewFloat(float64(f)), big.NewFloat(0.01))
@@ -1298,14 +1376,15 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
12981376
continue
12991377
}
13001378

1301-
// Check selected option exists
1302-
shippingOptions := make(map[string]*pb.Listing_ShippingOption)
1303-
for _, so := range rl.GetProtobuf().ShippingOptions {
1304-
shippingOptions[strings.ToLower(so.Name)] = so
1379+
// Check if physical good
1380+
if rl.GetContractType() != pb.Listing_Metadata_PHYSICAL_GOOD.String() {
1381+
continue
13051382
}
1306-
option, ok := shippingOptions[strings.ToLower(item.ShippingOption.Name)]
1307-
if !ok {
1308-
return big.NewInt(0), errors.New("shipping option not found in listing")
1383+
1384+
// Check selected option exists
1385+
option, err := getShippingOption(rl, item.ShippingOption.Name)
1386+
if err != nil {
1387+
return big.NewInt(0), err
13091388
}
13101389

13111390
if option.Type == pb.Listing_ShippingOption_LOCAL_PICKUP {
@@ -1440,6 +1519,18 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
14401519
return shippingTotal, nil
14411520
}
14421521

1522+
func getShippingOption(rl *repo.Listing, optionName string) (*pb.Listing_ShippingOption, error) {
1523+
shippingOptions := make(map[string]*pb.Listing_ShippingOption)
1524+
for _, so := range rl.GetProtobuf().ShippingOptions {
1525+
shippingOptions[strings.ToLower(so.Name)] = so
1526+
}
1527+
option, ok := shippingOptions[strings.ToLower(optionName)]
1528+
if !ok {
1529+
return nil, errors.New("shipping option not found in listing")
1530+
}
1531+
return option, nil
1532+
}
1533+
14431534
func verifySignaturesOnOrder(contract *pb.RicardianContract) error {
14441535
if err := verifyMessageSignature(
14451536
contract.BuyerOrder,

repo/checkout.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type CheckoutBreakdown struct {
1515
BasePrice string `json:"basePrice"`
1616
Coupon string `json:"coupon"`
1717
OptionSurcharge string `json:"optionSurcharge"`
18-
PriceCurrency CheckoutCurrency `json:"priceCurrency"`
1918
Quantity string `json:"quantity"`
2019
ShippingPrice string `json:"shippingPrice"`
2120
Tax string `json:"tax"`

0 commit comments

Comments
 (0)