Skip to content

Commit

Permalink
Implemented processing and exporting for BU Internal invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
QuanMPhm committed Apr 24, 2024
1 parent c54a956 commit d676c4a
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 1 deletion.
59 changes: 58 additions & 1 deletion process_report/process_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
COST_FIELD = "Cost"
CREDIT_FIELD = "Credit"
CREDIT_CODE_FIELD = "Credit Code"
SUBSIDY_FIELD = "Subsidy"
BALANCE_FIELD = "Balance"
###

Expand All @@ -35,7 +36,7 @@ def get_institution_from_pi(institute_map, pi_uname):


def load_institute_map() -> dict:
with open("institute_map.json", "r") as f:
with open("process_report/institute_map.json", "r") as f:
institute_map = json.load(f)

return institute_map
Expand Down Expand Up @@ -105,6 +106,12 @@ def main():
default="HU_only.csv",
help="Name of output csv for HU invoices",
)
parser.add_argument(
"--BU-invoice-file",
required=False,
default="BU_Internal.csv",
help="Name of output csv for BU invoices",
)
parser.add_argument(
"--HU-BU-invoice-file",
required=False,
Expand Down Expand Up @@ -144,6 +151,7 @@ def main():
export_billables(credited_projects, args.output_file)
export_pi_billables(billable_projects, args.output_folder)
export_HU_only(billable_projects, args.HU_invoice_file)
export_BU_only(billable_projects, args.BU_invoice_file)
export_HU_BU(billable_projects, args.HU_BU_invoice_file)
export_lenovo(billable_projects)

Expand Down Expand Up @@ -302,6 +310,55 @@ def export_HU_only(dataframe, output_file):
HU_projects.to_csv(output_file)


def export_BU_only(dataframe: pandas.DataFrame, output_file):
def get_project(row):
project_alloc = row[PROJECT_FIELD]
if project_alloc.rfind("-") == -1:
return project_alloc
else:
return project_alloc[: project_alloc.rfind("-")]

BU_projects = dataframe[dataframe[INSTITUTION_FIELD] == "Boston University"]
BU_projects["Project"] = BU_projects.apply(get_project, axis=1)
BU_projects[SUBSIDY_FIELD] = 0
BU_projects = BU_projects[
[
INVOICE_DATE_FIELD,
PI_FIELD,
"Project",
COST_FIELD,
CREDIT_FIELD,
SUBSIDY_FIELD,
BALANCE_FIELD,
]
]
BU_projects = _apply_BU_subsidy(BU_projects)
BU_projects.to_csv(output_file)


def _apply_BU_subsidy(dataframe):
subsidy_amount = 100
BU_pi_list = dataframe[PI_FIELD].unique()

for BU_pi in BU_pi_list:
pi_projects = dataframe[dataframe[PI_FIELD] == BU_pi]
remaining_subsidy = subsidy_amount
for i, row in pi_projects.iterrows():
project_remaining_cost = row[
BALANCE_FIELD
] # Applying BU Subsidy after applying credits, so Balance shows rem costs
applied_subsidy = min(project_remaining_cost, remaining_subsidy)

dataframe.at[i, SUBSIDY_FIELD] = applied_subsidy
dataframe.at[i, BALANCE_FIELD] = row[BALANCE_FIELD] - applied_subsidy
remaining_subsidy -= applied_subsidy

if remaining_subsidy == 0:
break

return dataframe


def export_HU_BU(dataframe, output_file):
HU_BU_projects = dataframe[
(dataframe[INSTITUTION_FIELD] == "Harvard University")
Expand Down
76 changes: 76 additions & 0 deletions process_report/tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,82 @@ def test_apply_credit_0002(self):
self.assertEqual(800, credited_projects.loc[5, "Balance"])


class TestBUSubsidy(TestCase):
def setUp(self):
data = {
"Invoice Month": [
"2024-03",
"2024-03",
"2024-03",
"2024-03",
"2024-03",
"2024-03",
],
"Manager (PI)": ["PI1", "PI2", "PI3", "PI3", "PI4", "PI4"],
"Institution": [
"Boston University",
"Boston University",
"Harvard University",
"Harvard University",
"Boston University",
"Boston University",
],
"Project - Allocation": [
"ProjectA-e6413",
"ProjectB-5t143t",
"ProjectC-t14334",
"ProjectD",
"ProjectE-test-r25135",
"ProjectF",
],
"Cost": [1050, 100, 10000, 1000, 1050, 100],
"Credit": [1000, 100, 0, 0, 1000, 0],
"Balance": [50, 0, 10000, 1000, 50, 100],
}
self.dataframe = pandas.DataFrame(data)
output_file = tempfile.NamedTemporaryFile(delete=False, mode="w", suffix=".csv")
self.output_file = output_file.name

def test_apply_BU_subsidy(self):
process_report.export_BU_only(self.dataframe, self.output_file)
output_df = pandas.read_csv(self.output_file)

self.assertTrue(
set(
[
process_report.INVOICE_DATE_FIELD,
"Project",
process_report.PI_FIELD,
process_report.COST_FIELD,
process_report.CREDIT_FIELD,
process_report.SUBSIDY_FIELD,
process_report.BALANCE_FIELD,
]
).issubset(output_df)
)

self.assertTrue(
set(["PI1", "PI2", "PI4"]).issubset(output_df["Manager (PI)"].unique())
)

self.assertTrue(
set(["ProjectA", "ProjectB", "ProjectE-test", "ProjectF"]).issubset(
output_df["Project"].unique()
)
)

self.assertEqual(4, len(output_df.index))
self.assertEqual(50, output_df.loc[0, "Subsidy"])
self.assertEqual(0, output_df.loc[1, "Subsidy"])
self.assertEqual(50, output_df.loc[2, "Subsidy"])
self.assertEqual(50, output_df.loc[3, "Subsidy"])

self.assertEqual(0, output_df.loc[0, "Balance"])
self.assertEqual(0, output_df.loc[1, "Balance"])
self.assertEqual(0, output_df.loc[2, "Balance"])
self.assertEqual(50, output_df.loc[3, "Balance"])


class TestValidateBillables(TestCase):
def setUp(self):
data = {
Expand Down

0 comments on commit d676c4a

Please sign in to comment.