@managesome/eposmatic-toolkit 0.2.375 → 0.2.377

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,5 @@
1
1
  import { SALES_SUMMARY_ENUM } from './ANALYTICS';
2
- import { PURCHASE_ORDER_STATUS_ENUM, WAREHOUSE_INVOICE_STATUS_ENUM } from './ENUMS';
2
+ import { PURCHASE_ORDER_STATUS_ENUM, SALE_ORDER_INVOICE_TYPE_ENUM, SALES_ORDER_STATUS_ENUM, WAREHOUSE_INVOICE_STATUS_ENUM } from './ENUMS';
3
3
  export declare enum WAREHOUSE_DASHBOARD_ANALYTICS_ENUM {
4
4
  PURCHASE_ORDERS = "PURCHASE_ORDERS",
5
5
  INVOICES = "INVOICES"
@@ -59,6 +59,22 @@ export interface IClientInvoicesOverview {
59
59
  totalAmount: number;
60
60
  totalQuantity: number;
61
61
  }
62
+ export interface ISaleOrderInvoiceEntry {
63
+ invoiceType: SALE_ORDER_INVOICE_TYPE_ENUM;
64
+ paymentType: string;
65
+ amount: number;
66
+ }
67
+ export interface ISaleOrdersOverview {
68
+ _id: string;
69
+ warehouse_id: string;
70
+ client_id: string;
71
+ order_status: SALES_ORDER_STATUS_ENUM;
72
+ client_name: string;
73
+ saleOrderId: string;
74
+ totalAmount: number;
75
+ totalQuantity: number;
76
+ invoices: ISaleOrderInvoiceEntry[];
77
+ }
62
78
  export interface IWarehousePurchaseOrdersSummary {
63
79
  [SALES_SUMMARY_ENUM.TOTAL_SALES]: {
64
80
  _id: string;
@@ -1932,19 +1932,10 @@ function getAddItemToCartData(data, payload, courses, listnerSettings, appliedCu
1932
1932
  itemDiscountDetails: itemWithValidatedRewardClaim.item_discount_details,
1933
1933
  rewardClaim: validatedRewardClaim,
1934
1934
  });
1935
- // Recalculate order-wide custom discount with caps before final sync
1936
- const syncedOrderDiscountDetails = appliedCustomDiscounts && appliedCustomDiscounts.length > 0
1937
- ? getSyncedOrderDiscountDetailsWithCaps({
1938
- orderDiscountDetails: orderWithSyncedItemRewardEvidence.order_discount_details,
1939
- appliedCustomDiscounts,
1940
- categories: orderWithSyncedItemRewardEvidence.categories,
1941
- platform: (orderWithSyncedItemRewardEvidence.platform ?? data.platform),
1942
- })
1943
- : orderWithSyncedItemRewardEvidence.order_discount_details;
1944
- const orderForSync = syncedOrderDiscountDetails !== orderWithSyncedItemRewardEvidence.order_discount_details
1945
- ? { ...orderWithSyncedItemRewardEvidence, order_discount_details: syncedOrderDiscountDetails }
1946
- : orderWithSyncedItemRewardEvidence;
1947
- const newData = calculateCartOrderInfoAndSyncData(orderForSync, courses, listnerSettings);
1935
+ const newData = calculateCartOrderInfoAndSyncData(orderWithSyncedItemRewardEvidence, courses, listnerSettings);
1936
+ if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
1937
+ return applyCustomDiscountCapsPostSync(newData, appliedCustomDiscounts, (orderWithSyncedItemRewardEvidence.platform ?? data.platform), courses, listnerSettings);
1938
+ }
1948
1939
  return newData;
1949
1940
  }
1950
1941
  function getSplitCartItemToTwo({ data, orderItemId, courses, listnerSettings }) {
@@ -2010,76 +2001,166 @@ function getAddItemsToCartData(data, payload, courses, listnerSettings, appliedC
2010
2001
  return newData;
2011
2002
  }
2012
2003
  /**
2013
- * Recalculate an order-wide special_discount from the original discount definition.
2014
- * Returns the updated order_discount_details, or the original if no match is found.
2015
- * This ensures that quantity/cart changes correctly respect max_amount/max_percentage caps.
2004
+ * Post-sync cap enforcement for custom discounts.
2005
+ *
2006
+ * After `calculateCartOrderInfoAndSyncData` has computed correct `total_discount`
2007
+ * values (handling GST, modifiers, item totals, etc.), this function checks
2008
+ * whether any order-level or item-level percentage discount exceeds its configured
2009
+ * `max_amount` cap. If so, the discount is collapsed to a fixed amount equal to
2010
+ * the cap and the cart is re-synced.
2011
+ *
2012
+ * All manual subtotal / tax computation is eliminated — the sync result is the
2013
+ * single source of truth for the discountable base.
2016
2014
  */
2017
- function getSyncedOrderDiscountDetailsWithCaps({ orderDiscountDetails, appliedCustomDiscounts, categories, platform, }) {
2018
- if (!orderDiscountDetails?.special_discount_id)
2019
- return orderDiscountDetails;
2020
- const matchedOrderDiscount = appliedCustomDiscounts.find((d) => d._id === orderDiscountDetails.special_discount_id && d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ORDER_WIDE);
2021
- if (!matchedOrderDiscount)
2022
- return orderDiscountDetails;
2023
- // Calculate current subtotal of all items in the categories
2024
- let currentSubtotal = 0;
2025
- if (categories) {
2026
- for (const category of categories) {
2027
- for (const item of category.items) {
2028
- const modifiersTotal = calculateItemModifiersPrice(item);
2029
- currentSubtotal += (item.price + modifiersTotal) * item.quantity;
2030
- }
2031
- }
2032
- }
2033
- const discountValue = matchedOrderDiscount.value[platform] ?? matchedOrderDiscount.value.BASE_PRICE;
2034
- let recalculatedDiscount;
2035
- let isSpecialDiscountPercentage = matchedOrderDiscount.is_percentage;
2036
- if (matchedOrderDiscount.is_percentage) {
2037
- // Clamp percentage to 100% to match apply-time logic in CustomDiscountItem.tsx
2038
- let discountPercentage = discountValue;
2039
- if (discountPercentage > 100)
2040
- discountPercentage = 100;
2041
- const maxAmount = matchedOrderDiscount.max_amount
2042
- ? (matchedOrderDiscount.max_amount[platform] ?? matchedOrderDiscount.max_amount.BASE_PRICE)
2043
- : null;
2044
- const hasCap = maxAmount != null && Number.isFinite(maxAmount) && maxAmount > 0;
2045
- if (hasCap) {
2046
- const computedDiscount = (discountPercentage / 100) * currentSubtotal;
2047
- if (computedDiscount > maxAmount && maxAmount <= currentSubtotal) {
2048
- // Cap exceeded — store as fixed amount equal to the cap
2049
- recalculatedDiscount = maxAmount;
2050
- isSpecialDiscountPercentage = false;
2051
- }
2052
- else {
2053
- // Within cap — keep as percentage
2054
- recalculatedDiscount = discountPercentage;
2055
- isSpecialDiscountPercentage = true;
2015
+ function applyCustomDiscountCapsPostSync(synced, appliedCustomDiscounts, platform, courses, listnerSettings) {
2016
+ let needsResync = false;
2017
+ let result = synced;
2018
+ // ── Order-level cap check ──────────────────────────────────────────
2019
+ const orderDetails = result.order_discount_details;
2020
+ if (orderDetails?.special_discount_id) {
2021
+ const matched = appliedCustomDiscounts.find((d) => d._id === orderDetails.special_discount_id && d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ORDER_WIDE);
2022
+ if (matched?.is_percentage && matched.max_amount) {
2023
+ const maxCap = (0, utils_1.parseNum)(matched.max_amount[platform] ?? matched.max_amount.BASE_PRICE);
2024
+ if (Number.isFinite(maxCap) && maxCap > 0) {
2025
+ // Cap breach: total_discount exceeds maxCap → switch to fixed
2026
+ if ((orderDetails.total_discount ?? 0) > maxCap) {
2027
+ result = {
2028
+ ...result,
2029
+ order_discount_details: {
2030
+ ...orderDetails,
2031
+ special_discount: maxCap,
2032
+ is_special_discount_percentage: false,
2033
+ total_discount: 0,
2034
+ },
2035
+ };
2036
+ needsResync = true;
2037
+ }
2038
+ // Un-cap: currently fixed (capped) but percentage would fit under cap → revert
2039
+ else if (orderDetails.is_special_discount_percentage === false && orderDetails.special_discount != null) {
2040
+ const discountPct = (0, utils_1.parseNum)(matched.value[platform] ?? matched.value.BASE_PRICE);
2041
+ if (Number.isFinite(discountPct) && discountPct > 0 && discountPct <= 100) {
2042
+ const computedDiscount = (discountPct / 100) * (result.price ?? 0);
2043
+ if (computedDiscount <= maxCap) {
2044
+ result = {
2045
+ ...result,
2046
+ order_discount_details: {
2047
+ ...orderDetails,
2048
+ special_discount: discountPct,
2049
+ is_special_discount_percentage: true,
2050
+ total_discount: 0,
2051
+ },
2052
+ };
2053
+ needsResync = true;
2054
+ }
2055
+ // Re-cap: percentage still exceeds cap, but the current flat amount
2056
+ // is below cap (e.g. scaled down by a quantity change). Push it
2057
+ // back to maxCap.
2058
+ else if (computedDiscount > maxCap && orderDetails.special_discount < maxCap) {
2059
+ result = {
2060
+ ...result,
2061
+ order_discount_details: {
2062
+ ...orderDetails,
2063
+ special_discount: maxCap,
2064
+ is_special_discount_percentage: false,
2065
+ total_discount: 0,
2066
+ },
2067
+ };
2068
+ needsResync = true;
2069
+ }
2070
+ }
2071
+ }
2056
2072
  }
2057
2073
  }
2058
- else {
2059
- // No cap configured — keep as percentage
2060
- recalculatedDiscount = discountPercentage;
2061
- isSpecialDiscountPercentage = true;
2062
- }
2063
2074
  }
2064
- else {
2065
- // Fixed discount — amount stays as-is, but still subject to max_amount cap
2066
- recalculatedDiscount = discountValue;
2067
- if (matchedOrderDiscount.max_amount) {
2068
- const maxAmount = matchedOrderDiscount.max_amount[platform] ?? matchedOrderDiscount.max_amount.BASE_PRICE;
2069
- if (maxAmount > 0 && recalculatedDiscount > maxAmount) {
2070
- recalculatedDiscount = maxAmount;
2075
+ // ── Item-level cap check ───────────────────────────────────────────
2076
+ const syncedCategories = result.categories;
2077
+ if (syncedCategories) {
2078
+ for (let ci = 0; ci < syncedCategories.length; ci++) {
2079
+ const category = syncedCategories[ci];
2080
+ let categoryChanged = false;
2081
+ const newItems = [];
2082
+ for (let ji = 0; ji < category.items.length; ji++) {
2083
+ const item = category.items[ji];
2084
+ const itemDetails = item.item_discount_details;
2085
+ if (!itemDetails?.special_discount_id) {
2086
+ newItems.push(item);
2087
+ continue;
2088
+ }
2089
+ const matched = appliedCustomDiscounts.find((d) => d._id === itemDetails.special_discount_id && d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ITEM_SPECIFIC);
2090
+ if (!matched?.is_percentage || !matched.max_amount) {
2091
+ newItems.push(item);
2092
+ continue;
2093
+ }
2094
+ const maxCap = (0, utils_1.parseNum)(matched.max_amount[platform] ?? matched.max_amount.BASE_PRICE);
2095
+ if (!Number.isFinite(maxCap) || maxCap <= 0) {
2096
+ newItems.push(item);
2097
+ continue;
2098
+ }
2099
+ // Cap breach: total_discount exceeds maxCap → switch to fixed
2100
+ if ((itemDetails.total_discount ?? 0) > maxCap) {
2101
+ newItems.push({
2102
+ ...item,
2103
+ item_discount_details: {
2104
+ ...itemDetails,
2105
+ special_discount: maxCap,
2106
+ is_special_discount_percentage: false,
2107
+ total_discount: 0,
2108
+ },
2109
+ });
2110
+ needsResync = true;
2111
+ categoryChanged = true;
2112
+ continue;
2113
+ }
2114
+ // Un-cap: currently fixed (capped) but percentage would fit under cap → revert
2115
+ if (itemDetails.is_special_discount_percentage === false && itemDetails.special_discount != null) {
2116
+ const discountPct = (0, utils_1.parseNum)(matched.value[platform] ?? matched.value.BASE_PRICE);
2117
+ if (Number.isFinite(discountPct) && discountPct > 0 && discountPct <= 100) {
2118
+ const computedDiscount = (discountPct / 100) * (item.subtotal ?? 0);
2119
+ if (computedDiscount <= maxCap) {
2120
+ newItems.push({
2121
+ ...item,
2122
+ item_discount_details: {
2123
+ ...itemDetails,
2124
+ special_discount: discountPct,
2125
+ is_special_discount_percentage: true,
2126
+ total_discount: 0,
2127
+ },
2128
+ });
2129
+ needsResync = true;
2130
+ categoryChanged = true;
2131
+ continue;
2132
+ }
2133
+ // Re-cap: percentage still exceeds cap, but the current flat amount
2134
+ // is below cap (e.g. scaled down proportionally by a quantity
2135
+ // decrease). Push it back to maxCap so the discount stays at the
2136
+ // configured limit instead of an incorrect scaled-down value.
2137
+ if (computedDiscount > maxCap && itemDetails.special_discount < maxCap) {
2138
+ newItems.push({
2139
+ ...item,
2140
+ item_discount_details: {
2141
+ ...itemDetails,
2142
+ special_discount: maxCap,
2143
+ is_special_discount_percentage: false,
2144
+ total_discount: 0,
2145
+ },
2146
+ });
2147
+ needsResync = true;
2148
+ categoryChanged = true;
2149
+ continue;
2150
+ }
2151
+ }
2152
+ }
2153
+ newItems.push(item);
2154
+ }
2155
+ if (categoryChanged) {
2156
+ result = {
2157
+ ...result,
2158
+ categories: result.categories.map((c, idx) => (idx === ci ? { ...c, items: newItems } : c)),
2159
+ };
2071
2160
  }
2072
- }
2073
- if (recalculatedDiscount > currentSubtotal) {
2074
- recalculatedDiscount = currentSubtotal;
2075
2161
  }
2076
2162
  }
2077
- return {
2078
- ...orderDiscountDetails,
2079
- special_discount: (0, utils_1.parseNum)(recalculatedDiscount),
2080
- is_special_discount_percentage: isSpecialDiscountPercentage,
2081
- total_discount: 0,
2082
- };
2163
+ return needsResync ? calculateCartOrderInfoAndSyncData(result, courses, listnerSettings) : result;
2083
2164
  }
2084
2165
  function getUpdateItemInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
2085
2166
  const orderItemIdsMap = {};
@@ -2097,73 +2178,6 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings, applie
2097
2178
  ...item,
2098
2179
  ...payload,
2099
2180
  };
2100
- // Recalculate item-level custom discount from the original discount definition
2101
- // when appliedCustomDiscounts are provided. This ensures quantity changes
2102
- // do not incorrectly linearly scale fixed discounts or bypass caps.
2103
- if (appliedCustomDiscounts &&
2104
- appliedCustomDiscounts.length > 0 &&
2105
- newItem.item_discount_details?.special_discount_id) {
2106
- const matchedDiscount = appliedCustomDiscounts.find((d) => d._id === newItem.item_discount_details.special_discount_id &&
2107
- d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ITEM_SPECIFIC);
2108
- if (matchedDiscount) {
2109
- const modifiersTotal = calculateItemModifiersPrice(newItem);
2110
- const itemTotalWithoutTax = (newItem.price + modifiersTotal) * newItem.quantity;
2111
- const platform = data.platform;
2112
- const discountValue = matchedDiscount.value[platform] ?? matchedDiscount.value.BASE_PRICE;
2113
- let recalculatedSpecialDiscount;
2114
- let isItemDiscountPercentage = matchedDiscount.is_percentage;
2115
- if (matchedDiscount.is_percentage) {
2116
- // Clamp percentage to 100% to match apply-time logic in CustomDiscountItem.tsx
2117
- let discountPercentage = discountValue;
2118
- if (discountPercentage > 100)
2119
- discountPercentage = 100;
2120
- const maxAmount = matchedDiscount.max_amount
2121
- ? (matchedDiscount.max_amount[platform] ?? matchedDiscount.max_amount.BASE_PRICE)
2122
- : null;
2123
- const hasCap = maxAmount != null && Number.isFinite(maxAmount) && maxAmount > 0;
2124
- if (hasCap) {
2125
- const computedDiscount = (discountPercentage / 100) * itemTotalWithoutTax;
2126
- if (computedDiscount > maxAmount && maxAmount <= itemTotalWithoutTax) {
2127
- // Cap exceeded — store as fixed amount equal to the cap
2128
- recalculatedSpecialDiscount = maxAmount;
2129
- isItemDiscountPercentage = false;
2130
- }
2131
- else {
2132
- // Within cap — keep as percentage
2133
- recalculatedSpecialDiscount = discountPercentage;
2134
- isItemDiscountPercentage = true;
2135
- }
2136
- }
2137
- else {
2138
- // No cap configured — keep as percentage
2139
- recalculatedSpecialDiscount = discountPercentage;
2140
- isItemDiscountPercentage = true;
2141
- }
2142
- }
2143
- else {
2144
- // Fixed discount — the amount should NOT scale with quantity.
2145
- recalculatedSpecialDiscount = discountValue;
2146
- if (matchedDiscount.max_amount) {
2147
- const maxAmount = matchedDiscount.max_amount[platform] ?? matchedDiscount.max_amount.BASE_PRICE;
2148
- if (maxAmount > 0 && recalculatedSpecialDiscount > maxAmount) {
2149
- recalculatedSpecialDiscount = maxAmount;
2150
- }
2151
- }
2152
- if (recalculatedSpecialDiscount > itemTotalWithoutTax) {
2153
- recalculatedSpecialDiscount = itemTotalWithoutTax;
2154
- }
2155
- }
2156
- newItem = {
2157
- ...newItem,
2158
- item_discount_details: {
2159
- ...newItem.item_discount_details,
2160
- special_discount: (0, utils_1.parseNum)(recalculatedSpecialDiscount),
2161
- is_special_discount_percentage: isItemDiscountPercentage,
2162
- total_discount: 0,
2163
- },
2164
- };
2165
- }
2166
- }
2167
2181
  }
2168
2182
  if (newItem.quantity > 0) {
2169
2183
  items.push(newItem);
@@ -2178,16 +2192,6 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings, applie
2178
2192
  }
2179
2193
  }
2180
2194
  }
2181
- // Recalculate order-wide custom discount from the original discount definition
2182
- // when appliedCustomDiscounts are provided and an order-wide discount is active.
2183
- const syncedOrderDiscountDetails = appliedCustomDiscounts && appliedCustomDiscounts.length > 0
2184
- ? getSyncedOrderDiscountDetailsWithCaps({
2185
- orderDiscountDetails: data.order_discount_details,
2186
- appliedCustomDiscounts,
2187
- categories,
2188
- platform: data.platform,
2189
- })
2190
- : data.order_discount_details;
2191
2195
  const iterations = [];
2192
2196
  if (payload.quantity != null && data.iterations != null && data.iterations.length > 0) {
2193
2197
  const [firstIteration, ...restIterations] = data.iterations;
@@ -2238,12 +2242,14 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings, applie
2238
2242
  }
2239
2243
  const order = {
2240
2244
  ...data,
2241
- order_discount_details: syncedOrderDiscountDetails,
2242
2245
  ...(iterations.length > 0 && { iterations }),
2243
2246
  categories,
2244
2247
  };
2245
- const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
2246
- return newData;
2248
+ const synced = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
2249
+ if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
2250
+ return applyCustomDiscountCapsPostSync(synced, appliedCustomDiscounts, data.platform, courses, listnerSettings);
2251
+ }
2252
+ return synced;
2247
2253
  }
2248
2254
  /* ======================><====================== */
2249
2255
  function getUpdateItemsInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
@@ -2361,23 +2367,16 @@ function getRemoveItemFromCartData(data, payload, courses, listnerSettings, appl
2361
2367
  iterations.push(firstIterationData);
2362
2368
  iterations.push(...restIterations);
2363
2369
  }
2364
- // Re-evaluate order-wide custom discount cap after item removal
2365
- const syncedOrderDiscountDetails = appliedCustomDiscounts && appliedCustomDiscounts.length > 0
2366
- ? getSyncedOrderDiscountDetailsWithCaps({
2367
- orderDiscountDetails: data.order_discount_details,
2368
- appliedCustomDiscounts,
2369
- categories,
2370
- platform: data.platform,
2371
- })
2372
- : data.order_discount_details;
2373
2370
  const order = {
2374
2371
  ...data,
2375
- order_discount_details: syncedOrderDiscountDetails,
2376
2372
  ...(iterations.length > 0 && { iterations }),
2377
2373
  categories,
2378
2374
  };
2379
- const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
2380
- return newData;
2375
+ const synced = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
2376
+ if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
2377
+ return applyCustomDiscountCapsPostSync(synced, appliedCustomDiscounts, data.platform, courses, listnerSettings);
2378
+ }
2379
+ return synced;
2381
2380
  }
2382
2381
  /* ======================>Order Object<====================== */
2383
2382
  function getUpdatedCartInfoOnInit(data, order, courses, platform, categories, mappedItems, tenantId, listnerSettings, offers) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.375",
3
+ "version": "0.2.377",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",