Spaces:
Sleeping
Sleeping
File size: 4,628 Bytes
190b318 565c94a 190b318 565c94a 190b318 565c94a 190b318 565c94a 190b318 565c94a 190b318 565c94a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | from pydantic import BaseModel, Field, validator
from typing import Optional, List, Dict, Any, Tuple
from datetime import datetime
from enum import Enum
import pandas as pd
class MatchStatus(str, Enum):
EXACT_MATCH = "exact_match"
PARTIAL_MATCH = "partial_match"
UNMATCHED = "unmatched"
APPROVED = "approved"
DISCREPANCY = "discrepancy"
class InvoiceRecord(BaseModel):
"""Canonical invoice record schema"""
id: str
document_type: str
document_number: str
reference_number: Optional[str] = None
po_number: Optional[str] = None
invoice_date: datetime
due_date: Optional[datetime] = None
total_billed: float
amount_outstanding: float
discount: Optional[float] = None
credit_advances_applied: Optional[float] = None
payment_amount: Optional[float] = None
ar_status: Optional[str] = None
open_balance: float
class Config:
arbitrary_types_allowed = True
class PaymentRecord(BaseModel):
"""Canonical payment record schema"""
id: str
document_type: str = "Payment"
document_number: str
reference_number: Optional[str] = None
po_number: Optional[str] = None
invoice_date: datetime
due_date: Optional[datetime] = None
total_billed: float
amount_outstanding: float
discount: Optional[float] = None
credit_advances_applied: Optional[float] = None
payment_amount: float
ar_status: Optional[str] = None
open_balance: float
class ReconciliationMatch(BaseModel):
"""Individual reconciliation match result"""
record_id: str
invoice_record: InvoiceRecord
match_status: MatchStatus
linked_payment_id: Optional[str] = None
payment_suggestions: List[Tuple[PaymentRecord, float]] = [] # (payment, confidence_score)
applied_payment_amount: Optional[float] = None
final_ar_status: Optional[str] = None
admin_notes: Optional[str] = None
confidence_score: float = 0.0
is_approved: bool = False
approved_by: Optional[str] = None
approved_at: Optional[datetime] = None
def to_dict(self) -> Dict[str, Any]:
return {
'record_id': self.record_id,
'document_number': self.invoice_record.document_number,
'invoice_date': self.invoice_record.invoice_date.strftime('%Y-%m-%d') if self.invoice_record.invoice_date else '',
'total_billed': self.invoice_record.total_billed,
'amount_outstanding': self.invoice_record.amount_outstanding,
'payment_amount': self.applied_payment_amount or 0,
'match_status': self.match_status.value,
'confidence_score': f"{self.confidence_score:.1%}",
'is_approved': self.is_approved,
'actions': 'Review' if self.match_status in [MatchStatus.PARTIAL_MATCH, MatchStatus.UNMATCHED] else 'View'
}
class ReconciliationResult(BaseModel):
"""Complete reconciliation results"""
matches: List[ReconciliationMatch] = []
statistics: Dict[str, Any] = {}
def get_statistics(self) -> Dict[str, Any]:
exact_matches = len([m for m in self.matches if m.match_status == MatchStatus.EXACT_MATCH])
partial_matches = len([m for m in self.matches if m.match_status == MatchStatus.PARTIAL_MATCH])
unmatched = len([m for m in self.matches if m.match_status == MatchStatus.UNMATCHED])
approved = len([m for m in self.matches if m.is_approved])
return {
'total_invoices': len(self.matches),
'total_payments': len([m for m in self.matches if m.payment_suggestions]),
'exact_matches': exact_matches,
'partial_matches': partial_matches,
'unmatched': unmatched,
'approved': approved,
'requires_action': partial_matches + unmatched
}
def to_dataframe(self) -> pd.DataFrame:
"""Convert results to pandas DataFrame for display"""
if not self.matches:
return pd.DataFrame()
data = [match.to_dict() for match in self.matches]
return pd.DataFrame(data)
def get_record(self, record_index: int) -> Optional[ReconciliationMatch]:
"""Get specific record by index"""
if 0 <= record_index < len(self.matches):
return self.matches[record_index]
return None
class AdminCorrection(BaseModel):
"""Admin correction data model"""
linked_payment_id: Optional[str] = None
payment_amount: Optional[float] = None
ar_status: Optional[str] = None
admin_notes: Optional[str] = None
action: str = "approve" # 'approve' or 'discrepancy' |