From 47f1b0d609a7e9a12713529714cc251ebb8b27d9 Mon Sep 17 00:00:00 2001 From: Alejolonber25 Date: Sun, 12 May 2024 16:12:23 -0500 Subject: [PATCH] END:BUILD: Export requests to excel implemented --- .../control_board/base_workteam.html | 1 - .../templates/control_board/table.html | 12 +- hiring_module/hiring_app/urls.py | 2 + hiring_module/hiring_app/views/__init__.py | 1 + .../control_board/export_requests_view.py | 8 + .../views/control_board/utilities.py | 257 +++++++++++++++++- hiring_module/requirements.txt | 1 + 7 files changed, 278 insertions(+), 4 deletions(-) create mode 100644 hiring_module/hiring_app/views/control_board/export_requests_view.py diff --git a/hiring_module/hiring_app/templates/control_board/base_workteam.html b/hiring_module/hiring_app/templates/control_board/base_workteam.html index a67095a..08470e6 100644 --- a/hiring_module/hiring_app/templates/control_board/base_workteam.html +++ b/hiring_module/hiring_app/templates/control_board/base_workteam.html @@ -13,7 +13,6 @@

Panel de control

-
diff --git a/hiring_module/hiring_app/templates/control_board/table.html b/hiring_module/hiring_app/templates/control_board/table.html index da3c806..b1704c8 100644 --- a/hiring_module/hiring_app/templates/control_board/table.html +++ b/hiring_module/hiring_app/templates/control_board/table.html @@ -10,8 +10,10 @@

Solicitudes de contratación

-
+
Listado de solicitudes de contratación
+
@@ -56,4 +58,10 @@
Listado de solicitudes de contrata
-
\ No newline at end of file +
+ + \ No newline at end of file diff --git a/hiring_module/hiring_app/urls.py b/hiring_module/hiring_app/urls.py index a99599b..644bb0c 100644 --- a/hiring_module/hiring_app/urls.py +++ b/hiring_module/hiring_app/urls.py @@ -10,6 +10,7 @@ from hiring_app.views.control_board.administrator_user_list_view import AdministratorUserListView from hiring_app.views.control_board.add_user_view import AddUserView from hiring_app.views.statistical_registers.statistics_view import StatisticsView +from hiring_app.views.control_board.export_requests_view import ExportRequestsView app_name = 'hiring_app' @@ -43,5 +44,6 @@ path('download_rut//', download_rut_file, name='download_rut'), path('statistics/', StatisticsView.as_view(), name='statistics'), + path('export_requests/', ExportRequestsView.as_view(), name='export_requests') ] diff --git a/hiring_module/hiring_app/views/__init__.py b/hiring_module/hiring_app/views/__init__.py index 5ba591d..50fe5f3 100644 --- a/hiring_module/hiring_app/views/__init__.py +++ b/hiring_module/hiring_app/views/__init__.py @@ -9,3 +9,4 @@ from .control_board.leader_dashboard_view import LeaderDashboardView from .control_board.manager_dashboard_view import ManagerDashboardView from .request_hiring.assign_leader import AssignLeaderView +from .control_board.export_requests_view import ExportRequestsView diff --git a/hiring_module/hiring_app/views/control_board/export_requests_view.py b/hiring_module/hiring_app/views/control_board/export_requests_view.py new file mode 100644 index 0000000..7572999 --- /dev/null +++ b/hiring_module/hiring_app/views/control_board/export_requests_view.py @@ -0,0 +1,8 @@ +from django.shortcuts import redirect +from django.views import View +from hiring_app.model.user_model import CustomUser +from .utilities import export_requests +class ExportRequestsView(View): + + def get(self, request): + return export_requests(self.request.user) \ No newline at end of file diff --git a/hiring_module/hiring_app/views/control_board/utilities.py b/hiring_module/hiring_app/views/control_board/utilities.py index 88b8c00..0c0b749 100644 --- a/hiring_module/hiring_app/views/control_board/utilities.py +++ b/hiring_module/hiring_app/views/control_board/utilities.py @@ -7,6 +7,8 @@ from hiring_app.model.cex_contract_request_model import CEXContractRequest from hiring_app.model.monitoring_contract_request_model import MonitoringContractRequest from hiring_app.model.provision_of_services_request_model import ProvisionOfServicesContractRequest +from django.http import HttpResponse +from openpyxl import Workbook # Decorator to redirect users to the correct dashboard based on their role def role_redirect(view_func): @@ -105,4 +107,257 @@ def get_requests(user): - +def export_requests(user): + groups = [group.name for group in user.groups.all()] + requests_CEX = CEXContractRequest.objects.none() + requests_monitoring = MonitoringContractRequest.objects.none() + requests_pos = ProvisionOfServicesContractRequest.objects.none() + + if 'admin' in groups: + requests_CEX = CEXContractRequest.objects.all() + requests_monitoring = MonitoringContractRequest.objects.all() + requests_pos = ProvisionOfServicesContractRequest.objects.all() + elif 'leader' in groups: + requests_CEX = CEXContractRequest.objects.filter(leader_assigned_to=user.id) + requests_monitoring = MonitoringContractRequest.objects.filter(leader_assigned_to=user.id) + requests_pos = ProvisionOfServicesContractRequest.objects.filter(leader_assigned_to=user.id) + elif 'manager' in groups: + requests_CEX = CEXContractRequest.objects.filter(manager_assigned_to=user.id) + requests_monitoring = MonitoringContractRequest.objects.filter(manager_assigned_to=user.id) + requests_pos = ProvisionOfServicesContractRequest.objects.filter(manager_assigned_to=user.id) + + + + wb = Workbook() + + ws1 = wb.active + ws1.title = "Solicitudes de Contratación" + + headers = [ + "ID", + "Start Date", + "Completion Date", + "Estimated Completion Date", + "Current State Start", + "State", + "Manager Assigned To", + "Leader Assigned To", + "Created By", + ] + + ws1.append(headers) + + for request in (list(requests_CEX) + list(requests_monitoring) + list(requests_pos)): + row_data = [ + str(request.id), + request.start_date.strftime('%Y-%m-%d'), + request.completion_date.strftime('%Y-%m-%d %H:%M:%S') if request.completion_date else None, + request.estimated_completion_date.strftime('%Y-%m-%d') if request.estimated_completion_date else None, + request.current_state_start.strftime('%Y-%m-%d %H:%M:%S'), + request.state, + f'{request.manager_assigned_to.first_name} {request.manager_assigned_to.last_name}' if request.manager_assigned_to else 'sin asignar', + f'{request.leader_assigned_to.first_name} {request.leader_assigned_to.last_name}' if request.leader_assigned_to else 'sin asignar', + f'{request.created_by.first_name} {request.created_by.last_name}' if request.created_by else 'sin asignar', + ] + ws1.append(row_data) + + + ws2 = wb.create_sheet(title="CEX") + + headers = [ + "ID", + "Start Date", + "Completion Date", + "Estimated Completion Date", + "Current State Start", + "State", + "Manager Assigned To", + "Leader Assigned To", + "Created By", + "Hiree Full Name", + "Hiree ID", + "Hiree Cellphone", + "Hiree Email", + "Cenco", + "Request Motive", + "Banking Entity", + "Bank Account Type", + "Bank Account Number", + "EPS", + "Pension Fund", + "ARL", + "Contract Value", + "Charge Account", + "RUT" + ] + ws2.append(headers) + + for cex_request in requests_CEX: + row_data = [ + str(cex_request.id), + cex_request.start_date.strftime('%Y-%m-%d'), + cex_request.completion_date.strftime('%Y-%m-%d %H:%M:%S') if cex_request.completion_date else None, + cex_request.estimated_completion_date.strftime('%Y-%m-%d') if cex_request.estimated_completion_date else None, + cex_request.current_state_start.strftime('%Y-%m-%d %H:%M:%S'), + cex_request.state, + f'{cex_request.manager_assigned_to.first_name} {cex_request.manager_assigned_to.last_name}' if cex_request.manager_assigned_to else 'sin asignar', + f'{cex_request.leader_assigned_to.first_name} {cex_request.leader_assigned_to.last_name}' if cex_request.leader_assigned_to else 'sin asignar', + f'{cex_request.created_by.first_name} {cex_request.created_by.last_name}' if cex_request.created_by else 'sin asignar', + cex_request.hiree_full_name, + cex_request.hiree_id, + cex_request.hiree_cellphone, + cex_request.hiree_email, + cex_request.cenco, + cex_request.request_motive, + cex_request.banking_entity, + cex_request.bank_account_type, + cex_request.bank_account_number, + cex_request.eps, + cex_request.pension_fund, + cex_request.arl, + cex_request.contract_value, + cex_request.charge_account, + cex_request.rut.url if cex_request.rut else "" + ] + ws2.append(row_data) + + ws3 = wb.create_sheet(title="Monitorías") + + headers = [ + "ID", + "Start Date", + "Completion Date", + "Estimated Completion Date", + "Current State Start", + "State", + "Manager Assigned To", + "Leader Assigned To", + "Created By", + "Cenco", + "Has Money in Cenco", + "Cenco Manager", + "Monitoring Type", + "Student Code", + "Student Full Name", + "Student ID", + "Student Email", + "Student Cellphone", + "Daviplata", + "Course or Project", + "Monitoring Description", + "Weekly Hours", + "Total Value to Pay", + "Is Unique Payment" + ] + ws3.append(headers) + + for monitoring_request in requests_monitoring: + row_data = [ + str(monitoring_request.id), + monitoring_request.start_date.strftime('%Y-%m-%d'), + monitoring_request.completion_date.strftime('%Y-%m-%d %H:%M:%S') if monitoring_request.completion_date else None, + monitoring_request.estimated_completion_date.strftime('%Y-%m-%d') if monitoring_request.estimated_completion_date else None, + monitoring_request.current_state_start.strftime('%Y-%m-%d %H:%M:%S'), + monitoring_request.state, + f'{monitoring_request.manager_assigned_to.first_name} {monitoring_request.manager_assigned_to.last_name}' if monitoring_request.manager_assigned_to else 'sin asignar', + f'{monitoring_request.leader_assigned_to.first_name} {monitoring_request.leader_assigned_to.last_name}' if monitoring_request.leader_assigned_to else 'sin asignar', + f'{monitoring_request.created_by.first_name} {monitoring_request.created_by.last_name}' if monitoring_request.created_by else 'sin asignar', + monitoring_request.cenco, + monitoring_request.has_money_in_cenco, + monitoring_request.cenco_manager, + monitoring_request.monitoring_type, + monitoring_request.student_code, + monitoring_request.student_full_name, + monitoring_request.student_id, + monitoring_request.student_email, + monitoring_request.student_cellphone, + monitoring_request.daviplata, + monitoring_request.course_or_proyect, + monitoring_request.monitoring_description, + monitoring_request.weekly_hours, + monitoring_request.total_value_to_pay, + monitoring_request.is_unique_payment + ] + ws3.append(row_data) + + ws4 = wb.create_sheet(title="Honorarios") + + headers = [ + "ID", + "Start Date", + "Completion Date", + "Estimated Completion Date", + "Current State Start", + "State", + "Manager Assigned To", + "Leader Assigned To", + "Created By", + "Hiree Full Name", + "Hiree ID", + "Hiree Cellphone", + "Hiree Email", + "Cenco", + "Request Motive", + "Banking Entity", + "Bank Account Type", + "Bank Account Number", + "EPS", + "Pension Fund", + "ARL", + "Contract Value", + "Charge Account", + "RUT", + "Course Name", + "Period", + "Group", + "Intensity", + "Total Hours", + "Course Code", + "Students Quantity", + "Additional Hours" + ] + + ws4.append(headers) + + for post_request in requests_pos: + row_data = [ + str(post_request.id), + post_request.start_date.strftime('%Y-%m-%d'), + post_request.completion_date.strftime('%Y-%m-%d %H:%M:%S') if post_request.completion_date else None, + post_request.estimated_completion_date.strftime('%Y-%m-%d') if post_request.estimated_completion_date else None, + post_request.current_state_start.strftime('%Y-%m-%d %H:%M:%S'), + post_request.state, + f'{post_request.manager_assigned_to.first_name} {post_request.manager_assigned_to.last_name}' if post_request.manager_assigned_to else 'sin asignar', + f'{post_request.leader_assigned_to.first_name} {post_request.leader_assigned_to.last_name}' if post_request.leader_assigned_to else 'sin asignar', + f'{post_request.created_by.first_name} {post_request.created_by.last_name}' if post_request.created_by else 'sin asignar', + post_request.hiree_full_name, + post_request.hiree_id, + post_request.hiree_cellphone, + post_request.hiree_email, + post_request.cenco, + post_request.request_motive, + post_request.banking_entity, + post_request.bank_account_type, + post_request.bank_account_number, + post_request.eps, + post_request.pension_fund, + post_request.arl, + post_request.contract_value, + post_request.charge_account, + post_request.rut.url if post_request.rut else "", + post_request.course_name, + post_request.period, + post_request.group, + post_request.intensity, + post_request.total_hours, + post_request.course_code, + post_request.students_quantity, + post_request.additional_hours + ] + ws4.append(row_data) + + response = HttpResponse(content_type='application/ms-excel') + response['Content-Disposition'] = 'attachment; filename="solicitudes.xlsx"' + wb.save(response) + + return response \ No newline at end of file diff --git a/hiring_module/requirements.txt b/hiring_module/requirements.txt index 96ffe06..029d66c 100644 --- a/hiring_module/requirements.txt +++ b/hiring_module/requirements.txt @@ -7,3 +7,4 @@ six == 1.16.0 sqlparse == 0.4.3 tzdata == 2022.7 selenium +openpyxl \ No newline at end of file