|
| 1 | +import logging |
| 2 | +from decimal import Decimal |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +from process_report.invoices import invoice |
| 6 | +from process_report.processors import discount_processor |
| 7 | + |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | +logging.basicConfig(level=logging.INFO) |
| 11 | + |
| 12 | +INVOICE_FIELD_LIST = [ |
| 13 | + invoice.INVOICE_DATE_FIELD, |
| 14 | + invoice.PROJECT_FIELD, |
| 15 | + invoice.PROJECT_ID_FIELD, |
| 16 | + invoice.PI_FIELD, |
| 17 | + invoice.INVOICE_EMAIL_FIELD, |
| 18 | + invoice.INVOICE_ADDRESS_FIELD, |
| 19 | + invoice.INSTITUTION_FIELD, |
| 20 | + invoice.INSTITUTION_ID_FIELD, |
| 21 | + invoice.GROUP_NAME_FIELD, |
| 22 | + invoice.GROUP_INSTITUTION_FIELD, |
| 23 | + invoice.GROUP_BALANCE_FIELD, |
| 24 | + invoice.GROUP_BALANCE_USED_FIELD, |
| 25 | + invoice.SU_HOURS_FIELD, |
| 26 | + invoice.SU_TYPE_FIELD, |
| 27 | + invoice.SU_CHARGE_FIELD, |
| 28 | + invoice.LENOVO_CHARGE_FIELD, |
| 29 | + invoice.RATE_FIELD, |
| 30 | + invoice.COST_FIELD, |
| 31 | + invoice.CREDIT_FIELD, |
| 32 | + invoice.CREDIT_CODE_FIELD, |
| 33 | + invoice.SUBSIDY_FIELD, |
| 34 | + invoice.BALANCE_FIELD, |
| 35 | + # Internally used fields |
| 36 | + invoice.IS_BILLABLE_FIELD, |
| 37 | + invoice.MISSING_PI_FIELD, |
| 38 | + invoice.PI_BALANCE_FIELD, |
| 39 | + invoice.PROJECT_NAME_FIELD, |
| 40 | + invoice.GROUP_MANAGED_FIELD, |
| 41 | + invoice.CLUSTER_NAME_FIELD, |
| 42 | + invoice.IS_COURSE_FIELD, |
| 43 | +] |
| 44 | + |
| 45 | +# Fields without defaults have None as default value |
| 46 | +FIELD_DEFAULT_MAPPING = { |
| 47 | + invoice.BALANCE_FIELD: invoice.COST_FIELD, |
| 48 | + invoice.PI_BALANCE_FIELD: invoice.COST_FIELD, |
| 49 | + invoice.SUBSIDY_FIELD: Decimal(0), |
| 50 | + invoice.IS_COURSE_FIELD: False, # All rows assumed to be non-course, unless marked otherwise by ColdfrontFetchProcessor |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +@dataclass |
| 55 | +class PISUCreditProcessor(discount_processor.DiscountProcessor): |
| 56 | + """ |
| 57 | + Initializes all columns that will be used in later Processors, with appropriate types and default values. |
| 58 | +
|
| 59 | + Fields without defaults specified in FIELD_DEFAULT_MAPPING will have None as default value |
| 60 | + Pre-existing columns in input dataframe will only be casted, not overwritten |
| 61 | + """ |
| 62 | + |
| 63 | + def _process(self): |
| 64 | + for field in INVOICE_FIELD_LIST: |
| 65 | + if field not in self.data.columns: |
| 66 | + default_value = FIELD_DEFAULT_MAPPING.get(field, None) |
| 67 | + |
| 68 | + # If default value is name of another column, copy values from that column and cast to correct type |
| 69 | + if default_value in INVOICE_FIELD_LIST: |
| 70 | + default_value = self.data[default_value] |
| 71 | + |
| 72 | + self.data[field] = default_value |
0 commit comments