XH_Digital_Management/application/pjt_mgnt/views.py

292 lines
13 KiB
Python
Raw Normal View History

2024-06-07 03:47:15 +08:00
from django.http import JsonResponse, Http404
2024-06-14 16:47:43 +08:00
from django.shortcuts import render
2024-06-07 03:47:15 +08:00
from django.template.loader import render_to_string
2024-06-06 15:08:38 +08:00
from django.urls import reverse
2024-06-14 16:47:43 +08:00
from django.contrib.auth.decorators import login_required
2024-06-07 03:47:15 +08:00
from application.pjt_mgnt.forms import ProjectLedgerForm, EmployeeProjectIncomeSettlementForm
2024-05-31 20:17:40 +08:00
from application.pjt_mgnt.models import *
2024-06-14 16:47:43 +08:00
from common.auth import custom_permission_required
2024-05-31 20:17:40 +08:00
from common.utils.page_helper import paginate_query_and_assign_numbers
2024-05-29 09:27:39 +08:00
@login_required
2024-06-14 16:47:43 +08:00
@custom_permission_required('pjt_mgnt.view_projectledger')
2024-05-31 20:17:40 +08:00
def proj_ledger_list_view(request):
# 声明查询集
query_set = ProjectLedger.objects.filter().order_by('-project_id')
2024-05-29 09:27:39 +08:00
2024-05-31 20:17:40 +08:00
# 获取查询参数
project_name = request.GET.get('project_name', '')
customer_name = request.GET.get('customer_name', '')
# 根据提供的参数进行筛选
2024-06-06 15:08:38 +08:00
if project_name:
query_set = query_set.filter(project_name__icontains=project_name)
if customer_name:
2024-06-07 03:47:15 +08:00
query_set = query_set.filter(customer_name__icontain=customer_name)
2024-05-31 20:17:40 +08:00
# 对查询结果进行分页每页10条记录
items = paginate_query_and_assign_numbers(
request=request,
queryset=query_set,
per_page=10
)
# 构建上下文查询参数字符串
2024-06-06 15:08:38 +08:00
query_params = '&project_name={}&customer_name={}'.format(project_name, customer_name)
2024-06-06 20:56:06 +08:00
# Excel上传模板
template_name = "项目管理-项目台账-Excel上传模板.xlsx"
# 构建上下文
2024-05-31 20:17:40 +08:00
context = {
2024-06-06 20:56:06 +08:00
"model_config": 'pjt_mgnt.ProjectLedger',
"items": items,
"breadcrumb_list": [
{"title": "首页", "name": "index"},
{"title": "项目管理", "name": "index"},
{"title": "项目台账", "name": "proj_ledger_list"}
],
"filters": [
{"type": "text", "id": "project_name", "name": "project_name", "label": "项目名称",
"placeholder": "请输入项目名称"},
{"type": "text", "id": "customer_name", "name": "customer_name", "label": "客户名称",
"placeholder": "请输入客户名称"}
],
"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": {
"project_name": {"type": "text", "width": "180px"},
"start_date": {"type": "date", "width": "180px"},
"end_date": {"type": "date", "width": "180px"},
"primary_department": {"type": "text", "width": "180px"},
"customer_name": {"type": "text", "width": "180px"},
"province": {"type": "text", "width": "180px"},
"city": {"type": "text", "width": "180px"},
"district": {"type": "text", "width": "180px"},
"project_leader": {"type": "text", "width": "180px"},
"project_members": {"type": "text", "width": "180px"},
"project_status": {"type": "select", "options": ['进行中', '暂停', '待收款', '完成'], "width": "180px"},
"resource_type": {"type": "select", "options": ['公司', '个人'], "width": "180px"},
"project_nature": {"type": "select", "options": ['新增', '存量', '新增及存量', '老客户新业务'],
"width": "180px"},
"project_progress": {"type": "text", "width": "180px"},
"contract_date": {"type": "date", "width": "180px"},
"contract_amount": {"type": "number", "width": "180px"},
"contract_rate": {"type": "number", "width": "180px"},
"revenue": {"type": "number", "width": "180px"},
"cost_rate": {"type": "number", "width": "180px"},
"cost": {"type": "number", "width": "180px"},
"net_income": {"type": "number", "width": "180px"},
"total_amount_including_tax": {"type": "number", "width": "180px"},
"repayment_amount": {"type": "number", "width": "180px"},
"receivable_net_income": {"type": "number", "width": "180px"},
"actual_net_income": {"type": "number", "width": "180px"},
"outstanding_net_income": {"type": "number", "width": "180px"},
"notes": {"type": "text", "width": "180px"},
}
},
"query_params": query_params,
"form_action_url": 'proj_ledger_list',
"modify_url": reverse("proj_ledger_list_modify"),
"add_url": reverse("proj_ledger_list_add"),
"delete_url": reverse("proj_ledger_list_delete"),
2024-06-07 03:47:15 +08:00
"table_exclude_field_name": ['project_id'], # Exclude primary key
"add_button": True,
"import_excel_button": True
2024-05-31 20:17:40 +08:00
}
2024-06-07 03:47:15 +08:00
return render(request, 'items_list.html', context)
2024-05-31 20:17:40 +08:00
@login_required
2024-06-14 16:47:43 +08:00
@custom_permission_required('pjt_mgnt.add_projectledger')
2024-06-06 15:08:38 +08:00
def proj_ledger_list_add(request):
if request.method == 'POST':
2024-06-07 03:47:15 +08:00
form = ProjectLedgerForm(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 = ProjectLedgerForm()
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)
2024-06-06 15:08:38 +08:00
@login_required
2024-06-14 16:47:43 +08:00
@custom_permission_required('pjt_mgnt.change_projectledger')
2024-06-06 15:08:38 +08:00
def proj_ledger_list_modify(request):
if request.method == 'POST':
2024-06-07 03:47:15 +08:00
if 'project_id' in request.POST:
instance = ProjectLedger.objects.get(project_id=request.POST['project_id'])
form = ProjectLedgerForm(request.POST, instance=instance)
else:
form = ProjectLedgerForm(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 'project_id' in request.GET:
try:
instance = ProjectLedger.objects.get(project_id=request.GET['project_id'])
form = ProjectLedgerForm(instance=instance)
except ProjectLedger.DoesNotExist:
raise Http404("对象不存在")
else:
form = ProjectLedgerForm()
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)
2024-06-06 15:08:38 +08:00
@login_required
2024-06-14 16:47:43 +08:00
@custom_permission_required('pjt_mgnt.delete_projectledger')
2024-06-06 15:08:38 +08:00
def proj_ledger_list_delete(request):
if request.method == 'GET':
project_id = request.GET.get('project_id')
ProjectLedger.objects.filter(project_id=project_id).delete()
return JsonResponse({"message": "删除成功"})
return JsonResponse({"message": "无效的请求方法"}, status=405)
@login_required
2024-06-14 16:47:43 +08:00
@custom_permission_required('pjt_mgnt.view_employeeprojectincome')
2024-05-31 20:17:40 +08:00
def emp_proj_income_list_view(request):
# 声明查询集
query_set = EmployeeProjectIncomeSettlement.objects.filter().order_by('-record_id')
# 获取查询参数
project_name = request.GET.get('project_name', '')
year_month = request.GET.get('year_month', '')
# 根据提供的参数进行筛选
2024-06-06 15:08:38 +08:00
if project_name:
query_set = query_set.filter(project_name__project_name__icontains=project_name)
if year_month:
query_set = query_set.filter(year_month__icontains=year_month)
2024-05-31 20:17:40 +08:00
# 对查询结果进行分页每页10条记录
items = paginate_query_and_assign_numbers(
request=request,
queryset=query_set,
per_page=10
)
# 构建上下文查询参数字符串
2024-06-06 15:08:38 +08:00
query_params = '&project_name={}&year_month={}'.format(project_name, year_month)
2024-06-06 20:56:06 +08:00
# Excel上传模板
2024-06-09 17:14:10 +08:00
template_name = "项目管理-项目组员收入结算-Excel上传模板.xlsx"
2024-05-31 20:17:40 +08:00
2024-06-06 20:56:06 +08:00
# 构建上下文
2024-05-31 20:17:40 +08:00
context = {
2024-06-06 20:56:06 +08:00
"model_config": 'pjt_mgnt.EmployeeProjectIncomeSettlement',
"items": items,
"breadcrumb_list": [
{"title": "首页", "name": "index"},
{"title": "项目管理", "name": "index"},
{"title": "项目组员收入结算表", "name": "emp_proj_income_list"}
],
"filters": [
{"type": "text", "id": "project_name", "name": "project_name", "label": "项目名称",
"placeholder": "请输入项目名称"},
{"type": "text", "id": "year_month", "name": "year_month", "label": "年月", "placeholder": "请输入年月"}
],
"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": {
"record_id": {"type": "text", "width": "180px"},
"project_name": {"type": "text", "width": "180px"},
"year_month": {"type": "text", "width": "180px"},
"actions": {"type": "actions", "width": "100px"}
}
},
"query_params": query_params,
"form_action_url": 'emp_proj_income_list',
"modify_url": reverse("emp_proj_income_list_modify"),
"add_url": reverse("emp_proj_income_list_add"),
"delete_url": reverse("emp_proj_income_list_delete"),
2024-06-07 03:47:15 +08:00
"table_exclude_field_name": ['record_id'], # Exclude primary key
"add_button": True,
"import_excel_button": True
2024-05-31 20:17:40 +08:00
}
2024-06-07 03:47:15 +08:00
return render(request, 'items_list.html', context)
2024-06-06 15:08:38 +08:00
@login_required
2024-06-14 16:47:43 +08:00
@custom_permission_required('pjt_mgnt.add_employeeprojectincome')
2024-06-06 15:08:38 +08:00
def emp_proj_income_list_add(request):
if request.method == 'POST':
2024-06-07 03:47:15 +08:00
form = EmployeeProjectIncomeSettlementForm(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 = EmployeeProjectIncomeSettlementForm()
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)
2024-06-06 15:08:38 +08:00
@login_required
2024-06-14 16:47:43 +08:00
@custom_permission_required('pjt_mgnt.change_employeeprojectincome')
2024-06-06 15:08:38 +08:00
def emp_proj_income_list_modify(request):
if request.method == 'POST':
2024-06-07 03:47:15 +08:00
if 'record_id' in request.POST:
instance = EmployeeProjectIncomeSettlement.objects.get(record_id=request.POST['record_id'])
form = EmployeeProjectIncomeSettlementForm(request.POST, instance=instance)
else:
form = EmployeeProjectIncomeSettlementForm(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 'record_id' in request.GET:
try:
instance = EmployeeProjectIncomeSettlement.objects.get(record_id=request.GET['record_id'])
form = EmployeeProjectIncomeSettlementForm(instance=instance)
except EmployeeProjectIncomeSettlement.DoesNotExist:
raise Http404("对象不存在")
else:
form = EmployeeProjectIncomeSettlementForm()
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)
2024-06-06 15:08:38 +08:00
@login_required
2024-06-14 16:47:43 +08:00
@custom_permission_required('pjt_mgnt.delete_employeeprojectincome')
2024-06-06 15:08:38 +08:00
def emp_proj_income_list_delete(request):
if request.method == 'GET':
record_id = request.GET.get('record_id')
EmployeeProjectIncomeSettlement.objects.filter(record_id=record_id).delete()
return JsonResponse({"message": "删除成功"})
return JsonResponse({"message": "无效的请求方法"}, status=405)