forked from erp-dev/erp
feat: sales_order api
This commit is contained in:
@@ -1,171 +0,0 @@
|
||||
from rest_framework import status, views, serializers, pagination
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from basic_info import models as basic_models
|
||||
from business import services as business_services
|
||||
from business import models as business_models
|
||||
from .stock_change_views.mixins import StockChangeViewMixin
|
||||
|
||||
|
||||
class PurchaseOrderItemSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = business_models.PurchaseOrderItem
|
||||
fields = [
|
||||
'id', 'product', 'price', 'color', 'quantity', 'unit',
|
||||
'empty_diff_percent', 'quantity_of_rolls', 'num_of_rolls',
|
||||
'batch_number', 'remarks', 'created_at', 'updated_at', 'spec',
|
||||
]
|
||||
read_only_fields = ['id', 'created_at', 'updated_at', 'total_amount', 'diff_quantity', 'real_quantity']
|
||||
|
||||
|
||||
class PurchaseOrderSerializer(serializers.ModelSerializer):
|
||||
total_amount = serializers.SerializerMethodField(read_only=True)
|
||||
diff_quantity = serializers.SerializerMethodField(read_only=True)
|
||||
total_quantity = serializers.SerializerMethodField(read_only=True)
|
||||
|
||||
def get_total_amount(self, obj: business_models.PurchaseOrder):
|
||||
return obj.get_total_amount()
|
||||
def get_diff_quantity(self, obj: business_models.PurchaseOrder):
|
||||
return obj.get_total_diff_quantity()
|
||||
def get_total_quantity(self, obj: business_models.PurchaseOrder):
|
||||
return obj.get_total_quantity()
|
||||
|
||||
supplier_name = serializers.CharField(source='supplier.name', read_only=True)
|
||||
operator_name = serializers.CharField(source='operator.name', read_only=True)
|
||||
warehouse_name = serializers.CharField(source='warehouse.name', read_only=True)
|
||||
items = PurchaseOrderItemSerializer(many=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = business_models.PurchaseOrder
|
||||
fields = [
|
||||
'id', 'supplier', 'supplier_name', 'purchase_date', 'kind',
|
||||
'total_amount', 'diff_quantity', 'total_quantity',
|
||||
'operator', 'operator_name', 'warehouse', 'warehouse_name',
|
||||
'status', 'remarks', 'created_at', 'updated_at', 'items',
|
||||
]
|
||||
read_only_fields = ['id', 'created_at', 'updated_at', 'items', 'supplier_name', 'operator_name', 'warehouse_name']
|
||||
|
||||
|
||||
class PurchaseOrderPagination(pagination.LimitOffsetPagination):
|
||||
default_limit = 20
|
||||
max_limit = 100
|
||||
|
||||
|
||||
class PurchaseOrderView(StockChangeViewMixin, views.APIView):
|
||||
"""采购订单查询与创建"""
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
pagination_class = PurchaseOrderPagination
|
||||
|
||||
def get(self, request):
|
||||
if not self.check_employee_permission(request):
|
||||
return self.permission_error_response('无权限访问')
|
||||
|
||||
merchant = request.user.employee.merchant
|
||||
queryset = business_models.PurchaseOrder.objects.filter(merchant=merchant).prefetch_related('items', 'supplier', 'operator', 'warehouse')
|
||||
paginator = self.pagination_class()
|
||||
page = paginator.paginate_queryset(queryset.order_by('-created_at'), request, view=self)
|
||||
serializer = PurchaseOrderSerializer(page, many=True)
|
||||
return paginator.get_paginated_response(serializer.data)
|
||||
|
||||
def post(self, request):
|
||||
if not self.check_employee_permission(request):
|
||||
return self.permission_error_response('无权限访问')
|
||||
|
||||
merchant = request.user.employee.merchant
|
||||
data = request.data or {}
|
||||
|
||||
supplier_id = data.get('supplier')
|
||||
warehouse_id = data.get('warehouse_id') or data.get('warehouse')
|
||||
order_date = data.get('order_date')
|
||||
items = data.get('items', [])
|
||||
remarks = data.get('remarks', '')
|
||||
|
||||
if not supplier_id:
|
||||
return Response({'error': '缺少供应商 ID'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if not warehouse_id:
|
||||
return Response({'error': '缺少仓库 ID'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if not order_date:
|
||||
return Response({'error': '缺少 order_date'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if not isinstance(items, list) or not items:
|
||||
return Response({'error': 'items 需要为非空数组'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
try:
|
||||
supplier = basic_models.Supplier.objects.get(id=supplier_id, merchant=merchant)
|
||||
except basic_models.Supplier.DoesNotExist:
|
||||
return Response({'error': f'供应商 {supplier_id} 不存在'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
try:
|
||||
warehouse = basic_models.WareHouse.objects.get(id=warehouse_id, merchant=merchant)
|
||||
except basic_models.WareHouse.DoesNotExist:
|
||||
return Response({'error': f'仓库 {warehouse_id} 不存在'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
operator = request.user.employee
|
||||
|
||||
try:
|
||||
purchase_order = business_services.create_purchase_order(
|
||||
merchant=merchant,
|
||||
supplier=supplier,
|
||||
order_date=order_date,
|
||||
warehouse=warehouse,
|
||||
operator=operator,
|
||||
items=items,
|
||||
remarks=remarks,
|
||||
created_by=request.user,
|
||||
)
|
||||
except ValueError as exc:
|
||||
return Response({'error': str(exc)}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
return Response(
|
||||
{
|
||||
'id': purchase_order.id,
|
||||
'status': purchase_order.status,
|
||||
'message': '采购单创建成功,等待审批',
|
||||
},
|
||||
status=status.HTTP_201_CREATED,
|
||||
)
|
||||
|
||||
|
||||
class PurchaseOrderReviewSerializer(serializers.Serializer):
|
||||
action = serializers.ChoiceField(choices=[('approve', '审批通过'), ('cancel', '作废')])
|
||||
|
||||
|
||||
class PurchaseOrderReviewView(StockChangeViewMixin, views.APIView):
|
||||
"""采购单审批 / 作废"""
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
ACTION_STATUS_MAP = {
|
||||
'approve': business_models.PurchaseOrderStatusEnum.APPROVED,
|
||||
'cancel': business_models.PurchaseOrderStatusEnum.CANCELLED,
|
||||
}
|
||||
|
||||
def post(self, request, pk: int):
|
||||
if not self.check_employee_permission(request):
|
||||
return self.permission_error_response('无权限访问')
|
||||
|
||||
merchant = request.user.employee.merchant
|
||||
try:
|
||||
purchase_order = business_models.PurchaseOrder.objects.select_related(
|
||||
'supplier', 'operator', 'warehouse'
|
||||
).prefetch_related('items').get(id=pk, merchant=merchant)
|
||||
except business_models.PurchaseOrder.DoesNotExist:
|
||||
return self.not_found_response('采购单不存在')
|
||||
|
||||
serializer = PurchaseOrderReviewSerializer(data=request.data or {})
|
||||
serializer.is_valid(raise_exception=True)
|
||||
action = serializer.validated_data['action']
|
||||
target_status = self.ACTION_STATUS_MAP[action]
|
||||
|
||||
try:
|
||||
business_services.review_purchase_order(
|
||||
purchase_order=purchase_order,
|
||||
target_status=target_status,
|
||||
reviewed_by=request.user,
|
||||
)
|
||||
except ValueError as exc:
|
||||
return Response({'error': str(exc)}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
refreshed_order = business_models.PurchaseOrder.objects.select_related(
|
||||
'supplier', 'operator', 'warehouse'
|
||||
).prefetch_related('items').get(id=purchase_order.id)
|
||||
return Response(PurchaseOrderSerializer(refreshed_order).data, status=status.HTTP_200_OK)
|
||||
Reference in New Issue
Block a user