增加预算表的显示功能

This commit is contained in:
2025-12-03 17:17:17 +08:00
parent ddad32b481
commit acf4ef12ec
16 changed files with 1058 additions and 398 deletions

View File

@@ -1,5 +1,5 @@
from rest_framework import viewsets
from .models import Branch, Activity, Evaluation, Event, VideoTerminal
from .models import Branch, Activity, Evaluation, Event, VideoTerminal, Budget, EquipmentBudget, InfrastructureBudget, BudgetTemplate, TemplateEquipmentItem, TemplateInfrastructureItem
from .serializers import BranchSerializer, ActivitySerializer, EvaluationSerializer
from django.shortcuts import render, redirect
from .models import PublicScreen
@@ -55,6 +55,12 @@ def branch_detail(request, branch_id):
equipment_images = branch.equipment_images.all()
public_screens = branch.public_screens.all()
# 获取预算数据
budgets = Budget.objects.filter(branch=branch).select_related('activity').prefetch_related('equipment_budgets', 'infrastructure_budgets').order_by('-created_at')
# 获取预算模板
budget_templates = BudgetTemplate.objects.all()
# 准备上下文数据
context = {
'branch': branch,
@@ -62,10 +68,62 @@ def branch_detail(request, branch_id):
'events': events,
'equipment_images': equipment_images,
'public_screens': public_screens,
'budgets': budgets,
'activities': activities,
'budget_templates': budget_templates,
}
return render(request, 'branch_detail.html', context)
def import_budget_template(request, branch_id):
"""从模板导入预算"""
if request.method == 'POST':
template_id = request.POST.get('template')
budget_name = request.POST.get('budget_name', '导入预算')
if template_id:
try:
# 获取模板和分支机构
template = BudgetTemplate.objects.get(pk=template_id)
branch = Branch.objects.get(pk=branch_id)
# 创建预算
budget = Budget.objects.create(
branch=branch,
name=budget_name
)
# 导入设备预算项
for equipment_item in template.equipment_items.all():
EquipmentBudget.objects.create(
budget=budget,
project=equipment_item.project,
model=equipment_item.model,
unit_price=equipment_item.unit_price,
procurement_method=equipment_item.procurement_method,
quantity=1 # 默认数量为1
)
# 导入基础设施预算项
for infrastructure_item in template.infrastructure_items.all():
InfrastructureBudget.objects.create(
budget=budget,
name=infrastructure_item.name,
remarks=infrastructure_item.remarks,
unit_price=infrastructure_item.unit_price,
unit=infrastructure_item.unit,
description=infrastructure_item.description,
quantity=1 # 默认数量为1
)
# 更新总预算
budget.update_total_budget()
except Exception as e:
print(f"导入预算模板失败: {e}")
return redirect('branch-detail', branch_id=branch_id)
# 在页面上显示所有的branch以及active的数量首页显示
def BranchAll(request):
branches = Branch.objects.exclude(activity__isnull=True).order_by('name')
@@ -442,3 +500,100 @@ def video_terminal_list(request):
'selected_type_name': selected_type_name,
}
return render(request, 'video_terminals.html', context)
# 预算相关视图
def create_budget(request, branch_id):
branch = Branch.objects.get(pk=branch_id)
activities = Activity.objects.filter(branch=branch)
if request.method == 'POST':
activity_id = request.POST.get('activity')
if activity_id:
activity = Activity.objects.get(pk=activity_id)
budget = Budget.objects.create(branch=branch, activity=activity)
return redirect('budget_detail', branch_id=branch_id, budget_id=budget.id)
context = {
'branch': branch,
'activities': activities,
}
return render(request, 'create_budget.html', context)
def budget_detail(request, branch_id, budget_id):
branch = Branch.objects.get(pk=branch_id)
budget = Budget.objects.get(pk=budget_id, branch=branch)
context = {
'branch': branch,
'budget': budget,
}
return render(request, 'budget_detail.html', context)
def add_equipment_budget(request, branch_id, budget_id):
branch = Branch.objects.get(pk=branch_id)
budget = Budget.objects.get(pk=budget_id, branch=branch)
if request.method == 'POST':
project = request.POST.get('project')
model = request.POST.get('model')
unit_price = request.POST.get('unit_price')
procurement_method = request.POST.get('procurement_method')
quantity = request.POST.get('quantity', 1)
if project and model and unit_price and procurement_method:
EquipmentBudget.objects.create(
budget=budget,
project=project,
model=model,
unit_price=unit_price,
procurement_method=procurement_method,
quantity=quantity
)
return redirect('budget_detail', branch_id=branch_id, budget_id=budget_id)
def add_infrastructure_budget(request, branch_id, budget_id):
branch = Branch.objects.get(pk=branch_id)
budget = Budget.objects.get(pk=budget_id, branch=branch)
if request.method == 'POST':
name = request.POST.get('name')
remarks = request.POST.get('remarks')
unit_price = request.POST.get('unit_price')
unit = request.POST.get('unit')
description = request.POST.get('description')
quantity = request.POST.get('quantity', 1)
if name and unit_price and unit:
InfrastructureBudget.objects.create(
budget=budget,
name=name,
remarks=remarks,
unit_price=unit_price,
unit=unit,
description=description,
quantity=quantity
)
return redirect('budget_detail', branch_id=branch_id, budget_id=budget_id)
def delete_equipment_budget(request, branch_id, budget_id, equipment_budget_id):
branch = Branch.objects.get(pk=branch_id)
budget = Budget.objects.get(pk=budget_id, branch=branch)
equipment_budget = EquipmentBudget.objects.get(pk=equipment_budget_id, budget=budget)
equipment_budget.delete()
return redirect('budget_detail', branch_id=branch_id, budget_id=budget_id)
def delete_infrastructure_budget(request, branch_id, budget_id, infrastructure_budget_id):
branch = Branch.objects.get(pk=branch_id)
budget = Budget.objects.get(pk=budget_id, branch=branch)
infrastructure_budget = InfrastructureBudget.objects.get(pk=infrastructure_budget_id, budget=budget)
infrastructure_budget.delete()
return redirect('budget_detail', branch_id=branch_id, budget_id=budget_id)