Source code for util.financial_integration.outbound_summary
from copy import copy
[docs]
class UnsupportedOutboundSummaryStateError(ValueError):
"""Raised when a state does not have an outbound summary builder."""
def _build_tx(sheet, rows):
detail_styles = {
"BEAD (Grant)": [copy(cell._style) for cell in sheet[2]],
"TMAP (State Match)": [copy(cell._style) for cell in sheet[3]],
}
total_styles = [copy(cell._style) for cell in sheet[sheet.max_row]]
sheet.delete_rows(2, sheet.max_row - 1)
sorted_rows = sorted(
rows,
key=lambda row: (
str(row["disbursement_id"]),
str(row["project_id"]),
str(row["invoice_id"]),
),
)
fields = (
"disbursement_id",
"project_id",
"subgrantee",
"vendor_payee_tin",
"funding_source",
"amount",
"pca",
"invoice_id",
)
identifier_columns = {1, 2, 4, 7, 8}
for row_number, row in enumerate(sorted_rows, start=2):
styles = detail_styles[row["funding_source"]]
for column, field in enumerate(fields, start=1):
cell = sheet.cell(row_number, column)
cell._style = copy(styles[column - 1])
cell.value = (
str(row[field]) if column in identifier_columns else row[field]
)
total_row = len(sorted_rows) + 2
for column, style in enumerate(total_styles, start=1):
sheet.cell(total_row, column)._style = copy(style)
sheet.cell(total_row, 5).value = f"TOTAL ({len(sorted_rows)} lines)"
sheet.cell(total_row, 6).value = (
f"=SUM(F2:F{total_row - 1})" if sorted_rows else "=SUM()"
)
sheet.auto_filter.ref = f"A1:H{total_row}"
[docs]
def build_outbound_summary(state, rows, template_path, output_path):
"""Build a state-specific cumulative outbound-payment workbook."""
from openpyxl import load_workbook
builders = {"TX": _build_tx}
try:
builder = builders[state]
except KeyError as error:
raise UnsupportedOutboundSummaryStateError(
f"Unsupported outbound summary state: {state}"
) from error
workbook = load_workbook(template_path)
builder(workbook.active, rows)
workbook.calculation.calcMode = "auto"
workbook.calculation.fullCalcOnLoad = True
workbook.calculation.forceFullCalc = True
workbook.save(output_path)