97 lines
3.6 KiB
Python
97 lines
3.6 KiB
Python
# Builds the Transactions + Summary Excel workbook, entirely in memory - callers decide where it ends up (an in-memory buffer streamed straight to the client, see routes/extraction.py's /api/export).
|
|
import openpyxl
|
|
from openpyxl.styles import Font
|
|
|
|
FONT_NAME = "Arial"
|
|
FONT_SIZE = 11
|
|
REGULAR_FONT = Font(name=FONT_NAME, size=FONT_SIZE, bold=False)
|
|
BOLD_FONT = Font(name=FONT_NAME, size=FONT_SIZE, bold=True)
|
|
ACCOUNTING_FORMAT = '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)'
|
|
|
|
_FLAGGED_INDEXES_BY_CHECK = {
|
|
"running_balance": "mismatches", "date_sanity": "issues", "structure_sanity": "issues",
|
|
"narrative_direction": "issues",
|
|
}
|
|
|
|
|
|
def _write_cell(ws, row, col, value, bold=False, number_format=None):
|
|
c = ws.cell(row=row, column=col, value=value)
|
|
c.font = BOLD_FONT if bold else REGULAR_FONT
|
|
if number_format:
|
|
c.number_format = number_format
|
|
return c
|
|
|
|
|
|
def _flagged_row_indexes(result):
|
|
"""{transaction_index: ["running_balance", ...]} - which checks
|
|
flagged which specific row, for the Transactions sheet's own flag
|
|
column."""
|
|
flags = {}
|
|
for check in result["checks"]:
|
|
key = _FLAGGED_INDEXES_BY_CHECK.get(check["name"])
|
|
if not key:
|
|
continue
|
|
for item in check.get(key, []):
|
|
idx = item.get("index")
|
|
if idx is not None:
|
|
flags.setdefault(idx, []).append(check["name"])
|
|
return flags
|
|
|
|
|
|
def build_workbook(result, title="Statement Extraction"):
|
|
"""
|
|
Builds the Transactions + Summary workbook in memory - never touches disk itself.
|
|
|
|
Data flow:
|
|
result (dict) - a pipeline_service.run()/recheck() result:
|
|
transactions, checks, status, period, etc.
|
|
title (str) - statement title shown on the Summary sheet
|
|
returns (openpyxl.Workbook) - the built, unsaved workbook
|
|
"""
|
|
wb = openpyxl.Workbook()
|
|
|
|
ws = wb.active
|
|
ws.title = "Transactions"
|
|
for col, width in zip("ABCDEF", (14, 60, 14, 16, 16, 16)):
|
|
ws.column_dimensions[col].width = width
|
|
headers = ["Date", "Description", "Amount", "Balance", "Status", "Check flags"]
|
|
for col, label in enumerate(headers, start=1):
|
|
_write_cell(ws, 1, col, label, bold=True)
|
|
|
|
flagged = _flagged_row_indexes(result)
|
|
row = 2
|
|
for i, t in enumerate(result["transactions"]):
|
|
_write_cell(ws, row, 1, t["date"])
|
|
_write_cell(ws, row, 2, t["description"])
|
|
_write_cell(ws, row, 3, t["amount"], number_format=ACCOUNTING_FORMAT)
|
|
_write_cell(ws, row, 4, t["balance"], number_format=ACCOUNTING_FORMAT)
|
|
_write_cell(ws, row, 5, t["status"])
|
|
_write_cell(ws, row, 6, ", ".join(flagged.get(i, [])))
|
|
row += 1
|
|
|
|
ws2 = wb.create_sheet("Summary")
|
|
for col, width in zip("AB", (30, 50)):
|
|
ws2.column_dimensions[col].width = width
|
|
_write_cell(ws2, 1, 1, title, bold=True)
|
|
r = 3
|
|
_write_cell(ws2, r, 1, "Status", bold=True)
|
|
_write_cell(ws2, r, 2, result["status"], bold=True)
|
|
r += 1
|
|
if result.get("period"):
|
|
_write_cell(ws2, r, 1, "Statement period")
|
|
_write_cell(ws2, r, 2, result["period"])
|
|
r += 1
|
|
if result.get("brought_forward_balance") is not None:
|
|
_write_cell(ws2, r, 1, "Statement's own balance brought forward")
|
|
_write_cell(ws2, r, 2, result["brought_forward_balance"], number_format=ACCOUNTING_FORMAT)
|
|
r += 1
|
|
r += 1
|
|
_write_cell(ws2, r, 1, "Check results", bold=True)
|
|
r += 1
|
|
for c in result["checks"]:
|
|
_write_cell(ws2, r, 1, c["name"])
|
|
_write_cell(ws2, r, 2, f"{c['outcome']}: {c['detail']}")
|
|
r += 1
|
|
|
|
return wb
|