forked from erp-dev/erp
fix
This commit is contained in:
@@ -403,6 +403,14 @@ STATEFLOW_CURRENT_STATE_MODE = 'NEXT'
|
|||||||
PRINTING_DEFAULT_PROCESS_ID = 1 # 默认印染流程ID
|
PRINTING_DEFAULT_PROCESS_ID = 1 # 默认印染流程ID
|
||||||
PLATE_ORDER_DEFAULT_PROCESS_ID = 2 # 默认开版流程ID
|
PLATE_ORDER_DEFAULT_PROCESS_ID = 2 # 默认开版流程ID
|
||||||
|
|
||||||
|
# 销售品自动创建配置(PrintingJob 流程完成时)
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# 指定从哪个流程节点获取数量参数,单位固定为"米"
|
||||||
|
# 如果该节点未完成或参数不存在,则不创建销售品
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
PRINTING_SALES_ITEM_SOURCE_STATE_ID = 2 # 包含米数参数的 State ID
|
||||||
|
PRINTING_SALES_ITEM_QUANTITY_KEY = '米数' # 数量参数的 key
|
||||||
|
|
||||||
# API 审计日志配置
|
# API 审计日志配置
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# 用于记录特定模块POST请求的历史现场,便于追踪"创建"操作。
|
# 用于记录特定模块POST请求的历史现场,便于追踪"创建"操作。
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ Printing 模块信号处理函数
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from decimal import Decimal, InvalidOperation
|
||||||
|
from django.conf import settings
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -15,40 +17,80 @@ def on_printing_job_process_completed(sender, **kwargs):
|
|||||||
PrintingJob 流程完成时的处理
|
PrintingJob 流程完成时的处理
|
||||||
|
|
||||||
当 PrintingJob 的工艺流程全部完成时,自动创建 SalesItem(销售品)。
|
当 PrintingJob 的工艺流程全部完成时,自动创建 SalesItem(销售品)。
|
||||||
|
数量和单位从指定的流程节点参数中获取(通过 settings 配置)。
|
||||||
|
|
||||||
|
配置项:
|
||||||
|
PRINTING_SALES_ITEM_SOURCE_STATE_ID: 指定从哪个 State 获取参数
|
||||||
|
PRINTING_SALES_ITEM_QUANTITY_KEY: 数量参数的 key
|
||||||
|
PRINTING_SALES_ITEM_UNIT_KEY: 单位参数的 key
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
sender: PrintingJob 类
|
sender: PrintingJob 类
|
||||||
content_object: PrintingJob 实例
|
content_object: PrintingJob 实例
|
||||||
|
business_object: BusinessObject 实例
|
||||||
last_completed_by: 最后步骤操作人
|
last_completed_by: 最后步骤操作人
|
||||||
其他参数见 stateflow.signals.process_completed
|
其他参数见 stateflow.signals.process_completed
|
||||||
"""
|
"""
|
||||||
from shipment.models import SalesItem, UnitChoices
|
from shipment.models import SalesItem, UnitChoices
|
||||||
|
|
||||||
content_object = kwargs.get('content_object')
|
content_object = kwargs.get('content_object')
|
||||||
|
business_object = kwargs.get('business_object')
|
||||||
last_completed_by = kwargs.get('last_completed_by')
|
last_completed_by = kwargs.get('last_completed_by')
|
||||||
|
|
||||||
if content_object is None:
|
if content_object is None or business_object is None:
|
||||||
logger.warning('process_completed 信号缺少 content_object')
|
logger.warning('process_completed 信号缺少 content_object 或 business_object')
|
||||||
return
|
return
|
||||||
|
|
||||||
printing_job = content_object
|
printing_job = content_object
|
||||||
|
|
||||||
# 单位映射:PrintingJob.unit(字符串)→ SalesItem.unit(IntegerChoices)
|
# 从 settings 获取配置
|
||||||
unit_mapping = {
|
source_state_id = getattr(settings, 'PRINTING_SALES_ITEM_SOURCE_STATE_ID', None)
|
||||||
'米': UnitChoices.METER,
|
quantity_key = getattr(settings, 'PRINTING_SALES_ITEM_QUANTITY_KEY', '米数')
|
||||||
'm': UnitChoices.METER,
|
|
||||||
'M': UnitChoices.METER,
|
|
||||||
'件': UnitChoices.PIECE,
|
|
||||||
'码': UnitChoices.YARD,
|
|
||||||
'yd': UnitChoices.YARD,
|
|
||||||
'YD': UnitChoices.YARD,
|
|
||||||
'个': UnitChoices.UNIT,
|
|
||||||
'pcs': UnitChoices.UNIT,
|
|
||||||
'PCS': UnitChoices.UNIT,
|
|
||||||
}
|
|
||||||
|
|
||||||
# 默认单位为"件"
|
if source_state_id is None:
|
||||||
sales_unit = unit_mapping.get(printing_job.unit, UnitChoices.PIECE)
|
logger.debug(
|
||||||
|
f'PrintingJob #{printing_job.id} 流程完成,'
|
||||||
|
f'但 PRINTING_SALES_ITEM_SOURCE_STATE_ID 未配置,跳过销售品创建'
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# 从 business_object 的状态流转记录中找到指定 state_id 的记录
|
||||||
|
state_log = business_object.state_logs.filter(
|
||||||
|
state_id=source_state_id,
|
||||||
|
is_cancelled=False
|
||||||
|
).first()
|
||||||
|
|
||||||
|
if state_log is None:
|
||||||
|
logger.warning(
|
||||||
|
f'PrintingJob #{printing_job.id} 流程完成,'
|
||||||
|
f'但未找到 state_id={source_state_id} 的已完成记录,跳过销售品创建'
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# 获取参数汇总
|
||||||
|
params = state_log.get_all_parameters_summary()
|
||||||
|
|
||||||
|
# 获取数量
|
||||||
|
quantity_raw = params.get(quantity_key)
|
||||||
|
if quantity_raw is None:
|
||||||
|
logger.warning(
|
||||||
|
f'PrintingJob #{printing_job.id} 流程完成,'
|
||||||
|
f'但 state_id={source_state_id} 的参数中缺少 {quantity_key},跳过销售品创建'
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# 转换数量为 Decimal
|
||||||
|
try:
|
||||||
|
quantity = Decimal(str(quantity_raw))
|
||||||
|
except (InvalidOperation, ValueError, TypeError) as e:
|
||||||
|
logger.warning(
|
||||||
|
f'PrintingJob #{printing_job.id} 流程完成,'
|
||||||
|
f'但 {quantity_key}={quantity_raw} 无法转换为数字: {e},跳过销售品创建'
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# 单位固定为"米"
|
||||||
|
sales_unit = UnitChoices.METER
|
||||||
|
|
||||||
# 获取产品名称
|
# 获取产品名称
|
||||||
product_name = printing_job.product.name if printing_job.product else '未知产品'
|
product_name = printing_job.product.name if printing_job.product else '未知产品'
|
||||||
@@ -63,7 +105,7 @@ def on_printing_job_process_completed(sender, **kwargs):
|
|||||||
sales_item = SalesItem.objects.create(
|
sales_item = SalesItem.objects.create(
|
||||||
shipment=None, # 暂不关联出货单,待后续分配
|
shipment=None, # 暂不关联出货单,待后续分配
|
||||||
name=product_name,
|
name=product_name,
|
||||||
quantity=printing_job.quantity,
|
quantity=quantity,
|
||||||
unit=sales_unit,
|
unit=sales_unit,
|
||||||
created_by=last_completed_by,
|
created_by=last_completed_by,
|
||||||
printing_job_id=printing_job.id,
|
printing_job_id=printing_job.id,
|
||||||
@@ -73,7 +115,7 @@ def on_printing_job_process_completed(sender, **kwargs):
|
|||||||
logger.info(
|
logger.info(
|
||||||
f'PrintingJob #{printing_job.id} 流程完成,'
|
f'PrintingJob #{printing_job.id} 流程完成,'
|
||||||
f'已创建销售品 SalesItem #{sales_item.id}: '
|
f'已创建销售品 SalesItem #{sales_item.id}: '
|
||||||
f'{product_name} x {printing_job.quantity}'
|
f'{product_name} x {quantity} {sales_item.get_unit_display()}'
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user