File size: 3,752 Bytes
8e65d73
 
74f57ab
8e65d73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74f57ab
8e65d73
 
 
 
 
 
 
 
 
74f57ab
8e65d73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74f57ab
8e65d73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74f57ab
 
 
8e65d73
 
 
74f57ab
8e65d73
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import os
from models import FraudData

DATA_FILE = 'data.json'

def load_receipts():
    """Load all stored receipts from data.json."""
    if not os.path.exists(DATA_FILE):
        print("Data file doesn't exist, returning empty list")
        return []
    
    try:
        with open(DATA_FILE, 'r') as f:
            data = json.load(f)
            print(f"Loaded data type: {type(data)}")
            
            # Handle case where data.json contains a single object instead of array
            if isinstance(data, dict):
                print("Data is a single dict, wrapping in array")
                return [data]
            elif isinstance(data, list):
                print(f"Data is a list with {len(data)} items")
                return data
            else:
                print("Data is neither dict nor list, returning empty list")
                return []
    except Exception as e:
        print(f"Error loading receipts: {e}")
        return []

def save_receipts(receipts):
    """Save receipts back to data.json."""
    with open(DATA_FILE, 'w') as f:
        json.dump(receipts, f, indent=4)

def receipts_are_equal(receipt1, receipt2):
    """Check if two receipts are the same, comparing only date and amount."""
    print(f"\n=== COMPARING RECEIPTS ===")
    print(f"Receipt1 merchant: {receipt1.get('merchant')}")
    print(f"Receipt2 merchant: {receipt2.get('merchant')}")
    
    # Check if both receipts exist
    if not receipt1 or not receipt2:
        print("One or both receipts are empty")
        return False
    
    # Only compare date and amount
    date1 = receipt1.get('date')
    date2 = receipt2.get('date')
    amount1 = receipt1.get('total_amount')
    amount2 = receipt2.get('total_amount')
    
    print(f"Comparing date: '{date1}' vs '{date2}'")
    print(f"Comparing total_amount: '{amount1}' vs '{amount2}'")
    
    if date1 != date2:
        print("Date field doesn't match")
        return False
    
    if amount1 != amount2:
        print("Total amount field doesn't match")
        return False
    
    print("Date and amount match - receipts are equal")
    return True

def is_duplicate(new_receipt, receipts):
    """Return True if new_receipt is already in receipts."""
    print(f"\n=== CHECKING FOR DUPLICATES ===")
    print(f"Checking new receipt against {len(receipts)} existing receipts")
    
    for i, old_receipt in enumerate(receipts):
        print(f"\nChecking against receipt {i}:")
        if receipts_are_equal(old_receipt, new_receipt):
            print(f"DUPLICATE FOUND at index {i}!")
            return True
    
    print("No duplicates found")
    return False

def process_receipt(new_receipt):
    print(f"\n=== PROCESSING RECEIPT ===")
    # Ensure we're working with a dict, not a string
    if isinstance(new_receipt, str):
        try:
            new_receipt = json.loads(new_receipt)
        except:
            return {"error": "Invalid receipt format"}
    
    receipts = load_receipts()
    print(f"Loaded {len(receipts)} existing receipts")
    
    if is_duplicate(new_receipt, receipts):
        print("SETTING FRAUD CHECK TO TRUE")
        # Create FraudData object and convert to dict
        fraud_data = FraudData(fraud_detected=True, fraud_type="duplicate")
        new_receipt['fraud_check'] = [fraud_data.dict()]
        # Do not save, just return
    else:
        print("SETTING FRAUD CHECK TO FALSE - SAVING RECEIPT")
        new_receipt['fraud_check'] = []  # Empty list means no fraud detected
        receipts.append(new_receipt)
        save_receipts(receipts)
    
    return new_receipt

# ---- Usage ----
# new_receipt = { ... }  # Your receipt JSON here
# result = process_receipt(new_receipt)
# print(result)