diff --git a/src/components/BatchTransactionForm.tsx b/src/components/BatchTransactionForm.tsx index 26229ef..c7747c8 100644 --- a/src/components/BatchTransactionForm.tsx +++ b/src/components/BatchTransactionForm.tsx @@ -105,28 +105,13 @@ const BatchTransactionForm: React.FC = ({ onClose, on const handlePaste = (value: string) => { setPastedData(value); const lines = value.trim().split('\n'); - const transactions: ParsedTransaction[] = []; - const seenTransactions = new Set(); const pasteTransactionMap = new Map(); - // Pre-populate seenTransactions with existing transactions from jobs - eligibleJobs.forEach(job => { - job.income.forEach(tx => { - const key = createTransactionKeyFromRecord(tx); - seenTransactions.add(key); - }); - }); - - let duplicates = 0; + // STEP 1: First combine all identical transactions within the pasted data lines.forEach((line, index) => { const parsed = parseTransactionLine(line); if (parsed) { const transactionKey = createTransactionKey(parsed); - const isDuplicate = seenTransactions.has(transactionKey); - - if (isDuplicate) { - duplicates++; - } // Check if this exact transaction already exists in our paste data if (pasteTransactionMap.has(transactionKey)) { @@ -145,20 +130,37 @@ const BatchTransactionForm: React.FC = ({ onClose, on buyer: parsed.buyer, location: parsed.location, corporation: parsed.corporation, - wallet: parsed.wallet, - assignedJobId: !isDuplicate ? findMatchingJob(parsed.itemName) : undefined, - isDuplicate + wallet: parsed.wallet }; pasteTransactionMap.set(transactionKey, newTransaction); - - if (!isDuplicate) { - seenTransactions.add(transactionKey); - } } } }); - // Convert map to array for display - each transaction is individual + // STEP 2: Now check combined transactions against existing job transactions + const existingTransactionKeys = new Set(); + eligibleJobs.forEach(job => { + job.income.forEach(tx => { + const key = createTransactionKeyFromRecord(tx); + existingTransactionKeys.add(key); + }); + }); + + let duplicates = 0; + // Update each combined transaction with duplicate status and job assignment + pasteTransactionMap.forEach((transaction, key) => { + const isDuplicate = existingTransactionKeys.has(key); + transaction.isDuplicate = isDuplicate; + + if (isDuplicate) { + duplicates++; + transaction.assignedJobId = undefined; + } else { + transaction.assignedJobId = findMatchingJob(transaction.itemName); + } + }); + + // Convert map to array for display const transactionList = Array.from(pasteTransactionMap.values()); setDuplicatesFound(duplicates);