82 lines
2.8 KiB
JavaScript
82 lines
2.8 KiB
JavaScript
// Split into inserts and updates
|
|
const insertsAndUpdates = batch.reduce((acc, po) => {
|
|
const key = `${po.po_id}-${po.pid}`;
|
|
if (existingPOMap.has(key)) {
|
|
const existing = existingPOMap.get(key);
|
|
// Check if any values are different
|
|
const hasChanges = columnNames.some(col => {
|
|
const newVal = po[col] ?? null;
|
|
const oldVal = existing[col] ?? null;
|
|
// Special handling for numbers to avoid type coercion issues
|
|
if (typeof newVal === 'number' && typeof oldVal === 'number') {
|
|
return Math.abs(newVal - oldVal) > 0.00001; // Allow for tiny floating point differences
|
|
}
|
|
// Special handling for receiving_history JSON
|
|
if (col === 'receiving_history') {
|
|
return JSON.stringify(newVal) !== JSON.stringify(oldVal);
|
|
}
|
|
return newVal !== oldVal;
|
|
});
|
|
|
|
if (hasChanges) {
|
|
console.log(`PO line changed: ${key}`, {
|
|
po_id: po.po_id,
|
|
pid: po.pid,
|
|
changes: columnNames.filter(col => {
|
|
const newVal = po[col] ?? null;
|
|
const oldVal = existing[col] ?? null;
|
|
if (typeof newVal === 'number' && typeof oldVal === 'number') {
|
|
return Math.abs(newVal - oldVal) > 0.00001;
|
|
}
|
|
if (col === 'receiving_history') {
|
|
return JSON.stringify(newVal) !== JSON.stringify(oldVal);
|
|
}
|
|
return newVal !== oldVal;
|
|
})
|
|
});
|
|
acc.updates.push({
|
|
po_id: po.po_id,
|
|
pid: po.pid,
|
|
values: columnNames.map(col => po[col] ?? null)
|
|
});
|
|
}
|
|
} else {
|
|
console.log(`New PO line: ${key}`);
|
|
acc.inserts.push({
|
|
po_id: po.po_id,
|
|
pid: po.pid,
|
|
values: columnNames.map(col => po[col] ?? null)
|
|
});
|
|
}
|
|
return acc;
|
|
}, { inserts: [], updates: [] });
|
|
|
|
// Handle inserts
|
|
if (insertsAndUpdates.inserts.length > 0) {
|
|
const insertPlaceholders = Array(insertsAndUpdates.inserts.length).fill(placeholderGroup).join(",");
|
|
|
|
const insertResult = await localConnection.query(`
|
|
INSERT INTO purchase_orders (${columnNames.join(",")})
|
|
VALUES ${insertPlaceholders}
|
|
`, insertsAndUpdates.inserts.map(i => i.values).flat());
|
|
|
|
recordsAdded += insertResult[0].affectedRows;
|
|
}
|
|
|
|
// Handle updates
|
|
if (insertsAndUpdates.updates.length > 0) {
|
|
const updatePlaceholders = Array(insertsAndUpdates.updates.length).fill(placeholderGroup).join(",");
|
|
|
|
const updateResult = await localConnection.query(`
|
|
INSERT INTO purchase_orders (${columnNames.join(",")})
|
|
VALUES ${updatePlaceholders}
|
|
ON DUPLICATE KEY UPDATE
|
|
${columnNames
|
|
.filter(col => col !== "po_id" && col !== "pid")
|
|
.map(col => `${col} = VALUES(${col})`)
|
|
.join(",")};
|
|
`, insertsAndUpdates.updates.map(u => u.values).flat());
|
|
|
|
// Each update affects 2 rows in affectedRows, so we divide by 2 to get actual count
|
|
recordsUpdated += insertsAndUpdates.updates.length;
|
|
}
|