1
0
forked from erp-dev/erp
This commit is contained in:
2026-01-13 18:40:47 +08:00
parent 96ac459964
commit 5e6834358d
2 changed files with 69 additions and 19 deletions

View File

@@ -5,6 +5,8 @@ Printing 模块信号处理函数
"""
import logging
from decimal import Decimal, InvalidOperation
from django.conf import settings
from django.db import transaction
logger = logging.getLogger(__name__)
@@ -15,40 +17,80 @@ def on_printing_job_process_completed(sender, **kwargs):
PrintingJob 流程完成时的处理
当 PrintingJob 的工艺流程全部完成时,自动创建 SalesItem销售品
数量和单位从指定的流程节点参数中获取(通过 settings 配置)。
配置项:
PRINTING_SALES_ITEM_SOURCE_STATE_ID: 指定从哪个 State 获取参数
PRINTING_SALES_ITEM_QUANTITY_KEY: 数量参数的 key
PRINTING_SALES_ITEM_UNIT_KEY: 单位参数的 key
Args:
sender: PrintingJob 类
content_object: PrintingJob 实例
business_object: BusinessObject 实例
last_completed_by: 最后步骤操作人
其他参数见 stateflow.signals.process_completed
"""
from shipment.models import SalesItem, UnitChoices
content_object = kwargs.get('content_object')
business_object = kwargs.get('business_object')
last_completed_by = kwargs.get('last_completed_by')
if content_object is None:
logger.warning('process_completed 信号缺少 content_object')
if content_object is None or business_object is None:
logger.warning('process_completed 信号缺少 content_object 或 business_object')
return
printing_job = content_object
# 单位映射PrintingJob.unit字符串→ SalesItem.unitIntegerChoices
unit_mapping = {
'': UnitChoices.METER,
'm': UnitChoices.METER,
'M': UnitChoices.METER,
'': UnitChoices.PIECE,
'': UnitChoices.YARD,
'yd': UnitChoices.YARD,
'YD': UnitChoices.YARD,
'': UnitChoices.UNIT,
'pcs': UnitChoices.UNIT,
'PCS': UnitChoices.UNIT,
}
# 从 settings 获取配置
source_state_id = getattr(settings, 'PRINTING_SALES_ITEM_SOURCE_STATE_ID', None)
quantity_key = getattr(settings, 'PRINTING_SALES_ITEM_QUANTITY_KEY', '米数')
# 默认单位为"件"
sales_unit = unit_mapping.get(printing_job.unit, UnitChoices.PIECE)
if source_state_id is None:
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 '未知产品'
@@ -63,7 +105,7 @@ def on_printing_job_process_completed(sender, **kwargs):
sales_item = SalesItem.objects.create(
shipment=None, # 暂不关联出货单,待后续分配
name=product_name,
quantity=printing_job.quantity,
quantity=quantity,
unit=sales_unit,
created_by=last_completed_by,
printing_job_id=printing_job.id,
@@ -73,7 +115,7 @@ def on_printing_job_process_completed(sender, **kwargs):
logger.info(
f'PrintingJob #{printing_job.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: