1
1
from dataclasses import dataclass
2
+ from datetime import datetime
2
3
import logging
3
4
import sys
4
5
@@ -23,6 +24,7 @@ class BillableInvoice(discount_invoice.DiscountInvoice):
23
24
nonbillable_pis : list [str ]
24
25
nonbillable_projects : list [str ]
25
26
old_pi_filepath : str
27
+ limit_new_pi_credit_to_partners : bool = False
26
28
27
29
@staticmethod
28
30
def _load_old_pis (old_pi_filepath ) -> pandas .DataFrame :
@@ -115,6 +117,31 @@ def export_s3(self, s3_bucket):
115
117
super ().export_s3 (s3_bucket )
116
118
s3_bucket .upload_file (self .old_pi_filepath , self .PI_S3_FILEPATH )
117
119
120
+ def _filter_partners (self , data ):
121
+ active_partnerships = list ()
122
+ institute_list = util .load_institute_list ()
123
+ for institute_info in institute_list :
124
+ if partnership_start_date := institute_info .get (
125
+ "mghpcc_partnership_start_date"
126
+ ):
127
+ partnership_year_month = "{:%Y-%m}" .format (
128
+ datetime .strptime (partnership_start_date , "%Y-%m-%d" )
129
+ )
130
+ if util .get_month_diff (self .invoice_month , partnership_year_month ) >= 0 :
131
+ active_partnerships .append (institute_info ["display_name" ])
132
+
133
+ return data [data [invoice .INSTITUTION_FIELD ].isin (active_partnerships )]
134
+
135
+ def _filter_excluded_su_types (self , data ):
136
+ return data [~ (data [invoice .SU_TYPE_FIELD ].isin (self .EXCLUDE_SU_TYPES ))]
137
+
138
+ def _get_credit_eligible_projects (self , data : pandas .DataFrame ):
139
+ filtered_data = self ._filter_excluded_su_types (data )
140
+ if self .limit_new_pi_credit_to_partners :
141
+ filtered_data = self ._filter_partners (filtered_data )
142
+
143
+ return filtered_data
144
+
118
145
def _apply_credits_new_pi (
119
146
self , data : pandas .DataFrame , old_pi_df : pandas .DataFrame
120
147
):
@@ -140,19 +167,19 @@ def get_initial_credit_amount(
140
167
)
141
168
print (f"New PI Credit set at { new_pi_credit_amount } for { self .invoice_month } " )
142
169
143
- current_pi_set = set (data [invoice .PI_FIELD ])
170
+ credit_eligible_projects = self ._get_credit_eligible_projects (data )
171
+ current_pi_set = set (credit_eligible_projects [invoice .PI_FIELD ])
144
172
for pi in current_pi_set :
145
- credit_eligible_projects = data [
146
- (data [invoice .PI_FIELD ] == pi )
147
- & ~ (data [invoice .SU_TYPE_FIELD ].isin (self .EXCLUDE_SU_TYPES ))
173
+ pi_projects = credit_eligible_projects [
174
+ credit_eligible_projects [invoice .PI_FIELD ] == pi
148
175
]
149
176
pi_age = self ._get_pi_age (old_pi_df , pi , self .invoice_month )
150
177
pi_old_pi_entry = old_pi_df .loc [
151
178
old_pi_df [invoice .PI_PI_FIELD ] == pi
152
179
].squeeze ()
153
180
154
181
if pi_age > 1 :
155
- for i , row in credit_eligible_projects .iterrows ():
182
+ for i , row in pi_projects .iterrows ():
156
183
data .at [i , invoice .BALANCE_FIELD ] = row [invoice .COST_FIELD ]
157
184
else :
158
185
if pi_age == 0 :
@@ -180,7 +207,7 @@ def get_initial_credit_amount(
180
207
181
208
credits_used = self .apply_flat_discount (
182
209
data ,
183
- credit_eligible_projects ,
210
+ pi_projects ,
184
211
remaining_credit ,
185
212
invoice .CREDIT_FIELD ,
186
213
invoice .BALANCE_FIELD ,
0 commit comments