fix: rounds values when sending to brightpearl

This commit is contained in:
Sebastian Rindom
2020-09-03 10:26:35 +02:00
parent 3451d3f411
commit 60bbd120ea
4 changed files with 216 additions and 13 deletions

View File

@@ -0,0 +1,155 @@
import BrightpearlService from "../brightpearl"
import Brightpearl, { mockCreateOrder } from "../../utils/brightpearl"
jest.mock("../../utils/brightpearl")
const OrderService = {
setMetadata: () => {
return Promise.resolve()
},
}
const TotalsService = {
getTotal: () => {
return Promise.resolve(123)
},
getLineDiscounts: () => {
return Promise.resolve([])
},
getShippingTotal: () => {
return 123.9999929393293
},
rounded: (value) => {
const decimalPlaces = 4
return Number(
Math.round(parseFloat(value + "e" + decimalPlaces)) + "e-" + decimalPlaces
)
},
}
const OAuthService = {
retrieveByName: () => {
return Promise.resolve({
data: {
access_token: "12345",
},
})
},
}
const RegionService = {
retrieve: () => {
return Promise.resolve({
tax_code: "1234",
})
},
}
describe("BrightpearlService", () => {
describe("createSalesOrder", () => {
const order = {
items: [
{
title: "Test",
content: {
variant: {
sku: "TEST",
},
unit_price: 11,
},
quantity: 2,
},
],
shipping_methods: [
{
name: "standard",
price: 123.9999929393293,
},
],
payment_method: {
_id: "123",
},
tax_rate: 0.231,
currency_code: "DKK",
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 bpService = new BrightpearlService(
{
orderService: OrderService,
totalsService: TotalsService,
oauthService: OAuthService,
regionService: RegionService,
},
{ account: "test" }
)
it("successfully builds sales order", async () => {
await bpService.createSalesOrder(order)
expect(mockCreateOrder).toHaveBeenCalledWith({
currency: { code: "DKK" },
ref: "1234",
externalRef: "12355",
channelId: "1",
installedIntegrationInstanceId: undefined,
statusId: "3",
customer: {
id: "12345",
address: {
addressFullName: "Test Testson",
addressLine1: "Test",
addressLine2: "TEst",
postalCode: "1234",
countryIsoCode: "DK",
telephone: "12345678",
email: "test@example.com",
},
},
delivery: {
shippingMethodId: 0,
address: {
addressFullName: "Test Testson",
addressLine1: "Test",
addressLine2: "TEst",
postalCode: "1234",
countryIsoCode: "DK",
telephone: "12345678",
email: "test@example.com",
},
},
rows: [
{
name: "Test",
net: 22,
tax: 5.082,
quantity: 2,
taxCode: "1234",
externalRef: undefined,
nominalCode: "4000",
},
{
name: "Shipping: standard",
quantity: 1,
net: 124,
tax: 28.644,
taxCode: "1234",
nominalCode: "4040",
},
],
})
})
})
})

View File

@@ -279,9 +279,12 @@ class BrightpearlService extends BaseService {
name: `${fromRefund.reason}: ${fromRefund.note}`,
quantity: 1,
taxCode: region.tax_code,
net: fromRefund.amount / (1 + fromOrder.tax_rate),
tax:
fromRefund.amount - fromRefund.amount / (1 + fromOrder.tax_rate),
net: this.totalsService_.rounded(
fromRefund.amount / (1 + fromOrder.tax_rate)
),
tax: this.totalsService_.rounded(
fromRefund.amount - fromRefund.amount / (1 + fromOrder.tax_rate)
),
nominalCode: accountingCode,
},
],
@@ -339,8 +342,12 @@ class BrightpearlService extends BaseService {
return row.externalRef === i.item_id
})
return {
net: (parentRow.net / parentRow.quantity) * i.quantity,
tax: (parentRow.tax / parentRow.quantity) * i.quantity,
net: this.totalsService_.rounded(
(parentRow.net / parentRow.quantity) * i.quantity
),
tax: this.totalsService_.rounded(
(parentRow.tax / parentRow.quantity) * i.quantity
),
productId: parentRow.productId,
taxCode: parentRow.taxCode,
externalRef: parentRow.externalRef,
@@ -360,8 +367,12 @@ class BrightpearlService extends BaseService {
name: "Difference",
quantity: 1,
taxCode: region.tax_code,
net: difference / (1 + fromOrder.tax_rate),
tax: difference - difference / (1 + fromOrder.tax_rate),
net: this.totalsService_.rounded(
difference / (1 + fromOrder.tax_rate)
),
tax: this.totalsService_.rounded(
difference - difference / (1 + fromOrder.tax_rate)
),
nominalCode: this.options.sales_account_code || "4000",
})
}
@@ -543,8 +554,10 @@ class BrightpearlService extends BaseService {
} else {
row.name = item.title
}
row.net = item.content.unit_price * item.quantity - discount.amount
row.tax = row.net * fromOrder.tax_rate
row.net = this.totalsService_.rounded(
item.content.unit_price * item.quantity - discount.amount
)
row.tax = this.totalsService_.rounded(row.net * fromOrder.tax_rate)
row.quantity = item.quantity
row.taxCode = region.tax_code
row.externalRef = item._id
@@ -560,8 +573,8 @@ class BrightpearlService extends BaseService {
lines.push({
name: `Shipping: ${shippingMethods.map((m) => m.name).join(" + ")}`,
quantity: 1,
net: shippingTotal,
tax: shippingTotal * fromOrder.tax_rate,
net: this.totalsService_.rounded(shippingTotal),
tax: this.totalsService_.rounded(shippingTotal * fromOrder.tax_rate),
taxCode: region.tax_code,
nominalCode: this.options.shipping_account_code || "4040",
})

View File

@@ -0,0 +1,37 @@
export const mockCreateOrder = jest
.fn()
.mockReturnValue(Promise.resolve("1234"))
const mock = jest.fn().mockImplementation(() => {
return {
warehouses: {
createReservation: jest.fn().mockReturnValue(Promise.resolve()),
},
payments: {
create: jest.fn().mockReturnValue(Promise.resolve()),
},
orders: {
create: mockCreateOrder,
retrieve: jest.fn().mockReturnValue(Promise.resolve()),
},
products: {
retrieveBySKU: jest.fn().mockReturnValue(
Promise.resolve({
productId: 1234,
})
),
},
customers: {
retrieveByEmail: jest.fn().mockReturnValue(
Promise.resolve([
{
primaryEmail: "test@example.com",
contactId: "12345",
},
])
),
},
}
})
export default mock

View File

@@ -93,7 +93,6 @@ class BrightpearlClient {
this.client_.interceptors.response.use(undefined, async (error) => {
const response = error.response
console.log(response.data && response.data.errors)
if (response) {
if (
@@ -104,7 +103,6 @@ class BrightpearlClient {
try {
await onRefresh(this)
} catch (authError) {
console.log(authError)
// refreshing has failed, but report the original error, i.e. 401
return Promise.reject(error)
}