feat(matrix): support new matrix layout with 4 added columns

- Update default column indices to July 2026 layout (EmpCO_Compliant,
  Comments, Categorisation Code, Ingredients added; PM Classification
  removed; downstream columns shifted)
- Seed internal Categorisation Code from the new Excel column while
  keeping app-side edits as the source of truth
- Guard legacy CPNP index-77 fallbacks: only trusted when the current
  layout still holds CPNP there (new layout stores Item To Root Units)
- Use resolved column index for forced zero stock instead of static one

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Christian Vidal Wolf
2026-07-08 13:28:47 +02:00
co-authored by Claude Fable 5
parent 633f523fc8
commit 0d50fce790
3 changed files with 81 additions and 32 deletions
+48 -6
View File
@@ -276,10 +276,26 @@ export default function App() {
console.warn('Supabase sync failed:', syncRes.status, errorText);
}
// Layout detection: older matrix files carried CPNP No. at index 77
// (LEGACY_CPNP_INDEX); the July 2026 layout stores "Item To Root Units"
// there, so legacy CPNP fallbacks must be disabled for the new layout.
const legacyCpnpIndexIsCpnp = String(headers[LEGACY_CPNP_INDEX] ?? '')
.toLowerCase()
.includes('cpnp');
// The new layout also ships Categorisation Code as a real Excel column
// (British spelling). It seeds the internal column below; app-side edits
// stored in the internal column keep precedence.
const normalizeHeaderKey = (h: unknown) =>
String(h ?? '').toLowerCase().replace(/[\s_-]+/g, '');
const excelCategorisationIdx = headers.findIndex(h => {
const key = normalizeHeaderKey(h);
return key === 'categorisationcode' || key === 'categorizationcode';
});
console.log('Fetching synced data from Supabase...');
const syncedData = await getAllSyncedRows();
console.log('Fetching history data from Supabase for merge...');
const historyData = await getHistoryDataForMerge();
const historyData = await getHistoryDataForMerge(legacyCpnpIndexIsCpnp);
console.log('Supabase synced rows:', Object.keys(syncedData).length, '| History rows:', Object.keys(historyData).length);
// Extend headers with virtual columns if the Excel is shorter than the saved data.
@@ -354,12 +370,15 @@ export default function App() {
});
// Older cosmetic edits stored CPNP in index 77 before the dedicated
// internal CPNP column was stabilized at index 107.
// internal CPNP column was stabilized at index 107. Only trust that
// index when the current layout still holds CPNP there — in the new
// layout index 77 is "Item To Root Units".
const legacyCpnp =
hist?.[COLUMNS.CPNP_NO] ??
synced?.data?.[COLUMNS.CPNP_NO] ??
hist?.[LEGACY_CPNP_INDEX] ??
synced?.data?.[LEGACY_CPNP_INDEX];
(legacyCpnpIndexIsCpnp
? hist?.[LEGACY_CPNP_INDEX] ?? synced?.data?.[LEGACY_CPNP_INDEX]
: undefined);
if (legacyCpnp !== undefined && legacyCpnp !== null && String(legacyCpnp).trim() !== '') {
finalRow[COLUMNS.CPNP_NO] = legacyCpnp;
}
@@ -370,9 +389,17 @@ export default function App() {
}
}
// Categorisation Code: Excel value is the base, internal app edits win
if (excelCategorisationIdx >= 0 && excelCategorisationIdx !== COLUMNS.CATEGORIZATION_CODE) {
const internalCat = finalRow[COLUMNS.CATEGORIZATION_CODE];
if (internalCat === undefined || internalCat === null || String(internalCat).trim() === '') {
finalRow[COLUMNS.CATEGORIZATION_CODE] = row[excelCategorisationIdx];
}
}
// Force stock 0 for specific SKUs (User request)
if (FORCED_ZERO_STOCK_SKUS.has(articleNo)) {
finalRow[COLUMNS.ITEM_AVAILABLE] = 0;
finalRow[resolvedCols.ITEM_AVAILABLE] = 0;
}
return finalRow.map((val: any, idx: number) => {
@@ -820,6 +847,7 @@ export default function App() {
synced: [] as string[],
failed: [] as string[],
skippedEmptyCategorization: [] as string[],
warnings: [] as string[],
};
try {
const syncColumns = resolveColumnIndices(appState.headers);
@@ -896,6 +924,9 @@ export default function App() {
});
setRowStatuses(prev => ({ ...prev, [articleNo]: 'synced' }));
summary.synced.push(articleNo);
if (retryResult.warning) {
summary.warnings.push(`${articleNo}: ${retryResult.warning}`);
}
continue;
}
@@ -932,15 +963,26 @@ export default function App() {
});
setRowStatuses(prev => ({ ...prev, [articleNo]: 'synced' }));
summary.synced.push(articleNo);
if (result.warning) {
summary.warnings.push(`${articleNo}: ${result.warning}`);
}
}
} finally {
setIsSyncingBC(false);
const summaryTitle = summary.failed.length > 0
? 'Business Central sync finished with errors.'
: summary.warnings.length > 0
? 'Business Central sync finished with warnings.'
: 'Business Central sync finished.';
const lines = [
`Business Central sync finished.`,
summaryTitle,
`Synced: ${summary.synced.length}`,
`Failed: ${summary.failed.length}`,
`Empty CategorizationCode skipped: ${summary.skippedEmptyCategorization.length}`,
];
if (summary.warnings.length > 0) {
lines.push('', 'Warnings (sync completed):', ...summary.warnings.slice(0, 10));
}
if (summary.failed.length > 0) {
lines.push('', 'Failures:', ...summary.failed.slice(0, 10));
}