Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PI invoices are now exported as PDFs #129

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

QuanMPhm
Copy link
Contributor

@QuanMPhm QuanMPhm commented Dec 31, 2024

Closes #84. This PR consists of the last commit.
This PR mostly involved changes to the pi-specific invoice class. More details in the commit message. Two Python packages were crucial for this task, Jinja and Selenium

@knikolla @larsks I am aware that running the invoice script with this change will make pandas print a bunch of FutureWarnings for each PDF printed. Should I address them after the current state of the PR is approved?

the `DiscountInvoice` class is now removed. No invoice class will
now class `_prepare()` or `_process()`.

`BUSubsidyProcessor`, which handles processing for the BU subsidy,
sets `IS_DISCOUNT_BY_NERC` to `False` because the
subsidy is not provided by NERC. Because of this, `BU Balance` indicates
the money which BU (not the PI they are subsidizing) owes to the
MGHPCC.

The test cases for the BU Subsidy has been refactored to be
more robust and readable.
The `Project` field has been added to `invoice.py`
Prepayments have been implemented by a new processor, `PrepaymentProcessor`.
The full implementation for prepayments involved the following changes:
- Several new columns names have been added to `invoices/invoice.py`,
including column names for the prepay data files and for the exported
invoices
- Many invoices will now export with 4 new columns: `Prepaid Group Name`,
`Prepaid Group Institution`, `Prepaid Group Balance`, `Prepaid Group Used`
- 4 command line arguments have been added to `process_report.py`.
3 of them allows the user to pass in a local version of the prepay credits,
contacts, and projects file. The last one (`—prepay-debits`) allows passing
a local version of the prepay debits files, and defaults to fetching from s3 if not
provided
- A set of test cases have been added for `PrepaymentProcessor`

Since the implementation of this feature required a lot of logic decisions
(i.e What happens if a prepaid project is active, but has no
funds?), below is (hopefully) an exhaustive list of code logic decisions
that were made. These can also be inferred through the test cases.
- Prepay projects listed in `prepaid_projects.csv` are identified by their
project name, not project - allocation name
- Attempting to process past invoices (“backprocessing”) may result in
incorrect output due to the nature of the prepay debit ledger
- While backprocessing is not supported for past months, processing
the same invoice month twice will still return correct output. In this
case, the month’s debit entry may be will be overwritten
- Prepay balances can be used in the same month they are added.
- The time range in which prepay projects are considered “active” includes
their start and end date
- After processing of any given invoice month, debit entries for that
month will be added. I emphasize this for clarification. A debit entry
such as:

`2024-11,G1,1062.48`

Should be interpreted as:
In the period from 2024-11-01 to 2024-11-30, prepay group G1 spent $1062.48
As opposed to:
In the period from 2024-10-01 to 2024-10-31, …

- If prepay projects are “active” but their prepay group has $0 balance,
their prepay info (group name, contact email) is still included, but the
prepay balance will be displayed as $0 and the prepay used as an empty field
…r` to `util`

Since these functions are now used in more than one location (`add_insti_proc`
and `prepay_proc`), it makes sense for them to be relocated to `util`

The test case checking institution-processing has changed slightly
The PI-specific dataframes will first be converted to HTML
tables using Jinja templates, and then converted to PDFs using
the Selenium Chrome driver.

A html template folder has been added, and the test cases
for the PI-specific invoice will now both check whether the
dataframe is formatted correctly and if the PDFs are
correctly generated.
@QuanMPhm QuanMPhm marked this pull request as ready for review January 2, 2025 20:08
Copy link
Member

@larsks larsks left a comment

Choose a reason for hiding this comment

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

I think using selenium is overkill here. You can drive chrome's print-to-pdf functionality directly from the command line:

google-chrome --headless --print-to-pdf=output.pdf --no-pdf-header-footer document.html

This takes a single subprocess.run() call. You can control the page size via CSS in your source document. Note that in the US, "Letter" size (8.5x11 inches) is much more common than A4.

Comment on lines +189 to +191
invoice_html_path = _create_html_invoice()
_create_pdf_invoice()
os.remove(invoice_html_path)
Copy link
Member

@larsks larsks Jan 2, 2025

Choose a reason for hiding this comment

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

Instead of manually cleaning up the temporary file, you could make more effective use of tempfile.NamedTemporaryFile here by passing an open file into your _create_html_invoice method:

with tempfile.NamedTemporaryFile(mode='w', suffix='.html') as tempfd:
  _create_html_invoice(tempfd)
  _create_pdf_invoice(tempfd.name)
def _create_html_invoice(tempfd):
    environment = Environment(loader=FileSystemLoader(TEMPLATE_DIR_PATH))
    template = environment.get_template("pi_invoice.html")
    content = template.render(
        data=pi_dataframe,
    )

    tempfd.write(content)

    # You might need this to ensure the content is visible in the file.
    tempfd.flush()

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 from the PI-specific invoices
2 participants