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
@@ -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))
}
}