|
|
@ -1,15 +1,18 @@
|
|
|
|
from django.db import transaction
|
|
|
|
from django.db import transaction
|
|
|
|
from django.db.models import Q
|
|
|
|
from django.db.models import Q, F, Sum
|
|
|
|
from drf_yasg import openapi
|
|
|
|
from drf_yasg import openapi
|
|
|
|
from drf_yasg.utils import swagger_auto_schema
|
|
|
|
from drf_yasg.utils import swagger_auto_schema
|
|
|
|
from rest_framework import viewsets
|
|
|
|
from rest_framework import viewsets, status
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
|
|
|
|
|
|
|
from ERP_5.utils.base_views import MultipleDestroyMixin, MultipleAuditMixin
|
|
|
|
from ERP_5.utils.base_views import MultipleDestroyMixin, MultipleAuditMixin
|
|
|
|
from ERP_5.utils.paginations import GlobalPagination
|
|
|
|
from ERP_5.utils.paginations import GlobalPagination
|
|
|
|
|
|
|
|
from basic_info.models import SupplierModel, SettlementAccountModel
|
|
|
|
from erp_system.models import UserModel
|
|
|
|
from erp_system.models import UserModel
|
|
|
|
from financial_info.models import PaymentModel
|
|
|
|
from financial_info.models import PaymentModel, PaymentItemModel
|
|
|
|
from financial_info.serializer.payment_serializer import PaymentSerializer
|
|
|
|
from financial_info.serializer.payment_serializer import PaymentSerializer
|
|
|
|
|
|
|
|
from warehouse_info.models import PurchaseStorageModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PaymentViewSet(viewsets.ModelViewSet, MultipleDestroyMixin, MultipleAuditMixin):
|
|
|
|
class PaymentViewSet(viewsets.ModelViewSet, MultipleDestroyMixin, MultipleAuditMixin):
|
|
|
@ -118,7 +121,6 @@ class PaymentViewSet(viewsets.ModelViewSet, MultipleDestroyMixin, MultipleAuditM
|
|
|
|
'ids': openapi.Schema(type=openapi.TYPE_ARRAY, items=openapi.Schema(type=openapi.TYPE_INTEGER),
|
|
|
|
'ids': openapi.Schema(type=openapi.TYPE_ARRAY, items=openapi.Schema(type=openapi.TYPE_INTEGER),
|
|
|
|
description="选择哪些需要批量的ID(主键)列表"),
|
|
|
|
description="选择哪些需要批量的ID(主键)列表"),
|
|
|
|
'user_id': openapi.Schema(type=openapi.TYPE_INTEGER, description="审核人的用户ID"),
|
|
|
|
'user_id': openapi.Schema(type=openapi.TYPE_INTEGER, description="审核人的用户ID"),
|
|
|
|
'is_audit': openapi.Schema(type=openapi.TYPE_STRING, description="是否审核,审核:1")
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@swagger_auto_schema(method='put', request_body=body_param, operation_description="批量审核")
|
|
|
|
@swagger_auto_schema(method='put', request_body=body_param, operation_description="批量审核")
|
|
|
@ -131,5 +133,64 @@ class PaymentViewSet(viewsets.ModelViewSet, MultipleDestroyMixin, MultipleAuditM
|
|
|
|
# 为了减少代码量,参数的验证就不写了
|
|
|
|
# 为了减少代码量,参数的验证就不写了
|
|
|
|
queryset = self.get_queryset().filter(id__in=audit_ids).all()
|
|
|
|
queryset = self.get_queryset().filter(id__in=audit_ids).all()
|
|
|
|
check_user = UserModel.objects.get(id=int(user_id))
|
|
|
|
check_user = UserModel.objects.get(id=int(user_id))
|
|
|
|
for item in queryset:
|
|
|
|
for pm in queryset: # 期中pm是付款单
|
|
|
|
pass
|
|
|
|
# 判断1:关联的采购单状态是否为“未审核”
|
|
|
|
|
|
|
|
if pm.purchase and pm.purchase.status == '0':
|
|
|
|
|
|
|
|
return Response(data={'detail': '采购订单未审核,不能付款'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
# 判断2 付款单的状态是否为:“已审核”
|
|
|
|
|
|
|
|
if pm.status == '1':
|
|
|
|
|
|
|
|
return Response(data={'detail': '付款单已经审核,不需要再次审核'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
# 判断3 结算账户的余额是否够用
|
|
|
|
|
|
|
|
if pm.this_money > pm.account.balance:
|
|
|
|
|
|
|
|
return Response(data={'detail': '选择的结算账户中的余额不够,不能付款'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 处理关联业务逻辑
|
|
|
|
|
|
|
|
# 业务一: 采购定金付款
|
|
|
|
|
|
|
|
if pm.pay_category == '1' and pm.purchase:
|
|
|
|
|
|
|
|
pm.purchase.status = '5' # 已付定金
|
|
|
|
|
|
|
|
pm.purchase.save()
|
|
|
|
|
|
|
|
# 业务二: 供应商欠款 支付
|
|
|
|
|
|
|
|
if pm.pay_category == '3':
|
|
|
|
|
|
|
|
SupplierModel.objects.filter(id=pm.supplier.id).update(current_pay=F('current_pay') - pm.pay_money)
|
|
|
|
|
|
|
|
# 业务三: 入库货款 支付
|
|
|
|
|
|
|
|
if pm.pay_category == '2' and pm.purchase:
|
|
|
|
|
|
|
|
if pm.item_list.exists(): # 这种判断不回从数据库中返回数据
|
|
|
|
|
|
|
|
for item in pm.item_list.all(): # 期中item是付款项目
|
|
|
|
|
|
|
|
# 判断4: 入库单必须是已经审核状态
|
|
|
|
|
|
|
|
if item.purchase_storage and item.purchase_storage.status != '1':
|
|
|
|
|
|
|
|
return Response(data={'detail': '入库单未审核或者已经付款,不能付款'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
# 业务三——第1种情况 对应的入库单,是否应该:付款完成 ,首先需要查询该入库单的已经付款金额之和
|
|
|
|
|
|
|
|
sum_dict = PaymentItemModel.objects\
|
|
|
|
|
|
|
|
.filter(purchase_storage_id=item.purchase_storage.id, payment__status='1')\
|
|
|
|
|
|
|
|
.aggregate(sum=Sum('this_money'))
|
|
|
|
|
|
|
|
pay_sum = sum_dict['sum'] if sum_dict['sum'] else 0
|
|
|
|
|
|
|
|
if item.should_money == (pay_sum + item.this_money):
|
|
|
|
|
|
|
|
item.purchase_storage.status = '2' # 把入库单的状态修改为:付款完成
|
|
|
|
|
|
|
|
item.purchase_storage.save()
|
|
|
|
|
|
|
|
# 业务三——第2种情况 对应的采购单状态是否要改为:采购完成
|
|
|
|
|
|
|
|
# 采购完成的条件: 1、该采购单必须是:全部入库状态;2、该采购单中 所有的入库单状态也都必须是:付款完成
|
|
|
|
|
|
|
|
purchase = item.payment.purchase
|
|
|
|
|
|
|
|
if purchase.status == '3':
|
|
|
|
|
|
|
|
# 查询 该采购单中 所有入库单状态不是:'付款完成' 的有多少个?
|
|
|
|
|
|
|
|
number = PurchaseStorageModel.objects\
|
|
|
|
|
|
|
|
.filter(purchase_id=purchase.id)\
|
|
|
|
|
|
|
|
.exclude(status='2').count()
|
|
|
|
|
|
|
|
if number == 0: # 则 所有的入库单状态都为:付款完成
|
|
|
|
|
|
|
|
purchase.status = '4' # 修改采购单状态为: 采购完成
|
|
|
|
|
|
|
|
purchase.save()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return Response(data={'detail': '没有选择付款项目,支付货款的时候必须选择'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
# 至此: 三种业务都完成
|
|
|
|
|
|
|
|
# 业务四: 修改 结算账户的余额
|
|
|
|
|
|
|
|
SettlementAccountModel.objects.filter(id=pm.account.id)\
|
|
|
|
|
|
|
|
.update(balance=F('balance') - pm.this_money)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 至此: 四种业务都完成,开始修改付款单的状态以及审核信息
|
|
|
|
|
|
|
|
# 审核
|
|
|
|
|
|
|
|
self.get_queryset().filter(id__in=audit_ids) \
|
|
|
|
|
|
|
|
.update(
|
|
|
|
|
|
|
|
status='1',
|
|
|
|
|
|
|
|
check_user_name=check_user.real_name,
|
|
|
|
|
|
|
|
check_user_id=check_user.id)
|
|
|
|
|
|
|
|
return Response(status=status.HTTP_200_OK)
|