"""月度结算集成测试(M3 ④:生成快照 → 审批 → 锁定)。 守的还有: - 确认回款口径(#8 策略点):仅 is_active=1 且未入结的台账行入结; 入结后重生成同月不再重复计入 - 通过即锁定(status=3 终态);锁定后回款写路径 400(走调整单) - 驳回回草稿可重提;开关两路(关=自动通过锁定;开=审批流转) - #12 暂口径:经理个人户业绩不入聚合基数(金标准仅业务员个人户, 聚合用例以「无经理台账」形态验证 items 按人汇总不聚合) """ import uuid import pytest from fastapi.testclient import TestClient from sqlalchemy import select from app.core.db import get_session_factory from app.models import MonthlySettlement from tests.integration.factories import business_payload, customer pytestmark = pytest.mark.integration _SETTLEMENTS = "/api/v1/settlements" _CALC = "/api/v1/calc" _BUSINESSES = "/api/v1/business/businesses" _SHIPMENTS = "/api/v1/business/shipments" _RECEIPTS = "/api/v1/business/receipts" _PERSONNEL = "/api/v1/base/personnel" _GOLD_ITEMS = [ { "product_name": "t_市政_月结", "product_category": "municipal_water", "quantity": "100", "sale_price": "100.00", "cost_amount": "3000.00", } ] def _role_id(role_code: str) -> int: from sqlalchemy import select from app.core.db import get_session_factory from app.models import BusinessRole factory = get_session_factory() with factory() as session: return int( session.execute( select(BusinessRole.id).where( BusinessRole.role_code == role_code, BusinessRole.status == 1, ) ).scalar_one() ) def _person( client: TestClient, headers: dict, *, role_code: str = "salesperson", manager_id: int | None = None, ) -> dict: resp = client.post( _PERSONNEL, json={ "personnel_name": f"t_月结_{uuid.uuid4().hex[:8]}", "business_role_id": _role_id(role_code), "salary_standard": "3000.00", **({"manager_id": manager_id} if manager_id is not None else {}), }, headers=headers, ) assert resp.status_code == 200, resp.text return resp.json()["data"] def _set_switch(client: TestClient, headers: dict, code: str, value: str) -> None: """显式设置审批开关(V8 起审批开关默认开启,用例不得依赖种子默认值)。""" resp = client.post( "/api/v1/system/params/changes", json={ "change_type": 1, "remark": f"用例设置 {code}={value}", "items": [{"param_code": code, "dimension_value": "", "value": value}], }, headers=headers, ) assert resp.status_code == 200, resp.text def _calc_one(client: TestClient, headers: dict) -> dict: """完整造一笔已核算回款(回款日 2026-10-20 → 归月 2026-10)。""" cust = customer(client, headers) person = _person(client, headers) resp = client.post( _BUSINESSES, json=business_payload( cust["id"], owner_personnel_id=person["id"], items=[dict(_GOLD_ITEMS[0], product_name=f"t_月结产品_{uuid.uuid4().hex[:6]}")], ), headers=headers, ) assert resp.status_code == 200, resp.text biz = resp.json()["data"] resp = client.post( _SHIPMENTS, json={ "business_id": biz["id"], "ship_date": "2026-10-01", "items": [{"product_name": "t_发", "quantity": "1", "sale_price": "1.00"}], }, headers=headers, ) assert resp.status_code == 200, resp.text resp = client.post( _RECEIPTS, json={ "business_id": biz["id"], "receipt_date": "2026-10-20", "amount": "20000.00", "shipment_id": resp.json()["data"]["id"], }, headers=headers, ) assert resp.status_code == 200, resp.text receipt = resp.json()["data"] resp = client.post(f"{_CALC}/receipts/{receipt['id']}/calc", headers=headers) assert resp.status_code == 200, resp.text return biz @pytest.fixture def finance(make_user, auth_headers) -> dict: user = make_user(["finance"]) user["headers"] = auth_headers(user) return user class TestGenerate: """生成快照:确认回款口径与按人汇总。""" def test_生成_按人汇总并回填台账(self, client: TestClient, admin: dict) -> None: _calc_one(client, admin["headers"]) resp = client.post( f"{_SETTLEMENTS}/generate", json={"month": "2026-10"}, headers=admin["headers"] ) assert resp.status_code == 200, resp.text out = resp.json()["data"] assert out["settlement_month"] == "2026-10" assert out["status"] == 1 # 草稿 assert len(out["items"]) >= 1 item = out["items"][0] assert item["is_probation"] in (0, 1) # 台账行已回填 settlement_id(入结标记,防重复计入) factory = get_session_factory() with factory() as session: settled = session.execute( select(MonthlySettlement).where(MonthlySettlement.settlement_month == "2026-10") ).scalar_one() assert settled.person_count >= 1 def test_无核算记录生成400(self, client: TestClient, admin: dict) -> None: resp = client.post( f"{_SETTLEMENTS}/generate", json={"month": "2099-01"}, headers=admin["headers"] ) assert resp.status_code == 400 assert "无可结算" in resp.json()["message"] def test_已审定月份重生成400(self, client: TestClient, admin: dict) -> None: _calc_one(client, admin["headers"]) assert ( client.post( f"{_SETTLEMENTS}/generate", json={"month": "2026-10"}, headers=admin["headers"] ).status_code == 200 ) # 显式关开关 → 提交即自动通过锁定(用例不依赖种子默认值) _set_switch(client, admin["headers"], "approval.monthly_settlement.enabled", "0") sid = client.get(f"{_SETTLEMENTS}", headers=admin["headers"]).json()["data"]["list"][0][ "id" ] assert ( client.post(f"{_SETTLEMENTS}/{sid}/submit", headers=admin["headers"]).status_code == 200 ) resp = client.post( f"{_SETTLEMENTS}/generate", json={"month": "2026-10"}, headers=admin["headers"] ) assert resp.status_code == 400 assert "已审定锁定" in resp.json()["message"] class TestSubmitAndLock: """提交审批与锁定(开关两路)。""" def test_开关关闭_提交即自动通过锁定( self, client: TestClient, admin: dict, finance: dict ) -> None: _calc_one(client, admin["headers"]) # 显式关开关(V8 起种子默认开启,用例不得依赖默认值) _set_switch(client, admin["headers"], "approval.monthly_settlement.enabled", "0") resp = client.post( f"{_SETTLEMENTS}/generate", json={"month": "2026-10"}, headers=admin["headers"] ) sid = resp.json()["data"]["id"] resp = client.post(f"{_SETTLEMENTS}/{sid}/submit", headers=admin["headers"]) assert resp.status_code == 200, resp.text out = resp.json()["data"] assert out["status"] == 3 # 自动通过=已审定(锁定) assert out["approval_order_no"] and out["approval_order_no"].startswith("AP") assert out["locked_at"] is not None def test_锁定后回款禁改(self, client: TestClient, admin: dict) -> None: from sqlalchemy import select biz = _calc_one(client, admin["headers"]) client.post(f"{_SETTLEMENTS}/generate", json={"month": "2026-10"}, headers=admin["headers"]) sid = client.get(f"{_SETTLEMENTS}", headers=admin["headers"]).json()["data"]["list"][0][ "id" ] client.post(f"{_SETTLEMENTS}/{sid}/submit", headers=admin["headers"]) # 找该月回款并尝试编辑(400:月已锁定) factory = get_session_factory() with factory() as session: from app.models import Receipt receipt_id = int( session.execute( select(Receipt.id).where(Receipt.business_id == biz["id"]) ).scalar_one() ) resp = client.put( f"{_RECEIPTS}/{receipt_id}", json={ "business_id": biz["id"], "receipt_date": "2026-10-21", "amount": "999.00", }, headers=admin["headers"], ) assert resp.status_code == 400 # 已核算禁改(M2 防线)先于月结锁定拦截生效——两者均 400 拒改 assert ("已核算" in resp.json()["message"]) or ("锁定" in resp.json()["message"]) class TestSwitchOn: """开关开启:审批流转后锁定。""" def test_提交待审_通过后锁定( self, client: TestClient, admin: dict, finance: dict, make_user, auth_headers ) -> None: # 开月结审批开关(立即生效) resp = client.post( "/api/v1/system/params/changes", json={ "change_type": 1, "remark": "开月结审批", "items": [ { "param_code": "approval.monthly_settlement.enabled", "dimension_value": "", "value": "1", } ], }, headers=admin["headers"], ) assert resp.status_code == 200, resp.text leader = make_user(["leader"]) leader["headers"] = auth_headers(leader) # 造 11 月的台账(避开 10 月已入结月份) cust = customer(client, admin["headers"]) person = _person(client, admin["headers"]) resp = client.post( _BUSINESSES, json=business_payload( cust["id"], owner_personnel_id=person["id"], items=[dict(_GOLD_ITEMS[0], product_name=f"t_11月_{uuid.uuid4().hex[:6]}")], ), headers=admin["headers"], ) assert resp.status_code == 200, resp.text biz = resp.json()["data"] resp = client.post( _SHIPMENTS, json={ "business_id": biz["id"], "ship_date": "2026-11-01", "items": [{"product_name": "t_发", "quantity": "1", "sale_price": "1.00"}], }, headers=admin["headers"], ) assert resp.status_code == 200, resp.text shipment = resp.json()["data"] resp = client.post( _RECEIPTS, json={ "business_id": biz["id"], "receipt_date": "2026-11-20", "amount": "20000.00", "shipment_id": shipment["id"], }, headers=admin["headers"], ) assert resp.status_code == 200, resp.text receipt = resp.json()["data"] assert ( client.post( f"{_CALC}/receipts/{receipt['id']}/calc", headers=admin["headers"] ).status_code == 200 ) resp = client.post( f"{_SETTLEMENTS}/generate", json={"month": "2026-11"}, headers=admin["headers"] ) assert resp.status_code == 200, resp.text sid = resp.json()["data"]["id"] # 提交 → 待财务复核 resp = client.post(f"{_SETTLEMENTS}/{sid}/submit", headers=admin["headers"]) assert resp.status_code == 200, resp.text assert resp.json()["data"]["status"] == 2 # 财务通过 → 待领导(approval_order_no 关联的审批单流转) order_no = resp.json()["data"]["approval_order_no"] order = client.get( "/api/v1/system/approvals", params={"keyword": order_no}, headers=admin["headers"] ).json()["data"]["list"][0] resp = client.post( f"/api/v1/system/approvals/{order['id']}/action", json={"action": 1}, headers=finance["headers"], ) assert resp.status_code == 200, resp.text # 财务节点通过只是流转到待领导——不得提前锁定月结(否则领导驳回后 # 锁定字段残留、两级审批形同虚设) mid = client.get(f"{_SETTLEMENTS}/{sid}", headers=admin["headers"]).json()["data"] assert mid["status"] == 2, "财务通过后应仍为待审批,不得提前锁定" assert mid["locked_at"] is None assert mid["locked_by"] is None # 领导通过 → 锁定 resp = client.post( f"/api/v1/system/approvals/{order['id']}/action", json={"action": 1}, headers=leader["headers"], ) assert resp.status_code == 200, resp.text final = client.get(f"{_SETTLEMENTS}/{sid}", headers=admin["headers"]).json()["data"] assert final["status"] == 3 assert final["locked_at"] is not None assert final["locked_by"] == leader["username"] # 审批通过人=领导账号 class TestManagerAggregate: """经理团队聚合:比例按参数实际值计算(守「百分数再乘 100」放大缺陷)。""" def test_聚合金额按实际比例计入(self, client: TestClient, admin: dict) -> None: manager = _person(client, admin["headers"], role_code="sales_manager") sales = _person(client, admin["headers"], manager_id=manager["id"]) cust = customer(client, admin["headers"]) resp = client.post( _BUSINESSES, json=business_payload( cust["id"], owner_personnel_id=sales["id"], items=[dict(_GOLD_ITEMS[0], product_name=f"t_聚合_{uuid.uuid4().hex[:6]}")], ), headers=admin["headers"], ) assert resp.status_code == 200, resp.text biz = resp.json()["data"] resp = client.post( _SHIPMENTS, json={ "business_id": biz["id"], "ship_date": "2026-10-01", "items": [{"product_name": "t_发", "quantity": "1", "sale_price": "1.00"}], }, headers=admin["headers"], ) assert resp.status_code == 200, resp.text resp = client.post( _RECEIPTS, json={ "business_id": biz["id"], "receipt_date": "2026-10-20", "amount": "20000.00", "shipment_id": resp.json()["data"]["id"], }, headers=admin["headers"], ) assert resp.status_code == 200, resp.text receipt = resp.json()["data"] assert ( client.post( f"{_CALC}/receipts/{receipt['id']}/calc", headers=admin["headers"] ).status_code == 200 ) resp = client.post( f"{_SETTLEMENTS}/generate", json={"month": "2026-10"}, headers=admin["headers"] ) assert resp.status_code == 200, resp.text items = resp.json()["data"]["items"] manager_items = [i for i in items if i["calc_type"] == 2] assert len(manager_items) == 1 # 业务员绩效 39.11;经理 = 39.11×12.5% + 39.11×5% = 6.84425 → 6.84 # (若调用方把百分数再 ×100,此处会是 684.43) assert manager_items[0]["personnel_id"] == manager["id"] assert manager_items[0]["performance_sum"] == "6.84" assert manager_items[0]["payable_sum"] == "6.84" class TestAdjustment: """调整单:已锁定月份补发/扣减的唯一通道。""" def test_新建调整单_开关关闭即生效(self, client: TestClient, admin: dict) -> None: # 显式关开关(用例不依赖种子默认值) _set_switch(client, admin["headers"], "approval.adjustment.enabled", "0") person = _person(client, admin["headers"]) resp = client.post( f"{_SETTLEMENTS}/adjustments", json={ "settlement_month": "2026-10", "personnel_id": person["id"], "direction": 1, "amount": "500.00", "reason": "补发差额", }, headers=admin["headers"], ) # 守 ENABLE_CODE_BY_TYPE 漏登记 type=3:修复前此处恒 400「未知的审批对象类型」 assert resp.status_code == 200, resp.text out = resp.json()["data"] assert out["status"] == 2 # 已生效