XH_Digital_Management/application/org_mgnt/views.py

496 lines
21 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.http import JsonResponse, Http404
from django.shortcuts import render, get_object_or_404, redirect
from django.template.loader import render_to_string
from django.urls import reverse
from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from django.contrib.auth.decorators import login_required, permission_required
from application.org_mgnt.forms import CompanyEntityForm, PrimaryDepartmentForm, SecondaryDepartmentForm, \
CompanyBankAccountForm
from application.org_mgnt.models import PrimaryDepartment, SecondaryDepartment, CompanyEntity, EntityChangeRecord, \
CompanyBankAccount
from application.org_mgnt.serializers import EntityChangeRecordSerializer, CompanyBankAccountSerializer
from common.utils.page_helper import paginate_query_and_assign_numbers
class StandardResultsSetPagination(PageNumberPagination):
page_size = 5
page_size_query_param = 'page_size'
max_page_size = 100
@login_required
@permission_required('org_mgnt.view_companyentity', raise_exception=True)
def eir_list_view(request):
# 声明查询集
query_set = CompanyEntity.objects.filter().order_by('-entity_id')
# 获取查询参数
company_name = request.GET.get('company_name', '')
business_status = request.GET.get('business_status', '')
taxpayer_identification_number = request.GET.get('taxpayer_identification_number', '')
# 根据提供的参数进行筛选
if company_name:
query_set = query_set.filter(company_name__icontains=company_name)
if business_status:
query_set = query_set.filter(business_status=business_status)
if taxpayer_identification_number:
query_set = query_set.filter(taxpayer_identification_number__icontains=taxpayer_identification_number)
# 对查询结果进行分页每页10条记录
items = paginate_query_and_assign_numbers(
request=request,
queryset=query_set,
per_page=10
)
# 构建上下文查询参数字符串
query_params = '&company_name={}&business_status={}&taxpayer_identification_number={}'.format(
company_name, business_status, taxpayer_identification_number
)
# Excel上传模板
template_name = "组织管理-公司主体信息登记表-Excel上传模板.xlsx"
# 构建上下文
context = {
"model_config": 'org_mgnt.CompanyEntity',
"items": items,
"breadcrumb_list": [
{"title": "首页", "name": "index"},
{"title": "组织管理", "name": "index"},
{"title": "公司主体信息登记表", "name": "eir_list"}
],
"filters": [
{"type": "text", "id": "company_name", "name": "company_name", "label": "公司名称",
"placeholder": "请输入公司名称"},
{"type": "select", "id": "business_status", "name": "business_status", "label": "公司经营状态",
"options": [{"value": "存续", "display": "存续"}, {"value": "注销", "display": "注销"}]},
{"type": "text", "id": "taxpayer_identification_number", "name": "taxpayer_identification_number",
"label": "纳税人识别号", "placeholder": "请输入纳税人识别号"}
],
"table_exclude_field_name": ['entity_id'],
"excel_upload_config": {
"template_url": reverse("download_template", kwargs={'template_name': template_name}),
"parse_url": reverse("common_excel_parse"),
"save_url": reverse("save_excel_table_data"),
"fields_preview_config": {
"entity_id": {"type": "text", "width": "180px"},
"company_type": {"type": "text", "width": "180px"},
"company_name": {"type": "text", "width": "180px"},
"registration_address": {"type": "text", "width": "180px"},
"registered_capital": {"type": "text", "width": "180px"},
"capital_paid_time": {"type": "date", "width": "180px"},
"capital_paid": {"type": "text", "width": "180px"},
"establishment_time": {"type": "date", "width": "180px"},
"operation_period": {"type": "text", "width": "180px"},
"taxpayer_identification_number": {"type": "text", "width": "180px"},
"business_status": {"type": "select", "width": "180px", "options": ["存续", "注销"]},
"scope_of_business": {"type": "text", "width": "180px"},
"purpose_of_company": {"type": "text", "width": "180px"},
"shareholders_and_stakes": {"type": "text", "width": "180px"},
"chairman": {"type": "text", "width": "180px"},
"directors": {"type": "text", "width": "180px"},
"supervisor_chairman": {"type": "text", "width": "180px"},
"employee_supervisor": {"type": "text", "width": "180px"},
"supervisors": {"type": "text", "width": "180px"},
"general_manager": {"type": "text", "width": "180px"},
"financial_officer": {"type": "text", "width": "180px"},
"historical_evolution": {"type": "textarea", "width": "180px"},
"related_bank_accounts": {"type": "text", "width": "180px"},
"actions": {"type": "actions", "width": "100px"}
}
},
"query_params": query_params,
"form_action_url": 'eir_list',
"modify_url": reverse("eir_list_modify"),
"add_url": reverse("eir_list_add"),
"delete_url": reverse("eir_list_delete"),
}
return render(request, 'ce_list.html', context)
@login_required
@permission_required('org_mgnt.add_companyentity', raise_exception=True)
def eir_list_add(request):
if request.method == 'POST':
form = CompanyEntityForm(request.POST)
if form.is_valid():
form.save()
return JsonResponse({"message": "添加成功"})
else:
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
form = CompanyEntityForm()
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)
@login_required
@permission_required('org_mgnt.change_companyentity', raise_exception=True)
def eir_list_modify(request):
if request.method == 'POST':
if 'id' in request.POST:
instance = CompanyEntity.objects.get(entity_id=request.POST['id'])
form = CompanyEntityForm(request.POST, instance=instance)
else:
form = CompanyEntityForm(request.POST)
if form.is_valid():
form.save()
return JsonResponse({"message": "保存成功"})
else:
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
if 'id' in request.GET:
try:
instance = CompanyEntity.objects.get(entity_id=request.GET['id'])
form = CompanyEntityForm(instance=instance)
except CompanyEntity.DoesNotExist:
raise Http404("对象不存在")
else:
form = CompanyEntityForm()
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)
@login_required
@permission_required('org_mgnt.delete_companyentity', raise_exception=True)
def eir_list_delete(request):
if request.method == 'GET':
target_id = request.GET.get('target_id')
CompanyEntity.objects.filter(entity_id=target_id).delete()
return JsonResponse({"message": "删除成功"})
return JsonResponse({"message": "无效的请求方法"}, status=405)
@api_view(['GET'])
@login_required
@permission_required('org_mgnt.view_entitychangerecord', raise_exception=True)
def change_record_list(request, entity_id):
entity = get_object_or_404(CompanyEntity, pk=entity_id)
change_records = entity.change_records.all().order_by('record_id')
paginator = StandardResultsSetPagination()
paginated_change_records = paginator.paginate_queryset(change_records, request)
serializer = EntityChangeRecordSerializer(paginated_change_records, many=True)
return paginator.get_paginated_response(serializer.data)
@api_view(['GET'])
@login_required
@permission_required('org_mgnt.view_companybankaccount', raise_exception=True)
def bank_account_list(request, entity_id):
entity = get_object_or_404(CompanyEntity, pk=entity_id)
bank_accounts = entity.bank_accounts.all().order_by('account_id')
paginator = StandardResultsSetPagination()
paginated_bank_accounts = paginator.paginate_queryset(bank_accounts, request)
serializer = CompanyBankAccountSerializer(paginated_bank_accounts, many=True)
response = paginator.get_paginated_response(serializer.data)
response_data = response.data
response_data['next_page'] = paginator.page.next_page_number() if paginator.page.has_next() else None
response_data['previous_page'] = paginator.page.previous_page_number() if paginator.page.has_previous() else None
return Response(response_data)
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def get_bank_account_details(request, account_id):
bank_account = get_object_or_404(CompanyBankAccount, pk=account_id)
serializer = CompanyBankAccountSerializer(bank_account)
return Response(serializer.data)
@api_view(['POST'])
@permission_classes([IsAuthenticated])
def add_bank_account(request, entity_id):
entity = get_object_or_404(CompanyEntity, pk=entity_id)
serializer = CompanyBankAccountSerializer(data=request.data)
if serializer.is_valid():
serializer.save(company_entity=entity)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['PUT'])
@permission_classes([IsAuthenticated])
def update_bank_account(request, account_id):
bank_account = get_object_or_404(CompanyBankAccount, pk=account_id)
serializer = CompanyBankAccountSerializer(bank_account, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['DELETE'])
@permission_classes([IsAuthenticated])
def delete_bank_account(request, account_id):
bank_account = get_object_or_404(CompanyBankAccount, pk=account_id)
bank_account.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
@login_required
@permission_required('org_mgnt.view_primarydepartment', raise_exception=True)
def pd_list_view(request):
# 声明查询集
query_set = PrimaryDepartment.objects.filter().order_by('-primary_department_id')
# 获取查询参数
department_name = request.GET.get('department_name', '')
# 根据提供的参数进行筛选
if department_name:
query_set = query_set.filter(department_name__icontains=department_name)
# 对查询结果进行分页每页10条记录
items = paginate_query_and_assign_numbers(
request=request,
queryset=query_set,
per_page=10
)
# 构建上下文查询参数字符串
query_params = '&department_name={}'.format(department_name)
# Excel上传模板
template_name = "组织管理-一级部门表-Excel上传模板.xlsx"
# 构建上下文
context = {
"model_config": 'org_mgnt.PrimaryDepartment',
"items": items,
"breadcrumb_list": [
{"title": "首页", "name": "index"},
{"title": "组织管理", "name": "index"},
{"title": "一级部门表", "name": "pd_list"}
],
"filters": [
{"type": "text", "id": "department_name", "name": "department_name", "label": "一级部门名称",
"placeholder": "请输入一级部门名称"}
],
"table_exclude_field_name": ['primary_department_id'],
"excel_upload_config": {
"template_url": reverse("download_template", kwargs={'template_name': template_name}),
"parse_url": reverse("common_excel_parse"),
"save_url": reverse("save_excel_table_data"),
"fields_preview_config": {
"primary_department_id": {"type": "text", "width": "180px"},
"department_name": {"type": "text", "width": "180px"},
"description": {"type": "text", "width": "180px"},
"actions": {"type": "actions", "width": "100px"}
}
},
"query_params": query_params,
"form_action_url": 'pd_list',
"modify_url": reverse("pd_list_modify"),
"add_url": reverse("pd_list_add"),
"delete_url": reverse("pd_list_delete"),
}
return render(request, 'items_list.html', context)
@login_required
@permission_required('org_mgnt.add_primarydepartment', raise_exception=True)
def pd_list_add(request):
if request.method == 'POST':
form = PrimaryDepartmentForm(request.POST)
if form.is_valid():
form.save()
return JsonResponse({"message": "添加成功"})
else:
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
form = PrimaryDepartmentForm()
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)
@login_required
@permission_required('org_mgnt.change_primarydepartment', raise_exception=True)
def pd_list_modify(request):
if request.method == 'POST':
if 'id' in request.POST:
instance = PrimaryDepartment.objects.get(primary_department_id=request.POST['id'])
form = PrimaryDepartmentForm(request.POST, instance=instance)
else:
form = PrimaryDepartmentForm(request.POST)
if form.is_valid():
form.save()
return JsonResponse({"message": "保存成功"})
else:
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
if 'id' in request.GET:
try:
instance = PrimaryDepartment.objects.get(primary_department_id=request.GET['id'])
form = PrimaryDepartmentForm(instance=instance)
except PrimaryDepartment.DoesNotExist:
raise Http404("对象不存在")
else:
form = PrimaryDepartmentForm()
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)
@login_required
@permission_required('org_mgnt.delete_primarydepartment', raise_exception=True)
def pd_list_delete(request):
if request.method == 'GET':
primary_department_id = request.GET.get('primary_department_id')
PrimaryDepartment.objects.filter(primary_department_id=primary_department_id).delete()
return JsonResponse({"message": "删除成功"})
return JsonResponse({"message": "无效的请求方法"}, status=405)
@login_required
@permission_required('org_mgnt.view_secondarydepartment', raise_exception=True)
def sd_list_view(request):
# 声明查询集
query_set = SecondaryDepartment.objects.filter().order_by('-secondary_department_id')
# 获取查询参数
secondary_department_name = request.GET.get('secondary_department_name', '')
# 根据提供的参数进行筛选
if secondary_department_name:
query_set = query_set.filter(secondary_department_name__icontains=secondary_department_name)
# 对查询结果进行分页每页10条记录
items = paginate_query_and_assign_numbers(
request=request,
queryset=query_set,
per_page=10
)
# 构建上下文查询参数字符串
query_params = '&secondary_department_name={}'.format(secondary_department_name)
# Excel上传模板
template_name = "组织管理-二级部门表-Excel上传模板.xlsx"
# 构建上下文
context = {
"model_config": 'org_mgnt.SecondaryDepartment',
"items": items,
"breadcrumb_list": [
{"title": "首页", "name": "index"},
{"title": "组织管理", "name": "index"},
{"title": "二级部门表", "name": "sd_list"}
],
"filters": [
{"type": "text", "id": "secondary_department_name", "name": "secondary_department_name",
"label": "二级部门名称", "placeholder": "请输入二级部门名称"}
],
"table_exclude_field_name": ['secondary_department_id'],
"excel_upload_config": {
"template_url": reverse("download_template", kwargs={'template_name': template_name}),
"parse_url": reverse("common_excel_parse"),
"save_url": reverse("save_excel_table_data"),
"fields_preview_config": {
"secondary_department_id": {"type": "text", "width": "180px"},
"primary_department_id": {"type": "number", "width": "180px"},
"secondary_department_name": {"type": "text", "width": "180px"},
"description": {"type": "text", "width": "180px"},
"actions": {"type": "actions", "width": "100px"}
}
},
"query_params": query_params,
"form_action_url": 'sd_list',
"modify_url": reverse("sd_list_modify"),
"add_url": reverse("sd_list_add"),
"delete_url": reverse("sd_list_delete"),
}
return render(request, 'items_list.html', context)
@login_required
@permission_required('org_mgnt.add_secondarydepartment', raise_exception=True)
def sd_list_add(request):
if request.method == 'POST':
form = SecondaryDepartmentForm(request.POST)
if form.is_valid():
form.save()
return JsonResponse({"message": "添加成功"})
else:
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
form = SecondaryDepartmentForm()
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)
@login_required
@permission_required('org_mgnt.change_secondarydepartment', raise_exception=True)
def sd_list_modify(request):
if request.method == 'POST':
if 'id' in request.POST:
instance = SecondaryDepartment.objects.get(secondary_department_id=request.POST['id'])
form = SecondaryDepartmentForm(request.POST, instance=instance)
else:
form = SecondaryDepartmentForm(request.POST)
if form.is_valid():
form.save()
return JsonResponse({"message": "保存成功"})
else:
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
if 'id' in request.GET:
try:
instance = SecondaryDepartment.objects.get(secondary_department_id=request.GET['id'])
form = SecondaryDepartmentForm(instance=instance)
except SecondaryDepartment.DoesNotExist:
raise Http404("对象不存在")
else:
form = SecondaryDepartmentForm()
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)
@login_required
@permission_required('org_mgnt.delete_secondarydepartment', raise_exception=True)
def sd_list_delete(request):
if request.method == 'GET':
secondary_department_id = request.GET.get('secondary_department_id')
SecondaryDepartment.objects.filter(secondary_department_id=secondary_department_id).delete()
return JsonResponse({"message": "删除成功"})
return JsonResponse({"message": "无效的请求方法"}, status=405)