u
This commit is contained in:
@@ -153,10 +153,27 @@ export async function executeCompanyImport({
|
|||||||
// MARK: - Phase 4: Orders / Receipts
|
// MARK: - Phase 4: Orders / Receipts
|
||||||
// #region Phase 4: Orders / Receipts
|
// #region Phase 4: Orders / Receipts
|
||||||
const allArtIdToSku = new Map();
|
const allArtIdToSku = new Map();
|
||||||
|
const parentSkus = new Set(articlesResult.parentSkus || []);
|
||||||
|
const parentIds = new Set();
|
||||||
for (const a of artRecords) {
|
for (const a of artRecords) {
|
||||||
const kArt = cleanValue(a.id ?? a.kArtikel);
|
const kArt = cleanValue(a.id ?? a.kArtikel);
|
||||||
const sku = cleanValue(a.sku ?? a.cArtNr);
|
const sku = cleanValue(a.sku ?? a.cArtNr);
|
||||||
if (kArt && sku) allArtIdToSku.set(kArt, sku);
|
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({
|
const ordersResult = await importOrders({
|
||||||
@@ -165,6 +182,7 @@ export async function executeCompanyImport({
|
|||||||
manRecords,
|
manRecords,
|
||||||
itemRecords,
|
itemRecords,
|
||||||
jtlArtIdToSku: allArtIdToSku,
|
jtlArtIdToSku: allArtIdToSku,
|
||||||
|
parentSkus,
|
||||||
progressTracker,
|
progressTracker,
|
||||||
});
|
});
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ const log = createLogger('import');
|
|||||||
* @param {Array<Record<string, string>>} params.manRecords
|
* @param {Array<Record<string, string>>} params.manRecords
|
||||||
* @param {Array<Record<string, string>>} params.itemRecords
|
* @param {Array<Record<string, string>>} params.itemRecords
|
||||||
* @param {Map<string, string>} [params.jtlArtIdToSku]
|
* @param {Map<string, string>} [params.jtlArtIdToSku]
|
||||||
|
* @param {Set<string>} [params.parentSkus]
|
||||||
* @param {object} params.progressTracker
|
* @param {object} params.progressTracker
|
||||||
* @returns {Promise<{ ordersImported: number, ordersSkipped: number, paymentsImported: number, shippingsImported: number }>}
|
* @returns {Promise<{ ordersImported: number, ordersSkipped: number, paymentsImported: number, shippingsImported: number }>}
|
||||||
*/
|
*/
|
||||||
@@ -27,6 +28,7 @@ export async function importOrders({
|
|||||||
manRecords,
|
manRecords,
|
||||||
itemRecords,
|
itemRecords,
|
||||||
jtlArtIdToSku = new Map(),
|
jtlArtIdToSku = new Map(),
|
||||||
|
parentSkus = new Set(),
|
||||||
progressTracker,
|
progressTracker,
|
||||||
}) {
|
}) {
|
||||||
let ordersImported = 0;
|
let ordersImported = 0;
|
||||||
@@ -157,7 +159,10 @@ export async function importOrders({
|
|||||||
|
|
||||||
const lines = rawItems.map((it) => {
|
const lines = rawItems.map((it) => {
|
||||||
const articleId = cleanValue(it.articleid ?? it.kArtikel);
|
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 rawName = cleanValue(it.name ?? it.cName ?? it.cNameStandard);
|
||||||
const name = rawName || (sku ? `Artikel ${sku}` : 'Artikel');
|
const name = rawName || (sku ? `Artikel ${sku}` : 'Artikel');
|
||||||
const amount = parseGermanFloat(it.amount ?? it.fAnzahl, 1);
|
const amount = parseGermanFloat(it.amount ?? it.fAnzahl, 1);
|
||||||
@@ -198,8 +203,31 @@ export async function importOrders({
|
|||||||
existingOrderNumbers.add(orderNumber);
|
existingOrderNumbers.add(orderNumber);
|
||||||
ordersImported++;
|
ordersImported++;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
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);
|
log.warn(`Failed to create order ${orderNumber}:`, err.message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (createdOrder) {
|
if (createdOrder) {
|
||||||
// Handle Payment
|
// Handle Payment
|
||||||
|
|||||||
Reference in New Issue
Block a user