Skip to content
This repository was archived by the owner on Sep 8, 2021. It is now read-only.

Commit a2e685e

Browse files
committed
Keep track of couponDiscount and memberDiscount on item prices
1 parent c55794e commit a2e685e

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gocommerce-js",
3-
"version": "3.8.0",
3+
"version": "3.8.1",
44
"description": "GoCommerce API client for JavaScript",
55
"main": "lib/index.js",
66
"files": [

src/calculator.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,17 @@ export function calculatePrices(settings, claims, country, currency, coupon, ite
9999
itemPrice.discount = calculateDiscount(
100100
itemPrice.subtotal, itemPrice.taxes, coupon.percentage, fixedAmount(coupon.fixed, currency), includeTaxes
101101
);
102+
itemPrice.couponDiscount = itemPrice.discount;
102103
}
103104
if (settings && settings.member_discounts) {
104105
settings.member_discounts.forEach((discount) => {
105106
if (couponValidFor(claims, discount, item)) {
106-
itemPrice.discount = itemPrice.discount || 0;
107-
itemPrice.discount += calculateDiscount(
107+
const memberDiscount = calculateDiscount(
108108
itemPrice.subtotal, itemPrice.taxes, discount.percentage, fixedAmount(discount.fixed, currency), includeTaxes
109109
);
110+
itemPrice.discount = itemPrice.discount || 0;
111+
itemPrice.discount += memberDiscount;
112+
itemPrice.memberDiscount = memberDiscount;
110113
}
111114
});
112115
}

src/calculator.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ test('fixed vat when prices include taxes', () => {
2929
expect(price.total).toBe(100);
3030
});
3131

32+
test('fixed vat when prices include taxes for a real example', () => {
33+
const price = calculatePrices({
34+
taxes: [{percentage: 7, product_types: ["book"], countries: ["Netherlands"]}],
35+
prices_include_taxes: true
36+
}, null, "Netherlands", "EUR", null, [{price: {cents: 2900}, type: "book"}]);
37+
expect(price.subtotal).toBe(2710);
38+
expect(price.taxes).toBe(190);
39+
expect(price.discount).toBe(0);
40+
expect(price.total).toBe(2900);
41+
});
42+
3243
test('country based VAT', () => {
3344
const settings = {taxes: [{percentage: 21, product_types: ["test"], countries: ["USA"]}]};
3445
const price = calculatePrices(settings, null, "USA", "USD", null, [{price: {cents: 100}, type: "test"}]);

0 commit comments

Comments
 (0)