Skip to content

Commit

Permalink
Implemented processor for prepayments
Browse files Browse the repository at this point in the history
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
  • Loading branch information
QuanMPhm committed Dec 3, 2024
1 parent c81622d commit 8a8d9c7
Show file tree
Hide file tree
Showing 10 changed files with 726 additions and 3 deletions.
4 changes: 4 additions & 0 deletions process_report/invoices/NERC_total_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class NERCTotalInvoice(invoice.Invoice):
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,
Expand Down
4 changes: 4 additions & 0 deletions process_report/invoices/billable_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class BillableInvoice(invoice.Invoice):
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,
Expand Down
4 changes: 4 additions & 0 deletions process_report/invoices/bu_internal_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class BUInternalInvoice(invoice.Invoice):
invoice.INVOICE_DATE_FIELD,
invoice.PI_FIELD,
"Project",
invoice.GROUP_NAME_FIELD,
invoice.GROUP_INSTITUTION_FIELD,
invoice.GROUP_BALANCE_FIELD,
invoice.COST_FIELD,
invoice.GROUP_BALANCE_USED_FIELD,
invoice.CREDIT_FIELD,
invoice.SUBSIDY_FIELD,
invoice.PI_BALANCE_FIELD,
Expand Down
17 changes: 17 additions & 0 deletions process_report/invoices/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
PI_2ND_USED = "2nd Month Used"
###

### Prepay files fields
PREPAY_MONTH_FIELD = "Month"
PREPAY_CREDIT_FIELD = "Credit"
PREPAY_DEBIT_FIELD = "Debit"
PREPAY_GROUP_NAME_FIELD = "Group Name"
PREPAY_GROUP_CONTACT_FIELD = "Group Contact Email"
PREPAY_MANAGED_FIELD = "MGHPCC Managed"
PREPAY_PROJECT_FIELD = "Project"
PREPAY_START_DATE_FIELD = "Start Date"
PREPAY_END_DATE_FIELD = "End Date"
###

### Invoice field names
INVOICE_DATE_FIELD = "Invoice Month"
PROJECT_FIELD = "Project - Allocation"
Expand All @@ -21,6 +33,10 @@
INVOICE_ADDRESS_FIELD = "Invoice Address"
INSTITUTION_FIELD = "Institution"
INSTITUTION_ID_FIELD = "Institution - Specific Code"
GROUP_NAME_FIELD = "Prepaid Group Name"
GROUP_INSTITUTION_FIELD = "Prepaid Group Institution"
GROUP_BALANCE_FIELD = "Prepaid Group Balance"
GROUP_BALANCE_USED_FIELD = "Prepaid Group Used"
SU_HOURS_FIELD = "SU Hours (GBhr or SUhr)"
SU_TYPE_FIELD = "SU Type"
SU_CHARGE_FIELD = "SU Charge"
Expand All @@ -38,6 +54,7 @@
MISSING_PI_FIELD = "Missing PI"
PI_BALANCE_FIELD = "PI Balance"
PROJECT_NAME_FIELD = "Project"
GROUP_MANAGED_FIELD = "MGHPCC Managed"
###


Expand Down
4 changes: 4 additions & 0 deletions process_report/invoices/pi_specific_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class PIInvoice(invoice.Invoice):
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,
Expand Down
65 changes: 62 additions & 3 deletions process_report/process_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
validate_billable_pi_processor,
new_pi_credit_processor,
bu_subsidy_processor,
prepayment_processor,
)

### PI file field names
Expand Down Expand Up @@ -53,9 +54,8 @@
###

PI_S3_FILEPATH = "PIs/PI.csv"


ALIAS_S3_FILEPATH = "PIs/alias.csv"
PREPAY_DEBITS_S3_FILEPATH = "Prepay/prepay_debits.csv"


def load_alias(alias_file):
Expand All @@ -73,6 +73,14 @@ def load_alias(alias_file):
return alias_dict


def load_prepay_csv(prepay_credits_path, prepay_projects_path, prepay_contacts_path):
return (
pandas.read_csv(prepay_credits_path),
pandas.read_csv(prepay_projects_path),
pandas.read_csv(prepay_contacts_path),
)


def get_iso8601_time():
return datetime.datetime.now().strftime("%Y%m%dT%H%M%SZ")

Expand Down Expand Up @@ -117,6 +125,24 @@ def main():
required=True,
help="File containing list of projects that are non-billable within a specified duration",
)
parser.add_argument(
"--prepay-credits",
required=False,
default="prepaid_credits.csv",
help="CSV listing all prepay group credits. Defaults to 'prepaid_credits.csv'",
)
parser.add_argument(
"--prepay-projects",
required=False,
default="prepaid_projects.csv",
help="CSV listing all prepay group projects. Defaults to 'prepaid_projects.csv'",
)
parser.add_argument(
"--prepay-contacts",
required=False,
default="prepaid_contacts.csv",
help="CSV listing all prepay group contact information. Defaults to 'prepaid_contacts.csv'",
)

parser.add_argument(
"--nonbillable-file",
Expand Down Expand Up @@ -164,6 +190,11 @@ def main():
required=False,
help="Name of alias file listing PIs with aliases (and their aliases). If not provided, defaults to fetching from S3",
)
parser.add_argument(
"--prepay-debits",
required=False,
help="Name of csv file listing all prepay group debits. If not provided, defaults to fetching from S3",
)
parser.add_argument(
"--BU-subsidy-amount",
required=True,
Expand All @@ -190,6 +221,15 @@ def main():
alias_file = fetch_s3_alias_file()
alias_dict = load_alias(alias_file)

if args.prepay_debits:
prepay_debits_filepath = args.prepay_debits
else:
prepay_debits_filepath = fetch_s3_prepay_debits()

prepay_credits, prepay_projects, prepay_info = load_prepay_csv(
args.prepay_credits, args.prepay_projects, args.prepay_contacts
)

merged_dataframe = merge_csv(csv_files)

pi = []
Expand Down Expand Up @@ -248,7 +288,19 @@ def main():
)
bu_subsidy_proc.process()

processed_data = bu_subsidy_proc.data
prepayment_proc = prepayment_processor.PrepaymentProcessor(
"",
invoice_month,
bu_subsidy_proc.data,
prepay_credits,
prepay_projects,
prepay_info,
prepay_debits_filepath,
args.upload_to_s3,
)
prepayment_proc.process()

processed_data = prepayment_proc.data

### Initialize invoices

Expand Down Expand Up @@ -378,6 +430,13 @@ def fetch_s3_old_pi_file():
return local_name


def fetch_s3_prepay_debits():
local_name = "prepay_debits.csv"
invoice_bucket = util.get_invoice_bucket()
invoice_bucket.download_file(PREPAY_DEBITS_S3_FILEPATH, local_name)
return local_name


def backup_to_s3_old_pi_file(old_pi_file):
invoice_bucket = util.get_invoice_bucket()
invoice_bucket.upload_file(old_pi_file, f"PIs/Archive/PI {get_iso8601_time()}.csv")
Expand Down
176 changes: 176 additions & 0 deletions process_report/processors/prepayment_processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import sys

from dataclasses import dataclass
import pandas

from process_report import util
from process_report.invoices import invoice
from process_report.processors import discount_processor


@dataclass
class PrepaymentProcessor(discount_processor.DiscountProcessor):
IS_DISCOUNT_BY_NERC = True

prepay_credits: pandas.DataFrame
prepay_projects: pandas.DataFrame
prepay_contacts: pandas.DataFrame
prepay_debits_filepath: str
upload_to_s3: bool

@staticmethod
def _load_prepay_debits(prepay_debits_filepath):
try:
prepay_debits = pandas.read_csv(prepay_debits_filepath)
except FileNotFoundError:
sys.exit("Applying prepayments failed. prepay debits file does not exist")

return prepay_debits

def _prepare(self):
self.data[invoice.GROUP_NAME_FIELD] = None
self.data[invoice.GROUP_INSTITUTION_FIELD] = None
self.data[invoice.GROUP_MANAGED_FIELD] = None
self.data[invoice.GROUP_BALANCE_FIELD] = None
self.data[invoice.GROUP_BALANCE_USED_FIELD] = None

self.prepay_debits = self._load_prepay_debits(self.prepay_debits_filepath)
self.group_info_dict = self._get_prepay_group_dict()

def _process(self):
self._add_prepay_info()
self._apply_prepayments()

self._export_prepay_debits()
if self.upload_to_s3:
self._export_s3_prepay_debits()

def _get_prepay_group_dict(self):
"""Loads prepay info into a dict for simpler indexing
during processing step"""
prepay_group_dict = dict()
for _, group_info in self.prepay_contacts.iterrows():
group_name = group_info[invoice.PREPAY_GROUP_NAME_FIELD]
prepay_group_dict[group_name] = dict()
prepay_group_dict[group_name][
invoice.PREPAY_GROUP_CONTACT_FIELD
] = group_info[invoice.PREPAY_GROUP_CONTACT_FIELD]
prepay_group_dict[group_name][invoice.PREPAY_MANAGED_FIELD] = group_info[
invoice.PREPAY_MANAGED_FIELD
]
prepay_group_dict[group_name][invoice.GROUP_BALANCE_FIELD] = 0
prepay_group_dict[group_name][invoice.PREPAY_PROJECT_FIELD] = []

for _, group_credit in self.prepay_credits.iterrows():
if (
util.get_month_diff(
self.invoice_month, group_credit[invoice.PREPAY_MONTH_FIELD]
)
>= 0
):
prepay_group_dict[group_credit[invoice.PREPAY_GROUP_NAME_FIELD]][
invoice.GROUP_BALANCE_FIELD
] += group_credit[invoice.PREPAY_CREDIT_FIELD]

for _, group_debit in self.prepay_debits.iterrows():
if (
util.get_month_diff(
self.invoice_month, group_debit[invoice.PREPAY_MONTH_FIELD]
)
> 0
):
prepay_group_dict[group_debit[invoice.PREPAY_GROUP_NAME_FIELD]][
invoice.GROUP_BALANCE_FIELD
] -= group_debit[invoice.PREPAY_DEBIT_FIELD]

for _, group_project in self.prepay_projects.iterrows():
if (
util.get_month_diff(
self.invoice_month, group_project[invoice.PREPAY_START_DATE_FIELD]
)
>= 0
and util.get_month_diff(
group_project[invoice.PREPAY_END_DATE_FIELD], self.invoice_month
)
>= 0
):
prepay_group_dict[group_project[invoice.PREPAY_GROUP_NAME_FIELD]][
invoice.PREPAY_PROJECT_FIELD
].append(group_project[invoice.PREPAY_PROJECT_FIELD])

return prepay_group_dict

def _add_prepay_info(self):
"""Populate prepaid group name, institute, and MGHPCC managed field"""
institute_list = util.load_institute_list()
institute_map = util.get_institute_mapping(institute_list)

for group_name, group_dict in self.group_info_dict.items():
group_institute = util.get_institution_from_pi(
institute_map, group_dict[invoice.PREPAY_GROUP_CONTACT_FIELD]
)

row_mask = self.data[invoice.PROJECT_NAME_FIELD].isin(
group_dict[invoice.PREPAY_PROJECT_FIELD]
)
col_mask = [
invoice.INVOICE_EMAIL_FIELD,
invoice.GROUP_NAME_FIELD,
invoice.GROUP_INSTITUTION_FIELD,
invoice.GROUP_MANAGED_FIELD,
]
self.data.loc[row_mask, col_mask] = [
group_dict[invoice.PREPAY_GROUP_CONTACT_FIELD],
group_name,
group_institute,
group_dict[invoice.PREPAY_MANAGED_FIELD],
]

def _apply_prepayments(self):
for group_name, group_dict in self.group_info_dict.items():
group_projects = self.data[
self.data[invoice.GROUP_NAME_FIELD] == group_name
]
prepay_amount_used = self.apply_flat_discount(
self.data,
group_projects,
invoice.PI_BALANCE_FIELD,
group_dict[invoice.GROUP_BALANCE_FIELD],
invoice.GROUP_BALANCE_USED_FIELD,
invoice.BALANCE_FIELD,
)

remaining_prepay_balance = (
group_dict[invoice.GROUP_BALANCE_FIELD] - prepay_amount_used
)
self.data.loc[
self.data[invoice.GROUP_NAME_FIELD] == group_name,
invoice.GROUP_BALANCE_FIELD,
] = remaining_prepay_balance

if prepay_amount_used > 0:
debit_entry_mask = (
self.prepay_debits[invoice.PREPAY_MONTH_FIELD] == self.invoice_month
) & (self.prepay_debits[invoice.PREPAY_GROUP_NAME_FIELD] == group_name)
if self.prepay_debits[debit_entry_mask].empty:
self.prepay_debits = pandas.concat(
[
self.prepay_debits,
pandas.DataFrame(
[[self.invoice_month, group_name, prepay_amount_used]],
columns=self.prepay_debits.columns,
),
],
ignore_index=True,
)
else:
self.prepay_debits.loc[
debit_entry_mask, invoice.PREPAY_DEBIT_FIELD
] = prepay_amount_used

def _export_prepay_debits(self):
self.prepay_debits.to_csv(self.prepay_debits_filepath, index=False)

def _export_s3_prepay_debits(self):
invoice_bucket = util.get_invoice_bucket()
invoice_bucket.upload_file(self.prepay_debits_filepath, "TODO.csv") # TODO
Loading

0 comments on commit 8a8d9c7

Please sign in to comment.