feat(bc): sync pricing units2

This commit is contained in:
Christian Vidal Wolf
2026-05-26 10:25:37 +02:00
parent c64ce84e44
commit 74d4d37769
6 changed files with 55 additions and 55 deletions
+36 -34
View File
@@ -1,9 +1,5 @@
import crypto from 'crypto';
import { getBcConfig, getBCToken, getItemsUrl } from './bc-runtime.js';
function getItemUnitsOfMeasureUrl(config) {
return `https://api.businesscentral.dynamics.com/v2.0/${config.tenantId}/production/api/craze/integrations/v1.0/companies(${config.companyId})/itemUnitsOfMeasure`;
}
import { getBcConfig, getBCToken, getItemsUrl, getItemUnitsOfMeasureUrl } from './bc-runtime.js';
function findHeaderIndex(headers, patterns) {
const normalized = headers.map(h => String(h || '').toLowerCase());
@@ -89,13 +85,11 @@ export function buildBusinessCentralMappingPreview(headers, row) {
const itemUnitsOfMeasurePayload = {
itemNo: getValue(row, articleNoIdx),
qtyPerUnitOfMeasure6: toBcDecimal(getValue(row, unitsOuterIdx)),
width4: toBcDecimal(getValue(row, innerWIdx)),
length4: toBcDecimal(getValue(row, innerLIdx)),
height4: toBcDecimal(getValue(row, innerHIdx)),
width6: toBcDecimal(getValue(row, outerWIdx)),
length6: toBcDecimal(getValue(row, outerLIdx)),
height6: toBcDecimal(getValue(row, outerHIdx)),
code: 'OUTER',
qtyPerUnitOfMeasure: toBcDecimal(getValue(row, unitsOuterIdx)),
width: toBcDecimal(getValue(row, outerWIdx)),
length: toBcDecimal(getValue(row, outerLIdx)),
height: toBcDecimal(getValue(row, outerHIdx)),
};
return {
@@ -115,13 +109,10 @@ export function buildBusinessCentralMappingPreview(headers, row) {
],
itemUnitsFields: [
makeFieldPreview('Article No.', articleNoIdx, 'itemNo', itemUnitsOfMeasurePayload.itemNo),
makeFieldPreview('Units Outer', unitsOuterIdx, 'qtyPerUnitOfMeasure6', itemUnitsOfMeasurePayload.qtyPerUnitOfMeasure6),
makeFieldPreview('CDU/Inner W (cm)', innerWIdx, 'width4', itemUnitsOfMeasurePayload.width4),
makeFieldPreview('CDU/Inner L (cm)', innerLIdx, 'length4', itemUnitsOfMeasurePayload.length4),
makeFieldPreview('CDU/Inner H (cm)', innerHIdx, 'height4', itemUnitsOfMeasurePayload.height4),
makeFieldPreview('Outer W (cm)', outerWIdx, 'width6', itemUnitsOfMeasurePayload.width6),
makeFieldPreview('Outer L (cm)', outerLIdx, 'length6', itemUnitsOfMeasurePayload.length6),
makeFieldPreview('Outer H (cm)', outerHIdx, 'height6', itemUnitsOfMeasurePayload.height6),
makeFieldPreview('Units Outer', unitsOuterIdx, 'qtyPerUnitOfMeasure', itemUnitsOfMeasurePayload.qtyPerUnitOfMeasure),
makeFieldPreview('Outer W (cm)', outerWIdx, 'width', itemUnitsOfMeasurePayload.width),
makeFieldPreview('Outer L (cm)', outerLIdx, 'length', itemUnitsOfMeasurePayload.length),
makeFieldPreview('Outer H (cm)', outerHIdx, 'height', itemUnitsOfMeasurePayload.height),
],
};
}
@@ -238,13 +229,21 @@ function getItemsSelect() {
function getItemUnitsSelect() {
return [
'itemNo',
'qtyPerUnitOfMeasure6',
'width4',
'length4',
'height4',
'width6',
'length6',
'height6',
'code',
'qtyPerUnitOfMeasure',
'qtyRoundingPrecision',
'length',
'width',
'height',
'cubage',
'weight',
'layerPerPalet2CRZ',
'outerPerLayer2CRZ',
'barCodeCRZ',
'layerPerPaletCRZ',
'outerPerLayerCRZ',
'netWeightCRZ',
'innerTypeBCT',
].join(',');
}
@@ -270,7 +269,7 @@ export async function fetchBusinessCentralSnapshot(config, token, articleNo) {
const { json: itemsJson, etag: itemsEtag } = await fetchJsonOrThrow(itemsUrl, token, 'BC GET items');
const item = Array.isArray(itemsJson.value) && itemsJson.value.length > 0 ? itemsJson.value[0] : null;
const uomFilter = encodeURIComponent(`itemNo eq '${articleNo}'`);
const uomFilter = encodeURIComponent(`itemNo eq '${articleNo}' and code eq '${config.itemUnitsCode || 'OUTER'}'`);
const uomUrl = `${getItemUnitsOfMeasureUrl(config)}?$filter=${uomFilter}&$select=${getItemUnitsSelect()}`;
const { json: uomJson, etag: uomEtag } = await fetchJsonOrThrow(uomUrl, token, 'BC GET itemUnitsOfMeasure');
const itemUnitsOfMeasure = Array.isArray(uomJson.value) && uomJson.value.length > 0 ? uomJson.value[0] : null;
@@ -388,13 +387,15 @@ function renderTemplate(template, context) {
const DECIMAL_FIELDS = new Set([
'minimumOrderQuantity',
'qtyPerUnitOfMeasure6',
'width4',
'length4',
'height4',
'width6',
'length6',
'height6',
'qtyPerUnitOfMeasure',
'length',
'width',
'height',
'cubage',
'weight',
'layerPerPalet2CRZ',
'outerPerLayer2CRZ',
'netWeightCRZ',
]);
const EMPTY_STRING_FIELDS = new Set([
@@ -518,6 +519,7 @@ export async function applyBusinessCentralSync(config, token, headers, row, prev
itemUnitsOfMeasureUrl: getItemUnitsOfMeasureUrl(config),
articleNo: mapping.articleNo,
itemNo: mapping.articleNo,
itemUnitsCode: config.itemUnitsCode || 'OUTER',
systemId: snapshot.item?.systemId || '',
cpnpNo: mapping.itemsPayload.cpnpNo,
itemsPayload: preview.items.desired,