fix(cma): entity isolation batch3 (cost abc write endpoints + allocations/stats reads + plan kr_id scope)
- cost.py: POST /abc/activities 按会话账套写入 entity_id;POST /abc/allocate 校验 activity_id 归属本账套(跨账套 404)并透传 entity_id;GET /abc/allocations 加账套过滤 - cost_engine.py: allocate_cost(entity_id=None) 按账套过滤作业中心 + 显式写入 AbcAllocation.entity_id(不传维持原语义) - action_plans.py: update_plan 的 kr_id 加账套校验(跨账套 404,对齐 create_plan 既有写法);plan_stats join KPIDefinition 按账套过滤(口径与 list_plans 一致) - tests: 新增 tests/test_cross_entity_isolation_batch3_20260918.py(10 例;改前 7 failed/3 passed → 改后 10 passed) - 回归: 1184 passed,1 xfailed → 1194 passed,1 xfailed(无新增失败)
This commit is contained in:
@@ -158,11 +158,21 @@ def calc_driver_rate(activity_id: int) -> dict:
|
||||
|
||||
|
||||
def allocate_cost(activity_id: int, period: str, product_code: str,
|
||||
product_name: str, driver_consumed: float) -> dict:
|
||||
"""按动因分配成本到产品"""
|
||||
product_name: str, driver_consumed: float,
|
||||
entity_id: Optional[int] = None) -> dict:
|
||||
"""按动因分配成本到产品
|
||||
|
||||
账套隔离(2026-09-18,project-20260918-entity-isolation-batch3):
|
||||
- entity_id 非 None 时,作业中心查询按本账套过滤(跨账套 activity_id 查不到 → 返回错误字典,
|
||||
端点侧已先行 404);
|
||||
- 新建 AbcAllocation 显式写入 entity_id(不传则维持原语义:不写归属,落模型默认)。
|
||||
"""
|
||||
db = get_session_local()()
|
||||
try:
|
||||
act = db.query(AbcActivity).filter(AbcActivity.id == activity_id).first()
|
||||
q_act = db.query(AbcActivity).filter(AbcActivity.id == activity_id)
|
||||
if entity_id is not None:
|
||||
q_act = q_act.filter(AbcActivity.entity_id == entity_id)
|
||||
act = q_act.first()
|
||||
if not act or act.driver_rate == 0:
|
||||
# 自动计算分配率
|
||||
if act and act.driver_volume > 0:
|
||||
@@ -171,7 +181,7 @@ def allocate_cost(activity_id: int, period: str, product_code: str,
|
||||
if not act or act.driver_rate == 0:
|
||||
return {"error": "分配率未设置"}
|
||||
allocated = round(driver_consumed * act.driver_rate, 2)
|
||||
alloc = AbcAllocation(
|
||||
alloc_kwargs = dict(
|
||||
period=period,
|
||||
activity_id=activity_id,
|
||||
product_code=product_code,
|
||||
@@ -179,6 +189,9 @@ def allocate_cost(activity_id: int, period: str, product_code: str,
|
||||
driver_consumed=driver_consumed,
|
||||
allocated_cost=allocated,
|
||||
)
|
||||
if entity_id is not None:
|
||||
alloc_kwargs["entity_id"] = entity_id
|
||||
alloc = AbcAllocation(**alloc_kwargs)
|
||||
db.add(alloc)
|
||||
db.commit()
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user