diff --git "a/src/streamlit_app.py" "b/src/streamlit_app.py"
--- "a/src/streamlit_app.py"
+++ "b/src/streamlit_app.py"
@@ -1,40 +1,3809 @@
-import altair as alt
-import numpy as np
-import pandas as pd
+# =========================
+# Invoice Extractor (Qwen3-VL via RunPod vLLM) - Batch Mode with Tax Validation
+# Updated code #2 with code #1 postprocessing logic
+# =========================
+import os
+from pathlib import Path
+
+# -----------------------------
+# Environment hardening (HF Spaces, /.cache issue)
+# -----------------------------
+_home = os.environ.get("HOME", "")
+if _home in ("", "/", None):
+ repo_dir = os.getcwd()
+ safe_home = repo_dir if os.access(repo_dir, os.W_OK) else "/tmp"
+ os.environ["HOME"] = safe_home
+ print(f"[startup] HOME not set or unwritable — setting HOME={safe_home}")
+
+streamlit_dir = Path(os.environ["HOME"]) / ".streamlit"
+try:
+ streamlit_dir.mkdir(parents=True, exist_ok=True)
+ print(f"[startup] ensured {streamlit_dir}")
+except Exception as e:
+ print(f"[startup] WARNING: could not create {streamlit_dir}: {e}")
+
+# -----------------------------
+# Imports
+# -----------------------------
+import json
+from io import BytesIO
+import hashlib
+from typing import Dict, Any, List
+from datetime import datetime, timedelta
+from copy import deepcopy
+
import streamlit as st
+import pandas as pd
+from PIL import Image
+
+try:
+ from pdf2image import convert_from_bytes
+except Exception:
+ convert_from_bytes = None
+
+import requests
+import base64
+import re
+
+# ── Pipeline integration setup ──────────────────────────────────────────────
+import sys
+from types import SimpleNamespace
+from decimal import Decimal
+
+_HERE_DIR = os.path.dirname(os.path.abspath(__file__))
+# Works both locally (Matching Functions/ subfolder) and on HF Spaces (flat src/)
+_MATCHING_DIR = (
+ os.path.join(_HERE_DIR, "Matching Functions")
+ if os.path.isdir(os.path.join(_HERE_DIR, "Matching Functions"))
+ else _HERE_DIR
+)
+
+_pipeline_available = False
+_pipeline_import_error = None
+try:
+ if _MATCHING_DIR not in sys.path:
+ sys.path.insert(0, _MATCHING_DIR)
+ from invoice_pipeline import run_invoice_pipeline
+ from two_way_match import InvoiceHeader, InvoiceLine, POHeader, POLine
+ from three_way_match import GRLineItem
+ from contract_match import ContractInvoiceHeader, ContractRecord, RateCardItem
+ _pipeline_available = True
+except Exception as _e:
+ _pipeline_import_error = str(_e)
+
+POD_URL = os.getenv("POD_URL", "")
+VLLM_API_KEY = os.getenv("VLLM_API_KEY", "")
+MODEL_NAME = "phase2-v1-merged"
+
+MAX_PAGES_PER_REQUEST = 10
+MAX_DIMENSION_PER_PAGE = 1024
+MAX_TOTAL_PAYLOAD_MB = 40.0
+
+# -----------------------------
+# Page config & CSS (must be the FIRST st.* call)
+# -----------------------------
+st.set_page_config(page_title="Invoice Extractor (Qwen3-VL) - Batch Mode", layout="wide")
+
+if not POD_URL or not VLLM_API_KEY:
+ st.error("⚠️ API credentials not configured. Please set POD_URL and VLLM_API_KEY in Space settings.")
+ st.stop()
+st.title("Invoice Extraction")
+
+st.markdown(
+ """
+
+
+ """,
+ unsafe_allow_html=True
+)
+
+DATA_EDITOR_HEIGHT = 380
+
+# -----------------------------
+# Helpers — numeric/date cleaning from code #1
+# -----------------------------
+def ensure_state(k: str, default):
+ """Initialize a session_state key once, then let widgets bind to it via key=... (no value=...)."""
+ if k not in st.session_state:
+ st.session_state[k] = default
+
+def parse_time_to_minutes(x) -> float:
+ """
+ Parse time format quantities to minutes.
+
+ Examples:
+ "0:35" → 35.0 (0 hours, 35 minutes = 35 minutes)
+ "1:30" → 90.0 (1 hour, 30 minutes = 90 minutes)
+ "2:15" → 135.0 (2 hours, 15 minutes = 135 minutes)
+ "0:05" → 5.0 (5 minutes)
+ "123" → 123.0 (regular number, not time format)
+ "1.5" → 1.5 (regular decimal, not time format)
+ """
+ if x is None:
+ return 0.0
+ if isinstance(x, (int, float)):
+ return float(x)
+
+ s = str(x).strip()
+ if s == "":
+ return 0.0
+
+ # Check if it's in time format (H:MM or HH:MM)
+ time_pattern = r'^(\d+):(\d{1,2})$'
+ match = re.match(time_pattern, s)
+
+ if match:
+ hours = int(match.group(1))
+ minutes = int(match.group(2))
+ total_minutes = (hours * 60) + minutes
+ return float(total_minutes)
+
+ # Not time format, treat as regular number
+ return 0.0
+
+def clean_quantity(x) -> float:
+ """
+ Parse quantity - handles both time format (H:MM) and regular numbers.
+
+ Examples:
+ "0:35" → 35.0 (time format: 35 minutes)
+ "1:30" → 90.0 (time format: 90 minutes)
+ "123" → 123.0 (regular number)
+ "1,234.56" → 1234.56 (US format with decimals)
+ "1.234,56" → 1234.56 (EUR format with decimals)
+ """
+ if x is None:
+ return 0.0
+ if isinstance(x, (int, float)):
+ return float(x)
+
+ s = str(x).strip()
+ if s == "":
+ return 0.0
+
+ # First check if it's time format (H:MM or HH:MM)
+ time_value = parse_time_to_minutes(s)
+ if time_value > 0.0:
+ return time_value
+
+ # Not time format, use regular number parsing
+ return clean_float(s)
+
+def clean_tax_percentage(x) -> float:
+ """
+ Parse tax percentage - ALWAYS treats periods as decimals, never as thousands.
+
+ Tax percentages are typically: 8.875, 19.5, 2.75, 0.0875, etc.
+ We should NEVER interpret "8.875" as "8875"
+
+ Examples:
+ "8.875" → 8.875 (decimal percentage)
+ "8,875" → 8.875 (European decimal)
+ "19.5" → 19.5 (standard decimal)
+ "2,75" → 2.75 (European decimal)
+ """
+ if x is None:
+ return 0.0
+ if isinstance(x, (int, float)):
+ return float(x)
+
+ s = str(x).strip()
+ if s == "":
+ return 0.0
+
+ # Handle negative signs (could be leading or trailing)
+ is_negative = False
+ if s.startswith('-'):
+ is_negative = True
+ s = s[1:].strip()
+ elif s.endswith('-'):
+ is_negative = True
+ s = s[:-1].strip()
+ elif s.startswith('(') and s.endswith(')'):
+ is_negative = True
+ s = s[1:-1].strip()
+
+ # Remove percent signs, currency symbols and spaces
+ s = re.sub(r'[%€$£¥₹\s]', '', s)
+
+ if s == "":
+ return 0.0
+
+ # Count occurrences
+ comma_count = s.count(',')
+ period_count = s.count('.')
+
+ # For tax percentages, logic is simpler:
+ # - If both comma and period exist, the LAST one is decimal separator
+ # - If only comma exists, it's decimal separator (European style)
+ # - If only period exists, it's ALWAYS decimal separator (no thousands logic)
+
+ if comma_count > 0 and period_count > 0:
+ # Both present - LAST one is decimal
+ last_comma = s.rfind(',')
+ last_period = s.rfind('.')
+
+ if last_comma > last_period:
+ # European: 1.234,56 → comma is decimal
+ s = s.replace('.', '').replace(',', '.')
+ else:
+ # US: 1,234.56 → period is decimal
+ s = s.replace(',', '')
+
+ elif comma_count > 0:
+ # Only comma - treat as European decimal separator
+ s = s.replace(',', '.')
+
+ # If only period(s) exist, keep as-is (always decimal for tax percentages)
+
+ # Clean any remaining non-numeric characters except period and minus
+ s = re.sub(r'[^\d.]', '', s)
+
+ if s == "" or s == ".":
+ return 0.0
+
+ try:
+ result = float(s)
+ return -result if is_negative else result
+ except ValueError:
+ return 0.0
+
+def clean_float(x, currency=None) -> float:
+ """
+ Parse a number string handling both US and European formats.
+ Use this for MONETARY AMOUNTS only, NOT for tax percentages.
+ US Format: 1,234,567.89 (comma = thousands, period = decimal)
+ European: 1.234.567,89 (period = thousands, comma = decimal)
+ Currency-aware for ambiguous cases (3 digits after comma):
+ - EUR: "10,000" → 10.0 (European decimal)
+ - USD/other: "10,000" → 10000 (thousands separator)
+ Examples:
+ "1,234.56" → 1234.56 (US)
+ "1.234,56" → 1234.56 (European)
+ "3.000,2234" → 3000.2234 (European with 4 decimal places)
+ "261,49" → 261.49 (European decimal only)
+ "39,22-" → -39.22 (European with trailing minus)
+ """
+ if x is None:
+ return 0.0
+ if isinstance(x, (int, float)):
+ return float(x)
+
+ s = str(x).strip()
+ if s == "":
+ return 0.0
+
+ # Handle negative signs (could be leading or trailing)
+ is_negative = False
+ if s.startswith('-'):
+ is_negative = True
+ s = s[1:].strip()
+ elif s.endswith('-'):
+ is_negative = True
+ s = s[:-1].strip()
+ elif s.startswith('(') and s.endswith(')'):
+ # Accounting format: (123.45) means negative
+ is_negative = True
+ s = s[1:-1].strip()
+
+ # Remove currency symbols and spaces
+ s = re.sub(r'[€$£¥₹\s]', '', s)
+
+ if s == "":
+ return 0.0
+
+ # Count occurrences
+ comma_count = s.count(',')
+ period_count = s.count('.')
+
+ # Find positions of last comma and last period
+ last_comma = s.rfind(',')
+ last_period = s.rfind('.')
+
+ # Determine format based on which separator comes last
+ if comma_count > 0 and period_count > 0:
+ # Both separators present - the LAST one is the decimal separator
+ if last_comma > last_period:
+ # European format: 1.234,56 → comma is decimal
+ # Remove periods (thousands), replace comma with period
+ s = s.replace('.', '').replace(',', '.')
+ else:
+ # US format: 1,234.56 → period is decimal
+ # Remove commas (thousands)
+ s = s.replace(',', '')
+
+ elif comma_count > 0 and period_count == 0:
+ # Only commas present
+ # Check what comes after the LAST comma
+ after_last_comma = s[last_comma + 1:] if last_comma < len(s) - 1 else ""
+
+ if comma_count == 1 and len(after_last_comma) == 3 and after_last_comma.isdigit():
+ # AMBIGUOUS CASE: Single comma with exactly 3 digits after
+ # Could be US thousands ("10,000" = 10000) or European decimal ("10,000" = 10.0)
+ # Use currency to decide:
+ if currency and currency.upper() == 'EUR':
+ # European: treat comma as decimal separator
+ # "10,000" → 10.000 → 10.0
+ s = s.replace(',', '.')
+ else:
+ # USD/other: treat comma as thousands separator
+ # "10,000" → 10000
+ s = s.replace(',', '')
+ elif comma_count == 1 and len(after_last_comma) <= 4 and after_last_comma.isdigit():
+ # Single comma with 1, 2, or 4 digits after → European decimal
+ # "261,49" → 261.49, "1234,5678" → 1234.5678
+ s = s.replace(',', '.')
+ elif len(after_last_comma) == 3 and comma_count >= 1:
+ # Multiple commas with 3 digits after last → thousands separator
+ # "1,234,567" → 1234567
+ s = s.replace(',', '')
+ else:
+ # Multiple commas → thousands separator
+ # "1,234,567" → 1234567
+ s = s.replace(',', '')
+
+ elif period_count > 0 and comma_count == 0:
+ # Only periods present
+ # Check what comes after the LAST period
+ after_last_period = s[last_period + 1:] if last_period < len(s) - 1 else ""
+
+ if period_count > 1:
+ # Multiple periods → definitely thousands separator (European: "1.234.567")
+ s = s.replace('.', '')
+ elif len(after_last_period) == 3 and after_last_period.isdigit():
+ # Single period with exactly 3 digits after
+ before_period = s[:last_period]
+ # Only treat as thousands if there are 2+ digits before period
+ # "10.000" or "123.000" → thousands
+ # "8.875" or "9.123" → decimal
+ if before_period.isdigit() and len(before_period) >= 2 and len(before_period) <= 3:
+ s = s.replace('.', '') # European thousands: "10.000" → 10000
+ # Otherwise keep as decimal: "8.875" → 8.875
+ # Otherwise keep as is (standard decimal like "1.50", "123.45")
+
+ # Clean any remaining non-numeric characters except period and minus
+ s = re.sub(r'[^\d.]', '', s)
+
+ if s == "" or s == ".":
+ return 0.0
+
+ try:
+ result = float(s)
+ return -result if is_negative else result
+ except ValueError:
+ return 0.0
+
+DECIMAL_DISPLAY_PLACES = 3
+TAX_RATE_CLOSE_CALL_TOLERANCE = 0.01
+SUMMARY_NUMERIC_FIELDS = {
+ "Subtotal",
+ "Tax Percentage",
+ "Total Tax",
+ "Discount Rate",
+ "Total Discount Amount",
+ "Total Amount",
+}
+TAX_RATE_FIELDS = {"Tax Percentage", "Tax Rate Per Item"}
+ITEM_NUMERIC_FIELDS = {
+ "Quantity",
+ "Unit Price",
+ "Amount",
+ "Discount Rate Per Item",
+ "Discount Amount Per Item",
+ "Tax Rate Per Item",
+ "Tax Amount Per Item",
+ "Line Total",
+ "Tax",
+ "line_total",
+}
+EDITOR_NUMERIC_COLUMNS = [
+ "Quantity",
+ "Unit Price",
+ "Amount",
+ "Discount Rate Per Item",
+ "Discount Amount Per Item",
+ "Tax Rate Per Item",
+ "Tax Amount Per Item",
+ "Line Total",
+]
+
+def is_blank_numeric_value(value) -> bool:
+ if value is None:
+ return True
+ try:
+ if bool(pd.isna(value)):
+ return True
+ except (TypeError, ValueError):
+ pass
+ return isinstance(value, str) and value.strip() == ""
+
+def limit_decimal_places(value, places: int = DECIMAL_DISPLAY_PLACES):
+ """Round numeric display/export values to at most `places` decimals."""
+ if is_blank_numeric_value(value):
+ return ""
+ rounded = round(clean_float(value), places)
+ if rounded == 0:
+ return 0.0
+ return rounded
+
+def normalize_tax_rate_close_call(value):
+ """Snap near-whole tax rates like 19.99/20.002/20.01 to 20."""
+ if is_blank_numeric_value(value):
+ return ""
+ rate = clean_tax_percentage(value)
+ if is_blank_numeric_value(rate):
+ return ""
+ nearest_whole = round(rate)
+ if abs(rate - nearest_whole) <= TAX_RATE_CLOSE_CALL_TOLERANCE + 1e-9:
+ return float(nearest_whole)
+ return limit_decimal_places(rate)
+
+def editable_numeric_value(field: str, value):
+ if field in TAX_RATE_FIELDS:
+ normalized = normalize_tax_rate_close_call(value)
+ else:
+ normalized = limit_decimal_places(value)
+ if normalized == "":
+ return 0.0
+ return float(normalized)
+
+def coerce_numeric_items_df(df: pd.DataFrame) -> pd.DataFrame:
+ """Keep editable item numeric columns numeric before save/export."""
+ out = df.copy() if isinstance(df, pd.DataFrame) else pd.DataFrame()
+ for col in EDITOR_NUMERIC_COLUMNS:
+ if col not in out.columns:
+ out[col] = 0.0
+ if col in TAX_RATE_FIELDS:
+ out[col] = out[col].apply(normalize_tax_rate_close_call)
+ else:
+ out[col] = out[col].apply(limit_decimal_places)
+ out[col] = pd.to_numeric(out[col], errors="coerce").fillna(0.0).astype(float)
+ return out
+
+def editor_dataframes_equal(left: pd.DataFrame, right: pd.DataFrame) -> bool:
+ """Stable equality check for deciding whether to commit data_editor output."""
+ if not isinstance(left, pd.DataFrame) or not isinstance(right, pd.DataFrame):
+ return False
+ left_norm = left.reset_index(drop=True).fillna("").astype(str)
+ right_norm = right.reset_index(drop=True).fillna("").astype(str)
+ return left_norm.equals(right_norm)
+
+def apply_data_editor_state(base_df: pd.DataFrame, editor_state) -> pd.DataFrame:
+ """Apply Streamlit data_editor edited/added/deleted row state to a base df."""
+ if isinstance(editor_state, pd.DataFrame):
+ return coerce_numeric_items_df(editor_state)
+ if not isinstance(editor_state, dict):
+ return coerce_numeric_items_df(base_df)
+
+ out = base_df.copy() if isinstance(base_df, pd.DataFrame) else pd.DataFrame()
+
+ for row_idx, updates in (editor_state.get("edited_rows") or {}).items():
+ try:
+ idx = int(row_idx)
+ except (TypeError, ValueError):
+ continue
+ if idx < 0 or idx >= len(out) or not isinstance(updates, dict):
+ continue
+ for col, value in updates.items():
+ if col in out.columns:
+ out.at[out.index[idx], col] = value
+
+ deleted_rows = sorted(
+ [int(i) for i in (editor_state.get("deleted_rows") or []) if str(i).lstrip("-").isdigit()],
+ reverse=True,
+ )
+ if deleted_rows:
+ drop_labels = [out.index[i] for i in deleted_rows if 0 <= i < len(out)]
+ out = out.drop(index=drop_labels)
+
+ added_rows = editor_state.get("added_rows") or []
+ if added_rows:
+ out = pd.concat([out, pd.DataFrame(added_rows)], ignore_index=True)
+
+ return coerce_numeric_items_df(out.reset_index(drop=True))
+
+def commit_data_editor_state(items_state_key: str, editor_key: str, selected_hash: str) -> None:
+ """Commit data_editor deltas to durable session state before rerun."""
+ committed = apply_data_editor_state(
+ st.session_state.get(items_state_key, pd.DataFrame()),
+ st.session_state.get(editor_key),
+ )
+ st.session_state[items_state_key] = committed
+ live_edited_data = deepcopy(st.session_state.batch_results[selected_hash].get("edited_data", {}) or {})
+ live_edited_data["Itemized Data"] = committed.to_dict("records")
+ st.session_state.batch_results[selected_hash]["edited_data"] = live_edited_data
+
+def limit_invoice_decimals(data: dict) -> dict:
+ """Apply the 3-decimal display cap only to known numeric invoice fields."""
+ for field in SUMMARY_NUMERIC_FIELDS:
+ if field in data:
+ if field in TAX_RATE_FIELDS:
+ data[field] = normalize_tax_rate_close_call(data.get(field))
+ else:
+ data[field] = limit_decimal_places(data.get(field))
+
+ for item in data.get("Itemized Data", []) or []:
+ if not isinstance(item, dict):
+ continue
+ for field in ITEM_NUMERIC_FIELDS:
+ if field in item:
+ if field in TAX_RATE_FIELDS:
+ item[field] = normalize_tax_rate_close_call(item.get(field))
+ else:
+ item[field] = limit_decimal_places(item.get(field))
+ return data
+
+def normalize_date(date_str, currency=None) -> str:
+ """
+ Normalize various date formats:
+ - Full dates (day-month-year) → dd-MMM-yyyy (e.g., 01-Jan-2025)
+ - Month-year only → MMM-yyyy (e.g., Aug-2025)
+
+ Currency-aware parsing:
+ - If currency is USD and date is numeric format (11/09/2025, 11-09-2025),
+ treat as MM/DD/YYYY
+ - For text formats (06-Nov-2025, December 6, 2025), parse normally
+
+ Returns empty string if date cannot be parsed
+ """
+ if not date_str or date_str == "":
+ return ""
+
+ if isinstance(date_str, str):
+ date_str = date_str.strip()
+ if date_str == "":
+ return ""
+
+ # EXTRA CLEANING: Replace various unicode spaces and clean up
+ # Non-breaking space, thin space, etc. → regular space
+ date_str = re.sub(r'[\u00A0\u2000-\u200B\u202F\u205F\u3000]', ' ', date_str)
+ # Remove zero-width characters
+ date_str = re.sub(r'[\u200B-\u200D\uFEFF]', '', date_str)
+ # Normalize multiple spaces to single space
+ date_str = re.sub(r'\s+', ' ', date_str).strip()
+
+ # Clean ordinal suffixes FIRST (1st, 2nd, 3rd, 4th, 06th, etc.)
+ cleaned_date = date_str
+ if isinstance(date_str, str):
+ # Handle ordinals: "06th December 2025" → "06 December 2025"
+ # Also handles: "December 6th, 2025" → "December 6, 2025"
+ cleaned_date = re.sub(r'(\d+)(st|nd|rd|th)\b', r'\1', date_str, flags=re.IGNORECASE)
+
+ # Check if date is NUMERIC format (contains only digits and separators)
+ # Pattern: XX/XX/XXXX, XX-XX-XXXX, XX.XX.XXXX (with 2 or 4 digit year)
+ is_numeric_format = bool(re.match(r'^\d{1,2}[/\-\.]\d{1,2}[/\-\.]\d{2,4}$', cleaned_date))
+
+ # US FORMAT PRIORITY: If currency is USD and date is numeric, try MM/DD/YYYY first
+ if currency and currency.upper() == 'USD' and is_numeric_format:
+ us_formats = [
+ "%m/%d/%Y", # 01/15/2025
+ "%m-%d-%Y", # 01-15-2025
+ "%m.%d.%Y", # 01.15.2025
+ "%m/%d/%y", # 01/15/25
+ "%m-%d-%y", # 01-15-25
+ "%m.%d.%y", # 01.15.25
+ ]
+ for fmt in us_formats:
+ try:
+ parsed_date = datetime.strptime(cleaned_date, fmt)
+ return parsed_date.strftime("%d-%b-%Y")
+ except (ValueError, TypeError):
+ continue
+
+ # FULL DATE FORMATS (day-month-year) - standard parsing
+ full_date_formats = [
+ # ISO formats (4-digit year) - these are unambiguous
+ "%Y-%m-%d", # 2025-01-15
+ "%Y/%m/%d", # 2025/01/15
+ "%Y.%m.%d", # 2025.01.15
+ "%Y %m %d", # 2025 01 15
+ "%Y%m%d", # 20250115 (compact)
+
+ # European formats with full month names (4-digit year) - UNAMBIGUOUS
+ "%d %B, %Y", # 15 December, 2025 (with comma)
+ "%d %b, %Y", # 15 Dec, 2025 (with comma)
+ "%d %B %Y", # 15 January 2025
+ "%d %b %Y", # 15 Jan 2025
+ "%d-%B-%Y", # 15-January-2025
+ "%d-%b-%Y", # 15-Jan-2025
+ "%d.%B.%Y", # 15.January.2025
+ "%d.%b.%Y", # 15.Jan.2025
+ "%d/%B/%Y", # 15/January/2025
+ "%d/%b/%Y", # 15/Jan/2025
+
+ # US formats with full month names (4-digit year) - UNAMBIGUOUS
+ "%B %d, %Y", # January 15, 2025
+ "%b %d, %Y", # Jan 15, 2025
+ "%B %d %Y", # January 15 2025
+ "%b %d %Y", # Jan 15 2025
+ "%B-%d-%Y", # January-15-2025
+ "%b-%d-%Y", # Jan-15-2025
+ "%B %d,%Y", # January 15,2025 (no space after comma)
+ "%b %d,%Y", # Jan 15,2025
+
+ # European formats - Day first (4-digit year)
+ "%d-%m-%Y", # 15-01-2025
+ "%d/%m/%Y", # 15/01/2025
+ "%d.%m.%Y", # 15.01.2025
+ "%d %m %Y", # 15 01 2025
+
+ # US formats - Month first (4-digit year) - only if not USD or not numeric
+ "%m-%d-%Y", # 01-15-2025
+ "%m/%d/%Y", # 01/15/2025
+ "%m.%d.%Y", # 01.15.2025
+ "%m %d %Y", # 01 15 2025
+
+ # European formats with 2-digit year - Day first
+ "%d-%m-%y", # 15-01-25
+ "%d/%m/%y", # 15/01/25
+ "%d.%m.%y", # 15.01.25
+ "%d %m %y", # 15 01 25
+
+ # US formats with 2-digit year - Month first
+ "%m-%d-%y", # 01-15-25
+ "%m/%d/%y", # 01/15/25
+ "%m.%d.%y", # 01.15.25
+ "%m %d %y", # 01 15 25
+
+ # ISO with 2-digit year
+ "%y-%m-%d", # 25-01-15
+ "%y/%m/%d", # 25/01/15
+ "%y.%m.%d", # 25.01.15
+ "%y %m %d", # 25 01 15
+
+ # Compact formats with 2-digit year
+ "%y%m%d", # 250115
+ "%d%m%y", # 150125
+ "%m%d%y", # 011525
+
+ # European formats with abbreviated month (2-digit year) - UNAMBIGUOUS
+ "%d %B, %y", # 15 December, 25 (with comma)
+ "%d %b, %y", # 15 Dec, 25 (with comma)
+ "%d-%b-%y", # 15-Jan-25
+ "%d/%b/%y", # 15/Jan/25
+ "%d.%b.%y", # 15.Jan.25
+ "%d %b %y", # 15 Jan 25
+ "%d-%B-%y", # 15-January-25
+ "%d/%B/%y", # 15/January/25
+
+ # US formats with abbreviated month (2-digit year) - UNAMBIGUOUS
+ "%b %d, %y", # Jan 15, 25
+ "%b %d %y", # Jan 15 25
+ "%B %d, %y", # January 15, 25
+ "%B %d %y", # January 15 25
+ "%b-%d-%y", # Jan-15-25
+ "%B-%d-%y", # January-15-25
+
+ # Compact 8-digit formats
+ "%d%m%Y", # 15012025
+ "%m%d%Y", # 01152025
+ "%Y%d%m", # 20251501
+ ]
+
+ # Try full date formats → output as dd-MMM-yyyy
+ for fmt in full_date_formats:
+ try:
+ parsed_date = datetime.strptime(cleaned_date, fmt)
+ return parsed_date.strftime("%d-%b-%Y")
+ except (ValueError, TypeError):
+ continue
+
+ # MONTH-YEAR ONLY FORMATS - output as MMM-yyyy
+ month_year_formats = [
+ # Full month name with year
+ "%B %Y", # August 2025
+ "%b %Y", # Aug 2025
+ "%B, %Y", # August, 2025
+ "%b, %Y", # Aug, 2025
+ "%B-%Y", # August-2025
+ "%b-%Y", # Aug-2025
+ "%B/%Y", # August/2025
+ "%b/%Y", # Aug/2025
+
+ # Numeric month-year (4-digit year)
+ "%m/%Y", # 08/2025
+ "%m-%Y", # 08-2025
+ "%m.%Y", # 08.2025
+ "%m %Y", # 08 2025
+ "%Y-%m", # 2025-08
+ "%Y/%m", # 2025/08
+ "%Y.%m", # 2025.08
+ "%Y %m", # 2025 08
+
+ # Numeric month-year (2-digit year)
+ "%m/%y", # 08/25
+ "%m-%y", # 08-25
+ "%m.%y", # 08.25
+ "%m %y", # 08 25
+ "%y-%m", # 25-08
+ "%y/%m", # 25/08
+
+ # Full month name with 2-digit year
+ "%B %y", # August 25
+ "%b %y", # Aug 25
+ "%B-%y", # August-25
+ "%b-%y", # Aug-25
+ ]
+
+ # Try month-year formats → output as MMM-yyyy (no day)
+ for fmt in month_year_formats:
+ try:
+ parsed_date = datetime.strptime(cleaned_date, fmt)
+ return parsed_date.strftime("%b-%Y") # Aug-2025 format
+ except (ValueError, TypeError):
+ continue
+
+ # If no format matched, return empty string
+ return ""
+
+def parse_date_to_object(date_str, currency=None):
+ """
+ Parse a date string to a datetime.date object for date_input widget
+ Currency-aware: If USD and numeric format, treat as MM/DD/YYYY
+ Returns None if date cannot be parsed
+ """
+ if not date_str or date_str == "":
+ return None
+
+ if isinstance(date_str, str):
+ date_str = date_str.strip()
+ if date_str == "":
+ return None
+
+ # EXTRA CLEANING: Replace various unicode spaces and clean up
+ date_str = re.sub(r'[\u00A0\u2000-\u200B\u202F\u205F\u3000]', ' ', date_str)
+ date_str = re.sub(r'[\u200B-\u200D\uFEFF]', '', date_str)
+ date_str = re.sub(r'\s+', ' ', date_str).strip()
+
+ # Clean ordinal suffixes FIRST (1st, 2nd, 3rd, 4th, 06th, etc.)
+ cleaned_date = str(date_str)
+ if isinstance(date_str, str):
+ cleaned_date = re.sub(r'(\d+)(st|nd|rd|th)\b', r'\1', date_str, flags=re.IGNORECASE)
+
+ # Check if date is NUMERIC format (contains only digits and separators)
+ is_numeric_format = bool(re.match(r'^\d{1,2}[/\-\.]\d{1,2}[/\-\.]\d{2,4}$', cleaned_date))
+
+ # US FORMAT PRIORITY: If currency is USD and date is numeric, try MM/DD/YYYY first
+ if currency and currency.upper() == 'USD' and is_numeric_format:
+ us_formats = [
+ "%m/%d/%Y", # 01/15/2025
+ "%m-%d-%Y", # 01-15-2025
+ "%m.%d.%Y", # 01.15.2025
+ "%m/%d/%y", # 01/15/25
+ "%m-%d-%y", # 01-15-25
+ "%m.%d.%y", # 01.15.25
+ ]
+ for fmt in us_formats:
+ try:
+ parsed_date = datetime.strptime(cleaned_date, fmt)
+ return parsed_date.date()
+ except (ValueError, TypeError):
+ continue
+
+ # Standard formats
+ formats = [
+ # ISO formats (4-digit year)
+ "%Y-%m-%d", "%Y/%m/%d", "%Y.%m.%d", "%Y %m %d", "%Y%m%d",
+
+ # Text month formats with comma - MUST BE FIRST for "06 December, 2025"
+ "%d %B, %Y", "%d %b, %Y", # 06 December, 2025 / 06 Dec, 2025
+
+ # Text month formats - UNAMBIGUOUS
+ "%d %B %Y", "%d %b %Y", "%d-%B-%Y", "%d-%b-%Y",
+ "%d.%B.%Y", "%d.%b.%Y", "%d/%B/%Y", "%d/%b/%Y",
+ "%B %d, %Y", "%b %d, %Y", "%B %d %Y", "%b %d %Y",
+ "%B-%d-%Y", "%b-%d-%Y", "%B %d,%Y", "%b %d,%Y",
+
+ # European formats - Day first
+ "%d-%m-%Y", "%d/%m/%Y", "%d.%m.%Y", "%d %m %Y",
+ "%d-%m-%y", "%d/%m/%y", "%d.%m.%y", "%d %m %y",
+
+ # US formats - Month first
+ "%m-%d-%Y", "%m/%d/%Y", "%m.%d.%Y", "%m %d %Y",
+ "%m-%d-%y", "%m/%d/%y", "%m.%d.%y", "%m %d %y",
+
+ # ISO with 2-digit year
+ "%y-%m-%d", "%y/%m/%d", "%y.%m.%d", "%y %m %d",
+
+ # Compact formats
+ "%y%m%d", "%d%m%y", "%m%d%y", "%d%m%Y", "%m%d%Y", "%Y%d%m",
+
+ # Text month with 2-digit year (with comma)
+ "%d %B, %y", "%d %b, %y", # 06 December, 25 / 06 Dec, 25
+ "%d-%b-%y", "%d/%b/%y", "%d.%b.%y", "%d %b %y",
+ "%d-%B-%y", "%d/%B/%y",
+ "%b %d, %y", "%b %d %y", "%B %d, %y", "%B %d %y",
+ "%b-%d-%y", "%B-%d-%y",
+
+ # Month-year only
+ "%B %Y", "%b %Y", "%B, %Y", "%b, %Y",
+ "%B-%Y", "%b-%Y", "%B/%Y", "%b/%Y",
+ "%m/%Y", "%m-%Y", "%m.%Y", "%m %Y",
+ "%Y-%m", "%Y/%m", "%Y.%m", "%Y %m",
+ "%m/%y", "%m-%y", "%m.%y", "%m %y",
+ "%y-%m", "%y/%m",
+ "%B %y", "%b %y", "%B-%y", "%b-%y",
+ ]
+
+ for fmt in formats:
+ try:
+ parsed_date = datetime.strptime(cleaned_date, fmt)
+ return parsed_date.date()
+ except (ValueError, TypeError):
+ continue
+
+ return None
+
+
+# =============================================================================
+# vLLM Inference — MULTI-IMAGE SINGLE REQUEST
+# =============================================================================
+def _encode_page(page: Image.Image, page_idx: int, max_dim: int = MAX_DIMENSION_PER_PAGE):
+ orig_w, orig_h = page.size
+ api_img = page.copy()
+ if orig_w > max_dim or orig_h > max_dim:
+ ratio = min(max_dim / orig_w, max_dim / orig_h)
+ new_size = (int(orig_w * ratio), int(orig_h * ratio))
+ api_img = api_img.resize(new_size, Image.Resampling.LANCZOS)
+ else:
+ new_size = (orig_w, orig_h)
+ buf = BytesIO()
+ api_img.save(buf, format="PNG", optimize=True)
+ b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
+ data_url = f"data:image/png;base64,{b64}"
+ return data_url, (orig_w, orig_h), new_size
+
+
+def run_inference_vllm(images: List[Image.Image]) -> str | None:
+ EXTRACTION_PROMPT = """Extract structured data from the provided invoice image(s). Return a single JSON object with three sections: header, items, summary.
+## Output Schema
+Return this exact structure. Use empty string "" for any field not found.
+json
+{
+ "header": {
+ "invoice_no": "",
+ "po_no": "",
+ "invoice_date": "",
+ "payment_terms": "",
+ "due_date": "",
+ "sender_name": "",
+ "sender_addr": "",
+ "tax_id": "",
+ "rcpt_name": "",
+ "rcpt_addr": "",
+ "bank_iban": "",
+ "bank_name": "",
+ "bank_acc_no": "",
+ "bank_routing": "",
+ "bank_swift": "",
+ "bank_acc_name": "",
+ "bank_branch": ""
+ },
+ "items": [
+ {
+ "service_name": "",
+ "service_start_date": "",
+ "service_end_date": "",
+ "descriptions": "",
+ "SKU": "",
+ "quantity": "",
+ "unit_price": "",
+ "amount": "",
+ "discount_rate_per_item": "",
+ "discount_amount_per_item": "",
+ "tax_rate_per_item": "",
+ "tax_amount_per_item": "",
+ "line_total": ""
+ }
+ ],
+ "summary": {
+ "subtotal": "",
+ "tax_rate": "",
+ "tax_amount": "",
+ "discount_rate": "",
+ "total_discount_amount": "",
+ "total_amount": "",
+ "currency": ""
+ }
+}
+## Core Rules
+1. EXTRACT ONLY WHAT IS EXPLICITLY VISIBLE. Never compute, derive, infer totals, or fill missing values via arithmetic. The following format normalizations and explicit-data operations ARE permitted:
+ - Bracketed amounts → negative: (100.00) → "-100.00".
+ - Date ranges → expanded to full start/end dates using the invoice year.
+ - Language normalization of payment terms → English.
+ - Summing multiple tax AMOUNTS shown separately into one combined amount.
+ - Summing multiple discount AMOUNTS shown separately into one combined amount.
+ - Default quantity to "1.00" when unit_price and amount are both shown and equal, with no quantity printed.
+ - Setting tax to "0.00" when "No VAT" / "VAT Exempt" is explicitly stated, OR when a tax rate is shown but tax amount is blank AND subtotal equals total as printed.
+2. Return "" for any field not present or not clearly identifiable.
+3. PRESERVE ORIGINAL FORMATTING for numbers (commas, decimals) and dates (except for date-range expansion noted above). For tax_rate and discount_rate, preserve exactly as printed including any % symbol.
+4. STRIP CURRENCY SYMBOLS from all amount/price fields. Place currency code (USD, EUR, GBP, INR, etc.) only in summary.currency. If multiple currencies appear, use the currency of total_amount. If no currency is shown anywhere on the invoice, set summary.currency to "".
+5. MULTI-PAGE: Merge all pages into one JSON object. Extract every line item as printed — do not deduplicate. Only skip a row if it is clearly a continuation header (e.g., the row repeats the table column titles like "Description / Qty / Price").
+6. NO DUPLICATION ACROSS SCHEMA SECTIONS: If a value appears in the line items area of the invoice, place it only in the items array of the schema. If it appears in the summary/totals area (the section at the bottom showing subtotal, tax, discount, and total — separate from the itemized table), place it only in the summary object of the schema. Do not copy the same value into both unless the invoice explicitly prints it in both areas.
+7. OUTPUT ONLY THE JSON. No preamble, no markdown fences, no explanation.
+8. INCLUDE ALL LINE ITEMS, even those with a zero amount, zero quantity, or negative amount. Never skip a line item because its value is 0 or negative. This applies equally to credit notes and adjustments. Negative quantities, if printed, are extracted as-is (preserve the negative sign).
+9. NO LINE ITEMS: If the invoice shows no individual line items (e.g., a flat-fee invoice with only a total), return an empty array: "items": [].
+10. CONFLICTING VALUES: If the invoice shows two different totals (e.g., "Total" and "Amount Due"), prefer the value labeled as the final payable amount ("Amount Due", "Balance Due", "Total Payable", "Grand Total").
+## Field Rules
+### Sender & Recipient
+1.sender_name: The invoicing company/person. Prefer company name over person name.
+2.sender_addr: The full address of the invoicing entity, wherever it appears on the invoice.
+3.tax_id: Always the SENDER's tax ID / VAT number. Never the recipient's.
+4.rcpt_name: The name of the recipient, customer, or entity being billed.
+5.rcpt_addr: The address of the recipient, customer, or entity being billed.
+### PO Number
+1.po_no: Extract if a single PO number is shown. If the same PO number appears multiple times on the invoice (e.g., header and footer), extract it once. If multiple DISTINCT PO numbers are present, return "".
+### Bank Details
+1.Prefer the bank account matching the invoice currency (USD invoice → USD account).
+2.If no currency-matching account exists, extract the only available account.
+3.If multiple non-matching accounts exist with no currency-matching account, return "" for all bank fields.
+4.bank_acc_name: The name of the account holder. It may also appear in phrases such as “Cheques should be payable to” or “Remittance should be payable to.” Extract the mentioned company/entity name as the bank_acc_name.
+### Dates
+5.Preserve the original date format exactly as printed.
+6.Date range exception: When a service period is shown as a range (e.g., "Feb 1-3"), expand to two full dates: service_start_date: "Feb 1 2026", service_end_date: "Feb 3 2026". Use the invoice year if the range omits a year.
+7.due_date: If multiple due dates, extract only the first. If payment terms are "due on receipt" / "due on presentation" / "immediate", set due_date = invoice_date.
+8.payment_terms: Normalize to English ("30 jours" → "30 days", "60 gg df" → "60 days").
+### Descriptions
+1.service_name: A short label, product name, or service title (e.g., "Consulting", "Web Hosting", "Premium Plan").
+2.descriptions: The full detailed description text of the line item.
+3.If the line item shows both a short label and a longer expanded text → put the short label in service_name and the longer text in descriptions.
+4.If only one text is available for the line item → place it in descriptions and leave service_name as "".
+5.If a SKU/code appears together with descriptive text (e.g., "SKU-12345 — Premium Web Hosting") → put the SKU in SKU, the descriptive portion in descriptions, and leave service_name as "" unless a separate short label also exists.
+6.Do not extract handwritten annotations or stamps unless they are clearly legible and printed-quality. When in doubt, extract only the printed text.
+### Amounts, Tax & Discounts
+1.Preserve original number formatting (commas, decimals) exactly as printed. Do not reinterpret decimal separators — preserve as written (European 1.234,56 stays 1.234,56; US 1,234.56 stays 1,234.56).
+2.Bracketed amounts are negative: (100.00) → "-100.00".
+3."No VAT" / "VAT Exempt" → set relevant tax field to "0.00".
+4.Tax rate shown but tax amount blank AND subtotal equals total as printed → tax amount = "0.00".
+5.Tax-inclusive pricing: If the invoice states "all prices include VAT" or "tax-inclusive pricing", extract amount as printed without separating tax. Extract summary.tax_amount, summary.tax_rate, tax_rate_per_item, and tax_amount_per_item exactly as printed on the invoice — do not attempt to back-calculate.
+6.Summing multiple values:
+ - Multiple tax AMOUNTS shown separately → sum into one combined amount in the appropriate field.
+ - Multiple DISCOUNT AMOUNTS shown separately → sum into one combined amount in the appropriate field.
+ - Multiple tax RATES (e.g., 5% CGST + 12% SGST) → do NOT sum. Concatenate as printed (e.g., "5% + 12%") or extract the dominant rate as printed. Never produce a summed rate like "17%".
+7.0Gross vs. Net amount columns: If both gross and net per-line amounts are shown, use the NET (pre-tax) amount as amount.
+8.Discount/tax as a line item: If discount or tax appears as a row in the line items table — identified by labels such as "Discount", "Rebate", "Tax", "VAT", "Adjustment", or by being a negative-value row with no quantity/unit price — create a separate entry in the items array. Populate ONLY descriptions, the relevant discount/tax fields (discount_rate_per_item, discount_amount_per_item, tax_rate_per_item, tax_amount_per_item), and line_total. Leave quantity, unit_price, and amount as "".
+9.Discount/tax in the totals/summary area: Place only in the summary object of the schema. Do not duplicate into the items array.
+### Quantity & Unit Price
+1.If unit_price and amount are shown and equal but no quantity is printed → quantity: "1.00".
+2.If the invoice uses alternate labels for quantity (e.g., impressions, units, hours, sessions, clicks) and unit price (e.g., CPM, rate, cost per unit), map them to quantity and unit_price respectively.
+3.If both a standard quantity and an alternate metric are shown, extract the standard quantity.
+4.Negative quantities (e.g., on credit notes) are preserved as-is with the negative sign.
+### Line Total
+line_total: Extract ONLY if ALL these conditions are met:
+ 1. The invoice explicitly prints a dedicated line total column or value for the row.
+ 2. The invoice has NO separate per-line tax column or per-line tax amount anywhere.
+ 3. The invoice has NO separate per-line discount column or per-line discount amount anywhere.
+If any per-line tax or discount column exists anywhere on the invoice → set line_total to "" for ALL rows.
+If no line total column exists on the invoice → line_total = ""."""
+
+ if not images:
+ st.error("No images provided to run_inference_vllm.")
+ return None
+
+ if len(images) > MAX_PAGES_PER_REQUEST:
+ st.warning(
+ f"Invoice has {len(images)} pages — only the first "
+ f"{MAX_PAGES_PER_REQUEST} will be sent per the request limit."
+ )
+ images = images[:MAX_PAGES_PER_REQUEST]
+
+ try:
+ image_content_blocks: List[dict] = []
+ total_b64_bytes = 0
+ resize_info = []
+
+ for idx, page in enumerate(images):
+ data_url, orig_size, new_size = _encode_page(page, idx)
+ total_b64_bytes += len(data_url)
+ if orig_size != new_size:
+ resize_info.append(
+ f"Page {idx+1}: {orig_size[0]}×{orig_size[1]} → {new_size[0]}×{new_size[1]}"
+ )
+ image_content_blocks.append({"type": "image_url", "image_url": {"url": data_url}})
+
+ if resize_info:
+ st.info("Resized for API payload:\n" + "\n".join(resize_info))
+
+ total_payload_mb = total_b64_bytes / (1024 * 1024)
+ if total_payload_mb > MAX_TOTAL_PAYLOAD_MB:
+ st.warning(
+ f"Large multi-page payload ({total_payload_mb:.1f} MB across "
+ f"{len(images)} page(s)). This may be slow or time-out."
+ )
+ else:
+ st.info(f"Sending {len(images)} page(s) in one request (payload ≈ {total_payload_mb:.2f} MB).")
+
+ user_content = image_content_blocks + [
+ {
+ "type": "text",
+ "text": (
+ f"The {len(images)} image(s) above are all pages of the same invoice "
+ f"(page 1 through page {len(images)}).\n"
+ "Extract invoice data from ALL pages combined into a single JSON object."
+ )
+ }
+ ]
+
+ payload = {
+ "model": MODEL_NAME,
+ "messages": [
+ {"role": "system", "content": EXTRACTION_PROMPT},
+ {"role": "user", "content": user_content}
+ ],
+ "temperature": 0,
+ "max_tokens": 10000
+ }
+
+ headers = {
+ "Authorization": f"Bearer {VLLM_API_KEY}",
+ "Content-Type": "application/json"
+ }
+
+ response = requests.post(
+ f"{POD_URL}/v1/chat/completions",
+ headers=headers,
+ json=payload,
+ timeout=600
+ )
+
+ if response.status_code == 200:
+ result = response.json()
+ return result["choices"][0]["message"]["content"]
+ else:
+ st.error(f"❌ API Error {response.status_code}")
+ try:
+ st.json(response.json())
+ except Exception:
+ st.code(response.text)
+ return None
+
+ except Exception as e:
+ st.error(f"Error calling vLLM: {str(e)}")
+ return None
+
+
+# -----------------------------
+# JSON Parser + postprocessing mapper from code #1
+# -----------------------------
+def parse_vllm_json(raw_json_text):
+ """Parse vLLM JSON output into:
+ 1) raw_data: untouched parsed JSON object
+ 2) mapped_data: cleaned structured format for validation
+ """
+ try:
+ text_to_parse = raw_json_text.strip()
+
+ if text_to_parse.startswith("```json"):
+ text_to_parse = text_to_parse[7:]
+ elif text_to_parse.startswith("```"):
+ text_to_parse = text_to_parse[3:]
+ if text_to_parse.endswith("```"):
+ text_to_parse = text_to_parse[:-3]
+ text_to_parse = text_to_parse.strip()
+
+ raw_data = json.loads(text_to_parse)
+
+ # Support both flat output and nested output
+ header = raw_data.get("header", raw_data)
+ summary = raw_data.get("summary", raw_data)
+ items = raw_data.get("items", [])
+
+ currency = summary.get("currency", header.get("currency", ""))
+
+ def clean_amount(value):
+ return clean_float(value, currency)
+
+ mapped = {
+ "Invoice Number": header.get("invoice_no", ""),
+ "PO Number": header.get("po_no", ""),
+ "Invoice Date": normalize_date(header.get("invoice_date", ""), currency),
+ "Payment Terms": header.get("payment_terms", ""),
+ "Due Date": normalize_date(header.get("due_date", ""), currency),
+ "Sender Name": header.get("sender_name", ""),
+ "Sender Address": header.get("sender_addr", ""),
+ "Tax ID": header.get("tax_id", ""),
+ "Sender": {
+ "Name": header.get("sender_name", ""),
+ "Address": header.get("sender_addr", "")
+ },
+ "Recipient Name": header.get("rcpt_name", ""),
+ "Recipient Address": header.get("rcpt_addr", ""),
+ "Recipient": {
+ "Name": header.get("rcpt_name", ""),
+ "Address": header.get("rcpt_addr", "")
+ },
+ "Bank Details": {
+ "bank_iban": header.get("bank_iban", ""),
+ "bank_name": header.get("bank_name", ""),
+ "bank_account_number": header.get("bank_acc_no", ""),
+ "bank_routing": header.get("bank_routing", ""),
+ "bank_swift": header.get("bank_swift", ""),
+ "bank_acc_name": header.get("bank_acc_name", ""),
+ "bank_branch": header.get("bank_branch", "")
+ },
+ "Subtotal": clean_amount(summary.get("subtotal", "0")),
+ "Tax Percentage": clean_tax_percentage(summary.get("tax_rate", "0")),
+ "Total Tax": clean_amount(summary.get("tax_amount", "0")),
+ "Discount Rate": clean_float(summary.get("discount_rate", "0"), currency),
+ "Total Discount Amount": clean_amount(summary.get("total_discount_amount", "0")),
+ "Total Amount": clean_amount(summary.get("total_amount", "0")),
+ "Currency": currency,
+ "Itemized Data": []
+ }
+
+ for item in items:
+ raw_tax = item.get("tax_amount_per_item", item.get("tax", ""))
+ raw_line_total = item.get("line_total", item.get("Line_total", ""))
+ tax_amount = clean_amount(raw_tax) if raw_tax != "" else 0.0
+ line_total = clean_amount(raw_line_total) if raw_line_total != "" else ""
+
+ mapped["Itemized Data"].append({
+ "Service Name": item.get("service_name", "") or item.get("Service_name", ""),
+ "Service Start Date": item.get("service_start_date", ""),
+ "Service End Date": item.get("service_end_date", ""),
+ "Description": item.get("descriptions", ""),
+ "SKU": item.get("SKU", ""),
+ "Quantity": clean_quantity(item.get("quantity", "0")),
+ "Unit Price": clean_amount(item.get("unit_price", "0")),
+ "Amount": clean_amount(item.get("amount", "0")),
+ "Discount Rate Per Item": clean_float(item.get("discount_rate_per_item", "0"), currency),
+ "Discount Amount Per Item": clean_amount(item.get("discount_amount_per_item", "0")),
+ "Tax Rate Per Item": clean_tax_percentage(item.get("tax_rate_per_item", "0")),
+ "Tax Amount Per Item": tax_amount,
+ "Tax_Raw": raw_tax,
+ "Tax": tax_amount,
+ "Line Total": line_total,
+ "line_total": raw_line_total
+ })
+
+ return raw_data, mapped
+
+ except Exception as e:
+ st.error(f"JSON parse error: {str(e)}")
+ return None, None
+
+
+
+
+# -----------------------------
+# Postprocessing logic from code #1
+# -----------------------------
+def validate_and_calculate_taxes(structured_data):
+ """
+ Validate and, when needed, calculate per-item tax values.
+ Supports both the legacy item keys:
+ - Tax
+ - Line Total
+ and the new item keys:
+ - Tax Amount Per Item
+ - Tax Rate Per Item
+ - Discount Amount Per Item
+ - Discount Rate Per Item
+ - line_total
+ """
+
+ subtotal = structured_data.get("Subtotal", 0.0)
+ total_amount = structured_data.get("Total Amount", 0.0)
+ model_tax_rate = structured_data.get("Tax Percentage", 0.0)
+ model_tax_amount = structured_data.get("Total Tax", 0.0)
+ items = structured_data.get("Itemized Data", [])
+
+ def get_raw_tax_value(item):
+ if not isinstance(item, dict):
+ return ""
+ raw = item.get("Tax_Raw", "")
+ if raw != "":
+ return raw
+ val = item.get("Tax Amount Per Item", item.get("Tax", ""))
+ return "" if val in ("", None) else val
+
+ def set_item_tax_value(item, value):
+ item["Tax Amount Per Item"] = value
+ item["Tax"] = value
+
+ def set_item_line_total(item, value):
+ item["Line Total"] = value
+ item["line_total"] = value
+
+ if subtotal >= total_amount or subtotal <= 0:
+ structured_data["tax_validated"] = False
+ structured_data["tax_skip_reason"] = "No tax detected"
+ return structured_data
+
+ if model_tax_rate > 0 and model_tax_amount == 0.0:
+ structured_data["tax_validated"] = False
+ structured_data["tax_skip_reason"] = "Tax rate exists but tax amount is 0"
+ return structured_data
+
+ taxable_items = []
+ non_taxable_items = []
+
+ for item in items:
+ amount = item.get("Amount", 0.0)
+ raw_tax_value = get_raw_tax_value(item)
+
+ if amount == 0.0:
+ set_item_tax_value(item, 0.0)
+ set_item_line_total(item, 0.0)
+ non_taxable_items.append(item)
+ continue
+
+ is_empty = False
+ is_explicitly_zero = False
+
+ if isinstance(raw_tax_value, str):
+ cleaned = raw_tax_value.strip()
+ if cleaned == "":
+ is_empty = True
+ else:
+ try:
+ cleaned_value = float(re.sub(r"[^\d\.-]", "", cleaned) or "0")
+ if cleaned_value == 0.0:
+ is_explicitly_zero = True
+ except (ValueError, TypeError):
+ pass
+ elif raw_tax_value is None or raw_tax_value == "":
+ is_empty = True
+ elif raw_tax_value == 0 or raw_tax_value == 0.0:
+ is_explicitly_zero = True
+
+ if is_empty or is_explicitly_zero:
+ set_item_tax_value(item, 0.0)
+ set_item_line_total(item, amount)
+ non_taxable_items.append(item)
+ continue
+
+ taxable_items.append(item)
+
+ authoritative_rate = None
+ authority_source = None
+
+ if taxable_items:
+ total_taxable_amount = sum(item.get("Amount", 0.0) for item in taxable_items)
+ if total_taxable_amount <= 0:
+ structured_data["tax_validated"] = False
+ structured_data["tax_skip_reason"] = "No taxable items with valid amounts"
+ return structured_data
+
+ if model_tax_rate > 0:
+ expected_tax_from_rate = total_taxable_amount * (model_tax_rate / 100)
+ expected_total_from_rate = subtotal + expected_tax_from_rate
+ error_from_rate = abs(expected_total_from_rate - total_amount)
+ else:
+ error_from_rate = float("inf")
+
+ if model_tax_amount > 0:
+ calculated_rate_from_amount = (model_tax_amount / total_taxable_amount) * 100
+ expected_total_from_amount = subtotal + model_tax_amount
+ error_from_amount = abs(expected_total_from_amount - total_amount)
+ else:
+ error_from_amount = float("inf")
+
+ if model_tax_rate > 0 or model_tax_amount > 0:
+ if error_from_rate <= error_from_amount:
+ authoritative_rate = round(model_tax_rate, 4)
+ authority_source = "tax_rate"
+ else:
+ authoritative_rate = round(calculated_rate_from_amount, 4)
+ authority_source = "tax_amount"
+ else:
+ structured_data["tax_validated"] = False
+ structured_data["tax_skip_reason"] = "No tax rate or amount provided"
+ return structured_data
+ else:
+ structured_data["tax_validated"] = False
+ structured_data["tax_skip_reason"] = "No taxable items found"
+ return structured_data
+
+ calculated_total_tax = 0.0
+ if authoritative_rate is not None:
+ for item in taxable_items:
+ amount = item.get("Amount", 0.0)
+ corrected_tax = round(amount * (authoritative_rate / 100), 2)
+ set_item_tax_value(item, corrected_tax)
+ calculated_total_tax += corrected_tax
+ set_item_line_total(item, round(amount + corrected_tax, 2))
+
+ structured_data["Tax Percentage"] = authoritative_rate
+ structured_data["Total Tax"] = round(calculated_total_tax, 2)
+ structured_data["Total Amount"] = round(subtotal + calculated_total_tax, 2)
+ structured_data["tax_validated"] = True
+ structured_data["tax_authority_source"] = authority_source
+ structured_data["original_tax_rate"] = model_tax_rate
+ structured_data["original_tax_amount"] = model_tax_amount
+
+ return structured_data
+
+from copy import deepcopy
+
+def recalc_display_totals_from_items(invoice_data):
+ """
+ UI/download-only recalculation.
+ subtotal = sum(Amount)
+ Line Total = Amount + Discount Amount Per Item + Tax Amount Per Item
+ """
+ data = deepcopy(invoice_data or {})
+ items = data.get("Itemized Data", []) or []
+
+ subtotal = 0.0
+ for item in items:
+ if not isinstance(item, dict):
+ continue
+
+ amount = clean_float(item.get("Amount", 0))
+ discount = clean_float(item.get("Discount Amount Per Item", 0))
+ tax = clean_float(item.get("Tax Amount Per Item", 0))
+
+ item["Line Total"] = round(amount - abs(discount) + tax, 2)
+ subtotal += amount
+
+ data["Subtotal"] = round(subtotal, 2)
+ return data
+
+from datetime import datetime, timedelta
+import re
+def derive_due_date_ui(invoice_date_str, payment_terms, due_date_str):
+ """
+ UI-only due date post-processing.
+ Rules:
+ 1) If due date already exists, keep it as-is.
+ 2) If only due date is missing and payment terms exist, derive due date from payment terms.
+ 3) For immediate/receipt/presentation terms, due date = invoice date.
+ 4) Never modifies raw model output.
+ """
+
+ # Rule 1: if due date is already present, do not touch it
+ if due_date_str is not None and str(due_date_str).strip() != "":
+ return due_date_str
+
+ # Rule 2: if payment terms are missing, no derivation
+ if payment_terms is None or str(payment_terms).strip() == "":
+ return ""
+
+ # Parse invoice date using your existing parser
+ invoice_date_obj = parse_date_to_object(invoice_date_str)
+ if not invoice_date_obj:
+ return ""
+
+ terms = str(payment_terms).lower().strip()
+
+ # Immediate / payable-on-receipt style terms
+ immediate_phrases = [
+ "due upon receipt",
+ "upon receipt",
+ "immediate",
+ "immediately",
+ "due on presentation",
+ "payable on receipt",
+ "payment on receipt",
+ "cash on delivery",
+ "cod",
+ "on receipt",
+ ]
+
+ if any(phrase in terms for phrase in immediate_phrases):
+ return invoice_date_obj.strftime("%d-%b-%Y")
+
+ # Extract day count from common term patterns
+ day_patterns = [
+ r"\bnet\s*(\d+)\b", # Net 30
+ r"\bwithin\s*(\d+)\s*days?\b", # within 30 days
+ r"\bdue\s*(\d+)\s*days?\b", # due 30 days
+ r"\b(\d+)\s*days?\b", # 30 days
+ r"\b(\d+)\s*d\b", # 30d
+ ]
+
+ for pattern in day_patterns:
+ match = re.search(pattern, terms)
+ if match:
+ days = int(match.group(1))
+ calculated = invoice_date_obj + timedelta(days=days)
+ return calculated.strftime("%d-%b-%Y")
+
+ return ""
+
+from copy import deepcopy
+
+def build_display_data_from_raw(raw_data: dict, mapped_data: dict) -> dict:
+ """
+ UI-only post-processing based on RAW model output.
+ - Does not modify raw_data
+ - Fills only if the raw field is truly empty
+ - Treats 0 / "0" / "0.0" as real values, not blank
+ """
+ data = deepcopy(mapped_data or {})
+ raw_data = raw_data or {}
+
+ def is_blank_raw(v):
+ return v is None or (isinstance(v, str) and v.strip() == "")
+
+ def num(v):
+ try:
+ if v is None or (isinstance(v, str) and v.strip() == ""):
+ return None
+ return float(clean_float(v))
+ except Exception:
+ return None
+
+ header = raw_data.get("header", raw_data)
+ summary = raw_data.get("summary", raw_data)
+ raw_items = raw_data.get("items", []) or []
+ display_items = deepcopy(data.get("Itemized Data", []) or [])
+
+ if is_blank_raw(data.get("Invoice Number")):
+ data["Invoice Number"] = (
+ header.get("invoice_no")
+ or raw_data.get("invoice_no")
+ or raw_data.get("invoice_no".lower())
+ or data.get("Invoice Number", "")
+ )
+
+ if is_blank_raw(data.get("PO Number")):
+ data["PO Number"] = header.get("po_no") or raw_data.get("po_no") or data.get("PO Number", "")
+
+ # ---- Header-level ----
+ if is_blank_raw(header.get("due_date")):
+ if is_blank_raw(data.get("Due Date")):
+ derived_due = derive_due_date_ui(
+ data.get("Invoice Date", ""),
+ data.get("Payment Terms", ""),
+ ""
+ )
+ if not is_blank_raw(derived_due):
+ data["Due Date"] = derived_due
+
+ # ---- Line items ----
+ for idx, raw_item in enumerate(raw_items):
+ if idx >= len(display_items):
+ display_items.append({})
+
+ item = display_items[idx]
+ raw_item = raw_item or {}
+
+ qty = num(item.get("Quantity"))
+ unit_price = num(item.get("Unit Price"))
+ amount = num(item.get("Amount"))
+ disc_rate = num(item.get("Discount Rate Per Item"))
+ disc_amt = num(item.get("Discount Amount Per Item"))
+ tax_rate = num(item.get("Tax Rate Per Item"))
+ tax_amt = num(item.get("Tax Amount Per Item"))
+
+ # Fill only when RAW field is blank
+ if is_blank_raw(raw_item.get("amount")) and qty is not None and unit_price is not None:
+ item["Amount"] = round(qty * unit_price, 2)
+ amount = num(item.get("Amount"))
+
+ if is_blank_raw(raw_item.get("quantity")) and amount is not None and unit_price not in (None, 0):
+ item["Quantity"] = round(amount / unit_price, 6)
+ qty = num(item.get("Quantity"))
+
+ if is_blank_raw(raw_item.get("unit_price")) and amount is not None and qty not in (None, 0):
+ item["Unit Price"] = round(amount / qty, 6)
+ unit_price = num(item.get("Unit Price"))
+
+ # Discount
+ if is_blank_raw(raw_item.get("discount_amount_per_item")) and disc_rate is not None and amount is not None:
+ item["Discount Amount Per Item"] = round(amount * (disc_rate / 100.0), 2)
+ disc_amt = num(item.get("Discount Amount Per Item"))
+
+ if is_blank_raw(raw_item.get("discount_rate_per_item")) and disc_amt is not None and amount not in (None, 0):
+ item["Discount Rate Per Item"] = round((disc_amt / amount) * 100.0, 4)
+ disc_rate = num(item.get("Discount Rate Per Item"))
+
+ # Tax
+ taxable_base = None if amount is None else amount - abs(disc_amt if disc_amt is not None else 0.0)
+
+ if is_blank_raw(raw_item.get("tax_amount_per_item")) and taxable_base is not None and tax_rate is not None:
+ item["Tax Amount Per Item"] = round(taxable_base * (tax_rate / 100.0), 2)
+ tax_amt = num(item.get("Tax Amount Per Item"))
+
+ if is_blank_raw(raw_item.get("tax_rate_per_item")) and taxable_base not in (None, 0) and tax_amt is not None:
+ item["Tax Rate Per Item"] = round((tax_amt / taxable_base) * 100.0, 4)
+
+ # Line total
+ if is_blank_raw(raw_item.get("line_total")):
+ amount = num(item.get("Amount"))
+ disc_amt = num(item.get("Discount Amount Per Item")) or 0.0
+ tax_amt = num(item.get("Tax Amount Per Item")) or 0.0
+ if amount is not None:
+ item["Line Total"] = round(amount - abs(disc_amt) + tax_amt, 2)
+
+ data["Itemized Data"] = display_items
+
+ # ---- Summary ----
+ amounts = []
+ discount_amounts = []
+ tax_amounts = []
+ taxable_bases = []
+
+ for item in display_items:
+ if not isinstance(item, dict):
+ continue
+ a = num(item.get("Amount"))
+ d = num(item.get("Discount Amount Per Item")) or 0.0
+ t = num(item.get("Tax Amount Per Item")) or 0.0
+
+ if a is not None:
+ amounts.append(a)
+ taxable_bases.append(a - d)
+
+ if num(item.get("Discount Amount Per Item")) is not None:
+ discount_amounts.append(d)
+
+ if num(item.get("Tax Amount Per Item")) is not None:
+ tax_amounts.append(t)
+
+ subtotal_calc = round(sum(amounts), 2) if amounts else None
+ total_discount_calc = round(sum(discount_amounts), 2) if discount_amounts else None
+ total_tax_calc = round(sum(tax_amounts), 2) if tax_amounts else None
+ taxable_base_total = round(sum(taxable_bases), 2) if taxable_bases else None
+
+ if is_blank_raw(summary.get("subtotal")) and subtotal_calc is not None:
+ data["Subtotal"] = subtotal_calc
+
+ if is_blank_raw(summary.get("total_discount_amount")) and total_discount_calc is not None:
+ data["Total Discount Amount"] = total_discount_calc
+
+ if is_blank_raw(summary.get("tax_amount")) and total_tax_calc is not None:
+ data["Total Tax"] = total_tax_calc
+
+ if is_blank_raw(summary.get("discount_rate")):
+ subtotal_for_rate = num(data.get("Subtotal"))
+ total_discount_for_rate = num(data.get("Total Discount Amount"))
+ if subtotal_for_rate not in (None, 0) and total_discount_for_rate is not None:
+ data["Discount Rate"] = round((total_discount_for_rate / subtotal_for_rate) * 100.0, 4)
+
+ if is_blank_raw(summary.get("tax_rate")):
+ total_tax_for_rate = num(data.get("Total Tax"))
+ if taxable_base_total not in (None, 0) and total_tax_for_rate is not None:
+ data["Tax Percentage"] = round((total_tax_for_rate / taxable_base_total) * 100.0, 4)
+
+ if is_blank_raw(summary.get("total_amount")):
+ subtotal_for_total = num(data.get("Subtotal"))
+ total_discount_for_total = num(data.get("Total Discount Amount")) or 0.0
+ total_tax_for_total = num(data.get("Total Tax")) or 0.0
+ if subtotal_for_total is not None:
+ data["Total Amount"] = round(subtotal_for_total - total_discount_for_total + total_tax_for_total, 2)
+
+ return data
+
+from decimal import Decimal
+import pandas as pd
+
+def fill_line_item_missing_fields_ui(df: pd.DataFrame, raw_items: list = None, summary_total_discount_amount=0.0) -> pd.DataFrame:
+ """
+ UI-only line-item post-processing.
+ Fills ONLY empty fields. Never overwrites existing values.
+ """
+ def is_raw_blank(raw_item: dict, field: str) -> bool:
+ v = raw_item.get(field, None)
+ if v is None:
+ return True
+ if isinstance(v, str) and v.strip() == "":
+ return True
+ return False
+ out = df.copy()
+ summary_discount_present = not (
+ summary_total_discount_amount is None
+ or str(summary_total_discount_amount).strip() == ""
+ or clean_float(summary_total_discount_amount) == 0.0
+ )
+
+ def is_blank(v):
+ if v is None:
+ return True
+ try:
+ return pd.isna(v) or str(v).strip() == ""
+ except Exception:
+ return False
+
+ def is_missing_numeric(v):
+ if v is None:
+ return True
+
+ try:
+ if pd.isna(v):
+ return True
+ except Exception:
+ pass
+
+ if str(v).strip() == "":
+ return True
+
+ try:
+ return clean_float(v) == 0.0
+ except Exception:
+ return False
+
+ def num(v):
+ if is_blank(v):
+ return None
+ try:
+ return float(clean_float(v))
+ except Exception:
+ return None
+
+ for idx in out.index:
+ raw_item = raw_items[idx] if raw_items and idx < len(raw_items) else {}
+ amount = num(out.at[idx, "Amount"]) if "Amount" in out.columns else None
+ quantity = num(out.at[idx, "Quantity"]) if "Quantity" in out.columns else None
+ unit_price = num(out.at[idx, "Unit Price"]) if "Unit Price" in out.columns else None
+ disc_amt = num(out.at[idx, "Discount Amount Per Item"]) if "Discount Amount Per Item" in out.columns else None
+ disc_rate = num(out.at[idx, "Discount Rate Per Item"]) if "Discount Rate Per Item" in out.columns else None
+ tax_rate = num(out.at[idx, "Tax Rate Per Item"]) if "Tax Rate Per Item" in out.columns else None
+ tax_amt = num(out.at[idx, "Tax Amount Per Item"]) if "Tax Amount Per Item" in out.columns else None
+ line_total = num(out.at[idx, "Line Total"]) if "Line Total" in out.columns else None
+
+ # amount = quantity × unit_price
+ if "Amount" in out.columns and is_raw_blank(raw_item, "amount"):
+ if quantity is not None and unit_price not in (None, 0):
+ amount = round(quantity * unit_price, 2)
+ out.at[idx, "Amount"] = amount
+
+ # quantity = amount ÷ unit_price
+ if "Quantity" in out.columns and is_raw_blank(raw_item, "quantity"):
+ if amount is not None and unit_price not in (None, 0):
+ quantity = round(amount / unit_price, 6)
+ out.at[idx, "Quantity"] = quantity
+
+ # unit_price = amount ÷ quantity
+ if "Unit Price" in out.columns and is_raw_blank(raw_item, "unit_price"):
+ if amount is not None and quantity not in (None, 0):
+ unit_price = round(amount / quantity, 6)
+ out.at[idx, "Unit Price"] = unit_price
+
+ # refresh numbers after possible fills
+ amount = num(out.at[idx, "Amount"]) if "Amount" in out.columns else amount
+ quantity = num(out.at[idx, "Quantity"]) if "Quantity" in out.columns else quantity
+ unit_price = num(out.at[idx, "Unit Price"]) if "Unit Price" in out.columns else unit_price
+
+ # discount_amount_per_item = amount × discount_rate_per_item ÷ 100
+ if "Discount Amount Per Item" in out.columns and is_blank(out.at[idx, "Discount Amount Per Item"]):
+ if disc_rate is not None and amount is not None:
+ disc_amt = round(amount * (disc_rate / 100.0), 2)
+ out.at[idx, "Discount Amount Per Item"] = disc_amt
+ elif not summary_discount_present:
+ # no discount signal -> no discount
+ disc_amt = 0.0
+ out.at[idx, "Discount Amount Per Item"] = 0.0
+
+ # discount_rate_per_item = (discount_amount_per_item ÷ amount) × 100
+ if "Discount Rate Per Item" in out.columns and is_raw_blank(raw_item, "discount_rate_per_item"):
+ if disc_amt is not None and amount not in (None, 0):
+ disc_rate = round((disc_amt / amount) * 100.0, 4)
+ out.at[idx, "Discount Rate Per Item"] = disc_rate
+
+ # taxable_base = amount − discount_amount_per_item
+ amount = num(out.at[idx, "Amount"]) if "Amount" in out.columns else amount
+ disc_amt = num(out.at[idx, "Discount Amount Per Item"]) if "Discount Amount Per Item" in out.columns else disc_amt
+ taxable_base = None if amount is None else amount - abs(disc_amt if disc_amt is not None else 0.0)
+ # tax_amount_per_item = taxable_base × tax_rate_per_item ÷ 100
+ if "Tax Amount Per Item" in out.columns and is_blank(out.at[idx, "Tax Amount Per Item"]):
+ if taxable_base is not None and tax_rate is not None:
+ tax_amt = round(taxable_base * (tax_rate / 100.0), 2)
+ out.at[idx, "Tax Amount Per Item"] = tax_amt
+
+ # tax_rate_per_item = (tax_amount_per_item ÷ taxable_base) × 100
+ if "Tax Rate Per Item" in out.columns and is_raw_blank(raw_item, "tax_rate_per_item"):
+ tax_amt = num(out.at[idx, "Tax Amount Per Item"])
+ if taxable_base not in (None, 0) and tax_amt is not None:
+ tax_rate = round((tax_amt / taxable_base) * 100.0, 4)
+ out.at[idx, "Tax Rate Per Item"] = tax_rate
+ # refresh tax_rate after possible back-calculation
+ tax_rate = num(out.at[idx, "Tax Rate Per Item"]) if "Tax Rate Per Item" in out.columns else tax_rate
+
+ # Line Total only if blank
+ if "Line Total" in out.columns and is_raw_blank(raw_item, "line_total"):
+ amount = num(out.at[idx, "Amount"])
+ disc_amt = num(out.at[idx, "Discount Amount Per Item"]) or 0.0
+ tax_amt = num(out.at[idx, "Tax Amount Per Item"]) or 0.0
+ if amount is not None:
+ out.at[idx, "Line Total"] = round(amount - abs(disc_amt) + tax_amt, 2)
+
+ return out
+
+def detect_and_reclassify_pseudo_lines(display_data: dict, raw_parsed_data: dict) -> dict:
+ """
+ Detects pseudo-lines based on field patterns, not description.
+
+ Pseudo-line conditions:
+ 1. quantity is 0 or 1, AND unit_price is blank, AND amount is blank
+ 2. discount fields (rate or amount) OR tax fields (rate or amount) is filled, along with line_total
+ 3. Promotes value to summary, removes pseudo-line from line items
+ """
+ from copy import deepcopy
+
+ data = deepcopy(display_data)
+ items = data.get("Itemized Data", []) or []
+ raw_items = (raw_parsed_data or {}).get("items", []) or []
+
+ def is_blank_val(v):
+ if v is None:
+ return True
+ if isinstance(v, str) and v.strip() == "":
+ return True
+ try:
+ return clean_float(v) == 0.0
+ except (ValueError, TypeError):
+ return True
+
+ def has_value(v):
+ return not is_blank_val(v)
+
+ real_items = []
+ for idx, item in enumerate(items):
+ raw_item = raw_items[idx] if idx < len(raw_items) else {}
+
+ # Condition 1: quantity is 0 or 1, unit_price blank, amount blank
+ qty = item.get("Quantity")
+ unit_price = item.get("Unit Price")
+ amount = item.get("Amount")
+
+ try:
+ qty_val = float(qty) if qty is not None else 0.0
+ except (ValueError, TypeError):
+ qty_val = 0.0
+
+ qty_ok = qty_val in (0.0, 1.0)
+ unit_price_blank = raw_item.get("unit_price", "") in ("", None) or str(raw_item.get("unit_price", "")).strip() == ""
+ amount_blank = raw_item.get("amount", "") in ("", None) or str(raw_item.get("amount", "")).strip() == ""
+
+ if not (qty_ok and unit_price_blank and amount_blank):
+ real_items.append(item)
+ continue
+
+ # Condition 2: discount or tax fields filled along with line_total
+ disc_rate = item.get("Discount Rate Per Item")
+ disc_amt = item.get("Discount Amount Per Item")
+ tax_rate = item.get("Tax Rate Per Item")
+ tax_amt = item.get("Tax Amount Per Item")
+
+ raw_disc_amt = raw_item.get("discount_amount_per_item", "")
+ raw_disc_rate = raw_item.get("discount_rate_per_item", "")
+ raw_tax_amt = raw_item.get("tax_amount_per_item", "")
+ raw_tax_rate = raw_item.get("tax_rate_per_item", "")
+
+ def raw_has_value(v):
+ if v is None or str(v).strip() == "":
+ return False
+ try:
+ return clean_float(v) != 0.0
+ except (ValueError, TypeError):
+ return False
+
+ has_discount = raw_has_value(raw_disc_amt) or raw_has_value(raw_disc_rate)
+ has_tax = raw_has_value(raw_tax_amt) or raw_has_value(raw_tax_rate)
+
+ if not (has_discount or has_tax):
+ real_items.append(item)
+ continue
+
+ # It's a pseudo-line — promote to summary
+ # Condition 3: route to correct summary field, don't override existing
+
+ if has_discount:
+ existing_disc = clean_float(data.get("Total Discount Amount", 0))
+ if existing_disc == 0.0:
+ if raw_has_value(raw_disc_amt):
+ pseudo_disc = clean_float(disc_amt)
+ elif raw_has_value(raw_disc_rate) and has_value(item.get("Line Total")):
+ pseudo_disc = clean_float(item.get("Line Total"))
+ else:
+ pseudo_disc = 0.0
+ if pseudo_disc != 0.0:
+ data["Total Discount Amount"] = pseudo_disc
+
+ if has_tax:
+ existing_tax = clean_float(data.get("Total Tax", 0))
+ if existing_tax == 0.0:
+ if raw_has_value(raw_tax_amt):
+ pseudo_tax = clean_float(tax_amt)
+ elif raw_has_value(raw_tax_rate) and has_value(item.get("Line Total")):
+ pseudo_tax = clean_float(item.get("Line Total"))
+ else:
+ pseudo_tax = 0.0
+ if pseudo_tax != 0.0:
+ data["Total Tax"] = pseudo_tax
+ if len(real_items) < len(items):
+ total_amount = clean_float(data.get("Total Amount", 0))
+ sum_line_totals = sum(clean_float(item.get("Line Total", 0)) for item in items)
+
+ if abs(sum_line_totals - total_amount) < 0.01:
+ for item in real_items:
+ amount = clean_float(item.get("Amount", 0))
+ if amount != 0:
+ # Clear line total so distribute_summary_tax_and_discount recalculates it
+ item["Line Total"] = ""
+ item["line_total"] = ""
+ data["Itemized Data"] = real_items
+ return data
+
+def distribute_summary_tax_and_discount(display_data: dict, raw_parsed_data: dict) -> dict:
+ """
+ UI-only post-processing: proportionally distributes summary-level tax_amount
+ and total_discount_amount down to line items — only when per-line values are missing.
+ Rules:
+ - NULL (blank) tax_rate_per_item → line is included in taxable pool
+ - 0 tax_rate_per_item → line is excluded, tax_amount_per_item set to 0 explicitly
+ - Negative-amount lines (credit notes) → included with signed allocation
+ - Residue after rounding → added to line with largest abs(taxable_base)
+ - Anomaly: summary_tax > 0 but total_taxable_base == 0 → flag for review
+ - Discount: same logic, weight = line.amount
+ """
+
+ from decimal import Decimal, ROUND_HALF_UP
+ from copy import deepcopy
+
+ data = deepcopy(display_data)
+ items = data.get("Itemized Data", []) or []
+ if not items:
+ return data
+
+ raw_items = (raw_parsed_data or {}).get("items", []) or []
+
+ summary_tax = Decimal(str(clean_float(data.get("Total Tax", 0))))
+ summary_discount = Decimal(str(clean_float(data.get("Total Discount Amount", 0))))
+
+ def is_raw_blank(raw_item: dict, field: str) -> bool:
+ """True if the model returned blank/null for this field."""
+ v = raw_item.get(field, None)
+ if v is None:
+ return True
+ if isinstance(v, str) and v.strip() == "":
+ return True
+ return False
+
+ def is_raw_explicitly_zero(raw_item: dict, field: str) -> bool:
+ """True if the model explicitly returned 0 for this field."""
+ v = raw_item.get(field, None)
+ if v is None:
+ return False
+ if isinstance(v, str) and v.strip() == "":
+ return False
+ try:
+ return clean_float(v) == 0.0
+ except (ValueError, TypeError):
+ return False
+
+ # DISCOUNT DISTRIBUTION
+ if summary_discount != Decimal("0"):
+ # Identify lines where discount_amount_per_item is blank in raw output
+ dist_candidates = []
+ for idx, item in enumerate(items):
+ raw_item = raw_items[idx] if idx < len(raw_items) else {}
+ if is_raw_blank(raw_item, "discount_amount_per_item"):
+ amount = Decimal(str(clean_float(item.get("Amount", 0))))
+ dist_candidates.append((idx, amount))
+
+ total_weight = sum(abs(a) for _, a in dist_candidates)
+
+ if total_weight > Decimal("0"):
+ allocated = Decimal("0")
+ residue_idx = None
+ largest_weight = Decimal("0")
+
+ for idx, amount in dist_candidates:
+ weight = abs(amount)
+ share = (summary_discount * amount / total_weight).quantize(
+ Decimal("0.01"), rounding=ROUND_HALF_UP
+ )
+ items[idx]["Discount Amount Per Item"] = float(share)
+ allocated += share
+
+ if weight > largest_weight:
+ largest_weight = weight
+ residue_idx = idx
+
+ # Residue correction
+ residue = summary_discount - allocated
+ if residue != Decimal("0") and residue_idx is not None:
+ current = Decimal(str(clean_float(items[residue_idx].get("Discount Amount Per Item", 0))))
+ items[residue_idx]["Discount Amount Per Item"] = float(current + residue)
+
+ # TAX DISTRIBUTION
+ if summary_tax == Decimal("0"):
+ # Still recalculate line totals even if no tax
+ for item in items:
+ if not isinstance(item, dict):
+ continue
+ amount = float(item.get("Amount", 0) or 0)
+ disc = abs(float(item.get("Discount Amount Per Item", 0) or 0))
+ tax = float(item.get("Tax Amount Per Item", 0) or 0)
+ if amount != 0:
+ item["Line Total"] = round(amount - disc + tax, 2)
+ data["Itemized Data"] = items
+ return data
+
+ # Mark confirmed-zero-tax lines first (tax_rate_per_item == 0 in raw)
+ for idx, item in enumerate(items):
+ raw_item = raw_items[idx] if idx < len(raw_items) else {}
+ if is_raw_explicitly_zero(raw_item, "tax_rate_per_item"):
+ items[idx]["Tax Amount Per Item"] = 0.0
+
+ # Build taxable pool: lines where tax_rate_per_item is NULL in raw output
+ # and tax_amount_per_item is also blank in raw output
+ taxable_pool = []
+ for idx, item in enumerate(items):
+ raw_item = raw_items[idx] if idx < len(raw_items) else {}
+
+ rate_is_blank = is_raw_blank(raw_item, "tax_rate_per_item")
+ tax_amt_is_blank = is_raw_blank(raw_item, "tax_amount_per_item")
+
+ if not tax_amt_is_blank:
+ # Model gave something for this line — don't touch it
+ continue
+
+ amount = Decimal(str(clean_float(item.get("Amount", 0))))
+ disc = Decimal(str(clean_float(item.get("Discount Amount Per Item", 0))))
+ taxable_base = amount - abs(disc)
+ taxable_pool.append((idx, taxable_base))
+
+ # Total taxable base using abs() for weight calculation
+ total_taxable_base = sum(abs(base) for _, base in taxable_pool)
+
+ # Anomaly guard
+ if total_taxable_base == Decimal("0"):
+ data["_tax_distribution_anomaly"] = (
+ "summary_tax > 0 but total_taxable_base == 0 — review required"
+ )
+ data["Itemized Data"] = items
+ return data
+
+ # Proportional distribution
+ allocated = Decimal("0")
+ residue_idx = None
+ largest_abs_base = Decimal("0")
+
+ for idx, taxable_base in taxable_pool:
+ # Weight = abs(base), but share is signed
+ share = (summary_tax * taxable_base / total_taxable_base).quantize(
+ Decimal("0.01"), rounding=ROUND_HALF_UP
+ )
+ items[idx]["Tax Amount Per Item"] = float(share)
+ allocated += share
+
+ if abs(taxable_base) > largest_abs_base:
+ largest_abs_base = abs(taxable_base)
+ residue_idx = idx
+
+ # Residue correction — guarantees SUM == summary_tax exactly
+ residue = summary_tax - allocated
+ if residue != Decimal("0") and residue_idx is not None:
+ current = Decimal(str(clean_float(items[residue_idx].get("Tax Amount Per Item", 0))))
+ items[residue_idx]["Tax Amount Per Item"] = float(current + residue)
+
+ for item in items:
+ if not isinstance(item, dict):
+ continue
+ amount = float(item.get("Amount", 0) or 0)
+ disc = abs(float(item.get("Discount Amount Per Item", 0) or 0))
+ tax = float(item.get("Tax Amount Per Item", 0) or 0)
+ if amount != 0:
+ item["Line Total"] = round(amount - disc + tax, 2)
+
+ data["Itemized Data"] = items
+ return data
+
+
+def prepare_processed_invoice_data(raw_parsed_data: dict, edited_data: dict) -> dict:
+ """Apply code #1's UI/download postprocessing sequence to a mapped invoice."""
+ processed = build_display_data_from_raw(raw_parsed_data or {}, edited_data or {})
+ processed = detect_and_reclassify_pseudo_lines(processed, raw_parsed_data or {})
+ processed = distribute_summary_tax_and_discount(processed, raw_parsed_data or {})
+
+ items_df = pd.DataFrame(processed.get("Itemized Data", []) or [])
+ if not items_df.empty:
+ items_df = fill_line_item_missing_fields_ui(
+ items_df,
+ raw_items=(raw_parsed_data or {}).get("items", []),
+ summary_total_discount_amount=processed.get("Total Discount Amount", 0.0),
+ )
+ processed["Itemized Data"] = items_df.to_dict("records")
+
+ items_list = processed.get("Itemized Data", []) or []
+ total_amount_sum = sum(clean_float(i.get("Amount", 0)) for i in items_list if isinstance(i, dict))
+ total_tax_sum = sum(clean_float(i.get("Tax Amount Per Item", 0)) for i in items_list if isinstance(i, dict))
+ total_disc_sum = sum(clean_float(i.get("Discount Amount Per Item", 0)) for i in items_list if isinstance(i, dict))
+ taxable_base = total_amount_sum - abs(total_disc_sum)
+
+ if taxable_base > 0 and total_tax_sum != 0:
+ processed["Tax Percentage"] = round((total_tax_sum / taxable_base) * 100, 4)
+ elif clean_float(processed.get("Tax Percentage", 0)) == 0:
+ tax_rates = [
+ clean_float(i.get("Tax Rate Per Item", 0))
+ for i in items_list
+ if isinstance(i, dict) and clean_float(i.get("Tax Rate Per Item", 0)) != 0
+ ]
+ if tax_rates:
+ processed["Tax Percentage"] = round(sum(tax_rates) / len(tax_rates), 4)
+
+ if total_amount_sum > 0 and total_disc_sum != 0:
+ processed["Discount Rate"] = round((abs(total_disc_sum) / total_amount_sum) * 100, 4)
+
+ return limit_invoice_decimals(processed)
+
+def display_data_for_result(result: dict) -> dict:
+ """Use saved user edits as authoritative; otherwise run initial postprocessing."""
+ result = result or {}
+ edited = deepcopy(result.get("edited_data", {}) or {})
+ if result.get("user_saved_edits"):
+ return limit_invoice_decimals(edited)
+ return prepare_processed_invoice_data(result.get("raw_parsed_data", {}), edited)
+
+def flatten_invoice_to_rows(invoice_data) -> list:
+ EXPECTED_BANK_FIELDS = [
+ "bank_name", "bank_account_number", "bank_acc_name",
+ "bank_iban", "bank_swift", "bank_routing", "bank_branch"
+ ]
+
+ def fmt_text(value):
+ if value is None or str(value).strip() == "":
+ return "NA"
+ return str(value).strip()
+
+ def fmt_amount(value):
+ if value is None or (isinstance(value, str) and value.strip() == ""):
+ return 0
+ return limit_decimal_places(value)
+
+ rows = []
+ invoice_data = invoice_data or {}
+ line_items = invoice_data.get("Itemized Data", []) or []
+
+ bank_details = {}
+ nested = invoice_data.get("Bank Details", {}) or {}
+ if isinstance(nested, dict):
+ for k, v in nested.items():
+ key_name = k if str(k).startswith("bank_") else f"bank_{k}"
+ bank_details[key_name] = v
+ for k, v in invoice_data.items():
+ if isinstance(k, str) and k.lower().startswith("bank_"):
+ bank_details[k] = v
+ for f in EXPECTED_BANK_FIELDS:
+ bank_details.setdefault(f, "")
+
+ def base_invoice_info():
+ return {
+ "Invoice Number": fmt_text(invoice_data.get("Invoice Number", "")),
+ "PO Number": fmt_text(invoice_data.get("PO Number", "")),
+ "Invoice Date": fmt_text(invoice_data.get("Invoice Date", "")),
+ "Payment Terms": fmt_text(invoice_data.get("Payment Terms", "")),
+ "Due Date": fmt_text(invoice_data.get("Due Date", "")),
+ "Currency": fmt_text(invoice_data.get("Currency", "")),
+ "Tax ID": fmt_text(invoice_data.get("Tax ID", "")),
+ "Subtotal": fmt_amount(invoice_data.get("Subtotal", 0.0)),
+ "Tax Percentage": fmt_amount(invoice_data.get("Tax Percentage", 0.0)),
+ "Total Tax": fmt_amount(invoice_data.get("Total Tax", 0.0)),
+ "Discount Rate": fmt_amount(invoice_data.get("Discount Rate", 0.0)),
+ "Total Discount Amount": fmt_amount(invoice_data.get("Total Discount Amount", 0.0)),
+ "Total Amount": fmt_amount(invoice_data.get("Total Amount", 0.0)),
+ "Sender Name": fmt_text(invoice_data.get("Sender Name", "") or (invoice_data.get("Sender", {}) or {}).get("Name", "")),
+ "Sender Address": fmt_text(invoice_data.get("Sender Address", "") or (invoice_data.get("Sender", {}) or {}).get("Address", "")),
+ "Recipient Name": fmt_text(invoice_data.get("Recipient Name", "") or (invoice_data.get("Recipient", {}) or {}).get("Name", "")),
+ "Recipient Address": fmt_text(invoice_data.get("Recipient Address", "") or (invoice_data.get("Recipient", {}) or {}).get("Address", "")),
+ }
+
+ def add_bank(row):
+ for k in EXPECTED_BANK_FIELDS:
+ row[k] = fmt_text(bank_details.get(k, ""))
+ return row
+
+ if not line_items:
+ row = add_bank(base_invoice_info())
+ row.update({
+ "Item Description": "NA",
+ "Service Name": "NA",
+ "Service Start Date": "NA",
+ "Service End Date": "NA",
+ "SKU": "NA",
+ "Item Quantity": 0,
+ "Item Unit Price": 0,
+ "Item Amount": 0,
+ "Discount Rate Per Item": 0,
+ "Discount Amount Per Item": 0,
+ "Tax Rate Per Item": 0,
+ "Tax Amount Per Item": 0,
+ "Item Line Total": 0,
+ "IO Number/Cost Centre": "NA",
+ })
+ rows.append(row)
+ return rows
+
+ for item in line_items:
+ if not isinstance(item, dict):
+ item = {}
+ row = add_bank(base_invoice_info())
+ row.update({
+ "Item Description": fmt_text(item.get("Description", item.get("Item Description", ""))),
+ "Service Name": fmt_text(item.get("Service Name", "")),
+ "Service Start Date": fmt_text(item.get("Service Start Date", "")),
+ "Service End Date": fmt_text(item.get("Service End Date", "")),
+ "SKU": fmt_text(item.get("SKU", "")),
+ "Item Quantity": fmt_amount(item.get("Quantity", item.get("Item Quantity", 0))),
+ "Item Unit Price": fmt_amount(item.get("Unit Price", item.get("Item Unit Price", 0))),
+ "Item Amount": fmt_amount(item.get("Amount", item.get("Item Amount", 0))),
+ "Discount Rate Per Item": fmt_amount(item.get("Discount Rate Per Item", 0)),
+ "Discount Amount Per Item": fmt_amount(item.get("Discount Amount Per Item", 0)),
+ "Tax Rate Per Item": fmt_amount(item.get("Tax Rate Per Item", 0)),
+ "Tax Amount Per Item": fmt_amount(item.get("Tax Amount Per Item", 0)),
+ "Item Line Total": fmt_amount(item.get("Line Total", item.get("Item Line Total", item.get("Amount", 0)))),
+ "IO Number/Cost Centre": fmt_text(item.get("IO Number/Cost Centre", "")),
+ })
+ rows.append(row)
+ return rows
+
+
+# -----------------------------
+# JSONL conversion
+# -----------------------------
+def build_page_filenames(file_name: str, num_pages: int) -> list[str]:
+ """
+ Single page → ["PI-2025-Dec-040.png"]
+ Multi page → ["PI-2025-Dec-051_page1.png", "PI-2025-Dec-051_page2.png"]
+ Always a list, always .png, single page keeps original name with no suffix.
+ """
+ stem = Path(file_name).stem
+ if num_pages == 1:
+ return [f"{stem}.png"]
+ return [f"{stem}_page{i+1}.png" for i in range(num_pages)]
+
+
+def convert_to_jsonl_record(edited_data, file_name="", num_pages: int = 1):
+ ed = edited_data or {}
+ currency = ed.get("Currency", "")
+ items = ed.get("Itemized Data", []) or []
+ bank = ed.get("Bank Details", {}) or {}
+
+ header = {
+ "invoice_no": ed.get("Invoice Number", ""),
+ "po_no": ed.get("PO Number", ""),
+ "invoice_date": ed.get("Invoice Date", ""),
+ "payment_terms": ed.get("Payment Terms", ""),
+ "due_date": ed.get("Due Date", ""),
+ "sender_name": ed.get("Sender Name", "") or (ed.get("Sender", {}) or {}).get("Name", ""),
+ "sender_addr": ed.get("Sender Address", "") or (ed.get("Sender", {}) or {}).get("Address", ""),
+ "tax_id": ed.get("Tax ID", ""),
+ "rcpt_name": ed.get("Recipient Name", "") or (ed.get("Recipient", {}) or {}).get("Name", ""),
+ "rcpt_addr": ed.get("Recipient Address", "") or (ed.get("Recipient", {}) or {}).get("Address", ""),
+ "bank_iban": bank.get("bank_iban", ""),
+ "bank_name": bank.get("bank_name", ""),
+ "bank_acc_no": bank.get("bank_account_number", ""),
+ "bank_routing": bank.get("bank_routing", ""),
+ "bank_swift": bank.get("bank_swift", ""),
+ "bank_acc_name": bank.get("bank_acc_name", ""),
+ "bank_branch": bank.get("bank_branch", ""),
+ }
+
+ jsonl_items = []
+ for it in items:
+ if not isinstance(it, dict):
+ continue
+ jsonl_items.append({
+ "service_name": it.get("Service Name", ""),
+ "service_start_date": it.get("Service Start Date", ""),
+ "service_end_date": it.get("Service End Date", ""),
+ "descriptions": it.get("Description", ""),
+ "SKU": it.get("SKU", ""),
+ "quantity": it.get("Quantity", ""),
+ "unit_price": it.get("Unit Price", ""),
+ "amount": it.get("Amount", ""),
+ "discount_rate_per_item": it.get("Discount Rate Per Item", ""),
+ "discount_amount_per_item": it.get("Discount Amount Per Item", ""),
+ "tax_rate_per_item": it.get("Tax Rate Per Item", ""),
+ "tax_amount_per_item": it.get("Tax Amount Per Item", ""),
+ "line_total": it.get("Line Total", ""),
+ })
+
+ summary = {
+ "subtotal": ed.get("Subtotal", ""),
+ "tax_rate": ed.get("Tax Percentage", ""),
+ "tax_amount": ed.get("Total Tax", ""),
+ "discount_rate": ed.get("Discount Rate", ""),
+ "total_discount_amount": ed.get("Total Discount Amount", ""),
+ "total_amount": ed.get("Total Amount", ""),
+ "currency": currency,
+ }
+
+ return {
+ "file_name": build_page_filenames(file_name, num_pages),
+ "gt_parse": {
+ "header": header,
+ "items": jsonl_items,
+ "summary": summary,
+ }
+ }
+
+
+def build_excel_bytes(df):
+ buf = BytesIO()
+ with pd.ExcelWriter(buf, engine="openpyxl") as writer:
+ df.to_excel(writer, index=False, sheet_name="Invoices")
+ ws = writer.sheets["Invoices"]
+ for col_cells in ws.columns:
+ max_len = 0
+ col_letter = col_cells[0].column_letter
+ for cell in col_cells:
+ try:
+ if cell.value:
+ max_len = max(max_len, len(str(cell.value)))
+ except Exception:
+ pass
+ ws.column_dimensions[col_letter].width = min(max_len + 3, 50)
+ return buf.getvalue()
+
+
+# =============================================================================
+# Extraction helper — inference + code #1 postprocessing
+# =============================================================================
+def _run_extraction(pages: List[Image.Image]) -> tuple[str | None, dict, dict]:
+ raw_json = run_inference_vllm(pages)
+ if not raw_json:
+ return None, {}, {}
+
+ raw_parsed_data, parsed_data = parse_vllm_json(raw_json)
+ if not parsed_data:
+ st.warning("Failed to parse JSON response from model.")
+ return raw_json, raw_parsed_data or {}, {}
+
+ mapped = validate_and_calculate_taxes(parsed_data)
+ safe_mapped = mapped if isinstance(mapped, dict) else {}
+ display_ready = prepare_processed_invoice_data(raw_parsed_data or {}, safe_mapped)
+ return raw_json, raw_parsed_data or {}, display_ready if isinstance(display_ready, dict) else safe_mapped
+
+
+# =============================================================================
+# Pipeline integration helpers
+# =============================================================================
+
+def load_default_configs() -> dict:
+ """Load default configs from disk; return Nones on failure (pipeline auto-loads)."""
+ configs: dict = {"platform_configs": None, "matching_configs": None}
+ _base = os.path.dirname(os.path.abspath(__file__))
+ try:
+ # Local: subfolder layout — HF Spaces: flat layout (same dir)
+ _p = os.path.join(_base, "Invoice Validation Functions", "platform_configs.json")
+ if not os.path.exists(_p):
+ _p = os.path.join(_base, "platform_configs.json")
+ with open(_p, encoding="utf-8") as _f:
+ configs["platform_configs"] = json.load(_f)
+ except Exception:
+ pass
+ try:
+ _p = os.path.join(_base, "Matching Functions", "matching_configs.json")
+ if not os.path.exists(_p):
+ _p = os.path.join(_base, "matching_configs.json")
+ with open(_p, encoding="utf-8") as _f:
+ configs["matching_configs"] = json.load(_f)
+ except Exception:
+ pass
+ return configs
+
+
+def _safe_int(val, default: int = 0) -> int:
+ """Convert a cell value (possibly NaN→"" after fillna) to int safely."""
+ if val is None or val == "":
+ return default
+ try:
+ return int(float(str(val)))
+ except (ValueError, TypeError):
+ return default
+
+
+def parse_validation_master(xlsx_bytes: bytes) -> dict:
+ """Parse Validation Master Excel → master_data dict entries."""
+ xls = pd.ExcelFile(BytesIO(xlsx_bytes))
+ result: dict = {}
+
+ if "Vendor Master" in xls.sheet_names:
+ df = xls.parse("Vendor Master").fillna("")
+ result["vendors"] = [
+ SimpleNamespace(
+ id=_safe_int(row.get("vendor_id"), 0),
+ tenant_id=_safe_int(row.get("tenant_id"), 1),
+ legal_name=str(row.get("legal_name", "")),
+ vendor_name=str(row.get("vendor_name", "")),
+ vendor_status=str(row.get("vendor_status", "active")),
+ deleted_at=None if str(row.get("deleted_at", "")).strip() == "" else str(row.get("deleted_at", "")),
+ billing_country=str(row.get("billing_country", "")),
+ billing_state=None if str(row.get("billing_state", "")).strip() == "" else str(row.get("billing_state", "")),
+ tax_registration_number=str(row.get("tax_registration_number", "")),
+ payment_terms=_safe_int(row.get("payment_terms"), 30),
+ exemption_status=str(row.get("exemption_status", "standard")),
+ )
+ for _, row in df.iterrows()
+ ]
+
+ if "Vendor Tax ID Master" in xls.sheet_names:
+ df = xls.parse("Vendor Tax ID Master").fillna("")
+ result["vendor_tax_ids"] = [
+ SimpleNamespace(
+ tenant_id=_safe_int(row.get("tenant_id"), 1),
+ vendor_id=_safe_int(row.get("vendor_id"), 0),
+ tax_id_canonical=str(row.get("tax_id_canonical", "")),
+ tax_id_type=str(row.get("tax_id_type", "")),
+ verified_at=str(row.get("verified_at", "")) or None,
+ deleted_at=None if str(row.get("deleted_at", "")).strip() == "" else str(row.get("deleted_at", "")),
+ effective_from=None,
+ effective_to=None,
+ )
+ for _, row in df.iterrows()
+ ]
+
+ if "Vendor Bank Master" in xls.sheet_names:
+ df = xls.parse("Vendor Bank Master").fillna("")
+ result["vendor_bank_accounts"] = [
+ SimpleNamespace(
+ tenant_id=_safe_int(row.get("tenant_id"), 1),
+ vendor_id=_safe_int(row.get("vendor_id"), 0),
+ iban_canonical=str(row.get("iban_canonical", "")),
+ acc_no_canonical=str(row.get("acc_no_canonical", "")),
+ swift_bic=str(row.get("swift_bic", "")),
+ bank_acc_name=str(row.get("bank_acc_name", "")),
+ currency_code=str(row.get("currency_code", "")),
+ bank_country=str(row.get("bank_country", "")),
+ is_primary=bool(_safe_int(row.get("is_primary"), 1)),
+ effective_from=None,
+ effective_to=None,
+ deleted_at=None if str(row.get("deleted_at", "")).strip() == "" else str(row.get("deleted_at", "")),
+ )
+ for _, row in df.iterrows()
+ ]
+
+ if "Tax Master" in xls.sheet_names:
+ df = xls.parse("Tax Master").fillna("")
+ result["tax_master_rows"] = [
+ SimpleNamespace(
+ id=_safe_int(row.get("id"), i + 1),
+ tenant_id=_safe_int(row.get("tenant_id"), 1),
+ is_active=bool(_safe_int(row.get("is_active"), 1)),
+ deleted_at=None,
+ country_code=str(row.get("country_code", "")),
+ region_code=None if str(row.get("region_code", "")).strip() == "" else str(row.get("region_code", "")),
+ tax_type=str(row.get("tax_type", "VAT")),
+ tax_rate=float(row.get("tax_rate", 0.0)),
+ tax_name=str(row.get("tax_name", "")),
+ effective_from=None,
+ effective_to=None,
+ )
+ for i, (_, row) in enumerate(df.iterrows())
+ ]
+
+ if "Currency Master" in xls.sheet_names:
+ df = xls.parse("Currency Master").fillna("")
+ result["currency_seed_map"] = {
+ str(row.get("currency_code", "")): {"minor_unit_value": str(row.get("minor_unit_value", "0.01"))}
+ for _, row in df.iterrows()
+ if str(row.get("currency_code", "")).strip()
+ }
+
+ if "Entity" in xls.sheet_names:
+ df = xls.parse("Entity").fillna("")
+ if not df.empty:
+ row = df.iloc[0]
+ result["entity"] = SimpleNamespace(
+ id=_safe_int(row.get("id"), 1),
+ tenant_id=_safe_int(row.get("tenant_id"), 1),
+ entity_id=_safe_int(row.get("entity_id"), 10),
+ country_code=str(row.get("country_code", "")),
+ vat_id=str(row.get("vat_id", "")),
+ region_code=None if str(row.get("region_code", "")).strip() == "" else str(row.get("region_code", "")),
+ )
+
+ return result
+
+
+def parse_po_master(xlsx_bytes: bytes) -> dict:
+ """Parse PO Master Excel → purchase_orders (for validation) + matching_pos + po_lines_map."""
+ df = pd.read_excel(BytesIO(xlsx_bytes)).fillna("")
+ val_pos: dict = {} # po_id → SimpleNamespace for validation layer
+ match_pos: dict = {} # po_id → POHeader for matching layer
+ canonical_map: dict = {} # po_id → po_number_canonical
+ po_lines_map: dict = {} # po_id → [POLine]
+
+ for _, row in df.iterrows():
+ po_id = _safe_int(row.get("po_id"), 0)
+ raw_po_no = str(row.get("po_number", ""))
+ po_canon = re.sub(r"[^A-Z0-9\-]", "", raw_po_no.upper())
+ pt_raw = str(row.get("payment_terms", "")).strip()
+ pt_val = _safe_int(pt_raw if pt_raw not in ("", "nan") else None, 30)
+
+ if po_id not in val_pos:
+ val_pos[po_id] = SimpleNamespace(
+ id=po_id, tenant_id=1,
+ po_number_canonical=po_canon,
+ payment_terms=pt_val, deleted_at=None,
+ )
+ match_pos[po_id] = POHeader(
+ id=po_id,
+ vendor_id=_safe_int(row.get("vendor_id"), 0),
+ currency=str(row.get("currency", "GBP")),
+ status=str(row.get("po_status", "open")),
+ )
+ canonical_map[po_id] = po_canon
+ po_lines_map[po_id] = []
+
+ line_id = str(row.get("line_id", "")).strip()
+ if line_id and line_id not in ("", "nan"):
+ try:
+ line_id_int = int(float(line_id))
+ except (ValueError, TypeError):
+ continue # skip non-numeric line IDs silently
+ desc = str(row.get("line_description", ""))
+ sku = str(row.get("line_sku", ""))
+ po_lines_map[po_id].append(POLine(
+ id=line_id_int,
+ description=desc,
+ description_canonical=re.sub(r"[^A-Z0-9]", "", desc.upper()),
+ sku=sku,
+ sku_canonical=re.sub(r"[^A-Z0-9]", "", sku.upper()),
+ quantity=Decimal(str(clean_float(row.get("line_quantity", 0)))),
+ invoiced_qty=Decimal(str(clean_float(row.get("line_invoiced_qty", 0)))),
+ unit_price=Decimal(str(clean_float(row.get("line_unit_price", 0)))),
+ item_type=str(row.get("line_item_type", "services")),
+ status=str(row.get("line_status", "open")),
+ ))
+
+ return {
+ "purchase_orders": list(val_pos.values()),
+ "matching_pos": [(match_pos[pid], canonical_map[pid]) for pid in match_pos],
+ "po_lines_map": po_lines_map,
+ }
+
+
+def parse_gr_master(xlsx_bytes: bytes) -> dict:
+ """Parse GR Master Excel → gr_rows list."""
+ df = pd.read_excel(BytesIO(xlsx_bytes)).fillna("")
+ gr_rows = []
+ for _, row in df.iterrows():
+ gr_date_raw = row.get("gr_date", "")
+ if hasattr(gr_date_raw, "date") and callable(gr_date_raw.date):
+ gr_date_val = gr_date_raw.date()
+ elif str(gr_date_raw).strip():
+ _d = parse_date_to_object(str(gr_date_raw))
+ gr_date_val = _d.date() if hasattr(_d, "date") and callable(_d.date) else _d
+ else:
+ gr_date_val = None
+ if gr_date_val is None:
+ continue
+ qty_acc_raw = str(row.get("quantity_accepted", "")).strip()
+ qty_accepted = Decimal(str(clean_float(qty_acc_raw))) if qty_acc_raw and qty_acc_raw not in ("nan", "") else None
+ gr_rows.append(GRLineItem(
+ id=_safe_int(row.get("gr_id"), 0),
+ po_line_id=_safe_int(row.get("po_line_id"), 0),
+ gr_date=gr_date_val,
+ inspection_status=str(row.get("inspection_status", "accepted")),
+ quantity_received=Decimal(str(clean_float(row.get("quantity_received", 0)))),
+ quantity_accepted=qty_accepted,
+ ))
+ return {"gr_rows": gr_rows}
+
+
+def parse_contract_master(xlsx_bytes: bytes) -> dict:
+ """Parse Contract Master Excel → contracts list."""
+ df = pd.read_excel(BytesIO(xlsx_bytes)).fillna("")
+
+ def _parse_date_col(val):
+ if hasattr(val, "date") and callable(val.date):
+ return val.date()
+ v = str(val).strip()
+ if not v or v in ("", "nan"):
+ return None
+ _d = parse_date_to_object(v)
+ return _d.date() if hasattr(_d, "date") and callable(_d.date) else _d
+
+ contracts_data: dict = {} # cid → (header_row, [rate_card_items])
+ for _, row in df.iterrows():
+ cid = _safe_int(row.get("contract_id"), 0)
+ if cid not in contracts_data:
+ contracts_data[cid] = (row, [])
+ desc = str(row.get("rate_description", ""))
+ if desc.strip() and desc.strip() != "nan":
+ contracts_data[cid][1].append(RateCardItem(
+ id=len(contracts_data[cid][1]) + 1,
+ description=desc,
+ description_canonical=re.sub(r"[^A-Z0-9]", "", desc.upper()),
+ unit_price=Decimal(str(clean_float(row.get("rate_unit_price", 0)))),
+ item_type=str(row.get("rate_item_type", "services")),
+ ))
+
+ contracts = []
+ for cid, (row, rate_items) in contracts_data.items():
+ tv_raw = str(row.get("total_value", "")).strip()
+ cb_raw = str(row.get("cumulative_billed_amount", "")).strip()
+ contracts.append(ContractRecord(
+ id=cid,
+ tenant_id=_safe_int(row.get("tenant_id"), 1),
+ vendor_id=_safe_int(row.get("vendor_id"), 0),
+ entity_id=_safe_int(row.get("entity_id"), 0),
+ currency=str(row.get("currency", "")),
+ status=str(row.get("status", "active")),
+ effective_from=_parse_date_col(row.get("effective_from", "")),
+ effective_to=_parse_date_col(row.get("effective_to", "")),
+ total_value=Decimal(str(clean_float(tv_raw))) if tv_raw and tv_raw not in ("nan", "") else None,
+ cumulative_billed_amount=Decimal(str(clean_float(cb_raw))) if cb_raw and cb_raw not in ("nan", "") else None,
+ rate_card_items=rate_items,
+ ))
+ return {"contracts": contracts}
+
+
+def _canonicalize_str(s: str) -> str:
+ return re.sub(r"[^A-Z0-9]", "", str(s).upper()) if s else ""
+
+
+def _pre_resolve_vendor_id(edited_data: dict, master_data: dict):
+ """Try to resolve vendor_id from master by tax_id or name match. Returns int or None."""
+ tax_id_raw = str(edited_data.get("Tax ID", "") or "").strip()
+ if tax_id_raw:
+ canonical = re.sub(r"^[A-Z]{2}", "", tax_id_raw.upper()).strip()
+ for vtid in master_data.get("vendor_tax_ids", []):
+ if getattr(vtid, "tax_id_canonical", "") == canonical:
+ return vtid.vendor_id
+ sender_name = str(edited_data.get("Sender Name", "") or "").strip()
+ if sender_name:
+ s_canon = _canonicalize_str(sender_name)
+ for v in master_data.get("vendors", []):
+ v_canon = _canonicalize_str(getattr(v, "vendor_name", "") or "")
+ if v_canon and v_canon == s_canon:
+ return v.id
+ return None
+
+
+def build_and_run_pipeline(edited_data: dict, master_data: dict, pipeline_configs: dict) -> dict:
+ """Build pipeline inputs from UI data + master data, then call run_invoice_pipeline."""
+ if not _pipeline_available:
+ return {
+ "final_status": "exception",
+ "error": f"Pipeline not loaded: {_pipeline_import_error}",
+ "risk_flags": [], "advisory_flags": [],
+ "match_result": None, "route": "error",
+ }
+ entity = master_data.get("entity")
+ if entity is None:
+ return {
+ "final_status": "exception", "error": "Entity not found in master data",
+ "risk_flags": [], "advisory_flags": [],
+ "match_result": None, "route": "error",
+ }
+ tenant_id = getattr(entity, "tenant_id", 1)
+ entity_id = getattr(entity, "entity_id", 10)
+
+ document = SimpleNamespace(
+ id=0, tenant_id=tenant_id, entity_id=entity_id,
+ status="validated", doc_type="invoice", tenant_timezone="UTC",
+ )
+
+ po_number_raw = str(edited_data.get("PO Number", "") or "").strip()
+ po_number_canonical = re.sub(r"[^A-Z0-9\-]", "", po_number_raw.upper()) if po_number_raw else None
+
+ sender_name = str(edited_data.get("Sender Name", "") or "")
+ sender_tax_id = str(edited_data.get("Tax ID", "") or "")
+ bank = edited_data.get("Bank Details", {}) or {}
+ bank_iban = str(bank.get("bank_iban", "") or "")
+ bank_iban_canonical = re.sub(r"\s+", "", bank_iban).upper() if bank_iban else ""
+
+ invoice_date = parse_date_to_object(str(edited_data.get("Invoice Date", "") or ""))
+ due_date = parse_date_to_object(str(edited_data.get("Due Date", "") or ""))
+ if hasattr(invoice_date, "date") and callable(invoice_date.date):
+ invoice_date = invoice_date.date()
+ if hasattr(due_date, "date") and callable(due_date.date):
+ due_date = due_date.date()
+
+ pt_raw = str(edited_data.get("Payment Terms", "") or "")
+ pt_match = re.search(r"\d+", pt_raw)
+ payment_terms = int(pt_match.group()) if pt_match else 0
+
+ currency = str(edited_data.get("Currency", "") or "")
+
+ extracted_fields = SimpleNamespace(
+ sender_name=sender_name,
+ sender_name_canonical=_canonicalize_str(sender_name),
+ sender_tax_id=sender_tax_id,
+ sender_tax_id_canonical=sender_tax_id,
+ bank_iban=bank_iban,
+ bank_iban_canonical=bank_iban_canonical,
+ bank_acc_no=str(bank.get("bank_account_number", "") or bank.get("bank_acc_no", "") or ""),
+ bank_swift=str(bank.get("bank_swift", "") or ""),
+ bank_acc_name=str(bank.get("bank_acc_name", "") or ""),
+ invoice_date=invoice_date,
+ due_date=due_date,
+ due_date_origin="invoice",
+ service_start_date=None,
+ service_end_date=None,
+ payment_terms=payment_terms,
+ po_number_canonical=po_number_canonical,
+ currency=currency,
+ invoice_no=str(edited_data.get("Invoice Number", "") or ""),
+ total_amount=Decimal(str(clean_float(edited_data.get("Total Amount", 0)))),
+ subtotal=Decimal(str(clean_float(edited_data.get("Subtotal", 0)))),
+ tax_amount=Decimal(str(clean_float(edited_data.get("Total Tax", 0)))),
+ tax_rate=Decimal(str(clean_float(edited_data.get("Tax Percentage", 0)))),
+ )
+
+ line_items_raw = [it for it in (edited_data.get("Itemized Data", []) or []) if isinstance(it, dict)]
+ line_items = [
+ SimpleNamespace(
+ line_number=i + 1,
+ amount=Decimal(str(clean_float(it.get("Amount", 0)))),
+ tax_rate_per_item=Decimal(str(clean_float(it.get("Tax Rate Per Item", 0)))),
+ tax_amount_per_item=Decimal(str(clean_float(it.get("Tax Amount Per Item", 0)))),
+ discount_amount_per_item=Decimal(str(clean_float(it.get("Discount Amount Per Item", 0))))
+ if clean_float(it.get("Discount Amount Per Item", 0)) else None,
+ )
+ for i, it in enumerate(line_items_raw)
+ ]
+
+ resolved_vendor_id = _pre_resolve_vendor_id(edited_data, master_data)
+
+ matching_invoice = InvoiceHeader(
+ id=0, status="validated",
+ vendor_id=resolved_vendor_id or 0,
+ currency=currency,
+ total_amount=Decimal(str(clean_float(edited_data.get("Subtotal", 0)))),
+ invoice_date=invoice_date,
+ )
+
+ matching_inv_lines = [
+ InvoiceLine(
+ id=i + 1,
+ description=str(it.get("Description", "") or ""),
+ description_canonical=_canonicalize_str(str(it.get("Description", "") or "")),
+ sku=str(it.get("SKU", "") or ""),
+ sku_canonical=_canonicalize_str(str(it.get("SKU", "") or "")),
+ quantity=Decimal(str(clean_float(it.get("Quantity", 1)))),
+ unit_price=Decimal(str(clean_float(it.get("Unit Price", 0)))),
+ amount=Decimal(str(clean_float(it.get("Amount", 0)))),
+ )
+ for i, it in enumerate(line_items_raw)
+ ]
+ if not matching_inv_lines:
+ matching_inv_lines = [InvoiceLine(
+ id=1, description="", description_canonical="", sku="", sku_canonical="",
+ quantity=Decimal("1"),
+ unit_price=Decimal(str(clean_float(edited_data.get("Subtotal", 0)))),
+ amount=Decimal(str(clean_float(edited_data.get("Subtotal", 0)))),
+ )]
+
+ # Extract service date range from line items — required by contract_match to avoid
+ # billing_period_unverified hard exception on every contract-route invoice.
+ # Must happen after line_items_raw is defined; update extracted_fields after.
+ svc_start = None
+ svc_end = None
+ for _it in line_items_raw:
+ for _key, _is_start in [("Service Start Date", True), ("Service End Date", False)]:
+ _raw = str(_it.get(_key, "") or "").strip()
+ if not _raw:
+ continue
+ _d = parse_date_to_object(_raw)
+ if _d is None:
+ continue
+ if hasattr(_d, "date") and callable(_d.date):
+ _d = _d.date()
+ if _is_start:
+ svc_start = _d if svc_start is None else min(svc_start, _d)
+ else:
+ svc_end = _d if svc_end is None else max(svc_end, _d)
+
+ extracted_fields.service_start_date = svc_start
+ extracted_fields.service_end_date = svc_end
+
+ po = None
+ po_lines: list = []
+ if po_number_canonical:
+ for match_ph, canon in master_data.get("matching_pos", []):
+ if canon == po_number_canonical:
+ po = match_ph
+ po_lines = master_data.get("po_lines_map", {}).get(match_ph.id, [])
+ break
+
+ gr_rows: list = []
+ if po_lines:
+ po_line_ids = {getattr(pl, "id", None) for pl in po_lines}
+ gr_rows = [gr for gr in master_data.get("gr_rows", [])
+ if getattr(gr, "po_line_id", None) in po_line_ids]
+
+ contract_invoice = None
+ if not po_number_canonical:
+ contract_invoice = ContractInvoiceHeader(
+ id=0, tenant_id=tenant_id, entity_id=entity_id,
+ resolved_vendor_id=resolved_vendor_id,
+ currency=currency, invoice_date=invoice_date,
+ service_start_date=svc_start,
+ service_end_date=svc_end,
+ total_amount=Decimal(str(clean_float(edited_data.get("Total Amount", 0)))),
+ )
+
+ pc = (pipeline_configs or {}).get("platform_configs") or None
+ mc = (pipeline_configs or {}).get("matching_configs") or None
+
+ return run_invoice_pipeline(
+ document=document,
+ extracted_fields=extracted_fields,
+ vendors=master_data.get("vendors", []),
+ vendor_bank_accounts=master_data.get("vendor_bank_accounts", []),
+ vendor_tax_ids=master_data.get("vendor_tax_ids", []),
+ entity=entity,
+ line_items=line_items,
+ purchase_orders=master_data.get("purchase_orders", []),
+ contracts=master_data.get("contracts", []),
+ tax_master_rows=master_data.get("tax_master_rows", []),
+ currency_seed_map=master_data.get("currency_seed_map", {}),
+ platform_configs=pc,
+ matching_invoice=matching_invoice,
+ matching_inv_lines=matching_inv_lines,
+ contract_invoice=contract_invoice,
+ po=po,
+ po_lines=po_lines or None,
+ gr_rows=gr_rows or None,
+ matching_configs=mc,
+ )
+
+
+# ── Flag display constants ────────────────────────────────────────────────────
+_FIELD_FLAG_MAP: dict = {
+ "Invoice Number": ["missing_invoice_number"],
+ "PO Number": ["po_not_found", "po_cancelled", "po_all_lines_closed", "po_closed", "missing_po_number"],
+ "Invoice Date": ["invoice_date_invalid", "invoice_in_future", "backdated_invoice", "missing_invoice_date"],
+ "Payment Terms": ["payment_terms_mismatch"],
+ "Due Date": ["due_date_missing", "due_date_before_invoice"],
+ "Tax ID": ["tax_id_mismatch", "unverified_tax_id", "missing_tax_id"],
+ "Currency": ["currency_mismatch"],
+ "Subtotal": ["subtotal_mismatch"],
+ "Tax Percentage": ["tax_rate_mismatch"],
+ "Total Tax": ["tax_amount_mismatch"],
+ "Total Amount": ["total_amount_mismatch", "po_ceiling_exhausted"],
+ "Sender Name": ["unknown_vendor", "vendor_name_mismatch", "cannot_match_no_vendor"],
+ "Bank IBAN": ["bank_iban_mismatch"],
+ "Account Number": ["bank_account_mismatch"],
+ "SWIFT": ["bank_swift_mismatch"],
+}
+
+_FLAG_LABELS: dict = {
+ "unknown_vendor": "Vendor not found in master data",
+ "vendor_name_mismatch": "Vendor name does not match master",
+ "tax_id_mismatch": "Tax ID does not match vendor master",
+ "unverified_tax_id": "Tax ID is unverified",
+ "bank_iban_mismatch": "IBAN does not match vendor master",
+ "bank_account_mismatch": "Account number does not match vendor master",
+ "bank_swift_mismatch": "SWIFT/BIC does not match vendor master",
+ "invoice_date_invalid": "Invoice date is invalid",
+ "invoice_in_future": "Invoice date is in the future",
+ "backdated_invoice": "Invoice is backdated beyond allowed period",
+ "due_date_missing": "Due date is missing",
+ "due_date_before_invoice": "Due date is before invoice date",
+ "payment_terms_mismatch": "Payment terms do not match vendor master",
+ "tax_rate_mismatch": "Tax rate does not match tax master",
+ "tax_amount_mismatch": "Tax amount does not match calculated value",
+ "subtotal_mismatch": "Subtotal does not match line items",
+ "total_amount_mismatch": "Total amount does not match subtotal + tax",
+ "po_ceiling_exhausted": "PO ceiling has been exhausted",
+ "po_not_found": "PO number not found in system",
+ "po_cancelled": "PO is cancelled",
+ "po_all_lines_closed": "All PO lines are closed or cancelled",
+ "po_closed": "PO is closed",
+ "currency_mismatch": "Currency does not match PO currency",
+ "missing_invoice_number": "Invoice number is missing",
+ "missing_po_number": "PO number is missing",
+ "missing_invoice_date": "Invoice date is missing",
+ "missing_tax_id": "Tax ID is missing",
+ "cannot_match_no_vendor": "Cannot match: vendor could not be resolved",
+}
+
+
+def _normalise_flags(pipeline_result: dict) -> tuple:
+ if not pipeline_result:
+ return [], []
+ def _n(f, sev):
+ if isinstance(f, dict):
+ return {"severity": sev, **f}
+ return {"code": str(f), "severity": sev}
+ risk = [_n(f, "risk") for f in (pipeline_result.get("risk_flags") or [])]
+ adv = [_n(f, "advisory") for f in (pipeline_result.get("advisory_flags") or [])]
+ return risk, adv
+
+
+def get_field_flags(pipeline_result, field_name: str) -> list:
+ if not pipeline_result:
+ return []
+ codes = _FIELD_FLAG_MAP.get(field_name, [])
+ if not codes:
+ return []
+ risk, adv = _normalise_flags(pipeline_result)
+ return [f for f in risk + adv if f.get("code", "") in codes]
+
+
+def build_flags_column(pipeline_result, num_lines: int) -> list:
+ if not pipeline_result or num_lines == 0:
+ return [""] * num_lines
+ mr = pipeline_result.get("match_result")
+ if mr is None:
+ return [""] * num_lines
+ result = [""] * num_lines
+ for exc in (getattr(mr, "match_exceptions", None) or []):
+ ln = getattr(exc, "line_id", None)
+ et = getattr(exc, "exception_type", "")
+ if ln is not None:
+ idx = int(ln) - 1
+ if 0 <= idx < num_lines:
+ result[idx] = (result[idx] + ", " + et) if result[idx] else et
+ return result
+
+
+def render_field_flag(flags: list) -> None:
+ for f in flags:
+ code = f.get("code", "")
+ label = _FLAG_LABELS.get(code, code.replace("_", " ").title())
+ if f.get("severity") == "advisory":
+ st.warning(f"Advisory: {label}")
+ else:
+ st.error(f"Flag: {label}")
+
+
+def render_status_badge(pipeline_result) -> None:
+ if not pipeline_result:
+ return
+ final_status = pipeline_result.get("final_status", "")
+ route = pipeline_result.get("route", "")
+ risk_count = len(pipeline_result.get("risk_flags") or [])
+ adv_count = len(pipeline_result.get("advisory_flags") or [])
+ mr = pipeline_result.get("match_result")
+ match_exc_count = len(getattr(mr, "match_exceptions", None) or []) if mr else 0
+ color_map = {"matched": "#1a7a1a", "partial_match": "#b8860b", "exception": "#c0392b"}
+ label_map = {"matched": "Matched", "partial_match": "Partial Match", "exception": "Exception"}
+ color = color_map.get(final_status, "#666666")
+ label = label_map.get(final_status, final_status.replace("_", " ").title() if final_status else "Pending")
+ parts = []
+ if risk_count:
+ parts.append(f"{risk_count} risk flag{'s' if risk_count != 1 else ''}")
+ if adv_count:
+ parts.append(f"{adv_count} advisory")
+ if match_exc_count:
+ parts.append(f"{match_exc_count} match exception{'s' if match_exc_count != 1 else ''}")
+ subtitle = " | ".join(parts) if parts else "No flags"
+ route_label = route.replace("_", " ").title() if route else "—"
+ error_msg = pipeline_result.get("error", "")
+ error_html = f"
Error: {error_msg}" if error_msg else ""
+ st.markdown(
+ f"
Page {cur_page_idx + 1} / {num_pages}
", + unsafe_allow_html=True) + with nav_cols[2]: + if st.button("Next ➡️", disabled=(cur_page_idx == num_pages - 1), key=f"next_page_{selected_hash}"): + st.session_state.batch_results[selected_hash]["current_page"] = cur_page_idx + 1 + st.rerun() + + st.write(f"**File Hash:** `{selected_hash[:8]}…` | **Pages:** {num_pages}") + + col_a, col_b = st.columns(2) + + with col_a: + if st.button("🔁 Re-Run (All Pages)", key=f"rerun_all_{selected_hash}", + help=f"Re-send all {num_pages} page(s) in one request"): + with st.spinner(f"Re-running inference on all {num_pages} page(s)…"): + try: + raw_json, raw_parsed_data, safe_mapped = _run_extraction(pages) + if raw_json is None: + st.error("No response from vLLM.") + else: + st.session_state.batch_results[selected_hash]["raw_pred"] = raw_json + st.session_state.batch_results[selected_hash]["raw_parsed_data"] = raw_parsed_data + st.session_state.batch_results[selected_hash]["mapped_data"] = safe_mapped + st.session_state.batch_results[selected_hash]["edited_data"] = safe_mapped.copy() + st.session_state.batch_results[selected_hash]["user_saved_edits"] = False + st.session_state.batch_results[selected_hash]["pipeline_result"] = None + if _pipeline_available and st.session_state.get("master_data"): + try: + _pr = build_and_run_pipeline(safe_mapped, st.session_state.master_data, st.session_state.pipeline_configs) + st.session_state.batch_results[selected_hash]["pipeline_result"] = _pr + except Exception as _pe: + st.session_state.batch_results[selected_hash]["pipeline_result"] = {"final_status": "exception", "error": str(_pe), "risk_flags": [], "advisory_flags": [], "match_result": None, "route": "error"} + for k in [k for k in st.session_state.keys() if k.endswith(f"_{selected_hash}")]: + del st.session_state[k] + st.success("✅ Re-run complete (all pages)") + st.rerun() + except Exception as e: + st.error(f"Re-run failed: {e}") + + with col_b: + if st.button(f"🔍 Extract Page {cur_page_idx + 1} Only", key=f"extract_page_{selected_hash}", + help="Send only the currently displayed page"): + with st.spinner(f"Running inference on page {cur_page_idx + 1} only…"): + try: + raw_json, raw_parsed_data, safe_mapped = _run_extraction([pages[cur_page_idx]]) + if raw_json is None: + st.error("No response from vLLM.") + else: + st.session_state.batch_results[selected_hash]["raw_pred"] = raw_json + st.session_state.batch_results[selected_hash]["raw_parsed_data"] = raw_parsed_data + st.session_state.batch_results[selected_hash]["mapped_data"] = safe_mapped + st.session_state.batch_results[selected_hash]["edited_data"] = safe_mapped.copy() + st.session_state.batch_results[selected_hash]["user_saved_edits"] = False + st.session_state.batch_results[selected_hash]["pipeline_result"] = None + if _pipeline_available and st.session_state.get("master_data"): + try: + _pr = build_and_run_pipeline(safe_mapped, st.session_state.master_data, st.session_state.pipeline_configs) + st.session_state.batch_results[selected_hash]["pipeline_result"] = _pr + except Exception as _pe: + st.session_state.batch_results[selected_hash]["pipeline_result"] = {"final_status": "exception", "error": str(_pe), "risk_flags": [], "advisory_flags": [], "match_result": None, "route": "error"} + for k in [k for k in st.session_state.keys() if k.endswith(f"_{selected_hash}")]: + del st.session_state[k] + st.success(f"✅ Page {cur_page_idx + 1} extracted!") + st.rerun() + except Exception as e: + st.error(f"Extraction failed: {e}") + else: + st.info("JSON upload — no image preview available.") + st.write(f"**File:** `{current['file_name']}` | **Source:** JSON") + + with st.expander("🔍 Show raw model output"): + raw_pred = current.get("raw_pred") + if raw_pred is None: + st.warning("No raw output available.") + else: + st.code(str(raw_pred), language="json") + + # ========================================================================= + # RIGHT COLUMN — editable form + # ========================================================================= + with frame_right: + st.subheader(f"Editable Invoice: {current['file_name']}") + pipeline_result = current.get("pipeline_result") + render_status_badge(pipeline_result) + + # Live widgets: keep the data editor outside a form so dynamic row adds/deletes are saved before downloads. + tabs = st.tabs(["Invoice Details", "Sender/Recipient", "Bank Details", "Line Items"]) + + with tabs[0]: + render_field_flag(get_field_flags(pipeline_result, "Invoice Number")) + st.text_input("Invoice Number", key=f"Invoice Number_{selected_hash}") + render_field_flag(get_field_flags(pipeline_result, "PO Number")) + st.text_input("PO Number", key=f"PO Number_{selected_hash}") + + render_field_flag(get_field_flags(pipeline_result, "Invoice Date")) + st.write("**Invoice Date:**") + raw_invoice_date = form_data.get("Invoice Date", "") + if raw_invoice_date: + st.info(f"📅 Model extracted: {raw_invoice_date}") + st.date_input("Select date:", key=f"Invoice Date_{selected_hash}", + format="DD/MM/YYYY", label_visibility="collapsed") + + render_field_flag(get_field_flags(pipeline_result, "Payment Terms")) + st.text_input("Payment Terms", key=f"Payment Terms_{selected_hash}") + + render_field_flag(get_field_flags(pipeline_result, "Due Date")) + st.write("**Due Date:**") + raw_due_date = form_data.get("Due Date", "") + if raw_due_date: + st.info(f"📅 Model extracted: {raw_due_date}") + st.date_input("Select date:", key=f"Due Date_{selected_hash}", + format="DD/MM/YYYY", label_visibility="collapsed") + + render_field_flag(get_field_flags(pipeline_result, "Tax ID")) + st.text_input("Tax ID / VAT Number", key=f"Tax ID_{selected_hash}") + + render_field_flag(get_field_flags(pipeline_result, "Currency")) + curr_options = ["USD", "EUR", "GBP", "INR", "Other"] + if st.session_state[f"Currency_{selected_hash}"] not in curr_options: + st.session_state[f"Currency_{selected_hash}"] = "Other" + st.selectbox("Currency", options=curr_options, key=f"Currency_{selected_hash}") + if st.session_state.get(f"Currency_{selected_hash}") == "Other": + st.text_input("Specify Currency", key=f"Currency_Custom_{selected_hash}") + + render_field_flag(get_field_flags(pipeline_result, "Subtotal")) + st.number_input("Subtotal", key=f"Subtotal_{selected_hash}", format="%.3f", step=0.001) + render_field_flag(get_field_flags(pipeline_result, "Tax Percentage")) + st.number_input("Tax %", key=f"Tax Percentage_{selected_hash}", format="%.3f", step=0.001) + render_field_flag(get_field_flags(pipeline_result, "Total Tax")) + st.number_input("Total Tax", key=f"Total Tax_{selected_hash}", format="%.3f", step=0.001) + st.number_input("Discount Rate %", key=f"Discount Rate_{selected_hash}", format="%.3f", step=0.001) + st.number_input("Total Discount Amount", key=f"Total Discount Amount_{selected_hash}", format="%.3f", step=0.001) + render_field_flag(get_field_flags(pipeline_result, "Total Amount")) + st.number_input("Total Amount", key=f"Total Amount_{selected_hash}", format="%.3f", step=0.001) + + with tabs[1]: + render_field_flag(get_field_flags(pipeline_result, "Sender Name")) + st.text_input("Sender Name", key=f"Sender Name_{selected_hash}") + st.text_area ("Sender Address", key=f"Sender Address_{selected_hash}", height=80) + st.text_input("Recipient Name", key=f"Recipient Name_{selected_hash}") + st.text_area ("Recipient Address", key=f"Recipient Address_{selected_hash}", height=80) + + with tabs[2]: + st.text_input("Bank Name", key=f"Bank_bank_name_{selected_hash}") + render_field_flag(get_field_flags(pipeline_result, "Account Number")) + st.text_input("Account Number", key=f"Bank_bank_account_number_{selected_hash}") + st.text_input("Account Name", key=f"Bank_bank_acc_name_{selected_hash}") + render_field_flag(get_field_flags(pipeline_result, "Bank IBAN")) + st.text_input("IBAN", key=f"Bank_bank_iban_{selected_hash}") + render_field_flag(get_field_flags(pipeline_result, "SWIFT")) + st.text_input("SWIFT", key=f"Bank_bank_swift_{selected_hash}") + st.text_input("Routing", key=f"Bank_bank_routing_{selected_hash}") + st.text_input("Branch", key=f"Bank_bank_branch_{selected_hash}") + + with tabs[3]: + items_state_key = f"items_df_{selected_hash}" + + if items_state_key not in st.session_state: + item_rows = form_data.get("Itemized Data", []) or [] + normalized = [] + for it in item_rows: + if not isinstance(it, dict): + it = {} + normalized.append({ + "Description": it.get("Description", it.get("Item Description", "")), + "Service Name": it.get("Service Name", ""), + "Service Start Date": it.get("Service Start Date", ""), + "Service End Date": it.get("Service End Date", ""), + "SKU": it.get("SKU", ""), + "Quantity": editable_numeric_value("Quantity", it.get("Quantity", it.get("Item Quantity", 0.0))), + "Unit Price": editable_numeric_value("Unit Price", it.get("Unit Price", it.get("Item Unit Price", 0.0))), + "Amount": editable_numeric_value("Amount", it.get("Amount", it.get("Item Amount", 0.0))), + "Discount Rate Per Item": editable_numeric_value("Discount Rate Per Item", it.get("Discount Rate Per Item", 0.0)), + "Discount Amount Per Item": editable_numeric_value("Discount Amount Per Item", it.get("Discount Amount Per Item", 0.0)), + "Tax Rate Per Item": editable_numeric_value("Tax Rate Per Item", it.get("Tax Rate Per Item", 0.0)), + "Tax Amount Per Item": editable_numeric_value("Tax Amount Per Item", it.get("Tax Amount Per Item", 0.0)), + "IO Number/Cost Centre": it.get("IO Number/Cost Centre", ""), + "Line Total": editable_numeric_value("Line Total", it.get("Line Total", it.get("Item Line Total", 0.0))), + }) + st.session_state[items_state_key] = coerce_numeric_items_df( + pd.DataFrame(normalized) if normalized + else pd.DataFrame(columns=[ + "Description", "Service Name", "Service Start Date", "Service End Date", "SKU", + "Quantity", "Unit Price", "Amount", + "Discount Rate Per Item", "Discount Amount Per Item", + "Tax Rate Per Item", "Tax Amount Per Item", + "IO Number/Cost Centre", "Line Total" + ]) + ) + + items_df = coerce_numeric_items_df(st.session_state[items_state_key]) + + # Build read-only Flags column from pipeline result + _flags_col = build_flags_column(pipeline_result, len(items_df)) + items_df_display = items_df.copy() + items_df_display.insert(0, "Flags", _flags_col) + + column_config = { + "Flags": st.column_config.TextColumn("Flags", width="medium"), + "Description": st.column_config.TextColumn("Description", width="large"), + "Service Name": st.column_config.TextColumn("Service Name", width="medium"), + "Service Start Date": st.column_config.TextColumn("Svc Start", width="small"), + "Service End Date": st.column_config.TextColumn("Svc End", width="small"), + "SKU": st.column_config.TextColumn("SKU", width="small"), + "Quantity": st.column_config.NumberColumn("Qty", width="small", format="%.3f", step=0.001), + "Unit Price": st.column_config.NumberColumn("Unit Price", width="small", format="%.3f", step=0.001), + "Amount": st.column_config.NumberColumn("Amount", width="small", format="%.3f", step=0.001), + "Discount Rate Per Item": st.column_config.NumberColumn("Disc %", width="small", format="%.3f", step=0.001), + "Discount Amount Per Item": st.column_config.NumberColumn("Disc Amt", width="small", format="%.3f", step=0.001), + "Tax Rate Per Item": st.column_config.NumberColumn("Tax %", width="small", format="%.3f", step=0.001), + "Tax Amount Per Item": st.column_config.NumberColumn("Tax Amt", width="small", format="%.3f", step=0.001), + "IO Number/Cost Centre": st.column_config.TextColumn("IO/Cost Centre", width="medium"), + "Line Total": st.column_config.NumberColumn("Line Total", width="small", format="%.3f", step=0.001), + } + + editor_key = f"items_editor_{selected_hash}" + edited_df = st.data_editor( + items_df_display, + num_rows="dynamic", + key=editor_key, + use_container_width=True, + height=DATA_EDITOR_HEIGHT, + column_config=column_config, + disabled=["Flags"], + ) + + saved = st.button("💾 Save All Edits", key=f"save_all_edits_{selected_hash}") + + currency = st.session_state.get(f"Currency_{selected_hash}", "USD") + if currency == "Other": + currency = st.session_state.get(f"Currency_Custom_{selected_hash}", "") + + items_state_key = f"items_df_{selected_hash}" + current_items_df = coerce_numeric_items_df(st.session_state.get(items_state_key, pd.DataFrame())) + st.session_state[items_state_key] = current_items_df + line_items_list = current_items_df.to_dict("records") + + def _build_updated_dict(): + current_df = apply_data_editor_state( + st.session_state.get(items_state_key, pd.DataFrame()), + st.session_state.get(f"items_editor_{selected_hash}"), + ) + current_df = current_df.drop(columns=["Flags"], errors="ignore") + current_line_items = current_df.to_dict("records") + updated = { + "Invoice Number": st.session_state.get(f"Invoice Number_{selected_hash}", ""), + "PO Number": st.session_state.get(f"PO Number_{selected_hash}", ""), + # Raw model string — not the date picker's reformatted value + "Invoice Date": st.session_state.get(f"Invoice Date_raw_{selected_hash}", ""), + "Payment Terms": st.session_state.get(f"Payment Terms_{selected_hash}", ""), + "Due Date": st.session_state.get(f"Due Date_raw_{selected_hash}", ""), + "Tax ID": st.session_state.get(f"Tax ID_{selected_hash}", ""), + "Currency": currency, + "Subtotal": editable_numeric_value("Subtotal", st.session_state.get(f"Subtotal_{selected_hash}", 0.0)), + "Tax Percentage": editable_numeric_value("Tax Percentage", st.session_state.get(f"Tax Percentage_{selected_hash}", 0.0)), + "Total Tax": editable_numeric_value("Total Tax", st.session_state.get(f"Total Tax_{selected_hash}", 0.0)), + "Discount Rate": editable_numeric_value("Discount Rate", st.session_state.get(f"Discount Rate_{selected_hash}", 0.0)), + "Total Discount Amount": editable_numeric_value("Total Discount Amount", st.session_state.get(f"Total Discount Amount_{selected_hash}", 0.0)), + "Total Amount": editable_numeric_value("Total Amount", st.session_state.get(f"Total Amount_{selected_hash}", 0.0)), + "Sender Name": st.session_state.get(f"Sender Name_{selected_hash}", ""), + "Sender Address": st.session_state.get(f"Sender Address_{selected_hash}", ""), + "Recipient Name": st.session_state.get(f"Recipient Name_{selected_hash}", ""), + "Recipient Address": st.session_state.get(f"Recipient Address_{selected_hash}", ""), + "Bank Details": { + "bank_name": st.session_state.get(f"Bank_bank_name_{selected_hash}", ""), + "bank_account_number": st.session_state.get(f"Bank_bank_account_number_{selected_hash}", ""), + "bank_acc_name": st.session_state.get(f"Bank_bank_acc_name_{selected_hash}", ""), + "bank_iban": st.session_state.get(f"Bank_bank_iban_{selected_hash}", ""), + "bank_swift": st.session_state.get(f"Bank_bank_swift_{selected_hash}", ""), + "bank_routing": st.session_state.get(f"Bank_bank_routing_{selected_hash}", ""), + "bank_branch": st.session_state.get(f"Bank_bank_branch_{selected_hash}", ""), + }, + "Itemized Data": current_line_items, + "Sender": {"Name": st.session_state.get(f"Sender Name_{selected_hash}", ""), + "Address": st.session_state.get(f"Sender Address_{selected_hash}", "")}, + "Recipient": {"Name": st.session_state.get(f"Recipient Name_{selected_hash}", ""), + "Address": st.session_state.get(f"Recipient Address_{selected_hash}", "")}, + } + return limit_invoice_decimals(updated) + + if saved: + updated = _build_updated_dict() + # Persist user-edited date picker values back as the date strings + invoice_date = st.session_state.get(f"Invoice Date_{selected_hash}") + due_date = st.session_state.get(f"Due Date_{selected_hash}") + if invoice_date is not None: + try: + updated["Invoice Date"] = invoice_date.strftime("%d-%b-%Y") + st.session_state[f"Invoice Date_raw_{selected_hash}"] = invoice_date.strftime("%d-%b-%Y") + except (AttributeError, ValueError): + pass + if due_date is not None: + try: + updated["Due Date"] = due_date.strftime("%d-%b-%Y") + st.session_state[f"Due Date_raw_{selected_hash}"] = due_date.strftime("%d-%b-%Y") + except (AttributeError, ValueError): + pass + updated = limit_invoice_decimals(updated) + st.session_state.batch_results[selected_hash]["edited_data"] = updated + st.session_state.batch_results[selected_hash]["user_saved_edits"] = True + if _pipeline_available and st.session_state.get("master_data"): + try: + _pr = build_and_run_pipeline(updated, st.session_state.master_data, st.session_state.pipeline_configs) + st.session_state.batch_results[selected_hash]["pipeline_result"] = _pr + except Exception as _pe: + st.session_state.batch_results[selected_hash]["pipeline_result"] = {"final_status": "exception", "error": str(_pe), "risk_flags": [], "advisory_flags": [], "match_result": None, "route": "error"} + for k in [k for k in list(st.session_state.keys()) if k.endswith(f"_{selected_hash}")]: + del st.session_state[k] + st.success("✅ Saved") + st.rerun() + + download_data = limit_invoice_decimals(_build_updated_dict()) + rows = flatten_invoice_to_rows(download_data) + file_df = pd.DataFrame(rows) + file_stem = Path(current["file_name"]).stem + + per_dl_cols = st.columns(3) + with per_dl_cols[0]: + st.download_button("📥 CSV", + file_df.to_csv(index=False).encode("utf-8"), + file_name=f"{file_stem}_full.csv", + mime="text/csv", + key=f"dl_csv_{selected_hash}") + with per_dl_cols[1]: + st.download_button("📥 Excel", + build_excel_bytes(file_df), + file_name=f"{file_stem}_full.xlsx", + mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + key=f"dl_xlsx_{selected_hash}") + with per_dl_cols[2]: + record = convert_to_jsonl_record(download_data, current["file_name"], num_pages) + jsonl_one = json.dumps(record, ensure_ascii=False).encode("utf-8") + st.download_button("📥 JSONL", + jsonl_one, + file_name=f"{file_stem}.jsonl", + mime="application/jsonl", + key=f"dl_jsonl_{selected_hash}") + + render_matching_result(pipeline_result) + +# ============================================================================= +# Processing placeholder +# ============================================================================= +elif st.session_state.is_processing_batch: + with frame_left: + st.info("⏳ Processing batch… Please wait.") + st.progress(0) + with frame_right: + st.caption("Preview & editor will appear here after extraction.") -""" -# Welcome to Streamlit! - -Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:. -If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community -forums](https://discuss.streamlit.io). - -In the meantime, below is an example of what you can do with just a few lines of code: -""" - -num_points = st.slider("Number of points in spiral", 1, 10000, 1100) -num_turns = st.slider("Number of turns in spiral", 1, 300, 31) - -indices = np.linspace(0, 1, num_points) -theta = 2 * np.pi * num_turns * indices -radius = indices - -x = radius * np.cos(theta) -y = radius * np.sin(theta) - -df = pd.DataFrame({ - "x": x, - "y": y, - "idx": indices, - "rand": np.random.randn(num_points), -}) - -st.altair_chart(alt.Chart(df, height=700, width=700) - .mark_point(filled=True) - .encode( - x=alt.X("x", axis=None), - y=alt.Y("y", axis=None), - color=alt.Color("idx", legend=None, scale=alt.Scale()), - size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])), - )) \ No newline at end of file +else: + with frame_left: + st.caption("Ready when you are.") + with frame_right: + st.caption("Preview & editor will appear here after extraction.") \ No newline at end of file