forked from erp-dev/erp
feat: payment order and receipt order
This commit is contained in:
@@ -104,7 +104,41 @@
|
||||
|
||||
---
|
||||
|
||||
## 4. 错误示例
|
||||
## 4. 付款单(PaymentOrder)
|
||||
|
||||
### 4.1 列表
|
||||
- **GET** `/api/v1/payment-orders/`
|
||||
- 返回字段:`supplier_name`、`payment_date`、`amount`、`status` 等。
|
||||
|
||||
### 4.2 创建
|
||||
- **POST** `/api/v1/payment-orders/`
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| `supplier` | int | 供应商 ID |
|
||||
| `payment_date` | str (`YYYY-MM-DD`) | 付款日期 |
|
||||
| `amount` | decimal | 付款金额(必须 > 0) |
|
||||
| `remarks` | str | 可选 |
|
||||
|
||||
### 4.3 审批 / 作废
|
||||
- **POST** `/api/v1/payment-orders/<id>/review/`
|
||||
- `{"action": "approve"}` 或 `{"action": "cancel"}`
|
||||
|
||||
---
|
||||
|
||||
## 5. 收款单(ReceiptOrder)
|
||||
|
||||
接口与付款单类似,只是主体为 `customer`:
|
||||
|
||||
- **GET** `/api/v1/receipt-orders/`
|
||||
- **POST** `/api/v1/receipt-orders/`:需要 `customer`、`receipt_date`、`amount`
|
||||
- **POST** `/api/v1/receipt-orders/<id>/review/`
|
||||
|
||||
审批通过表示“确认收款”,作废则恢复为初始状态。
|
||||
|
||||
---
|
||||
|
||||
## 6. 错误示例
|
||||
|
||||
| 场景 | HTTP | 返回体 |
|
||||
|------|------|--------|
|
||||
|
||||
133
api_v1/business/payment/views.py
Normal file
133
api_v1/business/payment/views.py
Normal file
@@ -0,0 +1,133 @@
|
||||
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 api_v1.views.stock_change_views.mixins import StockChangeViewMixin
|
||||
|
||||
|
||||
class PaymentOrderSerializer(serializers.ModelSerializer):
|
||||
supplier_name = serializers.CharField(source='supplier.name', read_only=True)
|
||||
operator_name = serializers.CharField(source='operator.name', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = business_models.PaymentOrder
|
||||
fields = [
|
||||
'id', 'supplier', 'supplier_name', 'payment_date',
|
||||
'amount', 'operator', 'operator_name', 'status',
|
||||
'remarks', 'created_at', 'updated_at',
|
||||
]
|
||||
read_only_fields = ['id', 'supplier_name', 'operator_name', 'status', 'created_at', 'updated_at']
|
||||
|
||||
|
||||
class PaymentOrderPagination(pagination.LimitOffsetPagination):
|
||||
default_limit = 20
|
||||
max_limit = 100
|
||||
|
||||
|
||||
class PaymentOrderView(StockChangeViewMixin, views.APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
pagination_class = PaymentOrderPagination
|
||||
|
||||
def get(self, request):
|
||||
if not self.check_employee_permission(request):
|
||||
return self.permission_error_response('无权限访问')
|
||||
merchant = request.user.employee.merchant
|
||||
queryset = business_models.PaymentOrder.objects.filter(merchant=merchant).select_related(
|
||||
'supplier', 'operator'
|
||||
).order_by('-created_at')
|
||||
paginator = self.pagination_class()
|
||||
page = paginator.paginate_queryset(queryset, request, view=self)
|
||||
serializer = PaymentOrderSerializer(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')
|
||||
payment_date = data.get('payment_date')
|
||||
amount = data.get('amount')
|
||||
remarks = data.get('remarks', '')
|
||||
|
||||
if not supplier_id:
|
||||
return Response({'error': '缺少供应商 ID'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if not payment_date:
|
||||
return Response({'error': '缺少 payment_date'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if amount is None:
|
||||
return Response({'error': '缺少 amount'}, 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)
|
||||
|
||||
operator = request.user.employee
|
||||
|
||||
try:
|
||||
payment_order = business_services.create_payment_order(
|
||||
merchant=merchant,
|
||||
supplier=supplier,
|
||||
payment_date=payment_date,
|
||||
amount=amount,
|
||||
operator=operator,
|
||||
remarks=remarks,
|
||||
)
|
||||
except ValueError as exc:
|
||||
return Response({'error': str(exc)}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
return Response(
|
||||
{
|
||||
'id': payment_order.id,
|
||||
'status': payment_order.status,
|
||||
'message': '付款单创建成功,等待审批',
|
||||
},
|
||||
status=status.HTTP_201_CREATED,
|
||||
)
|
||||
|
||||
|
||||
class PaymentOrderReviewSerializer(serializers.Serializer):
|
||||
action = serializers.ChoiceField(choices=[('approve', '审批通过'), ('cancel', '作废')])
|
||||
|
||||
|
||||
class PaymentOrderReviewView(StockChangeViewMixin, views.APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
ACTION_STATUS_MAP = {
|
||||
'approve': business_models.PaymentOrderStatusEnum.APPROVED,
|
||||
'cancel': business_models.PaymentOrderStatusEnum.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:
|
||||
payment_order = business_models.PaymentOrder.objects.select_related(
|
||||
'supplier', 'operator'
|
||||
).get(id=pk, merchant=merchant)
|
||||
except business_models.PaymentOrder.DoesNotExist:
|
||||
return self.not_found_response('付款单不存在')
|
||||
|
||||
serializer = PaymentOrderReviewSerializer(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_payment_order(
|
||||
payment_order=payment_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 = business_models.PaymentOrder.objects.select_related(
|
||||
'supplier', 'operator'
|
||||
).get(id=payment_order.id)
|
||||
return Response(PaymentOrderSerializer(refreshed).data, status=status.HTTP_200_OK)
|
||||
|
||||
133
api_v1/business/receipt/views.py
Normal file
133
api_v1/business/receipt/views.py
Normal file
@@ -0,0 +1,133 @@
|
||||
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 api_v1.views.stock_change_views.mixins import StockChangeViewMixin
|
||||
|
||||
|
||||
class ReceiptOrderSerializer(serializers.ModelSerializer):
|
||||
customer_name = serializers.CharField(source='customer.name', read_only=True)
|
||||
operator_name = serializers.CharField(source='operator.name', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = business_models.ReceiptOrder
|
||||
fields = [
|
||||
'id', 'customer', 'customer_name', 'receipt_date',
|
||||
'amount', 'operator', 'operator_name', 'status',
|
||||
'remarks', 'created_at', 'updated_at',
|
||||
]
|
||||
read_only_fields = ['id', 'customer_name', 'operator_name', 'status', 'created_at', 'updated_at']
|
||||
|
||||
|
||||
class ReceiptOrderPagination(pagination.LimitOffsetPagination):
|
||||
default_limit = 20
|
||||
max_limit = 100
|
||||
|
||||
|
||||
class ReceiptOrderView(StockChangeViewMixin, views.APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
pagination_class = ReceiptOrderPagination
|
||||
|
||||
def get(self, request):
|
||||
if not self.check_employee_permission(request):
|
||||
return self.permission_error_response('无权限访问')
|
||||
merchant = request.user.employee.merchant
|
||||
queryset = business_models.ReceiptOrder.objects.filter(merchant=merchant).select_related(
|
||||
'customer', 'operator'
|
||||
).order_by('-created_at')
|
||||
paginator = self.pagination_class()
|
||||
page = paginator.paginate_queryset(queryset, request, view=self)
|
||||
serializer = ReceiptOrderSerializer(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 {}
|
||||
customer_id = data.get('customer')
|
||||
receipt_date = data.get('receipt_date')
|
||||
amount = data.get('amount')
|
||||
remarks = data.get('remarks', '')
|
||||
|
||||
if not customer_id:
|
||||
return Response({'error': '缺少客户 ID'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if not receipt_date:
|
||||
return Response({'error': '缺少 receipt_date'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if amount is None:
|
||||
return Response({'error': '缺少 amount'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
try:
|
||||
customer = basic_models.Customer.objects.get(id=customer_id, merchant=merchant)
|
||||
except basic_models.Customer.DoesNotExist:
|
||||
return Response({'error': f'客户 {customer_id} 不存在'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
operator = request.user.employee
|
||||
|
||||
try:
|
||||
receipt_order = business_services.create_receipt_order(
|
||||
merchant=merchant,
|
||||
customer=customer,
|
||||
receipt_date=receipt_date,
|
||||
amount=amount,
|
||||
operator=operator,
|
||||
remarks=remarks,
|
||||
)
|
||||
except ValueError as exc:
|
||||
return Response({'error': str(exc)}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
return Response(
|
||||
{
|
||||
'id': receipt_order.id,
|
||||
'status': receipt_order.status,
|
||||
'message': '收款单创建成功,等待审批',
|
||||
},
|
||||
status=status.HTTP_201_CREATED,
|
||||
)
|
||||
|
||||
|
||||
class ReceiptOrderReviewSerializer(serializers.Serializer):
|
||||
action = serializers.ChoiceField(choices=[('approve', '审批通过'), ('cancel', '作废')])
|
||||
|
||||
|
||||
class ReceiptOrderReviewView(StockChangeViewMixin, views.APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
ACTION_STATUS_MAP = {
|
||||
'approve': business_models.ReceiptOrderStatusEnum.APPROVED,
|
||||
'cancel': business_models.ReceiptOrderStatusEnum.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:
|
||||
receipt_order = business_models.ReceiptOrder.objects.select_related(
|
||||
'customer', 'operator'
|
||||
).get(id=pk, merchant=merchant)
|
||||
except business_models.ReceiptOrder.DoesNotExist:
|
||||
return self.not_found_response('收款单不存在')
|
||||
|
||||
serializer = ReceiptOrderReviewSerializer(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_receipt_order(
|
||||
receipt_order=receipt_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 = business_models.ReceiptOrder.objects.select_related(
|
||||
'customer', 'operator'
|
||||
).get(id=receipt_order.id)
|
||||
return Response(ReceiptOrderSerializer(refreshed).data, status=status.HTTP_200_OK)
|
||||
|
||||
Reference in New Issue
Block a user