Skip to content

Implemented an invoice to export prepay groups invoices as PDFs#143

Open
QuanMPhm wants to merge 1 commit into
CCI-MOC:mainfrom
QuanMPhm:137/prepay_pdf
Open

Implemented an invoice to export prepay groups invoices as PDFs#143
QuanMPhm wants to merge 1 commit into
CCI-MOC:mainfrom
QuanMPhm:137/prepay_pdf

Conversation

@QuanMPhm

@QuanMPhm QuanMPhm commented Jan 17, 2025

Copy link
Copy Markdown
Contributor

Closes #137. This PR consists of the last commit. I feel like there is still a bit of polishing left to do, so I will submit this as a draft for now. I'll revisit this when #137 has been fully reviewed and merged.

The PR is quite similar to #137 in terms of logic, using the chromium library to generate the PDFs.

Comment on lines +146 to +175
def _create_html_invoice(temp_fd):
environment = Environment(loader=FileSystemLoader(TEMPLATE_DIR_PATH))
template = environment.get_template("pi_invoice.html")
content = template.render(
data=group_dataframe,
)
temp_fd.write(content)
temp_fd.flush()

def _create_pdf_invoice(temp_fd_name):
chrome_binary_location = os.environ.get(
"CHROME_BIN_PATH", "usr/bin/chromium"
)
if not os.path.exists(chrome_binary_location):
sys.exit(
f"Chrome binary does not exist at {chrome_binary_location}. Make sure the env var CHROME_BIN_PATH is set correctly or that Google Chrome is installed"
)

invoice_pdf_path = f"{self.name}/{group_instituition}_{group_contact_email}_{self.invoice_month}.pdf"
subprocess.run(
[
chrome_binary_location,
"--headless",
"--no-sandbox",
f"--print-to-pdf={invoice_pdf_path}",
"--no-pdf-header-footer",
"file://" + temp_fd_name,
],
capture_output=True,
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a duplicate of the code in process_report/invoices/pi_specific_invoice.py. Can we factor this out into a common base class (or mixin)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I believe #137 will undergo a lot of changes during review I will factor this into a base class after #137 is merged.

@QuanMPhm QuanMPhm force-pushed the 137/prepay_pdf branch 2 times, most recently from 13fa050 to cf00ea7 Compare June 2, 2025 20:48
@QuanMPhm QuanMPhm marked this pull request as ready for review June 2, 2025 20:48
@QuanMPhm QuanMPhm requested a review from naved001 June 2, 2025 20:48
A new invoice, `MOCAGroupInvoice`, has been added. Given the code
overlap between it and `PIInvoice`. Some refactoring has been made.
The list of different changes in this PR is:
- The Prepay Group and PI invoices now subclasses from a new
invoice class, `PDFInvoice`, which contains logic specific to exporting PDFs
- Unused logger removed from `PIInvoice`
@QuanMPhm

QuanMPhm commented Jun 2, 2025

Copy link
Copy Markdown
Contributor Author

@larsks @knikolla @naved001 This PR is now ready for review

@QuanMPhm QuanMPhm requested a review from larsks June 19, 2025 18:48

@knikolla knikolla left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is a good candidate for us to go over in a Zoom call line by line.

@joachimweyl ^^

help="Name of output folder containing pi-specific invoice csvs",
)
parser.add_argument(
"--prepay-groups-output-folder",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not make this configurable but provide default locations for invoices moving forward. Name should be hardcoded in the invoice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate PDFs for Prepaid Groups

3 participants