@darkpos/pricing 1.0.159 → 1.0.161

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ const usePricing = require('../index');
2
+
3
+ const pricingService = usePricing({});
4
+
5
+ describe('invoice.distributeTip tests', () => {
6
+ test('distributes tip proportionally across invoice options', () => {
7
+ const invoicesOptions = [{ amount: 1 }, { amount: 1 }, { amount: 1 }];
8
+
9
+ const result = pricingService.invoice.distributeTip({
10
+ invoicesOptions,
11
+ tip: 1,
12
+ totalAmountToPay: 3,
13
+ });
14
+
15
+ expect(result).not.toBe(invoicesOptions);
16
+ expect(result).toEqual([
17
+ { amount: 1, tip: 0.33 },
18
+ { amount: 1, tip: 0.33 },
19
+ { amount: 1, tip: 0.34 },
20
+ ]);
21
+ expect(invoicesOptions).toEqual([{ amount: 1 }, { amount: 1 }, { amount: 1 }]);
22
+ });
23
+
24
+ test('assigns undistributed tip to the last invoice option when total amount is zero', () => {
25
+ const invoicesOptions = [{ amount: 0 }, { amount: 0 }];
26
+
27
+ const result = pricingService.invoice.distributeTip({
28
+ invoicesOptions,
29
+ tip: 5,
30
+ totalAmountToPay: 0,
31
+ });
32
+
33
+ expect(result).toEqual([
34
+ { amount: 0, tip: 0 },
35
+ { amount: 0, tip: 5 },
36
+ ]);
37
+ expect(invoicesOptions).toEqual([{ amount: 0 }, { amount: 0 }]);
38
+ });
39
+ });
@@ -59,6 +59,23 @@ describe('getMaxAmountToRefund tests', () => {
59
59
  const payment = { amount: 50 };
60
60
  expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(50);
61
61
  });
62
+
63
+ test('Subtracts tip from refundable amount', () => {
64
+ const payment = { amount: 100, tip: 15 };
65
+ expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(85);
66
+ });
67
+ });
68
+
69
+ describe('getMaxAmountToVoid tests', () => {
70
+ test('Does not subtract tip from voidable amount', () => {
71
+ const payment = { amount: 100, tip: 15 };
72
+ expect(pricingService.payment.getMaxAmountToVoid({ payment })).toBe(100);
73
+ });
74
+
75
+ test('Subtracts credit, change, and refund', () => {
76
+ const payment = { amount: 100, credit: 10, change: 5, refund: 20, tip: 15 };
77
+ expect(pricingService.payment.getMaxAmountToVoid({ payment })).toBe(65);
78
+ });
62
79
  });
63
80
 
64
81
  describe('getRefundableOrderAmount tests', () => {
@@ -0,0 +1,44 @@
1
+ module.exports = ({ utils }) =>
2
+ function distributeTip({ invoicesOptions, tip = 0, totalAmountToPay }) {
3
+ const { math } = utils;
4
+
5
+ if (
6
+ tip <= 0 ||
7
+ !Array.isArray(invoicesOptions) ||
8
+ !invoicesOptions.length
9
+ ) {
10
+ return invoicesOptions;
11
+ }
12
+
13
+ const amountToPay =
14
+ totalAmountToPay !== undefined
15
+ ? totalAmountToPay
16
+ : invoicesOptions.reduce(
17
+ (acc, option) => math.add(acc, option.amount),
18
+ 0
19
+ );
20
+
21
+ let distributed = 0;
22
+ return invoicesOptions.map((option, idx) => {
23
+ const isLast = idx === invoicesOptions.length - 1;
24
+ if (isLast) {
25
+ return {
26
+ ...option,
27
+ tip: math.sub(tip, distributed),
28
+ };
29
+ }
30
+
31
+ const share =
32
+ amountToPay > 0
33
+ ? math.toDecimalPlaces(
34
+ math.mul(tip, math.div(option.amount, amountToPay))
35
+ )
36
+ : 0;
37
+ distributed = math.add(distributed, share);
38
+
39
+ return {
40
+ ...option,
41
+ tip: share,
42
+ };
43
+ });
44
+ };
@@ -1,5 +1,6 @@
1
1
  const getStatusByItems = require('./getStatusByItems');
2
2
  const getTotalByItems = require('./getTotalByItems');
3
+ const distributeTip = require('./distributeTip');
3
4
 
4
5
  const invoiceActions = (deps = {}) => {
5
6
  const actions = {};
@@ -12,6 +13,7 @@ const invoiceActions = (deps = {}) => {
12
13
  const freezedActions = Object.freeze({
13
14
  getStatusByItems: getStatusByItems(innerDeps),
14
15
  getTotalByItems: getTotalByItems(innerDeps),
16
+ distributeTip: distributeTip(innerDeps),
15
17
  });
16
18
 
17
19
  Object.keys(freezedActions).forEach(actionName => {
@@ -11,12 +11,12 @@ module.exports = ({ settings, _ }) =>
11
11
  null
12
12
  );
13
13
 
14
- const serialLenght = serial ? serial.length : 0;
14
+ const serialLength = serial ? serial.length : 0;
15
15
 
16
16
  const minLen = typeof minSerialLength === 'number' ? minSerialLength : null;
17
17
  const maxLen = typeof maxSerialLength === 'number' ? maxSerialLength : null;
18
18
 
19
- if (minLen != null && serialLenght < minLen) return false;
20
- if (maxLen != null && serialLenght > maxLen) return false;
19
+ if (minLen != null && serialLength < minLen) return false;
20
+ if (maxLen != null && serialLength > maxLen) return false;
21
21
  return true;
22
22
  };
@@ -7,6 +7,7 @@ module.exports = ({ utils }) => {
7
7
  payment.amount,
8
8
  payment.credit || 0,
9
9
  payment.change || 0,
10
+ payment.tip || 0,
10
11
  payment.refund || 0
11
12
  );
12
13
  };
@@ -0,0 +1,14 @@
1
+ module.exports = ({ utils }) => {
2
+ const { math } = utils;
3
+
4
+ return function getMaxAmountToVoid({ payment }) {
5
+ if (!payment) return 0;
6
+
7
+ return math.sub(
8
+ payment.amount,
9
+ payment.credit || 0,
10
+ payment.change || 0,
11
+ payment.refund || 0
12
+ );
13
+ };
14
+ };
@@ -1,5 +1,6 @@
1
1
  //
2
2
  const getMaxAmountToRefund = require('./getMaxAmountToRefund');
3
+ const getMaxAmountToVoid = require('./getMaxAmountToVoid');
3
4
  const getRefundableOrderAmount = require('./getRefundableOrderAmount');
4
5
  const getMethodLabel = require('./getMethodLabel');
5
6
  const getOverPaidOrders = require('./getOverPaidOrders');
@@ -14,6 +15,7 @@ const orderActions = (deps = {}) => {
14
15
 
15
16
  const freezedActions = Object.freeze({
16
17
  getMaxAmountToRefund: getMaxAmountToRefund(innerDeps),
18
+ getMaxAmountToVoid: getMaxAmountToVoid(innerDeps),
17
19
  getRefundableOrderAmount: getRefundableOrderAmount(innerDeps),
18
20
  getMethodLabel: getMethodLabel(innerDeps),
19
21
  getOverPaidOrders: getOverPaidOrders(innerDeps),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.159",
3
+ "version": "1.0.161",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -54,5 +54,5 @@
54
54
  "supertest": "^6.2.3",
55
55
  "supervisor": "^0.12.0"
56
56
  },
57
- "gitHead": "5d00359c0a4c0bac4d5668fdd4450ff9f5223dcb"
57
+ "gitHead": "8ed8397655fe1c68c761041bc4b17179bc52aa9a"
58
58
  }