# Builds a plain, generic transactions CSV export of an extraction result - callers decide where it ends up (an in-memory buffer streamed to the client, see routes/extraction.py's /api/export/csv). """ A plain CSV export - the same Date/Description/Amount/Balance/Status/ Check flags data excel_service.py's own Transactions sheet already writes, just as one flat CSV file instead of a workbook sheet. Distinct from xero_csv_service.py (which writes a specific Date/Amount/Payee/ Description shape, chosen for Xero's own import requirements) - this one is a general-purpose dump of the statement's own data, not formatted for any particular accounting software's own import rules, so every row and every field is written as-is, nothing skipped. Reuses excel_service.py's own `_flagged_row_indexes()` directly rather than re-deriving the same "which checks flagged which row" logic a second time - same precedent this codebase already sets elsewhere (qbo_service.py/pipeline_service.py both reusing checks_service's own `_parse_date` across module boundaries). """ import csv import io import re from services.excel_service import _flagged_row_indexes _NEWLINE_RE = re.compile(r"[\r\n]+") def build_csv(result): """ Builds the generic transactions CSV as a plain string - never touches disk, same discipline as excel_service.build_workbook/ qbo_service.build_qbo/xero_csv_service.build_csv. Data flow: result (dict) - a pipeline_service.run()/recheck() result: transactions, checks, etc. returns (str) - the complete CSV text, UTF-8, comma-delimited Unlike xero_csv_service.build_csv (which skips a transaction with no usable date/amount, since an accounting-import file needs every row to be structurally valid), this writes EVERY transaction exactly as excel_service.py's own Transactions sheet would - a blank/null field is written as an empty cell, not a reason to drop the whole row, the same "never silently exclude data" discipline every export in this app already follows, just expressed differently here since a plain CSV dump has no structural requirement a blank field would violate. """ flagged = _flagged_row_indexes(result) buffer = io.StringIO() writer = csv.writer(buffer) writer.writerow(["Date", "Description", "Amount", "Balance", "Status", "Check flags"]) for i, t in enumerate(result.get("transactions") or []): amount = t.get("amount") balance = t.get("balance") # A newline embedded in the description is collapsed to a space # before writing - same conservative choice xero_csv_service.py # already makes (and for the same reason): a multi-line field is # technically valid, RFC-4180-quoted CSV, but not every tool # that opens this file is guaranteed to parse an embedded raw # newline inside a quoted field the same way. description = _NEWLINE_RE.sub(" ", t.get("description") or "").strip() writer.writerow([ t.get("date") or "", description, f"{amount:.2f}" if amount is not None else "", f"{balance:.2f}" if balance is not None else "", t.get("status") or "", ", ".join(flagged.get(i, [])), ]) return buffer.getvalue()