"""ESM_1 (Online Resource 1)
Article: Settlement Speed and Merchant Liquidity in National QR Systems
Journal: Digital Finance
Author: Molla Md Rezaul Karim, Rupali Bank PLC, Dhaka, Bangladesh (corresponding author; rezakarim2003@gmail.com; ORCID 0009-0005-0806-074X)

Replication script. Reproduces Tables 3, 4, 5, 6 and 7, the counterfactual system float (Section 6.1),
the acquirer float (Section 7) and the working-capital stock (Section 4.3) from the parameters in Table 2.
Requirements: Python 3.8 or later, standard library only. Run: python ESM_1.py
"""
import datetime as dt

LBAR = 10 / 7  # Lemma 1: next-business-day payout, Sunday-Thursday working week, uniform sales
RATES = {"A: deposit rate": 0.0616, "B: bank lending": 0.1183, "C: NBFC lending": 0.1364, "D: microcredit ceiling": 0.24}
V_MERCHANT = 5000          # Tk per day, illustrative micro-merchant
V_SYSTEM = 143.54          # Tk crore per day, Bangla QR, 30 Sep 2026

def price_cost(r, lbar=LBAR):  # c = r * Lbar / 365
    return r * lbar / 365

def capacity_loss(q, G, lbar=LBAR):  # ell = q*Lbar / (G + q*Lbar)
    return q * lbar / (G + q * lbar)

print("Table 3: price channel")
for k, r in RATES.items():
    c = price_cost(r)
    print(f"  {k:24s} cost={c*100:.3f}% of sales  vs 1% MDR={c/0.01*100:.1f}%  annual Tk {V_MERCHANT*365*c:,.0f}")

print("\nCounterfactual system float (universal next-business-day payout)")
F = V_SYSTEM * LBAR
print(f"  float = Tk {F:.0f} crore; annual cost B = Tk {F*RATES['B: bank lending']:.0f} crore, D = Tk {F*0.24:.0f} crore")

print("\nTable 4 / Table 6: capacity reduction (%)")
qs = [0.05, 0.1, 0.3, 0.5, 0.7]
for G in [2, 3, 5, 7, 14, 21, 30]:
    print(f"  G={G:2d}: " + "  ".join(f"q={q}: {capacity_loss(q,G)*100:5.1f}" for q in qs))
print("  Lag sensitivity (G=7, q=0.3): " + ", ".join(f"L={L:.2f}: {capacity_loss(0.3,7,L)*100:.1f}" for L in [1, LBAR, 2, 3]))

print("\nTable 5: forgone gross profit (% of baseline sales)")
for G, q in [(14, 0.1), (7, 0.3), (3, 0.5)]:
    l = capacity_loss(q, G)
    print(f"  G={G}, q={q}: ell={l*100:.1f}%  m=10%: {l*10:.2f}%  m=20%: {l*20:.2f}%")

print("\nTable 7: 2026 calendar effective lag")
D = dt.date
def lbar_2026(fitr, adha):
    def closed(d):
        if adha and d == D(2026, 5, 23):  # Saturday opened by Bangladesh Bank directive
            return False
        if fitr and D(2026, 3, 17) <= d <= D(2026, 3, 23):
            return True
        if adha and D(2026, 5, 25) <= d <= D(2026, 5, 31):
            return True
        return d.weekday() in (4, 5)  # Friday, Saturday
    def lag(d):
        n = d + dt.timedelta(1)
        while closed(n):
            n += dt.timedelta(1)
        return (n - d).days
    days = [D(2026, 1, 1) + dt.timedelta(i) for i in range(365)]
    return sum(lag(d) for d in days) / 365
for label, f, a in [("weekend only", 0, 0), ("+ Eid-ul-Fitr", 1, 0), ("+ Eid-ul-Azha", 0, 1), ("+ both", 1, 1)]:
    print(f"  {label:14s} Lbar = {lbar_2026(f, a):.3f}")
L = lbar_2026(1, 1)
print(f"  with both closures: cost D = {price_cost(0.24, L)*100:.3f}%, capacity loss (G=7,q=0.3) = {capacity_loss(0.3, 7, L)*100:.1f}%")

# --- Acquirer float (Section 7) and working-capital stock (Section 4.3) ---
V_DAILY = 143.54          # Tk crore, Bangla QR daily value, September 2026
LB = 10/7                 # effective lag (Lemma 1)
print("\nAcquirer float under instant payout and T+1 interbank settlement (Tk crore)")
for s_off in (0.50, 0.75, 1.00):
    F = s_off * V_DAILY * LB
    print(f"  off-us share {s_off:.0%}: float {F:6.1f}; annual cost at 6.16% {F*0.0616:5.1f}; at 11.83% {F*0.1183:5.1f}")
for r in (0.0616, 0.1183):
    print(f"  cost per unit of off-us value at r={r:.2%}: {100*r*LB/365:.3f}% (acquirer incentive 0.10%)")
q, m = 0.3, 0.10
print(f"\nExtra working capital, central case: qL = {q*LB:.2f} days of cost-sales; = {q*LB*(1-m)/m:.1f} days of gross profit at m = {m:.0%}")
