u
This commit is contained in:
@@ -153,10 +153,27 @@ export async function executeCompanyImport({
|
||||
// MARK: - Phase 4: Orders / Receipts
|
||||
// #region Phase 4: Orders / Receipts
|
||||
const allArtIdToSku = new Map();
|
||||
const parentSkus = new Set(articlesResult.parentSkus || []);
|
||||
const parentIds = new Set();
|
||||
for (const a of artRecords) {
|
||||
const kArt = cleanValue(a.id ?? a.kArtikel);
|
||||
const sku = cleanValue(a.sku ?? a.cArtNr);
|
||||
if (kArt && sku) allArtIdToSku.set(kArt, sku);
|
||||
|
||||
const pid = cleanValue(a.parentid ?? a.kVaterArtikel);
|
||||
if (pid && pid !== '0') parentIds.add(pid);
|
||||
const isParent = a.is_parent === '1' || a.nIstVater === '1';
|
||||
if (isParent) {
|
||||
if (kArt) parentIds.add(kArt);
|
||||
if (sku) parentSkus.add(sku);
|
||||
}
|
||||
}
|
||||
for (const a of artRecords) {
|
||||
const kArt = cleanValue(a.id ?? a.kArtikel);
|
||||
if (kArt && parentIds.has(kArt)) {
|
||||
const sku = cleanValue(a.sku ?? a.cArtNr);
|
||||
if (sku) parentSkus.add(sku);
|
||||
}
|
||||
}
|
||||
|
||||
const ordersResult = await importOrders({
|
||||
@@ -165,6 +182,7 @@ export async function executeCompanyImport({
|
||||
manRecords,
|
||||
itemRecords,
|
||||
jtlArtIdToSku: allArtIdToSku,
|
||||
parentSkus,
|
||||
progressTracker,
|
||||
});
|
||||
// #endregion
|
||||
|
||||
@@ -18,6 +18,7 @@ const log = createLogger('import');
|
||||
* @param {Array<Record<string, string>>} params.manRecords
|
||||
* @param {Array<Record<string, string>>} params.itemRecords
|
||||
* @param {Map<string, string>} [params.jtlArtIdToSku]
|
||||
* @param {Set<string>} [params.parentSkus]
|
||||
* @param {object} params.progressTracker
|
||||
* @returns {Promise<{ ordersImported: number, ordersSkipped: number, paymentsImported: number, shippingsImported: number }>}
|
||||
*/
|
||||
@@ -27,6 +28,7 @@ export async function importOrders({
|
||||
manRecords,
|
||||
itemRecords,
|
||||
jtlArtIdToSku = new Map(),
|
||||
parentSkus = new Set(),
|
||||
progressTracker,
|
||||
}) {
|
||||
let ordersImported = 0;
|
||||
@@ -157,7 +159,10 @@ export async function importOrders({
|
||||
|
||||
const lines = rawItems.map((it) => {
|
||||
const articleId = cleanValue(it.articleid ?? it.kArtikel);
|
||||
const sku = cleanValue(it.sku ?? it.cArtNr) || (articleId && jtlArtIdToSku?.get?.(articleId)) || null;
|
||||
let sku = cleanValue(it.sku ?? it.cArtNr) || (articleId && jtlArtIdToSku?.get?.(articleId)) || null;
|
||||
if (sku && parentSkus.has(sku)) {
|
||||
sku = null;
|
||||
}
|
||||
const rawName = cleanValue(it.name ?? it.cName ?? it.cNameStandard);
|
||||
const name = rawName || (sku ? `Artikel ${sku}` : 'Artikel');
|
||||
const amount = parseGermanFloat(it.amount ?? it.fAnzahl, 1);
|
||||
@@ -198,7 +203,30 @@ export async function importOrders({
|
||||
existingOrderNumbers.add(orderNumber);
|
||||
ordersImported++;
|
||||
} catch (err) {
|
||||
log.warn(`Failed to create order ${orderNumber}:`, err.message);
|
||||
const parentMatch = err.message?.match(/A parent article cannot be booked on documents \(([^)]+)\)/);
|
||||
if (parentMatch) {
|
||||
const offendingSku = parentMatch[1];
|
||||
parentSkus.add(offendingSku);
|
||||
for (const l of lines) {
|
||||
if (l.sku === offendingSku) l.sku = null;
|
||||
}
|
||||
try {
|
||||
createdOrder = await store.order.create(googleAuthId, {
|
||||
number: orderNumber,
|
||||
externalNumber: cleanValue(m.extnr ?? m.cExterneAuftragsnummer) || null,
|
||||
date: validDate,
|
||||
createdAt: validDate,
|
||||
entityId: entityId || undefined,
|
||||
lines,
|
||||
});
|
||||
existingOrderNumbers.add(orderNumber);
|
||||
ordersImported++;
|
||||
} catch (retryErr) {
|
||||
log.warn(`Failed to create order ${orderNumber} after stripping parent SKU ${offendingSku}:`, retryErr.message);
|
||||
}
|
||||
} else {
|
||||
log.warn(`Failed to create order ${orderNumber}:`, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
if (createdOrder) {
|
||||
|
||||
Reference in New Issue
Block a user