eam-report-helper 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- eam_report_helper-0.0.1/.gitignore +1 -0
- eam_report_helper-0.0.1/LICENSE +0 -0
- eam_report_helper-0.0.1/PKG-INFO +10 -0
- eam_report_helper-0.0.1/README.md +0 -0
- eam_report_helper-0.0.1/conftest.py +0 -0
- eam_report_helper-0.0.1/pyproject.toml +20 -0
- eam_report_helper-0.0.1/requirements.txt +0 -0
- eam_report_helper-0.0.1/src/eam_report_helper/__init__.py +0 -0
- eam_report_helper-0.0.1/src/eam_report_helper/helpers.py +16 -0
- eam_report_helper-0.0.1/src/eam_report_helper/report_emp_questions.py +77 -0
- eam_report_helper-0.0.1/src/eam_report_helper/report_helper.py +103 -0
- eam_report_helper-0.0.1/src/eam_report_helper/report_helper_forms.py +185 -0
- eam_report_helper-0.0.1/src/eam_report_helper/report_newly_added_vendors.py +34 -0
- eam_report_helper-0.0.1/src/eam_report_helper/report_operator_tickets.py +122 -0
- eam_report_helper-0.0.1/src/eam_report_helper/report_safety_tickets.py +164 -0
- eam_report_helper-0.0.1/src/eam_report_helper/report_vendor_deficiencies.py +92 -0
- eam_report_helper-0.0.1/src/eam_report_helper/report_vendor_documents.py +102 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
*.pyc
|
|
File without changes
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: eam_report_helper
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A package that returns formatted data for reports
|
|
5
|
+
Author-email: Dave Gunn <daveg@8amsolutions.com>
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Requires-Python: >=3.8
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
[project]
|
|
8
|
+
name = "eam_report_helper"
|
|
9
|
+
version = "0.0.1"
|
|
10
|
+
authors = [
|
|
11
|
+
{ name="Dave Gunn", email="daveg@8amsolutions.com" }
|
|
12
|
+
]
|
|
13
|
+
description = "A package that returns formatted data for reports"
|
|
14
|
+
readme = "README.md"
|
|
15
|
+
requires-python = ">=3.8"
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
]
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import csv
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def generate_csv(column_list: list, file_name: str, data: list):
|
|
5
|
+
"""create csv file and write data to it"""
|
|
6
|
+
csv_columns = column_list
|
|
7
|
+
csv_file = file_name
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
with open(csv_file, 'w', encoding='utf8') as csvfile:
|
|
11
|
+
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
|
|
12
|
+
writer.writeheader()
|
|
13
|
+
for item in data:
|
|
14
|
+
writer.writerow(item)
|
|
15
|
+
except IOError:
|
|
16
|
+
print('error')
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
class EmpQuestionsReport:
|
|
4
|
+
def __init__(self, db, company_id: str) -> None:
|
|
5
|
+
self.db = db
|
|
6
|
+
self.companyId = company_id
|
|
7
|
+
|
|
8
|
+
def format_questions(self, emp_questions: list) -> list:
|
|
9
|
+
"""Format questions"""
|
|
10
|
+
results = []
|
|
11
|
+
for item in emp_questions:
|
|
12
|
+
if self.companyId in [x['id'] for x in item['employerList']]:
|
|
13
|
+
results.append(item)
|
|
14
|
+
|
|
15
|
+
return results
|
|
16
|
+
|
|
17
|
+
def format_answers(self, vendor_list_items: list, vend_answers: list, emp_questions_formatted: list, emp_questions: list) -> list:
|
|
18
|
+
"""Format answers"""
|
|
19
|
+
vendor_ids = [x['vendorId'] for x in vendor_list_items]
|
|
20
|
+
emp_question_ids = [x['id'] for x in emp_questions_formatted]
|
|
21
|
+
|
|
22
|
+
results = []
|
|
23
|
+
for item in vend_answers:
|
|
24
|
+
if (item['vendorId'] in vendor_ids) and (item['empQuestionId'] in emp_question_ids):
|
|
25
|
+
item['inputType'] = [x['inputType']
|
|
26
|
+
for x in emp_questions if x['id'] == item['empQuestionId']][0]
|
|
27
|
+
item['questionText'] = [x['text']
|
|
28
|
+
for x in emp_questions if x['id'] == item['empQuestionId']][0]
|
|
29
|
+
results.append(item)
|
|
30
|
+
|
|
31
|
+
return results
|
|
32
|
+
|
|
33
|
+
def format_answers_by_question_type(self, vend_answers_formatted: list) -> list:
|
|
34
|
+
"""Format answers by question type"""
|
|
35
|
+
for item in vend_answers_formatted:
|
|
36
|
+
if item['inputType'] == 'mult_select':
|
|
37
|
+
item['answer'] = str([x['text'] for x in item['answer'] if x['isActive'] is True]).replace(
|
|
38
|
+
'[', '').replace(']', '').replace('\'', '')
|
|
39
|
+
|
|
40
|
+
return vend_answers_formatted
|
|
41
|
+
|
|
42
|
+
def build_emp_questions_report(self) -> dict:
|
|
43
|
+
"""Build Tundra employee questions report"""
|
|
44
|
+
vendor_list_items = self.db.get_results(
|
|
45
|
+
query=f"SELECT c.vendorId, c.vendorName, c.employerId, c.employerName FROM c WHERE c.type = 'vendorList' AND c.employerId = '{self.companyId}'")
|
|
46
|
+
|
|
47
|
+
emp_questions = self.db.get_results(
|
|
48
|
+
query=f"SELECT * FROM c WHERE c.type = 'empQuestion'")
|
|
49
|
+
|
|
50
|
+
emp_questions_formatted = self.format_questions(emp_questions)
|
|
51
|
+
|
|
52
|
+
vend_answers = self.db.get_results(
|
|
53
|
+
query=f"SELECT * FROM c WHERE c.type = 'vendAnswer'")
|
|
54
|
+
|
|
55
|
+
vend_answers_formatted = self.format_answers(
|
|
56
|
+
vendor_list_items, vend_answers, emp_questions_formatted, emp_questions)
|
|
57
|
+
|
|
58
|
+
vend_answers_by_type = self.format_answers_by_question_type(
|
|
59
|
+
vend_answers_formatted)
|
|
60
|
+
|
|
61
|
+
rows = []
|
|
62
|
+
for vendor_id in list(set([x['vendorId'] for x in vendor_list_items])):
|
|
63
|
+
row_object = {'Vendor Name': [
|
|
64
|
+
x['vendorName'] for x in vendor_list_items if x['vendorId'] == vendor_id][0]}
|
|
65
|
+
for inner_item in vend_answers_by_type:
|
|
66
|
+
if vendor_id == inner_item['vendorId']:
|
|
67
|
+
row_object[inner_item['questionText']
|
|
68
|
+
] = inner_item['answer']
|
|
69
|
+
|
|
70
|
+
if len(row_object) > 1:
|
|
71
|
+
rows.append(row_object)
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
'file_name': 'Questions_Report.csv',
|
|
75
|
+
'headers': ['Vendor Name'] + [x['text'] for x in emp_questions_formatted],
|
|
76
|
+
'rows': rows
|
|
77
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import base64
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from shared_functions.reports.report_helper_forms import FormReportHelper
|
|
6
|
+
|
|
7
|
+
from shared_functions.reports.helpers import generate_csv
|
|
8
|
+
|
|
9
|
+
from shared_functions.reports.report_safety_tickets import SafetyTicketsReport
|
|
10
|
+
from shared_functions.reports.report_vendor_documents import VendorDocumentsReport
|
|
11
|
+
from shared_functions.reports.report_emp_questions import EmpQuestionsReport
|
|
12
|
+
from shared_functions.reports.report_vendor_deficiencies import VendorDeficienciesReport
|
|
13
|
+
from shared_functions.reports.report_newly_added_vendors import NewlyAddedVendorReport
|
|
14
|
+
from shared_functions.reports.report_operator_tickets import OperatorTicketReport
|
|
15
|
+
|
|
16
|
+
from eam_db_helper import db
|
|
17
|
+
from eam_b2c_helper import b2c
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger("azure")
|
|
20
|
+
logger.setLevel(logging.WARN)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ReportHelper:
|
|
24
|
+
"""Report Helper"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, company_id: str) -> None:
|
|
27
|
+
self.company_id = company_id
|
|
28
|
+
self.db = db.DatabaseHelper({
|
|
29
|
+
'account_uri': os.environ['COSMOS_ACCOUNT_URI'],
|
|
30
|
+
'key': os.environ['COSMOS_KEY'],
|
|
31
|
+
'db_name': os.environ['COSMOS_DATABASE'],
|
|
32
|
+
'container_name': os.environ['COSMOS_CONTAINER']
|
|
33
|
+
})
|
|
34
|
+
self.b2c_helper = b2c.B2CHelper({
|
|
35
|
+
"grant_type": "client_credentials",
|
|
36
|
+
"user_mgmt_client_id": os.environ["B2C_USER_MANAGEMENT_APP_CLIENT_ID"],
|
|
37
|
+
"scope": "https://graph.microsoft.com/.default",
|
|
38
|
+
"user_mgmt_client_secret": os.environ[
|
|
39
|
+
"B2C_USER_MANAGEMENT_APP_CLIENT_SECRET"
|
|
40
|
+
],
|
|
41
|
+
"tenant_id": os.environ["B2C_TENANT_ID"],
|
|
42
|
+
"ext_app_client_id": os.environ["B2C_EXTENSION_APP_CLIENT_ID"],
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
def assemble_data(self, column_names: list, rows: list, file_name: str) -> str:
|
|
46
|
+
"""Assemble data into csv and return binary string."""
|
|
47
|
+
generate_csv(
|
|
48
|
+
column_names, file_name, rows)
|
|
49
|
+
data = open(file_name, "rb").read()
|
|
50
|
+
base64_string = base64.b64encode(data)
|
|
51
|
+
os.remove(file_name)
|
|
52
|
+
return base64_string
|
|
53
|
+
|
|
54
|
+
def build_report(self, report_id: str) -> list:
|
|
55
|
+
"""Build report based on report_id"""
|
|
56
|
+
|
|
57
|
+
match report_id:
|
|
58
|
+
|
|
59
|
+
case '16918672-f971-42ce-bb16-c0ea46555321':
|
|
60
|
+
results = SafetyTicketsReport(
|
|
61
|
+
self.db, self.company_id, self.b2c_helper).build_safety_ticket_report()
|
|
62
|
+
|
|
63
|
+
case 'd8cba0aa-b3d8-4efa-a50d-6b85acf5569c':
|
|
64
|
+
results = VendorDocumentsReport(
|
|
65
|
+
self.db, self.company_id).build_vendor_document_report()
|
|
66
|
+
|
|
67
|
+
case 'c3fc0792-37d9-40a3-827f-30d2260701d8':
|
|
68
|
+
results = EmpQuestionsReport(
|
|
69
|
+
self.db, self.company_id).build_emp_questions_report()
|
|
70
|
+
|
|
71
|
+
case '91d72e1c-c280-4946-a0bd-37979f70652d':
|
|
72
|
+
results = VendorDeficienciesReport(
|
|
73
|
+
self.db, self.company_id).build_vendor_deficiencies_report()
|
|
74
|
+
|
|
75
|
+
for row in results['rows']:
|
|
76
|
+
del row['vendor_id']
|
|
77
|
+
|
|
78
|
+
results['headers'] = [x for x in results['rows'][0].keys()]
|
|
79
|
+
|
|
80
|
+
case 'cecee74f-2f2f-46d5-8fb3-3e3e624f7587':
|
|
81
|
+
results = FormReportHelper(self.db, self.b2c_helper).get_form_report(
|
|
82
|
+
"bbb961a1-e9ab-4419-bb21-34f4515e3997", "Insignia_IncidentReport_Tracking.csv")
|
|
83
|
+
|
|
84
|
+
case 'dc9b4bb1-91d6-4b37-aa9d-ee53cc7209b5':
|
|
85
|
+
results = FormReportHelper(self.db, self.b2c_helper).get_form_report(
|
|
86
|
+
"19ae3e00-bfb9-4105-8f44-7e5bb740afc8", "Insignia_NearMiss_HazID_Tracking.csv")
|
|
87
|
+
|
|
88
|
+
case '57dcd8cd-49bb-4ebc-8c6c-5db3c360d308':
|
|
89
|
+
results = FormReportHelper(self.db, self.b2c_helper).get_form_report(
|
|
90
|
+
"b3e979a8-19e0-49bb-b5dd-504df0b056bc", "Insignia_RegulatoryCorrectiveAction_Tracking.csv")
|
|
91
|
+
|
|
92
|
+
case 'dae860b7-3ea7-49a2-9d25-c95fedd42c60':
|
|
93
|
+
results = NewlyAddedVendorReport(
|
|
94
|
+
self.db, self.company_id).build_saturn_report()
|
|
95
|
+
|
|
96
|
+
case '0197faf6-da1a-424b-ad79-807519926737':
|
|
97
|
+
vendor_deficiency_results = VendorDeficienciesReport(
|
|
98
|
+
self.db, self.company_id).build_vendor_deficiencies_report()
|
|
99
|
+
|
|
100
|
+
results = OperatorTicketReport(
|
|
101
|
+
self.db, self.company_id, vendor_deficiency_results).build_operator_tickets_report()
|
|
102
|
+
|
|
103
|
+
return self.assemble_data(results['headers'], results['rows'], results['file_name'])
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
class FormReportHelper:
|
|
2
|
+
"""Form Report Helper"""
|
|
3
|
+
|
|
4
|
+
def __init__(self, database, b2c_helper):
|
|
5
|
+
self.database = database
|
|
6
|
+
self.b2c_helper = b2c_helper
|
|
7
|
+
|
|
8
|
+
def get_item_by_template(self, v: dict, elements: list) -> str:
|
|
9
|
+
"""Get the item by label"""
|
|
10
|
+
|
|
11
|
+
element: dict = [
|
|
12
|
+
x for x in elements if x['id'] == v['elementId']][0]
|
|
13
|
+
try:
|
|
14
|
+
v['value'] = [x['label']
|
|
15
|
+
for x in element['options'] if x['value'] == v['value']][0]
|
|
16
|
+
except IndexError:
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
return v['value']
|
|
20
|
+
|
|
21
|
+
def get_item_by_label(self, v: dict, users: list, template: dict) -> str:
|
|
22
|
+
"""Get the item by label"""
|
|
23
|
+
try:
|
|
24
|
+
row_item = v['value']
|
|
25
|
+
|
|
26
|
+
if v['type'] == 'inpSelectUser':
|
|
27
|
+
row_item = [x['displayName']
|
|
28
|
+
for x in users if x['id'] == v['value']['id']][0]
|
|
29
|
+
if v['type'] == 'inpSelect':
|
|
30
|
+
row_item = self.get_item_by_template(
|
|
31
|
+
v, template['elements'])
|
|
32
|
+
|
|
33
|
+
except KeyError:
|
|
34
|
+
row_item = v['values']
|
|
35
|
+
return row_item
|
|
36
|
+
|
|
37
|
+
def match_elements(self, distribution: dict, template: dict, users: list, sub_forms: list = None) -> dict:
|
|
38
|
+
"""Match the elements"""
|
|
39
|
+
row = {
|
|
40
|
+
'Form Name': distribution['template']['name'],
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if 'sequenceNumber' in distribution:
|
|
44
|
+
row['Report Number'] = distribution['sequenceNumber']
|
|
45
|
+
|
|
46
|
+
for k, v in distribution['data'].items():
|
|
47
|
+
if ('value' in v) or ('values' in v):
|
|
48
|
+
label = [x['label'] for x in template['elements']
|
|
49
|
+
if x['id'] == v['elementId']][0]
|
|
50
|
+
|
|
51
|
+
row[label] = self.get_item_by_label(v, users, template)
|
|
52
|
+
|
|
53
|
+
# Combine the sub form data with the form data
|
|
54
|
+
if sub_forms is not None:
|
|
55
|
+
for sub_form in sub_forms:
|
|
56
|
+
if distribution['id'] == sub_form['distribution_id']:
|
|
57
|
+
row = {**row, **sub_form['matched_elements']}
|
|
58
|
+
|
|
59
|
+
return row
|
|
60
|
+
|
|
61
|
+
def get_sub_forms(self, distributions: list, users: list) -> list:
|
|
62
|
+
"""Get the sub forms"""
|
|
63
|
+
|
|
64
|
+
sub_form_ref_list = []
|
|
65
|
+
# Match form with subform
|
|
66
|
+
for distribution in distributions:
|
|
67
|
+
for k, v in distribution['data'].items():
|
|
68
|
+
for key, value in v.items():
|
|
69
|
+
if key == 'subForm':
|
|
70
|
+
for item in value:
|
|
71
|
+
sub_form_ref_list.append({
|
|
72
|
+
'distribution_id': distribution['id'],
|
|
73
|
+
'sub_form_distribution_id': item['id']
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
# Get the sub form distributions
|
|
77
|
+
sub_forms = self.database.get_results(
|
|
78
|
+
query=f"SELECT * FROM c where c.type = 'formDistribution' and ARRAY_CONTAINS({[x['sub_form_distribution_id'] for x in sub_form_ref_list]}, c.id)")
|
|
79
|
+
|
|
80
|
+
for sub_form in sub_forms:
|
|
81
|
+
|
|
82
|
+
for ref in sub_form_ref_list:
|
|
83
|
+
if ref['sub_form_distribution_id'] == sub_form['id']:
|
|
84
|
+
ref['sub_form_template_id'] = sub_form['template']['id']
|
|
85
|
+
ref['sub_form'] = sub_form
|
|
86
|
+
|
|
87
|
+
# Get the sub form templates
|
|
88
|
+
templates = self.database.get_results(
|
|
89
|
+
query=f"SELECT * FROM c where c.type = 'formTemplate' and ARRAY_CONTAINS({[x['sub_form_template_id'] for x in sub_form_ref_list]}, c.id)")
|
|
90
|
+
|
|
91
|
+
for sub_form in sub_form_ref_list:
|
|
92
|
+
for template in templates:
|
|
93
|
+
if sub_form['sub_form_template_id'] == template['id']:
|
|
94
|
+
sub_form['sub_form_template'] = template
|
|
95
|
+
|
|
96
|
+
# Match the sub form distribution items with the template
|
|
97
|
+
for sub_form in sub_form_ref_list:
|
|
98
|
+
matched_elements = self.match_elements(sub_form['sub_form'],
|
|
99
|
+
sub_form['sub_form_template'], users, None)
|
|
100
|
+
matched_elements['Status'] = sub_form['sub_form']['status']
|
|
101
|
+
sub_form['matched_elements'] = matched_elements
|
|
102
|
+
|
|
103
|
+
# Get the sub form elements in proper order, this will only work for Corrective Action?
|
|
104
|
+
for sub_form in sub_form_ref_list:
|
|
105
|
+
sub_form['matched_elements'].pop('Form Name')
|
|
106
|
+
sub_form['matched_elements'] = dict({
|
|
107
|
+
'Corrective Action Description': sub_form['matched_elements']['Corrective Action Description'],
|
|
108
|
+
'Corrective Action Status': sub_form['matched_elements']['Status'],
|
|
109
|
+
'Corrective Action Asignee': sub_form['matched_elements']['Corrective Action Responsibility'],
|
|
110
|
+
'Corrective Action Target Date': sub_form['matched_elements']['Corrective Action Due Date'],
|
|
111
|
+
'Corrective Action Completion Date': sub_form['matched_elements']['Corrective Action Completion Date']
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
return sub_form_ref_list
|
|
115
|
+
|
|
116
|
+
def remove_empty_columns(self, column_names: list, rows: list):
|
|
117
|
+
"""Remove empty columns"""
|
|
118
|
+
|
|
119
|
+
for column_name in column_names:
|
|
120
|
+
try:
|
|
121
|
+
if all(row[column_name] == '' for row in rows):
|
|
122
|
+
for row in rows:
|
|
123
|
+
del row[column_name]
|
|
124
|
+
if all(row[column_name] == [] for row in rows):
|
|
125
|
+
for row in rows:
|
|
126
|
+
del row[column_name]
|
|
127
|
+
except KeyError:
|
|
128
|
+
pass
|
|
129
|
+
|
|
130
|
+
all_keys = [key for row in rows for key in row.keys()]
|
|
131
|
+
|
|
132
|
+
return {'column_names': list(dict.fromkeys(all_keys)), 'rows': rows}
|
|
133
|
+
|
|
134
|
+
def format_cell_lists(self, rows: list) -> list:
|
|
135
|
+
"""for all rows, if the value is a list, join the list into a string"""
|
|
136
|
+
for row in rows:
|
|
137
|
+
for key, value in row.items():
|
|
138
|
+
if isinstance(value, list):
|
|
139
|
+
# if vlaues in value is also a list, join them with a new line
|
|
140
|
+
for i, v in enumerate(value):
|
|
141
|
+
if isinstance(v, list):
|
|
142
|
+
value[i] = ' - '.join(v)
|
|
143
|
+
|
|
144
|
+
row[key] = '\n'.join(value)
|
|
145
|
+
|
|
146
|
+
return rows
|
|
147
|
+
|
|
148
|
+
def get_form_report(self, template_id: str, report_name: str) -> dict:
|
|
149
|
+
"""Get the Insignia Hazard ID History"""
|
|
150
|
+
template = self.database.get_result(
|
|
151
|
+
query=f"SELECT * FROM c where c.type = 'formTemplate' and c.id = '{template_id}'")
|
|
152
|
+
|
|
153
|
+
distributions = self.database.get_results(
|
|
154
|
+
query=f"SELECT * FROM c where c.type = 'formDistribution' and c.template.id = '{template_id}' ORDER BY c.sequenceNumber DESC")
|
|
155
|
+
|
|
156
|
+
users = self.b2c_helper.get_users(
|
|
157
|
+
company_id=template['companyId']
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
sub_forms = self.get_sub_forms(distributions, users)
|
|
161
|
+
|
|
162
|
+
rows = []
|
|
163
|
+
for distribution in distributions:
|
|
164
|
+
rows.append(self.match_elements(
|
|
165
|
+
distribution, template, users, sub_forms))
|
|
166
|
+
|
|
167
|
+
ordinal_list = []
|
|
168
|
+
|
|
169
|
+
for item in template['elements']:
|
|
170
|
+
ordinal_list.append(item)
|
|
171
|
+
|
|
172
|
+
if len(sub_forms) > 0:
|
|
173
|
+
column_names = [x['label'] for x in ordinal_list] + \
|
|
174
|
+
[x for x in sub_forms[0]['matched_elements'].keys()]
|
|
175
|
+
else:
|
|
176
|
+
column_names = [x['label'] for x in ordinal_list]
|
|
177
|
+
|
|
178
|
+
formatted_rows: dict = self.format_cell_lists(rows)
|
|
179
|
+
formatted_rows: dict = self.remove_empty_columns(column_names, rows)
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
'file_name': report_name,
|
|
183
|
+
'headers': formatted_rows['column_names'],
|
|
184
|
+
'rows': formatted_rows['rows']
|
|
185
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class NewlyAddedVendorReport:
|
|
5
|
+
def __init__(self, db, company_id):
|
|
6
|
+
self.db = db
|
|
7
|
+
self.company_id = company_id
|
|
8
|
+
|
|
9
|
+
def build_saturn_report(self):
|
|
10
|
+
results = self.db.get_results(
|
|
11
|
+
query=f"SELECT * FROM c where c.type = 'vendorList' and c.employerId = '{self.company_id}' and c.vendorDates.added >= 1727762400 order by c.vendorDates.added desc")
|
|
12
|
+
|
|
13
|
+
rows = []
|
|
14
|
+
for item in results:
|
|
15
|
+
if item['vendorDates']['added'] != '':
|
|
16
|
+
|
|
17
|
+
item['vendorDates']['added'] = datetime.datetime.fromtimestamp(
|
|
18
|
+
item['vendorDates']['added']).strftime('%Y-%m-%d')
|
|
19
|
+
rows.append({
|
|
20
|
+
'Vendor Name': item['vendorName'],
|
|
21
|
+
'Date Vendor Added': item['vendorDates']['added'],
|
|
22
|
+
'Vendor City': item['vendorAddress']['city'],
|
|
23
|
+
'Vendor Admin Name': item['vendorAccountAdmin']['name'],
|
|
24
|
+
'Vendor Admin Email': item['vendorAccountAdmin']['email'],
|
|
25
|
+
'Vendor Admin Phone': item['vendorAccountAdmin']['phone'],
|
|
26
|
+
'Rating': item['vendorOverallRating'],
|
|
27
|
+
'Current Deficiencies': '\n'.join(item['vendorOverallItems'])
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
'file_name': 'Added_Vendors.xlsx',
|
|
32
|
+
'rows': rows,
|
|
33
|
+
'headers': [x for x in rows[0].keys()] if len(rows) > 0 else []
|
|
34
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
class OperatorTicketReport:
|
|
2
|
+
"""Operator Ticket Report"""
|
|
3
|
+
|
|
4
|
+
def __init__(self, db, company_id: str, vendor_deficiency_results: dict) -> None:
|
|
5
|
+
self.db = db
|
|
6
|
+
self.company_id = company_id
|
|
7
|
+
self.vendor_deficiency_results = vendor_deficiency_results
|
|
8
|
+
|
|
9
|
+
def filter_operators(self) -> list:
|
|
10
|
+
"""Filter Operators"""
|
|
11
|
+
operators = [x for x in self.vendor_deficiency_results['rows']
|
|
12
|
+
if x['Subscription Type'] == 0]
|
|
13
|
+
|
|
14
|
+
distinct_operators = []
|
|
15
|
+
for operator in operators:
|
|
16
|
+
distinct_operators.append({
|
|
17
|
+
'vendor_id': operator['vendor_id'],
|
|
18
|
+
'Vendor Name': operator['Vendor Name'],
|
|
19
|
+
'Subscription Type': operator['Subscription Type'],
|
|
20
|
+
'Vendor Tags': operator['Vendor Tags'],
|
|
21
|
+
'Vendor Rating': operator['Vendor Rating'],
|
|
22
|
+
'Admin Name': operator['Admin Name'],
|
|
23
|
+
'Admin Email': operator['Admin Email'],
|
|
24
|
+
'Admin Phone': operator['Admin Phone'],
|
|
25
|
+
'Vendor Deficiencies': '\n'.join([x['Current Deficiencies'] for x in self.vendor_deficiency_results['rows'] if x['vendor_id'] == operator['vendor_id']])
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
return [dict(t) for t in {tuple(d.items()) for d in distinct_operators}]
|
|
29
|
+
|
|
30
|
+
def prime(self, tickets: list) -> list:
|
|
31
|
+
"""Prime"""
|
|
32
|
+
doc_questions = self.db.get_results(
|
|
33
|
+
query=f"SELECT * FROM c WHERE c.type = 'docQuestion' AND ARRAY_CONTAINS({list(set([x['docId'] for x in tickets]))}, c.docId)")
|
|
34
|
+
|
|
35
|
+
ticket_headers = []
|
|
36
|
+
for ticket in tickets:
|
|
37
|
+
for question in doc_questions:
|
|
38
|
+
if ticket['docId'] == question['docId']:
|
|
39
|
+
|
|
40
|
+
item = {
|
|
41
|
+
"header": f"{ticket['docType']}: {question['text']}",
|
|
42
|
+
"key": question['key'],
|
|
43
|
+
# "ticket_id": ticket['id']
|
|
44
|
+
}
|
|
45
|
+
if item not in ticket_headers:
|
|
46
|
+
ticket_headers.append(item)
|
|
47
|
+
|
|
48
|
+
return ticket_headers
|
|
49
|
+
|
|
50
|
+
def get_ocr_output(self, header_list: list, ticket: dict) -> dict:
|
|
51
|
+
"""Get OCR Output"""
|
|
52
|
+
for item in ticket['ocrOutput']['values']:
|
|
53
|
+
for key in header_list:
|
|
54
|
+
if item['key'] == key['key']:
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
'key': [x['header'] for x in header_list if x['header'].split(':')[0] == ticket['docType']][0],
|
|
58
|
+
'val': item['updatedValue'] if item['updatedValue'] != '' else item['ocrValue'],
|
|
59
|
+
'vendor_id': ticket['vendorId']
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
def build_final(self, tickets: list, header_list: list):
|
|
63
|
+
"""Build final"""
|
|
64
|
+
results = []
|
|
65
|
+
for inner in tickets:
|
|
66
|
+
|
|
67
|
+
for k, v in inner.items():
|
|
68
|
+
if k == 'ocrOutput':
|
|
69
|
+
ocr_value = self.get_ocr_output(header_list, inner)
|
|
70
|
+
results.append(ocr_value)
|
|
71
|
+
|
|
72
|
+
return results
|
|
73
|
+
|
|
74
|
+
def build_operator_tickets_report(self) -> dict:
|
|
75
|
+
"""Build Operator Tickets Report"""
|
|
76
|
+
operators = self.filter_operators()
|
|
77
|
+
|
|
78
|
+
tickets = self.db.get_results(
|
|
79
|
+
query=f"SELECT * FROM c WHERE c.type = 'vendorDoc' AND c.docClass = 'additionalRequirement' AND c.status != 'Not Submitted' AND c.employerId = '{self.company_id}' AND ARRAY_CONTAINS({[x['vendor_id'] for x in operators]}, c.vendorId)")
|
|
80
|
+
|
|
81
|
+
ticket_headers = self.prime(tickets)
|
|
82
|
+
ticket_results = self.build_final(tickets, ticket_headers)
|
|
83
|
+
|
|
84
|
+
rows = []
|
|
85
|
+
|
|
86
|
+
for operator in operators:
|
|
87
|
+
rows.append({
|
|
88
|
+
'vendor_id': operator['vendor_id'],
|
|
89
|
+
'Vendor Name': operator['Vendor Name'],
|
|
90
|
+
'Subscription Type': operator['Subscription Type'],
|
|
91
|
+
'Vendor Tags': operator['Vendor Tags'],
|
|
92
|
+
'Vendor Rating': operator['Vendor Rating'],
|
|
93
|
+
'Admin Name': operator['Admin Name'],
|
|
94
|
+
'Admin Email': operator['Admin Email'],
|
|
95
|
+
'Admin Phone': operator['Admin Phone'],
|
|
96
|
+
'Vendor Deficiencies': operator['Vendor Deficiencies']
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
final_headers = list(rows[0].keys()) + [x['header']
|
|
100
|
+
for x in ticket_headers]
|
|
101
|
+
final_rows = []
|
|
102
|
+
for item in rows:
|
|
103
|
+
final_rows.append(
|
|
104
|
+
{**item, **{x['header']: '' for x in ticket_headers}})
|
|
105
|
+
|
|
106
|
+
for item in final_rows:
|
|
107
|
+
for ticket in ticket_results:
|
|
108
|
+
if item['vendor_id'] == ticket['vendor_id']:
|
|
109
|
+
item[ticket['key']] = ticket['val']
|
|
110
|
+
|
|
111
|
+
more_final_rows = []
|
|
112
|
+
for item in final_rows:
|
|
113
|
+
item.pop('vendor_id')
|
|
114
|
+
more_final_rows.append(item)
|
|
115
|
+
|
|
116
|
+
final_headers.remove('vendor_id')
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
'file_name': 'Operator_Ticket_Report.csv',
|
|
120
|
+
'headers': final_headers,
|
|
121
|
+
'rows': more_final_rows
|
|
122
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SafetyTicketsReport:
|
|
6
|
+
"""Safety Tickets Report"""
|
|
7
|
+
|
|
8
|
+
def __init__(self, db, company_id: str, b2c_helper) -> None:
|
|
9
|
+
self.db = db
|
|
10
|
+
self.company_id = company_id
|
|
11
|
+
self.b2c_helper = b2c_helper
|
|
12
|
+
|
|
13
|
+
def get_report_headers(self) -> list:
|
|
14
|
+
"""Get report headers"""
|
|
15
|
+
|
|
16
|
+
docs = self.db.get_results(
|
|
17
|
+
query="SELECT * FROM c WHERE c.type = 'doc' AND c.docClass = 'additionalRequirement' AND c.isUserTicket = true")
|
|
18
|
+
|
|
19
|
+
results = []
|
|
20
|
+
for doc in docs:
|
|
21
|
+
if 'employerId' in doc:
|
|
22
|
+
if doc['employerId'] == self.company_id:
|
|
23
|
+
results.append(
|
|
24
|
+
{'docType': doc['docType'], 'docRef': doc['docRef']})
|
|
25
|
+
else:
|
|
26
|
+
pass
|
|
27
|
+
else:
|
|
28
|
+
results.append(
|
|
29
|
+
{'docType': doc['docType'], 'docRef': doc['docRef']})
|
|
30
|
+
|
|
31
|
+
return results
|
|
32
|
+
|
|
33
|
+
def remove_duplicates(self, active_docs: list):
|
|
34
|
+
"""Remove duplicates"""
|
|
35
|
+
test = []
|
|
36
|
+
for item in active_docs:
|
|
37
|
+
for key in item.keys():
|
|
38
|
+
test.append(key)
|
|
39
|
+
|
|
40
|
+
distinct_list = list(set(test))
|
|
41
|
+
|
|
42
|
+
test = []
|
|
43
|
+
for item in active_docs:
|
|
44
|
+
for inner in distinct_list:
|
|
45
|
+
for key, val in item.items():
|
|
46
|
+
if key == inner:
|
|
47
|
+
|
|
48
|
+
item = {
|
|
49
|
+
'key': key,
|
|
50
|
+
'val': val
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
item['val'] = datetime.strptime(val, '%Y-%m-%d')
|
|
55
|
+
except ValueError:
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
test.append(item)
|
|
59
|
+
|
|
60
|
+
filtered_list = [x for x in test if x['val'] != '']
|
|
61
|
+
|
|
62
|
+
final = []
|
|
63
|
+
for item in distinct_list:
|
|
64
|
+
try:
|
|
65
|
+
tested = datetime.strftime(
|
|
66
|
+
max([x['val'] for x in filtered_list if x['key'] == item]), '%Y-%m-%d')
|
|
67
|
+
final.append({item: tested})
|
|
68
|
+
except ValueError:
|
|
69
|
+
final.append({item: ''})
|
|
70
|
+
|
|
71
|
+
return final
|
|
72
|
+
|
|
73
|
+
def prime(self, base_dict: dict, user_doc_questions: list, user_doc_ids_by_user: list):
|
|
74
|
+
"""Prime"""
|
|
75
|
+
user_doc_questions = [
|
|
76
|
+
x for x in user_doc_questions if x['userDocId'] in user_doc_ids_by_user]
|
|
77
|
+
|
|
78
|
+
test = []
|
|
79
|
+
for k, v in enumerate(base_dict.items()):
|
|
80
|
+
for item in user_doc_questions:
|
|
81
|
+
if (k > 1) and (item['docRef'] == v[1]['docRef']):
|
|
82
|
+
test.append(
|
|
83
|
+
({f"{v[1]['docType']} {item['text']}": item['answer']}))
|
|
84
|
+
|
|
85
|
+
return test
|
|
86
|
+
|
|
87
|
+
def build_final(self, user_docs: list, header_list: list):
|
|
88
|
+
"""Build final"""
|
|
89
|
+
results = []
|
|
90
|
+
for item in header_list:
|
|
91
|
+
results.append({'key': item, 'val': ''})
|
|
92
|
+
for inner in user_docs:
|
|
93
|
+
|
|
94
|
+
for k, v in inner.items():
|
|
95
|
+
if item == k:
|
|
96
|
+
results.append({'key': k, 'val': v})
|
|
97
|
+
|
|
98
|
+
final_results = []
|
|
99
|
+
test = list(set(x['key'] for x in results))
|
|
100
|
+
for item in test:
|
|
101
|
+
tested = [x['val'] for x in results if x['key'] == item]
|
|
102
|
+
|
|
103
|
+
if len(tested) > 1:
|
|
104
|
+
final_results.append({
|
|
105
|
+
item: tested[1]
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
else:
|
|
109
|
+
final_results.append({
|
|
110
|
+
item: ''
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
return final_results
|
|
114
|
+
|
|
115
|
+
def build_safety_ticket_report(self):
|
|
116
|
+
"""Build safety ticket report"""
|
|
117
|
+
headers = self.get_report_headers()
|
|
118
|
+
|
|
119
|
+
b2c_users = self.b2c_helper.get_users(self.company_id)
|
|
120
|
+
|
|
121
|
+
users = [
|
|
122
|
+
x for x in b2c_users if x[f'extension_{os.environ["B2C_EXTENSION_APP_CLIENT_ID"]}_UserRoles'] != '8amAdmin']
|
|
123
|
+
|
|
124
|
+
user_docs = self.db.get_results(
|
|
125
|
+
query=f"SELECT * FROM c WHERE c.type = 'userDoc' and ARRAY_CONTAINS({[x['id'] for x in users]}, c.userId)")
|
|
126
|
+
|
|
127
|
+
user_doc_questions = self.db.get_results(
|
|
128
|
+
query=f"SELECT * FROM c WHERE c.type = 'userDocQuestion' and ARRAY_CONTAINS({[x['id'] for x in user_docs]}, c.userDocId)")
|
|
129
|
+
|
|
130
|
+
doc_questions = self.db.get_results(
|
|
131
|
+
query=f"SELECT * FROM c WHERE c.type = 'docQuestion'")
|
|
132
|
+
|
|
133
|
+
new_headers = []
|
|
134
|
+
for item in headers:
|
|
135
|
+
for inner in doc_questions:
|
|
136
|
+
if item['docRef'] == inner['docRef']:
|
|
137
|
+
new_headers.append(f"{item['docType']} {inner['text']}")
|
|
138
|
+
|
|
139
|
+
user_rows = []
|
|
140
|
+
for user in users:
|
|
141
|
+
user_doc_ids_by_user = [x['id']
|
|
142
|
+
for x in user_docs if user['id'] == x['userId']]
|
|
143
|
+
|
|
144
|
+
test = {**{'User Name': user['displayName'],
|
|
145
|
+
'User Id': user['id']}, **dict(list(enumerate(headers)))}
|
|
146
|
+
|
|
147
|
+
test = self.prime(test, user_doc_questions, user_doc_ids_by_user)
|
|
148
|
+
|
|
149
|
+
if test:
|
|
150
|
+
docs = self.remove_duplicates(test)
|
|
151
|
+
row = self.build_final(docs, new_headers)
|
|
152
|
+
row[0] = {
|
|
153
|
+
'User Name': user['displayName']
|
|
154
|
+
}
|
|
155
|
+
result = {}
|
|
156
|
+
for obj in row:
|
|
157
|
+
result.update(obj)
|
|
158
|
+
user_rows.append(result)
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
'file_name': 'Safety_Tickets_Report.csv',
|
|
162
|
+
'headers': ['User Name'] + new_headers,
|
|
163
|
+
'rows': user_rows
|
|
164
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class VendorDeficienciesReport:
|
|
5
|
+
"""Vendor Deficiencies Report"""
|
|
6
|
+
|
|
7
|
+
def __init__(self, db, company_id: str) -> None:
|
|
8
|
+
self.db = db
|
|
9
|
+
self.company_id = company_id
|
|
10
|
+
|
|
11
|
+
def get_ratings_updated(self, vendor_list: dict) -> dict:
|
|
12
|
+
"""Get ratings updated"""
|
|
13
|
+
if ('ratingsUpdated' in vendor_list) and (vendor_list['ratingsUpdated'] != ''):
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
'dateRatingApplied': datetime.fromtimestamp(
|
|
17
|
+
int(vendor_list['ratingsUpdated'])).strftime('%Y-%m-%d'),
|
|
18
|
+
'daysSinceRatingApplied': (
|
|
19
|
+
datetime.now() - datetime.fromtimestamp(int(vendor_list['ratingsUpdated']))).days
|
|
20
|
+
}
|
|
21
|
+
else:
|
|
22
|
+
return {
|
|
23
|
+
'dateRatingApplied': '',
|
|
24
|
+
'daysSinceRatingApplied': ''
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
def split_vendor_list_by_deficiency(self, vendor_list: dict) -> list:
|
|
28
|
+
"""Split vendor list by deficiency"""
|
|
29
|
+
new_list = []
|
|
30
|
+
if ('vendorOverallItems' in vendor_list) and (len(vendor_list['vendorOverallItems']) > 1):
|
|
31
|
+
for deficiency in vendor_list['vendorOverallItems']:
|
|
32
|
+
new_list.append({
|
|
33
|
+
'vendor_id': vendor_list['vendorId'],
|
|
34
|
+
'vendorName': vendor_list['vendorName'],
|
|
35
|
+
'vendorSubscriptionType': vendor_list['vendorSubscriptionType'],
|
|
36
|
+
'tags': vendor_list['tags'],
|
|
37
|
+
'vendorOverallRating': vendor_list['vendorOverallRating'],
|
|
38
|
+
'vendorDeficiency': deficiency,
|
|
39
|
+
'ratingsUpdated': vendor_list['ratingsUpdated'],
|
|
40
|
+
'vendorAccountAdmin': vendor_list['vendorAccountAdmin']
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
else:
|
|
44
|
+
new_list.append({
|
|
45
|
+
'vendor_id': vendor_list['vendorId'],
|
|
46
|
+
'vendorName': vendor_list['vendorName'],
|
|
47
|
+
'vendorSubscriptionType': vendor_list['vendorSubscriptionType'],
|
|
48
|
+
'tags': vendor_list['tags'],
|
|
49
|
+
'vendorOverallRating': vendor_list['vendorOverallRating'],
|
|
50
|
+
'vendorDeficiency': [x for x in vendor_list['vendorOverallItems']][0] if len(vendor_list['vendorOverallItems']) > 0 else '',
|
|
51
|
+
'ratingsUpdated': vendor_list['ratingsUpdated'],
|
|
52
|
+
'vendorAccountAdmin': vendor_list['vendorAccountAdmin']
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
return new_list
|
|
56
|
+
|
|
57
|
+
def build_vendor_deficiencies_report(self) -> dict:
|
|
58
|
+
"""Build vendor deficiencies report"""
|
|
59
|
+
vendor_lists = self.db.get_results(
|
|
60
|
+
query=f"SELECT c.vendorId, c.vendorName, c.vendorSubscriptionType, c.tags, c.vendorOverallRating, c.vendorOverallItems, c.employerDates.ratingsUpdated,\
|
|
61
|
+
c.vendorAccountAdmin from c where c.type = 'vendorList'AND c.employerId = '{self.company_id}'")
|
|
62
|
+
|
|
63
|
+
formatted_list = []
|
|
64
|
+
for vendor_list in vendor_lists:
|
|
65
|
+
vendor_list['ratingsUpdated'] = self.get_ratings_updated(
|
|
66
|
+
vendor_list)
|
|
67
|
+
|
|
68
|
+
vendor_list: list = self.split_vendor_list_by_deficiency(
|
|
69
|
+
vendor_list)
|
|
70
|
+
|
|
71
|
+
for item in vendor_list:
|
|
72
|
+
item['tags'] = '\n'.join(item['tags'])
|
|
73
|
+
|
|
74
|
+
formatted_list.append({
|
|
75
|
+
'vendor_id': item['vendor_id'],
|
|
76
|
+
'Vendor Name': item['vendorName'],
|
|
77
|
+
'Subscription Type': item['vendorSubscriptionType'],
|
|
78
|
+
'Vendor Tags': item['tags'],
|
|
79
|
+
'Vendor Rating': item['vendorOverallRating'],
|
|
80
|
+
'Current Deficiencies': item['vendorDeficiency'],
|
|
81
|
+
'Date Rating Applied': item['ratingsUpdated']['dateRatingApplied'],
|
|
82
|
+
'Days Since Rating Applied': item['ratingsUpdated']['daysSinceRatingApplied'],
|
|
83
|
+
'Admin Name': item['vendorAccountAdmin']['name'],
|
|
84
|
+
'Admin Email': item['vendorAccountAdmin']['email'],
|
|
85
|
+
'Admin Phone': item['vendorAccountAdmin']['phone']
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
'file_name': 'Def_Report.csv',
|
|
90
|
+
'headers': list(formatted_list[0].keys()),
|
|
91
|
+
'rows': formatted_list
|
|
92
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class VendorDocumentsReport:
|
|
5
|
+
"""Report Vendor Documents"""
|
|
6
|
+
|
|
7
|
+
def __init__(self, db, company_id: str) -> None:
|
|
8
|
+
self.db = db
|
|
9
|
+
self.company_id = company_id
|
|
10
|
+
|
|
11
|
+
def key_func(self, k: dict) -> str:
|
|
12
|
+
"""Key function"""
|
|
13
|
+
return k['Vendor Name']
|
|
14
|
+
|
|
15
|
+
def match_expiry_date(self, vendor_doc: dict, vendor_qoc_questions: list) -> str:
|
|
16
|
+
"""Match expiry date"""
|
|
17
|
+
expiry_date = ''
|
|
18
|
+
|
|
19
|
+
if 'values' in vendor_doc['ocrOutput']:
|
|
20
|
+
|
|
21
|
+
if vendor_doc['ocrOutput']['values'] != []:
|
|
22
|
+
|
|
23
|
+
for item in vendor_doc['ocrOutput']['values']:
|
|
24
|
+
|
|
25
|
+
if item['key'] == 'expiry_date':
|
|
26
|
+
|
|
27
|
+
if item['updatedValue'] != '':
|
|
28
|
+
expiry_date = item['updatedValue']
|
|
29
|
+
else:
|
|
30
|
+
expiry_date = item['ocrValue']
|
|
31
|
+
else:
|
|
32
|
+
|
|
33
|
+
for vendor_qoc_question in vendor_qoc_questions:
|
|
34
|
+
if vendor_doc['id'] == vendor_qoc_question['vendorDocId']:
|
|
35
|
+
expiry_date = vendor_qoc_question['answer']
|
|
36
|
+
|
|
37
|
+
else:
|
|
38
|
+
|
|
39
|
+
for vendor_qoc_question in vendor_qoc_questions:
|
|
40
|
+
if vendor_doc['id'] == vendor_qoc_question['vendorDocId']:
|
|
41
|
+
expiry_date = vendor_qoc_question['answer']
|
|
42
|
+
|
|
43
|
+
return expiry_date
|
|
44
|
+
|
|
45
|
+
def build_vendor_document_report(self) -> dict:
|
|
46
|
+
"""Build vendor document report"""
|
|
47
|
+
|
|
48
|
+
headers = ["Vendor Name", "Document Type", "Document Name",
|
|
49
|
+
"Status", "Expiry Date", "Last Modified"]
|
|
50
|
+
|
|
51
|
+
vendor_list_by_employer = self.db.get_results(
|
|
52
|
+
query=f"SELECT c.vendorId, c.vendorName FROM c WHERE c.type = 'vendorList' AND c.employerId = '{self.company_id}'")
|
|
53
|
+
|
|
54
|
+
results = [x['vendorId'] for x in vendor_list_by_employer]
|
|
55
|
+
|
|
56
|
+
docs = self.db.get_results(
|
|
57
|
+
query=f"SELECT c.id, c.vendorId, c.employerId, c.docType, c.docName, c.status, c.ocrOutput, c._ts FROM c WHERE c.type = 'vendorDoc' AND ARRAY_CONTAINS({results}, c.vendorId) AND c.status != 'Not Submitted'")
|
|
58
|
+
|
|
59
|
+
vendor_doc_questions = self.db.get_results(
|
|
60
|
+
query="SELECT * FROM c WHERE c.type = 'vendorDocQuestion' AND c.key = 'expiry_date'")
|
|
61
|
+
|
|
62
|
+
results = []
|
|
63
|
+
for doc in docs:
|
|
64
|
+
|
|
65
|
+
if 'docName' in doc:
|
|
66
|
+
doc_name = doc['docName']
|
|
67
|
+
else:
|
|
68
|
+
doc_name = ''
|
|
69
|
+
|
|
70
|
+
expiry_date = self.match_expiry_date(doc, vendor_doc_questions)
|
|
71
|
+
|
|
72
|
+
if 'employerId' in doc:
|
|
73
|
+
if doc['employerId'] == self.company_id:
|
|
74
|
+
|
|
75
|
+
results.append({
|
|
76
|
+
'Vendor Name': [x['vendorName'] for x in vendor_list_by_employer if x['vendorId'] == doc['vendorId']][0],
|
|
77
|
+
'Document Type': doc['docType'],
|
|
78
|
+
'Document Name': doc_name,
|
|
79
|
+
'Status': doc['status'],
|
|
80
|
+
'Expiry Date': expiry_date,
|
|
81
|
+
'Last Modified': datetime.fromtimestamp(doc["_ts"]).strftime("%Y-%m-%d")
|
|
82
|
+
})
|
|
83
|
+
else:
|
|
84
|
+
pass
|
|
85
|
+
|
|
86
|
+
else:
|
|
87
|
+
results.append({
|
|
88
|
+
'Vendor Name': [x['vendorName'] for x in vendor_list_by_employer if x['vendorId'] == doc['vendorId']][0],
|
|
89
|
+
'Document Type': doc['docType'],
|
|
90
|
+
'Document Name': doc_name,
|
|
91
|
+
'Status': doc['status'],
|
|
92
|
+
'Expiry Date': expiry_date,
|
|
93
|
+
'Last Modified': datetime.fromtimestamp(doc["_ts"]).strftime("%Y-%m-%d")
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
rows = sorted(results, key=self.key_func)
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
'file_name': 'Vendor_Documents_Report.csv',
|
|
100
|
+
'headers': headers,
|
|
101
|
+
'rows': rows
|
|
102
|
+
}
|