Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import re
|
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
import itertools
|
| 8 |
-
import glob
|
| 9 |
from collections import defaultdict
|
| 10 |
|
| 11 |
def clean_amount(val):
|
|
@@ -35,7 +34,6 @@ def extract_branch_advanced(text, assignment, assignment_clean, till_map, mid_ma
|
|
| 35 |
return till_map[asg_clean_str]
|
| 36 |
|
| 37 |
if "BARCLAYCARD" in text_str.upper():
|
| 38 |
-
# Captures the '1' as part of the 7-digit MID following 'STL0'
|
| 39 |
mid_match = re.search(r'STL0(\d{7})', text_str)
|
| 40 |
if mid_match:
|
| 41 |
mid_number = mid_match.group(1)
|
|
@@ -62,7 +60,7 @@ def extract_branch_advanced(text, assignment, assignment_clean, till_map, mid_ma
|
|
| 62 |
return None
|
| 63 |
|
| 64 |
def process_reconciliation(file_main, file_mapping, file_mid, start_match_id):
|
| 65 |
-
"""High-speed reconciliation engine featuring
|
| 66 |
try:
|
| 67 |
if file_main is None or file_mapping is None:
|
| 68 |
raise ValueError("Missing file upload. Please provide both the Ledger and TILL mapping files.")
|
|
@@ -82,14 +80,12 @@ def process_reconciliation(file_main, file_mapping, file_mid, start_match_id):
|
|
| 82 |
|
| 83 |
till_map = dict(zip(mapping_df['Assignment_clean'], mapping_df['Branch_clean']))
|
| 84 |
|
| 85 |
-
# Parse Barclaycard BPS layout using skiprows to bypass top-level metadata lines
|
| 86 |
mid_map = {}
|
| 87 |
if file_mid is not None:
|
| 88 |
try:
|
| 89 |
mid_df = pd.read_csv(file_mid.name, skiprows=4)
|
| 90 |
mid_df.columns = mid_df.columns.str.strip()
|
| 91 |
|
| 92 |
-
# Direct target mapping definitions matching your specific file headers
|
| 93 |
mid_col = "Outlet Number" if "Outlet Number" in mid_df.columns else None
|
| 94 |
br_col = "Customer Reference No" if "Customer Reference No" in mid_df.columns else None
|
| 95 |
|
|
@@ -99,7 +95,7 @@ def process_reconciliation(file_main, file_mapping, file_mid, start_match_id):
|
|
| 99 |
b_id = str(row[br_col]).split('.')[0].strip().zfill(4)
|
| 100 |
mid_map[m_id] = b_id
|
| 101 |
except Exception as e:
|
| 102 |
-
print(f"Internal Note: Handled
|
| 103 |
|
| 104 |
extracted_branches = []
|
| 105 |
for idx, row in main_df.iterrows():
|
|
@@ -186,7 +182,7 @@ def process_reconciliation(file_main, file_mapping, file_mid, start_match_id):
|
|
| 186 |
cb_branch_window_pool[br].append(cb_row)
|
| 187 |
|
| 188 |
# -------------------------------------------------------------------------
|
| 189 |
-
# PASS 2: MAIN BALANCING CORE (DAILY BUNDLES &
|
| 190 |
# -------------------------------------------------------------------------
|
| 191 |
for idx, sa_row in statements.iterrows():
|
| 192 |
branch = str(sa_row['Extracted_Branch'])
|
|
@@ -198,6 +194,7 @@ def process_reconciliation(file_main, file_mapping, file_mid, start_match_id):
|
|
| 198 |
sa_text = str(sa_row['Item Text']).upper()
|
| 199 |
|
| 200 |
is_barclaycard = "BARCLAYCARD" in sa_text
|
|
|
|
| 201 |
days_span = 6 if is_barclaycard else 3
|
| 202 |
lookback_start = sa_date - pd.offsets.BDay(days_span)
|
| 203 |
|
|
@@ -241,7 +238,7 @@ def process_reconciliation(file_main, file_mapping, file_mid, start_match_id):
|
|
| 241 |
break
|
| 242 |
curr_date -= pd.Timedelta(days=1)
|
| 243 |
|
| 244 |
-
# Many-to-One Window Pooling Strategy
|
| 245 |
if not matched_subset:
|
| 246 |
all_branch_rows = cb_branch_window_pool.get(branch, [])
|
| 247 |
window_rows = [
|
|
|
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
import itertools
|
|
|
|
| 8 |
from collections import defaultdict
|
| 9 |
|
| 10 |
def clean_amount(val):
|
|
|
|
| 34 |
return till_map[asg_clean_str]
|
| 35 |
|
| 36 |
if "BARCLAYCARD" in text_str.upper():
|
|
|
|
| 37 |
mid_match = re.search(r'STL0(\d{7})', text_str)
|
| 38 |
if mid_match:
|
| 39 |
mid_number = mid_match.group(1)
|
|
|
|
| 60 |
return None
|
| 61 |
|
| 62 |
def process_reconciliation(file_main, file_mapping, file_mid, start_match_id):
|
| 63 |
+
"""High-speed reconciliation engine featuring an expanded 6-day lookback window for credit card delay streams."""
|
| 64 |
try:
|
| 65 |
if file_main is None or file_mapping is None:
|
| 66 |
raise ValueError("Missing file upload. Please provide both the Ledger and TILL mapping files.")
|
|
|
|
| 80 |
|
| 81 |
till_map = dict(zip(mapping_df['Assignment_clean'], mapping_df['Branch_clean']))
|
| 82 |
|
|
|
|
| 83 |
mid_map = {}
|
| 84 |
if file_mid is not None:
|
| 85 |
try:
|
| 86 |
mid_df = pd.read_csv(file_mid.name, skiprows=4)
|
| 87 |
mid_df.columns = mid_df.columns.str.strip()
|
| 88 |
|
|
|
|
| 89 |
mid_col = "Outlet Number" if "Outlet Number" in mid_df.columns else None
|
| 90 |
br_col = "Customer Reference No" if "Customer Reference No" in mid_df.columns else None
|
| 91 |
|
|
|
|
| 95 |
b_id = str(row[br_col]).split('.')[0].strip().zfill(4)
|
| 96 |
mid_map[m_id] = b_id
|
| 97 |
except Exception as e:
|
| 98 |
+
print(f"Internal Note: Handled header shift: {str(e)}")
|
| 99 |
|
| 100 |
extracted_branches = []
|
| 101 |
for idx, row in main_df.iterrows():
|
|
|
|
| 182 |
cb_branch_window_pool[br].append(cb_row)
|
| 183 |
|
| 184 |
# -------------------------------------------------------------------------
|
| 185 |
+
# PASS 2: MAIN BALANCING CORE (DAILY BUNDLES & EXTENDED LOOKBACK WINDOWS)
|
| 186 |
# -------------------------------------------------------------------------
|
| 187 |
for idx, sa_row in statements.iterrows():
|
| 188 |
branch = str(sa_row['Extracted_Branch'])
|
|
|
|
| 194 |
sa_text = str(sa_row['Item Text']).upper()
|
| 195 |
|
| 196 |
is_barclaycard = "BARCLAYCARD" in sa_text
|
| 197 |
+
# EXPANDED TRACKING: 6 full working days for Barclaycard, 3 for standard cashbook items
|
| 198 |
days_span = 6 if is_barclaycard else 3
|
| 199 |
lookback_start = sa_date - pd.offsets.BDay(days_span)
|
| 200 |
|
|
|
|
| 238 |
break
|
| 239 |
curr_date -= pd.Timedelta(days=1)
|
| 240 |
|
| 241 |
+
# Many-to-One Cross-Day Window Residual Pooling Strategy
|
| 242 |
if not matched_subset:
|
| 243 |
all_branch_rows = cb_branch_window_pool.get(branch, [])
|
| 244 |
window_rows = [
|