-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMOCA_group_specific_invoice.py
More file actions
134 lines (114 loc) · 4.87 KB
/
Copy pathMOCA_group_specific_invoice.py
File metadata and controls
134 lines (114 loc) · 4.87 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
127
128
129
130
131
132
133
134
import os
from dataclasses import dataclass
import tempfile
import pandas
from process_report.invoices import invoice, pdf_invoice
@dataclass
class MOCAGroupInvoice(pdf_invoice.PDFInvoice):
CREDIT_COLUMN_COPY_LIST = [
invoice.INVOICE_DATE_FIELD,
invoice.INVOICE_EMAIL_FIELD,
invoice.GROUP_NAME_FIELD,
invoice.GROUP_INSTITUTION_FIELD,
]
TOTAL_COLUMN_LIST = [
invoice.COST_FIELD,
invoice.GROUP_BALANCE_USED_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,
]
prepay_credits: pandas.DataFrame
def _prepare(self):
self.export_data = self.data[
self.data[invoice.IS_BILLABLE_FIELD] & ~self.data[invoice.MISSING_PI_FIELD]
]
self.export_data = self.export_data[
~self.export_data[invoice.GROUP_NAME_FIELD].isna()
]
self.group_list = self.export_data[invoice.GROUP_NAME_FIELD].unique()
def _get_group_dataframe(self, data, group):
group_projects = (
data[data[invoice.GROUP_NAME_FIELD] == group].copy().reset_index(drop=True)
)
# Add row for each prepay credit for the group in the invoice month
group_credit_mask = (
self.prepay_credits[invoice.PREPAY_MONTH_FIELD] == self.invoice_month
) & (self.prepay_credits[invoice.PREPAY_GROUP_NAME_FIELD] == group)
group_credit_info = self.prepay_credits[group_credit_mask]
for _, credit_info in group_credit_info.iterrows():
group_credit = credit_info[invoice.PREPAY_CREDIT_FIELD]
group_projects.loc[len(group_projects)] = None
# In this "credit row", certain values should be
# the same for every columns (i.e Invoice Month, Group Name, etc.)
for column_name in self.CREDIT_COLUMN_COPY_LIST:
if column_name in group_projects.columns:
group_projects.loc[group_projects.index[-1], column_name] = (
group_projects.loc[0, column_name]
)
# Group is billed for the credit amount
group_projects.loc[
group_projects.index[-1], [invoice.COST_FIELD, invoice.BALANCE_FIELD]
] = [group_credit] * 2
# Add sum row
column_sums = []
sum_columns_list = []
for column_name in self.TOTAL_COLUMN_LIST:
if column_name in group_projects.columns:
column_sums.append(group_projects[column_name].sum())
sum_columns_list.append(column_name)
group_projects.loc[len(group_projects)] = (
None # Adds a new row to end of dataframe initialized with None
)
group_projects.loc[group_projects.index[-1], invoice.INVOICE_DATE_FIELD] = (
"Total"
)
group_projects.loc[group_projects.index[-1], sum_columns_list] = column_sums
# Add dollar signs
for column_name in self.DOLLAR_COLUMN_LIST:
if column_name in group_projects.columns:
group_projects[column_name] = group_projects[column_name].apply(
lambda data: data if pandas.isna(data) else f"${data}"
)
group_projects.fillna("", inplace=True)
return group_projects
def export(self):
self._filter_columns()
if not os.path.exists(self.name):
os.mkdir(self.name)
for group in self.group_list:
group_dataframe = self._get_group_dataframe(self.export_data, group)
group_instituition = group_dataframe[invoice.GROUP_INSTITUTION_FIELD].iat[0]
group_contact_email = group_dataframe[invoice.INVOICE_EMAIL_FIELD].iat[0]
group_invoice_path = f"{self.name}/{group_instituition}_{group_contact_email}_{self.invoice_month}.pdf"
with tempfile.NamedTemporaryFile(mode="w", suffix=".html") as temp_fd:
self._create_html_invoice(temp_fd, group_dataframe, "pi_invoice.html")
self._create_pdf_invoice(temp_fd.name, group_invoice_path)