diff --git a/app/main.py b/app/main.py index adba633..c23331c 100644 --- a/app/main.py +++ b/app/main.py @@ -732,6 +732,19 @@ async def match_specific_receipts(request: MatchSpecificRequest, db: db_dependen filtered_results = [r for r in matching_results if r.confidence_score >= CONFIDENCE_THRESHOLD] logger.info(f"After filtering by {CONFIDENCE_THRESHOLD*100}% threshold: {len(filtered_results)} matches remain") + # Track matched transaction IDs + matched_transaction_ids = set() + for result in filtered_results: + if result.transaction: + matched_transaction_ids.add(result.transaction.id) + + # Find unmatched transactions + unmatched_transactions = [ + txn for txn in transactions + if txn.id not in matched_transaction_ids + ] + logger.info(f"Found {len(unmatched_transactions)} unmatched transactions") + # Convert matching results to response format match_responses = [] for result in filtered_results: @@ -811,6 +824,40 @@ async def match_specific_receipts(request: MatchSpecificRequest, db: db_dependen ) match_responses.append(match_response) + # Add unmatched transactions as MatchResponse objects with empty receipt data + for txn in unmatched_transactions: + unmatched_match = MatchResponse( + receipt_id="", + transaction_id=txn.id, + confidence_score=0.0, + match_reason="Unmatched transaction", + receipt_vendor="", + receipt_amount=0.0, + receipt_description="", + receipt_category="", + receipt_tax_amount=0.0, + transaction_vendor=txn.vendor, + transaction_amount=txn.amount, + tax_analysis=None, + flag_for_review=None, + auto_approve=None, + transaction_source=txn.source, + TxnId=txn.TxnId, + AccountType=txn.AccountType, + AccountNumber=txn.AccountNumber, + TransactionDate=txn.TransactionDate, + TransactionType=txn.TransactionType, + ChequeNumber=txn.ChequeNumber, + Description1=txn.Description1, + Description2=txn.Description2, + VendorId=txn.VendorId, + VendorName=txn.VendorName, + AccountId=txn.AccountId, + AccountName=txn.AccountName, + Source=txn.source, + ) + match_responses.append(unmatched_match) + # Calculate statistics on filtered results high_confidence = len( [r for r in filtered_results if r.confidence_score >= 0.8] @@ -827,15 +874,17 @@ async def match_specific_receipts(request: MatchSpecificRequest, db: db_dependen stats = { "total": len(match_responses), + "matched": len(filtered_results), + "unmatched_transactions": len(unmatched_transactions), "high_confidence": high_confidence, "low_confidence": low_confidence, "avg_score": round(avg_score, 2), } logger.info(f"Generated stats: {stats}") - logger.info(f"Match responses: {match_responses}") + logger.info(f"Total responses (matched + unmatched): {len(match_responses)}") logger.info( - f"Match-specific completed successfully with {len(match_responses)} matches" + f"Match-specific completed successfully with {len(filtered_results)} matches and {len(unmatched_transactions)} unmatched transactions" ) return MatchingResponse(matches=match_responses, stats=stats)