fix: don't divide zero decimal currencies

This commit is contained in:
Sebastian Rindom
2021-03-22 10:31:22 +01:00
parent 2b282bd742
commit cfab2d408a
5 changed files with 95 additions and 24 deletions

View File

@@ -0,0 +1,13 @@
import zeroDecimalCurrencies from "./zero-decimal-currencies"
const humanizeAmount = (amount, currency) => {
let divisor = 100
if (zeroDecimalCurrencies.includes(currency.toLowerCase())) {
divisor = 1
}
return amount / divisor
}
export default humanizeAmount

View File

@@ -5,3 +5,5 @@ export { default as MedusaError } from "./errors"
export { default as getConfigFile } from "./get-config-file"
export { default as createRequireFromPath } from "./create-require-from-path"
export { default as compareObjectsByProp } from "./compare-objects"
export { default as zeroDecimalCurrencies } from "./zero-decimal-currencies"
export { default as humanizeAmount } from "./humanize-amount"

View File

@@ -0,0 +1,20 @@
const zeroDecimalCurrencies = [
"bif",
"clp",
"djf",
"gnf",
"jpy",
"kmf",
"krw",
"mga",
"pyg",
"rwf",
"ugx",
"vnd",
"vuv",
"xaf",
"xof",
"xpf",
]
export default zeroDecimalCurrencies

View File

@@ -1,4 +1,4 @@
import { MedusaError } from "medusa-core-utils"
import { MedusaError, humanizeAmount } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces"
import Brightpearl from "../utils/brightpearl"
@@ -290,10 +290,12 @@ class BrightpearlService extends BaseService {
taxCode: region.tax_code,
net: this.bpnum_(
fromRefund.amount,
fromOrder.currency_code,
10000 / (100 + fromOrder.tax_rate)
),
tax: this.bpnum_(
fromRefund.amount * (1 - 100 / (100 + fromOrder.tax_rate))
fromRefund.amount * (1 - 100 / (100 + fromOrder.tax_rate)),
fromOrder.currency_code
),
nominalCode: accountingCode,
},
@@ -311,7 +313,7 @@ class BrightpearlService extends BaseService {
paymentMethodCode: this.options.payment_method_code || "1220",
orderId: creditId,
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
amountPaid: this.bpnum_(fromRefund.amount),
amountPaid: this.bpnum_(fromRefund.amount, fromOrder.currency_code),
paymentDate: new Date(),
paymentType,
}
@@ -380,8 +382,15 @@ class BrightpearlService extends BaseService {
name: "Difference",
quantity: 1,
taxCode: region.tax_code,
net: this.bpnum_(difference, 10000 / (100 + fromOrder.tax_rate)),
tax: this.bpnum_(difference * (1 - 100 / (100 + fromOrder.tax_rate))),
net: this.bpnum_(
difference,
fromOrder.currency_code,
10000 / (100 + fromOrder.tax_rate)
),
tax: this.bpnum_(
difference * (1 - 100 / (100 + fromOrder.tax_rate)),
fromOrder.currency_code
),
nominalCode: this.options.sales_account_code || "4000",
})
}
@@ -397,7 +406,10 @@ class BrightpearlService extends BaseService {
paymentMethodCode: this.options.payment_method_code || "1220",
orderId: creditId,
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
amountPaid: this.bpnum_(fromReturn.refund_amount),
amountPaid: this.bpnum_(
fromReturn.refund_amount,
fromOrder.currencyCode
),
paymentDate: new Date(),
paymentType,
}
@@ -640,10 +652,12 @@ class BrightpearlService extends BaseService {
name: `#${fromOrder.display_id}: Claim ${fromClaim.id}`,
net: this.bpnum_(
fromClaim.refund_amount,
fromOrder.currency_code,
10000 / (100 + fromOrder.tax_rate)
),
tax: this.bpnum_(
fromClaim.refund_amount * (1 - 100 / (100 + fromOrder.tax_rate))
fromClaim.refund_amount * (1 - 100 / (100 + fromOrder.tax_rate)),
fromOrder.currency_code
),
taxCode: region.tax_code,
nominalCode: this.options.sales_account_code || `4000`,
@@ -663,7 +677,10 @@ class BrightpearlService extends BaseService {
paymentMethodCode: this.options.payment_method_code || "1220",
orderId: creditId,
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
amountPaid: this.bpnum_(fromClaim.refund_amount),
amountPaid: this.bpnum_(
fromClaim.refund_amount,
fromOrder.currency_code
),
paymentDate: new Date(),
paymentType,
}
@@ -775,7 +792,7 @@ class BrightpearlService extends BaseService {
orderId: soId,
paymentDate: new Date(),
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
amountPaid: this.bpnum_(fromOrder.total),
amountPaid: this.bpnum_(fromOrder.total, fromOrder.currency_code),
paymentType,
}
@@ -811,9 +828,13 @@ class BrightpearlService extends BaseService {
}
if (config.include_price) {
row.net = this.bpnum_(item.unit_price * item.quantity - ld.amount)
row.net = this.bpnum_(
item.unit_price * item.quantity - ld.amount,
fromOrder.currency_code
)
row.tax = this.bpnum_(
item.unit_price * item.quantity - ld.amount,
fromOrder.currency_code,
fromOrder.tax_rate
)
} else if (config.is_claim) {
@@ -821,7 +842,11 @@ class BrightpearlService extends BaseService {
bpProduct.productId,
this.options.cost_price_list || `1`
)
row.tax = this.bpnum_(row.net * 100, fromOrder.tax_rate)
row.tax = this.bpnum_(
row.net * 100,
fromOrder.currency_code,
fromOrder.tax_rate
)
}
row.quantity = item.quantity
@@ -847,8 +872,12 @@ class BrightpearlService extends BaseService {
if (gcTotal) {
lines.push({
name: `Gift Card`,
net: this.bpnum_(-1 * gcTotal),
tax: this.bpnum_(-1 * gcTotal, fromOrder.tax_rate),
net: this.bpnum_(-1 * gcTotal, fromOrder.currency_code),
tax: this.bpnum_(
-1 * gcTotal,
fromOrder.currency_code,
fromOrder.tax_rate
),
quantity: 1,
taxCode: region.tax_code,
nominalCode: this.options.gift_card_account_code || "4000",
@@ -863,8 +892,12 @@ class BrightpearlService extends BaseService {
lines.push({
name: `Shipping: ${shippingMethods.map((m) => m.name).join(" + ")}`,
quantity: 1,
net: this.bpnum_(shippingTotal),
tax: this.bpnum_(shippingTotal, fromOrder.tax_rate),
net: this.bpnum_(shippingTotal, fromOrder.currency_code),
tax: this.bpnum_(
shippingTotal,
fromOrder.currency_code,
fromOrder.tax_rate
),
taxCode: region.tax_code,
nominalCode: this.options.shipping_account_code || "4040",
})
@@ -1103,8 +1136,8 @@ class BrightpearlService extends BaseService {
)
}
bpnum_(number, taxRate = 100) {
const bpNumber = number / 100
bpnum_(number, currency, taxRate = 100) {
const bpNumber = humanizeAmount(number, currency)
return this.bpround_(bpNumber * (taxRate / 100))
}
}

View File

@@ -1,6 +1,7 @@
import Analytics from "analytics-node"
import axios from "axios"
import { BaseService } from "medusa-interfaces"
import { humanizeAmount } from "medusa-core-utils"
class SegmentService extends BaseService {
/**
@@ -58,11 +59,13 @@ class SegmentService extends BaseService {
}
async buildOrder(order) {
const subtotal = order.subtotal / 100
const total = order.total / 100
const tax = order.tax_total / 100
const discount = order.discount_total / 100
const shipping = order.shipping_total / 100
const curr = order.currency_code
const subtotal = humanizeAmount(order.subtotal, curr)
const total = humanizeAmount(order.total, curr)
const tax = humanizeAmount(order.tax_total, curr)
const discount = humanizeAmount(order.discount_total, curr)
const shipping = humanizeAmount(order.shipping_total, curr)
const revenue = total - tax
let coupon
@@ -119,7 +122,7 @@ class SegmentService extends BaseService {
const revenue = await this.getReportingValue(
order.currency_code,
lineTotal / 100
humanizeAmount(lineTotal, curr)
)
let sku = ""
@@ -141,7 +144,7 @@ class SegmentService extends BaseService {
return {
name,
variant,
price: lineTotal / 100 / item.quantity,
price: humanizeAmount(lineTotal, curr) / item.quantity,
reporting_revenue: revenue,
product_id: item.variant.product_id,
category: product.collection?.title,