Browse Source

代理商结算金额逻辑更改 + 调整代码

linhaohong 1 year ago
parent
commit
a6ce4b594e
1 changed files with 48 additions and 19 deletions
  1. 48 19
      AdminController/CloudServiceManage/AgentCustomerController.py

+ 48 - 19
AdminController/CloudServiceManage/AgentCustomerController.py

@@ -18,7 +18,7 @@ from django.core.paginator import Paginator
 from datetime import datetime, timedelta
 
 from AgentModel.models import AgentCustomerInfo, AgentCustomerCard, AgentCustomerPackage, AgentCloudServicePackage, \
-    AgentDeviceOrder, AgentAccountWithdraw, AgentDevice, AgentAccount, ApplyAgent
+    AgentDeviceOrder, AgentAccountWithdraw, AgentDevice, AgentAccount, ApplyAgent, AgentDeviceOrderInstallment
 from Model.models import UnicomCombo, Store_Meal, Device_User
 
 from Object.ResponseObject import ResponseObject
@@ -94,7 +94,7 @@ class AgentCustomerView(View):
                 return self.get_agent_settle_order(userID, request_dict, response)
             elif operation == 'getDataStatistics':
                 return self.get_data_statistics(userID, response)
-            
+
             # 代理商提现功能
             elif operation == 'getAgentAccountWithdraw':
                 return self.get_agent_account_withdraw(userID, request_dict, response)
@@ -180,8 +180,6 @@ class AgentCustomerView(View):
         except Exception as e:
             return response.json({'error': str(e)}, status=500)
 
-
-
     def get_agent_service_package(self, request_dict, response):
         """
         查询所有代理云服务套餐
@@ -336,8 +334,8 @@ class AgentCustomerView(View):
             return response.json(444, 'Missing package_id')
         try:
             ac_service_package = AgentCloudServicePackage.objects.get(pk=id)
-            #假删除,否则查利润会出问题
-            ac_service_package.status=0
+            # 假删除,否则查利润会出问题
+            ac_service_package.status = 0
             ac_service_package.save()
             return response.json(0)
 
@@ -530,6 +528,10 @@ class AgentCustomerView(View):
 
         page = int(request_dict.get('page', 1))  # 默认为第一页
         page_size = int(request_dict.get('page_size', 10))  # 默认每页10条记录
+
+        if status is None:
+            return response.json(444)
+
         try:
             agent_customer_info = AgentCustomerInfo.objects.filter(user_id=userID).first()
             if agent_customer_info is None:
@@ -550,9 +552,6 @@ class AgentCustomerView(View):
                 end_time = int(end_date.timestamp()) - 1
                 agent_device_orders_qs = agent_device_orders_qs.filter(created_time__gte=start_time,
                                                                        created_time__lte=end_time)
-            if status:
-                agent_device_orders_qs = agent_device_orders_qs.filter(status=status)
-
             if startTime < endTime:
                 agent_device_orders_qs = agent_device_orders_qs.filter(created_time__gte=startTime,
                                                                        created_time__lte=endTime)
@@ -561,8 +560,26 @@ 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)
 
-            # 计算利润总额和订单总数
-            total_profit = agent_device_orders_qs.aggregate(Sum('profit'))['profit__sum'] or 0
+            # 计算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
+            else:
+                total_profit = full_profit
 
             # 应用分页
             agent_device_orders_qs = agent_device_orders_qs.order_by('-created_time')
@@ -669,12 +686,12 @@ class AgentCustomerView(View):
 
         try:
             # 计算冻结金额
-            frozen_amount_qs = AgentAccountWithdraw.objects.filter(ac_id=ac_id, status__in=[1,2,3])
+            frozen_amount_qs = AgentAccountWithdraw.objects.filter(ac_id=ac_id, status__in=[1, 2, 3])
             frozen_amount = frozen_amount_qs.aggregate(total=Sum('amount'))['total'] or Decimal('0.00')
 
             # 计算余额:已结算 - (退款+已打款)
             incomes_qs = AgentAccount.objects.filter(ac_id=ac_id, status=1)
-            expense_qs = AgentAccount.objects.filter(ac_id=ac_id, status__in=[2,3])
+            expense_qs = AgentAccount.objects.filter(ac_id=ac_id, status__in=[2, 3])
             incomes_all = incomes_qs.aggregate(total=Sum('amount'))['total'] or Decimal('0.00')
             expense_all = expense_qs.aggregate(total=Sum('amount'))['total'] or Decimal('0.00')
             total_profit = incomes_all - expense_all
@@ -718,6 +735,16 @@ class AgentCustomerView(View):
         tomorrow_start = today_start + timedelta(days=1)
 
         try:
+            # 计算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
+
+            print(partial_settled_profit)
             # 总利润
             total_profit_all = \
                 agent_device_orders_qs.filter(status__in=[1, 2], is_del=False).aggregate(
@@ -726,9 +753,12 @@ class AgentCustomerView(View):
             total_profit_no = \
                 agent_device_orders_qs.filter(status=1, is_del=False).aggregate(total=Sum('profit'))[
                     'total'] or Decimal('0.00')
+            total_profit_no = total_profit_no - partial_settled_profit
+
             total_profit_yes = \
                 agent_device_orders_qs.filter(status=2, is_del=False).aggregate(total=Sum('profit'))[
                     'total'] or Decimal('0.00')
+            total_profit_yes = total_profit_yes + partial_settled_profit
 
             # 总营业额
             profit_amount_all = agent_device_orders_qs.filter(status__in=[1, 2], is_del=False).aggregate(
@@ -809,7 +839,7 @@ class AgentCustomerView(View):
             total_profit = incomes_all - expense_all
 
             # 冻结余额
-            frozen_amount_qs = AgentAccountWithdraw.objects.filter(ac_id=ac_id, status__in=[1,2,3])
+            frozen_amount_qs = AgentAccountWithdraw.objects.filter(ac_id=ac_id, status__in=[1, 2, 3])
             frozen_amount = frozen_amount_qs.aggregate(total=Sum('amount'))['total'] or Decimal('0.00')
 
             # 可提现余额 = 总余额 - 冻结金额
@@ -828,7 +858,6 @@ class AgentCustomerView(View):
             print(e)
             return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
-
     def get_withdrawal_review(self, request_dict, response):
         """
         后台用户提现申请审核列表
@@ -907,9 +936,9 @@ class AgentCustomerView(View):
                 AgentAccount.objects.create(ac_id=agent_account_withdraw.ac_id,
                                             amount=agent_account_withdraw.amount,
                                             status=3,
-                                            created_time = int(time.time()),
-                                            updated_time = int(time.time()),
-                                            remark = f"{userID}修改为已提现")
+                                            created_time=int(time.time()),
+                                            updated_time=int(time.time()),
+                                            remark=f"{userID}修改为已提现")
             return response.json(0)
 
         except Exception as e:
@@ -941,4 +970,4 @@ class AgentCustomerView(View):
 
         ApplyAgent.objects.create(name=name, phone=clean_phone, regin=regin, remark=remark)
 
-        return response.json(0, '申请已提交')
+        return response.json(0, '申请已提交')