File size: 5,944 Bytes
dcc24f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
FinEE Normalizer - Data normalization utilities.

Handles normalization of:
- Amounts (₹2,500.00 → 2500.0)
- Dates (various formats → DD-MM-YYYY)
- Account numbers (masking, formatting)
- Reference numbers (padding)
"""

import re
from datetime import datetime, date
from typing import Optional, Union
from dateutil import parser as date_parser


def normalize_amount(amount_str: Union[str, float, int, None]) -> Optional[float]:
    """
    Normalize amount string to float.
    
    Handles:
    - Currency symbols (Rs., ₹, INR)
    - Commas (2,500.00)
    - Spaces (Rs. 2 500)
    
    Args:
        amount_str: Amount in various formats
        
    Returns:
        Float amount or None if parsing fails
    """
    if amount_str is None:
        return None
    
    if isinstance(amount_str, (int, float)):
        return float(amount_str)
    
    if not isinstance(amount_str, str):
        return None
    
    # Remove currency symbols (specific prefixes)
    cleaned = amount_str.strip()
    cleaned = re.sub(r'^(?:Rs\.?|INR|₹)\s*', '', cleaned, flags=re.IGNORECASE)
    
    # Remove commas
    cleaned = cleaned.replace(',', '')
    
    # Handle Indian lakhs/crores notation (if present)
    cleaned = cleaned.replace(' ', '')
    
    try:
        return float(cleaned)
    except ValueError:
        return None


def normalize_date(date_str: Optional[str], output_format: str = '%d-%m-%Y') -> Optional[str]:
    """
    Normalize date string to standard format.
    
    Handles:
    - DD-MM-YY, DD-MM-YYYY
    - DD/MM/YY, DD/MM/YYYY
    - DD Mon YYYY (28 Dec 2025)
    - YYYY-MM-DD (ISO format)
    
    Args:
        date_str: Date in various formats
        output_format: Output format (default: DD-MM-YYYY)
        
    Returns:
        Normalized date string or None if parsing fails
    """
    if not date_str:
        return None
    
    # Clean input
    date_str = date_str.strip()
    
    # Common Indian date formats to try
    formats = [
        '%d-%m-%Y',      # 28-12-2025
        '%d-%m-%y',      # 28-12-25
        '%d/%m/%Y',      # 28/12/2025
        '%d/%m/%y',      # 28/12/25
        '%d %b %Y',      # 28 Dec 2025
        '%d %b %y',      # 28 Dec 25
        '%d %B %Y',      # 28 December 2025
        '%d %B %y',      # 28 December 25
        '%Y-%m-%d',      # 2025-12-28 (ISO)
        '%d.%m.%Y',      # 28.12.2025
        '%d.%m.%y',      # 28.12.25
    ]
    
    # Try each format
    for fmt in formats:
        try:
            parsed = datetime.strptime(date_str, fmt)
            
            # Handle 2-digit years (assume 20xx for years < 50)
            if parsed.year < 100:
                if parsed.year < 50:
                    parsed = parsed.replace(year=parsed.year + 2000)
                else:
                    parsed = parsed.replace(year=parsed.year + 1900)
            
            return parsed.strftime(output_format)
        except ValueError:
            continue
    
    # Fallback to dateutil parser
    try:
        parsed = date_parser.parse(date_str, dayfirst=True)
        return parsed.strftime(output_format)
    except (ValueError, TypeError):
        return None


def normalize_account(account_str: Optional[str], mask: bool = False) -> Optional[str]:
    """
    Normalize account number.
    
    Args:
        account_str: Account number string
        mask: If True, mask all but last 4 digits
        
    Returns:
        Normalized account number
    """
    if not account_str:
        return None
    
    # Extract digits only
    digits = re.sub(r'\D', '', str(account_str))
    
    if not digits:
        return None
    
    if mask and len(digits) > 4:
        return '*' * (len(digits) - 4) + digits[-4:]
    
    return digits


def normalize_reference(ref_str: Optional[str]) -> Optional[str]:
    """
    Normalize transaction reference number.
    
    Args:
        ref_str: Reference number string
        
    Returns:
        Normalized reference number
    """
    if not ref_str:
        return None
    
    # Extract alphanumeric characters
    cleaned = re.sub(r'[^A-Za-z0-9]', '', str(ref_str))
    
    return cleaned if cleaned else None


def normalize_vpa(vpa_str: Optional[str]) -> Optional[str]:
    """
    Normalize UPI VPA.
    
    Args:
        vpa_str: VPA string
        
    Returns:
        Lowercase VPA
    """
    if not vpa_str:
        return None
    
    # Remove extra whitespace and lowercase
    cleaned = vpa_str.strip().lower()
    
    # Validate VPA format (should have @)
    if '@' not in cleaned:
        return None
    
    return cleaned


def normalize_merchant(merchant_str: Optional[str]) -> Optional[str]:
    """
    Normalize merchant name.
    
    Args:
        merchant_str: Merchant name string
        
    Returns:
        Cleaned merchant name
    """
    if not merchant_str:
        return None
    
    # Title case and clean
    cleaned = merchant_str.strip()
    
    # Remove common prefixes/suffixes
    prefixes = ['payment to', 'paid to', 'transfer to', 'upi-']
    for prefix in prefixes:
        if cleaned.lower().startswith(prefix):
            cleaned = cleaned[len(prefix):].strip()
    
    return cleaned if cleaned else None


def normalize_type(type_str: Optional[str]) -> Optional[str]:
    """
    Normalize transaction type.
    
    Args:
        type_str: Type string (debit/credit variants)
        
    Returns:
        'debit' or 'credit'
    """
    if not type_str:
        return None
    
    type_lower = str(type_str).lower().strip()
    
    debit_keywords = ['debit', 'debited', 'withdrawn', 'sent', 'paid', 'spent', 'purchase']
    credit_keywords = ['credit', 'credited', 'received', 'refund', 'cashback', 'reversed']
    
    for kw in debit_keywords:
        if kw in type_lower:
            return 'debit'
    
    for kw in credit_keywords:
        if kw in type_lower:
            return 'credit'
    
    return None