Jelajahi Sumber

优化计算结算代码

linhaohong 1 tahun lalu
induk
melakukan
7642584652
1 mengubah file dengan 22 tambahan dan 18 penghapusan
  1. 22 18
      AdminController/CloudServiceManage/AgentCustomerController.py

+ 22 - 18
AdminController/CloudServiceManage/AgentCustomerController.py

@@ -557,27 +557,31 @@ class AgentCustomerView(View):
                     AgentCloudServicePackage.objects.filter(type=int(package_type)).values_list('id', flat=True))
                 agent_device_orders_qs = agent_device_orders_qs.filter(csp_id__in=csp_ids)
 
-            # 计算AgentDeviceOrderInstallment表 结算金额
-            partial_settled_profit = 0
-            for agent_device_order in agent_device_orders_qs:
-                ado_id = agent_device_order.id
-                settled_profit = \
-                    AgentDeviceOrderInstallment.objects.filter(ado_id=ado_id, status=2).aggregate(Sum('amount'))[
-                        'amount__sum'] or 0
-                partial_settled_profit = partial_settled_profit + settled_profit
-
-            if status:
-                agent_device_orders_qs = agent_device_orders_qs.filter(status=status)
-
-            # 计算利润总额
-            full_profit = agent_device_orders_qs.aggregate(Sum('profit'))['profit__sum'] or 0
-            if status == '1':
-                total_profit = full_profit - partial_settled_profit
-            elif status == '2':
-                total_profit = full_profit + partial_settled_profit
+            if status is None:
+                total_profit = agent_device_orders_qs.aggregate(Sum('profit'))['profit__sum'] or 0
             else:
+                # 计算特定状态的device_orders总额
+                full_profit = agent_device_orders_qs.filter(status=status).aggregate(Sum('profit'))[
+                                  'profit__sum'] or 0
+
+                # 初始化total_profit
                 total_profit = full_profit
 
+                # 对于状态1和2,计算部分结算的利润
+                if status in ["1", "2"]:
+                    partial_settled_profit = AgentDeviceOrderInstallment.objects.filter(
+                        ado_id__in=agent_device_orders_qs.filter(status=1).values_list('id', flat=True),
+                        status=2
+                    ).aggregate(total=Sum('amount'))['total'] or 0
+
+                    # 根据状态调整total_profit
+                    if status == "1":
+                        total_profit -= partial_settled_profit
+                    else:  # 当status为"2"时,添加partial_settled_profit(根据业务逻辑,这可能需要调整)
+                        total_profit += partial_settled_profit
+
+                agent_device_orders_qs = agent_device_orders_qs.filter(status=status)
+
             # 应用分页
             agent_device_orders_qs = agent_device_orders_qs.order_by('-created_time')
             paginator = Paginator(agent_device_orders_qs, page_size)