166 lines
5.6 KiB
Python
166 lines
5.6 KiB
Python
"""Wave 0 Customer Credit Limit & Exposure Mock Adapter for Iteration 04.
|
|
|
|
Pre-seeds deterministic customer profiles across wholesale, processor,
|
|
institutional, and distributor segments with sanctioned credit limits
|
|
and current outstanding accounts receivable balances.
|
|
"""
|
|
from decimal import Decimal, ROUND_HALF_UP
|
|
from typing import Dict, Any, List, Optional
|
|
|
|
|
|
CUSTOMERS = [
|
|
{
|
|
"id": "CUST-001",
|
|
"name": "Adani Wilmar Ltd",
|
|
"segment": "Processor",
|
|
"city": "Indore",
|
|
"state": "Madhya Pradesh",
|
|
"credit_limit_inr": Decimal("5000000.00"),
|
|
"outstanding_inr": Decimal("1250000.00"),
|
|
"payment_terms": "30 Days",
|
|
"is_active": True,
|
|
},
|
|
{
|
|
"id": "CUST-002",
|
|
"name": "Patanjali Foods Ltd",
|
|
"segment": "Processor",
|
|
"city": "Nagda",
|
|
"state": "Madhya Pradesh",
|
|
"credit_limit_inr": Decimal("7500000.00"),
|
|
"outstanding_inr": Decimal("6500000.00"),
|
|
"payment_terms": "15 Days",
|
|
"is_active": True,
|
|
},
|
|
{
|
|
"id": "CUST-003",
|
|
"name": "Kargil Grain Traders",
|
|
"segment": "Wholesale",
|
|
"city": "Bhopal",
|
|
"state": "Madhya Pradesh",
|
|
"credit_limit_inr": Decimal("2000000.00"),
|
|
"outstanding_inr": Decimal("1750000.00"),
|
|
"payment_terms": "CAD",
|
|
"is_active": True,
|
|
},
|
|
{
|
|
"id": "CUST-004",
|
|
"name": "Maruti Agro Distributors",
|
|
"segment": "Distributor",
|
|
"city": "Ujjain",
|
|
"state": "Madhya Pradesh",
|
|
"credit_limit_inr": Decimal("1500000.00"),
|
|
"outstanding_inr": Decimal("200000.00"),
|
|
"payment_terms": "15 Days",
|
|
"is_active": True,
|
|
},
|
|
{
|
|
"id": "CUST-005",
|
|
"name": "National Agricultural Federation (NAFED)",
|
|
"segment": "Institutional",
|
|
"city": "New Delhi",
|
|
"state": "Delhi",
|
|
"credit_limit_inr": Decimal("10000000.00"),
|
|
"outstanding_inr": Decimal("0.00"),
|
|
"payment_terms": "Advance",
|
|
"is_active": True,
|
|
},
|
|
{
|
|
"id": "cpty-demo-customer-01",
|
|
"name": "MP Soya Extraction Industries Ltd",
|
|
"segment": "Processor",
|
|
"city": "Pithampur",
|
|
"state": "Madhya Pradesh",
|
|
"credit_limit_inr": Decimal("5000000.00"),
|
|
"outstanding_inr": Decimal("4150000.00"),
|
|
"payment_terms": "15 Days",
|
|
"is_active": True,
|
|
},
|
|
]
|
|
|
|
|
|
class CustomerCreditMock:
|
|
"""Mock helper for customer credit risk and exposure calculations."""
|
|
|
|
@classmethod
|
|
def get_customer(cls, customer_id: str) -> Optional[Dict[str, Any]]:
|
|
for c in CUSTOMERS:
|
|
if c["id"] == customer_id:
|
|
return dict(c)
|
|
return None
|
|
|
|
@classmethod
|
|
def list_customers(cls, segment: Optional[str] = None) -> List[Dict[str, Any]]:
|
|
results = []
|
|
for c in CUSTOMERS:
|
|
if segment is None or c["segment"].lower() == segment.lower():
|
|
results.append(dict(c))
|
|
return results
|
|
|
|
@classmethod
|
|
def calculate_projected_exposure(
|
|
cls, customer_id: str, new_order_value_inr: Decimal
|
|
) -> Dict[str, Any]:
|
|
"""Calculates pre-order exposure metrics per STORY-SALES-002 / FR-SALESCONTRACTS-002."""
|
|
cust = cls.get_customer(customer_id)
|
|
if not cust:
|
|
raise ValueError(f"Customer '{customer_id}' not found in registry")
|
|
|
|
credit_limit = cust["credit_limit_inr"]
|
|
current_outstanding = cust["outstanding_inr"]
|
|
projected_exposure = current_outstanding + new_order_value_inr
|
|
|
|
if credit_limit <= Decimal("0.00"):
|
|
exposure_ratio_pct = Decimal("100.00") if projected_exposure > 0 else Decimal("0.00")
|
|
else:
|
|
exposure_ratio_pct = ((projected_exposure / credit_limit) * Decimal("100.0")).quantize(
|
|
Decimal("0.01"), rounding=ROUND_HALF_UP
|
|
)
|
|
|
|
warning_flag = exposure_ratio_pct > Decimal("80.00")
|
|
hard_block = exposure_ratio_pct > Decimal("100.00")
|
|
|
|
return {
|
|
"customer_id": customer_id,
|
|
"customer_name": cust["name"],
|
|
"credit_limit_inr": credit_limit,
|
|
"current_outstanding_inr": current_outstanding,
|
|
"new_order_value_inr": new_order_value_inr,
|
|
"projected_exposure_inr": projected_exposure,
|
|
"exposure_ratio_pct": exposure_ratio_pct,
|
|
"warning_flag": warning_flag,
|
|
"hard_block": hard_block,
|
|
"requires_partner_override": hard_block,
|
|
}
|
|
|
|
@classmethod
|
|
def update_outstanding(cls, customer_id: str, delta_inr: Decimal) -> Dict[str, Any]:
|
|
"""Adjusts outstanding receivables balance after invoicing or settlement."""
|
|
for c in CUSTOMERS:
|
|
if c["id"] == customer_id:
|
|
c["outstanding_inr"] = (c["outstanding_inr"] + delta_inr).quantize(Decimal("0.01"))
|
|
return dict(c)
|
|
raise ValueError(f"Customer '{customer_id}' not found in registry")
|
|
|
|
@classmethod
|
|
def reset_for_tests(cls) -> None:
|
|
"""Resets all customer balances to baseline values for isolated test execution."""
|
|
baselines = {
|
|
"CUST-001": Decimal("1250000.00"),
|
|
"CUST-002": Decimal("6500000.00"),
|
|
"CUST-003": Decimal("1750000.00"),
|
|
"CUST-004": Decimal("200000.00"),
|
|
"CUST-005": Decimal("0.00"),
|
|
"cpty-demo-customer-01": Decimal("4150000.00"),
|
|
}
|
|
for c in CUSTOMERS:
|
|
if c["id"] in baselines:
|
|
c["outstanding_inr"] = baselines[c["id"]]
|
|
|
|
@classmethod
|
|
def get_all_customers(cls) -> List[Dict[str, Any]]:
|
|
return cls.list_customers()
|
|
|
|
|
|
customer_credit_mock = CustomerCreditMock()
|
|
|