From 96a89ac2311921af642ea3d728ecea56f6161557 Mon Sep 17 00:00:00 2001 From: Robert Romero Date: Tue, 9 Sep 2025 16:01:46 -0700 Subject: [PATCH] refactor invoice sections --- src/invoice.py | 67 +++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/src/invoice.py b/src/invoice.py index e40da07..8d855ef 100755 --- a/src/invoice.py +++ b/src/invoice.py @@ -83,24 +83,12 @@ def _profile_sections(profile): [line for line in to_info if line.strip()]) -def generate_invoice(buffer, invoice_data): - """Create the PDF invoice into ``buffer``.""" - doc = SimpleDocTemplate( - buffer, - pagesize=LETTER, - rightMargin=40, - leftMargin=40, - topMargin=40, - bottomMargin=40, - ) - styles = getSampleStyleSheet() - styles.add(ParagraphStyle(name="RightAlign", parent=styles["Normal"], alignment=TA_RIGHT)) - styles.add(ParagraphStyle(name="Heading", parent=styles["Heading1"], fontSize=16, spaceAfter=10)) - styles.add(ParagraphStyle(name="SubHeading", parent=styles["Heading2"], fontSize=12, spaceAfter=8)) - - elements = [] - elements.append(Paragraph("Recharge Services Invoice", styles["Heading"])) - elements.append(Spacer(1, 12)) +def _header_elements(invoice_data, styles): + """Return flowables for the invoice header section.""" + elements = [ + Paragraph("Recharge Services Invoice", styles["Heading"]), + Spacer(1, 12), + ] header_table_data = [ [ @@ -113,8 +101,7 @@ def generate_invoice(buffer, invoice_data): ], ] header_table = Table(header_table_data, colWidths=[250, 250]) - elements.append(header_table) - elements.append(Spacer(1, 20)) + elements.extend([header_table, Spacer(1, 20)]) elements.append(Paragraph("From (Billed By)", styles["SubHeading"])) for line in invoice_data["from_info"]: @@ -126,7 +113,12 @@ def generate_invoice(buffer, invoice_data): elements.append(Paragraph(line, styles["Normal"])) elements.append(Spacer(1, 20)) - elements.append(Paragraph("Invoice Summary", styles["SubHeading"])) + return elements + + +def _table_elements(invoice_data, styles): + """Return flowables for the invoice summary table.""" + elements = [Paragraph("Invoice Summary", styles["SubHeading"])] table_data = [["Description", "Billing Period", "Qty / Units", "Rate", "Amount"]] for item in invoice_data["items"]: table_data.append( @@ -155,18 +147,21 @@ def generate_invoice(buffer, invoice_data): ] ) ) - elements.append(table) - elements.append(Spacer(1, 20)) + elements.extend([table, Spacer(1, 20)]) + return elements - elements.append(Paragraph("Payment Instructions", styles["SubHeading"])) + +def _note_elements(invoice_data, styles): + """Return flowables for payment instructions and notes.""" + elements = [Paragraph("Payment Instructions", styles["SubHeading"])] for line in invoice_data["bank_info"]: elements.append(Paragraph(line, styles["Normal"])) elements.append(Spacer(1, 20)) elements.append(Paragraph("Notes", styles["SubHeading"])) elements.append(Paragraph(invoice_data["notes"], styles["Normal"])) - elements.append(Spacer(1, 20)) + elements.append( Paragraph( "Generated by Recharge Management System on " @@ -175,6 +170,28 @@ def generate_invoice(buffer, invoice_data): styles["Normal"], ) ) + return elements + + +def generate_invoice(buffer, invoice_data): + """Create the PDF invoice into ``buffer``.""" + doc = SimpleDocTemplate( + buffer, + pagesize=LETTER, + rightMargin=40, + leftMargin=40, + topMargin=40, + bottomMargin=40, + ) + styles = getSampleStyleSheet() + styles.add(ParagraphStyle(name="RightAlign", parent=styles["Normal"], alignment=TA_RIGHT)) + styles.add(ParagraphStyle(name="Heading", parent=styles["Heading1"], fontSize=16, spaceAfter=10)) + styles.add(ParagraphStyle(name="SubHeading", parent=styles["Heading2"], fontSize=12, spaceAfter=8)) + + elements = [] + elements.extend(_header_elements(invoice_data, styles)) + elements.extend(_table_elements(invoice_data, styles)) + elements.extend(_note_elements(invoice_data, styles)) doc.build(elements)