-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpi_specific_invoice.py
More file actions
126 lines (103 loc) · 3.99 KB
/
Copy pathpi_specific_invoice.py
File metadata and controls
126 lines (103 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
from dataclasses import dataclass
import tempfile
import logging
import pandas
from process_report.invoices import invoice, pdf_invoice
TEMPLATE_DIR_PATH = "process_report/templates"
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
@dataclass
class PIInvoice(pdf_invoice.PDFInvoice):
"""
This invoice operates on data processed by these Processors:
- ValidateBillablePIsProcessor
- NewPICreditProcessor
"""
TOTAL_COLUMN_LIST = [
invoice.COST_FIELD,
invoice.CREDIT_FIELD,
invoice.BALANCE_FIELD,
]
DOLLAR_COLUMN_LIST = [
invoice.RATE_FIELD,
invoice.GROUP_BALANCE_FIELD,
invoice.COST_FIELD,
invoice.GROUP_BALANCE_USED_FIELD,
invoice.CREDIT_FIELD,
invoice.BALANCE_FIELD,
]
export_columns_list = [
invoice.INVOICE_DATE_FIELD,
invoice.PROJECT_FIELD,
invoice.PROJECT_ID_FIELD,
invoice.PI_FIELD,
invoice.INVOICE_EMAIL_FIELD,
invoice.INVOICE_ADDRESS_FIELD,
invoice.INSTITUTION_FIELD,
invoice.INSTITUTION_ID_FIELD,
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,
]
def _prepare(self):
self.export_data = self.data[
self.data[invoice.IS_BILLABLE_FIELD] & ~self.data[invoice.MISSING_PI_FIELD]
]
self.pi_list = self.export_data[invoice.PI_FIELD].unique()
def _get_pi_dataframe(self, data, pi):
pi_projects = data[data[invoice.PI_FIELD] == pi].copy().reset_index(drop=True)
# Remove prepay group data if it's empty
if pandas.isna(pi_projects[invoice.GROUP_NAME_FIELD]).all():
pi_projects = pi_projects.drop(
[
invoice.GROUP_NAME_FIELD,
invoice.GROUP_INSTITUTION_FIELD,
invoice.GROUP_BALANCE_FIELD,
invoice.GROUP_BALANCE_USED_FIELD,
],
axis=1,
)
# Add a row containing sums for certain columns
column_sums = []
sum_columns_list = []
for column_name in self.TOTAL_COLUMN_LIST:
if column_name in pi_projects.columns:
column_sums.append(pi_projects[column_name].sum())
sum_columns_list.append(column_name)
pi_projects.loc[len(pi_projects)] = (
None # Adds a new row to end of dataframe initialized with None
)
pi_projects.loc[pi_projects.index[-1], invoice.INVOICE_DATE_FIELD] = "Total"
pi_projects.loc[pi_projects.index[-1], sum_columns_list] = column_sums
# Add dollar sign to certain columns
for column_name in self.DOLLAR_COLUMN_LIST:
if column_name in pi_projects.columns:
pi_projects[column_name] = pi_projects[column_name].apply(
lambda data: data if pandas.isna(data) else f"${data}"
)
pi_projects.fillna("", inplace=True)
return pi_projects
def export(self):
self._filter_columns()
# self.name is name of folder storing invoices
os.makedirs(self.name, exist_ok=True)
for pi in self.pi_list:
if pandas.isna(pi):
continue
pi_dataframe = self._get_pi_dataframe(self.export_data, pi)
pi_instituition = pi_dataframe[invoice.INSTITUTION_FIELD].iat[0]
invoice_pdf_path = (
f"{self.name}/{pi_instituition}_{pi}_{self.invoice_month}.pdf"
)
with tempfile.NamedTemporaryFile(mode="w", suffix=".html") as temp_fd:
self._create_html_invoice(temp_fd, pi_dataframe, "pi_invoice.html")
self._create_pdf_invoice(temp_fd.name, invoice_pdf_path)