forked from erp-dev/erp
feat: added printing module
This commit is contained in:
@@ -4,4 +4,4 @@ from django.apps import AppConfig
|
||||
class StateflowConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'stateflow'
|
||||
verbose_name = '印染流程'
|
||||
verbose_name = '任务流程'
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-12 04:08
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('stateflow', '0012_alter_businessobject_content_type_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterUniqueTogether(
|
||||
name='stateflowrecord',
|
||||
unique_together=set(),
|
||||
),
|
||||
]
|
||||
17
stateflow/migrations/0014_alter_process_options.py
Normal file
17
stateflow/migrations/0014_alter_process_options.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-12 06:19
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('stateflow', '0013_alter_stateflowrecord_unique_together'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='process',
|
||||
options={'verbose_name': '流程编排', 'verbose_name_plural': '流程编排'},
|
||||
),
|
||||
]
|
||||
@@ -72,8 +72,8 @@ class Process(ModelBase):
|
||||
return [item.state for item in process_nodes]
|
||||
|
||||
class Meta:
|
||||
verbose_name = '印染流程'
|
||||
verbose_name_plural = '印染流程'
|
||||
verbose_name = '流程编排'
|
||||
verbose_name_plural = '流程编排'
|
||||
|
||||
|
||||
class ProcessNode(ModelBase):
|
||||
@@ -155,5 +155,5 @@ class StateFlowRecord(ModelBase):
|
||||
db_table = 'state_flow_record'
|
||||
verbose_name = '状态流转记录'
|
||||
verbose_name_plural = '状态流转记录'
|
||||
unique_together = [['business_object', 'state']]
|
||||
# 移除 unique_together 约束,允许同一状态多次记录(支持撤销后重新执行)
|
||||
ordering = ['completed_at']
|
||||
|
||||
@@ -83,14 +83,18 @@ def get_progress_percentage(business_object: 'models.BusinessObject') -> float:
|
||||
return (completed_count / total_nodes) * 100
|
||||
|
||||
|
||||
def advance_to_next_state(business_object: 'models.BusinessObject', user: 'User') -> Tuple[bool, str]:
|
||||
def advance_to_next_state(business_object: 'models.BusinessObject', user) -> Tuple[bool, str]:
|
||||
"""
|
||||
将订单推进到下一个状态
|
||||
|
||||
返回: (是否成功, 消息)
|
||||
"""
|
||||
can_advance, reason = can_advance_to_next_state(business_object)
|
||||
if not can_advance:
|
||||
return False, reason
|
||||
|
||||
with transaction.atomic():
|
||||
# 获取当前状态
|
||||
# 获取当前状态(返回第一个未完成的节点)
|
||||
current_state = get_business_object_current_state(business_object)
|
||||
|
||||
# 如果当前状态为 None,说明是初始状态(未开始)
|
||||
@@ -108,10 +112,7 @@ def advance_to_next_state(business_object: 'models.BusinessObject', user: 'User'
|
||||
)
|
||||
return True, f"已完成状态: {first_node.state.name}"
|
||||
|
||||
# 检查当前状态是否已完成(且未撤销)
|
||||
if business_object.state_logs.filter(state=current_state, is_cancelled=False).exists():
|
||||
return False, f"当前状态 '{current_state.name}' 已完成"
|
||||
|
||||
# current_state 不为 None 时,它一定是未完成的节点
|
||||
# 标记当前状态为已完成
|
||||
models.StateFlowRecord.objects.create(
|
||||
business_object=business_object,
|
||||
@@ -128,12 +129,12 @@ def advance_to_next_state(business_object: 'models.BusinessObject', user: 'User'
|
||||
return True, f"已完成状态: {current_state.name}"
|
||||
|
||||
|
||||
def reset_order_progress(business_object: 'models.BusinessObject') -> None:
|
||||
def reset_business_object_progress(business_object: 'models.BusinessObject') -> None:
|
||||
"""
|
||||
重置订单进度
|
||||
重置业务对象进度
|
||||
|
||||
注意:不删除历史记录,而是标记所有记录为已撤销,并记录撤销时间
|
||||
这样可以保留完整的操作历史
|
||||
这样可以保留完整的操作历史,支持审计需求
|
||||
"""
|
||||
from django.utils import timezone
|
||||
|
||||
@@ -143,6 +144,10 @@ def reset_order_progress(business_object: 'models.BusinessObject') -> None:
|
||||
)
|
||||
|
||||
|
||||
# 向后兼容的别名
|
||||
reset_order_progress = reset_business_object_progress
|
||||
|
||||
|
||||
def get_current_state_parameters(business_object: 'models.BusinessObject') -> List['models.StateParameter']:
|
||||
"""获取订单当前状态的参数列表"""
|
||||
current_state = get_business_object_current_state(business_object)
|
||||
@@ -188,10 +193,10 @@ def can_advance_to_next_state(business_object: 'models.BusinessObject') -> Tuple
|
||||
|
||||
current_state = get_business_object_current_state(business_object)
|
||||
|
||||
# 如果当前状态为 None
|
||||
# 如果当前状态为 None,有两种情况:
|
||||
# 1. 没有完成记录 - 未开始
|
||||
# 2. 所有节点已完成 - 已完成
|
||||
if current_state is None:
|
||||
# 检查是否所有状态都已完成
|
||||
total_nodes = business_object.process.process_nodes.count()
|
||||
completed_count = business_object.state_logs.filter(is_cancelled=False).count()
|
||||
|
||||
if completed_count == 0:
|
||||
@@ -201,10 +206,7 @@ def can_advance_to_next_state(business_object: 'models.BusinessObject') -> Tuple
|
||||
# 所有状态都已完成
|
||||
return False, "流程已完成,无法继续推进"
|
||||
|
||||
# 检查当前状态是否已完成
|
||||
if business_object.state_logs.filter(state=current_state, is_cancelled=False).exists():
|
||||
return False, f"当前状态 '{current_state.name}' 已完成,无法重复完成"
|
||||
|
||||
# current_state 不为 None 时,它一定是第一个未完成的节点,可以推进
|
||||
return True, f"可以推进到: {current_state.name}"
|
||||
|
||||
|
||||
|
||||
@@ -121,7 +121,7 @@ class OrderStateFlowTestCase(TestCase):
|
||||
self.assertEqual(self.business_object.state_logs.count(), 2)
|
||||
|
||||
# 重置进度
|
||||
services.reset_order_progress(self.business_object)
|
||||
services.reset_business_object_progress(self.business_object)
|
||||
|
||||
# 日志记录应该还在(不删除)
|
||||
self.assertEqual(self.business_object.state_logs.count(), 2)
|
||||
@@ -147,7 +147,7 @@ class OrderStateFlowTestCase(TestCase):
|
||||
services.advance_to_next_state(self.business_object, self.user)
|
||||
|
||||
# 重置进度
|
||||
services.reset_order_progress(self.business_object)
|
||||
services.reset_business_object_progress(self.business_object)
|
||||
|
||||
timeline = self.business_object.get_timeline()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user