@darkpos/pricing 1.0.147 → 1.0.149

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.
@@ -197,6 +197,133 @@ describe('applyRefundToInvoices', () => {
197
197
  });
198
198
  });
199
199
 
200
+ describe('applyRefundToInvoices — 5-item order with 3 items priced to 0', () => {
201
+ const ORDER_5_ID = 'order-5items';
202
+
203
+ // Items as they appear in the payment invoice (stale totals from payment time)
204
+ const shortsInvoiceItem = {
205
+ orderId: ORDER_5_ID,
206
+ orderItemId: 'shorts',
207
+ total: 6.3,
208
+ totalPaid: 6.3,
209
+ amount: 6.3,
210
+ status: { paid: { value: true } },
211
+ };
212
+ const jacket1InvoiceItem = {
213
+ orderId: ORDER_5_ID,
214
+ orderItemId: 'jacket-1',
215
+ total: 8.93,
216
+ totalPaid: 8.93,
217
+ amount: 8.93,
218
+ status: { paid: { value: true } },
219
+ };
220
+ const longJacketInvoiceItem = {
221
+ orderId: ORDER_5_ID,
222
+ orderItemId: 'long-jacket',
223
+ total: 12.6,
224
+ totalPaid: 12.6,
225
+ amount: 12.6,
226
+ status: { paid: { value: true } },
227
+ };
228
+ const jacket2InvoiceItem = {
229
+ orderId: ORDER_5_ID,
230
+ orderItemId: 'jacket-2',
231
+ total: 8.93,
232
+ totalPaid: 8.93,
233
+ amount: 8.93,
234
+ status: { paid: { value: true } },
235
+ };
236
+ const coatInvoiceItem = {
237
+ orderId: ORDER_5_ID,
238
+ orderItemId: 'coat',
239
+ total: 13.65,
240
+ totalPaid: 3.24,
241
+ amount: 3.24,
242
+ status: { paid: { value: false } },
243
+ };
244
+
245
+ // Live order state after Jacket×2 and Long Jacket prices changed to 0
246
+ const liveOrder5 = {
247
+ _id: ORDER_5_ID,
248
+ items: [
249
+ {
250
+ _id: 'shorts',
251
+ total: 6.3,
252
+ totalPaid: 6.3,
253
+ status: { paid: { value: true } },
254
+ },
255
+ {
256
+ _id: 'jacket-1',
257
+ total: 0,
258
+ totalPaid: 8.93,
259
+ status: { paid: { value: true } },
260
+ },
261
+ {
262
+ _id: 'long-jacket',
263
+ total: 0,
264
+ totalPaid: 12.6,
265
+ status: { paid: { value: true } },
266
+ },
267
+ {
268
+ _id: 'jacket-2',
269
+ total: 0,
270
+ totalPaid: 8.93,
271
+ status: { paid: { value: true } },
272
+ },
273
+ {
274
+ _id: 'coat',
275
+ total: 13.65,
276
+ totalPaid: 3.24,
277
+ status: { paid: { value: false } },
278
+ },
279
+ ],
280
+ };
281
+
282
+ const ordersById5 = { [ORDER_5_ID]: liveOrder5 };
283
+
284
+ const paymentInvoices5 = [
285
+ {
286
+ invoiceId: 'inv-5items',
287
+ amount: 40,
288
+ paymentOrderItems: [
289
+ shortsInvoiceItem,
290
+ jacket1InvoiceItem,
291
+ longJacketInvoiceItem,
292
+ jacket2InvoiceItem,
293
+ coatInvoiceItem,
294
+ ],
295
+ },
296
+ ];
297
+
298
+ test('correctly refunds 20.05 — clears the 3 zero-priced items and credits the remaining balance to coat', () => {
299
+ const refundInvoices = pricing.invoice.applyRefundToInvoices(
300
+ paymentInvoices5,
301
+ ordersById5,
302
+ 20.05
303
+ );
304
+
305
+ expect(refundInvoices).toHaveLength(1);
306
+ expect(refundInvoices[0].amount).toBe(-20.05);
307
+ expect(refundInvoices[0].paymentOrderItems).toHaveLength(4); // shorts has 0 excess, skipped
308
+
309
+ const byItemId = Object.fromEntries(
310
+ refundInvoices[0].paymentOrderItems.map(item => [item.orderItemId, item])
311
+ );
312
+
313
+ expect(byItemId['jacket-1'].amount).toBe(-8.93);
314
+ expect(byItemId['jacket-1'].status.paid.value).toBe(true);
315
+
316
+ expect(byItemId['long-jacket'].amount).toBe(-12.6);
317
+ expect(byItemId['long-jacket'].status.paid.value).toBe(true);
318
+
319
+ expect(byItemId['jacket-2'].amount).toBe(-8.93);
320
+ expect(byItemId['jacket-2'].status.paid.value).toBe(true);
321
+
322
+ expect(byItemId.coat.amount).toBe(10.41);
323
+ expect(byItemId.coat.status.paid.value).toBe(true);
324
+ });
325
+ });
326
+
200
327
  describe('buildItemRefund', () => {
201
328
  const overpaidItemState = pricing.invoice.resolveItemState(
202
329
  overpaidInvoiceItem,
@@ -505,4 +505,24 @@ describe('pickEndDate function', () => {
505
505
 
506
506
  expect(result).toBe('2025-05-13T21:00:00Z');
507
507
  });
508
+
509
+ test('pickEndDate - applies addDays when fromDate is provided', () => {
510
+ const schedule = {
511
+ addDays: 3,
512
+ readyHour: { hour: 17, minute: 0 },
513
+ skipDays: [],
514
+ cutHour: { hour: 16, minute: 0 },
515
+ cutDay: 1,
516
+ };
517
+
518
+ const pricingService = usePricing(getDefaultSettings([schedule]));
519
+
520
+ const result = pricingService.store.pickEndDate(
521
+ undefined,
522
+ undefined,
523
+ '2026-03-01T09:00:00'
524
+ );
525
+
526
+ expect(result).toBe('2026-03-04T22:00:00Z');
527
+ });
508
528
  });
@@ -7,10 +7,10 @@ module.exports = ({ utils }) => {
7
7
  each => each._id === paymentOrderItem.orderItemId
8
8
  );
9
9
 
10
- const itemTotal =
11
- (orderItem && orderItem.total) || paymentOrderItem.total || 0;
12
- const itemTotalPaid =
13
- (orderItem && orderItem.totalPaid) || paymentOrderItem.totalPaid || 0;
10
+ const itemTotal = orderItem ? orderItem.total : paymentOrderItem.total || 0;
11
+ const itemTotalPaid = orderItem
12
+ ? orderItem.totalPaid
13
+ : paymentOrderItem.totalPaid || 0;
14
14
  const currentStatus =
15
15
  (orderItem && orderItem.status) || paymentOrderItem.status || {};
16
16
 
@@ -1,8 +1,13 @@
1
1
  module.exports = ({ utils }) =>
2
2
  function getOverPaidAmount({ order }) {
3
3
  if (!order) return 0;
4
- const total = Number(order.total || 0);
5
- const totalPaid = Number(order.totalPaid || 0);
6
4
 
7
- return utils.math.sub(totalPaid, total);
5
+ return (order.items || []).reduce((net, item) => {
6
+ const itemTotalPaid = Number(item.totalPaid || 0);
7
+ if (itemTotalPaid <= 0) return net;
8
+ return utils.math.add(
9
+ net,
10
+ utils.math.sub(itemTotalPaid, Number(item.total || 0))
11
+ );
12
+ }, 0);
8
13
  };
@@ -18,11 +18,7 @@ module.exports = ({ moment, actions }) =>
18
18
 
19
19
  let addDays = 0;
20
20
 
21
- if (
22
- !fromDate &&
23
- typeof addDaysParam === 'number' &&
24
- addDaysParam <= MAX_ADD_DAYS
25
- ) {
21
+ if (typeof addDaysParam === 'number' && addDaysParam <= MAX_ADD_DAYS) {
26
22
  addDays = addDaysParam;
27
23
  }
28
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.147",
3
+ "version": "1.0.149",
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": "c983de0f3b8d4978e2fb0b68551ba347dbdc4d55"
57
+ "gitHead": "0d01074b06953dfc95097eb12cbace0ad7175ee9"
58
58
  }