@final-commerce/common 2.12.2-staging2.1 → 2.13.0-staging2.1
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.
- package/dist/index.d.mts +76 -3
- package/dist/index.d.ts +76 -3
- package/dist/index.js +171 -40
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +156 -40
- package/dist/index.mjs.map +1 -1
- package/dist/pos-types/index.d.mts +12 -1
- package/dist/pos-types/index.d.ts +12 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -136,11 +136,14 @@ __export(index_exports, {
|
|
|
136
136
|
ancestorChain: () => ancestorChain,
|
|
137
137
|
applyAssignment: () => applyAssignment,
|
|
138
138
|
assertUnitSane: () => assertUnitSane,
|
|
139
|
+
attachResolvedUnits: () => attachResolvedUnits,
|
|
139
140
|
availableIn: () => availableIn,
|
|
141
|
+
bankersRoundDiv: () => bankersRoundDiv,
|
|
140
142
|
baseFor: () => baseFor,
|
|
141
143
|
baseFulfillmentDefinitions: () => baseFulfillmentDefinitions,
|
|
142
144
|
baseOf: () => baseOf,
|
|
143
145
|
basePaymentDefinitions: () => basePaymentDefinitions,
|
|
146
|
+
bookCostPerUnit: () => bookCostPerUnit,
|
|
144
147
|
buildAvailability: () => buildAvailability,
|
|
145
148
|
buildGrid: () => buildGrid,
|
|
146
149
|
buildOrderContext: () => buildOrderContext,
|
|
@@ -150,6 +153,9 @@ __export(index_exports, {
|
|
|
150
153
|
canTransition: () => canTransition,
|
|
151
154
|
cheapestCompositeItem: () => cheapestCompositeItem,
|
|
152
155
|
cheapestCompositePrice: () => cheapestCompositePrice,
|
|
156
|
+
componentUnitsPerLine: () => componentUnitsPerLine,
|
|
157
|
+
compositeCostOfKnown: () => compositeCostOfKnown,
|
|
158
|
+
compositeCostRange: () => compositeCostRange,
|
|
153
159
|
compositeItemCost: () => compositeItemCost,
|
|
154
160
|
compositeItemUnavailable: () => compositeItemUnavailable,
|
|
155
161
|
compositeMakeable: () => compositeMakeable,
|
|
@@ -158,6 +164,7 @@ __export(index_exports, {
|
|
|
158
164
|
compositeVariantInStock: () => compositeVariantInStock,
|
|
159
165
|
conditionSummary: () => conditionSummary,
|
|
160
166
|
convertFixedFeeToLocalCurrency: () => convertFixedFeeToLocalCurrency,
|
|
167
|
+
costPriceChangeStamp: () => costPriceChangeStamp,
|
|
161
168
|
createAmount: () => createAmount,
|
|
162
169
|
crossAxisRuleApplies: () => crossAxisRuleApplies,
|
|
163
170
|
currentAssignees: () => currentAssignees,
|
|
@@ -195,6 +202,7 @@ __export(index_exports, {
|
|
|
195
202
|
hasSellableChoice: () => hasSellableChoice,
|
|
196
203
|
holdExpiresAt: () => holdExpiresAt,
|
|
197
204
|
inferStatesFromLegacyStatus: () => inferStatesFromLegacyStatus,
|
|
205
|
+
innerCompositeOption: () => innerCompositeOption,
|
|
198
206
|
isAdyenRolloutCountry: () => isAdyenRolloutCountry,
|
|
199
207
|
isAffirmCountry: () => isAffirmCountry,
|
|
200
208
|
isAffirmPricePlan: () => isAffirmPricePlan,
|
|
@@ -202,16 +210,19 @@ __export(index_exports, {
|
|
|
202
210
|
isConditionOperator: () => isConditionOperator,
|
|
203
211
|
isFulfillmentState: () => isFulfillmentState,
|
|
204
212
|
isInCart: () => isInCart,
|
|
213
|
+
isInnerComposite: () => isInnerComposite,
|
|
205
214
|
isPaymentState: () => isPaymentState,
|
|
206
215
|
isSupportedCurrency: () => isSupportedCurrency,
|
|
207
216
|
isSystemFamilyCode: () => isSystemFamilyCode,
|
|
208
217
|
isValidQuantity: () => isValidQuantity,
|
|
209
218
|
isWhole: () => isWhole,
|
|
219
|
+
landedCostPerUnit: () => landedCostPerUnit,
|
|
210
220
|
lineStockMoves: () => lineStockMoves,
|
|
211
221
|
listCountries: () => listCountries,
|
|
212
222
|
listProvinces: () => listProvinces,
|
|
213
223
|
listShippingCountries: () => listShippingCountries,
|
|
214
224
|
mapToLegacyStatus: () => mapToLegacyStatus,
|
|
225
|
+
marginPercentOf: () => marginPercentOf,
|
|
215
226
|
maxPrecisionFor: () => maxPrecisionFor,
|
|
216
227
|
mergeFragment: () => mergeFragment,
|
|
217
228
|
modifiersSellable: () => modifiersSellable,
|
|
@@ -246,6 +257,7 @@ __export(index_exports, {
|
|
|
246
257
|
roundToPrecision: () => roundToPrecision,
|
|
247
258
|
sameFamily: () => sameFamily,
|
|
248
259
|
setOrderState: () => setOrderState,
|
|
260
|
+
shelfComponents: () => shelfComponents,
|
|
249
261
|
shipsToCountry: () => shipsToCountry,
|
|
250
262
|
shopDayKey: () => shopDayKey,
|
|
251
263
|
shopDayRange: () => shopDayRange,
|
|
@@ -259,6 +271,9 @@ __export(index_exports, {
|
|
|
259
271
|
validateFragmentCompatibility: () => validateFragmentCompatibility,
|
|
260
272
|
validateOrderStatuses: () => validateOrderStatuses,
|
|
261
273
|
validateShippingZone: () => validateShippingZone,
|
|
274
|
+
variantCostAtSale: () => variantCostAtSale,
|
|
275
|
+
variantDisplayName: () => variantDisplayName,
|
|
276
|
+
variantLabel: () => variantLabel,
|
|
262
277
|
violatesFinancialInvariant: () => violatesFinancialInvariant,
|
|
263
278
|
windowEndsAt: () => windowEndsAt,
|
|
264
279
|
withinPresence: () => withinPresence
|
|
@@ -1286,6 +1301,16 @@ function resolveUnit(unitId, companyUnits = []) {
|
|
|
1286
1301
|
if (!unit2) throw new Error(`Unknown measurement unit: ${unitId}`);
|
|
1287
1302
|
return unit2;
|
|
1288
1303
|
}
|
|
1304
|
+
function attachResolvedUnits(variants, companyUnits, logLabel = "attachUnits") {
|
|
1305
|
+
for (const variant of variants)
|
|
1306
|
+
if (variant.unitId)
|
|
1307
|
+
try {
|
|
1308
|
+
variant.unit = resolveUnit(variant.unitId, companyUnits);
|
|
1309
|
+
} catch (error) {
|
|
1310
|
+
console.error(`[${logLabel}] unit ${variant.unitId} does not resolve. ${error.message}`);
|
|
1311
|
+
}
|
|
1312
|
+
return variants;
|
|
1313
|
+
}
|
|
1289
1314
|
function resolveFamily(familyId, companyFamilies = []) {
|
|
1290
1315
|
const family = SYSTEM_FAMILIES.find((f) => f.familyId === familyId) ?? companyFamilies.find((f) => f.familyId === familyId);
|
|
1291
1316
|
if (!family) throw new Error(`Unknown measurement unit family: ${familyId}`);
|
|
@@ -1826,6 +1851,66 @@ function hasSellableChoice(modifier, outletId) {
|
|
|
1826
1851
|
}
|
|
1827
1852
|
var modifiersSellable = (modifiers, outletId) => modifiers.every((modifier) => !modifier.required || hasSellableChoice(modifier, outletId));
|
|
1828
1853
|
|
|
1854
|
+
// utils/line-stock-moves.util.ts
|
|
1855
|
+
function lineStockMoves(item, quantity, reservations = []) {
|
|
1856
|
+
const sign = Math.sign(quantity);
|
|
1857
|
+
const pool = (stockVariantId) => stockVariantId ? { stockVariantId: stockVariantId.toString() } : {};
|
|
1858
|
+
if (!item.components?.length)
|
|
1859
|
+
return [
|
|
1860
|
+
{
|
|
1861
|
+
productId: item.productId.toString(),
|
|
1862
|
+
variantId: item.variantId.toString(),
|
|
1863
|
+
fields: {
|
|
1864
|
+
quantity,
|
|
1865
|
+
...item.stockQuantity != null ? { stockQuantity: sign * item.stockQuantity, unitId: item.unitId?.toString() } : {},
|
|
1866
|
+
...pool(item.stockVariantId)
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
];
|
|
1870
|
+
const booked = new Set(
|
|
1871
|
+
reservations.filter((reservation) => reservation.lineItemInternalId && reservation.lineItemInternalId === item.internalId).map((reservation) => reservation.componentIndex)
|
|
1872
|
+
);
|
|
1873
|
+
return shelfComponents(item.components).flatMap(({ component, componentIndex, parentUnits }) => {
|
|
1874
|
+
if (booked.has(componentIndex)) return [];
|
|
1875
|
+
if (component.unitId && component.stockQuantity == null)
|
|
1876
|
+
throw new Error(
|
|
1877
|
+
`Composite ${item.productId?.toString()} component ${component.variantId.toString()} is sold in ${component.unitId.toString()} but carries no stockQuantity`
|
|
1878
|
+
);
|
|
1879
|
+
return [
|
|
1880
|
+
{
|
|
1881
|
+
productId: component.productId.toString(),
|
|
1882
|
+
variantId: component.variantId.toString(),
|
|
1883
|
+
componentIndex,
|
|
1884
|
+
fields: {
|
|
1885
|
+
quantity: component.quantity * parentUnits * quantity,
|
|
1886
|
+
...component.stockQuantity != null ? { stockQuantity: component.stockQuantity * parentUnits * quantity, unitId: component.unitId?.toString() } : {},
|
|
1887
|
+
...pool(component.stockVariantId),
|
|
1888
|
+
...item.productId ? { compositeProductId: item.productId.toString() } : {},
|
|
1889
|
+
...item.name ? { compositeName: item.name } : {}
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
];
|
|
1893
|
+
});
|
|
1894
|
+
}
|
|
1895
|
+
function componentUnitsPerLine(components, index) {
|
|
1896
|
+
const component = components[index];
|
|
1897
|
+
if (!component) throw new Error(`No component ${index} on the line`);
|
|
1898
|
+
if (component.parentIndex == null) return component.quantity;
|
|
1899
|
+
if (component.parentIndex >= index)
|
|
1900
|
+
throw new Error(`Component ${index} points to parent ${component.parentIndex}, which is not before it`);
|
|
1901
|
+
return component.quantity * componentUnitsPerLine(components, component.parentIndex);
|
|
1902
|
+
}
|
|
1903
|
+
var isInnerComposite = (components, index) => components.some((child) => child.parentIndex === index);
|
|
1904
|
+
var shelfComponents = (components) => components.flatMap(
|
|
1905
|
+
(component, componentIndex) => isInnerComposite(components, componentIndex) ? [] : [
|
|
1906
|
+
{
|
|
1907
|
+
component,
|
|
1908
|
+
componentIndex,
|
|
1909
|
+
parentUnits: component.parentIndex == null ? 1 : componentUnitsPerLine(components, component.parentIndex)
|
|
1910
|
+
}
|
|
1911
|
+
]
|
|
1912
|
+
);
|
|
1913
|
+
|
|
1829
1914
|
// utils/composite-availability.util.ts
|
|
1830
1915
|
function poolStockAt(rows, outletId) {
|
|
1831
1916
|
if (!rows.some((row) => row.manageStock === true)) return null;
|
|
@@ -1964,7 +2049,9 @@ function poolDemand(lines) {
|
|
|
1964
2049
|
(demand.get(poolOf(line)) ?? 0) + (line.unit ? toBase(line.quantity * times, line.unit) : line.quantity * times)
|
|
1965
2050
|
);
|
|
1966
2051
|
for (const line of lines) {
|
|
1967
|
-
if (line.components)
|
|
2052
|
+
if (line.components)
|
|
2053
|
+
for (const { component, parentUnits } of shelfComponents(line.components))
|
|
2054
|
+
add(component, line.quantity * parentUnits);
|
|
1968
2055
|
else add(line, 1);
|
|
1969
2056
|
}
|
|
1970
2057
|
return demand;
|
|
@@ -2006,47 +2093,76 @@ function compositeReservationMismatches(order) {
|
|
|
2006
2093
|
});
|
|
2007
2094
|
}
|
|
2008
2095
|
|
|
2009
|
-
// utils/
|
|
2010
|
-
function
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
);
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
];
|
|
2096
|
+
// utils/variant-cost.util.ts
|
|
2097
|
+
function variantCostAtSale({
|
|
2098
|
+
costPrice,
|
|
2099
|
+
costPriceUpdatedAt,
|
|
2100
|
+
landedCost,
|
|
2101
|
+
lastReceivedAt
|
|
2102
|
+
}) {
|
|
2103
|
+
if (landedCost == null) return costPrice ?? null;
|
|
2104
|
+
if (costPrice == null) return landedCost;
|
|
2105
|
+
if (!costPriceUpdatedAt || !lastReceivedAt) return costPrice;
|
|
2106
|
+
return new Date(lastReceivedAt).getTime() > new Date(costPriceUpdatedAt).getTime() ? landedCost : costPrice;
|
|
2107
|
+
}
|
|
2108
|
+
function compositeCostOfKnown(items) {
|
|
2109
|
+
const known = items.filter((item) => item.cost !== null);
|
|
2110
|
+
if (!known.length) return null;
|
|
2111
|
+
return known.reduce((sum, { cost, quantity }) => sum + extendPrice(cost, quantity), 0);
|
|
2112
|
+
}
|
|
2113
|
+
function compositeCostRange(parts) {
|
|
2114
|
+
const missing = new Set(parts.flatMap((part) => part.options.flatMap((option) => option.missing)));
|
|
2115
|
+
const [low, high] = [pickedEnd(parts, "low"), pickedEnd(parts, "high")];
|
|
2116
|
+
const [lowCost, highCost] = [low.length ? compositeCostOfKnown(low) : 0, compositeCostOfKnown(high)];
|
|
2117
|
+
return { range: lowCost === null || highCost === null ? null : [lowCost, highCost], missing: [...missing] };
|
|
2118
|
+
}
|
|
2119
|
+
function innerCompositeOption(name, quantity, cost) {
|
|
2120
|
+
const [low, high] = cost.range ?? [null, null];
|
|
2121
|
+
return { name, quantity, low, high, missing: cost.range === null && !cost.missing.length ? [name] : cost.missing };
|
|
2122
|
+
}
|
|
2123
|
+
function variantLabel(variant, variantCount) {
|
|
2124
|
+
if (variantCount === 1) return "";
|
|
2125
|
+
const attributes = (variant.attributes ?? []).map((attribute) => attribute.value ?? attribute.values?.[0] ?? "");
|
|
2126
|
+
return attributes.filter(Boolean).join(" / ") || variant.name || variant.sku || "";
|
|
2127
|
+
}
|
|
2128
|
+
function variantDisplayName(productName, variant, variantCount) {
|
|
2129
|
+
const label = variantLabel(variant, variantCount);
|
|
2130
|
+
return label ? `${productName} \xB7 ${label}` : productName;
|
|
2131
|
+
}
|
|
2132
|
+
function pickedEnd(parts, end) {
|
|
2133
|
+
return parts.flatMap((part) => {
|
|
2134
|
+
if (!part.options.length) return [];
|
|
2135
|
+
const byCost = part.options.flatMap(({ quantity, [end]: cost }) => cost === null ? [] : [{ cost, quantity }]).sort((a, b) => extendPrice(a.cost, a.quantity) - extendPrice(b.cost, b.quantity));
|
|
2136
|
+
const pick = (end === "low" ? byCost[0] : byCost[byCost.length - 1]) ?? { cost: null, quantity: 1 };
|
|
2137
|
+
return Array(end === "low" ? part.minPicks : part.maxPicks).fill(pick);
|
|
2048
2138
|
});
|
|
2049
2139
|
}
|
|
2140
|
+
function costPriceChangeStamp(next, stored) {
|
|
2141
|
+
if (next === void 0 || next === (stored ?? null)) return {};
|
|
2142
|
+
return { costPriceUpdatedAt: /* @__PURE__ */ new Date() };
|
|
2143
|
+
}
|
|
2144
|
+
function bankersRoundDiv(numerator, denominator) {
|
|
2145
|
+
if (denominator === 0n) return 0n;
|
|
2146
|
+
const negative = numerator < 0n !== denominator < 0n;
|
|
2147
|
+
const n = numerator < 0n ? -numerator : numerator;
|
|
2148
|
+
const d = denominator < 0n ? -denominator : denominator;
|
|
2149
|
+
const quotient = n / d;
|
|
2150
|
+
const twiceRemainder = n % d * 2n;
|
|
2151
|
+
let result;
|
|
2152
|
+
if (twiceRemainder < d) {
|
|
2153
|
+
result = quotient;
|
|
2154
|
+
} else if (twiceRemainder > d) {
|
|
2155
|
+
result = quotient + 1n;
|
|
2156
|
+
} else {
|
|
2157
|
+
result = quotient % 2n === 0n ? quotient : quotient + 1n;
|
|
2158
|
+
}
|
|
2159
|
+
return negative ? -result : result;
|
|
2160
|
+
}
|
|
2161
|
+
function landedCostPerUnit(landedValue, baseQuantity, ratioToBase = 1) {
|
|
2162
|
+
return Number(bankersRoundDiv(BigInt(Math.round(landedValue * ratioToBase)), BigInt(baseQuantity)));
|
|
2163
|
+
}
|
|
2164
|
+
var bookCostPerUnit = (book, ratioToBase) => book && book.totalQuantity > 0 && ratioToBase ? landedCostPerUnit(book.totalLandedValue, book.totalQuantity, ratioToBase) : null;
|
|
2165
|
+
var marginPercentOf = (grossProfit, costedGross) => grossProfit === null || !costedGross ? null : grossProfit * 100 / costedGross;
|
|
2050
2166
|
|
|
2051
2167
|
// units/family-code.ts
|
|
2052
2168
|
var MAX_SLUG_LENGTH = 32;
|
|
@@ -4524,11 +4640,14 @@ var EXPENSE_DEFAULT_CATEGORIES = [
|
|
|
4524
4640
|
ancestorChain,
|
|
4525
4641
|
applyAssignment,
|
|
4526
4642
|
assertUnitSane,
|
|
4643
|
+
attachResolvedUnits,
|
|
4527
4644
|
availableIn,
|
|
4645
|
+
bankersRoundDiv,
|
|
4528
4646
|
baseFor,
|
|
4529
4647
|
baseFulfillmentDefinitions,
|
|
4530
4648
|
baseOf,
|
|
4531
4649
|
basePaymentDefinitions,
|
|
4650
|
+
bookCostPerUnit,
|
|
4532
4651
|
buildAvailability,
|
|
4533
4652
|
buildGrid,
|
|
4534
4653
|
buildOrderContext,
|
|
@@ -4538,6 +4657,9 @@ var EXPENSE_DEFAULT_CATEGORIES = [
|
|
|
4538
4657
|
canTransition,
|
|
4539
4658
|
cheapestCompositeItem,
|
|
4540
4659
|
cheapestCompositePrice,
|
|
4660
|
+
componentUnitsPerLine,
|
|
4661
|
+
compositeCostOfKnown,
|
|
4662
|
+
compositeCostRange,
|
|
4541
4663
|
compositeItemCost,
|
|
4542
4664
|
compositeItemUnavailable,
|
|
4543
4665
|
compositeMakeable,
|
|
@@ -4546,6 +4668,7 @@ var EXPENSE_DEFAULT_CATEGORIES = [
|
|
|
4546
4668
|
compositeVariantInStock,
|
|
4547
4669
|
conditionSummary,
|
|
4548
4670
|
convertFixedFeeToLocalCurrency,
|
|
4671
|
+
costPriceChangeStamp,
|
|
4549
4672
|
createAmount,
|
|
4550
4673
|
crossAxisRuleApplies,
|
|
4551
4674
|
currentAssignees,
|
|
@@ -4583,6 +4706,7 @@ var EXPENSE_DEFAULT_CATEGORIES = [
|
|
|
4583
4706
|
hasSellableChoice,
|
|
4584
4707
|
holdExpiresAt,
|
|
4585
4708
|
inferStatesFromLegacyStatus,
|
|
4709
|
+
innerCompositeOption,
|
|
4586
4710
|
isAdyenRolloutCountry,
|
|
4587
4711
|
isAffirmCountry,
|
|
4588
4712
|
isAffirmPricePlan,
|
|
@@ -4590,16 +4714,19 @@ var EXPENSE_DEFAULT_CATEGORIES = [
|
|
|
4590
4714
|
isConditionOperator,
|
|
4591
4715
|
isFulfillmentState,
|
|
4592
4716
|
isInCart,
|
|
4717
|
+
isInnerComposite,
|
|
4593
4718
|
isPaymentState,
|
|
4594
4719
|
isSupportedCurrency,
|
|
4595
4720
|
isSystemFamilyCode,
|
|
4596
4721
|
isValidQuantity,
|
|
4597
4722
|
isWhole,
|
|
4723
|
+
landedCostPerUnit,
|
|
4598
4724
|
lineStockMoves,
|
|
4599
4725
|
listCountries,
|
|
4600
4726
|
listProvinces,
|
|
4601
4727
|
listShippingCountries,
|
|
4602
4728
|
mapToLegacyStatus,
|
|
4729
|
+
marginPercentOf,
|
|
4603
4730
|
maxPrecisionFor,
|
|
4604
4731
|
mergeFragment,
|
|
4605
4732
|
modifiersSellable,
|
|
@@ -4634,6 +4761,7 @@ var EXPENSE_DEFAULT_CATEGORIES = [
|
|
|
4634
4761
|
roundToPrecision,
|
|
4635
4762
|
sameFamily,
|
|
4636
4763
|
setOrderState,
|
|
4764
|
+
shelfComponents,
|
|
4637
4765
|
shipsToCountry,
|
|
4638
4766
|
shopDayKey,
|
|
4639
4767
|
shopDayRange,
|
|
@@ -4647,6 +4775,9 @@ var EXPENSE_DEFAULT_CATEGORIES = [
|
|
|
4647
4775
|
validateFragmentCompatibility,
|
|
4648
4776
|
validateOrderStatuses,
|
|
4649
4777
|
validateShippingZone,
|
|
4778
|
+
variantCostAtSale,
|
|
4779
|
+
variantDisplayName,
|
|
4780
|
+
variantLabel,
|
|
4650
4781
|
violatesFinancialInvariant,
|
|
4651
4782
|
windowEndsAt,
|
|
4652
4783
|
withinPresence
|