fix: don't divide zero decimal currencies
This commit is contained in:
1 parent
2b282bd742
commit
cfab2d408a
5 files changed
+95
-24
No files matched your search
@@ -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
|
||||||
@@ -5,3 +5,5 @@ export { default as MedusaError } from "./errors"
|
|||||||
export { default as getConfigFile } from "./get-config-file"
|
export { default as getConfigFile } from "./get-config-file"
|
||||||
export { default as createRequireFromPath } from "./create-require-from-path"
|
export { default as createRequireFromPath } from "./create-require-from-path"
|
||||||
export { default as compareObjectsByProp } from "./compare-objects"
|
export { default as compareObjectsByProp } from "./compare-objects"
|
||||||
|
export { default as zeroDecimalCurrencies } from "./zero-decimal-currencies"
|
||||||
|
export { default as humanizeAmount } from "./humanize-amount"
|
||||||
@@ -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
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError, humanizeAmount } from "medusa-core-utils"
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
import Brightpearl from "../utils/brightpearl"
|
import Brightpearl from "../utils/brightpearl"
|
||||||
|
|
||||||
@@ -290,10 +290,12 @@ class BrightpearlService extends BaseService {
|
|||||||
taxCode: region.tax_code,
|
taxCode: region.tax_code,
|
||||||
net: this.bpnum_(
|
net: this.bpnum_(
|
||||||
fromRefund.amount,
|
fromRefund.amount,
|
||||||
|
fromOrder.currency_code,
|
||||||
10000 / (100 + fromOrder.tax_rate)
|
10000 / (100 + fromOrder.tax_rate)
|
||||||
),
|
),
|
||||||
tax: this.bpnum_(
|
tax: this.bpnum_(
|
||||||
fromRefund.amount * (1 - 100 / (100 + fromOrder.tax_rate))
|
fromRefund.amount * (1 - 100 / (100 + fromOrder.tax_rate)),
|
||||||
|
fromOrder.currency_code
|
||||||
),
|
),
|
||||||
nominalCode: accountingCode,
|
nominalCode: accountingCode,
|
||||||
},
|
},
|
||||||
@@ -311,7 +313,7 @@ class BrightpearlService extends BaseService {
|
|||||||
paymentMethodCode: this.options.payment_method_code || "1220",
|
paymentMethodCode: this.options.payment_method_code || "1220",
|
||||||
orderId: creditId,
|
orderId: creditId,
|
||||||
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
|
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
|
||||||
amountPaid: this.bpnum_(fromRefund.amount),
|
amountPaid: this.bpnum_(fromRefund.amount, fromOrder.currency_code),
|
||||||
paymentDate: new Date(),
|
paymentDate: new Date(),
|
||||||
paymentType,
|
paymentType,
|
||||||
}
|
}
|
||||||
@@ -380,8 +382,15 @@ class BrightpearlService extends BaseService {
|
|||||||
name: "Difference",
|
name: "Difference",
|
||||||
quantity: 1,
|
quantity: 1,
|
||||||
taxCode: region.tax_code,
|
taxCode: region.tax_code,
|
||||||
net: this.bpnum_(difference, 10000 / (100 + fromOrder.tax_rate)),
|
net: this.bpnum_(
|
||||||
tax: this.bpnum_(difference * (1 - 100 / (100 + fromOrder.tax_rate))),
|
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",
|
nominalCode: this.options.sales_account_code || "4000",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -397,7 +406,10 @@ class BrightpearlService extends BaseService {
|
|||||||
paymentMethodCode: this.options.payment_method_code || "1220",
|
paymentMethodCode: this.options.payment_method_code || "1220",
|
||||||
orderId: creditId,
|
orderId: creditId,
|
||||||
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
|
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
|
||||||
amountPaid: this.bpnum_(fromReturn.refund_amount),
|
amountPaid: this.bpnum_(
|
||||||
|
fromReturn.refund_amount,
|
||||||
|
fromOrder.currencyCode
|
||||||
|
),
|
||||||
paymentDate: new Date(),
|
paymentDate: new Date(),
|
||||||
paymentType,
|
paymentType,
|
||||||
}
|
}
|
||||||
@@ -640,10 +652,12 @@ class BrightpearlService extends BaseService {
|
|||||||
name: `#${fromOrder.display_id}: Claim ${fromClaim.id}`,
|
name: `#${fromOrder.display_id}: Claim ${fromClaim.id}`,
|
||||||
net: this.bpnum_(
|
net: this.bpnum_(
|
||||||
fromClaim.refund_amount,
|
fromClaim.refund_amount,
|
||||||
|
fromOrder.currency_code,
|
||||||
10000 / (100 + fromOrder.tax_rate)
|
10000 / (100 + fromOrder.tax_rate)
|
||||||
),
|
),
|
||||||
tax: this.bpnum_(
|
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,
|
taxCode: region.tax_code,
|
||||||
nominalCode: this.options.sales_account_code || `4000`,
|
nominalCode: this.options.sales_account_code || `4000`,
|
||||||
@@ -663,7 +677,10 @@ class BrightpearlService extends BaseService {
|
|||||||
paymentMethodCode: this.options.payment_method_code || "1220",
|
paymentMethodCode: this.options.payment_method_code || "1220",
|
||||||
orderId: creditId,
|
orderId: creditId,
|
||||||
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
|
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
|
||||||
amountPaid: this.bpnum_(fromClaim.refund_amount),
|
amountPaid: this.bpnum_(
|
||||||
|
fromClaim.refund_amount,
|
||||||
|
fromOrder.currency_code
|
||||||
|
),
|
||||||
paymentDate: new Date(),
|
paymentDate: new Date(),
|
||||||
paymentType,
|
paymentType,
|
||||||
}
|
}
|
||||||
@@ -775,7 +792,7 @@ class BrightpearlService extends BaseService {
|
|||||||
orderId: soId,
|
orderId: soId,
|
||||||
paymentDate: new Date(),
|
paymentDate: new Date(),
|
||||||
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
|
currencyIsoCode: fromOrder.currency_code.toUpperCase(),
|
||||||
amountPaid: this.bpnum_(fromOrder.total),
|
amountPaid: this.bpnum_(fromOrder.total, fromOrder.currency_code),
|
||||||
paymentType,
|
paymentType,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -811,9 +828,13 @@ class BrightpearlService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (config.include_price) {
|
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_(
|
row.tax = this.bpnum_(
|
||||||
item.unit_price * item.quantity - ld.amount,
|
item.unit_price * item.quantity - ld.amount,
|
||||||
|
fromOrder.currency_code,
|
||||||
fromOrder.tax_rate
|
fromOrder.tax_rate
|
||||||
)
|
)
|
||||||
} else if (config.is_claim) {
|
} else if (config.is_claim) {
|
||||||
@@ -821,7 +842,11 @@ class BrightpearlService extends BaseService {
|
|||||||
bpProduct.productId,
|
bpProduct.productId,
|
||||||
this.options.cost_price_list || `1`
|
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
|
row.quantity = item.quantity
|
||||||
@@ -847,8 +872,12 @@ class BrightpearlService extends BaseService {
|
|||||||
if (gcTotal) {
|
if (gcTotal) {
|
||||||
lines.push({
|
lines.push({
|
||||||
name: `Gift Card`,
|
name: `Gift Card`,
|
||||||
net: this.bpnum_(-1 * gcTotal),
|
net: this.bpnum_(-1 * gcTotal, fromOrder.currency_code),
|
||||||
tax: this.bpnum_(-1 * gcTotal, fromOrder.tax_rate),
|
tax: this.bpnum_(
|
||||||
|
-1 * gcTotal,
|
||||||
|
fromOrder.currency_code,
|
||||||
|
fromOrder.tax_rate
|
||||||
|
),
|
||||||
quantity: 1,
|
quantity: 1,
|
||||||
taxCode: region.tax_code,
|
taxCode: region.tax_code,
|
||||||
nominalCode: this.options.gift_card_account_code || "4000",
|
nominalCode: this.options.gift_card_account_code || "4000",
|
||||||
@@ -863,8 +892,12 @@ class BrightpearlService extends BaseService {
|
|||||||
lines.push({
|
lines.push({
|
||||||
name: `Shipping: ${shippingMethods.map((m) => m.name).join(" + ")}`,
|
name: `Shipping: ${shippingMethods.map((m) => m.name).join(" + ")}`,
|
||||||
quantity: 1,
|
quantity: 1,
|
||||||
net: this.bpnum_(shippingTotal),
|
net: this.bpnum_(shippingTotal, fromOrder.currency_code),
|
||||||
tax: this.bpnum_(shippingTotal, fromOrder.tax_rate),
|
tax: this.bpnum_(
|
||||||
|
shippingTotal,
|
||||||
|
fromOrder.currency_code,
|
||||||
|
fromOrder.tax_rate
|
||||||
|
),
|
||||||
taxCode: region.tax_code,
|
taxCode: region.tax_code,
|
||||||
nominalCode: this.options.shipping_account_code || "4040",
|
nominalCode: this.options.shipping_account_code || "4040",
|
||||||
})
|
})
|
||||||
@@ -1103,8 +1136,8 @@ class BrightpearlService extends BaseService {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
bpnum_(number, taxRate = 100) {
|
bpnum_(number, currency, taxRate = 100) {
|
||||||
const bpNumber = number / 100
|
const bpNumber = humanizeAmount(number, currency)
|
||||||
return this.bpround_(bpNumber * (taxRate / 100))
|
return this.bpround_(bpNumber * (taxRate / 100))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import Analytics from "analytics-node"
|
import Analytics from "analytics-node"
|
||||||
import axios from "axios"
|
import axios from "axios"
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
|
import { humanizeAmount } from "medusa-core-utils"
|
||||||
|
|
||||||
class SegmentService extends BaseService {
|
class SegmentService extends BaseService {
|
||||||
/**
|
/**
|
||||||
@@ -58,11 +59,13 @@ class SegmentService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async buildOrder(order) {
|
async buildOrder(order) {
|
||||||
const subtotal = order.subtotal / 100
|
const curr = order.currency_code
|
||||||
const total = order.total / 100
|
|
||||||
const tax = order.tax_total / 100
|
const subtotal = humanizeAmount(order.subtotal, curr)
|
||||||
const discount = order.discount_total / 100
|
const total = humanizeAmount(order.total, curr)
|
||||||
const shipping = order.shipping_total / 100
|
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
|
const revenue = total - tax
|
||||||
|
|
||||||
let coupon
|
let coupon
|
||||||
@@ -119,7 +122,7 @@ class SegmentService extends BaseService {
|
|||||||
|
|
||||||
const revenue = await this.getReportingValue(
|
const revenue = await this.getReportingValue(
|
||||||
order.currency_code,
|
order.currency_code,
|
||||||
lineTotal / 100
|
humanizeAmount(lineTotal, curr)
|
||||||
)
|
)
|
||||||
|
|
||||||
let sku = ""
|
let sku = ""
|
||||||
@@ -141,7 +144,7 @@ class SegmentService extends BaseService {
|
|||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
variant,
|
variant,
|
||||||
price: lineTotal / 100 / item.quantity,
|
price: humanizeAmount(lineTotal, curr) / item.quantity,
|
||||||
reporting_revenue: revenue,
|
reporting_revenue: revenue,
|
||||||
product_id: item.variant.product_id,
|
product_id: item.variant.product_id,
|
||||||
category: product.collection?.title,
|
category: product.collection?.title,
|
||||||
|
|||||||
Reference in new issue
Block a user