fix: account for non-division currencies in sales credit (#342)

This commit is contained in:
Sebastian Rindom
2021-08-30 12:03:46 +02:00
committed by GitHub
parent d9d84e1353
commit bd1e115696
4 changed files with 78 additions and 2 deletions

View File

@@ -27,6 +27,7 @@
"cross-env": "^7.0.2",
"eslint": "^6.8.0",
"jest": "^25.5.2",
"medusa-interfaces": "^1.1.21",
"medusa-test-utils": "^1.1.23",
"prettier": "^2.0.5"
},

View File

@@ -1,7 +1,7 @@
jest.unmock("axios")
import BrightpearlService from "../brightpearl"
import { mockCreateOrder } from "../../utils/brightpearl"
import { mockCreateOrder, mockCreateCredit } from "../../utils/brightpearl"
import MockAdapter from "axios-mock-adapter"
jest.mock("../../utils/brightpearl")
@@ -47,6 +47,47 @@ const order = {
email: "test@example.com",
}
const krwOrder = {
region: {
tax_code: "1234",
},
items: [
{
title: "Test",
variant: {
sku: "TEST",
},
unit_price: 1100,
quantity: 2,
},
],
shipping_total: 12399,
shipping_methods: [
{
name: "standard",
price: 12399,
},
],
payment_method: {
id: "123",
},
tax_rate: 0,
currency_code: "KRW",
display_id: "1234",
id: "12355",
discounts: [],
shipping_address: {
first_name: "Test",
last_name: "Testson",
address_1: "Test",
address_2: "TEst",
postal_code: "1234",
country_code: "DK",
phone: "12345678",
},
email: "test@example.com",
}
const roundingOrder = {
region: {
tax_code: "1234",
@@ -398,4 +439,32 @@ describe("BrightpearlService", () => {
})
})
})
describe("bpnum_", () => {
const bpService = new BrightpearlService(
{
orderService: OrderService,
totalsService: TotalsService,
oauthService: OAuthService,
regionService: RegionService,
},
{ account: "test" }
)
it("sales credit diff. calc - KRW", async () => {
jest.clearAllMocks()
const total = 100000
const refund_amount = 100000
const difference = bpService.bpnum_(refund_amount, "krw") - total
expect(difference).toEqual(0)
})
it("sales credit diff. calc - DKK", async () => {
jest.clearAllMocks()
const total = 100000
const refund_amount = 100000
const difference = bpService.bpnum_(refund_amount, "dkk") - total
expect(difference).toEqual(-99000)
})
})
})

View File

@@ -414,7 +414,8 @@ class BrightpearlService extends BaseService {
return acc + next.net + next.tax
}, 0)
const difference = (fromReturn.refund_amount / 100 - total) * 100
const difference =
this.bpnum_(fromReturn.refund_amount, fromOrder.currency_code) - total
if (difference) {
order.rows.push({
name: "Difference",

View File

@@ -3,6 +3,10 @@ export const mockCreateOrder = jest
.fn()
.mockReturnValue(Promise.resolve("1234"))
export const mockCreateCredit = jest
.fn()
.mockReturnValue(Promise.resolve("1234"))
const mock = jest.fn().mockImplementation(function (options) {
this.tokenStore_ = options.token_store
this.token_ = options.access_token
@@ -65,6 +69,7 @@ const mock = jest.fn().mockImplementation(function (options) {
}
this.orders = {
create: mockCreateOrder,
createCredit: mockCreateCredit,
retrieve: jest.fn().mockReturnValue(Promise.resolve()),
}
this.products = {