@darkpos/pricing 1.0.86 → 1.0.88

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.
@@ -3891,4 +3891,78 @@ describe('Order actions', () => {
3891
3891
  discount: -1.09,
3892
3892
  });
3893
3893
  });
3894
+
3895
+ test('Mark paid status true if total is 0 and autoMarkAsPaid: true', () => {
3896
+ const orderItem = {
3897
+ price: 0,
3898
+ quantity: 1,
3899
+ };
3900
+ const order = { items: [orderItem] };
3901
+ const pricing = usePricing({
3902
+ store: { _settings: { order: { autoMarkAsPaid: true } } },
3903
+ });
3904
+ const newOrder = pricing.order.calculate(order);
3905
+
3906
+ expect(newOrder).toHaveProperty('total', 0);
3907
+ expect(newOrder.status).toMatchObject({ fullyPaid: true });
3908
+ expect(newOrder.items[0].total).toBe(0);
3909
+ expect(newOrder.items[0].status).toMatchObject({
3910
+ paid: {
3911
+ value: true,
3912
+ },
3913
+ });
3914
+
3915
+ const newOrder2 = pricingService.order.calculate(order);
3916
+
3917
+ expect(newOrder2).toHaveProperty('total', 0);
3918
+ expect(newOrder2.status).toBe(undefined);
3919
+ expect(newOrder2.items[0].total).toBe(0);
3920
+ expect(newOrder2.items[0].status).toBe(undefined);
3921
+ });
3922
+
3923
+ test('Unset paid status if total is > 0 and autoMarkAsPaid: true', () => {
3924
+ const orderItem = {
3925
+ price: 25,
3926
+ quantity: 1,
3927
+ status: {
3928
+ paid: { value: true, date: new Date() },
3929
+ },
3930
+ };
3931
+ const pricing = usePricing({
3932
+ store: { _settings: { order: { autoMarkAsPaid: true } } },
3933
+ });
3934
+ const order = { items: [orderItem], status: { fullyPaid: true } };
3935
+ const newOrder = pricing.order.calculate(order);
3936
+
3937
+ expect(newOrder).toHaveProperty('total', 25);
3938
+ expect(newOrder.status).toMatchObject({ fullyPaid: false });
3939
+ expect(newOrder.items[0].total).toBe(25);
3940
+ expect(newOrder.items[0].status).toMatchObject({
3941
+ paid: undefined,
3942
+ });
3943
+
3944
+ const newOrder2 = pricingService.order.calculate(order);
3945
+
3946
+ expect(newOrder2).toHaveProperty('total', 25);
3947
+ expect(newOrder2.status).toMatchObject({ fullyPaid: true });
3948
+ expect(newOrder2.items[0].total).toBe(25);
3949
+ expect(newOrder2.items[0].status).toMatchObject({ paid: { value: true } });
3950
+ });
3951
+
3952
+ test('Dont update status if not needed', () => {
3953
+ const orderItem = {
3954
+ price: 25,
3955
+ quantity: 1,
3956
+ };
3957
+ const pricing = usePricing({
3958
+ store: { _settings: { order: { autoMarkAsPaid: true } } },
3959
+ });
3960
+ const order = { items: [orderItem] };
3961
+ const newOrder = pricing.order.calculate(order);
3962
+
3963
+ expect(newOrder).toHaveProperty('total', 25);
3964
+ expect(newOrder.status).toMatchObject({});
3965
+ expect(newOrder.items[0].total).toBe(25);
3966
+ expect(newOrder.items[0].status).toMatchObject({});
3967
+ });
3894
3968
  });
@@ -189,6 +189,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
189
189
  modifiers: [...modifiersToNotCompute, ...modifiers],
190
190
  subTotals,
191
191
  total,
192
+ status: actions.getUpdatedStatus({ status: item.status, total }),
192
193
  };
193
194
  };
194
195
 
@@ -0,0 +1,20 @@
1
+ module.exports = ({ actions, settings }) =>
2
+ function getUpdatedStatus({ status, total }) {
3
+ if (!settings || !settings.order || !settings.order.autoMarkAsPaid)
4
+ return status;
5
+
6
+ const localStatus = status || {};
7
+
8
+ if (actions.isFullyPaid({ item: { status: localStatus } }) && total !== 0) {
9
+ return { ...localStatus, paid: undefined };
10
+ }
11
+
12
+ if (
13
+ !actions.isFullyPaid({ item: { status: localStatus } }) &&
14
+ total === 0
15
+ ) {
16
+ return { ...localStatus, paid: { value: true, date: new Date() } };
17
+ }
18
+
19
+ return localStatus;
20
+ };
package/lib/item/index.js CHANGED
@@ -62,6 +62,7 @@ const getSubtotal = require('./getSubtotal');
62
62
  const isSomeTagsMatch = require('./isSomeTagsMatch');
63
63
  const getTotals = require('./getTotals');
64
64
  const patchItem = require('./patchItem');
65
+ const getUpdatedStatus = require('./getUpdatedStatus');
65
66
 
66
67
  const itemActions = (deps = {}) => {
67
68
  const actions = {};
@@ -137,6 +138,7 @@ const itemActions = (deps = {}) => {
137
138
  isSomeTagsMatch: isSomeTagsMatch(innerDeps),
138
139
  getTotals: getTotals(innerDeps),
139
140
  patchItem: patchItem(innerDeps),
141
+ getUpdatedStatus: getUpdatedStatus(innerDeps),
140
142
  });
141
143
 
142
144
  Object.keys(freezedActions).forEach(actionName => {
@@ -2,5 +2,5 @@ module.exports = () =>
2
2
  function isFullyPaid({ item }) {
3
3
  if (!item) return false;
4
4
 
5
- return item.status && item.status.paid.value;
5
+ return item.status && item.status.paid && item.status.paid.value;
6
6
  };
File without changes
@@ -150,9 +150,15 @@ module.exports = ({
150
150
  if (difference > 0) addToItemTotal(difference);
151
151
  }
152
152
 
153
+ const { subTotals, subTotal, total } =
154
+ itemActions.getItemsTotals(calculatedItems);
155
+
153
156
  return {
154
157
  ...order,
155
- ...itemActions.getItemsTotals(calculatedItems),
158
+ subTotals,
159
+ subTotal,
160
+ total,
156
161
  items: calculatedItems,
162
+ status: actions.getUpdatedStatus({ status: order.status, total }),
157
163
  };
158
164
  };
@@ -1,24 +1,37 @@
1
- module.exports = ({ actions }) =>
2
- function createSubOrder({ parentOrder, items }) {
1
+ module.exports = ({ actions, settings, _ }) => {
2
+ const orderSettings = _.get(settings, 'order');
3
+
4
+ return function createSubOrder({ parentOrder, items }) {
5
+ // exclude props from order
6
+ const {
7
+ _id,
8
+ displayId,
9
+ parentId,
10
+ isParent,
11
+ subTotal,
12
+ discount,
13
+ fee,
14
+ tax,
15
+ total,
16
+ items: parentItems,
17
+ modifiers,
18
+ status,
19
+ ...rest
20
+ } = parentOrder;
21
+
3
22
  const subOrder = actions.create({
4
- // ...order,
5
- displayId: null,
6
- discount: 0,
7
- tax: 0,
8
- totalPaid: 0,
9
- total: 0,
23
+ ...rest,
24
+ parentId: orderSettings.allowSuborder ? _id : null,
10
25
  status: {
11
26
  order: 'open',
12
- delivery: parentOrder.status.delivery,
13
- detailed: parentOrder.status.detailed,
27
+ delivery: status.delivery || false,
28
+ detailed: status.detailed || false,
14
29
  fullyPaid: false,
15
30
  fullyPicked: false,
16
31
  },
17
- parentId: parentOrder._id,
18
32
  items,
19
- modifiers: [],
20
- notes: parentOrder.notes || [],
21
33
  });
22
34
 
23
35
  return subOrder;
24
36
  };
37
+ };
@@ -0,0 +1,23 @@
1
+ module.exports = ({ actions, settings }) =>
2
+ function getUpdatedStatus({ status, total }) {
3
+ if (!settings || !settings.order || !settings.order.autoMarkAsPaid)
4
+ return status;
5
+
6
+ const localStatus = status || {};
7
+
8
+ if (
9
+ actions.isFullyPaid({ order: { status: localStatus } }) &&
10
+ total !== 0
11
+ ) {
12
+ return { ...localStatus, fullyPaid: false };
13
+ }
14
+
15
+ if (
16
+ !actions.isFullyPaid({ order: { status: localStatus } }) &&
17
+ total === 0
18
+ ) {
19
+ return { ...localStatus, fullyPaid: true };
20
+ }
21
+
22
+ return localStatus;
23
+ };
@@ -91,6 +91,8 @@ const resetItem = require('./resetItem');
91
91
  const splitItems = require('./splitItems');
92
92
  const getNewItems = require('./getNewItems');
93
93
  const mapSubOrders = require('./mapSubOrders');
94
+ const isFullyPaid = require('./isFullyPaid');
95
+ const getUpdatedStatus = require('./getUpdatedStatus');
94
96
 
95
97
  const orderActions = (deps = {}) => {
96
98
  const actions = {};
@@ -193,6 +195,8 @@ const orderActions = (deps = {}) => {
193
195
  splitItems: splitItems(innerDeps),
194
196
  getNewItems: getNewItems(innerDeps),
195
197
  mapSubOrders: mapSubOrders(innerDeps),
198
+ isFullyPaid: isFullyPaid(innerDeps),
199
+ getUpdatedStatus: getUpdatedStatus(innerDeps),
196
200
  });
197
201
 
198
202
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,6 @@
1
+ module.exports = () =>
2
+ function isFullyPaid({ order }) {
3
+ if (!order) return false;
4
+
5
+ return order.status && order.status.fullyPaid;
6
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.86",
3
+ "version": "1.0.88",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -51,5 +51,5 @@
51
51
  "supertest": "^6.2.3",
52
52
  "supervisor": "^0.12.0"
53
53
  },
54
- "gitHead": "feab9f47beb2312e3b82bae4eb2ec63f18d64be3"
54
+ "gitHead": "3090bddbb5b0651296b29acf011c2bfe8db6d42d"
55
55
  }