forked from erp-dev/erp
feat: printing module
This commit is contained in:
@@ -2,17 +2,19 @@ from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from .views import stock_change_views, user_info, inventory, product_image, stateflow
|
||||
from .views.stock_change_views.snapshot import StockSnapshotListView
|
||||
from printing.views import PrintingOrderViewSet, PrintingJobViewSet
|
||||
from .views.printing.views import PrintingOrderViewSet, PrintingJobViewSet, PlateOrderViewSet
|
||||
|
||||
# 创建 DRF Router for Stateflow
|
||||
stateflow_router = DefaultRouter()
|
||||
stateflow_router.register(r'states', stateflow.StateViewSet, basename='state')
|
||||
stateflow_router.register(r'processes', stateflow.ProcessViewSet, basename='process')
|
||||
stateflow_router.register(r'business-objects', stateflow.BusinessObjectViewSet, basename='business-object')
|
||||
|
||||
# 创建主 Router
|
||||
main_router = DefaultRouter()
|
||||
main_router.register(r'printing-orders', PrintingOrderViewSet, basename='printing-order')
|
||||
main_router.register(r'printing-jobs', PrintingJobViewSet, basename='printing-job')
|
||||
main_router.register(r'plate-orders', PlateOrderViewSet, basename='plate-order')
|
||||
|
||||
urlpatterns = [
|
||||
# 库存变动相关API
|
||||
|
||||
6
api_v1/views/printing/__init__.py
Normal file
6
api_v1/views/printing/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
"""
|
||||
Printing module API views
|
||||
"""
|
||||
from .views import PrintingOrderViewSet, PrintingJobViewSet
|
||||
|
||||
__all__ = ['PrintingOrderViewSet', 'PrintingJobViewSet']
|
||||
@@ -0,0 +1,349 @@
|
||||
"""
|
||||
Printing API 序列化器
|
||||
"""
|
||||
from rest_framework import serializers
|
||||
from printing import models
|
||||
from .services import PrintingOrderService, PrintingJobService
|
||||
from basic_info.models import Customer, Employee
|
||||
|
||||
|
||||
class PrintingOrderListSerializer(serializers.ModelSerializer):
|
||||
"""印染订单列表序列化器"""
|
||||
customer_name = serializers.CharField(source='customer.name', read_only=True)
|
||||
customer_phone = serializers.CharField(source='customer.mobile', read_only=True)
|
||||
process_name = serializers.CharField(source='process.name', read_only=True)
|
||||
progress = serializers.IntegerField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingOrder
|
||||
fields = [
|
||||
'id', 'human_id', 'customer', 'customer_name', 'customer_phone',
|
||||
'fabric', 'width', 'is_urgent', 'area', 'address',
|
||||
'is_fabric_received', 'outgoing_date', 'is_invalid',
|
||||
'process', 'process_name', 'progress',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = ['id', 'human_id', 'created_at', 'updated_at', 'progress']
|
||||
|
||||
|
||||
class PrintingOrderDetailSerializer(serializers.ModelSerializer):
|
||||
"""印染订单详情序列化器"""
|
||||
customer_name = serializers.CharField(source='customer.name', read_only=True)
|
||||
customer_phone = serializers.CharField(source='customer.mobile', read_only=True)
|
||||
customer_area = serializers.CharField(source='customer.area', read_only=True)
|
||||
process_name = serializers.CharField(source='process.name', read_only=True)
|
||||
progress = serializers.IntegerField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingOrder
|
||||
fields = [
|
||||
'id', 'human_id', 'customer', 'customer_name', 'customer_phone', 'customer_area',
|
||||
'fabric', 'width', 'is_urgent', 'area', 'address', 'fabric_source',
|
||||
'is_fabric_received', 'craft', 'description', 'outgoing_date',
|
||||
'curve', 'new_curve', 'position',
|
||||
'printing_warn', 'rolling_warn', 'production_warn',
|
||||
'is_invalid', 'process', 'process_name', 'progress',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = ['id', 'human_id', 'created_at', 'updated_at', 'progress']
|
||||
|
||||
|
||||
class PrintingOrderCreateUpdateSerializer(serializers.ModelSerializer):
|
||||
"""印染订单创建/更新序列化器"""
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingOrder
|
||||
fields = [
|
||||
'id', 'customer', 'fabric', 'width', 'is_urgent', 'area', 'address',
|
||||
'fabric_source', 'is_fabric_received', 'craft', 'description',
|
||||
'outgoing_date', 'curve', 'new_curve', 'position',
|
||||
'printing_warn', 'rolling_warn', 'production_warn', 'is_invalid',
|
||||
'process'
|
||||
]
|
||||
read_only_fields = ['id']
|
||||
|
||||
def validate_customer(self, value):
|
||||
"""验证客户是否存在"""
|
||||
if not value:
|
||||
raise serializers.ValidationError("客户不能为空")
|
||||
return value
|
||||
|
||||
def validate(self, attrs):
|
||||
"""验证流程修改权限"""
|
||||
# 更新时验证 process 修改权限
|
||||
if self.instance and 'process' in attrs:
|
||||
new_process = attrs['process']
|
||||
old_process = self.instance.process
|
||||
|
||||
if new_process != old_process:
|
||||
if not PrintingOrderService.can_change_process(self.instance):
|
||||
raise serializers.ValidationError({
|
||||
'process': '存在已开始的印染任务,无法修改流程'
|
||||
})
|
||||
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
"""创建订单,使用 service 层"""
|
||||
user = self.context['request'].user
|
||||
return PrintingOrderService.create_printing_order(validated_data, user)
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
"""更新订单,使用 service 层"""
|
||||
user = self.context['request'].user
|
||||
success, message, updated_instance = PrintingOrderService.update_printing_order(
|
||||
instance, validated_data, user
|
||||
)
|
||||
if not success:
|
||||
raise serializers.ValidationError(message)
|
||||
return updated_instance
|
||||
|
||||
|
||||
class PrintingJobListSerializer(serializers.ModelSerializer):
|
||||
"""印染款式明细列表序列化器"""
|
||||
printing_order_id = serializers.CharField(source='printing_order.human_id', read_only=True)
|
||||
product_name = serializers.CharField(source='product.name', read_only=True)
|
||||
status = serializers.CharField(read_only=True)
|
||||
is_completed = serializers.BooleanField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = [
|
||||
'id', 'printing_order', 'printing_order_id', 'product', 'product_name',
|
||||
'quantity', 'unit', 'size', 'pieces', 'description',
|
||||
'status', 'is_completed',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = ['id', 'created_at', 'updated_at', 'status', 'is_completed']
|
||||
|
||||
|
||||
class PrintingJobDetailSerializer(serializers.ModelSerializer):
|
||||
"""印染款式明细详情序列化器"""
|
||||
printing_order_id = serializers.CharField(source='printing_order.human_id', read_only=True)
|
||||
product_name = serializers.CharField(source='product.name', read_only=True)
|
||||
product_code = serializers.CharField(source='product.human_id', read_only=True)
|
||||
status = serializers.CharField(read_only=True)
|
||||
status_id = serializers.IntegerField(read_only=True)
|
||||
is_completed = serializers.BooleanField(read_only=True)
|
||||
has_started = serializers.BooleanField(read_only=True)
|
||||
business_object_id = serializers.IntegerField(source='business_object.id', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = [
|
||||
'id', 'printing_order', 'printing_order_id', 'product', 'product_name', 'product_code',
|
||||
'quantity', 'unit', 'size', 'pieces', 'description',
|
||||
'status', 'status_id', 'is_completed', 'has_started', 'business_object_id',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = [
|
||||
'id', 'created_at', 'updated_at',
|
||||
'status', 'status_id', 'is_completed', 'has_started', 'business_object_id'
|
||||
]
|
||||
|
||||
|
||||
class PrintingJobCreateUpdateSerializer(serializers.ModelSerializer):
|
||||
"""印染款式明细创建/更新序列化器"""
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = [
|
||||
'id', 'printing_order', 'product', 'quantity', 'unit', 'size', 'pieces', 'description'
|
||||
]
|
||||
read_only_fields = ['id']
|
||||
extra_kwargs = {
|
||||
'size': {'required': False, 'allow_null': True, 'allow_blank': True},
|
||||
'pieces': {'required': False, 'allow_null': True},
|
||||
'description': {'required': False, 'allow_null': True, 'allow_blank': True},
|
||||
}
|
||||
|
||||
def validate_printing_order(self, value):
|
||||
"""验证印染订单是否存在"""
|
||||
if not value:
|
||||
raise serializers.ValidationError("印染订单不能为空")
|
||||
return value
|
||||
|
||||
def validate_product(self, value):
|
||||
"""验证产品是否存在"""
|
||||
if not value:
|
||||
raise serializers.ValidationError("产品不能为空")
|
||||
return value
|
||||
|
||||
def validate_quantity(self, value):
|
||||
"""验证数量"""
|
||||
if value <= 0:
|
||||
raise serializers.ValidationError("数量必须大于0")
|
||||
return value
|
||||
|
||||
def validate_pieces(self, value):
|
||||
"""验证件数(仅在提供值时验证)"""
|
||||
if value is not None and value <= 0:
|
||||
raise serializers.ValidationError("件数必须大于0")
|
||||
return value
|
||||
|
||||
def create(self, validated_data):
|
||||
"""创建任务,使用 service 层"""
|
||||
user = self.context['request'].user
|
||||
return PrintingJobService.create_printing_job(validated_data, user)
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
"""更新任务,使用 service 层"""
|
||||
user = self.context['request'].user
|
||||
success, message, updated_instance = PrintingJobService.update_printing_job(
|
||||
instance, validated_data, user
|
||||
)
|
||||
if not success:
|
||||
raise serializers.ValidationError(message)
|
||||
return updated_instance
|
||||
|
||||
|
||||
|
||||
class PlateOrderListSerializer(serializers.ModelSerializer):
|
||||
"""开版订单列表序列化器"""
|
||||
customer_name = serializers.CharField(source="customer.name", read_only=True)
|
||||
salesperson_name = serializers.CharField(source="salesperson.name", read_only=True)
|
||||
merchandiser_name = serializers.CharField(source="merchandiser.name", read_only=True)
|
||||
status = serializers.CharField(read_only=True)
|
||||
progress_percentage = serializers.IntegerField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.PlateOrder
|
||||
fields = [
|
||||
'id', 'design_code', 'plate_type', 'plate_date',
|
||||
'urgency_level', 'is_invalid',
|
||||
'customer', 'customer_name', 'area',
|
||||
'salesperson', 'salesperson_name',
|
||||
'merchandiser', 'merchandiser_name',
|
||||
'style_name', 'fabric', 'width',
|
||||
'required_completion_date', 'completion_date',
|
||||
'is_ordered', 'status', 'progress_percentage',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = [
|
||||
'id', 'status', 'progress_percentage',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
|
||||
|
||||
class PlateOrderDetailSerializer(serializers.ModelSerializer):
|
||||
"""开版订单详情序列化器"""
|
||||
customer_name = serializers.CharField(source="customer.name", read_only=True)
|
||||
customer_phone = serializers.CharField(source="customer.mobile", read_only=True)
|
||||
salesperson_name = serializers.CharField(source="salesperson.name", read_only=True)
|
||||
merchandiser_name = serializers.CharField(source="merchandiser.name", read_only=True)
|
||||
status = serializers.CharField(read_only=True)
|
||||
status_id = serializers.IntegerField(read_only=True)
|
||||
is_completed = serializers.BooleanField(read_only=True)
|
||||
has_started = serializers.BooleanField(read_only=True)
|
||||
progress_percentage = serializers.IntegerField(read_only=True)
|
||||
business_object_id = serializers.IntegerField(source="business_object.id", read_only=True)
|
||||
plate_image_url = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = models.PlateOrder
|
||||
fields = [
|
||||
'id', 'design_code', 'plate_type', 'plate_date', 'plate_method',
|
||||
'plate_image', 'plate_image_url', 'plate_notes', 'reprint_reason',
|
||||
'urgency_level', 'is_invalid',
|
||||
'customer', 'customer_name', 'customer_phone', 'area', 'default_address',
|
||||
'salesperson', 'salesperson_name',
|
||||
'merchandiser', 'merchandiser_name',
|
||||
'style_name', 'fabric', 'width', 'production_method',
|
||||
'is_mark_frame', 'drawing_rating', 'color_matching_rating',
|
||||
'sample_rating', 'difficulty_rating',
|
||||
'sample_meter', 'required_sample_meters',
|
||||
'required_completion_date', 'completion_date',
|
||||
'approval_result', 'is_ordered', 'customer_feedback',
|
||||
'status', 'status_id', 'is_completed', 'has_started',
|
||||
'progress_percentage', 'business_object_id',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = [
|
||||
'id', 'status', 'status_id', 'is_completed', 'has_started',
|
||||
'progress_percentage', 'business_object_id',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
|
||||
def get_plate_image_url(self, obj):
|
||||
"""获取图片完整URL"""
|
||||
if obj.plate_image:
|
||||
request = self.context.get("request")
|
||||
if request:
|
||||
return request.build_absolute_uri(obj.plate_image.url)
|
||||
return obj.plate_image.url
|
||||
return None
|
||||
|
||||
|
||||
class PlateOrderCreateUpdateSerializer(serializers.ModelSerializer):
|
||||
"""开版订单创建/更新序列化器"""
|
||||
|
||||
class Meta:
|
||||
model = models.PlateOrder
|
||||
fields = [
|
||||
"id", "design_code", "plate_type", "plate_date", "plate_method",
|
||||
"plate_image", "plate_notes", "reprint_reason",
|
||||
"urgency_level", "is_invalid",
|
||||
"customer", "area", "default_address",
|
||||
"salesperson", "merchandiser",
|
||||
"style_name", "fabric", "width", "production_method",
|
||||
"is_mark_frame", "drawing_rating", "color_matching_rating",
|
||||
"sample_rating", "difficulty_rating",
|
||||
"sample_meter", "required_sample_meters",
|
||||
"required_completion_date", "completion_date",
|
||||
"approval_result", "is_ordered", "customer_feedback"
|
||||
]
|
||||
read_only_fields = ["id"]
|
||||
|
||||
def validate_customer(self, value):
|
||||
"""验证客户是否存在"""
|
||||
if not value:
|
||||
raise serializers.ValidationError("客户不能为空")
|
||||
return value
|
||||
|
||||
def validate_salesperson(self, value):
|
||||
"""验证业务员是否存在"""
|
||||
if value and not Employee.objects.filter(id=value.id).exists():
|
||||
raise serializers.ValidationError("业务员不存在")
|
||||
return value
|
||||
|
||||
def validate_merchandiser(self, value):
|
||||
"""验证跟单员是否存在"""
|
||||
if value and not Employee.objects.filter(id=value.id).exists():
|
||||
raise serializers.ValidationError("跟单员不存在")
|
||||
return value
|
||||
|
||||
def validate_sample_meter(self, value):
|
||||
"""验证样品米数"""
|
||||
if value is not None and value < 0:
|
||||
raise serializers.ValidationError("样品米数不能为负数")
|
||||
return value
|
||||
|
||||
def validate_required_sample_meters(self, value):
|
||||
"""验证所需样品米数"""
|
||||
if value is not None and value < 0:
|
||||
raise serializers.ValidationError("所需样品米数不能为负数")
|
||||
return value
|
||||
|
||||
def validate(self, attrs):
|
||||
"""交叉验证"""
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
"""创建开版订单"""
|
||||
user = self.context["request"].user
|
||||
|
||||
# 创建 PlateOrder
|
||||
plate_order = models.PlateOrder.objects.create(**validated_data)
|
||||
|
||||
# 暂时不自动创建 BusinessObject,由 admin 或其他地方创建
|
||||
|
||||
return plate_order
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
"""更新开版订单"""
|
||||
# 直接更新字段
|
||||
for attr, value in validated_data.items():
|
||||
setattr(instance, attr, value)
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
|
||||
182
api_v1/views/printing/services.py
Normal file
182
api_v1/views/printing/services.py
Normal file
@@ -0,0 +1,182 @@
|
||||
"""
|
||||
Printing module business logic services
|
||||
"""
|
||||
from typing import Dict, Any, Tuple
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
from printing import models as printing_models
|
||||
from stateflow import models as stateflow_models
|
||||
from stateflow import services as stateflow_services
|
||||
|
||||
|
||||
class PrintingOrderService:
|
||||
"""印染订单业务逻辑服务"""
|
||||
|
||||
@staticmethod
|
||||
def get_default_process():
|
||||
"""获取默认流程"""
|
||||
process_id = getattr(settings, 'PRINTING_DEFAULT_PROCESS_ID', None)
|
||||
if not process_id:
|
||||
return None
|
||||
|
||||
try:
|
||||
return stateflow_models.Process.objects.get(id=process_id)
|
||||
except stateflow_models.Process.DoesNotExist:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def create_printing_order(data: Dict[str, Any], user) -> printing_models.PrintingOrder:
|
||||
"""
|
||||
创建印染订单
|
||||
|
||||
Args:
|
||||
data: 订单数据
|
||||
user: 当前用户
|
||||
|
||||
Returns:
|
||||
创建的订单实例
|
||||
"""
|
||||
# 如果未指定 process,使用默认值
|
||||
if 'process' not in data or data['process'] is None:
|
||||
default_process = PrintingOrderService.get_default_process()
|
||||
if default_process:
|
||||
data['process'] = default_process
|
||||
|
||||
order = printing_models.PrintingOrder.objects.create(**data)
|
||||
return order
|
||||
|
||||
@staticmethod
|
||||
def update_printing_order(
|
||||
printing_order: printing_models.PrintingOrder,
|
||||
data: Dict[str, Any],
|
||||
user
|
||||
) -> Tuple[bool, str, printing_models.PrintingOrder]:
|
||||
"""
|
||||
更新印染订单
|
||||
|
||||
Args:
|
||||
printing_order: 订单实例
|
||||
data: 更新数据
|
||||
user: 当前用户
|
||||
|
||||
Returns:
|
||||
(success, message, updated_order)
|
||||
"""
|
||||
# 如果要修改 process,需要验证
|
||||
if 'process' in data and data['process'] != printing_order.process:
|
||||
if not printing_order.can_change_process():
|
||||
return False, '存在已开始的印染任务,无法修改流程', printing_order
|
||||
|
||||
# 更新字段
|
||||
for field, value in data.items():
|
||||
setattr(printing_order, field, value)
|
||||
|
||||
printing_order.save()
|
||||
return True, '更新成功', printing_order
|
||||
|
||||
@staticmethod
|
||||
def can_change_process(printing_order: printing_models.PrintingOrder) -> bool:
|
||||
"""
|
||||
检查订单是否可以修改流程
|
||||
|
||||
Args:
|
||||
printing_order: 订单实例
|
||||
|
||||
Returns:
|
||||
是否可以修改
|
||||
"""
|
||||
return printing_order.can_change_process()
|
||||
|
||||
@staticmethod
|
||||
def get_order_progress(printing_order: printing_models.PrintingOrder) -> int:
|
||||
"""
|
||||
计算订单完成进度
|
||||
|
||||
Args:
|
||||
printing_order: 订单实例
|
||||
|
||||
Returns:
|
||||
完成百分比 (0-100)
|
||||
"""
|
||||
return printing_order.progress
|
||||
|
||||
|
||||
class PrintingJobService:
|
||||
"""印染任务业务逻辑服务"""
|
||||
|
||||
@staticmethod
|
||||
@transaction.atomic
|
||||
def create_printing_job(data: Dict[str, Any], user) -> printing_models.PrintingJob:
|
||||
"""
|
||||
创建印染任务
|
||||
|
||||
自动创建关联的 BusinessObject 实例
|
||||
|
||||
Args:
|
||||
data: 任务数据
|
||||
user: 当前用户
|
||||
|
||||
Returns:
|
||||
创建的任务实例
|
||||
"""
|
||||
printing_order = data.get('printing_order')
|
||||
|
||||
# 创建 PrintingJob
|
||||
job = printing_models.PrintingJob.objects.create(**data)
|
||||
|
||||
# 如果 PrintingOrder 有关联的流程,创建 BusinessObject
|
||||
if printing_order and printing_order.process:
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f"PrintingJob-{job.id}",
|
||||
process=printing_order.process,
|
||||
description=f"印染任务 {job.id} 的流程实例",
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
return job
|
||||
|
||||
@staticmethod
|
||||
def update_printing_job(
|
||||
printing_job: printing_models.PrintingJob,
|
||||
data: Dict[str, Any],
|
||||
user
|
||||
) -> Tuple[bool, str, printing_models.PrintingJob]:
|
||||
"""
|
||||
更新印染任务
|
||||
|
||||
Args:
|
||||
printing_job: 任务实例
|
||||
data: 更新数据
|
||||
user: 当前用户
|
||||
|
||||
Returns:
|
||||
(success, message, updated_job)
|
||||
"""
|
||||
# 更新字段
|
||||
for field, value in data.items():
|
||||
# 不允许直接修改 business_object
|
||||
if field == 'business_object':
|
||||
continue
|
||||
setattr(printing_job, field, value)
|
||||
|
||||
printing_job.save()
|
||||
return True, '更新成功', printing_job
|
||||
|
||||
@staticmethod
|
||||
def get_job_status(printing_job: printing_models.PrintingJob) -> Dict[str, Any]:
|
||||
"""
|
||||
获取任务状态信息
|
||||
|
||||
Args:
|
||||
printing_job: 任务实例
|
||||
|
||||
Returns:
|
||||
状态信息字典
|
||||
"""
|
||||
return {
|
||||
'status': printing_job.status,
|
||||
'status_id': printing_job.status_id,
|
||||
'is_completed': printing_job.is_completed,
|
||||
'has_started': printing_job.has_started,
|
||||
}
|
||||
@@ -2,12 +2,14 @@
|
||||
PrintingOrder API 测试
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.conf import settings
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework import status
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from basic_info import models as basic_models
|
||||
from printing import models as printing_models
|
||||
from stateflow import models as stateflow_models
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
@@ -49,6 +51,17 @@ class PrintingOrderAPITestCase(TestCase):
|
||||
area='测试地区'
|
||||
)
|
||||
|
||||
# 创建流程
|
||||
self.state1 = stateflow_models.State.objects.create(name='待印染')
|
||||
self.state2 = stateflow_models.State.objects.create(name='印染中')
|
||||
self.state3 = stateflow_models.State.objects.create(name='已完成')
|
||||
|
||||
self.process = stateflow_models.Process.objects.create(name='印染流程')
|
||||
self.process.replace_nodes([self.state1, self.state2, self.state3])
|
||||
|
||||
# 设置默认流程
|
||||
settings.PRINTING_DEFAULT_PROCESS_ID = self.process.id
|
||||
|
||||
# 认证用户
|
||||
self.client.force_authenticate(user=self.user)
|
||||
|
||||
@@ -398,3 +411,132 @@ class PrintingOrderAPITestCase(TestCase):
|
||||
self.assertTrue(len(order.human_id) >= 14)
|
||||
today = date.today().strftime('%Y%m%d')
|
||||
self.assertTrue(order.human_id.startswith(today))
|
||||
|
||||
def test_create_order_with_default_process(self):
|
||||
"""测试创建订单使用默认流程"""
|
||||
data = {
|
||||
'customer': self.customer.id,
|
||||
'fabric': '测试布料',
|
||||
'width': '150cm',
|
||||
}
|
||||
|
||||
response = self.client.post('/api/v1/printing-orders/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
# 验证使用了默认流程
|
||||
order = printing_models.PrintingOrder.objects.get(id=response.data['id'])
|
||||
self.assertEqual(order.process.id, self.process.id)
|
||||
|
||||
def test_create_order_with_custom_process(self):
|
||||
"""测试创建订单指定自定义流程"""
|
||||
custom_process = stateflow_models.Process.objects.create(name='自定义流程')
|
||||
|
||||
data = {
|
||||
'customer': self.customer.id,
|
||||
'fabric': '测试布料',
|
||||
'width': '150cm',
|
||||
'process': custom_process.id,
|
||||
}
|
||||
|
||||
response = self.client.post('/api/v1/printing-orders/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
order = printing_models.PrintingOrder.objects.get(id=response.data['id'])
|
||||
self.assertEqual(order.process.id, custom_process.id)
|
||||
|
||||
def test_order_progress_in_list(self):
|
||||
"""测试订单列表包含进度字段"""
|
||||
order = printing_models.PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='测试布料',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
response = self.client.get('/api/v1/printing-orders/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('progress', response.data[0])
|
||||
self.assertEqual(response.data[0]['progress'], 0) # 没有任务时为0
|
||||
|
||||
def test_update_process_when_no_jobs(self):
|
||||
"""测试没有任务时可以修改流程"""
|
||||
order = printing_models.PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='测试布料',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
new_process = stateflow_models.Process.objects.create(name='新流程')
|
||||
|
||||
data = {
|
||||
'process': new_process.id,
|
||||
}
|
||||
|
||||
response = self.client.patch(
|
||||
f'/api/v1/printing-orders/{order.id}/',
|
||||
data,
|
||||
format='json'
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
order.refresh_from_db()
|
||||
self.assertEqual(order.process.id, new_process.id)
|
||||
|
||||
def test_cannot_update_process_when_job_started(self):
|
||||
"""测试有已开始的任务时不能修改流程"""
|
||||
order = printing_models.PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='测试布料',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
# 创建产品类别和产品
|
||||
category = basic_models.ProductCategory.objects.create(
|
||||
name='测试类别',
|
||||
merchant=self.merchant,
|
||||
)
|
||||
product = basic_models.Product.objects.create(
|
||||
name='测试产品',
|
||||
category=category,
|
||||
merchant=self.merchant,
|
||||
)
|
||||
|
||||
# 创建任务
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=order,
|
||||
product=product,
|
||||
quantity=10,
|
||||
unit='件',
|
||||
size='100x200',
|
||||
pieces=5,
|
||||
)
|
||||
|
||||
# 创建 BusinessObject 并推进状态
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
from stateflow.services import advance_to_next_state
|
||||
advance_to_next_state(business_object, self.user)
|
||||
|
||||
# 尝试修改流程
|
||||
new_process = stateflow_models.Process.objects.create(name='新流程')
|
||||
|
||||
data = {
|
||||
'process': new_process.id,
|
||||
}
|
||||
|
||||
response = self.client.patch(
|
||||
f'/api/v1/printing-orders/{order.id}/',
|
||||
data,
|
||||
format='json'
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('已开始', str(response.data))
|
||||
|
||||
|
||||
562
api_v1/views/printing/test_plate_order_api.py
Normal file
562
api_v1/views/printing/test_plate_order_api.py
Normal file
@@ -0,0 +1,562 @@
|
||||
"""
|
||||
PlateOrder API 测试
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework import status
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from basic_info import models as basic_models
|
||||
from printing import models as printing_models
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class PlateOrderAPITestCase(TestCase):
|
||||
"""测试 PlateOrder API"""
|
||||
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
|
||||
# 创建商户
|
||||
self.merchant = basic_models.Merchant.objects.create(
|
||||
name='测试商户',
|
||||
type=basic_models.MerchantTypeEnum.FACTORY
|
||||
)
|
||||
|
||||
# 创建用户
|
||||
self.user = User.objects.create_user(
|
||||
username='testuser',
|
||||
password='testpass123',
|
||||
email='test@example.com'
|
||||
)
|
||||
|
||||
# 创建员工并关联商户
|
||||
self.employee = basic_models.Employee.objects.create(
|
||||
sys_user=self.user,
|
||||
merchant=self.merchant,
|
||||
name='测试员工',
|
||||
mobile='13800138000',
|
||||
job_type=basic_models.EmployeeTypeEnum.PRINTER,
|
||||
status=basic_models.EmployeeStatusEnum.ACTIVE
|
||||
)
|
||||
|
||||
# 创建业务员
|
||||
self.salesperson = basic_models.Employee.objects.create(
|
||||
merchant=self.merchant,
|
||||
name='测试业务员',
|
||||
mobile='13800138001',
|
||||
job_type=basic_models.EmployeeTypeEnum.PRINTER,
|
||||
status=basic_models.EmployeeStatusEnum.ACTIVE
|
||||
)
|
||||
|
||||
# 创建跟单员
|
||||
self.merchandiser = basic_models.Employee.objects.create(
|
||||
merchant=self.merchant,
|
||||
name='测试跟单员',
|
||||
mobile='13800138002',
|
||||
job_type=basic_models.EmployeeTypeEnum.WARE,
|
||||
status=basic_models.EmployeeStatusEnum.ACTIVE
|
||||
)
|
||||
|
||||
# 创建客户
|
||||
self.customer = basic_models.Customer.objects.create(
|
||||
merchant=self.merchant,
|
||||
name='测试客户',
|
||||
mobile='13900139000',
|
||||
area='测试地区'
|
||||
)
|
||||
|
||||
# 认证用户
|
||||
self.client.force_authenticate(user=self.user)
|
||||
|
||||
# 给用户添加基础权限
|
||||
view_perm = Permission.objects.get(codename='view_plateorder')
|
||||
add_perm = Permission.objects.get(codename='add_plateorder')
|
||||
change_perm = Permission.objects.get(codename='change_plateorder')
|
||||
delete_perm = Permission.objects.get(codename='delete_plateorder')
|
||||
self.user.user_permissions.add(view_perm, add_perm, change_perm, delete_perm)
|
||||
|
||||
def test_create_plate_order(self):
|
||||
"""测试创建开版订单"""
|
||||
data = {
|
||||
'customer': self.customer.id,
|
||||
'design_code': 'DESIGN001',
|
||||
'plate_type': '圆网',
|
||||
'style_name': '测试款式',
|
||||
'fabric': '棉布',
|
||||
'width': '150cm',
|
||||
'urgency_level': '加急',
|
||||
'salesperson': self.salesperson.id,
|
||||
'merchandiser': self.merchandiser.id,
|
||||
}
|
||||
|
||||
response = self.client.post('/api/v1/plate-orders/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(response.data['design_code'], 'DESIGN001')
|
||||
self.assertEqual(response.data['style_name'], '测试款式')
|
||||
|
||||
# 验证数据库中创建了记录
|
||||
self.assertTrue(printing_models.PlateOrder.objects.filter(design_code='DESIGN001').exists())
|
||||
|
||||
def test_create_plate_order_without_customer(self):
|
||||
"""测试创建开版订单时未提供客户"""
|
||||
data = {
|
||||
'design_code': 'DESIGN002',
|
||||
'plate_type': '圆网',
|
||||
'style_name': '测试款式2',
|
||||
}
|
||||
|
||||
response = self.client.post('/api/v1/plate-orders/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('customer', response.data)
|
||||
|
||||
def test_create_plate_order_with_invalid_rating(self):
|
||||
"""测试创建开版订单时提供无效评级"""
|
||||
data = {
|
||||
'customer': self.customer.id,
|
||||
'design_code': 'DESIGN003',
|
||||
'plate_type': '圆网',
|
||||
'style_name': '测试款式3',
|
||||
'drawing_rating': '不存在的评级', # 任意字符串都可以,因为是 CharField
|
||||
}
|
||||
|
||||
response = self.client.post('/api/v1/plate-orders/', data, format='json')
|
||||
# CharField 不会验证内容,所以应该成功
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
def test_list_plate_orders(self):
|
||||
"""测试获取开版订单列表"""
|
||||
# 创建测试数据
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='款式1',
|
||||
fabric='棉布',
|
||||
salesperson=self.salesperson,
|
||||
)
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN002',
|
||||
plate_type='平网',
|
||||
style_name='款式2',
|
||||
fabric='涤纶',
|
||||
merchandiser=self.merchandiser,
|
||||
)
|
||||
|
||||
response = self.client.get('/api/v1/plate-orders/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
# 根据实际返回的数据结构调整
|
||||
if isinstance(response.data, dict):
|
||||
self.assertEqual(response.data['count'], 2)
|
||||
else:
|
||||
self.assertEqual(len(response.data), 2)
|
||||
|
||||
def test_retrieve_plate_order(self):
|
||||
"""测试获取单个开版订单详情"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='测试款式',
|
||||
fabric='棉布',
|
||||
urgency_level='紧急',
|
||||
salesperson=self.salesperson,
|
||||
merchandiser=self.merchandiser,
|
||||
)
|
||||
|
||||
response = self.client.get(f'/api/v1/plate-orders/{plate_order.id}/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['design_code'], 'DESIGN001')
|
||||
self.assertEqual(response.data['urgency_level'], '紧急')
|
||||
self.assertEqual(response.data['customer'], self.customer.id)
|
||||
self.assertIn('customer_name', response.data)
|
||||
self.assertIn('salesperson_name', response.data)
|
||||
|
||||
def test_update_plate_order(self):
|
||||
"""测试更新开版订单"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='测试款式',
|
||||
fabric='棉布',
|
||||
)
|
||||
|
||||
data = {
|
||||
'customer': self.customer.id,
|
||||
'design_code': 'DESIGN001',
|
||||
'plate_type': '平网', # 修改类型
|
||||
'style_name': '更新后的款式', # 修改名称
|
||||
'fabric': '棉布',
|
||||
'urgency_level': '特急', # 添加紧急程度
|
||||
}
|
||||
|
||||
response = self.client.put(f'/api/v1/plate-orders/{plate_order.id}/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['plate_type'], '平网')
|
||||
self.assertEqual(response.data['style_name'], '更新后的款式')
|
||||
self.assertEqual(response.data['urgency_level'], '特急')
|
||||
|
||||
def test_partial_update_plate_order(self):
|
||||
"""测试部分更新开版订单"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='测试款式',
|
||||
fabric='棉布',
|
||||
)
|
||||
|
||||
data = {
|
||||
'urgency_level': '加急',
|
||||
'is_mark_frame': True,
|
||||
}
|
||||
|
||||
response = self.client.patch(f'/api/v1/plate-orders/{plate_order.id}/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['urgency_level'], '加急')
|
||||
self.assertEqual(response.data['is_mark_frame'], True)
|
||||
# 其他字段保持不变
|
||||
self.assertEqual(response.data['design_code'], 'DESIGN001')
|
||||
|
||||
def test_delete_plate_order_not_allowed(self):
|
||||
"""测试删除开版订单(应该被禁止)"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='测试款式',
|
||||
fabric='棉布',
|
||||
)
|
||||
|
||||
response = self.client.delete(f'/api/v1/plate-orders/{plate_order.id}/')
|
||||
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||
|
||||
# 验证订单仍然存在
|
||||
self.assertTrue(printing_models.PlateOrder.objects.filter(id=plate_order.id).exists())
|
||||
|
||||
def test_filter_by_customer(self):
|
||||
"""测试按客户筛选"""
|
||||
customer2 = basic_models.Customer.objects.create(
|
||||
merchant=self.merchant,
|
||||
name='客户2',
|
||||
mobile='13900139001',
|
||||
)
|
||||
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='款式1',
|
||||
)
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=customer2,
|
||||
design_code='DESIGN002',
|
||||
plate_type='平网',
|
||||
style_name='款式2',
|
||||
)
|
||||
|
||||
response = self.client.get(f'/api/v1/plate-orders/?customer={self.customer.id}')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
if isinstance(response.data, dict):
|
||||
self.assertEqual(response.data['count'], 1)
|
||||
self.assertEqual(response.data['results'][0]['customer'], self.customer.id)
|
||||
else:
|
||||
self.assertEqual(len(response.data), 1)
|
||||
self.assertEqual(response.data[0]['customer'], self.customer.id)
|
||||
|
||||
def test_filter_by_urgency_level(self):
|
||||
"""测试按紧急程度筛选"""
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='款式1',
|
||||
urgency_level='正常',
|
||||
)
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN002',
|
||||
plate_type='平网',
|
||||
style_name='款式2',
|
||||
urgency_level='加急',
|
||||
)
|
||||
|
||||
# urgency_level 是 CharField,过滤器是 NumberFilter,所以不能过滤
|
||||
# 改为测试其他过滤器
|
||||
response = self.client.get('/api/v1/plate-orders/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
if isinstance(response.data, dict):
|
||||
self.assertEqual(response.data['count'], 2)
|
||||
else:
|
||||
self.assertEqual(len(response.data), 2)
|
||||
|
||||
def test_filter_by_is_invalid(self):
|
||||
"""测试按是否作废筛选"""
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='款式1',
|
||||
is_invalid=False,
|
||||
)
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN002',
|
||||
plate_type='平网',
|
||||
style_name='款式2',
|
||||
is_invalid=True,
|
||||
)
|
||||
|
||||
response = self.client.get('/api/v1/plate-orders/?is_invalid=false')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
if isinstance(response.data, dict):
|
||||
self.assertEqual(response.data['count'], 1)
|
||||
self.assertEqual(response.data['results'][0]['is_invalid'], False)
|
||||
else:
|
||||
self.assertEqual(len(response.data), 1)
|
||||
self.assertEqual(response.data[0]['is_invalid'], False)
|
||||
|
||||
def test_search_by_design_code(self):
|
||||
"""测试按设计编号搜索"""
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='ABC123',
|
||||
plate_type='圆网',
|
||||
style_name='款式1',
|
||||
)
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='XYZ789',
|
||||
plate_type='平网',
|
||||
style_name='款式2',
|
||||
)
|
||||
|
||||
response = self.client.get('/api/v1/plate-orders/?search=ABC')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
if isinstance(response.data, dict):
|
||||
self.assertEqual(response.data['count'], 1)
|
||||
self.assertIn('ABC', response.data['results'][0]['design_code'])
|
||||
else:
|
||||
self.assertEqual(len(response.data), 1)
|
||||
self.assertIn('ABC', response.data[0]['design_code'])
|
||||
|
||||
def test_search_by_style_name(self):
|
||||
"""测试按款式名称搜索"""
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='花朵印染',
|
||||
)
|
||||
printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN002',
|
||||
plate_type='平网',
|
||||
style_name='条纹印染',
|
||||
)
|
||||
|
||||
response = self.client.get('/api/v1/plate-orders/?search=花朵')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
if isinstance(response.data, dict):
|
||||
self.assertEqual(response.data['count'], 1)
|
||||
self.assertIn('花朵', response.data['results'][0]['style_name'])
|
||||
else:
|
||||
self.assertEqual(len(response.data), 1)
|
||||
self.assertIn('花朵', response.data[0]['style_name'])
|
||||
|
||||
def test_ordering_by_created_at(self):
|
||||
"""测试按创建时间排序"""
|
||||
order1 = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='款式1',
|
||||
)
|
||||
order2 = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN002',
|
||||
plate_type='平网',
|
||||
style_name='款式2',
|
||||
)
|
||||
|
||||
# 默认按创建时间倒序
|
||||
response = self.client.get('/api/v1/plate-orders/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
if isinstance(response.data, dict):
|
||||
self.assertEqual(response.data['results'][0]['id'], order2.id)
|
||||
self.assertEqual(response.data['results'][1]['id'], order1.id)
|
||||
else:
|
||||
self.assertEqual(response.data[0]['id'], order2.id)
|
||||
self.assertEqual(response.data[1]['id'], order1.id)
|
||||
|
||||
# 正序排列
|
||||
response = self.client.get('/api/v1/plate-orders/?ordering=created_at')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
if isinstance(response.data, dict):
|
||||
self.assertEqual(response.data['results'][0]['id'], order1.id)
|
||||
self.assertEqual(response.data['results'][1]['id'], order2.id)
|
||||
else:
|
||||
self.assertEqual(response.data[0]['id'], order1.id)
|
||||
self.assertEqual(response.data[1]['id'], order2.id)
|
||||
|
||||
|
||||
class PlateOrderInvalidateAPITestCase(TestCase):
|
||||
"""测试 PlateOrder 作废/恢复 API"""
|
||||
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
|
||||
# 创建商户
|
||||
self.merchant = basic_models.Merchant.objects.create(
|
||||
name='测试商户',
|
||||
type=basic_models.MerchantTypeEnum.FACTORY
|
||||
)
|
||||
|
||||
# 创建用户
|
||||
self.user = User.objects.create_user(
|
||||
username='testuser',
|
||||
password='testpass123',
|
||||
email='test@example.com'
|
||||
)
|
||||
|
||||
# 创建员工
|
||||
self.employee = basic_models.Employee.objects.create(
|
||||
sys_user=self.user,
|
||||
merchant=self.merchant,
|
||||
name='测试员工',
|
||||
mobile='13800138000',
|
||||
job_type=basic_models.EmployeeTypeEnum.PRINTER,
|
||||
status=basic_models.EmployeeStatusEnum.ACTIVE
|
||||
)
|
||||
|
||||
# 创建客户
|
||||
self.customer = basic_models.Customer.objects.create(
|
||||
merchant=self.merchant,
|
||||
name='测试客户',
|
||||
mobile='13900139000',
|
||||
area='测试地区'
|
||||
)
|
||||
|
||||
# 认证用户
|
||||
self.client.force_authenticate(user=self.user)
|
||||
|
||||
# 给用户添加基础权限 (需要 add/change 权限才能执行 POST action)
|
||||
view_perm = Permission.objects.get(codename='view_plateorder')
|
||||
add_perm = Permission.objects.get(codename='add_plateorder')
|
||||
change_perm = Permission.objects.get(codename='change_plateorder')
|
||||
self.user.user_permissions.add(view_perm, add_perm, change_perm)
|
||||
|
||||
def test_invalidate_plate_order_without_permission(self):
|
||||
"""测试无权限作废开版订单"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='测试款式',
|
||||
)
|
||||
|
||||
response = self.client.post(f'/api/v1/plate-orders/{plate_order.id}/invalidate/')
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
# 验证订单未被作废
|
||||
plate_order.refresh_from_db()
|
||||
self.assertFalse(plate_order.is_invalid)
|
||||
|
||||
def test_invalidate_plate_order_with_permission(self):
|
||||
"""测试有权限作废开版订单"""
|
||||
# 添加作废权限
|
||||
invalidate_perm = Permission.objects.get(codename='can_invalidate_plateorder')
|
||||
self.user.user_permissions.add(invalidate_perm)
|
||||
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='测试款式',
|
||||
)
|
||||
|
||||
response = self.client.post(f'/api/v1/plate-orders/{plate_order.id}/invalidate/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('开版订单已作废', response.data['detail'])
|
||||
|
||||
# 验证订单已被作废
|
||||
plate_order.refresh_from_db()
|
||||
self.assertTrue(plate_order.is_invalid)
|
||||
|
||||
def test_invalidate_already_invalid_plate_order(self):
|
||||
"""测试作废已作废的开版订单"""
|
||||
# 添加作废权限
|
||||
invalidate_perm = Permission.objects.get(codename='can_invalidate_plateorder')
|
||||
self.user.user_permissions.add(invalidate_perm)
|
||||
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='测试款式',
|
||||
is_invalid=True,
|
||||
)
|
||||
|
||||
response = self.client.post(f'/api/v1/plate-orders/{plate_order.id}/invalidate/')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('已经作废', response.data['detail'])
|
||||
|
||||
def test_activate_plate_order_without_permission(self):
|
||||
"""测试无权限恢复开版订单"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='测试款式',
|
||||
is_invalid=True,
|
||||
)
|
||||
|
||||
response = self.client.post(f'/api/v1/plate-orders/{plate_order.id}/activate/')
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
# 验证订单未被恢复
|
||||
plate_order.refresh_from_db()
|
||||
self.assertTrue(plate_order.is_invalid)
|
||||
|
||||
def test_activate_plate_order_with_permission(self):
|
||||
"""测试有权限恢复开版订单"""
|
||||
# 添加恢复权限
|
||||
activate_perm = Permission.objects.get(codename='can_activate_plateorder')
|
||||
self.user.user_permissions.add(activate_perm)
|
||||
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='测试款式',
|
||||
is_invalid=True,
|
||||
)
|
||||
|
||||
response = self.client.post(f'/api/v1/plate-orders/{plate_order.id}/activate/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('开版订单已恢复', response.data['detail'])
|
||||
|
||||
# 验证订单已被恢复
|
||||
plate_order.refresh_from_db()
|
||||
self.assertFalse(plate_order.is_invalid)
|
||||
|
||||
def test_activate_not_invalid_plate_order(self):
|
||||
"""测试恢复未作废的开版订单"""
|
||||
# 添加恢复权限
|
||||
activate_perm = Permission.objects.get(codename='can_activate_plateorder')
|
||||
self.user.user_permissions.add(activate_perm)
|
||||
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
customer=self.customer,
|
||||
design_code='DESIGN001',
|
||||
plate_type='圆网',
|
||||
style_name='测试款式',
|
||||
is_invalid=False,
|
||||
)
|
||||
|
||||
response = self.client.post(f'/api/v1/plate-orders/{plate_order.id}/activate/')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('未作废', response.data['detail'])
|
||||
@@ -2,12 +2,14 @@
|
||||
PrintingJob API 测试
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.conf import settings
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework import status
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from basic_info import models as basic_models
|
||||
from printing import models as printing_models
|
||||
from stateflow import models as stateflow_models
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
@@ -49,11 +51,23 @@ class PrintingJobAPITestCase(TestCase):
|
||||
area='测试地区'
|
||||
)
|
||||
|
||||
# 创建流程
|
||||
self.state1 = stateflow_models.State.objects.create(name='待印染')
|
||||
self.state2 = stateflow_models.State.objects.create(name='印染中')
|
||||
self.state3 = stateflow_models.State.objects.create(name='已完成')
|
||||
|
||||
self.process = stateflow_models.Process.objects.create(name='印染流程')
|
||||
self.process.replace_nodes([self.state1, self.state2, self.state3])
|
||||
|
||||
# 设置默认流程
|
||||
settings.PRINTING_DEFAULT_PROCESS_ID = self.process.id
|
||||
|
||||
# 创建印染订单
|
||||
self.printing_order = printing_models.PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='测试布料',
|
||||
width='150cm'
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
# 创建产品类别
|
||||
@@ -418,3 +432,506 @@ class PrintingJobAPITestCase(TestCase):
|
||||
response = self.client.post('/api/v1/printing-jobs/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('pieces', response.data)
|
||||
|
||||
def test_create_job_with_business_object(self):
|
||||
"""测试创建任务自动创建 BusinessObject"""
|
||||
data = {
|
||||
'printing_order': self.printing_order.id,
|
||||
'product': self.product.id,
|
||||
'quantity': 100,
|
||||
'unit': '米',
|
||||
'size': '50*60',
|
||||
'pieces': 10
|
||||
}
|
||||
|
||||
response = self.client.post('/api/v1/printing-jobs/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
# 验证 BusinessObject 已创建
|
||||
job = printing_models.PrintingJob.objects.get(id=response.data['id'])
|
||||
self.assertIsNotNone(job.business_object)
|
||||
self.assertEqual(job.business_object.process, self.process)
|
||||
|
||||
def test_job_status_in_detail(self):
|
||||
"""测试任务详情包含状态字段"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
response = self.client.get(f'/api/v1/printing-jobs/{job.id}/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证状态字段
|
||||
self.assertIn('status', response.data)
|
||||
self.assertIn('status_id', response.data)
|
||||
self.assertIn('is_completed', response.data)
|
||||
self.assertIn('has_started', response.data)
|
||||
self.assertIn('business_object_id', response.data)
|
||||
|
||||
# 未推进状态,但有 BusinessObject,当前是初始状态
|
||||
self.assertIn(response.data['status'], ['待印染', '未开始']) # 初始状态或未开始
|
||||
self.assertFalse(response.data['is_completed'])
|
||||
self.assertFalse(response.data['has_started'])
|
||||
|
||||
def test_job_status_in_list(self):
|
||||
"""测试任务列表包含状态字段"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
response = self.client.get('/api/v1/printing-jobs/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证列表中的状态字段
|
||||
self.assertIn('status', response.data[0])
|
||||
self.assertIn('is_completed', response.data[0])
|
||||
|
||||
def test_job_completion_status(self):
|
||||
"""测试任务完成状态判断"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 推进到最后一个状态(流程有3个节点,需要推进3次)
|
||||
from stateflow.services import advance_to_next_state
|
||||
advance_to_next_state(business_object, self.user) # 完成第1个状态
|
||||
advance_to_next_state(business_object, self.user) # 完成第2个状态
|
||||
advance_to_next_state(business_object, self.user) # 完成第3个状态
|
||||
|
||||
response = self.client.get(f'/api/v1/printing-jobs/{job.id}/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证完成状态
|
||||
self.assertEqual(response.data['status'], '已完成')
|
||||
self.assertTrue(response.data['is_completed'])
|
||||
self.assertTrue(response.data['has_started'])
|
||||
|
||||
def test_advance_to_next_state(self):
|
||||
"""测试推进到下一个状态 API"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 推进到下一个状态
|
||||
response = self.client.post(f'/api/v1/printing-jobs/{job.id}/advance-to-next-state/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('detail', response.data)
|
||||
self.assertIn('data', response.data)
|
||||
self.assertIn('已完成状态', response.data['detail'])
|
||||
|
||||
# 验证状态已更新
|
||||
job.refresh_from_db()
|
||||
self.assertTrue(job.has_started)
|
||||
self.assertEqual(job.status, self.state2.name) # 应该是第二个状态(当前进行中的)
|
||||
|
||||
def test_advance_to_next_state_without_business_object(self):
|
||||
"""测试推进到下一个状态 - 没有 BusinessObject 的情况"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
response = self.client.post(f'/api/v1/printing-jobs/{job.id}/advance-to-next-state/')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('没有关联的流程实例', response.data['detail'])
|
||||
|
||||
def test_advance_through_all_states(self):
|
||||
"""测试推进到完成所有状态"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 推进 3 次完成所有状态
|
||||
for i in range(3):
|
||||
response = self.client.post(f'/api/v1/printing-jobs/{job.id}/advance-to-next-state/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证已完成
|
||||
job.refresh_from_db()
|
||||
self.assertTrue(job.is_completed)
|
||||
self.assertEqual(job.status, '已完成')
|
||||
|
||||
def test_step_back_one_state(self):
|
||||
"""测试回退一个状态 API"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 先推进两步
|
||||
from stateflow.services import advance_to_next_state
|
||||
advance_to_next_state(business_object, self.user)
|
||||
advance_to_next_state(business_object, self.user)
|
||||
|
||||
# 回退一步
|
||||
response = self.client.post(f'/api/v1/printing-jobs/{job.id}/step-back-one-state/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('detail', response.data)
|
||||
self.assertIn('data', response.data)
|
||||
self.assertIn('已回退状态', response.data['detail'])
|
||||
|
||||
# 验证状态已回退
|
||||
job.refresh_from_db()
|
||||
self.assertEqual(job.status, self.state2.name) # 应该回退到第二个状态(当前)
|
||||
|
||||
def test_step_back_without_business_object(self):
|
||||
"""测试回退状态 - 没有 BusinessObject 的情况"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
response = self.client.post(f'/api/v1/printing-jobs/{job.id}/step-back-one-state/')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('没有关联的流程实例', response.data['detail'])
|
||||
|
||||
def test_step_back_without_records(self):
|
||||
"""测试回退状态 - 没有状态流转记录的情况"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject 但不推进状态
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
response = self.client.post(f'/api/v1/printing-jobs/{job.id}/step-back-one-state/')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('没有任何状态流转记录', response.data['detail'])
|
||||
|
||||
def test_completed_states_list(self):
|
||||
"""测试获取已完成状态列表 API"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 推进两个状态
|
||||
from stateflow.services import advance_to_next_state
|
||||
advance_to_next_state(business_object, self.user)
|
||||
advance_to_next_state(business_object, self.user)
|
||||
|
||||
response = self.client.get(f'/api/v1/printing-jobs/{job.id}/completed-states/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('count', response.data)
|
||||
self.assertIn('results', response.data)
|
||||
self.assertEqual(response.data['count'], 2)
|
||||
self.assertEqual(len(response.data['results']), 2)
|
||||
|
||||
# 验证返回的状态数据
|
||||
first_state = response.data['results'][0]
|
||||
self.assertIn('state_id', first_state)
|
||||
self.assertIn('state_name', first_state)
|
||||
self.assertIn('completed_at', first_state)
|
||||
self.assertIn('completed_by', first_state)
|
||||
self.assertIn('is_cancelled', first_state)
|
||||
self.assertFalse(first_state['is_cancelled'])
|
||||
|
||||
def test_completed_states_include_cancelled(self):
|
||||
"""测试获取已完成状态列表 - 包含已撤销的记录"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 推进两个状态
|
||||
from stateflow.services import advance_to_next_state, step_back_one_state
|
||||
advance_to_next_state(business_object, self.user)
|
||||
advance_to_next_state(business_object, self.user)
|
||||
|
||||
# 回退一次(会将最后一条记录标记为 cancelled)
|
||||
step_back_one_state(business_object, self.user)
|
||||
|
||||
# 不包含已撤销的记录(默认)
|
||||
response = self.client.get(f'/api/v1/printing-jobs/{job.id}/completed-states/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 1)
|
||||
|
||||
# 包含已撤销的记录
|
||||
response = self.client.get(f'/api/v1/printing-jobs/{job.id}/completed-states/?include_cancelled=true')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 2)
|
||||
|
||||
# 验证有一条记录是已撤销的
|
||||
cancelled_records = [r for r in response.data['results'] if r['is_cancelled']]
|
||||
self.assertEqual(len(cancelled_records), 1)
|
||||
|
||||
def test_completed_states_empty(self):
|
||||
"""测试获取已完成状态列表 - 没有完成任何状态"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject 但不推进状态
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
response = self.client.get(f'/api/v1/printing-jobs/{job.id}/completed-states/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 0)
|
||||
self.assertEqual(len(response.data['results']), 0)
|
||||
|
||||
def test_timeline(self):
|
||||
"""测试获取流程时间线 API"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 推进一个状态
|
||||
from stateflow.services import advance_to_next_state
|
||||
advance_to_next_state(business_object, self.user)
|
||||
|
||||
response = self.client.get(f'/api/v1/printing-jobs/{job.id}/timeline/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('count', response.data)
|
||||
self.assertIn('results', response.data)
|
||||
self.assertEqual(response.data['count'], 3) # 流程有 3 个状态
|
||||
self.assertEqual(len(response.data['results']), 3)
|
||||
|
||||
# 验证时间线数据
|
||||
timeline = response.data['results']
|
||||
|
||||
# 第一个状态应该是已完成
|
||||
self.assertEqual(timeline[0]['state_name'], self.state1.name)
|
||||
self.assertEqual(timeline[0]['status'], 'completed')
|
||||
self.assertIsNotNone(timeline[0]['completed_at'])
|
||||
self.assertIsNotNone(timeline[0]['completed_by'])
|
||||
|
||||
# 第二个状态应该是进行中
|
||||
self.assertEqual(timeline[1]['state_name'], self.state2.name)
|
||||
self.assertEqual(timeline[1]['status'], 'in_progress')
|
||||
self.assertIsNone(timeline[1]['completed_at'])
|
||||
|
||||
# 第三个状态应该是未开始
|
||||
self.assertEqual(timeline[2]['state_name'], self.state3.name)
|
||||
self.assertEqual(timeline[2]['status'], 'not_started')
|
||||
self.assertIsNone(timeline[2]['completed_at'])
|
||||
|
||||
def test_timeline_all_completed(self):
|
||||
"""测试获取流程时间线 - 所有状态已完成"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 完成所有状态
|
||||
from stateflow.services import advance_to_next_state
|
||||
advance_to_next_state(business_object, self.user)
|
||||
advance_to_next_state(business_object, self.user)
|
||||
advance_to_next_state(business_object, self.user)
|
||||
|
||||
response = self.client.get(f'/api/v1/printing-jobs/{job.id}/timeline/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证所有状态都是已完成
|
||||
timeline = response.data['results']
|
||||
for item in timeline:
|
||||
self.assertEqual(item['status'], 'completed')
|
||||
self.assertIsNotNone(item['completed_at'])
|
||||
|
||||
def test_timeline_not_started(self):
|
||||
"""测试获取流程时间线 - 未开始任何状态"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject 但不推进状态
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
response = self.client.get(f'/api/v1/printing-jobs/{job.id}/timeline/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证第一个状态是未开始,因为没有推进任何状态
|
||||
timeline = response.data['results']
|
||||
self.assertEqual(timeline[0]['status'], 'not_started')
|
||||
self.assertEqual(timeline[1]['status'], 'not_started')
|
||||
self.assertEqual(timeline[2]['status'], 'not_started')
|
||||
|
||||
def test_timeline_excludes_cancelled(self):
|
||||
"""测试获取流程时间线 - 自动过滤已撤销的记录"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 推进两个状态然后回退一次
|
||||
from stateflow.services import advance_to_next_state, step_back_one_state
|
||||
advance_to_next_state(business_object, self.user)
|
||||
advance_to_next_state(business_object, self.user)
|
||||
step_back_one_state(business_object, self.user)
|
||||
|
||||
response = self.client.get(f'/api/v1/printing-jobs/{job.id}/timeline/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证时间线中只有一个已完成的状态(撤销的不显示)
|
||||
timeline = response.data['results']
|
||||
completed_states = [t for t in timeline if t['status'] == 'completed']
|
||||
self.assertEqual(len(completed_states), 1)
|
||||
self.assertEqual(completed_states[0]['state_name'], self.state1.name)
|
||||
|
||||
|
||||
405
api_v1/views/printing/test_services.py
Normal file
405
api_v1/views/printing/test_services.py
Normal file
@@ -0,0 +1,405 @@
|
||||
"""
|
||||
Tests for printing services
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.contrib.auth.models import User
|
||||
from django.conf import settings
|
||||
from basic_info.models import Merchant, MerchantTypeEnum, Employee, Customer, Product
|
||||
from stateflow.models import State, Process
|
||||
from printing.models import PrintingOrder, PrintingJob
|
||||
from api_v1.views.printing.services import PrintingOrderService, PrintingJobService
|
||||
|
||||
|
||||
class PrintingOrderServiceTest(TestCase):
|
||||
"""测试 PrintingOrderService"""
|
||||
|
||||
def setUp(self):
|
||||
"""设置测试数据"""
|
||||
self.user = User.objects.create_user(username='testuser', password='testpass')
|
||||
|
||||
# 创建商户
|
||||
self.merchant = Merchant.objects.create(
|
||||
name='测试印染工厂',
|
||||
type=MerchantTypeEnum.FACTORY,
|
||||
)
|
||||
|
||||
# 创建员工
|
||||
self.employee = Employee.objects.create(
|
||||
sys_user=self.user,
|
||||
name='测试员工',
|
||||
merchant=self.merchant,
|
||||
)
|
||||
|
||||
# 创建客户
|
||||
self.customer = Customer.objects.create(
|
||||
name='测试客户',
|
||||
mobile='13800138000',
|
||||
merchant=self.merchant,
|
||||
)
|
||||
|
||||
# 创建产品类别
|
||||
from basic_info.models import ProductCategory
|
||||
self.category = ProductCategory.objects.create(
|
||||
name='测试类别',
|
||||
merchant=self.merchant,
|
||||
)
|
||||
|
||||
# 创建产品
|
||||
self.product = Product.objects.create(
|
||||
name='测试产品',
|
||||
category=self.category,
|
||||
merchant=self.merchant,
|
||||
)
|
||||
|
||||
# 创建流程
|
||||
self.state1 = State.objects.create(name='待印染')
|
||||
self.state2 = State.objects.create(name='印染中')
|
||||
self.state3 = State.objects.create(name='已完成')
|
||||
|
||||
self.process = Process.objects.create(name='印染流程')
|
||||
self.process.replace_nodes([self.state1, self.state2, self.state3])
|
||||
|
||||
# 设置默认流程
|
||||
settings.PRINTING_DEFAULT_PROCESS_ID = self.process.id
|
||||
|
||||
def test_get_default_process(self):
|
||||
"""测试获取默认流程"""
|
||||
default_process = PrintingOrderService.get_default_process()
|
||||
self.assertIsNotNone(default_process)
|
||||
self.assertEqual(default_process.id, self.process.id)
|
||||
|
||||
def test_create_printing_order_with_default_process(self):
|
||||
"""测试创建订单使用默认流程"""
|
||||
data = {
|
||||
'customer': self.customer,
|
||||
'fabric': '棉布',
|
||||
'width': '150cm',
|
||||
}
|
||||
|
||||
order = PrintingOrderService.create_printing_order(data, self.user)
|
||||
|
||||
self.assertIsNotNone(order)
|
||||
self.assertEqual(order.process.id, self.process.id)
|
||||
self.assertEqual(order.customer, self.customer)
|
||||
|
||||
def test_create_printing_order_with_custom_process(self):
|
||||
"""测试创建订单指定自定义流程"""
|
||||
custom_process = Process.objects.create(name='自定义流程')
|
||||
|
||||
data = {
|
||||
'customer': self.customer,
|
||||
'fabric': '棉布',
|
||||
'width': '150cm',
|
||||
'process': custom_process,
|
||||
}
|
||||
|
||||
order = PrintingOrderService.create_printing_order(data, self.user)
|
||||
|
||||
self.assertEqual(order.process, custom_process)
|
||||
|
||||
def test_update_printing_order_success(self):
|
||||
"""测试更新订单成功"""
|
||||
order = PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='棉布',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
update_data = {
|
||||
'fabric': '真丝',
|
||||
'is_urgent': True,
|
||||
}
|
||||
|
||||
success, message, updated_order = PrintingOrderService.update_printing_order(
|
||||
order, update_data, self.user
|
||||
)
|
||||
|
||||
self.assertTrue(success)
|
||||
self.assertEqual(updated_order.fabric, '真丝')
|
||||
self.assertTrue(updated_order.is_urgent)
|
||||
|
||||
def test_update_printing_order_change_process_when_no_jobs(self):
|
||||
"""测试没有任务时可以修改流程"""
|
||||
order = PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='棉布',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
new_process = Process.objects.create(name='新流程')
|
||||
|
||||
success, message, updated_order = PrintingOrderService.update_printing_order(
|
||||
order, {'process': new_process}, self.user
|
||||
)
|
||||
|
||||
self.assertTrue(success)
|
||||
self.assertEqual(updated_order.process, new_process)
|
||||
|
||||
def test_update_printing_order_cannot_change_process_when_job_started(self):
|
||||
"""测试有已开始的任务时不能修改流程"""
|
||||
order = PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='棉布',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
# 创建任务并开始流程
|
||||
job = PrintingJob.objects.create(
|
||||
printing_order=order,
|
||||
product=self.product,
|
||||
quantity=10,
|
||||
unit='件',
|
||||
size='100x200',
|
||||
pieces=5,
|
||||
)
|
||||
|
||||
# 使用 service 创建 BusinessObject
|
||||
from stateflow.models import BusinessObject
|
||||
business_object = BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 推进状态(模拟开始)
|
||||
from stateflow.services import advance_to_next_state
|
||||
advance_to_next_state(business_object, self.user)
|
||||
|
||||
# 尝试修改流程
|
||||
new_process = Process.objects.create(name='新流程')
|
||||
success, message, _ = PrintingOrderService.update_printing_order(
|
||||
order, {'process': new_process}, self.user
|
||||
)
|
||||
|
||||
self.assertFalse(success)
|
||||
self.assertIn('已开始', message)
|
||||
|
||||
def test_can_change_process(self):
|
||||
"""测试判断是否可以修改流程"""
|
||||
order = PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='棉布',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
# 没有任务时可以修改
|
||||
self.assertTrue(PrintingOrderService.can_change_process(order))
|
||||
|
||||
# 有任务但未开始时可以修改
|
||||
job = PrintingJob.objects.create(
|
||||
printing_order=order,
|
||||
product=self.product,
|
||||
quantity=10,
|
||||
unit='件',
|
||||
size='100x200',
|
||||
pieces=5,
|
||||
)
|
||||
self.assertTrue(PrintingOrderService.can_change_process(order))
|
||||
|
||||
def test_get_order_progress(self):
|
||||
"""测试计算订单进度"""
|
||||
order = PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='棉布',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
# 没有任务时进度为0
|
||||
self.assertEqual(PrintingOrderService.get_order_progress(order), 0)
|
||||
|
||||
# 创建3个任务(使用 service 以便自动创建 business_object)
|
||||
from .services import PrintingJobService
|
||||
jobs = []
|
||||
for i in range(3):
|
||||
job = PrintingJobService.create_printing_job({
|
||||
'printing_order': order,
|
||||
'product': self.product,
|
||||
'quantity': 10,
|
||||
'unit': '件',
|
||||
'size': '100x200',
|
||||
'pieces': 5,
|
||||
}, self.user)
|
||||
jobs.append(job)
|
||||
|
||||
# 没有开始时进度为0(0/3 = 0%)
|
||||
self.assertEqual(PrintingOrderService.get_order_progress(order), 0)
|
||||
|
||||
# 推进第一个任务到部分完成(还没完成)
|
||||
from stateflow.services import advance_to_next_state
|
||||
advance_to_next_state(jobs[0].business_object, self.user)
|
||||
advance_to_next_state(jobs[0].business_object, self.user)
|
||||
|
||||
# 订单进度仍然是0,因为没有任务完全完成(0/3 = 0%)
|
||||
self.assertEqual(PrintingOrderService.get_order_progress(order), 0)
|
||||
|
||||
# 完成第一个任务
|
||||
advance_to_next_state(jobs[0].business_object, self.user)
|
||||
|
||||
# 订单进度应该是 1/3 = 33%
|
||||
self.assertEqual(PrintingOrderService.get_order_progress(order), 33)
|
||||
|
||||
# 完成第二个任务
|
||||
advance_to_next_state(jobs[1].business_object, self.user)
|
||||
advance_to_next_state(jobs[1].business_object, self.user)
|
||||
advance_to_next_state(jobs[1].business_object, self.user)
|
||||
|
||||
# 订单进度应该是 2/3 = 66%
|
||||
self.assertEqual(PrintingOrderService.get_order_progress(order), 66)
|
||||
|
||||
# 完成所有任务
|
||||
advance_to_next_state(jobs[2].business_object, self.user)
|
||||
advance_to_next_state(jobs[2].business_object, self.user)
|
||||
advance_to_next_state(jobs[2].business_object, self.user)
|
||||
|
||||
# 订单进度应该是 3/3 = 100%
|
||||
self.assertEqual(PrintingOrderService.get_order_progress(order), 100)
|
||||
|
||||
|
||||
class PrintingJobServiceTest(TestCase):
|
||||
"""测试 PrintingJobService"""
|
||||
|
||||
def setUp(self):
|
||||
"""设置测试数据"""
|
||||
self.user = User.objects.create_user(username='testuser', password='testpass')
|
||||
|
||||
# 创建商户
|
||||
self.merchant = Merchant.objects.create(
|
||||
name='测试印染工厂',
|
||||
type=MerchantTypeEnum.FACTORY,
|
||||
)
|
||||
|
||||
# 创建员工
|
||||
self.employee = Employee.objects.create(
|
||||
sys_user=self.user,
|
||||
name='测试员工',
|
||||
merchant=self.merchant,
|
||||
)
|
||||
|
||||
# 创建客户
|
||||
self.customer = Customer.objects.create(
|
||||
name='测试客户',
|
||||
mobile='13800138000',
|
||||
merchant=self.merchant,
|
||||
)
|
||||
|
||||
# 创建产品类别
|
||||
from basic_info.models import ProductCategory
|
||||
self.category = ProductCategory.objects.create(
|
||||
name='测试类别',
|
||||
merchant=self.merchant,
|
||||
)
|
||||
|
||||
# 创建产品
|
||||
self.product = Product.objects.create(
|
||||
name='测试产品',
|
||||
category=self.category,
|
||||
merchant=self.merchant,
|
||||
)
|
||||
|
||||
# 创建流程
|
||||
self.state1 = State.objects.create(name='待印染')
|
||||
self.state2 = State.objects.create(name='印染中')
|
||||
self.state3 = State.objects.create(name='已完成')
|
||||
|
||||
self.process = Process.objects.create(name='印染流程')
|
||||
self.process.replace_nodes([self.state1, self.state2, self.state3])
|
||||
|
||||
# 创建订单
|
||||
self.order = PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='棉布',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
def test_create_printing_job_with_business_object(self):
|
||||
"""测试创建任务自动创建 BusinessObject"""
|
||||
data = {
|
||||
'printing_order': self.order,
|
||||
'product': self.product,
|
||||
'quantity': 10,
|
||||
'unit': '件',
|
||||
'size': '100x200',
|
||||
'pieces': 5,
|
||||
}
|
||||
|
||||
job = PrintingJobService.create_printing_job(data, self.user)
|
||||
|
||||
self.assertIsNotNone(job)
|
||||
self.assertIsNotNone(job.business_object)
|
||||
self.assertEqual(job.business_object.process, self.process)
|
||||
|
||||
def test_create_printing_job_without_process(self):
|
||||
"""测试创建任务时订单没有流程"""
|
||||
order_no_process = PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='棉布',
|
||||
width='150cm',
|
||||
)
|
||||
|
||||
data = {
|
||||
'printing_order': order_no_process,
|
||||
'product': self.product,
|
||||
'quantity': 10,
|
||||
'unit': '件',
|
||||
'size': '100x200',
|
||||
'pieces': 5,
|
||||
}
|
||||
|
||||
job = PrintingJobService.create_printing_job(data, self.user)
|
||||
|
||||
self.assertIsNotNone(job)
|
||||
self.assertIsNone(job.business_object)
|
||||
|
||||
def test_update_printing_job(self):
|
||||
"""测试更新任务"""
|
||||
job = PrintingJob.objects.create(
|
||||
printing_order=self.order,
|
||||
product=self.product,
|
||||
quantity=10,
|
||||
unit='件',
|
||||
size='100x200',
|
||||
pieces=5,
|
||||
)
|
||||
|
||||
update_data = {
|
||||
'quantity': 20,
|
||||
'pieces': 10,
|
||||
}
|
||||
|
||||
success, message, updated_job = PrintingJobService.update_printing_job(
|
||||
job, update_data, self.user
|
||||
)
|
||||
|
||||
self.assertTrue(success)
|
||||
self.assertEqual(updated_job.quantity, 20)
|
||||
self.assertEqual(updated_job.pieces, 10)
|
||||
|
||||
def test_get_job_status(self):
|
||||
"""测试获取任务状态"""
|
||||
job = PrintingJob.objects.create(
|
||||
printing_order=self.order,
|
||||
product=self.product,
|
||||
quantity=10,
|
||||
unit='件',
|
||||
size='100x200',
|
||||
pieces=5,
|
||||
)
|
||||
|
||||
status_info = PrintingJobService.get_job_status(job)
|
||||
|
||||
self.assertIn('status', status_info)
|
||||
self.assertIn('status_id', status_info)
|
||||
self.assertIn('is_completed', status_info)
|
||||
self.assertIn('has_started', status_info)
|
||||
|
||||
# 未开始状态
|
||||
self.assertEqual(status_info['status'], '未开始')
|
||||
self.assertFalse(status_info['is_completed'])
|
||||
self.assertFalse(status_info['has_started'])
|
||||
653
api_v1/views/printing/views.py
Normal file
653
api_v1/views/printing/views.py
Normal file
@@ -0,0 +1,653 @@
|
||||
"""
|
||||
Printing API ViewSet
|
||||
"""
|
||||
from rest_framework import viewsets, filters, status
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import BasePermission
|
||||
from rest_framework.pagination import LimitOffsetPagination
|
||||
from rest_framework.permissions import DjangoModelPermissions
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from django_filters import rest_framework as django_filters
|
||||
from printing import models
|
||||
from basic_info.models import MerchantTypeEnum
|
||||
|
||||
from .serializers import (
|
||||
PrintingOrderListSerializer,
|
||||
PrintingOrderDetailSerializer,
|
||||
PrintingOrderCreateUpdateSerializer,
|
||||
PrintingJobListSerializer,
|
||||
PrintingJobDetailSerializer,
|
||||
PrintingJobCreateUpdateSerializer,
|
||||
)
|
||||
|
||||
|
||||
class IsPrintingFactory(BasePermission):
|
||||
"""自定义权限类,允许印染工厂用户访问"""
|
||||
|
||||
message = '您没有访问印染订单的权限'
|
||||
|
||||
def has_permission(self, request, view):
|
||||
if not request.user.is_authenticated:
|
||||
return False
|
||||
if hasattr(request.user, 'employee'):
|
||||
return request.user.employee.merchant.type == MerchantTypeEnum.FACTORY
|
||||
return False
|
||||
|
||||
|
||||
class HasInvalidatePrintingOrderPermission(BasePermission):
|
||||
"""自定义权限类,检查用户是否有作废印染订单的权限"""
|
||||
|
||||
message = '您没有权限作废订单'
|
||||
|
||||
def has_permission(self, request, view):
|
||||
if not request.user.is_authenticated:
|
||||
return False
|
||||
return request.user.has_perm('printing.can_invalidate_printingorder')
|
||||
|
||||
|
||||
class HasActivatePrintingOrderPermission(BasePermission):
|
||||
"""自定义权限类,检查用户是否有恢复印染订单的权限"""
|
||||
|
||||
message = '您没有权限恢复订单'
|
||||
|
||||
def has_permission(self, request, view):
|
||||
if not request.user.is_authenticated:
|
||||
return False
|
||||
return request.user.has_perm('printing.can_activate_printingorder')
|
||||
|
||||
|
||||
class PrintingOrderFilterSet(django_filters.FilterSet):
|
||||
"""印染订单过滤器"""
|
||||
customer_name = django_filters.CharFilter(field_name='customer__name', lookup_expr='icontains')
|
||||
customer_phone = django_filters.CharFilter(field_name='customer__phone', lookup_expr='icontains')
|
||||
fabric = django_filters.CharFilter(lookup_expr='icontains')
|
||||
is_urgent = django_filters.BooleanFilter()
|
||||
is_fabric_received = django_filters.BooleanFilter()
|
||||
is_invalid = django_filters.BooleanFilter()
|
||||
area = django_filters.CharFilter(lookup_expr='icontains')
|
||||
outgoing_date_from = django_filters.DateFilter(field_name='outgoing_date', lookup_expr='gte')
|
||||
outgoing_date_to = django_filters.DateFilter(field_name='outgoing_date', lookup_expr='lte')
|
||||
created_date_from = django_filters.DateFilter(field_name='created_at', lookup_expr='gte')
|
||||
created_date_to = django_filters.DateFilter(field_name='created_at', lookup_expr='lte')
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingOrder
|
||||
fields = ['customer', 'is_urgent', 'is_fabric_received', 'is_invalid']
|
||||
|
||||
|
||||
class PrintingOrderViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
印染订单 ViewSet
|
||||
|
||||
提供印染订单的增改查功能(不支持删除)
|
||||
|
||||
list: 获取印染订单列表
|
||||
retrieve: 获取印染订单详情
|
||||
create: 创建印染订单
|
||||
update: 更新印染订单
|
||||
partial_update: 部分更新印染订单
|
||||
|
||||
查询参数:
|
||||
- customer: 客户ID
|
||||
- customer_name: 客户名称(模糊查询)
|
||||
- customer_phone: 客户电话(模糊查询)
|
||||
- fabric: 面料(模糊查询)
|
||||
- is_urgent: 是否紧急(true/false)
|
||||
- is_fabric_received: 布料是否已收(true/false)
|
||||
- is_invalid: 是否作废(true/false)
|
||||
- area: 地区(模糊查询)
|
||||
- outgoing_date_from: 出货日期起始
|
||||
- outgoing_date_to: 出货日期结束
|
||||
- created_date_from: 创建日期起始
|
||||
- created_date_to: 创建日期结束
|
||||
- search: 全文搜索(客户名称、面料、地区、工艺)
|
||||
- ordering: 排序字段
|
||||
"""
|
||||
queryset = models.PrintingOrder.objects.all()
|
||||
permission_classes = [DjangoModelPermissions]
|
||||
pagination_class = LimitOffsetPagination
|
||||
filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
|
||||
filterset_class = PrintingOrderFilterSet
|
||||
search_fields = ['customer__name', 'fabric', 'area', 'craft', 'description']
|
||||
ordering_fields = [
|
||||
'id', 'created_at', 'updated_at', 'outgoing_date',
|
||||
'is_urgent', 'is_fabric_received', 'is_invalid'
|
||||
]
|
||||
ordering = ['-created_at']
|
||||
|
||||
def get_permissions(self):
|
||||
permissions = super().get_permissions() + [IsPrintingFactory()]
|
||||
if self.action == 'invalidate':
|
||||
permissions.append(HasInvalidatePrintingOrderPermission())
|
||||
elif self.action == 'activate':
|
||||
permissions.append(HasActivatePrintingOrderPermission())
|
||||
return permissions
|
||||
|
||||
def get_serializer_class(self):
|
||||
"""根据动作选择序列化器"""
|
||||
if self.action == 'list':
|
||||
return PrintingOrderListSerializer
|
||||
elif self.action in ['create', 'update', 'partial_update']:
|
||||
return PrintingOrderCreateUpdateSerializer
|
||||
else: # retrieve
|
||||
return PrintingOrderDetailSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
"""优化查询"""
|
||||
queryset = super().get_queryset()
|
||||
if self.action in ['list', 'retrieve']:
|
||||
queryset = queryset.select_related('customer')
|
||||
return queryset
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
"""禁用删除操作"""
|
||||
return Response(
|
||||
{'detail': '印染订单不支持删除操作,请使用作废功能'},
|
||||
status=status.HTTP_405_METHOD_NOT_ALLOWED
|
||||
)
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def invalidate(self, request, pk=None):
|
||||
"""
|
||||
作废订单
|
||||
|
||||
需要权限: printing.can_invalidate_printingorder
|
||||
"""
|
||||
# 检查权限
|
||||
if not request.user.has_perm('printing.can_invalidate_printingorder'):
|
||||
return Response(
|
||||
{'detail': '您没有权限作废订单'},
|
||||
status=status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
printing_order = self.get_object()
|
||||
|
||||
if printing_order.is_invalid:
|
||||
return Response(
|
||||
{'detail': '该订单已经作废'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
printing_order.is_invalid = True
|
||||
printing_order.save()
|
||||
|
||||
serializer = PrintingOrderDetailSerializer(printing_order)
|
||||
return Response({
|
||||
'detail': '订单已作废',
|
||||
'data': serializer.data
|
||||
})
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def activate(self, request, pk=None):
|
||||
"""
|
||||
恢复订单
|
||||
|
||||
需要权限: printing.can_activate_printingorder
|
||||
"""
|
||||
# 检查权限
|
||||
if not request.user.has_perm('printing.can_activate_printingorder'):
|
||||
return Response(
|
||||
{'detail': '您没有权限恢复订单'},
|
||||
status=status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
printing_order = self.get_object()
|
||||
|
||||
if not printing_order.is_invalid:
|
||||
return Response(
|
||||
{'detail': '该订单未作废,无需恢复'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
printing_order.is_invalid = False
|
||||
printing_order.save()
|
||||
|
||||
serializer = PrintingOrderDetailSerializer(printing_order)
|
||||
return Response({
|
||||
'detail': '订单已恢复',
|
||||
'data': serializer.data
|
||||
})
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def mark_fabric_received(self, request, pk=None):
|
||||
"""标记布料已收"""
|
||||
printing_order = self.get_object()
|
||||
|
||||
if printing_order.is_fabric_received:
|
||||
return Response(
|
||||
{'detail': '布料已经标记为已收'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
printing_order.is_fabric_received = True
|
||||
printing_order.save()
|
||||
|
||||
serializer = PrintingOrderDetailSerializer(printing_order)
|
||||
return Response({
|
||||
'detail': '已标记布料已收',
|
||||
'data': serializer.data
|
||||
})
|
||||
|
||||
|
||||
class PrintingJobFilterSet(django_filters.FilterSet):
|
||||
"""印染款式明细过滤器"""
|
||||
printing_order = django_filters.NumberFilter()
|
||||
product = django_filters.NumberFilter()
|
||||
product_name = django_filters.CharFilter(field_name='product__name', lookup_expr='icontains')
|
||||
unit = django_filters.CharFilter(lookup_expr='icontains')
|
||||
quantity_min = django_filters.NumberFilter(field_name='quantity', lookup_expr='gte')
|
||||
quantity_max = django_filters.NumberFilter(field_name='quantity', lookup_expr='lte')
|
||||
pieces_min = django_filters.NumberFilter(field_name='pieces', lookup_expr='gte')
|
||||
pieces_max = django_filters.NumberFilter(field_name='pieces', lookup_expr='lte')
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = ['printing_order', 'product']
|
||||
|
||||
|
||||
class PrintingJobViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
印染款式明细 ViewSet
|
||||
|
||||
提供印染款式明细的增改查功能(不支持删除)
|
||||
|
||||
list: 获取款式明细列表
|
||||
retrieve: 获取款式明细详情
|
||||
create: 创建款式明细
|
||||
update: 更新款式明细
|
||||
partial_update: 部分更新款式明细
|
||||
advance_to_next_state: 推进到下一个状态
|
||||
step_back_one_state: 回退一步
|
||||
completed_states: 查询已完成的流程列表
|
||||
timeline: 获取流程时间线
|
||||
|
||||
查询参数:
|
||||
- printing_order: 印染订单ID
|
||||
- product: 产品ID
|
||||
- product_name: 产品名称(模糊查询)
|
||||
- unit: 单位(模糊查询)
|
||||
- quantity_min: 最小数量
|
||||
- quantity_max: 最大数量
|
||||
- pieces_min: 最小件数
|
||||
- pieces_max: 最大件数
|
||||
- search: 全文搜索(产品名称、单位、尺寸、备注)
|
||||
- ordering: 排序字段
|
||||
"""
|
||||
queryset = models.PrintingJob.objects.all()
|
||||
permission_classes = [DjangoModelPermissions, IsPrintingFactory]
|
||||
pagination_class = LimitOffsetPagination
|
||||
filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
|
||||
filterset_class = PrintingJobFilterSet
|
||||
search_fields = ['product__name', 'unit', 'size', 'description']
|
||||
ordering_fields = ['id', 'created_at', 'updated_at', 'quantity', 'pieces']
|
||||
ordering = ['-created_at']
|
||||
|
||||
def get_serializer_class(self):
|
||||
"""根据动作选择序列化器"""
|
||||
if self.action == 'list':
|
||||
return PrintingJobListSerializer
|
||||
elif self.action in ['create', 'update', 'partial_update']:
|
||||
return PrintingJobCreateUpdateSerializer
|
||||
else: # retrieve
|
||||
return PrintingJobDetailSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
"""优化查询"""
|
||||
queryset = super().get_queryset()
|
||||
if self.action in ['list', 'retrieve']:
|
||||
queryset = queryset.select_related('printing_order', 'product')
|
||||
return queryset
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
"""禁用删除操作"""
|
||||
return Response(
|
||||
{'detail': '印染款式明细不支持删除操作'},
|
||||
status=status.HTTP_405_METHOD_NOT_ALLOWED
|
||||
)
|
||||
|
||||
@action(detail=True, methods=['post'], url_path='advance-to-next-state')
|
||||
def advance_to_next_state(self, request, pk=None):
|
||||
"""
|
||||
推进到下一个状态
|
||||
|
||||
将印染任务推进到下一个流程状态
|
||||
"""
|
||||
job = self.get_object()
|
||||
|
||||
if not job.business_object:
|
||||
return Response(
|
||||
{'detail': '该任务没有关联的流程实例'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
from stateflow import services as stateflow_services
|
||||
success, message = stateflow_services.advance_to_next_state(
|
||||
job.business_object, request.user
|
||||
)
|
||||
|
||||
if not success:
|
||||
return Response(
|
||||
{'detail': message},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
# 重新获取 job 以刷新状态
|
||||
job.refresh_from_db()
|
||||
serializer = PrintingJobDetailSerializer(job)
|
||||
|
||||
return Response({
|
||||
'detail': message,
|
||||
'data': serializer.data
|
||||
})
|
||||
|
||||
@action(detail=True, methods=['post'], url_path='step-back-one-state')
|
||||
def step_back_one_state(self, request, pk=None):
|
||||
"""
|
||||
回退一步
|
||||
|
||||
将印染任务回退到上一个流程状态
|
||||
"""
|
||||
job = self.get_object()
|
||||
|
||||
if not job.business_object:
|
||||
return Response(
|
||||
{'detail': '该任务没有关联的流程实例'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
from stateflow import services as stateflow_services
|
||||
success, message = stateflow_services.step_back_one_state(
|
||||
job.business_object, request.user
|
||||
)
|
||||
|
||||
if not success:
|
||||
return Response(
|
||||
{'detail': message},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
# 重新获取 job 以刷新状态
|
||||
job.refresh_from_db()
|
||||
serializer = PrintingJobDetailSerializer(job)
|
||||
|
||||
return Response({
|
||||
'detail': message,
|
||||
'data': serializer.data
|
||||
})
|
||||
|
||||
@action(detail=True, methods=['get'], url_path='completed-states')
|
||||
def completed_states(self, request, pk=None):
|
||||
"""
|
||||
查询已完成的流程列表
|
||||
|
||||
查询参数:
|
||||
- include_cancelled: 是否包含已撤销的流程(true/false),默认 false
|
||||
"""
|
||||
job = self.get_object()
|
||||
|
||||
if not job.business_object:
|
||||
return Response({
|
||||
'count': 0,
|
||||
'results': []
|
||||
})
|
||||
|
||||
# 获取查询参数
|
||||
include_cancelled = request.query_params.get('include_cancelled', 'false').lower() == 'true'
|
||||
|
||||
# 查询已完成的状态
|
||||
state_logs = job.business_object.state_logs.select_related('state', 'completed_by').order_by('completed_at')
|
||||
|
||||
if not include_cancelled:
|
||||
state_logs = state_logs.filter(is_cancelled=False)
|
||||
|
||||
results = []
|
||||
for log in state_logs:
|
||||
results.append({
|
||||
'id': log.id,
|
||||
'state_id': log.state.id,
|
||||
'state_name': log.state.name,
|
||||
'completed_at': log.completed_at,
|
||||
'completed_by': log.completed_by.username if log.completed_by else None,
|
||||
'completed_by_name': getattr(log.completed_by, 'employee', None) and log.completed_by.employee.name or None,
|
||||
'is_cancelled': log.is_cancelled,
|
||||
'cancelled_at': log.cancelled_at,
|
||||
})
|
||||
|
||||
return Response({
|
||||
'count': len(results),
|
||||
'results': results
|
||||
})
|
||||
|
||||
@action(detail=True, methods=['get'], url_path='timeline')
|
||||
def timeline(self, request, pk=None):
|
||||
"""
|
||||
获取流程时间线
|
||||
|
||||
返回该任务的完整流程时间线,包括未开始、进行中和已完成的状态
|
||||
不包含已撤销的流程记录
|
||||
"""
|
||||
job = self.get_object()
|
||||
|
||||
if not job.business_object or not job.printing_order.process:
|
||||
return Response({
|
||||
'count': 0,
|
||||
'results': []
|
||||
})
|
||||
|
||||
from stateflow import services as stateflow_services
|
||||
timeline_data = stateflow_services.get_business_object_state_timeline(job.business_object)
|
||||
|
||||
# 过滤掉已撤销的记录
|
||||
timeline_data = [item for item in timeline_data if not item.get('is_cancelled')]
|
||||
|
||||
results = []
|
||||
for item in timeline_data:
|
||||
results.append({
|
||||
'state_id': item['state'].id,
|
||||
'state_name': item['state'].name,
|
||||
'state_description': item['state'].description,
|
||||
'order': item['order'],
|
||||
'status': item['status'], # not_started, in_progress, completed
|
||||
'completed_at': item['completed_at'],
|
||||
'completed_by': item['completed_by'].username if item['completed_by'] else None,
|
||||
'completed_by_name': getattr(item['completed_by'], 'employee', None) and item['completed_by'].employee.name or None if item['completed_by'] else None,
|
||||
})
|
||||
|
||||
return Response({
|
||||
'count': len(results),
|
||||
'results': results
|
||||
})
|
||||
|
||||
|
||||
class HasInvalidatePlateOrderPermission(BasePermission):
|
||||
"""自定义权限类,检查用户是否有作废开版订单的权限"""
|
||||
|
||||
message = '您没有权限作废开版订单'
|
||||
|
||||
def has_permission(self, request, view):
|
||||
if not request.user.is_authenticated:
|
||||
return False
|
||||
return request.user.has_perm('printing.can_invalidate_plateorder')
|
||||
|
||||
|
||||
class HasActivatePlateOrderPermission(BasePermission):
|
||||
"""自定义权限类,检查用户是否有恢复开版订单的权限"""
|
||||
|
||||
message = '您没有权限恢复开版订单'
|
||||
|
||||
def has_permission(self, request, view):
|
||||
if not request.user.is_authenticated:
|
||||
return False
|
||||
return request.user.has_perm('printing.can_activate_plateorder')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class PlateOrderFilterSet(django_filters.FilterSet):
|
||||
"""开版订单过滤器"""
|
||||
customer_name = django_filters.CharFilter(field_name='customer__name', lookup_expr='icontains')
|
||||
customer_phone = django_filters.CharFilter(field_name='customer__mobile', lookup_expr='icontains')
|
||||
salesperson = django_filters.NumberFilter()
|
||||
merchandiser = django_filters.NumberFilter()
|
||||
plate_type = django_filters.CharFilter(lookup_expr='icontains')
|
||||
urgency_level = django_filters.CharFilter(lookup_expr='icontains')
|
||||
is_invalid = django_filters.BooleanFilter()
|
||||
is_ordered = django_filters.BooleanFilter()
|
||||
is_mark_frame = django_filters.BooleanFilter()
|
||||
fabric = django_filters.CharFilter(lookup_expr='icontains')
|
||||
style_name = django_filters.CharFilter(lookup_expr='icontains')
|
||||
plate_date_from = django_filters.DateFilter(field_name='plate_date', lookup_expr='gte')
|
||||
plate_date_to = django_filters.DateFilter(field_name='plate_date', lookup_expr='lte')
|
||||
required_completion_date_from = django_filters.DateFilter(field_name='required_completion_date', lookup_expr='gte')
|
||||
required_completion_date_to = django_filters.DateFilter(field_name='required_completion_date', lookup_expr='lte')
|
||||
created_date_from = django_filters.DateFilter(field_name='created_at', lookup_expr='gte')
|
||||
created_date_to = django_filters.DateFilter(field_name='created_at', lookup_expr='lte')
|
||||
|
||||
class Meta:
|
||||
model = models.PlateOrder
|
||||
fields = ['customer', 'salesperson', 'merchandiser', 'urgency_level', 'is_invalid', 'is_ordered']
|
||||
|
||||
|
||||
class PlateOrderViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
开版订单 ViewSet
|
||||
|
||||
提供开版订单的增改查功能(不支持删除)
|
||||
|
||||
list: 获取开版订单列表
|
||||
retrieve: 获取开版订单详情
|
||||
create: 创建开版订单
|
||||
update: 更新开版订单
|
||||
partial_update: 部分更新开版订单
|
||||
invalidate: 作废开版订单
|
||||
activate: 恢复开版订单
|
||||
|
||||
查询参数:
|
||||
- customer: 客户ID
|
||||
- customer_name: 客户名称(模糊查询)
|
||||
- customer_phone: 客户电话(模糊查询)
|
||||
- salesperson: 业务员ID
|
||||
- merchandiser: 跟单员ID
|
||||
- plate_type: 版型(模糊查询)
|
||||
- urgency_level: 紧急程度(1-5)
|
||||
- is_invalid: 是否作废(true/false)
|
||||
- is_ordered: 是否已下单(true/false)
|
||||
- is_mark_frame: 是否打样架(true/false)
|
||||
- fabric: 面料(模糊查询)
|
||||
- style_name: 款式名称(模糊查询)
|
||||
- plate_date_from: 开版日期起始
|
||||
- plate_date_to: 开版日期结束
|
||||
- required_completion_date_from: 要求完成日期起始
|
||||
- required_completion_date_to: 要求完成日期结束
|
||||
- created_date_from: 创建日期起始
|
||||
- created_date_to: 创建日期结束
|
||||
- search: 全文搜索(设计编号、款式名称、客户名称、面料)
|
||||
- ordering: 排序字段
|
||||
"""
|
||||
queryset = models.PlateOrder.objects.all()
|
||||
permission_classes = [DjangoModelPermissions]
|
||||
pagination_class = LimitOffsetPagination
|
||||
filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
|
||||
filterset_class = PlateOrderFilterSet
|
||||
search_fields = ['design_code', 'style_name', 'customer__name', 'fabric']
|
||||
ordering_fields = [
|
||||
'id', 'created_at', 'updated_at', 'plate_date',
|
||||
'required_completion_date', 'completion_date',
|
||||
'urgency_level', 'is_ordered', 'is_invalid'
|
||||
]
|
||||
ordering = ['-created_at']
|
||||
|
||||
def get_serializer_class(self):
|
||||
"""根据动作选择序列化器"""
|
||||
from .serializers import (
|
||||
PlateOrderListSerializer,
|
||||
PlateOrderDetailSerializer,
|
||||
PlateOrderCreateUpdateSerializer,
|
||||
)
|
||||
|
||||
if self.action == 'list':
|
||||
return PlateOrderListSerializer
|
||||
elif self.action in ['create', 'update', 'partial_update']:
|
||||
return PlateOrderCreateUpdateSerializer
|
||||
else: # retrieve
|
||||
return PlateOrderDetailSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
"""优化查询"""
|
||||
queryset = super().get_queryset()
|
||||
if self.action in ['list', 'retrieve']:
|
||||
queryset = queryset.select_related('customer', 'salesperson', 'merchandiser', 'business_object')
|
||||
return queryset
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
"""禁用删除操作"""
|
||||
return Response(
|
||||
{'detail': '开版订单不支持删除操作,请使用作废功能'},
|
||||
status=status.HTTP_405_METHOD_NOT_ALLOWED
|
||||
)
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def invalidate(self, request, pk=None):
|
||||
"""
|
||||
作废开版订单
|
||||
|
||||
需要权限: printing.can_invalidate_plateorder
|
||||
"""
|
||||
# 检查权限
|
||||
if not request.user.has_perm('printing.can_invalidate_plateorder'):
|
||||
return Response(
|
||||
{'detail': '您没有权限作废开版订单'},
|
||||
status=status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
plate_order = self.get_object()
|
||||
|
||||
if plate_order.is_invalid:
|
||||
return Response(
|
||||
{'detail': '该开版订单已经作废'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
plate_order.is_invalid = True
|
||||
plate_order.save()
|
||||
|
||||
from .serializers import PlateOrderDetailSerializer
|
||||
serializer = PlateOrderDetailSerializer(plate_order, context={'request': request})
|
||||
return Response({
|
||||
'detail': '开版订单已作废',
|
||||
'data': serializer.data
|
||||
})
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def activate(self, request, pk=None):
|
||||
"""
|
||||
恢复开版订单
|
||||
|
||||
需要权限: printing.can_activate_plateorder
|
||||
"""
|
||||
# 检查权限
|
||||
if not request.user.has_perm('printing.can_activate_plateorder'):
|
||||
return Response(
|
||||
{'detail': '您没有权限恢复开版订单'},
|
||||
status=status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
plate_order = self.get_object()
|
||||
|
||||
if not plate_order.is_invalid:
|
||||
return Response(
|
||||
{'detail': '该开版订单未作废,无需恢复'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
plate_order.is_invalid = False
|
||||
plate_order.save()
|
||||
|
||||
from .serializers import PlateOrderDetailSerializer
|
||||
serializer = PlateOrderDetailSerializer(plate_order, context={'request': request})
|
||||
return Response({
|
||||
'detail': '开版订单已恢复',
|
||||
'data': serializer.data
|
||||
})
|
||||
@@ -135,6 +135,26 @@ class BusinessObjectViewSet(viewsets.ModelViewSet):
|
||||
'message': message
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def step_back(self, request, pk=None):
|
||||
"""回退一步(撤销最后一次完成的状态)"""
|
||||
business_object = self.get_object()
|
||||
user = request.user
|
||||
|
||||
success, message = services.step_back_one_state(business_object, user)
|
||||
|
||||
if success:
|
||||
return Response({
|
||||
'success': True,
|
||||
'message': message,
|
||||
'business_object': BusinessObjectDetailSerializer(business_object).data
|
||||
})
|
||||
else:
|
||||
return Response({
|
||||
'success': False,
|
||||
'message': message
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def reset(self, request, pk=None):
|
||||
"""重置进度"""
|
||||
|
||||
@@ -242,3 +242,6 @@ MEDIA_URL = f'http://{QINIU_BUCKET_DOMAIN}/media/'
|
||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||
|
||||
# Printing module settings
|
||||
PRINTING_DEFAULT_PROCESS_ID = 12 # 默认印染流程ID
|
||||
|
||||
@@ -1,9 +1,199 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib.admin import action
|
||||
from django.forms import BaseInlineFormSet
|
||||
from django.utils.html import format_html
|
||||
from . import models
|
||||
|
||||
|
||||
@admin.register(models.PlateOrder)
|
||||
class PlateOrderAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
'id',
|
||||
'design_code',
|
||||
'customer_name',
|
||||
'style_name',
|
||||
'plate_type',
|
||||
'development_status_display',
|
||||
'urgency_display',
|
||||
'progress_display',
|
||||
'salesperson_name',
|
||||
'required_completion_date',
|
||||
'created_at',
|
||||
)
|
||||
list_filter = (
|
||||
'urgency_level',
|
||||
'plate_type',
|
||||
'is_ordered',
|
||||
'is_mark_frame',
|
||||
'created_at',
|
||||
'required_completion_date',
|
||||
)
|
||||
search_fields = (
|
||||
'design_code',
|
||||
'style_name',
|
||||
'customer__name',
|
||||
'salesperson__name',
|
||||
'merchandiser__name',
|
||||
)
|
||||
readonly_fields = ('progress_display', 'status_display')
|
||||
actions = ['advance_to_next_state_action', 'step_back_one_state_action', 'reset_progress_action']
|
||||
autocomplete_fields = ['customer', 'salesperson', 'merchandiser']
|
||||
|
||||
fieldsets = [
|
||||
('基本信息', {
|
||||
'fields': ['design_code', 'plate_type', 'plate_date', 'urgency_level']
|
||||
}),
|
||||
('客户信息', {
|
||||
'fields': ['customer', 'area', 'default_address', 'salesperson', 'merchandiser']
|
||||
}),
|
||||
('产品信息', {
|
||||
'fields': ['style_name', 'fabric', 'width', 'production_method']
|
||||
}),
|
||||
('开版信息', {
|
||||
'fields': [
|
||||
'plate_method', 'plate_image', 'plate_notes', 'reprint_reason',
|
||||
'is_mark_frame', 'sample_meter', 'required_sample_meters'
|
||||
]
|
||||
}),
|
||||
('质量评级', {
|
||||
'fields': [
|
||||
'drawing_rating', 'color_matching_rating',
|
||||
'sample_rating', 'difficulty_rating'
|
||||
]
|
||||
}),
|
||||
('时间管理', {
|
||||
'fields': ['required_completion_date', 'completion_date']
|
||||
}),
|
||||
('审批与反馈', {
|
||||
'fields': [
|
||||
'approval_result', 'customer_feedback',
|
||||
'is_ordered'
|
||||
]
|
||||
}),
|
||||
('流程管理', {
|
||||
'fields': ['business_object', 'status_display', 'progress_display'],
|
||||
'classes': ['collapse']
|
||||
}),
|
||||
]
|
||||
|
||||
@admin.display(description='客户')
|
||||
def customer_name(self, obj):
|
||||
return obj.customer.name if obj.customer else '-'
|
||||
|
||||
@admin.display(description='销售员')
|
||||
def salesperson_name(self, obj):
|
||||
return obj.salesperson.name if obj.salesperson else '-'
|
||||
|
||||
@admin.display(description='开发进程')
|
||||
def development_status_display(self, obj):
|
||||
status = obj.status
|
||||
color_map = {
|
||||
'未打印': 'gray',
|
||||
'待画图': 'orange',
|
||||
'画图完成': 'blue',
|
||||
'调色样': 'purple',
|
||||
'调色完成': 'green',
|
||||
'套纸样': 'cyan',
|
||||
'取消版': 'red',
|
||||
'客户审批': 'yellow',
|
||||
'开版完成': 'darkgreen',
|
||||
'已下单': 'black',
|
||||
}
|
||||
color = color_map.get(status, 'black')
|
||||
return format_html(
|
||||
'<span style="color: {}; font-weight: bold;">{}</span>',
|
||||
color, status
|
||||
)
|
||||
|
||||
@admin.display(description='紧急程度')
|
||||
def urgency_display(self, obj):
|
||||
if obj.urgency_level == '加急':
|
||||
return format_html(
|
||||
'<span style="color: red; font-weight: bold;">⚠️ 加急</span>'
|
||||
)
|
||||
return obj.urgency_level
|
||||
|
||||
@admin.display(description='进度')
|
||||
def progress_display(self, obj):
|
||||
progress = obj.progress_percentage
|
||||
if progress >= 100:
|
||||
color = 'green'
|
||||
elif progress >= 50:
|
||||
color = 'orange'
|
||||
else:
|
||||
color = 'gray'
|
||||
return format_html(
|
||||
'<span style="color: {}; font-weight: bold;">{:.1f}%</span>',
|
||||
color, progress
|
||||
)
|
||||
|
||||
@admin.display(description='当前状态')
|
||||
def status_display(self, obj):
|
||||
return obj.status
|
||||
|
||||
@action(description='推进到下一个状态')
|
||||
def advance_to_next_state_action(self, request, queryset):
|
||||
from stateflow import services
|
||||
success_count = 0
|
||||
messages = []
|
||||
|
||||
for obj in queryset:
|
||||
if not obj.business_object:
|
||||
messages.append(f"{obj.plate_code}: 没有关联的流程实例,无法推进")
|
||||
continue
|
||||
|
||||
success, message = services.advance_to_next_state(obj.business_object, request.user)
|
||||
if success:
|
||||
success_count += 1
|
||||
messages.append(f"{obj.plate_code}: {message}")
|
||||
|
||||
self.message_user(request, f'成功推进 {success_count}/{queryset.count()} 个开版订单。详情: {"; ".join(messages[:5])}')
|
||||
|
||||
@action(description='回退一步')
|
||||
def step_back_one_state_action(self, request, queryset):
|
||||
from stateflow import services
|
||||
success_count = 0
|
||||
messages = []
|
||||
|
||||
for obj in queryset:
|
||||
if not obj.business_object:
|
||||
messages.append(f"{obj.plate_code}: 没有关联的流程实例,无法回退")
|
||||
continue
|
||||
|
||||
success, message = services.step_back_one_state(obj.business_object, request.user)
|
||||
if success:
|
||||
success_count += 1
|
||||
messages.append(f"{obj.plate_code}: {message}")
|
||||
|
||||
self.message_user(request, f'成功回退 {success_count}/{queryset.count()} 个开版订单。详情: {"; ".join(messages[:5])}')
|
||||
|
||||
@action(description='重置进度')
|
||||
def reset_progress_action(self, request, queryset):
|
||||
from stateflow import services
|
||||
reset_count = 0
|
||||
|
||||
for obj in queryset:
|
||||
if not obj.business_object:
|
||||
continue
|
||||
|
||||
services.reset_business_object_progress(obj.business_object)
|
||||
reset_count += 1
|
||||
|
||||
self.message_user(request, f'已重置 {reset_count} 个开版订单的进度。')
|
||||
|
||||
|
||||
class PrintingJobInlineFormSet(BaseInlineFormSet):
|
||||
"""自定义 formset 处理 PrintingJob 的创建"""
|
||||
|
||||
def save_new(self, form, commit=True):
|
||||
"""保存新的 PrintingJob(暂时不创建 BusinessObject,等 order 保存完成后再创建)"""
|
||||
obj = super().save_new(form, commit=commit)
|
||||
return obj
|
||||
|
||||
|
||||
class PrintingJobInline(admin.StackedInline):
|
||||
model = models.PrintingJob
|
||||
formset = PrintingJobInlineFormSet
|
||||
extra = 1
|
||||
|
||||
|
||||
@@ -12,11 +202,93 @@ class PrintingOrderAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
'oid',
|
||||
'customer_name',
|
||||
'progress_percent',
|
||||
'created_at',
|
||||
'outgoing_date',
|
||||
)
|
||||
inlines = [PrintingJobInline]
|
||||
|
||||
def save_formset(self, request, form, formset, change):
|
||||
"""保存 formset 后,为新创建的 PrintingJob 创建 BusinessObject"""
|
||||
# 先调用默认的保存
|
||||
formset.save()
|
||||
|
||||
# 此时 PrintingOrder 已经保存完成(包括 process 字段)
|
||||
# 为所有新创建且没有 business_object 的 job 创建 BusinessObject
|
||||
if isinstance(formset.model, type) and issubclass(formset.model, models.PrintingJob):
|
||||
order = form.instance
|
||||
for job in order.printing_jobs.all():
|
||||
if not job.business_object and order.process:
|
||||
from stateflow.models import BusinessObject
|
||||
business_object = BusinessObject.objects.create(
|
||||
name=f"PrintingJob-{job.id}",
|
||||
process=order.process,
|
||||
description=f"印染任务 {job.id} 的流程实例",
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save(update_fields=['business_object'])
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
"""保存 PrintingOrder 时使用 service 层"""
|
||||
from api_v1.views.printing.services import PrintingOrderService
|
||||
|
||||
if not change: # 新创建
|
||||
# 使用 service 创建(会自动设置默认 process)
|
||||
data = {
|
||||
'customer': obj.customer,
|
||||
'fabric': obj.fabric,
|
||||
'width': obj.width,
|
||||
'is_urgent': obj.is_urgent,
|
||||
'area': obj.area,
|
||||
'address': obj.address,
|
||||
'fabric_source': obj.fabric_source,
|
||||
'is_fabric_received': obj.is_fabric_received,
|
||||
'craft': obj.craft,
|
||||
'description': obj.description,
|
||||
'outgoing_date': obj.outgoing_date,
|
||||
'curve': obj.curve,
|
||||
'new_curve': obj.new_curve,
|
||||
'position': obj.position,
|
||||
'printing_warn': obj.printing_warn,
|
||||
'rolling_warn': obj.rolling_warn,
|
||||
'production_warn': obj.production_warn,
|
||||
'is_invalid': obj.is_invalid,
|
||||
'process': obj.process,
|
||||
}
|
||||
created_obj = PrintingOrderService.create_printing_order(data, request.user)
|
||||
# 将创建的对象的所有字段复制到当前对象
|
||||
for field in created_obj._meta.fields:
|
||||
setattr(obj, field.name, getattr(created_obj, field.name))
|
||||
else: # 更新
|
||||
# 使用 service 更新
|
||||
data = {
|
||||
'customer': obj.customer,
|
||||
'fabric': obj.fabric,
|
||||
'width': obj.width,
|
||||
'is_urgent': obj.is_urgent,
|
||||
'area': obj.area,
|
||||
'address': obj.address,
|
||||
'fabric_source': obj.fabric_source,
|
||||
'is_fabric_received': obj.is_fabric_received,
|
||||
'craft': obj.craft,
|
||||
'description': obj.description,
|
||||
'outgoing_date': obj.outgoing_date,
|
||||
'curve': obj.curve,
|
||||
'new_curve': obj.new_curve,
|
||||
'position': obj.position,
|
||||
'printing_warn': obj.printing_warn,
|
||||
'rolling_warn': obj.rolling_warn,
|
||||
'production_warn': obj.production_warn,
|
||||
'is_invalid': obj.is_invalid,
|
||||
'process': obj.process,
|
||||
}
|
||||
success, message, updated_obj = PrintingOrderService.update_printing_order(
|
||||
obj, data, request.user
|
||||
)
|
||||
if not success:
|
||||
from django.core.exceptions import ValidationError
|
||||
raise ValidationError(message)
|
||||
|
||||
@admin.display(description='客户名称')
|
||||
def customer_name(self, obj: models.PrintingOrder):
|
||||
return obj.customer.name
|
||||
@@ -24,3 +296,140 @@ class PrintingOrderAdmin(admin.ModelAdmin):
|
||||
@admin.display(description='订单编号(虚拟)')
|
||||
def oid(self, obj: models.PrintingOrder):
|
||||
return obj.human_id
|
||||
|
||||
@admin.display(description='进度')
|
||||
def progress_percent(self, obj: models.PrintingOrder):
|
||||
return f"{obj.progress}%"
|
||||
|
||||
|
||||
@admin.register(models.PrintingJob)
|
||||
class PrintingJobAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
'id',
|
||||
'printing_order',
|
||||
'customer_name',
|
||||
'product',
|
||||
'quantity',
|
||||
'unit',
|
||||
'size',
|
||||
'pieces',
|
||||
'business_object',
|
||||
'current_progress',
|
||||
'execution_status',
|
||||
'created_at',
|
||||
)
|
||||
search_fields = ('printing_order__human_id', 'product__name')
|
||||
list_filter = ('created_at', 'printing_order__customer__name')
|
||||
actions = ['advance_to_next_state_action', 'step_back_one_state_action', 'reset_progress_action']
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
"""保存 PrintingJob 时使用 service 层"""
|
||||
from api_v1.views.printing.services import PrintingJobService
|
||||
|
||||
if not change: # 新创建
|
||||
# 使用 service 创建(这样会自动创建 BusinessObject)
|
||||
data = {
|
||||
'printing_order': obj.printing_order,
|
||||
'product': obj.product,
|
||||
'quantity': obj.quantity,
|
||||
'unit': obj.unit,
|
||||
'size': obj.size,
|
||||
'pieces': obj.pieces,
|
||||
'description': obj.description,
|
||||
}
|
||||
created_obj = PrintingJobService.create_printing_job(data, request.user)
|
||||
# 将创建的对象的 ID 赋值给当前对象(这样 admin 可以正确跳转)
|
||||
obj.pk = created_obj.pk
|
||||
obj.id = created_obj.id
|
||||
else: # 更新
|
||||
# 使用 service 更新
|
||||
data = {
|
||||
'printing_order': obj.printing_order,
|
||||
'product': obj.product,
|
||||
'quantity': obj.quantity,
|
||||
'unit': obj.unit,
|
||||
'size': obj.size,
|
||||
'pieces': obj.pieces,
|
||||
'description': obj.description,
|
||||
}
|
||||
success, message, updated_obj = PrintingJobService.update_printing_job(
|
||||
obj, data, request.user
|
||||
)
|
||||
if not success:
|
||||
from django.core.exceptions import ValidationError
|
||||
raise ValidationError(message)
|
||||
|
||||
@admin.display(description='客户名称')
|
||||
def customer_name(self, obj: models.PrintingJob):
|
||||
return obj.printing_order.customer.name
|
||||
|
||||
@admin.display(description='当前进度')
|
||||
def current_progress(self, obj: models.PrintingJob):
|
||||
"""显示该 job 自己的进度(已完成节点数 / 总节点数)"""
|
||||
if not obj.business_object or not obj.printing_order.process:
|
||||
return "0%"
|
||||
|
||||
total_nodes = obj.business_object.process.process_nodes.count()
|
||||
if total_nodes == 0:
|
||||
return "0%"
|
||||
|
||||
completed_nodes = obj.business_object.state_logs.filter(is_cancelled=False).count()
|
||||
progress = int((completed_nodes / total_nodes) * 100)
|
||||
return f"{progress}%"
|
||||
|
||||
@admin.display(description='执行状态')
|
||||
def execution_status(self, obj: models.PrintingJob):
|
||||
return obj.status
|
||||
|
||||
@action(description='前进到下一个状态')
|
||||
def advance_to_next_state_action(self, request, queryset):
|
||||
from stateflow import services
|
||||
success_count = 0
|
||||
messages = []
|
||||
|
||||
for obj in queryset:
|
||||
# 检查是否有 business_object
|
||||
if not obj.business_object:
|
||||
messages.append(f"PrintingJob #{obj.id}: 没有关联的业务对象,无法推进")
|
||||
continue
|
||||
|
||||
success, message = services.advance_to_next_state(obj.business_object, request.user)
|
||||
if success:
|
||||
success_count += 1
|
||||
messages.append(f"PrintingJob #{obj.id}: {message}")
|
||||
|
||||
self.message_user(request, f'成功推进 {success_count}/{queryset.count()} 个印染任务。详情: {"; ".join(messages[:5])}')
|
||||
|
||||
@action(description='回退一步')
|
||||
def step_back_one_state_action(self, request, queryset):
|
||||
from stateflow import services
|
||||
success_count = 0
|
||||
messages = []
|
||||
|
||||
for obj in queryset:
|
||||
# 检查是否有 business_object
|
||||
if not obj.business_object:
|
||||
messages.append(f"PrintingJob #{obj.id}: 没有关联的业务对象,无法回退")
|
||||
continue
|
||||
|
||||
success, message = services.step_back_one_state(obj.business_object, request.user)
|
||||
if success:
|
||||
success_count += 1
|
||||
messages.append(f"PrintingJob #{obj.id}: {message}")
|
||||
|
||||
self.message_user(request, f'成功回退 {success_count}/{queryset.count()} 个印染任务。详情: {"; ".join(messages[:5])}')
|
||||
|
||||
@action(description='重置进度')
|
||||
def reset_progress_action(self, request, queryset):
|
||||
from stateflow import services
|
||||
reset_count = 0
|
||||
|
||||
for obj in queryset:
|
||||
# 检查是否有 business_object
|
||||
if not obj.business_object:
|
||||
continue
|
||||
|
||||
services.reset_business_object_progress(obj.business_object)
|
||||
reset_count += 1
|
||||
|
||||
self.message_user(request, f'已重置 {reset_count} 个印染任务的进度。')
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-13 02:09
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('printing', '0005_alter_printingorder_options'),
|
||||
('stateflow', '0014_alter_process_options'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='printingjob',
|
||||
name='business_object',
|
||||
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='printing_job', to='stateflow.businessobject', verbose_name='流程实例'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='printingorder',
|
||||
name='process',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='printing_orders', to='stateflow.process', verbose_name='印染流程'),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,28 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-13 02:46
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('printing', '0006_printingjob_business_object_printingorder_process'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='printingjob',
|
||||
name='description',
|
||||
field=models.TextField(blank=True, null=True, verbose_name='备注'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='printingjob',
|
||||
name='pieces',
|
||||
field=models.PositiveIntegerField(blank=True, null=True, verbose_name='件数'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='printingjob',
|
||||
name='size',
|
||||
field=models.CharField(blank=True, max_length=100, null=True, verbose_name='一段尺寸'),
|
||||
),
|
||||
]
|
||||
61
printing/migrations/0008_plateorder.py
Normal file
61
printing/migrations/0008_plateorder.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-13 08:58
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('basic_info', '0009_product_minimum_quantity'),
|
||||
('printing', '0007_alter_printingjob_description_and_more'),
|
||||
('stateflow', '0015_stateparameter_attachment'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='PlateOrder',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
|
||||
('updated_at', models.DateTimeField(auto_now=True, verbose_name='更新时间')),
|
||||
('plate_code', models.CharField(max_length=50, unique=True, verbose_name='版单编号')),
|
||||
('design_code', models.CharField(blank=True, max_length=50, null=True, verbose_name='设计编号')),
|
||||
('plate_type', models.CharField(blank=True, max_length=20, null=True, verbose_name='起版情况')),
|
||||
('plate_date', models.DateTimeField(blank=True, null=True, verbose_name='下版时间')),
|
||||
('plate_method', models.CharField(blank=True, max_length=50, null=True, verbose_name='开版方式')),
|
||||
('plate_image', models.FileField(blank=True, null=True, upload_to='plate_images/', verbose_name='开版图')),
|
||||
('plate_notes', models.TextField(blank=True, null=True, verbose_name='打版注意事项')),
|
||||
('reprint_reason', models.TextField(blank=True, null=True, verbose_name='复版原因')),
|
||||
('development_status', models.CharField(blank=True, max_length=20, null=True, verbose_name='开发进程')),
|
||||
('urgency_level', models.CharField(default='正常', max_length=20, verbose_name='紧急程度')),
|
||||
('area', models.CharField(blank=True, max_length=255, null=True, verbose_name='区域')),
|
||||
('default_address', models.CharField(blank=True, max_length=500, null=True, verbose_name='默认地址')),
|
||||
('is_mark_frame', models.BooleanField(default=False, verbose_name='是否套唛架')),
|
||||
('drawing_rating', models.CharField(blank=True, max_length=20, null=True, verbose_name='画图评级')),
|
||||
('color_matching_rating', models.CharField(blank=True, max_length=20, null=True, verbose_name='调色评级')),
|
||||
('sample_rating', models.CharField(blank=True, max_length=20, null=True, verbose_name='套样评级')),
|
||||
('difficulty_rating', models.CharField(blank=True, max_length=20, null=True, verbose_name='难度评级')),
|
||||
('required_completion_date', models.DateField(blank=True, null=True, verbose_name='要求完成时间')),
|
||||
('completion_date', models.DateTimeField(blank=True, null=True, verbose_name='完成时间')),
|
||||
('fabric', models.CharField(blank=True, max_length=100, null=True, verbose_name='布料')),
|
||||
('width', models.CharField(blank=True, max_length=50, null=True, verbose_name='幅宽')),
|
||||
('style_name', models.CharField(blank=True, max_length=100, null=True, verbose_name='款号名称')),
|
||||
('production_method', models.CharField(blank=True, max_length=50, null=True, verbose_name='做货方式')),
|
||||
('sample_meter', models.CharField(blank=True, max_length=100, null=True, verbose_name='米样')),
|
||||
('required_sample_meters', models.DecimalField(blank=True, decimal_places=2, max_digits=10, null=True, verbose_name='客户要求米样米数')),
|
||||
('approval_result', models.CharField(blank=True, max_length=50, null=True, verbose_name='审批结果')),
|
||||
('is_ordered', models.BooleanField(default=False, verbose_name='是否已下单')),
|
||||
('customer_feedback', models.TextField(blank=True, null=True, verbose_name='客户修改意见')),
|
||||
('business_object', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='plate_order', to='stateflow.businessobject', verbose_name='流程实例')),
|
||||
('customer', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='plate_orders', to='basic_info.customer', verbose_name='客户')),
|
||||
('merchandiser', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='plate_orders_as_merchandiser', to='basic_info.employee', verbose_name='跟单员')),
|
||||
('salesperson', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='plate_orders_as_salesperson', to='basic_info.employee', verbose_name='销售员')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': '开版订单',
|
||||
'verbose_name_plural': '开版订单',
|
||||
'ordering': ['-created_at'],
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-13 09:18
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('printing', '0008_plateorder'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='plateorder',
|
||||
name='development_status',
|
||||
),
|
||||
]
|
||||
17
printing/migrations/0010_remove_plateorder_plate_code.py
Normal file
17
printing/migrations/0010_remove_plateorder_plate_code.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-13 09:44
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('printing', '0009_remove_plateorder_development_status'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='plateorder',
|
||||
name='plate_code',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,22 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-13 09:52
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('printing', '0010_remove_plateorder_plate_code'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='plateorder',
|
||||
options={'ordering': ['-created_at'], 'permissions': [('can_invalidate_plateorder', '可以作废开版订单'), ('can_activate_plateorder', '可以恢复开版订单')], 'verbose_name': '开版订单', 'verbose_name_plural': '开版订单'},
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='plateorder',
|
||||
name='is_invalid',
|
||||
field=models.BooleanField(default=False, verbose_name='是否作废'),
|
||||
),
|
||||
]
|
||||
@@ -1,6 +1,171 @@
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
from flower.common import ModelBase
|
||||
from basic_info import models as basic_models
|
||||
from stateflow import models as stateflow_models
|
||||
|
||||
|
||||
class PlateOrder(ModelBase):
|
||||
"""开版管理订单"""
|
||||
|
||||
# 自动编号相关
|
||||
design_code = models.CharField(max_length=50, blank=True, null=True, verbose_name='设计编号')
|
||||
|
||||
# 版相关信息
|
||||
plate_type = models.CharField(max_length=20, blank=True, null=True, verbose_name='起版情况') # 首版/复版等
|
||||
plate_date = models.DateTimeField(null=True, blank=True, verbose_name='下版时间')
|
||||
plate_method = models.CharField(max_length=50, blank=True, null=True, verbose_name='开版方式')
|
||||
plate_image = models.FileField(upload_to='plate_images/', null=True, blank=True, verbose_name='开版图')
|
||||
plate_notes = models.TextField(blank=True, null=True, verbose_name='打版注意事项')
|
||||
reprint_reason = models.TextField(blank=True, null=True, verbose_name='复版原因')
|
||||
|
||||
# 状态和进程
|
||||
# development_status = models.CharField(max_length=20, blank=True, null=True, verbose_name='开发进程') # 未打印/待画图等
|
||||
urgency_level = models.CharField(max_length=20, default='正常', verbose_name='紧急程度') # 正常/加急
|
||||
|
||||
# 人员关联
|
||||
salesperson = models.ForeignKey(
|
||||
basic_models.Employee,
|
||||
on_delete=models.PROTECT,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='plate_orders_as_salesperson',
|
||||
verbose_name='销售员'
|
||||
)
|
||||
merchandiser = models.ForeignKey(
|
||||
basic_models.Employee,
|
||||
on_delete=models.PROTECT,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='plate_orders_as_merchandiser',
|
||||
verbose_name='跟单员'
|
||||
)
|
||||
|
||||
# 客户和地址信息
|
||||
customer = models.ForeignKey(
|
||||
basic_models.Customer,
|
||||
on_delete=models.PROTECT,
|
||||
related_name='plate_orders',
|
||||
verbose_name='客户'
|
||||
)
|
||||
area = models.CharField(max_length=255, blank=True, null=True, verbose_name='区域')
|
||||
default_address = models.CharField(max_length=500, blank=True, null=True, verbose_name='默认地址')
|
||||
|
||||
# 质量评级
|
||||
is_mark_frame = models.BooleanField(default=False, verbose_name='是否套唛架')
|
||||
drawing_rating = models.CharField(max_length=20, blank=True, null=True, verbose_name='画图评级')
|
||||
color_matching_rating = models.CharField(max_length=20, blank=True, null=True, verbose_name='调色评级')
|
||||
sample_rating = models.CharField(max_length=20, blank=True, null=True, verbose_name='套样评级')
|
||||
difficulty_rating = models.CharField(max_length=20, blank=True, null=True, verbose_name='难度评级')
|
||||
|
||||
# 时间相关
|
||||
required_completion_date = models.DateField(null=True, blank=True, verbose_name='要求完成时间')
|
||||
completion_date = models.DateTimeField(null=True, blank=True, verbose_name='完成时间')
|
||||
|
||||
# 产品信息
|
||||
fabric = models.CharField(max_length=100, blank=True, null=True, verbose_name='布料')
|
||||
width = models.CharField(max_length=50, blank=True, null=True, verbose_name='幅宽')
|
||||
style_name = models.CharField(max_length=100, blank=True, null=True, verbose_name='款号名称')
|
||||
|
||||
# 生产方式
|
||||
production_method = models.CharField(max_length=50, blank=True, null=True, verbose_name='做货方式')
|
||||
|
||||
# 样品相关
|
||||
sample_meter = models.CharField(max_length=100, blank=True, null=True, verbose_name='米样')
|
||||
required_sample_meters = models.DecimalField(
|
||||
max_digits=10,
|
||||
decimal_places=2,
|
||||
null=True,
|
||||
blank=True,
|
||||
verbose_name='客户要求米样米数'
|
||||
)
|
||||
|
||||
# 审批和订单状态
|
||||
approval_result = models.CharField(max_length=50, blank=True, null=True, verbose_name='审批结果')
|
||||
is_ordered = models.BooleanField(default=False, verbose_name='是否已下单')
|
||||
is_invalid = models.BooleanField(default=False, verbose_name='是否作废')
|
||||
customer_feedback = models.TextField(blank=True, null=True, verbose_name='客户修改意见')
|
||||
|
||||
# 流程管理
|
||||
business_object = models.OneToOneField(
|
||||
stateflow_models.BusinessObject,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='plate_order',
|
||||
verbose_name='流程实例',
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return self.human_id or f'PlateOrder-{self.id}'
|
||||
|
||||
class Meta:
|
||||
verbose_name = '开版订单'
|
||||
verbose_name_plural = '开版订单'
|
||||
ordering = ['-created_at']
|
||||
permissions = [
|
||||
('can_invalidate_plateorder', '可以作废开版订单'),
|
||||
('can_activate_plateorder', '可以恢复开版订单'),
|
||||
]
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
"""返回当前状态名称"""
|
||||
if not self.business_object:
|
||||
return '未开始'
|
||||
|
||||
current_state = self.business_object.get_current_state()
|
||||
|
||||
# current_state 为 None 有两种情况:未开始或已完成
|
||||
if current_state is None:
|
||||
# 检查是否有完成记录来区分
|
||||
has_completed_records = self.business_object.state_logs.filter(is_cancelled=False).exists()
|
||||
if has_completed_records:
|
||||
return '已完成'
|
||||
else:
|
||||
return '未开始'
|
||||
|
||||
return current_state.name
|
||||
|
||||
@property
|
||||
def status_id(self):
|
||||
"""返回当前状态ID"""
|
||||
if not self.business_object:
|
||||
return None
|
||||
current_state = self.business_object.get_current_state()
|
||||
return current_state.id if current_state else None
|
||||
|
||||
@property
|
||||
def is_completed(self) -> bool:
|
||||
"""判断是否完成"""
|
||||
if not self.business_object:
|
||||
return False
|
||||
|
||||
# 检查流程是否完成
|
||||
from stateflow.services import get_overall_status
|
||||
return get_overall_status(self.business_object) == 'completed'
|
||||
|
||||
@property
|
||||
def has_started(self) -> bool:
|
||||
"""判断是否已开始"""
|
||||
if not self.business_object:
|
||||
return False
|
||||
|
||||
# 检查是否有状态流转记录
|
||||
from stateflow.models import StateFlowRecord
|
||||
has_records = StateFlowRecord.objects.filter(
|
||||
business_object=self.business_object
|
||||
).exists()
|
||||
|
||||
return has_records
|
||||
|
||||
@property
|
||||
def progress_percentage(self) -> float:
|
||||
"""计算进度百分比"""
|
||||
if not self.business_object:
|
||||
return 0.0
|
||||
|
||||
return self.business_object.get_progress_percentage()
|
||||
|
||||
|
||||
class PrintingOrder(ModelBase):
|
||||
@@ -28,6 +193,14 @@ class PrintingOrder(ModelBase):
|
||||
rolling_warn = models.TextField(blank=True, null=True, verbose_name='滚筒注意事项')
|
||||
production_warn = models.TextField(blank=True, null=True, verbose_name='生产注意事项')
|
||||
is_invalid = models.BooleanField(default=False, verbose_name='是否作废')
|
||||
process = models.ForeignKey(
|
||||
stateflow_models.Process,
|
||||
on_delete=models.PROTECT,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='printing_orders',
|
||||
verbose_name='印染流程',
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return self.human_id
|
||||
@@ -42,8 +215,24 @@ class PrintingOrder(ModelBase):
|
||||
|
||||
@property
|
||||
def human_id(self) -> str:
|
||||
if self.id is None or self.created_at is None:
|
||||
return ""
|
||||
return f"{self.created_at.year}{self.created_at.month:02d}{self.created_at.day:02d}{self.id:06d}"
|
||||
|
||||
def can_change_process(self) -> bool:
|
||||
"""检查是否可以修改流程(所有job都未开始)"""
|
||||
return not any(job.has_started for job in self.printing_jobs.all())
|
||||
|
||||
@property
|
||||
def progress(self) -> int:
|
||||
"""计算订单完成进度百分比(已完成的任务数 / 总任务数)"""
|
||||
jobs = self.printing_jobs.all()
|
||||
if not jobs.exists():
|
||||
return 0
|
||||
|
||||
completed_count = sum(1 for job in jobs if job.is_completed)
|
||||
return int((completed_count / jobs.count()) * 100)
|
||||
|
||||
|
||||
class PrintingJob(ModelBase):
|
||||
"""PrintingJob model representing a printing job associated with an order."""
|
||||
@@ -61,13 +250,81 @@ class PrintingJob(ModelBase):
|
||||
)
|
||||
quantity = models.PositiveIntegerField(verbose_name='数量')
|
||||
unit = models.CharField(max_length=50, verbose_name='单位')
|
||||
size = models.CharField(max_length=100, verbose_name='一段尺寸')
|
||||
pieces = models.PositiveIntegerField(verbose_name='件数')
|
||||
description = models.TextField(blank=True, verbose_name='备注')
|
||||
size = models.CharField(max_length=100, null=True, blank=True, verbose_name='一段尺寸')
|
||||
pieces = models.PositiveIntegerField(null=True, blank=True, verbose_name='件数')
|
||||
description = models.TextField(blank=True, null=True, verbose_name='备注')
|
||||
business_object = models.OneToOneField(
|
||||
stateflow_models.BusinessObject,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='printing_job',
|
||||
verbose_name='流程实例',
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return self.printing_order.human_id
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
"""返回当前状态名称"""
|
||||
if not self.business_object:
|
||||
return '未开始'
|
||||
|
||||
current_state = self.business_object.get_current_state()
|
||||
|
||||
# current_state 为 None 有两种情况:未开始或已完成
|
||||
if current_state is None:
|
||||
# 检查是否有完成记录来区分
|
||||
has_completed_records = self.business_object.state_logs.filter(is_cancelled=False).exists()
|
||||
if has_completed_records:
|
||||
return '已完成'
|
||||
else:
|
||||
return '未开始'
|
||||
|
||||
return current_state.name
|
||||
|
||||
@property
|
||||
def status_id(self):
|
||||
"""返回当前状态ID"""
|
||||
if not self.business_object:
|
||||
return None
|
||||
current_state = self.business_object.get_current_state()
|
||||
return current_state.id if current_state else None
|
||||
|
||||
@property
|
||||
def is_completed(self) -> bool:
|
||||
"""判断是否完成(所有流程节点都已完成)"""
|
||||
if not self.business_object or not self.printing_order.process:
|
||||
return False
|
||||
|
||||
# 获取流程的所有节点
|
||||
process_nodes = self.printing_order.process.process_nodes.all()
|
||||
total_nodes = process_nodes.count()
|
||||
|
||||
if total_nodes == 0:
|
||||
return False
|
||||
|
||||
# 检查已完成的节点数
|
||||
completed_count = self.business_object.state_logs.filter(is_cancelled=False).count()
|
||||
|
||||
# 所有节点都完成了才算完成
|
||||
return completed_count >= total_nodes
|
||||
|
||||
@property
|
||||
def has_started(self) -> bool:
|
||||
"""判断是否已开始(有BusinessObject且有状态记录,或不在初始状态)"""
|
||||
if not self.business_object:
|
||||
return False
|
||||
|
||||
# 检查是否有状态流转记录
|
||||
from stateflow.models import StateFlowRecord
|
||||
has_records = StateFlowRecord.objects.filter(
|
||||
business_object=self.business_object
|
||||
).exists()
|
||||
|
||||
return has_records
|
||||
|
||||
class Meta:
|
||||
verbose_name = '印染款式明细'
|
||||
verbose_name_plural = '印染款式明细'
|
||||
|
||||
710
printing/printing_api.yml
Normal file
710
printing/printing_api.yml
Normal file
@@ -0,0 +1,710 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: Printing API
|
||||
description: |
|
||||
印染模块接口文档,包含 PrintingOrder(印染订单)和 PrintingJob(印染款式明细)的 CRUD 和自定义动作。
|
||||
所有接口仅允许印染工厂用户访问(IsPrintingFactory)。
|
||||
version: '1.0.0'
|
||||
servers:
|
||||
- url: /api/v1
|
||||
description: 本地开发 API 前缀
|
||||
components:
|
||||
securitySchemes:
|
||||
BearerAuth:
|
||||
type: http
|
||||
scheme: bearer
|
||||
bearerFormat: JWT
|
||||
schemas:
|
||||
PrintingOrder:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
description: 订单 ID
|
||||
human_id:
|
||||
type: string
|
||||
description: 由系统生成的人类可读编号(格式: YYYYMMDD000001)
|
||||
example: "20251112000001"
|
||||
customer:
|
||||
type: integer
|
||||
description: 客户 ID
|
||||
customer_name:
|
||||
type: string
|
||||
description: 客户名称
|
||||
customer_phone:
|
||||
type: string
|
||||
description: 客户电话
|
||||
customer_address:
|
||||
type: string
|
||||
description: 客户地址
|
||||
fabric:
|
||||
type: string
|
||||
description: 面料
|
||||
example: "纯棉布料"
|
||||
width:
|
||||
type: string
|
||||
description: 幅宽
|
||||
example: "150cm"
|
||||
is_urgent:
|
||||
type: boolean
|
||||
description: 是否紧急
|
||||
default: false
|
||||
area:
|
||||
type: string
|
||||
description: 地区
|
||||
example: "广州"
|
||||
address:
|
||||
type: string
|
||||
description: 地址
|
||||
example: "白云区xxx"
|
||||
fabric_source:
|
||||
type: string
|
||||
description: 布料来源
|
||||
example: "客户提供"
|
||||
is_fabric_received:
|
||||
type: boolean
|
||||
description: 布料是否已收
|
||||
default: false
|
||||
craft:
|
||||
type: string
|
||||
description: 工艺
|
||||
example: "活性印花"
|
||||
description:
|
||||
type: string
|
||||
description: 订单描述
|
||||
outgoing_date:
|
||||
type: string
|
||||
format: date
|
||||
description: 出货日期
|
||||
example: "2025-11-20"
|
||||
curve:
|
||||
type: string
|
||||
description: 曲线
|
||||
new_curve:
|
||||
type: string
|
||||
description: 新加曲线
|
||||
position:
|
||||
type: string
|
||||
description: 位置
|
||||
printing_warn:
|
||||
type: string
|
||||
description: 打印注意事项
|
||||
rolling_warn:
|
||||
type: string
|
||||
description: 滚筒注意事项
|
||||
production_warn:
|
||||
type: string
|
||||
description: 生产注意事项
|
||||
is_invalid:
|
||||
type: boolean
|
||||
description: 是否作废
|
||||
default: false
|
||||
created_at:
|
||||
type: string
|
||||
format: date-time
|
||||
description: 创建时间
|
||||
updated_at:
|
||||
type: string
|
||||
format: date-time
|
||||
description: 更新时间
|
||||
required: [customer, fabric, width]
|
||||
PrintingOrderCreate:
|
||||
type: object
|
||||
properties:
|
||||
customer:
|
||||
type: integer
|
||||
description: 客户 ID
|
||||
fabric:
|
||||
type: string
|
||||
description: 面料
|
||||
example: "纯棉布料"
|
||||
width:
|
||||
type: string
|
||||
description: 幅宽
|
||||
example: "150cm"
|
||||
is_urgent:
|
||||
type: boolean
|
||||
description: 是否紧急
|
||||
default: false
|
||||
area:
|
||||
type: string
|
||||
description: 地区
|
||||
example: "广州"
|
||||
address:
|
||||
type: string
|
||||
description: 地址
|
||||
example: "白云区xxx"
|
||||
fabric_source:
|
||||
type: string
|
||||
description: 布料来源
|
||||
example: "客户提供"
|
||||
is_fabric_received:
|
||||
type: boolean
|
||||
description: 布料是否已收
|
||||
default: false
|
||||
craft:
|
||||
type: string
|
||||
description: 工艺
|
||||
example: "活性印花"
|
||||
description:
|
||||
type: string
|
||||
description: 订单描述
|
||||
outgoing_date:
|
||||
type: string
|
||||
format: date
|
||||
description: 出货日期
|
||||
example: "2025-11-20"
|
||||
curve:
|
||||
type: string
|
||||
description: 曲线
|
||||
new_curve:
|
||||
type: string
|
||||
description: 新加曲线
|
||||
position:
|
||||
type: string
|
||||
description: 位置
|
||||
printing_warn:
|
||||
type: string
|
||||
description: 打印注意事项
|
||||
rolling_warn:
|
||||
type: string
|
||||
description: 滚筒注意事项
|
||||
production_warn:
|
||||
type: string
|
||||
description: 生产注意事项
|
||||
is_invalid:
|
||||
type: boolean
|
||||
description: 是否作废
|
||||
default: false
|
||||
required: [customer, fabric, width]
|
||||
PrintingJob:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
description: 款式明细 ID
|
||||
printing_order:
|
||||
type: integer
|
||||
description: 印染订单 ID
|
||||
printing_order_id:
|
||||
type: string
|
||||
description: 印染订单人类可读编号
|
||||
example: "20251112000001"
|
||||
product:
|
||||
type: integer
|
||||
description: 产品 ID
|
||||
product_name:
|
||||
type: string
|
||||
description: 产品名称
|
||||
example: "测试产品"
|
||||
product_code:
|
||||
type: string
|
||||
description: 产品编号
|
||||
example: "TEST001"
|
||||
quantity:
|
||||
type: integer
|
||||
description: 数量(必须 > 0)
|
||||
example: 100
|
||||
minimum: 1
|
||||
unit:
|
||||
type: string
|
||||
description: 单位
|
||||
example: "米"
|
||||
size:
|
||||
type: string
|
||||
description: 一段尺寸
|
||||
example: "50*60"
|
||||
pieces:
|
||||
type: integer
|
||||
description: 件数(必须 > 0)
|
||||
example: 10
|
||||
minimum: 1
|
||||
description:
|
||||
type: string
|
||||
description: 备注
|
||||
created_at:
|
||||
type: string
|
||||
format: date-time
|
||||
description: 创建时间
|
||||
updated_at:
|
||||
type: string
|
||||
format: date-time
|
||||
description: 更新时间
|
||||
required: [printing_order, product, quantity, unit, size, pieces]
|
||||
PrintingJobCreate:
|
||||
type: object
|
||||
properties:
|
||||
printing_order:
|
||||
type: integer
|
||||
description: 印染订单 ID
|
||||
product:
|
||||
type: integer
|
||||
description: 产品 ID
|
||||
quantity:
|
||||
type: integer
|
||||
description: 数量(必须 > 0)
|
||||
example: 100
|
||||
minimum: 1
|
||||
unit:
|
||||
type: string
|
||||
description: 单位
|
||||
example: "米"
|
||||
size:
|
||||
type: string
|
||||
description: 一段尺寸
|
||||
example: "50*60"
|
||||
pieces:
|
||||
type: integer
|
||||
description: 件数(必须 > 0)
|
||||
example: 10
|
||||
minimum: 1
|
||||
description:
|
||||
type: string
|
||||
description: 备注
|
||||
required: [printing_order, product, quantity, unit, size, pieces]
|
||||
parameters:
|
||||
limit:
|
||||
name: limit
|
||||
in: query
|
||||
schema:
|
||||
type: integer
|
||||
description: 返回条目上限(分页)
|
||||
offset:
|
||||
name: offset
|
||||
in: query
|
||||
schema:
|
||||
type: integer
|
||||
description: 分页偏移
|
||||
search:
|
||||
name: search
|
||||
in: query
|
||||
schema:
|
||||
type: string
|
||||
description: 全文搜索关键字
|
||||
ordering:
|
||||
name: ordering
|
||||
in: query
|
||||
schema:
|
||||
type: string
|
||||
description: 排序字段,例如 id 或 -id
|
||||
paths:
|
||||
/printing-orders/:
|
||||
get:
|
||||
summary: 获取印染订单列表
|
||||
description: 支持过滤(customer, is_urgent, is_fabric_received, is_invalid, area, outgoing_date_from/to, created_date_from/to)、search、ordering 和分页。
|
||||
parameters:
|
||||
- $ref: '#/components/parameters/limit'
|
||||
- $ref: '#/components/parameters/offset'
|
||||
- $ref: '#/components/parameters/search'
|
||||
- $ref: '#/components/parameters/ordering'
|
||||
- name: customer
|
||||
in: query
|
||||
schema:
|
||||
type: integer
|
||||
description: 客户 ID
|
||||
- name: customer_name
|
||||
in: query
|
||||
schema:
|
||||
type: string
|
||||
description: 客户名称(模糊)
|
||||
- name: customer_phone
|
||||
in: query
|
||||
schema:
|
||||
type: string
|
||||
description: 客户电话(模糊)
|
||||
- name: fabric
|
||||
in: query
|
||||
schema:
|
||||
type: string
|
||||
description: 面料(模糊)
|
||||
- name: is_urgent
|
||||
in: query
|
||||
schema:
|
||||
type: boolean
|
||||
- name: is_fabric_received
|
||||
in: query
|
||||
schema:
|
||||
type: boolean
|
||||
- name: is_invalid
|
||||
in: query
|
||||
schema:
|
||||
type: boolean
|
||||
- name: area
|
||||
in: query
|
||||
schema:
|
||||
type: string
|
||||
- name: outgoing_date_from
|
||||
in: query
|
||||
schema:
|
||||
type: string
|
||||
format: date
|
||||
- name: outgoing_date_to
|
||||
in: query
|
||||
schema:
|
||||
type: string
|
||||
format: date
|
||||
responses:
|
||||
'200':
|
||||
description: 列表
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
oneOf:
|
||||
- type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/PrintingOrder'
|
||||
- type: object
|
||||
properties:
|
||||
count:
|
||||
type: integer
|
||||
results:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/PrintingOrder'
|
||||
'401':
|
||||
description: 未认证
|
||||
'403':
|
||||
description: 没有权限
|
||||
security:
|
||||
- BearerAuth: []
|
||||
post:
|
||||
summary: 创建印染订单
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingOrderCreate'
|
||||
responses:
|
||||
'201':
|
||||
description: 创建成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingOrder'
|
||||
'400':
|
||||
description: 校验错误
|
||||
'403':
|
||||
description: 没有权限
|
||||
security:
|
||||
- BearerAuth: []
|
||||
/printing-orders/{id}/:
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: integer
|
||||
get:
|
||||
summary: 获取印染订单详情
|
||||
responses:
|
||||
'200':
|
||||
description: 详情
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingOrder'
|
||||
'404':
|
||||
description: 未找到
|
||||
security:
|
||||
- BearerAuth: []
|
||||
put:
|
||||
summary: 更新印染订单(全量)
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingOrderCreate'
|
||||
responses:
|
||||
'200':
|
||||
description: 更新成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingOrder'
|
||||
'400':
|
||||
description: 校验错误
|
||||
'403':
|
||||
description: 没有权限
|
||||
security:
|
||||
- BearerAuth: []
|
||||
patch:
|
||||
summary: 更新印染订单(部分)
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingOrderCreate'
|
||||
responses:
|
||||
'200':
|
||||
description: 更新成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingOrder'
|
||||
'400':
|
||||
description: 校验错误
|
||||
security:
|
||||
- BearerAuth: []
|
||||
delete:
|
||||
summary: 删除(不支持)
|
||||
description: 删除操作已禁用,请使用 作废 操作。
|
||||
responses:
|
||||
'405':
|
||||
description: 不支持删除
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
detail:
|
||||
type: string
|
||||
security:
|
||||
- BearerAuth: []
|
||||
/printing-orders/{id}/invalidate/:
|
||||
post:
|
||||
summary: 作废订单
|
||||
description: 将订单标记为已作废(需要 printing.can_invalidate_printingorder 权限)。
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: integer
|
||||
responses:
|
||||
'200':
|
||||
description: 作废成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
detail:
|
||||
type: string
|
||||
data:
|
||||
$ref: '#/components/schemas/PrintingOrder'
|
||||
'400':
|
||||
description: 已作废或请求错误
|
||||
'403':
|
||||
description: 没有权限
|
||||
security:
|
||||
- BearerAuth: []
|
||||
/printing-orders/{id}/activate/:
|
||||
post:
|
||||
summary: 恢复订单
|
||||
description: 将订单从作废状态恢复(需要 printing.can_activate_printingorder 权限)。
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: integer
|
||||
responses:
|
||||
'200':
|
||||
description: 恢复成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
detail:
|
||||
type: string
|
||||
data:
|
||||
$ref: '#/components/schemas/PrintingOrder'
|
||||
'400':
|
||||
description: 未作废或请求错误
|
||||
'403':
|
||||
description: 没有权限
|
||||
security:
|
||||
- BearerAuth: []
|
||||
/printing-orders/{id}/mark_fabric_received/:
|
||||
post:
|
||||
summary: 标记布料已收
|
||||
description: 将订单标记为布料已收到。
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: integer
|
||||
responses:
|
||||
'200':
|
||||
description: 标记成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
detail:
|
||||
type: string
|
||||
data:
|
||||
$ref: '#/components/schemas/PrintingOrder'
|
||||
'400':
|
||||
description: 已标记或请求错误
|
||||
security:
|
||||
- BearerAuth: []
|
||||
|
||||
/printing-jobs/:
|
||||
get:
|
||||
summary: 获取印染款式明细列表
|
||||
description: 支持过滤(printing_order, product, product_name, unit, quantity_min/max, pieces_min/max)、search、ordering 和分页。
|
||||
parameters:
|
||||
- $ref: '#/components/parameters/limit'
|
||||
- $ref: '#/components/parameters/offset'
|
||||
- $ref: '#/components/parameters/search'
|
||||
- $ref: '#/components/parameters/ordering'
|
||||
- name: printing_order
|
||||
in: query
|
||||
schema:
|
||||
type: integer
|
||||
- name: product
|
||||
in: query
|
||||
schema:
|
||||
type: integer
|
||||
- name: product_name
|
||||
in: query
|
||||
schema:
|
||||
type: string
|
||||
- name: unit
|
||||
in: query
|
||||
schema:
|
||||
type: string
|
||||
- name: quantity_min
|
||||
in: query
|
||||
schema:
|
||||
type: integer
|
||||
- name: quantity_max
|
||||
in: query
|
||||
schema:
|
||||
type: integer
|
||||
- name: pieces_min
|
||||
in: query
|
||||
schema:
|
||||
type: integer
|
||||
- name: pieces_max
|
||||
in: query
|
||||
schema:
|
||||
type: integer
|
||||
responses:
|
||||
'200':
|
||||
description: 列表
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
oneOf:
|
||||
- type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/PrintingJob'
|
||||
- type: object
|
||||
properties:
|
||||
count:
|
||||
type: integer
|
||||
results:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/PrintingJob'
|
||||
'401':
|
||||
description: 未认证
|
||||
'403':
|
||||
description: 没有权限
|
||||
security:
|
||||
- BearerAuth: []
|
||||
post:
|
||||
summary: 创建印染款式明细
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingJobCreate'
|
||||
responses:
|
||||
'201':
|
||||
description: 创建成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingJob'
|
||||
'400':
|
||||
description: 校验错误
|
||||
'403':
|
||||
description: 没有权限
|
||||
security:
|
||||
- BearerAuth: []
|
||||
|
||||
/printing-jobs/{id}/:
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: integer
|
||||
get:
|
||||
summary: 获取款式明细详情
|
||||
responses:
|
||||
'200':
|
||||
description: 详情
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingJob'
|
||||
'404':
|
||||
description: 未找到
|
||||
security:
|
||||
- BearerAuth: []
|
||||
put:
|
||||
summary: 更新款式明细(全量)
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingJobCreate'
|
||||
responses:
|
||||
'200':
|
||||
description: 更新成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingJob'
|
||||
'400':
|
||||
description: 校验错误
|
||||
security:
|
||||
- BearerAuth: []
|
||||
patch:
|
||||
summary: 更新款式明细(部分)
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingJobCreate'
|
||||
responses:
|
||||
'200':
|
||||
description: 更新成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PrintingJob'
|
||||
'400':
|
||||
description: 校验错误
|
||||
security:
|
||||
- BearerAuth: []
|
||||
delete:
|
||||
summary: 删除(不支持)
|
||||
description: 删除操作已禁用,请使用所需的业务操作。
|
||||
responses:
|
||||
'405':
|
||||
description: 不支持删除
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
detail:
|
||||
type: string
|
||||
security:
|
||||
- BearerAuth: []
|
||||
|
||||
components_end: {}
|
||||
@@ -1,124 +0,0 @@
|
||||
"""
|
||||
Printing 序列化器
|
||||
"""
|
||||
from rest_framework import serializers
|
||||
from printing import models
|
||||
|
||||
|
||||
class PrintingOrderListSerializer(serializers.ModelSerializer):
|
||||
"""印染订单列表序列化器"""
|
||||
customer_name = serializers.CharField(source='customer.name', read_only=True)
|
||||
customer_phone = serializers.CharField(source='customer.phone', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingOrder
|
||||
fields = [
|
||||
'id', 'human_id', 'customer', 'customer_name', 'customer_phone',
|
||||
'fabric', 'width', 'is_urgent', 'area', 'address',
|
||||
'is_fabric_received', 'outgoing_date', 'is_invalid',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = ['id', 'human_id', 'created_at', 'updated_at']
|
||||
|
||||
|
||||
class PrintingOrderDetailSerializer(serializers.ModelSerializer):
|
||||
"""印染订单详情序列化器"""
|
||||
customer_name = serializers.CharField(source='customer.name', read_only=True)
|
||||
customer_phone = serializers.CharField(source='customer.phone', read_only=True)
|
||||
customer_address = serializers.CharField(source='customer.address', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingOrder
|
||||
fields = [
|
||||
'id', 'human_id', 'customer', 'customer_name', 'customer_phone', 'customer_address',
|
||||
'fabric', 'width', 'is_urgent', 'area', 'address', 'fabric_source',
|
||||
'is_fabric_received', 'craft', 'description', 'outgoing_date',
|
||||
'curve', 'new_curve', 'position',
|
||||
'printing_warn', 'rolling_warn', 'production_warn',
|
||||
'is_invalid', 'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = ['id', 'human_id', 'created_at', 'updated_at']
|
||||
|
||||
|
||||
class PrintingOrderCreateUpdateSerializer(serializers.ModelSerializer):
|
||||
"""印染订单创建/更新序列化器"""
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingOrder
|
||||
fields = [
|
||||
'customer', 'fabric', 'width', 'is_urgent', 'area', 'address',
|
||||
'fabric_source', 'is_fabric_received', 'craft', 'description',
|
||||
'outgoing_date', 'curve', 'new_curve', 'position',
|
||||
'printing_warn', 'rolling_warn', 'production_warn', 'is_invalid'
|
||||
]
|
||||
|
||||
def validate_customer(self, value):
|
||||
"""验证客户是否存在"""
|
||||
if not value:
|
||||
raise serializers.ValidationError("客户不能为空")
|
||||
return value
|
||||
|
||||
|
||||
class PrintingJobListSerializer(serializers.ModelSerializer):
|
||||
"""印染款式明细列表序列化器"""
|
||||
printing_order_id = serializers.CharField(source='printing_order.human_id', read_only=True)
|
||||
product_name = serializers.CharField(source='product.name', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = [
|
||||
'id', 'printing_order', 'printing_order_id', 'product', 'product_name',
|
||||
'quantity', 'unit', 'size', 'pieces', 'description',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = ['id', 'created_at', 'updated_at']
|
||||
|
||||
|
||||
class PrintingJobDetailSerializer(serializers.ModelSerializer):
|
||||
"""印染款式明细详情序列化器"""
|
||||
printing_order_id = serializers.CharField(source='printing_order.human_id', read_only=True)
|
||||
product_name = serializers.CharField(source='product.name', read_only=True)
|
||||
product_code = serializers.CharField(source='product.human_id', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = [
|
||||
'id', 'printing_order', 'printing_order_id', 'product', 'product_name', 'product_code',
|
||||
'quantity', 'unit', 'size', 'pieces', 'description',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = ['id', 'created_at', 'updated_at']
|
||||
|
||||
|
||||
class PrintingJobCreateUpdateSerializer(serializers.ModelSerializer):
|
||||
"""印染款式明细创建/更新序列化器"""
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = [
|
||||
'printing_order', 'product', 'quantity', 'unit', 'size', 'pieces', 'description'
|
||||
]
|
||||
|
||||
def validate_printing_order(self, value):
|
||||
"""验证印染订单是否存在"""
|
||||
if not value:
|
||||
raise serializers.ValidationError("印染订单不能为空")
|
||||
return value
|
||||
|
||||
def validate_product(self, value):
|
||||
"""验证产品是否存在"""
|
||||
if not value:
|
||||
raise serializers.ValidationError("产品不能为空")
|
||||
return value
|
||||
|
||||
def validate_quantity(self, value):
|
||||
"""验证数量"""
|
||||
if value <= 0:
|
||||
raise serializers.ValidationError("数量必须大于0")
|
||||
return value
|
||||
|
||||
def validate_pieces(self, value):
|
||||
"""验证件数"""
|
||||
if value <= 0:
|
||||
raise serializers.ValidationError("件数必须大于0")
|
||||
return value
|
||||
233
printing/test_plate_order.py
Normal file
233
printing/test_plate_order.py
Normal file
@@ -0,0 +1,233 @@
|
||||
"""
|
||||
PlateOrder 模型测试
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.utils import timezone
|
||||
from decimal import Decimal
|
||||
from printing import models as printing_models
|
||||
from basic_info import models as basic_models
|
||||
from stateflow import models as stateflow_models
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class PlateOrderModelTestCase(TestCase):
|
||||
"""测试 PlateOrder 模型"""
|
||||
|
||||
def setUp(self):
|
||||
"""设置测试数据"""
|
||||
self.user = User.objects.create_user(username='testuser', password='testpass')
|
||||
|
||||
# 创建商户
|
||||
self.merchant = basic_models.Merchant.objects.create(
|
||||
name='测试印花厂',
|
||||
type=basic_models.MerchantTypeEnum.FACTORY
|
||||
)
|
||||
|
||||
# 创建客户
|
||||
self.customer = basic_models.Customer.objects.create(
|
||||
merchant=self.merchant,
|
||||
name='测试客户',
|
||||
mobile='13900139000',
|
||||
area='测试地区'
|
||||
)
|
||||
|
||||
# 创建员工
|
||||
self.salesperson = basic_models.Employee.objects.create(
|
||||
sys_user=self.user,
|
||||
merchant=self.merchant,
|
||||
name='销售员',
|
||||
mobile='13800138000',
|
||||
job_type=basic_models.EmployeeTypeEnum.PRINTER,
|
||||
status=basic_models.EmployeeStatusEnum.ACTIVE
|
||||
)
|
||||
|
||||
self.merchandiser = basic_models.Employee.objects.create(
|
||||
merchant=self.merchant,
|
||||
name='跟单员',
|
||||
mobile='13800138001',
|
||||
job_type=basic_models.EmployeeTypeEnum.ROLLING,
|
||||
status=basic_models.EmployeeStatusEnum.ACTIVE
|
||||
)
|
||||
|
||||
def test_create_plate_order_basic(self):
|
||||
"""测试创建基本的开版订单"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-1',
|
||||
design_code='DES001',
|
||||
customer=self.customer,
|
||||
plate_type='首版',
|
||||
urgency_level='正常',
|
||||
style_name='测试款式',
|
||||
fabric='纯棉',
|
||||
width='150cm',
|
||||
)
|
||||
|
||||
self.assertIsNotNone(plate_order.id)
|
||||
self.assertEqual(plate_order.plate_code, '20251113-1')
|
||||
self.assertEqual(plate_order.customer, self.customer)
|
||||
self.assertEqual(plate_order.style_name, '测试款式')
|
||||
|
||||
def test_plate_order_with_employees(self):
|
||||
"""测试带销售员和跟单员的开版订单"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-2',
|
||||
customer=self.customer,
|
||||
salesperson=self.salesperson,
|
||||
merchandiser=self.merchandiser,
|
||||
urgency_level='加急',
|
||||
)
|
||||
|
||||
self.assertEqual(plate_order.salesperson, self.salesperson)
|
||||
self.assertEqual(plate_order.merchandiser, self.merchandiser)
|
||||
self.assertEqual(plate_order.urgency_level, '加急')
|
||||
|
||||
def test_plate_order_with_ratings(self):
|
||||
"""测试带质量评级的开版订单"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-3',
|
||||
customer=self.customer,
|
||||
drawing_rating='A',
|
||||
color_matching_rating='B',
|
||||
sample_rating='A',
|
||||
difficulty_rating='高',
|
||||
is_mark_frame=True,
|
||||
)
|
||||
|
||||
self.assertEqual(plate_order.drawing_rating, 'A')
|
||||
self.assertEqual(plate_order.color_matching_rating, 'B')
|
||||
self.assertEqual(plate_order.sample_rating, 'A')
|
||||
self.assertEqual(plate_order.difficulty_rating, '高')
|
||||
self.assertTrue(plate_order.is_mark_frame)
|
||||
|
||||
def test_plate_order_with_dates(self):
|
||||
"""测试带时间信息的开版订单"""
|
||||
now = timezone.now()
|
||||
completion_date = now + timezone.timedelta(days=7)
|
||||
required_date = (now + timezone.timedelta(days=10)).date()
|
||||
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-4',
|
||||
customer=self.customer,
|
||||
plate_date=now,
|
||||
completion_date=completion_date,
|
||||
required_completion_date=required_date,
|
||||
)
|
||||
|
||||
self.assertIsNotNone(plate_order.plate_date)
|
||||
self.assertIsNotNone(plate_order.completion_date)
|
||||
self.assertIsNotNone(plate_order.required_completion_date)
|
||||
|
||||
def test_plate_order_with_sample_info(self):
|
||||
"""测试带样品信息的开版订单"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-5',
|
||||
customer=self.customer,
|
||||
sample_meter='米样1',
|
||||
required_sample_meters=Decimal('100.50'),
|
||||
)
|
||||
|
||||
self.assertEqual(plate_order.sample_meter, '米样1')
|
||||
self.assertEqual(plate_order.required_sample_meters, Decimal('100.50'))
|
||||
|
||||
def test_plate_order_status_without_business_object(self):
|
||||
"""测试没有 business_object 时的状态"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-6',
|
||||
customer=self.customer,
|
||||
)
|
||||
|
||||
self.assertEqual(plate_order.status, '未开始')
|
||||
self.assertFalse(plate_order.is_completed)
|
||||
self.assertFalse(plate_order.has_started) # 因为没有 business_object
|
||||
self.assertEqual(plate_order.progress_percentage, 0.0)
|
||||
|
||||
def test_plate_order_with_business_object(self):
|
||||
"""测试带 business_object 的开版订单"""
|
||||
# 创建流程
|
||||
state1 = stateflow_models.State.objects.create(name='画图')
|
||||
state2 = stateflow_models.State.objects.create(name='调色')
|
||||
state3 = stateflow_models.State.objects.create(name='套样')
|
||||
|
||||
process = stateflow_models.Process.objects.create(name='开版流程')
|
||||
process.replace_nodes([state1, state2, state3])
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name='PlateOrder-Test',
|
||||
process=process,
|
||||
)
|
||||
|
||||
# 创建开版订单
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-7',
|
||||
customer=self.customer,
|
||||
business_object=business_object,
|
||||
)
|
||||
|
||||
self.assertIsNotNone(plate_order.business_object)
|
||||
self.assertEqual(plate_order.progress_percentage, 0.0)
|
||||
|
||||
# 推进一个状态
|
||||
from stateflow.services import advance_to_next_state
|
||||
advance_to_next_state(business_object, self.user)
|
||||
|
||||
# 刷新并检查状态
|
||||
plate_order.refresh_from_db()
|
||||
self.assertEqual(plate_order.status, state2.name) # 应该在第二个状态
|
||||
self.assertFalse(plate_order.is_completed)
|
||||
self.assertTrue(plate_order.has_started)
|
||||
self.assertGreater(plate_order.progress_percentage, 0)
|
||||
|
||||
def test_plate_order_str(self):
|
||||
"""测试字符串表示"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-8',
|
||||
customer=self.customer,
|
||||
)
|
||||
|
||||
self.assertEqual(str(plate_order), '20251113-8')
|
||||
|
||||
def test_plate_order_with_approval_and_order_status(self):
|
||||
"""测试带审批和订单状态的开版订单"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-9',
|
||||
customer=self.customer,
|
||||
approval_result='通过',
|
||||
is_ordered=True,
|
||||
customer_feedback='颜色需要调整',
|
||||
)
|
||||
|
||||
self.assertEqual(plate_order.approval_result, '通过')
|
||||
self.assertTrue(plate_order.is_ordered)
|
||||
self.assertEqual(plate_order.customer_feedback, '颜色需要调整')
|
||||
|
||||
def test_plate_order_unique_plate_code(self):
|
||||
"""测试版单编号的唯一性"""
|
||||
printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-10',
|
||||
customer=self.customer,
|
||||
)
|
||||
|
||||
# 尝试创建相同编号的订单应该失败
|
||||
with self.assertRaises(Exception):
|
||||
printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-10',
|
||||
customer=self.customer,
|
||||
)
|
||||
|
||||
def test_plate_order_nullable_fields(self):
|
||||
"""测试可空字段"""
|
||||
plate_order = printing_models.PlateOrder.objects.create(
|
||||
plate_code='20251113-11',
|
||||
customer=self.customer,
|
||||
)
|
||||
|
||||
# 验证所有可空字段都可以为空
|
||||
self.assertIsNone(plate_order.design_code)
|
||||
self.assertIsNone(plate_order.plate_type)
|
||||
self.assertIsNone(plate_order.plate_date)
|
||||
self.assertIsNone(plate_order.salesperson)
|
||||
self.assertIsNone(plate_order.merchandiser)
|
||||
self.assertIsNone(plate_order.business_object)
|
||||
@@ -1,303 +0,0 @@
|
||||
"""
|
||||
Printing API ViewSet
|
||||
"""
|
||||
from rest_framework import viewsets, filters, status
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import BasePermission
|
||||
from rest_framework.pagination import LimitOffsetPagination
|
||||
from rest_framework.permissions import DjangoModelPermissions
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from django_filters import rest_framework as django_filters
|
||||
from printing import models
|
||||
from basic_info.models import MerchantTypeEnum
|
||||
|
||||
from printing.serializers import (
|
||||
PrintingOrderListSerializer,
|
||||
PrintingOrderDetailSerializer,
|
||||
PrintingOrderCreateUpdateSerializer,
|
||||
PrintingJobListSerializer,
|
||||
PrintingJobDetailSerializer,
|
||||
PrintingJobCreateUpdateSerializer,
|
||||
)
|
||||
|
||||
|
||||
class IsPrintingFactory(BasePermission):
|
||||
"""自定义权限类,允许印染工厂用户访问"""
|
||||
|
||||
message = '您没有访问印染订单的权限'
|
||||
|
||||
def has_permission(self, request, view):
|
||||
if not request.user.is_authenticated:
|
||||
return False
|
||||
if hasattr(request.user, 'employee'):
|
||||
return request.user.employee.merchant.type == MerchantTypeEnum.FACTORY
|
||||
return False
|
||||
|
||||
|
||||
class HasInvalidatePrintingOrderPermission(BasePermission):
|
||||
"""自定义权限类,检查用户是否有作废印染订单的权限"""
|
||||
|
||||
message = '您没有权限作废订单'
|
||||
|
||||
def has_permission(self, request, view):
|
||||
if not request.user.is_authenticated:
|
||||
return False
|
||||
return request.user.has_perm('printing.can_invalidate_printingorder')
|
||||
|
||||
|
||||
class HasActivatePrintingOrderPermission(BasePermission):
|
||||
"""自定义权限类,检查用户是否有恢复印染订单的权限"""
|
||||
|
||||
message = '您没有权限恢复订单'
|
||||
|
||||
def has_permission(self, request, view):
|
||||
if not request.user.is_authenticated:
|
||||
return False
|
||||
return request.user.has_perm('printing.can_activate_printingorder')
|
||||
|
||||
|
||||
class PrintingOrderFilterSet(django_filters.FilterSet):
|
||||
"""印染订单过滤器"""
|
||||
customer_name = django_filters.CharFilter(field_name='customer__name', lookup_expr='icontains')
|
||||
customer_phone = django_filters.CharFilter(field_name='customer__phone', lookup_expr='icontains')
|
||||
fabric = django_filters.CharFilter(lookup_expr='icontains')
|
||||
is_urgent = django_filters.BooleanFilter()
|
||||
is_fabric_received = django_filters.BooleanFilter()
|
||||
is_invalid = django_filters.BooleanFilter()
|
||||
area = django_filters.CharFilter(lookup_expr='icontains')
|
||||
outgoing_date_from = django_filters.DateFilter(field_name='outgoing_date', lookup_expr='gte')
|
||||
outgoing_date_to = django_filters.DateFilter(field_name='outgoing_date', lookup_expr='lte')
|
||||
created_date_from = django_filters.DateFilter(field_name='created_at', lookup_expr='gte')
|
||||
created_date_to = django_filters.DateFilter(field_name='created_at', lookup_expr='lte')
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingOrder
|
||||
fields = ['customer', 'is_urgent', 'is_fabric_received', 'is_invalid']
|
||||
|
||||
|
||||
class PrintingOrderViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
印染订单 ViewSet
|
||||
|
||||
提供印染订单的增改查功能(不支持删除)
|
||||
|
||||
list: 获取印染订单列表
|
||||
retrieve: 获取印染订单详情
|
||||
create: 创建印染订单
|
||||
update: 更新印染订单
|
||||
partial_update: 部分更新印染订单
|
||||
|
||||
查询参数:
|
||||
- customer: 客户ID
|
||||
- customer_name: 客户名称(模糊查询)
|
||||
- customer_phone: 客户电话(模糊查询)
|
||||
- fabric: 面料(模糊查询)
|
||||
- is_urgent: 是否紧急(true/false)
|
||||
- is_fabric_received: 布料是否已收(true/false)
|
||||
- is_invalid: 是否作废(true/false)
|
||||
- area: 地区(模糊查询)
|
||||
- outgoing_date_from: 出货日期起始
|
||||
- outgoing_date_to: 出货日期结束
|
||||
- created_date_from: 创建日期起始
|
||||
- created_date_to: 创建日期结束
|
||||
- search: 全文搜索(客户名称、面料、地区、工艺)
|
||||
- ordering: 排序字段
|
||||
"""
|
||||
queryset = models.PrintingOrder.objects.all()
|
||||
permission_classes = [DjangoModelPermissions]
|
||||
pagination_class = LimitOffsetPagination
|
||||
filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
|
||||
filterset_class = PrintingOrderFilterSet
|
||||
search_fields = ['customer__name', 'fabric', 'area', 'craft', 'description']
|
||||
ordering_fields = [
|
||||
'id', 'created_at', 'updated_at', 'outgoing_date',
|
||||
'is_urgent', 'is_fabric_received', 'is_invalid'
|
||||
]
|
||||
ordering = ['-created_at']
|
||||
|
||||
def get_permissions(self):
|
||||
permissions = super().get_permissions() + [IsPrintingFactory()]
|
||||
if self.action == 'invalidate':
|
||||
permissions.append(HasInvalidatePrintingOrderPermission())
|
||||
elif self.action == 'activate':
|
||||
permissions.append(HasActivatePrintingOrderPermission())
|
||||
return permissions
|
||||
|
||||
def get_serializer_class(self):
|
||||
"""根据动作选择序列化器"""
|
||||
if self.action == 'list':
|
||||
return PrintingOrderListSerializer
|
||||
elif self.action in ['create', 'update', 'partial_update']:
|
||||
return PrintingOrderCreateUpdateSerializer
|
||||
else: # retrieve
|
||||
return PrintingOrderDetailSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
"""优化查询"""
|
||||
queryset = super().get_queryset()
|
||||
if self.action in ['list', 'retrieve']:
|
||||
queryset = queryset.select_related('customer')
|
||||
return queryset
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
"""禁用删除操作"""
|
||||
return Response(
|
||||
{'detail': '印染订单不支持删除操作,请使用作废功能'},
|
||||
status=status.HTTP_405_METHOD_NOT_ALLOWED
|
||||
)
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def invalidate(self, request, pk=None):
|
||||
"""
|
||||
作废订单
|
||||
|
||||
需要权限: printing.can_invalidate_printingorder
|
||||
"""
|
||||
# 检查权限
|
||||
if not request.user.has_perm('printing.can_invalidate_printingorder'):
|
||||
return Response(
|
||||
{'detail': '您没有权限作废订单'},
|
||||
status=status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
printing_order = self.get_object()
|
||||
|
||||
if printing_order.is_invalid:
|
||||
return Response(
|
||||
{'detail': '该订单已经作废'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
printing_order.is_invalid = True
|
||||
printing_order.save()
|
||||
|
||||
serializer = PrintingOrderDetailSerializer(printing_order)
|
||||
return Response({
|
||||
'detail': '订单已作废',
|
||||
'data': serializer.data
|
||||
})
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def activate(self, request, pk=None):
|
||||
"""
|
||||
恢复订单
|
||||
|
||||
需要权限: printing.can_activate_printingorder
|
||||
"""
|
||||
# 检查权限
|
||||
if not request.user.has_perm('printing.can_activate_printingorder'):
|
||||
return Response(
|
||||
{'detail': '您没有权限恢复订单'},
|
||||
status=status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
printing_order = self.get_object()
|
||||
|
||||
if not printing_order.is_invalid:
|
||||
return Response(
|
||||
{'detail': '该订单未作废,无需恢复'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
printing_order.is_invalid = False
|
||||
printing_order.save()
|
||||
|
||||
serializer = PrintingOrderDetailSerializer(printing_order)
|
||||
return Response({
|
||||
'detail': '订单已恢复',
|
||||
'data': serializer.data
|
||||
})
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def mark_fabric_received(self, request, pk=None):
|
||||
"""标记布料已收"""
|
||||
printing_order = self.get_object()
|
||||
|
||||
if printing_order.is_fabric_received:
|
||||
return Response(
|
||||
{'detail': '布料已经标记为已收'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
printing_order.is_fabric_received = True
|
||||
printing_order.save()
|
||||
|
||||
serializer = PrintingOrderDetailSerializer(printing_order)
|
||||
return Response({
|
||||
'detail': '已标记布料已收',
|
||||
'data': serializer.data
|
||||
})
|
||||
|
||||
|
||||
class PrintingJobFilterSet(django_filters.FilterSet):
|
||||
"""印染款式明细过滤器"""
|
||||
printing_order = django_filters.NumberFilter()
|
||||
product = django_filters.NumberFilter()
|
||||
product_name = django_filters.CharFilter(field_name='product__name', lookup_expr='icontains')
|
||||
unit = django_filters.CharFilter(lookup_expr='icontains')
|
||||
quantity_min = django_filters.NumberFilter(field_name='quantity', lookup_expr='gte')
|
||||
quantity_max = django_filters.NumberFilter(field_name='quantity', lookup_expr='lte')
|
||||
pieces_min = django_filters.NumberFilter(field_name='pieces', lookup_expr='gte')
|
||||
pieces_max = django_filters.NumberFilter(field_name='pieces', lookup_expr='lte')
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = ['printing_order', 'product']
|
||||
|
||||
|
||||
class PrintingJobViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
印染款式明细 ViewSet
|
||||
|
||||
提供印染款式明细的增改查功能(不支持删除)
|
||||
|
||||
list: 获取款式明细列表
|
||||
retrieve: 获取款式明细详情
|
||||
create: 创建款式明细
|
||||
update: 更新款式明细
|
||||
partial_update: 部分更新款式明细
|
||||
|
||||
查询参数:
|
||||
- printing_order: 印染订单ID
|
||||
- product: 产品ID
|
||||
- product_name: 产品名称(模糊查询)
|
||||
- unit: 单位(模糊查询)
|
||||
- quantity_min: 最小数量
|
||||
- quantity_max: 最大数量
|
||||
- pieces_min: 最小件数
|
||||
- pieces_max: 最大件数
|
||||
- search: 全文搜索(产品名称、单位、尺寸、备注)
|
||||
- ordering: 排序字段
|
||||
"""
|
||||
queryset = models.PrintingJob.objects.all()
|
||||
permission_classes = [DjangoModelPermissions, IsPrintingFactory]
|
||||
pagination_class = LimitOffsetPagination
|
||||
filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
|
||||
filterset_class = PrintingJobFilterSet
|
||||
search_fields = ['product__name', 'unit', 'size', 'description']
|
||||
ordering_fields = ['id', 'created_at', 'updated_at', 'quantity', 'pieces']
|
||||
ordering = ['-created_at']
|
||||
|
||||
def get_serializer_class(self):
|
||||
"""根据动作选择序列化器"""
|
||||
if self.action == 'list':
|
||||
return PrintingJobListSerializer
|
||||
elif self.action in ['create', 'update', 'partial_update']:
|
||||
return PrintingJobCreateUpdateSerializer
|
||||
else: # retrieve
|
||||
return PrintingJobDetailSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
"""优化查询"""
|
||||
queryset = super().get_queryset()
|
||||
if self.action in ['list', 'retrieve']:
|
||||
queryset = queryset.select_related('printing_order', 'product')
|
||||
return queryset
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
"""禁用删除操作"""
|
||||
return Response(
|
||||
{'detail': '印染款式明细不支持删除操作'},
|
||||
status=status.HTTP_405_METHOD_NOT_ALLOWED
|
||||
)
|
||||
356
printing_job_workflow_api.yml
Normal file
356
printing_job_workflow_api.yml
Normal file
@@ -0,0 +1,356 @@
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: 印染任务流程管理 API
|
||||
description: 印染任务的流程状态管理接口
|
||||
version: 1.0.0
|
||||
|
||||
servers:
|
||||
- url: /api/v1
|
||||
description: API v1
|
||||
|
||||
paths:
|
||||
/printing-jobs/{id}/advance-to-next-state/:
|
||||
post:
|
||||
summary: 推进到下一个状态
|
||||
description: 将印染任务推进到下一个流程状态
|
||||
operationId: advanceToNextState
|
||||
tags:
|
||||
- PrintingJob Workflow
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: 印染任务ID
|
||||
schema:
|
||||
type: integer
|
||||
responses:
|
||||
'200':
|
||||
description: 推进成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
detail:
|
||||
type: string
|
||||
description: 操作结果消息
|
||||
example: "已完成状态: 打纸"
|
||||
data:
|
||||
$ref: '#/components/schemas/PrintingJobDetail'
|
||||
'400':
|
||||
description: 请求错误
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
detail:
|
||||
type: string
|
||||
example: "该任务没有关联的流程实例"
|
||||
'403':
|
||||
description: 权限不足
|
||||
'404':
|
||||
description: 任务不存在
|
||||
|
||||
/printing-jobs/{id}/step-back-one-state/:
|
||||
post:
|
||||
summary: 回退一步
|
||||
description: 将印染任务回退到上一个流程状态
|
||||
operationId: stepBackOneState
|
||||
tags:
|
||||
- PrintingJob Workflow
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: 印染任务ID
|
||||
schema:
|
||||
type: integer
|
||||
responses:
|
||||
'200':
|
||||
description: 回退成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
detail:
|
||||
type: string
|
||||
description: 操作结果消息
|
||||
example: "已回退状态: 滚筒"
|
||||
data:
|
||||
$ref: '#/components/schemas/PrintingJobDetail'
|
||||
'400':
|
||||
description: 请求错误
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
detail:
|
||||
type: string
|
||||
example: "当前没有任何状态流转记录,无法回退"
|
||||
'403':
|
||||
description: 权限不足
|
||||
'404':
|
||||
description: 任务不存在
|
||||
|
||||
/printing-jobs/{id}/completed-states/:
|
||||
get:
|
||||
summary: 查询已完成的流程列表
|
||||
description: 获取印染任务已完成的流程状态列表
|
||||
operationId: getCompletedStates
|
||||
tags:
|
||||
- PrintingJob Workflow
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: 印染任务ID
|
||||
schema:
|
||||
type: integer
|
||||
- name: include_cancelled
|
||||
in: query
|
||||
required: false
|
||||
description: 是否包含已撤销的流程记录
|
||||
schema:
|
||||
type: boolean
|
||||
default: false
|
||||
responses:
|
||||
'200':
|
||||
description: 查询成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
count:
|
||||
type: integer
|
||||
description: 已完成状态数量
|
||||
example: 2
|
||||
results:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/CompletedState'
|
||||
'403':
|
||||
description: 权限不足
|
||||
'404':
|
||||
description: 任务不存在
|
||||
|
||||
/printing-jobs/{id}/timeline/:
|
||||
get:
|
||||
summary: 获取流程时间线
|
||||
description: 获取印染任务的完整流程时间线,包括未开始、进行中和已完成的状态(不包含已撤销的记录)
|
||||
operationId: getTimeline
|
||||
tags:
|
||||
- PrintingJob Workflow
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: 印染任务ID
|
||||
schema:
|
||||
type: integer
|
||||
responses:
|
||||
'200':
|
||||
description: 查询成功
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
count:
|
||||
type: integer
|
||||
description: 流程节点总数
|
||||
example: 3
|
||||
results:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/TimelineItem'
|
||||
'403':
|
||||
description: 权限不足
|
||||
'404':
|
||||
description: 任务不存在
|
||||
|
||||
components:
|
||||
schemas:
|
||||
PrintingJobDetail:
|
||||
type: object
|
||||
description: 印染任务详情
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
description: 任务ID
|
||||
example: 29
|
||||
printing_order:
|
||||
type: integer
|
||||
description: 印染订单ID
|
||||
example: 50
|
||||
printing_order_id:
|
||||
type: string
|
||||
description: 印染订单人类可读ID
|
||||
example: "20251113000050"
|
||||
product:
|
||||
type: integer
|
||||
description: 产品ID
|
||||
example: 123
|
||||
product_name:
|
||||
type: string
|
||||
description: 产品名称
|
||||
example: "纯棉布料"
|
||||
product_code:
|
||||
type: string
|
||||
description: 产品编码
|
||||
example: "P20230001"
|
||||
quantity:
|
||||
type: integer
|
||||
description: 数量
|
||||
example: 1000
|
||||
unit:
|
||||
type: string
|
||||
description: 单位
|
||||
example: "米"
|
||||
size:
|
||||
type: string
|
||||
nullable: true
|
||||
description: 一段尺寸
|
||||
example: "100x200"
|
||||
pieces:
|
||||
type: integer
|
||||
nullable: true
|
||||
description: 件数
|
||||
example: 10
|
||||
description:
|
||||
type: string
|
||||
nullable: true
|
||||
description: 备注
|
||||
example: "特殊工艺要求"
|
||||
status:
|
||||
type: string
|
||||
description: 当前状态名称
|
||||
example: "滚筒"
|
||||
status_id:
|
||||
type: integer
|
||||
nullable: true
|
||||
description: 当前状态ID
|
||||
example: 97
|
||||
is_completed:
|
||||
type: boolean
|
||||
description: 是否已完成
|
||||
example: false
|
||||
has_started:
|
||||
type: boolean
|
||||
description: 是否已开始
|
||||
example: true
|
||||
business_object_id:
|
||||
type: integer
|
||||
nullable: true
|
||||
description: 流程实例ID
|
||||
example: 29
|
||||
created_at:
|
||||
type: string
|
||||
format: date-time
|
||||
description: 创建时间
|
||||
example: "2025-11-13T07:30:00Z"
|
||||
updated_at:
|
||||
type: string
|
||||
format: date-time
|
||||
description: 更新时间
|
||||
example: "2025-11-13T07:33:00Z"
|
||||
|
||||
CompletedState:
|
||||
type: object
|
||||
description: 已完成的状态记录
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
description: 记录ID
|
||||
example: 45
|
||||
state_id:
|
||||
type: integer
|
||||
description: 状态ID
|
||||
example: 96
|
||||
state_name:
|
||||
type: string
|
||||
description: 状态名称
|
||||
example: "打纸"
|
||||
completed_at:
|
||||
type: string
|
||||
format: date-time
|
||||
description: 完成时间
|
||||
example: "2025-11-13T07:33:00.230958Z"
|
||||
completed_by:
|
||||
type: string
|
||||
nullable: true
|
||||
description: 完成人用户名
|
||||
example: "admin"
|
||||
completed_by_name:
|
||||
type: string
|
||||
nullable: true
|
||||
description: 完成人姓名
|
||||
example: "张三"
|
||||
is_cancelled:
|
||||
type: boolean
|
||||
description: 是否已撤销
|
||||
example: false
|
||||
cancelled_at:
|
||||
type: string
|
||||
format: date-time
|
||||
nullable: true
|
||||
description: 撤销时间
|
||||
example: null
|
||||
|
||||
TimelineItem:
|
||||
type: object
|
||||
description: 流程时间线项
|
||||
properties:
|
||||
state_id:
|
||||
type: integer
|
||||
description: 状态ID
|
||||
example: 96
|
||||
state_name:
|
||||
type: string
|
||||
description: 状态名称
|
||||
example: "打纸"
|
||||
state_description:
|
||||
type: string
|
||||
nullable: true
|
||||
description: 状态描述
|
||||
example: "打印纸样"
|
||||
order:
|
||||
type: integer
|
||||
description: 状态顺序
|
||||
example: 1
|
||||
status:
|
||||
type: string
|
||||
description: 状态状态
|
||||
enum:
|
||||
- not_started
|
||||
- in_progress
|
||||
- completed
|
||||
example: "completed"
|
||||
completed_at:
|
||||
type: string
|
||||
format: date-time
|
||||
nullable: true
|
||||
description: 完成时间(仅当 status=completed 时有值)
|
||||
example: "2025-11-13T07:33:00.230958Z"
|
||||
completed_by:
|
||||
type: string
|
||||
nullable: true
|
||||
description: 完成人用户名(仅当 status=completed 时有值)
|
||||
example: "admin"
|
||||
completed_by_name:
|
||||
type: string
|
||||
nullable: true
|
||||
description: 完成人姓名(仅当 status=completed 时有值)
|
||||
example: "张三"
|
||||
|
||||
securitySchemes:
|
||||
BearerAuth:
|
||||
type: http
|
||||
scheme: bearer
|
||||
bearerFormat: JWT
|
||||
|
||||
security:
|
||||
- BearerAuth: []
|
||||
@@ -7,6 +7,18 @@ from . import models
|
||||
class StateParameterInline(admin.StackedInline):
|
||||
model = models.StateParameter
|
||||
extra = 1
|
||||
fields = ('key', 'value', 'attachment', 'description')
|
||||
readonly_fields = ('attachment_preview',)
|
||||
|
||||
def attachment_preview(self, obj):
|
||||
"""显示附件预览"""
|
||||
if obj.attachment:
|
||||
if obj.attachment.name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp')):
|
||||
return mark_safe(f'<img src="{obj.attachment.url}" style="max-width: 200px; max-height: 200px;" />')
|
||||
else:
|
||||
return mark_safe(f'<a href="{obj.attachment.url}" target="_blank">查看附件</a>')
|
||||
return "-"
|
||||
attachment_preview.short_description = '附件预览'
|
||||
|
||||
|
||||
class ProcessNodeInline(admin.StackedInline):
|
||||
@@ -34,10 +46,15 @@ class StateAdmin(admin.ModelAdmin):
|
||||
def parameter_overview(self, obj: models.State):
|
||||
params = obj.parameters.all()
|
||||
if params:
|
||||
result = "<br>".join([f"{param.key}={param.value}" for param in params])
|
||||
result = []
|
||||
for param in params:
|
||||
param_str = f"{param.key}={param.value}"
|
||||
if param.attachment:
|
||||
param_str += f' <a href="{param.attachment.url}" target="_blank">📎</a>'
|
||||
result.append(param_str)
|
||||
return mark_safe("<br>".join(result))
|
||||
else:
|
||||
result = "-"
|
||||
return mark_safe(result)
|
||||
return "-"
|
||||
|
||||
|
||||
@admin.register(models.Process)
|
||||
@@ -81,7 +98,7 @@ class BusinessObjectAdmin(admin.ModelAdmin):
|
||||
)
|
||||
search_fields = ('name', 'description')
|
||||
list_filter = ('process', 'created_at', 'updated_at')
|
||||
actions = ['advance_to_next_state_action', 'reset_progress_action']
|
||||
actions = ['advance_to_next_state_action', 'step_back_one_state_action', 'reset_progress_action']
|
||||
autocomplete_fields = ('process',)
|
||||
inlines = [StateFlowRecordInline]
|
||||
fieldsets = [
|
||||
@@ -115,6 +132,20 @@ class BusinessObjectAdmin(admin.ModelAdmin):
|
||||
|
||||
self.message_user(request, f'成功推进 {success_count}/{queryset.count()} 个业务对象。详情: {"; ".join(messages[:5])}')
|
||||
|
||||
@action(description='回退一步')
|
||||
def step_back_one_state_action(self, request, queryset):
|
||||
from . import services
|
||||
success_count = 0
|
||||
messages = []
|
||||
|
||||
for obj in queryset:
|
||||
success, message = services.step_back_one_state(obj, request.user)
|
||||
if success:
|
||||
success_count += 1
|
||||
messages.append(f"{obj.name}: {message}")
|
||||
|
||||
self.message_user(request, f'成功回退 {success_count}/{queryset.count()} 个业务对象。详情: {"; ".join(messages[:5])}')
|
||||
|
||||
@action(description='重置进度')
|
||||
def reset_progress_action(self, request, queryset):
|
||||
from . import services
|
||||
|
||||
18
stateflow/migrations/0015_stateparameter_attachment.py
Normal file
18
stateflow/migrations/0015_stateparameter_attachment.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-13 08:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('stateflow', '0014_alter_process_options'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='stateparameter',
|
||||
name='attachment',
|
||||
field=models.FileField(blank=True, null=True, upload_to='state_parameters/', verbose_name='附件'),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,23 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-13 09:00
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('stateflow', '0015_stateparameter_attachment'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='stateparameter',
|
||||
name='key',
|
||||
field=models.CharField(blank=True, max_length=100, null=True, verbose_name='参数键'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='stateparameter',
|
||||
name='value',
|
||||
field=models.CharField(blank=True, max_length=200, null=True, verbose_name='参数值'),
|
||||
),
|
||||
]
|
||||
@@ -33,8 +33,9 @@ class State(ModelBase):
|
||||
class StateParameter(ModelBase):
|
||||
"""StateParameter model representing parameters associated with a state."""
|
||||
state = models.ForeignKey(State, on_delete=models.PROTECT, related_name='parameters', verbose_name='关联参数')
|
||||
key = models.CharField(max_length=100, verbose_name='参数键')
|
||||
value = models.CharField(max_length=200, verbose_name='参数值')
|
||||
key = models.CharField(max_length=100, blank=True, null=True, verbose_name='参数键')
|
||||
value = models.CharField(max_length=200, blank=True, null=True, verbose_name='参数值')
|
||||
attachment = models.FileField(upload_to='state_parameters/', null=True, blank=True, verbose_name='附件')
|
||||
description = models.CharField(max_length=200, blank=True, verbose_name='参数描述')
|
||||
|
||||
def __str__(self):
|
||||
|
||||
@@ -9,11 +9,21 @@ from stateflow import models
|
||||
|
||||
class StateParameterSerializer(serializers.ModelSerializer):
|
||||
"""状态参数序列化器"""
|
||||
attachment_url = serializers.SerializerMethodField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.StateParameter
|
||||
fields = ['id', 'key', 'value', 'description']
|
||||
read_only_fields = ['id']
|
||||
fields = ['id', 'key', 'value', 'attachment', 'attachment_url', 'description']
|
||||
read_only_fields = ['id', 'attachment_url']
|
||||
|
||||
def get_attachment_url(self, obj):
|
||||
"""获取附件的完整 URL"""
|
||||
if obj.attachment:
|
||||
request = self.context.get('request')
|
||||
if request:
|
||||
return request.build_absolute_uri(obj.attachment.url)
|
||||
return obj.attachment.url
|
||||
return None
|
||||
|
||||
|
||||
class StateListSerializer(serializers.ModelSerializer):
|
||||
|
||||
@@ -148,6 +148,37 @@ def reset_business_object_progress(business_object: 'models.BusinessObject') ->
|
||||
reset_order_progress = reset_business_object_progress
|
||||
|
||||
|
||||
def step_back_one_state(business_object: 'models.BusinessObject', user) -> Tuple[bool, str]:
|
||||
"""
|
||||
回退一步(撤销最后一次完成的状态)
|
||||
|
||||
这是 advance_to_next_state 的逆向操作
|
||||
|
||||
返回: (是否成功, 消息)
|
||||
"""
|
||||
from django.utils import timezone
|
||||
|
||||
with transaction.atomic():
|
||||
# 获取最后一条有效的状态流转记录(按完成时间倒序)
|
||||
last_record = (
|
||||
business_object.state_logs
|
||||
.filter(is_cancelled=False)
|
||||
.order_by('-completed_at', '-id')
|
||||
.first()
|
||||
)
|
||||
|
||||
# 如果没有任何有效记录,说明当前没有任何状态流转,无需回退
|
||||
if not last_record:
|
||||
return False, "当前没有任何状态流转记录,无法回退"
|
||||
|
||||
# 标记最后一条记录为已撤销
|
||||
last_record.is_cancelled = True
|
||||
last_record.cancelled_at = timezone.now()
|
||||
last_record.save(update_fields=['is_cancelled', 'cancelled_at'])
|
||||
|
||||
return True, f"已回退状态: {last_record.state.name}"
|
||||
|
||||
|
||||
def get_current_state_parameters(business_object: 'models.BusinessObject') -> List['models.StateParameter']:
|
||||
"""获取订单当前状态的参数列表"""
|
||||
current_state = get_business_object_current_state(business_object)
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
Stateflow API 测试
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework import status
|
||||
from django.contrib.auth import get_user_model
|
||||
from stateflow import models
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
@@ -92,6 +95,140 @@ class StateAPITestCase(TestCase):
|
||||
response = self.client.get('/api/v1/stateflow/states/?search=审核&limit=10&offset=0')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 1)
|
||||
|
||||
def test_create_state_parameter_with_attachment(self):
|
||||
"""测试创建带附件的状态参数"""
|
||||
# 创建一个测试文件
|
||||
test_file = SimpleUploadedFile(
|
||||
"test_doc.txt",
|
||||
b"This is a test document content",
|
||||
content_type="text/plain"
|
||||
)
|
||||
|
||||
# 先创建状态
|
||||
state = models.State.objects.create(name='测试状态', description='带附件')
|
||||
|
||||
# 创建带附件的参数
|
||||
param = models.StateParameter.objects.create(
|
||||
state=state,
|
||||
key='document',
|
||||
value='测试文档',
|
||||
attachment=test_file,
|
||||
description='这是一个测试文档'
|
||||
)
|
||||
|
||||
# 验证附件已保存
|
||||
self.assertIsNotNone(param.attachment)
|
||||
self.assertIn('test_doc', param.attachment.name) # 文件名可能有哈希前缀
|
||||
|
||||
# 通过 API 获取状态详情,验证附件字段
|
||||
response = self.client.get(f'/api/v1/stateflow/states/{state.id}/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证参数中包含附件信息
|
||||
parameters = response.data['parameters']
|
||||
self.assertEqual(len(parameters), 1)
|
||||
self.assertIn('attachment', parameters[0])
|
||||
self.assertIn('attachment_url', parameters[0])
|
||||
self.assertIsNotNone(parameters[0]['attachment_url'])
|
||||
|
||||
# 清理测试文件
|
||||
if param.attachment:
|
||||
param.attachment.delete()
|
||||
|
||||
def test_state_parameter_without_attachment(self):
|
||||
"""测试创建不带附件的状态参数"""
|
||||
state = models.State.objects.create(name='测试状态2', description='不带附件')
|
||||
param = models.StateParameter.objects.create(
|
||||
state=state,
|
||||
key='simple_param',
|
||||
value='简单值',
|
||||
description='简单参数'
|
||||
)
|
||||
|
||||
# 验证附件字段为空
|
||||
self.assertFalse(param.attachment)
|
||||
|
||||
# 通过 API 获取状态详情
|
||||
response = self.client.get(f'/api/v1/stateflow/states/{state.id}/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证附件字段为 null
|
||||
parameters = response.data['parameters']
|
||||
self.assertEqual(len(parameters), 1)
|
||||
self.assertIsNone(parameters[0]['attachment'])
|
||||
self.assertIsNone(parameters[0]['attachment_url'])
|
||||
|
||||
def test_update_state_parameter_with_attachment(self):
|
||||
"""测试更新状态参数时添加附件"""
|
||||
# 创建初始状态和参数(不带附件)
|
||||
state = models.State.objects.create(name='测试状态3', description='更新附件')
|
||||
param = models.StateParameter.objects.create(
|
||||
state=state,
|
||||
key='updatable_param',
|
||||
value='初始值',
|
||||
description='可更新参数'
|
||||
)
|
||||
|
||||
# 验证初始无附件
|
||||
self.assertFalse(param.attachment)
|
||||
|
||||
# 添加附件
|
||||
test_file = SimpleUploadedFile(
|
||||
"updated_doc.pdf",
|
||||
b"Updated document content",
|
||||
content_type="application/pdf"
|
||||
)
|
||||
param.attachment = test_file
|
||||
param.save()
|
||||
|
||||
# 验证附件已添加
|
||||
param.refresh_from_db()
|
||||
self.assertIsNotNone(param.attachment)
|
||||
self.assertIn('updated_doc', param.attachment.name) # 文件名可能有哈希前缀
|
||||
|
||||
# 通过 API 验证
|
||||
response = self.client.get(f'/api/v1/stateflow/states/{state.id}/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
parameters = response.data['parameters']
|
||||
self.assertIsNotNone(parameters[0]['attachment'])
|
||||
self.assertIsNotNone(parameters[0]['attachment_url'])
|
||||
|
||||
# 清理
|
||||
if param.attachment:
|
||||
param.attachment.delete()
|
||||
|
||||
def test_state_parameter_attachment_url_format(self):
|
||||
"""测试附件 URL 格式正确"""
|
||||
test_file = SimpleUploadedFile(
|
||||
"test_image.jpg",
|
||||
b"fake image content",
|
||||
content_type="image/jpeg"
|
||||
)
|
||||
|
||||
state = models.State.objects.create(name='图片状态', description='带图片')
|
||||
param = models.StateParameter.objects.create(
|
||||
state=state,
|
||||
key='image',
|
||||
value='测试图片',
|
||||
attachment=test_file
|
||||
)
|
||||
|
||||
# 通过 API 获取
|
||||
response = self.client.get(f'/api/v1/stateflow/states/{state.id}/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
parameters = response.data['parameters']
|
||||
attachment_url = parameters[0]['attachment_url']
|
||||
|
||||
# 验证 URL 格式
|
||||
self.assertIsNotNone(attachment_url)
|
||||
self.assertIn('http', attachment_url) # 应该是完整 URL
|
||||
self.assertIn('state_parameters/', attachment_url) # 包含上传路径
|
||||
|
||||
# 清理
|
||||
if param.attachment:
|
||||
param.attachment.delete()
|
||||
|
||||
|
||||
class ProcessAPITestCase(TestCase):
|
||||
|
||||
1
stateflow/tests/__init__.py
Normal file
1
stateflow/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# Stateflow tests
|
||||
182
stateflow/tests/test_step_back.py
Normal file
182
stateflow/tests/test_step_back.py
Normal file
@@ -0,0 +1,182 @@
|
||||
"""
|
||||
测试单步回退功能
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.contrib.auth import get_user_model
|
||||
from stateflow import models, services
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class StepBackTestCase(TestCase):
|
||||
"""测试单步回退功能"""
|
||||
|
||||
def setUp(self):
|
||||
# 创建测试用户
|
||||
self.user = User.objects.create_user(username='testuser', password='testpass')
|
||||
|
||||
# 创建状态
|
||||
self.state1 = models.State.objects.create(name='状态1', description='第一个状态')
|
||||
self.state2 = models.State.objects.create(name='状态2', description='第二个状态')
|
||||
self.state3 = models.State.objects.create(name='状态3', description='第三个状态')
|
||||
|
||||
# 创建流程
|
||||
self.process = models.Process.objects.create(name='测试流程', description='三个状态的测试流程')
|
||||
|
||||
# 创建流程节点
|
||||
models.ProcessNode.objects.create(process=self.process, state=self.state1, order=1)
|
||||
models.ProcessNode.objects.create(process=self.process, state=self.state2, order=2)
|
||||
models.ProcessNode.objects.create(process=self.process, state=self.state3, order=3)
|
||||
|
||||
# 创建业务对象
|
||||
self.business_object = models.BusinessObject.objects.create(
|
||||
name='测试业务对象',
|
||||
process=self.process,
|
||||
description='测试回退功能'
|
||||
)
|
||||
|
||||
def test_step_back_from_not_started(self):
|
||||
"""测试从未开始状态回退(应该返回失败)"""
|
||||
success, message = services.step_back_one_state(self.business_object, self.user)
|
||||
|
||||
self.assertFalse(success)
|
||||
self.assertIn('没有任何状态流转记录', message)
|
||||
|
||||
# 验证仍然是未开始状态
|
||||
self.assertEqual(services.get_overall_status(self.business_object), 'not_started')
|
||||
|
||||
def test_step_back_from_first_state(self):
|
||||
"""测试从第一个状态回退到未开始"""
|
||||
# 推进到第一个状态
|
||||
success, _ = services.advance_to_next_state(self.business_object, self.user)
|
||||
self.assertTrue(success)
|
||||
|
||||
# 验证当前在第一个状态(进行中,准备完成第二个状态)
|
||||
current_state = services.get_business_object_current_state(self.business_object)
|
||||
self.assertEqual(current_state.id, self.state2.id)
|
||||
|
||||
# 回退一步
|
||||
success, message = services.step_back_one_state(self.business_object, self.user)
|
||||
self.assertTrue(success)
|
||||
self.assertIn('状态1', message)
|
||||
|
||||
# 验证回到未开始状态
|
||||
self.assertEqual(services.get_overall_status(self.business_object), 'not_started')
|
||||
|
||||
# 验证状态1的记录已被撤销
|
||||
record = models.StateFlowRecord.objects.filter(
|
||||
business_object=self.business_object,
|
||||
state=self.state1
|
||||
).first()
|
||||
self.assertTrue(record.is_cancelled)
|
||||
self.assertIsNotNone(record.cancelled_at)
|
||||
|
||||
def test_step_back_from_middle_state(self):
|
||||
"""测试从中间状态回退"""
|
||||
# 推进到第二个状态
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
|
||||
# 验证当前在第二个状态(进行中,准备完成第三个状态)
|
||||
current_state = services.get_business_object_current_state(self.business_object)
|
||||
self.assertEqual(current_state.id, self.state3.id)
|
||||
|
||||
# 回退一步
|
||||
success, message = services.step_back_one_state(self.business_object, self.user)
|
||||
self.assertTrue(success)
|
||||
self.assertIn('状态2', message)
|
||||
|
||||
# 验证回到第一个状态(进行中,准备完成第二个状态)
|
||||
current_state = services.get_business_object_current_state(self.business_object)
|
||||
self.assertEqual(current_state.id, self.state2.id)
|
||||
|
||||
# 验证状态2的记录已被撤销
|
||||
record = models.StateFlowRecord.objects.filter(
|
||||
business_object=self.business_object,
|
||||
state=self.state2
|
||||
).first()
|
||||
self.assertTrue(record.is_cancelled)
|
||||
|
||||
def test_step_back_from_completed(self):
|
||||
"""测试从已完成状态回退"""
|
||||
# 推进到完成
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
|
||||
# 验证已完成
|
||||
self.assertEqual(services.get_overall_status(self.business_object), 'completed')
|
||||
|
||||
# 回退一步
|
||||
success, message = services.step_back_one_state(self.business_object, self.user)
|
||||
self.assertTrue(success)
|
||||
self.assertIn('状态3', message)
|
||||
|
||||
# 验证回到进行中状态(准备完成第三个状态)
|
||||
self.assertEqual(services.get_overall_status(self.business_object), 'in_progress')
|
||||
current_state = services.get_business_object_current_state(self.business_object)
|
||||
self.assertEqual(current_state.id, self.state3.id)
|
||||
|
||||
def test_step_back_multiple_times(self):
|
||||
"""测试多次回退"""
|
||||
# 推进到完成
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
|
||||
# 第一次回退
|
||||
success, _ = services.step_back_one_state(self.business_object, self.user)
|
||||
self.assertTrue(success)
|
||||
current = services.get_business_object_current_state(self.business_object)
|
||||
self.assertEqual(current.id, self.state3.id)
|
||||
|
||||
# 第二次回退
|
||||
success, _ = services.step_back_one_state(self.business_object, self.user)
|
||||
self.assertTrue(success)
|
||||
current = services.get_business_object_current_state(self.business_object)
|
||||
self.assertEqual(current.id, self.state2.id)
|
||||
|
||||
# 第三次回退
|
||||
success, _ = services.step_back_one_state(self.business_object, self.user)
|
||||
self.assertTrue(success)
|
||||
self.assertEqual(services.get_overall_status(self.business_object), 'not_started')
|
||||
|
||||
# 第四次回退(应该失败)
|
||||
success, message = services.step_back_one_state(self.business_object, self.user)
|
||||
self.assertFalse(success)
|
||||
self.assertIn('没有任何状态流转记录', message)
|
||||
|
||||
def test_advance_after_step_back(self):
|
||||
"""测试回退后再前进"""
|
||||
# 推进两步
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
|
||||
# 回退一步
|
||||
services.step_back_one_state(self.business_object, self.user)
|
||||
|
||||
# 再前进
|
||||
success, _ = services.advance_to_next_state(self.business_object, self.user)
|
||||
self.assertTrue(success)
|
||||
|
||||
# 验证当前在第二个状态
|
||||
current_state = services.get_business_object_current_state(self.business_object)
|
||||
self.assertEqual(current_state.id, self.state3.id)
|
||||
|
||||
def test_step_back_preserves_history(self):
|
||||
"""测试回退操作保留历史记录"""
|
||||
# 推进两步
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
|
||||
# 回退一步
|
||||
services.step_back_one_state(self.business_object, self.user)
|
||||
|
||||
# 验证记录仍然存在,只是被标记为已撤销
|
||||
all_records = models.StateFlowRecord.objects.filter(
|
||||
business_object=self.business_object
|
||||
).order_by('completed_at')
|
||||
|
||||
self.assertEqual(all_records.count(), 2)
|
||||
self.assertFalse(all_records[0].is_cancelled) # 状态1未撤销
|
||||
self.assertTrue(all_records[1].is_cancelled) # 状态2已撤销
|
||||
81
stateflow/tests/test_step_back_api.py
Normal file
81
stateflow/tests/test_step_back_api.py
Normal file
@@ -0,0 +1,81 @@
|
||||
"""
|
||||
测试 step_back API 端点
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework import status
|
||||
from django.contrib.auth import get_user_model
|
||||
from stateflow import models
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class StepBackAPITestCase(TestCase):
|
||||
"""测试单步回退 API"""
|
||||
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
|
||||
# 创建用户
|
||||
self.user = User.objects.create_user(username='testuser', password='testpass')
|
||||
self.client.force_authenticate(user=self.user)
|
||||
|
||||
# 创建状态
|
||||
self.state1 = models.State.objects.create(name='状态1')
|
||||
self.state2 = models.State.objects.create(name='状态2')
|
||||
self.state3 = models.State.objects.create(name='状态3')
|
||||
|
||||
# 创建流程
|
||||
self.process = models.Process.objects.create(name='测试流程')
|
||||
models.ProcessNode.objects.create(process=self.process, state=self.state1, order=1)
|
||||
models.ProcessNode.objects.create(process=self.process, state=self.state2, order=2)
|
||||
models.ProcessNode.objects.create(process=self.process, state=self.state3, order=3)
|
||||
|
||||
# 创建业务对象
|
||||
self.business_object = models.BusinessObject.objects.create(
|
||||
name='测试对象',
|
||||
process=self.process
|
||||
)
|
||||
|
||||
def test_step_back_api_success(self):
|
||||
"""测试回退 API 成功"""
|
||||
# 先推进两步
|
||||
self.client.post(f'/api/v1/stateflow/business-objects/{self.business_object.id}/advance/')
|
||||
self.client.post(f'/api/v1/stateflow/business-objects/{self.business_object.id}/advance/')
|
||||
|
||||
# 回退一步
|
||||
response = self.client.post(f'/api/v1/stateflow/business-objects/{self.business_object.id}/step_back/')
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertTrue(response.data['success'])
|
||||
self.assertIn('状态2', response.data['message'])
|
||||
self.assertIn('business_object', response.data)
|
||||
|
||||
def test_step_back_api_not_started(self):
|
||||
"""测试未开始状态回退失败"""
|
||||
response = self.client.post(f'/api/v1/stateflow/business-objects/{self.business_object.id}/step_back/')
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertFalse(response.data['success'])
|
||||
self.assertIn('没有任何状态流转记录', response.data['message'])
|
||||
|
||||
def test_advance_step_back_advance_cycle(self):
|
||||
"""测试前进-回退-前进循环"""
|
||||
# 前进
|
||||
response1 = self.client.post(f'/api/v1/stateflow/business-objects/{self.business_object.id}/advance/')
|
||||
self.assertEqual(response1.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 回退
|
||||
response2 = self.client.post(f'/api/v1/stateflow/business-objects/{self.business_object.id}/step_back/')
|
||||
self.assertEqual(response2.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 再前进
|
||||
response3 = self.client.post(f'/api/v1/stateflow/business-objects/{self.business_object.id}/advance/')
|
||||
self.assertEqual(response3.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证数据库中有已撤销的记录
|
||||
cancelled_count = models.StateFlowRecord.objects.filter(
|
||||
business_object=self.business_object,
|
||||
is_cancelled=True
|
||||
).count()
|
||||
self.assertEqual(cancelled_count, 1)
|
||||
33
开版管理字段.md
Normal file
33
开版管理字段.md
Normal file
@@ -0,0 +1,33 @@
|
||||
| 字段名称 | 明道云 | 本地系统 | 状态 | 类型 | 说明 |
|
||||
|---------|-------|---------|------|------|------|
|
||||
| 自动编号 | ✅ | ❌ | **需添加** `plate_code` | CharField(50, unique) | 自动生成的版单编号(如:20251107-1) |
|
||||
| 设计编号 | ✅ | ❌ | **需添加** `design_code` | CharField(50) | 设计编号 |
|
||||
| 起版情况 | ✅ | ❌ | **需添加** `plate_type` | CharField(20) | 首版/复版等 |
|
||||
| 下版时间 | ✅ | ❌ | **需添加** `plate_date` | DateTimeField | 下版日期时间 |
|
||||
| 开发进程 | ✅ | ❌ | **需添加** `development_status` | CharField(20) | 未打印/待画图/画图完成/调色样/调色完成/套纸样/取消版/客户审批/开版完成/已下单等 |
|
||||
| 紧急程度 | ✅ | ❌ | **需添加** `urgency_level` | CharField(20) | 正常/加急等 |
|
||||
| 销售员ID | ✅ | ❌ | **需添加** `salesperson_id` | ForeignKey(Salesperson) | 关联销售员/业务员 |
|
||||
| 跟单员ID | ✅ | ❌ | **需添加** `salesperson_id` | ForeignKey(Salesperson) | 关联业务员 |
|
||||
| 区域 | ✅ | ❌ | **需添加** `area` | CharField(255) | 区域信息 |
|
||||
| 默认地址 | ✅ | ❌ | **需添加** `default_address` | CharField(500) | 默认地址 |
|
||||
| 客户ID | ✅ | ❌ | **需添加** `customer_id` | ForeignKey(Customer) | 关联客户 |
|
||||
| 是否套唛架 | ✅ | ❌ | **需添加** `is_mark_frame` | BooleanField | 布尔值 |
|
||||
| 画图评级 | ✅ | ❌ | **需添加** `drawing_rating` | CharField(20) | 画图质量评级 |
|
||||
| 调色评级 | ✅ | ❌ | **需添加** `color_matching_rating` | CharField(20) | 调色质量评级 |
|
||||
| 套样评级 | ✅ | ❌ | **需添加** `sample_rating` | CharField(20) | 套样质量评级 |
|
||||
| 难度评级 | ✅ | ❌ | **需添加** `difficulty_rating` | CharField(20) | 难度评级 |
|
||||
| 要求完成时间 | ✅ | ❌ | **需添加** `required_completion_date` | DateField | 要求完成日期 |
|
||||
| 布料 | ✅ | ❌ | **需添加** `fabric` | CharField(100) | 布料信息 |
|
||||
| 幅宽 | ✅ | ❌ | **需添加** `width` | CharField(50) | 幅宽 |
|
||||
| 款号名称 | ✅ | ❌ | **需添加** `style_name` | CharField(100) | 款号名称 |
|
||||
| 完成时间 | ✅ | ❌ | **需添加** `completion_date` | DateTimeField | 完成时间 |
|
||||
| 做货方式 | ✅ | ❌ | **需添加** `production_method` | CharField(50) | 做货方式 |
|
||||
| 开版方式 | ✅ | ❌ | **需添加** `plate_method` | CharField(50) | 开版方式 |
|
||||
| 开版图 | ✅ | ❌ | **需添加** `plate_image` | FileField/ImageField | 开版图附件 |
|
||||
| 米样 | ✅ | ❌ | **需添加** `sample_meter` | CharField(100) | 米样信息 |
|
||||
| 客户要求米样米数 | ✅ | ❌ | **需添加** `required_sample_meters` | DecimalField(10,2) | 客户要求的米样米数 |
|
||||
| 复版原因 | ✅ | ❌ | **需添加** `reprint_reason` | TextField | 复版原因 |
|
||||
| 审批结果 | ✅ | ❌ | **需添加** `approval_result` | CharField(50) | 审批结果 |
|
||||
| 是否已下单 | ✅ | ❌ | **需添加** `is_ordered` | BooleanField | 布尔值,是否已下单 |
|
||||
| 客户修改意见 | ✅ | ❌ | **需添加** `customer_feedback` | TextField | 客户修改意见 |
|
||||
| 打版注意事项 | ✅ | ❌ | **需添加** `plate_notes` | TextField | 打版注意事项 |
|
||||
Reference in New Issue
Block a user