feat(migration): V7 核算台账/月结/调整单四表与核算纯函数

- V7 四表:performance_ledgers(append-only 版本行,uk(receipt_id,calc_version)
  库级防重兜底)/monthly_settlements(月份 uk,通过即锁定终态)/
  settlement_items(按人固化可负工资,uk 三列)/adjustment_orders(TZ 单号)
- registry +1:approval.adjustment.enabled(19 项,BOOL 首组扩至三开关)
- formula 补核算纯函数:clamp_weight_r(从预览内联提取共用)/monthly_salary
  三口径/probation_annual_note/manager_team_share(#12 暂口径:基数不含
  经理个人户业绩,聚合集中一处答复后只改)
- 迁移 downgrade 只保留 drop_table+alter 回滚+种子行反向 DELETE;
  performance_ledgers 表名复数(test_models 红线断言)
- 断言同步:EXPECTED_TABLES 四新表/registry 19/BOOL 三开关/conftest
  清理四新表与 ParamValue 边界 22
- schema_sync+全量单测 460 passed

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
liuwenxinandClaude Code committed 2026-10-16 16:32:32 +08:00
1 parent 30a179fc4c
commit a5c86640e2
17 files changed
+1464 -51

No files matched your search

+15 -4
View File
@@ -30,6 +30,7 @@ from app.core.constants import ROLE_ADMIN, ROLE_USER
from app.core.db import get_session_factory
from app.core.security import create_access_token, hash_password
from app.models import (
AdjustmentOrder,
ApprovalOrder,
Business,
BusinessItem,
@@ -37,16 +38,19 @@ from app.models import (
BusinessStatusLog,
Customer,
CustomerRatingGrade,
MonthlySettlement,
OperationLog,
OwnershipChangeLog,
ParamChangeBatch,
ParamValue,
PerformanceLedger,
Personnel,
PersonnelPlan,
PersonnelRegion,
Product,
Receipt,
Role,
SettlementItem,
Shipment,
ShipmentItem,
SystemSetting,
@@ -178,12 +182,19 @@ def _purge_test_data() -> None:
# 审批单无 t_ 前缀可用,按 AP 编号前缀全量清(steps 外键 CASCADE 随删;
# ref_id 固化无外键,业务已在上一步删除,无悬挂影响)
session.execute(delete(ApprovalOrder).where(ApprovalOrder.order_no.like("AP%")))
# 开关参数恢复种子基线:测试提交的变更批次条目 id 均大于种子终值 21
# (V3 十九条 + V6 两条),删新条目与新批次后解析回退到种子值(开关=0)
stray_values = list(session.execute(select(ParamValue).where(ParamValue.id > 21)).scalars())
# ---- M3-core:核算台账 / 月结 / 调整单(仅测试写入,全量清;
# 删除顺序:台账→明细→月结头→调整单,TZ 前缀全量清)----
session.execute(delete(PerformanceLedger))
session.execute(delete(SettlementItem))
session.execute(delete(MonthlySettlement))
session.execute(delete(AdjustmentOrder).where(AdjustmentOrder.order_no.like("TZ%")))
# 开关参数恢复种子基线:测试提交的变更批次条目 id 均大于种子终值 22
# (V3 十九条 + V6 两条 + V7 调整单开关一条),删新条目与新批次后解析
# 回退到种子值(开关=0)
stray_values = list(session.execute(select(ParamValue).where(ParamValue.id > 22)).scalars())
if stray_values:
stray_batches = {v.batch_id for v in stray_values}
session.execute(delete(ParamValue).where(ParamValue.id > 21))
session.execute(delete(ParamValue).where(ParamValue.id > 22))
session.execute(delete(ParamChangeBatch).where(ParamChangeBatch.id.in_(stray_batches)))
# 系统设置恢复种子单行(test_settings 会改公司名/传 logo——残留会让
# 后续 e2e 的品牌断言在污染状态下运行);uploads 下的测试 logo 一并删
+69
View File
@@ -11,10 +11,14 @@ from fractions import Fraction
from app.services.formula import (
apply_barter,
clamp_weight_r,
converted_profit,
manager_team_share,
monthly_salary,
payment_days,
payment_weight,
performance_amount,
probation_annual_note,
profit_rate,
salesperson_rate,
start_payment_ok,
@@ -137,3 +141,68 @@ class TestModifiers:
def test_起付判定_未达到60点(self) -> None:
assert start_payment_ok(Decimal("59999"), Decimal("100000"), Fraction(60)) is False
class TestClampWeightR:
"""R 钳制:向下取整 + [0,100] 封顶封底(从预览内联提取的共用实现)。"""
def test_向下取整(self) -> None:
assert clamp_weight_r(Decimal("5999.99"), Decimal("10000")) == 59
def test_封顶100_封底0(self) -> None:
assert clamp_weight_r(Decimal("20000"), Decimal("10000")) == 100
assert clamp_weight_r(Decimal("0"), Decimal("10000")) == 0
def test_销售额0_防御0(self) -> None:
assert clamp_weight_r(Decimal("100"), Decimal("0")) == 0
class TestMonthlySalary:
"""月工资分岗口径三档(对照表 #8)。"""
def test_INCLUDE_绩效加基本(self) -> None:
assert monthly_salary(Decimal("1000"), Decimal("3000"), "INCLUDE") == Decimal("4000")
def test_DEDUCT_绩效减基本_可为负(self) -> None:
assert monthly_salary(Decimal("1000"), Decimal("3000"), "DEDUCT") == Decimal("-2000")
def test_EXCLUDE_仅绩效(self) -> None:
assert monthly_salary(Decimal("1000"), Decimal("3000"), "EXCLUDE") == Decimal("1000")
def test_基本缺失按0(self) -> None:
assert monthly_salary(Decimal("1000"), None, "INCLUDE") == Decimal("1000")
def test_未知口径报错(self) -> None:
import pytest
with pytest.raises(ValueError):
monthly_salary(Decimal("1"), Decimal("1"), "UNKNOWN")
class TestProbationAnnualNote:
"""试用期年累计说明:超阈值才有 note,金额精确到分。"""
def test_未超阈值_无说明(self) -> None:
assert probation_annual_note(Decimal("18000"), Decimal("18000")) is None
assert probation_annual_note(Decimal("100"), Decimal("18000")) is None
def test_超额_给出精确差额(self) -> None:
note = probation_annual_note(Decimal("20000.5"), Decimal("18000"))
assert note is not None and "2000.50" in note
class TestManagerTeamShare:
"""经理 12.5%+5% 聚合(#12 暂口径:基数不含经理个人户业绩——由调用方
传入的基数集合保证,本函数只做乘法与求和)。"""
def test_两段乘加_精确分数(self) -> None:
# 入参为百分数(与种子一致:12.5 表示 12.5%):10000×12.5%=1250、50000×5%=2500
got = manager_team_share(
Decimal("10000"), Decimal("50000"), team_pct=Fraction(25, 2), company_pct=Fraction(5)
)
assert got == Decimal("3750")
def test_零基数_零结果(self) -> None:
assert manager_team_share(
Decimal("0"), Decimal("0"), team_pct=Fraction(0), company_pct=Fraction(0)
) == Decimal("0")
+4
View File
@@ -40,6 +40,10 @@ EXPECTED_TABLES = {
"shipment_items",
"receipts",
"customer_rating_grades",
"performance_ledgers",
"monthly_settlements",
"settlement_items",
"adjustment_orders",
}
# 单复数同形的集合名词:本身即复数语义,不受「以 s 结尾」检查约束
+6 -4
View File
@@ -18,7 +18,7 @@ from app.services.param_registry import (
validate_and_normalize,
)
# 与 db/migration 迁移种子保持一致的 18 项编码(V3 十六项 + V6 审批开关两项)
# 与 db/migration 迁移种子保持一致的 19 项编码(V3 十六项 + V6 审批开关两项 + V7 调整单开关)
EXPECTED_CODES = {
"salesperson.rate.start",
"salesperson.rate.slope",
@@ -38,14 +38,15 @@ EXPECTED_CODES = {
"probation.salary.basic",
"approval.secondary_allocation.enabled",
"approval.monthly_settlement.enabled",
"approval.adjustment.enabled",
}
class TestRegistry:
"""注册表元数据完整性(与迁移种子同源)。"""
def test_注册表共18项(self) -> None:
assert len(PARAM_REGISTRY) == 18
def test_注册表共19项(self) -> None:
assert len(PARAM_REGISTRY) == 19
def test_编码集合与迁移种子一致(self) -> None:
assert set(PARAM_REGISTRY) == EXPECTED_CODES
@@ -70,10 +71,11 @@ class TestRegistry:
assert spec.default_value in spec.options
def test_BOOL开关_仅接受0与1(self) -> None:
"""V6 首例 BOOL 注册的两个审批开关:合法值与非法值边界。"""
"""审批开关 BOOL 组(V6 两个 + V7 调整单):合法值与非法值边界。"""
for code in (
"approval.secondary_allocation.enabled",
"approval.monthly_settlement.enabled",
"approval.adjustment.enabled",
):
spec = PARAM_REGISTRY[code]
assert spec.value_type == "BOOL"