From 9eb897e097ba036fa3abe30bdc786b36af7f0811 Mon Sep 17 00:00:00 2001 From: Quan Pham Date: Thu, 19 Dec 2024 13:33:33 -0500 Subject: [PATCH] NERC Prepay Credits file is now exported through `PrepaymentProcessor` An optional argument, `export_NERC_credits` is added for testing purposes, to prevent the credits files from being written while running test cases --- .../processors/prepayment_processor.py | 24 +++++++++++++++++++ .../processors/test_prepayment_processor.py | 23 ++++++++++++++++++ process_report/tests/util.py | 1 + 3 files changed, 48 insertions(+) diff --git a/process_report/processors/prepayment_processor.py b/process_report/processors/prepayment_processor.py index e8ee863..d25172a 100644 --- a/process_report/processors/prepayment_processor.py +++ b/process_report/processors/prepayment_processor.py @@ -27,6 +27,7 @@ def PREPAY_DEBITS_S3_BACKUP_FILEPATH(self): prepay_contacts: pandas.DataFrame prepay_debits_filepath: str upload_to_s3: bool + export_NERC_credits: bool = True # For testing purposes @staticmethod def _load_prepay_debits(prepay_debits_filepath): @@ -53,6 +54,9 @@ def _process(self): self._add_prepay_info() self._apply_prepayments() + if self.export_NERC_credits: + credits_snapshot = self._get_prepay_credits_snapshot() + self._export_prepay_credits_snapshot(credits_snapshot) self._export_prepay_debits() if self.upload_to_s3: self._export_s3_prepay_debits() @@ -200,6 +204,21 @@ def _apply_prepayments(self): self.prepay_debits.loc[ debit_entry_mask, invoice.PREPAY_DEBIT_FIELD ] = prepay_amount_used + + def _get_prepay_credits_snapshot(self): + managed_groups_list = list() + for group_name, group_dict in self.group_info_dict.items(): + if group_dict[invoice.PREPAY_MANAGED_FIELD]: + managed_groups_list.append(group_name) + + credits_mask = ( + self.prepay_credits[invoice.PREPAY_MONTH_FIELD] == self.invoice_month + ) & ( + self.prepay_credits[invoice.PREPAY_GROUP_NAME_FIELD].isin( + managed_groups_list + ) + ) + return self.prepay_credits[credits_mask] def _backup_s3_prepay_debits(self): invoice_bucket = util.get_invoice_bucket() @@ -207,6 +226,11 @@ def _backup_s3_prepay_debits(self): self.prepay_debits_filepath, self.PREPAY_DEBITS_S3_BACKUP_FILEPATH ) + def _export_prepay_credits_snapshot(self, credits_snapshot): + credits_snapshot.to_csv( + f"NERC_Prepaid_Group-Credits-{self.invoice_month}.csv", index=False + ) + def _export_prepay_debits(self): self.prepay_debits.to_csv(self.prepay_debits_filepath, index=False) diff --git a/process_report/tests/unit/processors/test_prepayment_processor.py b/process_report/tests/unit/processors/test_prepayment_processor.py index 0c5ca1a..0b64e6a 100644 --- a/process_report/tests/unit/processors/test_prepayment_processor.py +++ b/process_report/tests/unit/processors/test_prepayment_processor.py @@ -429,3 +429,26 @@ def test_two_group_one_project(self): answer_prepay_debits, invoice_month, ) + + def test_get_credit_snapshot(self): + invoice_month = "2024-10" + test_prepay_credits = self._get_test_prepay_credits( + ["2024-10", "2024-10", "2024-10", "2024-09", "2024-09"], + ["G1", "G2", "G3", "G1", "G2"], + [0] * 5, + ) + test_group_info_dict = { + "G1": {"MGHPCC Managed": True}, + "G2": {"MGHPCC Managed": False}, + "G3": {"MGHPCC Managed": True}, + } + answer_credits_snapshot = test_prepay_credits.iloc[[0, 2]] + + new_prepayment_proc = test_utils.new_prepayment_processor( + invoice_month=invoice_month + ) + new_prepayment_proc.prepay_credits = test_prepay_credits + new_prepayment_proc.group_info_dict = test_group_info_dict + output_snapshot = new_prepayment_proc._get_prepay_credits_snapshot() + + self.assertTrue(answer_credits_snapshot.equals(output_snapshot)) diff --git a/process_report/tests/util.py b/process_report/tests/util.py index 057b9e5..b14eb39 100644 --- a/process_report/tests/util.py +++ b/process_report/tests/util.py @@ -168,4 +168,5 @@ def new_prepayment_processor( prepay_contacts, prepay_debits_filepath, upload_to_s3, + False, )