fix: better rounding of totals

This commit is contained in:
Sebastian Rindom committed 2020-08-28 12:47:53 +02:00
1 parent 9650f7d1c1
commit 3093cba610
3 files changed
+25 -19

No files matched your search

@@ -18,39 +18,38 @@ var inventorySync = /*#__PURE__*/function () {
case 0: case 0:
brightpearlService = container.resolve("brightpearlService"); brightpearlService = container.resolve("brightpearlService");
eventBus = container.resolve("eventBusService"); eventBus = container.resolve("eventBusService");
console.log("hi inventory"); _context.prev = 2;
_context.prev = 3; _context.next = 5;
_context.next = 6;
return brightpearlService.getClient(); return brightpearlService.getClient();
case 6: case 5:
client = _context.sent; client = _context.sent;
pattern = "43 4,10,14,20 * * *"; // nice for tests "*/10 * * * * *" pattern = "43 4,10,14,20 * * *"; // nice for tests "*/10 * * * * *"
eventBus.createCronJob("inventory-sync", {}, pattern, brightpearlService.syncInventory()); eventBus.createCronJob("inventory-sync", {}, pattern, brightpearlService.syncInventory());
_context.next = 16; _context.next = 15;
break; break;
case 11: case 10:
_context.prev = 11; _context.prev = 10;
_context.t0 = _context["catch"](3); _context.t0 = _context["catch"](2);
if (!(_context.t0.name === "not_allowed")) { if (!(_context.t0.name === "not_allowed")) {
_context.next = 15; _context.next = 14;
break; break;
} }
return _context.abrupt("return"); return _context.abrupt("return");
case 15: case 14:
throw _context.t0; throw _context.t0;
case 16: case 15:
case "end": case "end":
return _context.stop(); return _context.stop();
} }
} }
}, _callee, null, [[3, 11]]); }, _callee, null, [[2, 10]]);
})); }));
return function inventorySync(_x) { return function inventorySync(_x) {
@@ -524,7 +524,7 @@ class BrightpearlService extends BaseService {
) )
let lineDiscounts = [] let lineDiscounts = []
if (discount) { if (discount) {
lineDiscounts = this.discountService_.getLineDiscounts( lineDiscounts = this.totalsService_.getLineDiscounts(
fromOrder, fromOrder,
discount discount
) )
+13 -6
View File
@@ -50,7 +50,7 @@ class TotalsService extends BaseService {
item.content.unit_price * item.content.quantity * item.quantity item.content.unit_price * item.content.quantity * item.quantity
} }
}) })
return subtotal return this.rounded(subtotal)
} }
/** /**
@@ -77,12 +77,12 @@ class TotalsService extends BaseService {
const discountTotal = await this.getDiscountTotal(object) const discountTotal = await this.getDiscountTotal(object)
const region = await this.regionService_.retrieve(object.region_id) const region = await this.regionService_.retrieve(object.region_id)
const { tax_rate } = region const { tax_rate } = region
return (subtotal - discountTotal + shippingTotal) * tax_rate return this.rounded((subtotal - discountTotal + shippingTotal) * tax_rate)
} }
getRefundedTotal(object) { getRefundedTotal(object) {
const total = object.refunds.reduce((acc, next) => acc + next.amount, 0) const total = object.refunds.reduce((acc, next) => acc + next.amount, 0)
return total return this.rounded(total)
} }
getLineItemRefund(order, lineItem) { getLineItemRefund(order, lineItem) {
@@ -105,7 +105,7 @@ class TotalsService extends BaseService {
const discountAmount = const discountAmount =
(discountedLine.amount / discountedLine.item.quantity) * lineItem.quantity (discountedLine.amount / discountedLine.item.quantity) * lineItem.quantity
return ( return this.rounded(
(lineItem.content.unit_price * lineItem.quantity - discountAmount) * (lineItem.content.unit_price * lineItem.quantity - discountAmount) *
(1 + taxRate) (1 + taxRate)
) )
@@ -121,7 +121,7 @@ class TotalsService extends BaseService {
*/ */
getRefundTotal(order, lineItems) { getRefundTotal(order, lineItems) {
const refunds = lineItems.map(i => this.getLineItemRefund(order, i)) const refunds = lineItems.map(i => this.getLineItemRefund(order, i))
return refunds.reduce((acc, next) => acc + next, 0) return this.rounded(refunds.reduce((acc, next) => acc + next, 0))
} }
/** /**
@@ -292,7 +292,14 @@ class TotalsService extends BaseService {
toReturn = _.sumBy(itemFixedDiscounts, d => d.amount) toReturn = _.sumBy(itemFixedDiscounts, d => d.amount)
} }
return Math.min(subtotal, toReturn) return this.rounded(Math.min(subtotal, toReturn))
}
rounded(value) {
const decimalPlaces = 4
return Number(
Math.round(parseFloat(value + "e" + decimalPlaces)) + "e-" + decimalPlaces
)
} }
} }