@managesome/eposmatic-toolkit 0.2.445 → 0.2.448

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.
@@ -1,5 +1,31 @@
1
+ import { SORT_TYPE_ENUM } from '../../constants';
1
2
  import { IWarehouseSaleOrderRFQ, IWarehouseSaleOrderRFQCategory, IWarehouseSaleOrderRFQItem, IWarehouseSaleOrderRFQIteration } from '../../schemas';
3
+ import { IWarehouseGeneralSaleOrderRfqItem, IWarehouseSaleOrderRfqCartItemsObj } from '../types';
2
4
  export declare function getWarehouseSaleOrderRfqIterationsWithPrice(iterations?: IWarehouseSaleOrderRFQIteration[] | undefined, writeable?: boolean): IWarehouseSaleOrderRFQIteration[];
3
5
  export declare function getWarehouseSaleOrderRfqItemsWithPrice(items: IWarehouseSaleOrderRFQItem[] | undefined, convertToPrice: (price: number) => number): IWarehouseSaleOrderRFQItem[];
4
6
  export declare function getWarehouseSaleOrderRfqCategoriesWithPrice(categories: IWarehouseSaleOrderRFQCategory[] | undefined, convertToPrice: (price: number) => number): IWarehouseSaleOrderRFQCategory[];
5
7
  export declare function getWarehouseSaleOrderRfqWithPrice<T extends Partial<IWarehouseSaleOrderRFQ> | IWarehouseSaleOrderRFQ | IWarehouseSaleOrderRFQIteration>(order: T, writeable?: boolean): T;
8
+ export declare function calculateCartWarehouseSaleOrderRfqOrderInfoAndSyncData<T extends IWarehouseSaleOrderRFQ | Partial<IWarehouseSaleOrderRFQ>>(data: T, calculateItemMetadata?: boolean): T;
9
+ export declare function syncWarehouseSaleOrderRfqData(order: IWarehouseSaleOrderRFQ): IWarehouseSaleOrderRFQ;
10
+ export declare function skipItemMetadataFromWarehouseSaleOrderRfqCategories(categories: IWarehouseSaleOrderRFQ['categories'], previousCategories?: IWarehouseSaleOrderRFQ['categories']): IWarehouseSaleOrderRFQ['categories'];
11
+ export declare function getWarehouseSaleOrderRfqWithAllFields<T extends IWarehouseSaleOrderRFQ | Partial<IWarehouseSaleOrderRFQ>>(order: T, skipItemMetadata?: boolean): T;
12
+ export interface IGetSaleOrderRfqCartItemsObjPayload {
13
+ currentIterationCategories: IWarehouseSaleOrderRFQCategory[] | undefined;
14
+ previousIterationCategories: IWarehouseSaleOrderRFQCategory[] | undefined;
15
+ mappedItems: Record<string, IWarehouseGeneralSaleOrderRfqItem>;
16
+ shouldSortItemsViaId?: boolean;
17
+ isAnEdit?: boolean;
18
+ sortType?: SORT_TYPE_ENUM;
19
+ }
20
+ export declare function getWarehouseSaleOrderRfqCartItemsObj({ currentIterationCategories, previousIterationCategories, mappedItems, shouldSortItemsViaId, isAnEdit, sortType, }: IGetSaleOrderRfqCartItemsObjPayload): IWarehouseSaleOrderRfqCartItemsObj;
21
+ interface IGetVoidItemsWithReasonSaleOrderRfq {
22
+ currentCategories: IWarehouseSaleOrderRFQCategory[];
23
+ iterationsFromFrontend: IWarehouseSaleOrderRFQIteration[] | undefined;
24
+ previousCategories: IWarehouseSaleOrderRFQCategory[];
25
+ }
26
+ export declare function getVoidItemsWithReasonWarehouseSaleOrderRfq({ currentCategories, iterationsFromFrontend, previousCategories, }: IGetVoidItemsWithReasonSaleOrderRfq): {
27
+ newIterationCategories: IWarehouseSaleOrderRFQCategory[];
28
+ voidOrderItems: IWarehouseSaleOrderRFQItem[];
29
+ hasNewItems: boolean;
30
+ };
31
+ export {};
@@ -1,10 +1,26 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.getWarehouseSaleOrderRfqIterationsWithPrice = getWarehouseSaleOrderRfqIterationsWithPrice;
4
7
  exports.getWarehouseSaleOrderRfqItemsWithPrice = getWarehouseSaleOrderRfqItemsWithPrice;
5
8
  exports.getWarehouseSaleOrderRfqCategoriesWithPrice = getWarehouseSaleOrderRfqCategoriesWithPrice;
6
9
  exports.getWarehouseSaleOrderRfqWithPrice = getWarehouseSaleOrderRfqWithPrice;
10
+ exports.calculateCartWarehouseSaleOrderRfqOrderInfoAndSyncData = calculateCartWarehouseSaleOrderRfqOrderInfoAndSyncData;
11
+ exports.syncWarehouseSaleOrderRfqData = syncWarehouseSaleOrderRfqData;
12
+ exports.skipItemMetadataFromWarehouseSaleOrderRfqCategories = skipItemMetadataFromWarehouseSaleOrderRfqCategories;
13
+ exports.getWarehouseSaleOrderRfqWithAllFields = getWarehouseSaleOrderRfqWithAllFields;
14
+ exports.getWarehouseSaleOrderRfqCartItemsObj = getWarehouseSaleOrderRfqCartItemsObj;
15
+ exports.getVoidItemsWithReasonWarehouseSaleOrderRfq = getVoidItemsWithReasonWarehouseSaleOrderRfq;
16
+ const bson_objectid_1 = __importDefault(require("bson-objectid"));
17
+ const lodash_1 = require("lodash");
18
+ const constants_1 = require("../../constants");
7
19
  const utils_1 = require("../utils");
20
+ /* =================================================================
21
+ * Sale-order RFQ common helpers — price conversion, field defaults, cart UI.
22
+ * Mirrors common-warehouse-purchase-order-rfq-actions.ts for sale RFQ types.
23
+ * ================================================================= */
8
24
  // ── Iterations ──────────────────────────────────────────────────
9
25
  function getWarehouseSaleOrderRfqIterationsWithPrice(iterations, writeable) {
10
26
  if (!iterations)
@@ -58,3 +74,550 @@ function getWarehouseSaleOrderRfqWithPrice(order, writeable) {
58
74
  };
59
75
  return payload;
60
76
  }
77
+ // ── Cart calculation ──
78
+ function calculateCartWarehouseSaleOrderRfqOrderInfoAndSyncData(data, calculateItemMetadata) {
79
+ const isConfirmed = data.order_status === constants_1.WAREHOUSE_SALE_ORDER_RFQ_STATUS_ENUM.CONFIRMED;
80
+ let subtotal = 0;
81
+ const categories = [];
82
+ const length = data.categories?.length ?? 0;
83
+ for (let c = 0; c < length; c++) {
84
+ const category = data.categories[c];
85
+ const newItems = [];
86
+ const itemsLength = category.items.length;
87
+ for (let i = 0; i < itemsLength; i++) {
88
+ const item = category.items[i];
89
+ const itemSubtotal = (0, utils_1.parseNum)(item.price * item.quantity);
90
+ const newItem = {
91
+ ...item,
92
+ is_ready: item.is_ready || isConfirmed,
93
+ subtotal: itemSubtotal,
94
+ _discount_amount: 0,
95
+ _gross_amount: 0,
96
+ _gst_amount: 0,
97
+ _net_amount: 0,
98
+ };
99
+ delete newItem.void_reason;
100
+ newItems.push(newItem);
101
+ subtotal += itemSubtotal;
102
+ }
103
+ categories.push({ ...category, items: newItems });
104
+ }
105
+ let grand_total = subtotal;
106
+ let gst_amount = 0;
107
+ const gst = data.gst ?? 0;
108
+ if (data.is_gst_inclusive) {
109
+ gst_amount = (0, utils_1.parseFloatCeil)((grand_total * gst) / (100 + gst));
110
+ }
111
+ else {
112
+ gst_amount = (0, utils_1.getPercentageAmount)(grand_total, gst);
113
+ grand_total += gst_amount;
114
+ }
115
+ grand_total = (0, utils_1.parseNum)(grand_total);
116
+ const amount_earned = (0, utils_1.parseNum)(grand_total);
117
+ if (calculateItemMetadata) {
118
+ let leftGrossAmount = subtotal;
119
+ let leftNetAmount = subtotal;
120
+ let leftDiscountAmount = 0;
121
+ let leftGstAmount = gst_amount;
122
+ categories.forEach((category, categoryIndex) => {
123
+ const isLastCategory = categoryIndex === categories.length - 1;
124
+ category.items.forEach((item, itemIndex) => {
125
+ const isLastItem = isLastCategory && itemIndex === category.items.length - 1;
126
+ let grossAmount = item.subtotal;
127
+ let netAmount = item.subtotal;
128
+ let itemTotalDiscountAmount = 0;
129
+ let itemGst = 0;
130
+ if (gst_amount > 0) {
131
+ itemGst = (item.subtotal / subtotal) * gst_amount;
132
+ }
133
+ grossAmount = (0, utils_1.parseFloatCeil)(grossAmount);
134
+ const netAmountParsed = (0, utils_1.parseFloatCeil)(netAmount);
135
+ const itemTotalDiscountAmountParsed = (0, utils_1.parseFloatCeil)(itemTotalDiscountAmount);
136
+ itemGst = (0, utils_1.parseFloatCeil)(itemGst);
137
+ leftGrossAmount -= grossAmount;
138
+ leftNetAmount -= netAmountParsed;
139
+ leftDiscountAmount -= itemTotalDiscountAmountParsed;
140
+ leftGstAmount -= itemGst;
141
+ if (isLastItem) {
142
+ grossAmount += leftGrossAmount;
143
+ netAmount += leftNetAmount;
144
+ itemTotalDiscountAmount += leftDiscountAmount;
145
+ itemGst += leftGstAmount;
146
+ }
147
+ item._gross_amount = Math.max(grossAmount, 0);
148
+ item._net_amount = Math.max(netAmount, 0);
149
+ item._discount_amount = Math.max(itemTotalDiscountAmount, 0);
150
+ item._gst_amount = Math.max(itemGst, 0);
151
+ });
152
+ });
153
+ }
154
+ return {
155
+ ...data,
156
+ categories,
157
+ amount_earned,
158
+ gst_amount,
159
+ price: (0, utils_1.parseNum)(subtotal),
160
+ grand_total,
161
+ };
162
+ }
163
+ // ── Sync ──
164
+ function syncWarehouseSaleOrderRfqData(order) {
165
+ const updatedOrder = {
166
+ ...order,
167
+ iterations: [],
168
+ };
169
+ const newOrder = getWarehouseSaleOrderRfqWithPrice(calculateCartWarehouseSaleOrderRfqOrderInfoAndSyncData(getWarehouseSaleOrderRfqWithPrice(updatedOrder), true), true);
170
+ newOrder.iterations = order.iterations;
171
+ return newOrder;
172
+ }
173
+ // ── Skip item metadata ──
174
+ function skipItemMetadataFromWarehouseSaleOrderRfqCategories(categories, previousCategories) {
175
+ return categories.map((category) => {
176
+ const previousCategory = previousCategories?.find((prevCategory) => prevCategory.category_id === category.category_id);
177
+ return {
178
+ ...category,
179
+ items: category.items.map((item) => {
180
+ const previousItem = previousCategory?.items.find((prevItem) => prevItem._id === item._id);
181
+ const void_reason = previousItem?.void_reason ?? item.void_reason;
182
+ const newItem = {
183
+ ...item,
184
+ void_reason,
185
+ _discount_amount: 0,
186
+ _gst_amount: 0,
187
+ _gross_amount: 0,
188
+ _net_amount: 0,
189
+ };
190
+ return newItem;
191
+ }),
192
+ };
193
+ });
194
+ }
195
+ // ── Optional field helpers ──
196
+ function getCommonOptionalBoolFieldsSaleOrderRfq(order, partial) {
197
+ const defaultValue = partial ? undefined : false;
198
+ return {
199
+ is_gst_inclusive: order['is_gst_inclusive'] || defaultValue,
200
+ is_reopened: order['is_reopened'] || defaultValue,
201
+ };
202
+ }
203
+ function getCommonOptionalNumFieldsSaleOrderRfq(order, partial) {
204
+ const defaultValue = partial ? undefined : 0;
205
+ return {
206
+ gst: order['gst'] || defaultValue,
207
+ gst_amount: order['gst_amount'] || defaultValue,
208
+ };
209
+ }
210
+ function getWarehouseSaleOrderRfqWithAllFields(order, skipItemMetadata) {
211
+ return {
212
+ ...order,
213
+ ...getCommonOptionalBoolFieldsSaleOrderRfq(order, false),
214
+ ...getCommonOptionalNumFieldsSaleOrderRfq(order, false),
215
+ ...(skipItemMetadata && order.categories != null
216
+ ? {
217
+ categories: skipItemMetadataFromWarehouseSaleOrderRfqCategories(order.categories),
218
+ }
219
+ : {}),
220
+ delete_reason: order['delete_reason'] ?? null,
221
+ cancel_reason: order['cancel_reason'] ?? null,
222
+ };
223
+ }
224
+ const getSaleRfqItemComparableData = (item) => ({
225
+ item_id: item.item_id,
226
+ name: item.name,
227
+ price: item.price,
228
+ special_instructions: item.special_instructions,
229
+ _id: item._id,
230
+ quantity: item.quantity,
231
+ });
232
+ function getWarehouseSaleOrderRfqCartItemsObj({ currentIterationCategories, previousIterationCategories, mappedItems, shouldSortItemsViaId = true, isAnEdit, sortType, }) {
233
+ const newMappedItems = { ...mappedItems };
234
+ if (Object.keys(newMappedItems).length === 0) {
235
+ previousIterationCategories?.forEach((category) => {
236
+ category.items.forEach((item) => {
237
+ if (newMappedItems[item.item_id] == null) {
238
+ newMappedItems[item.item_id] = {
239
+ _id: item.item_id,
240
+ name: item.name,
241
+ category_id: category.category_id,
242
+ category_name: category.name,
243
+ category_image_url: '',
244
+ image_url: '',
245
+ description: '',
246
+ };
247
+ }
248
+ });
249
+ });
250
+ currentIterationCategories?.forEach((category) => {
251
+ category.items.forEach((item) => {
252
+ if (newMappedItems[item.item_id] == null) {
253
+ newMappedItems[item.item_id] = {
254
+ _id: item.item_id,
255
+ name: item.name,
256
+ category_id: category.category_id,
257
+ category_name: category.name,
258
+ category_image_url: '',
259
+ image_url: '',
260
+ description: '',
261
+ };
262
+ }
263
+ });
264
+ });
265
+ }
266
+ // =====================><=====================
267
+ const getCartItems = (items) => {
268
+ const orderItems = items.map((item) => {
269
+ const selectedItem = newMappedItems[item.item_id] ?? {};
270
+ return {
271
+ orderItem: item,
272
+ image_url: selectedItem.image_url ?? '',
273
+ description: selectedItem.description ?? '',
274
+ category_name: selectedItem.category_name ?? '',
275
+ category_id: selectedItem.category_id ?? '',
276
+ };
277
+ });
278
+ if (shouldSortItemsViaId) {
279
+ if (sortType === constants_1.SORT_TYPE_ENUM.DESC) {
280
+ orderItems.sort((a, b) => (0, bson_objectid_1.default)(b.orderItem._id).getTimestamp().getTime() - (0, bson_objectid_1.default)(a.orderItem._id).getTimestamp().getTime());
281
+ }
282
+ else {
283
+ orderItems.sort((a, b) => (0, bson_objectid_1.default)(a.orderItem._id).getTimestamp().getTime() - (0, bson_objectid_1.default)(b.orderItem._id).getTimestamp().getTime());
284
+ }
285
+ }
286
+ return orderItems;
287
+ };
288
+ // =====================><=====================
289
+ /*
290
+ If the order has iterations in it, then we get the last iteration.
291
+ We would compare the changes in the last iteration with the current iteration
292
+ and show the changes in the cart.
293
+ */
294
+ let totalItemsInCart = 0;
295
+ const currentItems = [];
296
+ if (currentIterationCategories != null) {
297
+ for (let i = 0; i < currentIterationCategories.length; i++) {
298
+ const category = currentIterationCategories[i];
299
+ for (let j = 0; j < category.items.length; j++) {
300
+ const item = { ...category.items[j] };
301
+ currentItems.push(item);
302
+ totalItemsInCart += item.quantity;
303
+ }
304
+ }
305
+ }
306
+ let unChangedItems = currentItems;
307
+ const editedItems = [];
308
+ let removedItems = [];
309
+ let newItems = [];
310
+ let disabledReason = '';
311
+ // If previous iteration exists, then it's an update to the order.
312
+ // In that case we would keep the previous iteration as it is and
313
+ // compare the current iteration with the previous iteration.
314
+ // If the current iteration is different from the previous iteration in any way
315
+ // then we would update the current iteration with only those items that are different
316
+ const isAnUpdate = isAnEdit ?? (previousIterationCategories != null && previousIterationCategories.length > 0);
317
+ if (isAnUpdate) {
318
+ // we need to compare the current iteration with the previous iteration
319
+ const currentItemsMap = currentItems.reduce((acc, currentItem) => {
320
+ acc[currentItem._id] = currentItem;
321
+ return acc;
322
+ }, {});
323
+ const prevItems = [];
324
+ const prevItemsMap = {};
325
+ const prevCategories = previousIterationCategories ?? [];
326
+ for (let i = 0; i < prevCategories.length; i++) {
327
+ const category = prevCategories[i];
328
+ for (let j = 0; j < category.items.length; j++) {
329
+ const prevItem = category.items[j];
330
+ let item = prevItem;
331
+ const currentItem = currentItemsMap[prevItem._id];
332
+ if (currentItem != null && currentItem.is_ready != null) {
333
+ item = {
334
+ ...item,
335
+ is_ready: currentItem.is_ready,
336
+ };
337
+ }
338
+ prevItems.push(item);
339
+ prevItemsMap[item._id] = item;
340
+ }
341
+ }
342
+ // NOTE: We will always set the items from currentItemsMap because we are dealing with current iteration
343
+ unChangedItems = [];
344
+ let totalTempRemovedItems = 0;
345
+ for (let i = 0; i < prevItems.length; i++) {
346
+ const prevItem = {
347
+ ...prevItems[i],
348
+ };
349
+ const currentItem = {
350
+ ...currentItemsMap[prevItem._id],
351
+ };
352
+ // Step 1: Get the items that are not present in the current iteration
353
+ if (currentItemsMap[prevItem._id] == null) {
354
+ removedItems.push(prevItem);
355
+ }
356
+ // Step 2: Get the items that are same in the current iteration
357
+ else if ((0, lodash_1.isEqual)(getSaleRfqItemComparableData(currentItem), getSaleRfqItemComparableData(prevItem))) {
358
+ unChangedItems.push(prevItem);
359
+ }
360
+ // Step 3: Get the items that are present in the previous iteration but
361
+ // This is turn for edit items
362
+ // but we change logic for display purpose
363
+ else {
364
+ const currQty = currentItem.quantity;
365
+ const prevQty = prevItem.quantity;
366
+ if (currQty !== prevQty) {
367
+ const subtotalForOne = currentItem.subtotal / currentItem.quantity;
368
+ // currQty = 8
369
+ // prevQty = 3
370
+ // diff = 5
371
+ const diff = currQty - prevQty;
372
+ const changeQty = Math.abs(diff);
373
+ const itm = {
374
+ ...currentItem,
375
+ quantity: changeQty,
376
+ subtotal: (0, utils_1.parseNum)(changeQty * subtotalForOne),
377
+ };
378
+ if (diff > 0) {
379
+ // We need to show it as a new item
380
+ newItems.push(itm);
381
+ const unchangedQty = currQty - changeQty;
382
+ const tempUnchangedItm = {
383
+ ...currentItem,
384
+ quantity: unchangedQty,
385
+ subtotal: (0, utils_1.parseNum)(unchangedQty * subtotalForOne),
386
+ };
387
+ unChangedItems.push(tempUnchangedItm);
388
+ }
389
+ else {
390
+ itm.void_reason = prevItem.void_reason ?? currentItem.void_reason;
391
+ // We need to show it as a removed item
392
+ removedItems.push(itm);
393
+ const unchangedQty = prevQty - changeQty;
394
+ const tempUnchangedItm = {
395
+ ...currentItem,
396
+ quantity: unchangedQty,
397
+ subtotal: (0, utils_1.parseNum)(unchangedQty * subtotalForOne),
398
+ };
399
+ unChangedItems.push(tempUnchangedItm);
400
+ totalTempRemovedItems++;
401
+ }
402
+ }
403
+ else {
404
+ const curr = {
405
+ _id: currentItem._id,
406
+ item_id: currentItem.item_id,
407
+ name: currentItem.name,
408
+ special_instructions: currentItem.special_instructions,
409
+ };
410
+ const prev = {
411
+ _id: prevItem._id,
412
+ item_id: prevItem.item_id,
413
+ name: prevItem.name,
414
+ special_instructions: prevItem.special_instructions,
415
+ };
416
+ const isBothEqual = (0, lodash_1.isEqual)(curr, prev);
417
+ if (isBothEqual) {
418
+ unChangedItems.push(currentItem);
419
+ }
420
+ else {
421
+ // For edited item, we don't need to have tempUnchangedItemIds
422
+ // because it's the turn for edited item
423
+ editedItems.push(currentItem);
424
+ }
425
+ }
426
+ }
427
+ }
428
+ // Step 4: Get the items that are present in the current iteration
429
+ // but not present in the previous iteration
430
+ for (let i = 0; i < currentItems.length; i++) {
431
+ const item = currentItems[i];
432
+ if (prevItemsMap[item._id] == null) {
433
+ newItems.push(item);
434
+ }
435
+ }
436
+ // Lastly, we need to check if any same item has been removed and added
437
+ // If so, the we need to remove the item from removedItems and newItems, and add it to unChangedItems
438
+ if (removedItems.length > 0 && newItems.length > 0) {
439
+ const newItemsMap = newItems.reduce((acc, item) => {
440
+ acc[item.item_id] = [...(acc[item.item_id] ?? []), item];
441
+ return acc;
442
+ }, {});
443
+ const revertedRemovedItems = [];
444
+ const revertedNewItems = [];
445
+ const additionalNewItems = [];
446
+ for (let i = 0; i < removedItems.length; i++) {
447
+ const removedItem = removedItems[i];
448
+ // Need to check if the removed item is present in the newItems
449
+ const newItemsForItem = newItemsMap[removedItem.item_id];
450
+ if (newItemsForItem == null)
451
+ continue;
452
+ if (newItemsForItem.length > 0) {
453
+ const newItemsForItemLength = newItemsForItem.length;
454
+ for (let j = 0; j < newItemsForItemLength; j++) {
455
+ const newItem = newItemsForItem[j];
456
+ const newI = {
457
+ // _id: newItem._id,
458
+ item_id: newItem.item_id,
459
+ name: newItem.name,
460
+ price: newItem.price,
461
+ // subtotal: number;
462
+ // quantity: newItem.quantity,
463
+ special_instructions: newItem.special_instructions,
464
+ };
465
+ const removedI = {
466
+ // _id: removedItem._id,
467
+ item_id: removedItem.item_id,
468
+ name: removedItem.name,
469
+ price: removedItem.price,
470
+ // subtotal: number;
471
+ // quantity: removedItem.quantity,
472
+ special_instructions: removedItem.special_instructions,
473
+ };
474
+ if ((0, lodash_1.isEqual)(newI, removedI)) {
475
+ if (newItem.quantity === removedItem.quantity) {
476
+ newItemsMap[removedItem.item_id] = newItemsForItem.filter((item) => item._id !== newItem._id);
477
+ if (newItemsMap[removedItem.item_id].length === 0) {
478
+ delete newItemsMap[removedItem.item_id];
479
+ }
480
+ revertedNewItems.push(newItem);
481
+ revertedRemovedItems.push(removedItem);
482
+ // We will break the loop when ever we push to revertedRemovedItems
483
+ break;
484
+ }
485
+ else {
486
+ if (removedItem.quantity > newItem.quantity) {
487
+ newItemsMap[removedItem.item_id] = newItemsForItem.filter((item) => item._id !== newItem._id);
488
+ if (newItemsMap[removedItem.item_id].length === 0) {
489
+ delete newItemsMap[removedItem.item_id];
490
+ }
491
+ revertedNewItems.push(newItem);
492
+ const subtotalForSingle = removedItem.subtotal / removedItem.quantity;
493
+ // We will only minus the quantity from removedItem
494
+ const newQuantity = removedItem.quantity - newItem.quantity;
495
+ const newSubtotal = (0, utils_1.parseNum)(newQuantity * subtotalForSingle);
496
+ removedItem.quantity = newQuantity;
497
+ removedItem.subtotal = newSubtotal;
498
+ }
499
+ else {
500
+ revertedRemovedItems.push(removedItem);
501
+ const nQuantity = newItem.quantity;
502
+ const subtotalForSingle = newItem.subtotal / nQuantity;
503
+ const newQuantity = nQuantity - removedItem.quantity;
504
+ const newSubtotal = (0, utils_1.parseNum)(newQuantity * subtotalForSingle);
505
+ newItem.quantity = newQuantity;
506
+ newItem.subtotal = newSubtotal;
507
+ // =====================><=====================
508
+ const remainingQuantity = nQuantity - newQuantity;
509
+ const adItem = {
510
+ ...newItem,
511
+ quantity: remainingQuantity,
512
+ subtotal: (0, utils_1.parseNum)(remainingQuantity * subtotalForSingle),
513
+ };
514
+ additionalNewItems.push(adItem);
515
+ // We will break the loop when ever we push to revertedRemovedItems
516
+ break;
517
+ }
518
+ }
519
+ }
520
+ }
521
+ }
522
+ else {
523
+ delete newItemsMap[removedItem.item_id];
524
+ }
525
+ }
526
+ for (let i = 0; i < revertedRemovedItems.length; i++) {
527
+ const rItem = revertedRemovedItems[i];
528
+ removedItems = removedItems.filter((item) => item._id !== rItem._id);
529
+ }
530
+ for (let i = 0; i < revertedNewItems.length; i++) {
531
+ const nItem = revertedNewItems[i];
532
+ newItems = newItems.filter((item) => item._id !== nItem._id);
533
+ unChangedItems.push(nItem);
534
+ }
535
+ for (let i = 0; i < additionalNewItems.length; i++) {
536
+ const aItem = additionalNewItems[i];
537
+ unChangedItems.push(aItem);
538
+ }
539
+ }
540
+ disabledReason =
541
+ unChangedItems.length === prevItems.length &&
542
+ newItems.length === 0 &&
543
+ removedItems.length === 0 &&
544
+ editedItems.length === 0
545
+ ? 'No changes in the order'
546
+ : prevItems.length === removedItems.length - totalTempRemovedItems &&
547
+ editedItems.length === 0 &&
548
+ newItems.length === 0
549
+ ? 'At least one item is required'
550
+ : '';
551
+ }
552
+ const cartItemsObj = {
553
+ unChangedItems: getCartItems(unChangedItems),
554
+ editedItems: getCartItems(editedItems),
555
+ removedItems: getCartItems(removedItems),
556
+ newItems: getCartItems(newItems),
557
+ totalSelectedItems: (0, utils_1.parseNum)(totalItemsInCart, true),
558
+ disabledReason,
559
+ isAnUpdate,
560
+ };
561
+ return cartItemsObj;
562
+ }
563
+ function getVoidItemsWithReasonWarehouseSaleOrderRfq({ currentCategories, iterationsFromFrontend, previousCategories, }) {
564
+ const voidOrderItemReasonsMap = {};
565
+ const latestIterationCategories = iterationsFromFrontend?.[0]?.categories;
566
+ if (latestIterationCategories != null && latestIterationCategories.length > 0) {
567
+ for (let i = 0; i < latestIterationCategories.length; i++) {
568
+ const category = latestIterationCategories[i];
569
+ for (let j = 0; j < category.items.length; j++) {
570
+ const item = category.items[j];
571
+ if (item.void_reason != null && item.void_reason.length > 0) {
572
+ voidOrderItemReasonsMap[item._id] = item.void_reason;
573
+ }
574
+ }
575
+ }
576
+ }
577
+ const { newItems, removedItems } = getWarehouseSaleOrderRfqCartItemsObj({
578
+ currentIterationCategories: currentCategories,
579
+ previousIterationCategories: previousCategories,
580
+ mappedItems: {},
581
+ shouldSortItemsViaId: false,
582
+ isAnEdit: true,
583
+ });
584
+ const hasNewItems = newItems.length > 0;
585
+ const voidOrderItems = [];
586
+ const newVoidOrderItemReasonsMap = {};
587
+ for (let i = 0; i < removedItems.length; i++) {
588
+ const { orderItem } = removedItems[i];
589
+ const voidReason = voidOrderItemReasonsMap[orderItem._id];
590
+ voidOrderItems.push({
591
+ ...orderItem,
592
+ subtotal: Math.round(orderItem.subtotal),
593
+ void_reason: voidReason ?? '-',
594
+ });
595
+ if (voidReason != null) {
596
+ newVoidOrderItemReasonsMap[orderItem._id] = voidReason;
597
+ }
598
+ }
599
+ const newIterationCategories = [];
600
+ for (let i = 0; i < previousCategories.length; i++) {
601
+ const category = previousCategories[i];
602
+ const newCategoryItems = [];
603
+ for (let j = 0; j < category.items.length; j++) {
604
+ const prevItem = category.items[j];
605
+ const voidReason = newVoidOrderItemReasonsMap[prevItem._id];
606
+ const orderItem = {
607
+ ...prevItem,
608
+ subtotal: Math.round(prevItem.subtotal),
609
+ };
610
+ if (voidReason != null) {
611
+ orderItem.void_reason = voidReason;
612
+ }
613
+ newCategoryItems.push(orderItem);
614
+ }
615
+ if (newCategoryItems.length === 0)
616
+ continue;
617
+ newIterationCategories.push({
618
+ ...category,
619
+ items: newCategoryItems,
620
+ });
621
+ }
622
+ return { newIterationCategories, voidOrderItems, hasNewItems };
623
+ }
@@ -18,6 +18,7 @@ export * from './order-engine';
18
18
  export * from './pra-generate-invoice';
19
19
  export * from './warehouse-purchase-order-cart-actions';
20
20
  export * from './warehouse-purchase-order-rfq-cart-actions';
21
+ export * from './warehouse-sale-order-rfq-cart-actions';
21
22
  export * from './report-webview';
22
23
  export * from './reports';
23
24
  export * from './smartpay-apis';
@@ -34,6 +34,7 @@ __exportStar(require("./order-engine"), exports);
34
34
  __exportStar(require("./pra-generate-invoice"), exports);
35
35
  __exportStar(require("./warehouse-purchase-order-cart-actions"), exports);
36
36
  __exportStar(require("./warehouse-purchase-order-rfq-cart-actions"), exports);
37
+ __exportStar(require("./warehouse-sale-order-rfq-cart-actions"), exports);
37
38
  __exportStar(require("./report-webview"), exports);
38
39
  __exportStar(require("./reports"), exports);
39
40
  __exportStar(require("./smartpay-apis"), exports);
@@ -0,0 +1,26 @@
1
+ import { IWarehouseSaleOrderRFQItem } from '../../schemas';
2
+ export interface IWarehouseGeneralSaleOrderRfqItem {
3
+ _id: string;
4
+ name: string;
5
+ category_id: string;
6
+ category_name: string;
7
+ category_image_url: string | null;
8
+ image_url: string | null;
9
+ description: string;
10
+ }
11
+ export interface IWarehouseSaleOrderRfqCartItem {
12
+ orderItem: IWarehouseSaleOrderRFQItem;
13
+ image_url: string | null;
14
+ description: string;
15
+ category_name: string;
16
+ category_id: string;
17
+ }
18
+ export interface IWarehouseSaleOrderRfqCartItemsObj {
19
+ unChangedItems: IWarehouseSaleOrderRfqCartItem[];
20
+ editedItems: IWarehouseSaleOrderRfqCartItem[];
21
+ removedItems: IWarehouseSaleOrderRfqCartItem[];
22
+ newItems: IWarehouseSaleOrderRfqCartItem[];
23
+ totalSelectedItems: number;
24
+ disabledReason: string;
25
+ isAnUpdate: boolean;
26
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,5 @@
1
1
  import { Schema } from 'mongoose';
2
+ import { WAREHOUSE_SALES_ORDER_STATUS_ENUM } from '../../constants';
2
3
  export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
3
4
  timestamps: true;
4
5
  versionKey: false;
@@ -153,7 +154,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
153
154
  price: number;
154
155
  order_number: number;
155
156
  special_instructions: string;
156
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
157
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
157
158
  tax_number: string;
158
159
  amount_earned: number;
159
160
  grand_total: number;
@@ -300,7 +301,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
300
301
  }>;
301
302
  price: number;
302
303
  special_instructions: string;
303
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
304
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
304
305
  tax_number: string;
305
306
  amount_earned: number;
306
307
  grand_total: number;
@@ -456,7 +457,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
456
457
  }>;
457
458
  price: number;
458
459
  special_instructions: string;
459
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
460
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
460
461
  tax_number: string;
461
462
  amount_earned: number;
462
463
  grand_total: number;
@@ -612,7 +613,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
612
613
  }>;
613
614
  price: number;
614
615
  special_instructions: string;
615
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
616
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
616
617
  tax_number: string;
617
618
  amount_earned: number;
618
619
  grand_total: number;
@@ -797,7 +798,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
797
798
  price: number;
798
799
  order_number: number;
799
800
  special_instructions: string;
800
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
801
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
801
802
  tax_number: string;
802
803
  amount_earned: number;
803
804
  grand_total: number;
@@ -944,7 +945,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
944
945
  }>;
945
946
  price: number;
946
947
  special_instructions: string;
947
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
948
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
948
949
  tax_number: string;
949
950
  amount_earned: number;
950
951
  grand_total: number;
@@ -1100,7 +1101,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
1100
1101
  }>;
1101
1102
  price: number;
1102
1103
  special_instructions: string;
1103
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1104
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1104
1105
  tax_number: string;
1105
1106
  amount_earned: number;
1106
1107
  grand_total: number;
@@ -1256,7 +1257,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
1256
1257
  }>;
1257
1258
  price: number;
1258
1259
  special_instructions: string;
1259
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1260
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1260
1261
  tax_number: string;
1261
1262
  amount_earned: number;
1262
1263
  grand_total: number;
@@ -1441,7 +1442,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
1441
1442
  price: number;
1442
1443
  order_number: number;
1443
1444
  special_instructions: string;
1444
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1445
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1445
1446
  tax_number: string;
1446
1447
  amount_earned: number;
1447
1448
  grand_total: number;
@@ -1588,7 +1589,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
1588
1589
  }>;
1589
1590
  price: number;
1590
1591
  special_instructions: string;
1591
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1592
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1592
1593
  tax_number: string;
1593
1594
  amount_earned: number;
1594
1595
  grand_total: number;
@@ -1744,7 +1745,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
1744
1745
  }>;
1745
1746
  price: number;
1746
1747
  special_instructions: string;
1747
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1748
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1748
1749
  tax_number: string;
1749
1750
  amount_earned: number;
1750
1751
  grand_total: number;
@@ -1900,7 +1901,7 @@ export declare const SaleOrdersSchema: Schema<any, import("mongoose").Model<any,
1900
1901
  }>;
1901
1902
  price: number;
1902
1903
  special_instructions: string;
1903
- order_status: import("../../constants").WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1904
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
1904
1905
  tax_number: string;
1905
1906
  amount_earned: number;
1906
1907
  grand_total: number;
@@ -212,7 +212,7 @@ exports.SaleOrdersSchema = new mongoose_1.Schema({
212
212
  order_status: {
213
213
  type: String,
214
214
  enum: constants_1.ALL_WAREHOUSE_SALES_ORDER_STATUS_NAMES,
215
- required: true,
215
+ default: constants_1.WAREHOUSE_SALES_ORDER_STATUS_ENUM.DRAFT,
216
216
  },
217
217
  tax_number: {
218
218
  type: String,
@@ -81,7 +81,6 @@ export * from './warehouse_purchase_order_vendor_returns';
81
81
  export * from './warehouse_purchase_orders';
82
82
  export * from './warehouse_sale_deliveries';
83
83
  export * from './warehouse_sale_invoices';
84
- export * from './warehouse_sale_order_common';
85
84
  export * from './warehouse_sale_order_rfqs';
86
85
  export * from './warehouse_sale_orders';
87
86
  export * from './warehouse_staffs';
@@ -97,7 +97,6 @@ __exportStar(require("./warehouse_purchase_order_vendor_returns"), exports);
97
97
  __exportStar(require("./warehouse_purchase_orders"), exports);
98
98
  __exportStar(require("./warehouse_sale_deliveries"), exports);
99
99
  __exportStar(require("./warehouse_sale_invoices"), exports);
100
- __exportStar(require("./warehouse_sale_order_common"), exports);
101
100
  __exportStar(require("./warehouse_sale_order_rfqs"), exports);
102
101
  __exportStar(require("./warehouse_sale_orders"), exports);
103
102
  __exportStar(require("./warehouse_staffs"), exports);
@@ -1,8 +1,37 @@
1
1
  import { WAREHOUSE_SALE_ORDER_RFQ_STATUS_ENUM } from '../../constants';
2
- import { IWarehouseSaleOrderCommonCategoryByCategoryId, IWarehouseSaleOrderCommonItem, IWarehouseSaleOrderCommonIterationBase } from './warehouse_sale_order_common';
3
- export type IWarehouseSaleOrderRFQItem = IWarehouseSaleOrderCommonItem;
4
- export type IWarehouseSaleOrderRFQCategory = IWarehouseSaleOrderCommonCategoryByCategoryId;
5
- export interface IWarehouseSaleOrderRFQIteration extends IWarehouseSaleOrderCommonIterationBase<IWarehouseSaleOrderRFQCategory, WAREHOUSE_SALE_ORDER_RFQ_STATUS_ENUM | string> {
2
+ import { IStaffDetails } from './orders';
3
+ export type IWarehouseSaleOrderRFQItem = {
4
+ _id: string;
5
+ item_id: string;
6
+ is_ready?: boolean;
7
+ void_reason?: string | null;
8
+ name: string;
9
+ price: number;
10
+ subtotal: number;
11
+ quantity: number;
12
+ _net_amount: number;
13
+ _gross_amount: number;
14
+ _gst_amount: number;
15
+ _discount_amount: number;
16
+ special_instructions: string;
17
+ };
18
+ export type IWarehouseSaleOrderRFQCategory = {
19
+ name: string;
20
+ category_id: string;
21
+ items: IWarehouseSaleOrderRFQItem[];
22
+ };
23
+ export interface IWarehouseSaleOrderRFQIteration {
24
+ categories: IWarehouseSaleOrderRFQCategory[];
25
+ staff_details: IStaffDetails;
26
+ is_gst_inclusive?: boolean;
27
+ gst?: number | null;
28
+ gst_amount?: number | null;
29
+ special_instructions: string;
30
+ price: number;
31
+ amount_earned: number;
32
+ grand_total: number;
33
+ order_status: WAREHOUSE_SALE_ORDER_RFQ_STATUS_ENUM;
34
+ updatedAt: Date;
6
35
  tax_number: string;
7
36
  }
8
37
  export interface IWarehouseSaleOrderRFQ extends IWarehouseSaleOrderRFQIteration {
@@ -1,8 +1,37 @@
1
1
  import { WAREHOUSE_SALES_ORDER_STATUS_ENUM } from '../../constants';
2
- import { IWarehouseSaleOrderCommonCategoryByCategoryId, IWarehouseSaleOrderCommonItem, IWarehouseSaleOrderCommonIterationBase } from './warehouse_sale_order_common';
3
- export type IWarehouseSaleOrderItem = IWarehouseSaleOrderCommonItem;
4
- export type IWarehouseSaleOrderCategory = IWarehouseSaleOrderCommonCategoryByCategoryId;
5
- export interface IWarehouseSaleOrderIteration extends IWarehouseSaleOrderCommonIterationBase<IWarehouseSaleOrderCategory, WAREHOUSE_SALES_ORDER_STATUS_ENUM> {
2
+ import { IStaffDetails } from './orders';
3
+ export type IWarehouseSaleOrderItem = {
4
+ _id: string;
5
+ item_id: string;
6
+ is_ready?: boolean;
7
+ void_reason?: string | null;
8
+ name: string;
9
+ price: number;
10
+ subtotal: number;
11
+ quantity: number;
12
+ _net_amount: number;
13
+ _gross_amount: number;
14
+ _gst_amount: number;
15
+ _discount_amount: number;
16
+ special_instructions: string;
17
+ };
18
+ export type IWarehouseSaleOrderCategory = {
19
+ name: string;
20
+ category_id: string;
21
+ items: IWarehouseSaleOrderItem[];
22
+ };
23
+ export interface IWarehouseSaleOrderIteration {
24
+ categories: IWarehouseSaleOrderCategory[];
25
+ staff_details: IStaffDetails;
26
+ is_gst_inclusive?: boolean;
27
+ gst?: number | null;
28
+ gst_amount?: number | null;
29
+ special_instructions: string;
30
+ price: number;
31
+ amount_earned: number;
32
+ grand_total: number;
33
+ order_status: WAREHOUSE_SALES_ORDER_STATUS_ENUM;
34
+ updatedAt: Date;
6
35
  tax_number: string;
7
36
  }
8
37
  export interface IWarehouseSaleOrder extends IWarehouseSaleOrderIteration {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.445",
3
+ "version": "0.2.448",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",