Improve transaction deduplication logic
Limit deduplication to transactions of the relevant jobs.
This commit is contained in:
@@ -68,16 +68,7 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
|
||||
parsed.totalAmount.toString(),
|
||||
parsed.buyer,
|
||||
parsed.location
|
||||
].join('|');
|
||||
console.log('Created key from parsed transaction:', {
|
||||
key,
|
||||
date: normalizeDate(parsed.date.toISOString()),
|
||||
itemName: parsed.itemName,
|
||||
quantity: parsed.quantity,
|
||||
totalAmount: parsed.totalAmount,
|
||||
buyer: parsed.buyer,
|
||||
location: parsed.location
|
||||
});
|
||||
].join('|');
|
||||
return key;
|
||||
};
|
||||
|
||||
@@ -90,15 +81,6 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
|
||||
tx.buyer,
|
||||
tx.location
|
||||
].join('|');
|
||||
console.log('Created key from existing transaction:', {
|
||||
key,
|
||||
date: normalizeDate(tx.date),
|
||||
itemName: tx.itemName,
|
||||
quantity: tx.quantity,
|
||||
totalPrice: tx.totalPrice,
|
||||
buyer: tx.buyer,
|
||||
location: tx.location
|
||||
});
|
||||
return key;
|
||||
};
|
||||
|
||||
@@ -107,19 +89,15 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
|
||||
const lines = value.trim().split('\n');
|
||||
const pasteTransactionMap = new Map<string, ParsedTransaction>();
|
||||
|
||||
console.log('=== STEP 1: Processing pasted lines ===');
|
||||
// STEP 1: First combine all identical transactions within the pasted data
|
||||
lines.forEach((line, index) => {
|
||||
lines.forEach((line) => {
|
||||
const parsed = parseTransactionLine(line);
|
||||
if (parsed) {
|
||||
const transactionKey = createTransactionKey(parsed);
|
||||
console.log(`Line ${index + 1}: Key = ${transactionKey}`);
|
||||
|
||||
// Check if this exact transaction already exists in our paste data
|
||||
if (pasteTransactionMap.has(transactionKey)) {
|
||||
// Merge with existing transaction in paste
|
||||
const existing = pasteTransactionMap.get(transactionKey)!;
|
||||
console.log(`Merging duplicate: adding ${parsed.quantity} to existing ${existing.quantity}`);
|
||||
existing.quantity += parsed.quantity;
|
||||
existing.totalPrice += Math.abs(parsed.totalAmount);
|
||||
} else {
|
||||
@@ -136,31 +114,36 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
|
||||
wallet: parsed.wallet
|
||||
};
|
||||
pasteTransactionMap.set(transactionKey, newTransaction);
|
||||
console.log(`Added new transaction: ${parsed.itemName} x${parsed.quantity}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
console.log('=== STEP 2: Checking against existing transactions ===');
|
||||
// STEP 2: Now check combined transactions against existing job transactions
|
||||
const existingTransactionKeys = new Set<string>();
|
||||
eligibleJobs.forEach(job => {
|
||||
job.income.forEach(tx => {
|
||||
const key = createTransactionKeyFromRecord(tx);
|
||||
existingTransactionKeys.add(key);
|
||||
console.log(`Existing transaction key: ${key}`);
|
||||
});
|
||||
// STEP 2: Identify which jobs these transactions belong to
|
||||
const relevantJobIds = new Set<string>();
|
||||
pasteTransactionMap.forEach((transaction) => {
|
||||
const matchingJobId = findMatchingJob(transaction.itemName);
|
||||
if (matchingJobId) {
|
||||
relevantJobIds.add(matchingJobId);
|
||||
}
|
||||
});
|
||||
|
||||
// STEP 3: Only check against transactions from relevant jobs
|
||||
const existingTransactionKeys = new Set<string>();
|
||||
eligibleJobs.forEach(job => {
|
||||
if (relevantJobIds.has(job.id)) {
|
||||
job.income.forEach(tx => {
|
||||
const key = createTransactionKeyFromRecord(tx);
|
||||
existingTransactionKeys.add(key);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// STEP 4: Mark duplicates and assign jobs
|
||||
let duplicates = 0;
|
||||
console.log('=== STEP 3: Marking duplicates and assigning jobs ===');
|
||||
// Update each combined transaction with duplicate status and job assignment
|
||||
pasteTransactionMap.forEach((transaction, key) => {
|
||||
const isDuplicate = existingTransactionKeys.has(key);
|
||||
transaction.isDuplicate = isDuplicate;
|
||||
|
||||
console.log(`Transaction ${transaction.itemName}: isDuplicate = ${isDuplicate}`);
|
||||
|
||||
if (isDuplicate) {
|
||||
duplicates++;
|
||||
transaction.assignedJobId = undefined;
|
||||
|
Reference in New Issue
Block a user