-
Notifications
You must be signed in to change notification settings - Fork 6
Implemented an invoice to export prepay groups invoices as PDFs #143
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
Open
QuanMPhm
wants to merge
1
commit into
CCI-MOC:main
Choose a base branch
from
QuanMPhm:137/prepay_pdf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+461
−53
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import os | ||
| from dataclasses import dataclass | ||
| import tempfile | ||
|
|
||
| import pandas | ||
|
|
||
| from process_report.invoices import invoice, pdf_invoice | ||
|
|
||
|
|
||
| @dataclass | ||
| class MOCAGroupInvoice(pdf_invoice.PDFInvoice): | ||
| CREDIT_COLUMN_COPY_LIST = [ | ||
| invoice.INVOICE_DATE_FIELD, | ||
| invoice.INVOICE_EMAIL_FIELD, | ||
| invoice.GROUP_NAME_FIELD, | ||
| invoice.GROUP_INSTITUTION_FIELD, | ||
| ] | ||
| TOTAL_COLUMN_LIST = [ | ||
| invoice.COST_FIELD, | ||
| invoice.GROUP_BALANCE_USED_FIELD, | ||
| invoice.CREDIT_FIELD, | ||
| invoice.BALANCE_FIELD, | ||
| ] | ||
|
|
||
| DOLLAR_COLUMN_LIST = [ | ||
| invoice.RATE_FIELD, | ||
| invoice.GROUP_BALANCE_FIELD, | ||
| invoice.COST_FIELD, | ||
| invoice.GROUP_BALANCE_USED_FIELD, | ||
| invoice.CREDIT_FIELD, | ||
| invoice.BALANCE_FIELD, | ||
| ] | ||
|
|
||
| export_columns_list = [ | ||
| invoice.INVOICE_DATE_FIELD, | ||
| invoice.PROJECT_FIELD, | ||
| invoice.PROJECT_ID_FIELD, | ||
| invoice.PI_FIELD, | ||
| invoice.INVOICE_EMAIL_FIELD, | ||
| invoice.INVOICE_ADDRESS_FIELD, | ||
| invoice.INSTITUTION_FIELD, | ||
| invoice.INSTITUTION_ID_FIELD, | ||
| invoice.SU_HOURS_FIELD, | ||
| invoice.SU_TYPE_FIELD, | ||
| invoice.RATE_FIELD, | ||
| invoice.GROUP_NAME_FIELD, | ||
| invoice.GROUP_INSTITUTION_FIELD, | ||
| invoice.GROUP_BALANCE_FIELD, | ||
| invoice.COST_FIELD, | ||
| invoice.GROUP_BALANCE_USED_FIELD, | ||
| invoice.CREDIT_FIELD, | ||
| invoice.CREDIT_CODE_FIELD, | ||
| invoice.BALANCE_FIELD, | ||
| ] | ||
|
|
||
| prepay_credits: pandas.DataFrame | ||
|
|
||
| def _prepare(self): | ||
| self.export_data = self.data[ | ||
| self.data[invoice.IS_BILLABLE_FIELD] & ~self.data[invoice.MISSING_PI_FIELD] | ||
| ] | ||
| self.export_data = self.export_data[ | ||
| ~self.export_data[invoice.GROUP_NAME_FIELD].isna() | ||
| ] | ||
| self.group_list = self.export_data[invoice.GROUP_NAME_FIELD].unique() | ||
|
|
||
| def _get_group_dataframe(self, data, group): | ||
| group_projects = ( | ||
| data[data[invoice.GROUP_NAME_FIELD] == group].copy().reset_index(drop=True) | ||
| ) | ||
|
|
||
| # Add row for each prepay credit for the group in the invoice month | ||
| group_credit_mask = ( | ||
| self.prepay_credits[invoice.PREPAY_MONTH_FIELD] == self.invoice_month | ||
| ) & (self.prepay_credits[invoice.PREPAY_GROUP_NAME_FIELD] == group) | ||
| group_credit_info = self.prepay_credits[group_credit_mask] | ||
| for _, credit_info in group_credit_info.iterrows(): | ||
| group_credit = credit_info[invoice.PREPAY_CREDIT_FIELD] | ||
| group_projects.loc[len(group_projects)] = None | ||
|
|
||
| # In this "credit row", certain values should be | ||
| # the same for every columns (i.e Invoice Month, Group Name, etc.) | ||
| for column_name in self.CREDIT_COLUMN_COPY_LIST: | ||
| if column_name in group_projects.columns: | ||
| group_projects.loc[group_projects.index[-1], column_name] = ( | ||
| group_projects.loc[0, column_name] | ||
| ) | ||
|
|
||
| # Group is billed for the credit amount | ||
| group_projects.loc[ | ||
| group_projects.index[-1], [invoice.COST_FIELD, invoice.BALANCE_FIELD] | ||
| ] = [group_credit] * 2 | ||
|
|
||
| # Add sum row | ||
| column_sums = [] | ||
| sum_columns_list = [] | ||
| for column_name in self.TOTAL_COLUMN_LIST: | ||
| if column_name in group_projects.columns: | ||
| column_sums.append(group_projects[column_name].sum()) | ||
| sum_columns_list.append(column_name) | ||
| group_projects.loc[len(group_projects)] = ( | ||
| None # Adds a new row to end of dataframe initialized with None | ||
| ) | ||
| group_projects.loc[group_projects.index[-1], invoice.INVOICE_DATE_FIELD] = ( | ||
| "Total" | ||
| ) | ||
| group_projects.loc[group_projects.index[-1], sum_columns_list] = column_sums | ||
|
|
||
| # Add dollar signs | ||
| for column_name in self.DOLLAR_COLUMN_LIST: | ||
| if column_name in group_projects.columns: | ||
| group_projects[column_name] = group_projects[column_name].apply( | ||
| lambda data: data if pandas.isna(data) else f"${data}" | ||
| ) | ||
|
|
||
| group_projects.fillna("", inplace=True) | ||
|
|
||
| return group_projects | ||
|
|
||
| def export(self): | ||
| self._filter_columns() | ||
|
|
||
| if not os.path.exists(self.name): | ||
| os.mkdir(self.name) | ||
|
|
||
| for group in self.group_list: | ||
| group_dataframe = self._get_group_dataframe(self.export_data, group) | ||
| group_instituition = group_dataframe[invoice.GROUP_INSTITUTION_FIELD].iat[0] | ||
| group_contact_email = group_dataframe[invoice.INVOICE_EMAIL_FIELD].iat[0] | ||
| group_invoice_path = f"{self.name}/{group_instituition}_{group_contact_email}_{self.invoice_month}.pdf" | ||
|
|
||
| with tempfile.NamedTemporaryFile(mode="w", suffix=".html") as temp_fd: | ||
| self._create_html_invoice(temp_fd, group_dataframe, "pi_invoice.html") | ||
| self._create_pdf_invoice(temp_fd.name, group_invoice_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import os | ||
| import sys | ||
| from dataclasses import dataclass | ||
| import subprocess | ||
|
|
||
| import pandas | ||
| from jinja2 import Environment, FileSystemLoader | ||
|
|
||
| import process_report.invoices.invoice as invoice | ||
| import process_report.util as util | ||
|
|
||
|
|
||
| TEMPLATE_DIR_PATH = "process_report/templates" | ||
|
|
||
|
|
||
| @dataclass | ||
| class PDFInvoice(invoice.Invoice): | ||
| @staticmethod | ||
| def _create_html_invoice(temp_fd, data: pandas.DataFrame, template_filename: str): | ||
| environment = Environment(loader=FileSystemLoader(TEMPLATE_DIR_PATH)) | ||
| template = environment.get_template(template_filename) | ||
| content = template.render( | ||
| data=data, | ||
| ) | ||
| temp_fd.write(content) | ||
| temp_fd.flush() | ||
|
|
||
| @staticmethod | ||
| def _create_pdf_invoice(html_filepath: str, output_pdf_path: str): | ||
| 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" | ||
| ) | ||
|
|
||
| subprocess.run( | ||
| [ | ||
| chrome_binary_location, | ||
| "--headless", | ||
| "--no-sandbox", | ||
| f"--print-to-pdf={output_pdf_path}", | ||
| "--no-pdf-header-footer", | ||
| "file://" + html_filepath, | ||
| ], | ||
| capture_output=True, | ||
| ) | ||
|
|
||
| def export_s3(self, s3_bucket): | ||
| def _export_s3_group_invoice(invoice): | ||
| invoice_path = os.path.join(self.name, invoice) | ||
| striped_invoice_path = os.path.splitext(invoice_path)[0] | ||
| output_s3_path = f"Invoices/{self.invoice_month}/{striped_invoice_path}.pdf" | ||
| output_s3_archive_path = f"Invoices/{self.invoice_month}/Archive/{striped_invoice_path} {util.get_iso8601_time()}.pdf" | ||
| s3_bucket.upload_file(invoice_path, output_s3_path) | ||
| s3_bucket.upload_file(invoice_path, output_s3_archive_path) | ||
|
|
||
| # self.name is name of folder storing PDF invoices | ||
| for invoice_filename in os.listdir(self.name): | ||
| _export_s3_group_invoice(invoice_filename) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.