from rest_framework import status, views, serializers, pagination from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from api_v1.views.stock_change_views.mixins import StockChangeViewMixin from business import models as business_models from business import pre_order_services class PrePurchaseOrderItemSerializer(serializers.ModelSerializer): class Meta: model = business_models.PrePurchaseOrderItem fields = [ 'id', 'product_id', 'product_name', 'color', 'quantity', 'unit', 'spec', 'quantity_of_rolls', 'num_of_rolls', 'order_quantity', 'remarks', 'created_at', 'updated_at', ] read_only_fields = ['id', 'created_at', 'updated_at'] class PrePurchaseOrderSerializer(serializers.ModelSerializer): human_id = serializers.CharField(read_only=True) merchant_name = serializers.CharField(source='merchant.name', read_only=True) supplier_name = serializers.CharField(source='supplier.name', read_only=True) warehouse_name = serializers.CharField(source='warehouse.name', read_only=True) created_by_username = serializers.CharField(source='created_by.username', read_only=True) operator_name = serializers.CharField(source='operator.name', read_only=True) items = PrePurchaseOrderItemSerializer(many=True, read_only=True) class Meta: model = business_models.PrePurchaseOrder fields = [ 'id', 'human_id', 'merchant', 'merchant_name', 'supplier', 'supplier_name', 'warehouse', 'warehouse_name', 'created_by', 'created_by_username', 'operator', 'operator_name', 'kind', 'remarks', 'created_at', 'updated_at', 'items', ] read_only_fields = [ 'id', 'human_id', 'created_at', 'updated_at', 'merchant_name', 'supplier_name', 'warehouse_name', 'created_by_username', 'operator_name', 'items', ] class PrePurchaseOrderPagination(pagination.LimitOffsetPagination): default_limit = 20 max_limit = 100 class PrePurchaseOrderView(StockChangeViewMixin, views.APIView): """预采购单查询与创建""" permission_classes = [IsAuthenticated] pagination_class = PrePurchaseOrderPagination def get(self, request): if not self.check_employee_permission(request): return self.permission_error_response('无权限访问') merchant = request.user.employee.merchant queryset = pre_order_services.list_pre_purchase_orders(merchant=merchant) paginator = self.pagination_class() page = paginator.paginate_queryset(queryset.order_by('-created_at'), request, view=self) serializer = PrePurchaseOrderSerializer(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 employee = request.user.employee data = request.data or {} try: pre_purchase_order = pre_order_services.create_pre_purchase_order( merchant=merchant, supplier_id=data.get('supplier') or data.get('supplier_id'), warehouse_id=data.get('warehouse') or data.get('warehouse_id'), operator=employee, created_by=request.user, kind=data.get('kind'), items=data.get('items'), remarks=data.get('remarks', ''), ) except ValueError as exc: return Response({'error': str(exc)}, status=status.HTTP_400_BAD_REQUEST) return Response(PrePurchaseOrderSerializer(pre_purchase_order).data, status=status.HTTP_201_CREATED) class PrePurchaseOrderDetailView(StockChangeViewMixin, views.APIView): permission_classes = [IsAuthenticated] def _get_order(self, request, pk: int): if not self.check_employee_permission(request): return None, self.permission_error_response('无权限访问') merchant = request.user.employee.merchant try: order = pre_order_services.get_pre_purchase_order( merchant=merchant, pre_purchase_order_id=pk, ) return order, None except ValueError: return None, self.not_found_response('预采购单不存在') def get(self, request, pk: int): order, error_response = self._get_order(request, pk) if error_response: return error_response return Response(PrePurchaseOrderSerializer(order).data, status=status.HTTP_200_OK) def put(self, request, pk: int): return self._update(request, pk) def patch(self, request, pk: int): return self._update(request, pk) def _update(self, request, pk: int): order, error_response = self._get_order(request, pk) if error_response: return error_response data = request.data or {} try: updated = pre_order_services.update_pre_purchase_order( pre_purchase_order=order, supplier_id=data.get('supplier') or data.get('supplier_id'), warehouse_id=data.get('warehouse') or data.get('warehouse_id'), kind=data.get('kind'), items=data.get('items'), remarks=data.get('remarks'), ) except ValueError as exc: return Response({'error': str(exc)}, status=status.HTTP_400_BAD_REQUEST) return Response(PrePurchaseOrderSerializer(updated).data, status=status.HTTP_200_OK) def delete(self, request, pk: int): order, error_response = self._get_order(request, pk) if error_response: return error_response pre_order_services.delete_pre_purchase_order(pre_purchase_order=order) return Response(status=status.HTTP_204_NO_CONTENT)