File size: 4,887 Bytes
3dfe050
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
"""
make_india_blank_dates.py — synthetic copy of a common Indian e-statement layout
(Standard Chartered style), with made-up data. It reproduces the traits that broke
the converter on a real statement:

  - the table is only framed (no row rules), so pdfplumber returns one giant row
  - left-aligned headers above right-aligned amounts, in columns whose whitespace
    gaps don't line up with the headers
  - same-day transactions leave the Date and Value Date cells blank
  - descriptions wrap over several lines with no amounts
  - "dd Mon yy" dates, a BALANCE FORWARD row at the top of every page,
    a TOTAL row, and a disclaimer footer below the table

Writes tests/fixtures/hard/t9_india_blank_dates.pdf
"""
from __future__ import annotations

import random
from pathlib import Path

from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas

OUT = Path(__file__).resolve().parent.parent / "fixtures" / "hard" / "t9_india_blank_dates.pdf"

random.seed(19)
NARR = [
    ["UPI/{n}/", "PAYTM/ADD-MONEY@PAYTM/PYTM0123456/", "{n}/"],
    ["ATM WITHDRAWAL SELF-SWITCH", "AT NFS 20:10:24/{n}", "00000000150000/INR"],
    ["PURCHASE SAMPLE STORE", "CHENNAI IN 10:13:08/726630"],
    ["NEFT SAMPLE EMPLOYER PVT LTD", "SALARY"],
    ["CGST @ 9.00%"],
    ["IMPS/P2A/{n}/0350015", "SAMPLE PAYEE"],
]


def money(c: int) -> str:
    return f"{c / 100:,.2f}"


def make_rows(n=34, opening=11_445_365):
    rows, bal, day = [], opening, 3
    for i in range(n):
        if random.random() < 0.55:
            day += 1
        credit = random.random() < 0.3
        amt = random.randint(1_000, 2_500_000 if credit else 400_000)
        bal += amt if credit else -amt
        lines = [s.format(n=917400000000 + i) for s in random.choice(NARR)]
        rows.append({"day": min(day, 28), "lines": lines, "cr": amt if credit else 0,
                     "dr": 0 if credit else amt, "bal": bal})
    return rows, opening, bal


def draw():
    rows, opening, closing = make_rows()
    c = canvas.Canvas(str(OUT), pagesize=A4)
    W, H = A4
    X = {"date": 40, "value": 95, "desc": 150, "chq": 330, "dep": 380, "wdl": 445, "bal": 515}
    R = {"dep": 425, "wdl": 495, "bal": 565}          # right edges of amount columns
    lh = 10

    def header(y):
        c.setFont("Helvetica-Bold", 8)
        c.drawString(X["value"], y + 9, "Value")
        for k, t in (("date", "Date"), ("desc", "Description"), ("chq", "Cheque"),
                     ("dep", "Deposit"), ("wdl", "Withdrawal"), ("bal", "Balance")):
            c.drawString(X[k], y, t)
        c.drawString(X["value"], y - 9, "Date")
        c.setFont("Helvetica", 8)
        return y - 24

    def row(y, d, lines, dep="", wdl="", bal=""):
        if d:
            c.drawString(X["date"], y, d)
            c.drawString(X["value"], y, d)
        c.drawString(X["desc"], y, lines[0])
        for k, v in (("dep", dep), ("wdl", wdl), ("bal", bal)):
            if v:
                c.drawRightString(R[k], y, v)
        for extra in lines[1:]:
            y -= lh
            c.drawString(X["desc"] + 6, y, extra)
        return y - lh

    def footer(page, top):
        c.rect(35, top, W - 70, H - 140 - top)        # frame only, no row rules
        c.setFont("Helvetica", 7)
        c.drawString(40, 70, "Bank deposits are insured up to Rs 5 lakh per depositor.")
        c.drawString(40, 60, "Report irregularities within 30 days of the statement date.")
        c.drawString(40, 40, f"Page {page}")
        c.setFont("Helvetica", 8)

    c.setFont("Helvetica-Bold", 11)
    c.drawString(40, H - 60, "ACCOUNT STATEMENT")
    c.setFont("Helvetica", 8)
    c.drawString(380, H - 80, "ACCOUNT NO.     : 99999990001")
    c.drawString(380, H - 92, "CURRENCY INR")

    page, y = 1, header(H - 150)
    month = "Jun 25"
    last_day = None
    y = row(y, f"03 {month}", ["BALANCE FORWARD"], bal=money(opening))
    for r in rows:
        if y < 140:
            footer(page, y)
            c.showPage()
            page += 1
            c.setFont("Helvetica", 8)
            y = header(H - 150)
            y = row(y, f"{last_day:02d} {month}", ["BALANCE FORWARD"], bal=money(prev_bal))
        d = f"{r['day']:02d} {month}" if r["day"] != last_day else ""
        y = row(y, d, r["lines"], dep=money(r["cr"]) if r["cr"] else "",
                wdl=money(r["dr"]) if r["dr"] else "", bal=money(r["bal"]))
        last_day, prev_bal = r["day"], r["bal"]
    tot_cr = sum(r["cr"] for r in rows)
    tot_dr = sum(r["dr"] for r in rows)
    c.setFont("Helvetica-Bold", 8)
    c.drawString(X["desc"] + 60, y, "TOTAL")
    c.drawRightString(R["dep"], y, money(tot_cr))
    c.drawRightString(R["wdl"], y, money(tot_dr))
    c.drawRightString(R["bal"], y, money(closing))
    footer(page, y - 6)
    c.save()
    return len(rows), tot_cr / 100, tot_dr / 100, closing / 100


if __name__ == "__main__":
    print(OUT, draw())