fix(contract-payment): fix comments of contract payment plan
This commit is contained in:
1 parent
60d4f06018
commit
1c0654c6f8
25 files changed
+956
-76
No files matched your search
@@ -0,0 +1,117 @@
|
||||
"""
|
||||
backend/tests/test_contract_payment_ledger_focus.py
|
||||
|
||||
用途:验收合同台账汇总卡按合同计数,以及点卡片过滤列表。
|
||||
依赖:Flask test client、ContractPaymentContract、ContractPaymentNode、ledger_focus。
|
||||
逻辑:固定业务日 2026-09-10,分别构造即将到期、逾期、待补充、本月应付合同后核对计数与 focus 筛选。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
from app import db
|
||||
from app.models import ContractPaymentContract, ContractPaymentNode, User
|
||||
from app.services.contract_payment.ledger_focus import ledger_focus_counts
|
||||
|
||||
|
||||
def _create_user(*, username: str) -> User:
|
||||
"""创建具备合同付款预警访问权的落库用户。"""
|
||||
user = User(
|
||||
username=username,
|
||||
email=f'{username}@example.com',
|
||||
role='user',
|
||||
status='active',
|
||||
real_name=f'{username}-实名',
|
||||
employee_id=f'{username.upper()}-EMP',
|
||||
department='采购部',
|
||||
)
|
||||
user.set_feature_permissions({'contract_payment.access': True})
|
||||
user.set_password('password123')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
db.session.refresh(user)
|
||||
db.session.expunge(user)
|
||||
return user
|
||||
|
||||
|
||||
def _auth_headers(user: User) -> dict[str, str]:
|
||||
"""生成测试鉴权头。"""
|
||||
return {
|
||||
'X-User-Id': str(user.id),
|
||||
'X-Username': user.username,
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
|
||||
def _add_contract(*, contract_no: str, status: str = 'active') -> ContractPaymentContract:
|
||||
"""写入一份已进入台账的合同。"""
|
||||
contract = ContractPaymentContract(
|
||||
contract_no=contract_no,
|
||||
parse_status='parsed',
|
||||
status=status,
|
||||
)
|
||||
db.session.add(contract)
|
||||
db.session.flush()
|
||||
return contract
|
||||
|
||||
|
||||
def _add_node(contract: ContractPaymentContract, *, due: date | None, node_status: str = 'active') -> None:
|
||||
"""写入一个付款节点,供汇总卡按到期日分类。"""
|
||||
db.session.add(ContractPaymentNode(
|
||||
contract_id=contract.id,
|
||||
node_code=f'prepayment-{contract.id}',
|
||||
node_type='prepayment',
|
||||
status=node_status,
|
||||
due_date=due,
|
||||
offset_days=0,
|
||||
reminder_days=14,
|
||||
))
|
||||
|
||||
|
||||
def _seed_focus_contracts() -> None:
|
||||
"""构造四张汇总卡各自至少命中一份的台账合同。"""
|
||||
today = date(2026, 9, 10)
|
||||
due = _add_contract(contract_no='HT-FOCUS-DUE')
|
||||
_add_node(due, due=today.replace(day=14))
|
||||
overdue = _add_contract(contract_no='HT-FOCUS-OVERDUE')
|
||||
_add_node(overdue, due=date(2026, 8, 20))
|
||||
_add_contract(contract_no='HT-FOCUS-NEEDS')
|
||||
month = _add_contract(contract_no='HT-FOCUS-MONTH')
|
||||
_add_node(month, due=date(2026, 9, 28))
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def test_ledger_focus_counts_by_contract(app):
|
||||
"""四张汇总卡均按合同计数;本月应付只含本自然月未付合同。"""
|
||||
with app.app_context():
|
||||
_seed_focus_contracts()
|
||||
counts = ledger_focus_counts(session=db.session, today=date(2026, 9, 10))
|
||||
# 关键断言:即将到期 1、逾期 1、待补充 1、本月应付为 9/14 与 9/28 两份。
|
||||
assert counts['due'] == 1
|
||||
assert counts['overdue'] == 1
|
||||
assert counts['needs_completion'] == 1
|
||||
assert counts['due_this_month'] == 2
|
||||
|
||||
|
||||
def test_list_contracts_focus_filters_ledger(client, app, monkeypatch):
|
||||
"""点汇总卡后列表只返回对应合同;汇总接口不跟 keyword。"""
|
||||
monkeypatch.setattr(
|
||||
'app.services.contract_payment.ledger_focus.business_today',
|
||||
lambda today=None: today or date(2026, 9, 10),
|
||||
)
|
||||
with app.app_context():
|
||||
user = _create_user(username='cp_focus_list')
|
||||
_seed_focus_contracts()
|
||||
|
||||
headers = _auth_headers(user)
|
||||
overdue = client.get('/api/contract-payment/contracts?focus=overdue', headers=headers)
|
||||
summary = client.get('/api/contract-payment/contracts/summary', headers=headers)
|
||||
empty = client.get('/api/contract-payment/contracts?focus=overdue&keyword=NO-SUCH', headers=headers)
|
||||
# 关键断言:逾期卡只筛出逾期合同;汇总仍看全量台账。
|
||||
assert overdue.status_code == 200
|
||||
data = overdue.get_json()['data']
|
||||
assert data['total'] == 1
|
||||
assert data['items'][0]['contract_no'] == 'HT-FOCUS-OVERDUE'
|
||||
assert summary.get_json()['data']['overdue'] == 1
|
||||
assert empty.get_json()['data']['total'] == 0
|
||||
@@ -0,0 +1,98 @@
|
||||
"""
|
||||
backend/tests/test_contract_payment_list.py
|
||||
|
||||
用途:验收合同付款台账列表的 keyword 筛选,尤其是按供应商名称模糊匹配。
|
||||
依赖:Flask test client、ContractPaymentContract、用户功能权限。
|
||||
逻辑:构造两份已解析合同,确认供应商片段只命中对应行,且合同号/采购单号检索仍可用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app import db
|
||||
from app.models import ContractPaymentContract, User
|
||||
|
||||
|
||||
def _create_user(*, username: str) -> User:
|
||||
"""创建具备合同付款预警访问权的落库用户,供列表接口走 legacy header 认证。"""
|
||||
user = User(
|
||||
username=username,
|
||||
email=f'{username}@example.com',
|
||||
role='user',
|
||||
status='active',
|
||||
real_name=f'{username}-实名',
|
||||
employee_id=f'{username.upper()}-EMP',
|
||||
department='采购部',
|
||||
)
|
||||
user.set_feature_permissions({'contract_payment.access': True})
|
||||
user.set_password('password123')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
db.session.refresh(user)
|
||||
db.session.expunge(user)
|
||||
return user
|
||||
|
||||
|
||||
def _auth_headers(user: User) -> dict[str, str]:
|
||||
"""生成测试鉴权头。"""
|
||||
return {
|
||||
'X-User-Id': str(user.id),
|
||||
'X-Username': user.username,
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
|
||||
def _seed_parsed_contracts() -> None:
|
||||
"""写入两份已进入台账的合同,供应商名称可区分。"""
|
||||
db.session.add_all(
|
||||
[
|
||||
ContractPaymentContract(
|
||||
contract_no='HT-20260903-30622-0',
|
||||
workflow_no='5203-2609020001',
|
||||
purchase_order_no='5203-2609020001',
|
||||
supplier_name='诺亘(上海)工业自动化有限公司',
|
||||
parse_status='parsed',
|
||||
status='active',
|
||||
),
|
||||
ContractPaymentContract(
|
||||
contract_no='HT-20260903-30612-0',
|
||||
workflow_no='5203-2608270005',
|
||||
purchase_order_no='5203-2608270005',
|
||||
supplier_name='无锡市质谨机电科技有限公司',
|
||||
parse_status='parsed',
|
||||
status='active',
|
||||
),
|
||||
]
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def test_list_contracts_filters_by_supplier_name(client, app):
|
||||
"""关键词命中供应商名称时只返回对应合同,不误伤其他供应商。"""
|
||||
with app.app_context():
|
||||
user = _create_user(username='cp_list_supplier')
|
||||
_seed_parsed_contracts()
|
||||
|
||||
headers = _auth_headers(user)
|
||||
res = client.get('/api/contract-payment/contracts?keyword=诺亘', headers=headers)
|
||||
assert res.status_code == 200
|
||||
data = res.get_json()['data']
|
||||
# 关键断言:供应商模糊匹配生效,且仍走 parsed 台账口径。
|
||||
assert data['total'] == 1
|
||||
assert data['items'][0]['supplier_name'] == '诺亘(上海)工业自动化有限公司'
|
||||
assert data['items'][0]['contract_no'] == 'HT-20260903-30622-0'
|
||||
|
||||
|
||||
def test_list_contracts_keyword_still_matches_contract_and_po(client, app):
|
||||
"""合同号、采购单号原有检索不能被供应商筛选挤掉。"""
|
||||
with app.app_context():
|
||||
user = _create_user(username='cp_list_keyword')
|
||||
_seed_parsed_contracts()
|
||||
|
||||
headers = _auth_headers(user)
|
||||
by_po = client.get('/api/contract-payment/contracts?keyword=5203-2608270005', headers=headers)
|
||||
by_contract = client.get('/api/contract-payment/contracts?keyword=HT-20260903-30622-0', headers=headers)
|
||||
# 关键断言:原合同号/采购单号路径仍精确命中。
|
||||
assert by_po.get_json()['data']['total'] == 1
|
||||
assert by_po.get_json()['data']['items'][0]['purchase_order_no'] == '5203-2608270005'
|
||||
assert by_contract.get_json()['data']['total'] == 1
|
||||
assert by_contract.get_json()['data']['items'][0]['contract_no'] == 'HT-20260903-30622-0'
|
||||
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
backend/tests/test_contract_payment_parse.py
|
||||
|
||||
用途:验证合同付款预警解析器、三轮预警升级、ERP 金额匹配及周汇总摘要。
|
||||
依赖:contract_payment.parse_service、alert_service、erp_payment_service、supervisor_oa_service(三轮测试 mock)。
|
||||
逻辑:覆盖确定性解析边界、pending 计数与下一次周五 18:00;三轮升级 mock OA 矩阵,不调用外部 OA、文档库或企业微信。
|
||||
用途:验证合同付款预警解析器、三轮预警升级、ERP 金额匹配、周汇总摘要及企微 Excel 收件人列。
|
||||
依赖:contract_payment.parse_service、alert_service、erp_payment_service、supervisor_oa_service(三轮测试 mock)、openpyxl。
|
||||
逻辑:覆盖确定性解析边界、pending 计数与下一次周五 18:00;三轮升级 mock OA 矩阵,不调用外部 OA、文档库或企业微信;Excel 用例只断言「收件人」列为真实姓名。
|
||||
"""
|
||||
|
||||
from datetime import date, datetime
|
||||
@@ -17,10 +17,17 @@ from app.models import (
|
||||
ContractPaymentNode,
|
||||
User,
|
||||
)
|
||||
from io import BytesIO
|
||||
|
||||
from openpyxl import load_workbook
|
||||
|
||||
from app.services.contract_payment.alert_service import (
|
||||
bind_contract_owner_from_applicant,
|
||||
build_weekly_alert_summary,
|
||||
generate_alerts,
|
||||
_alert_status_label,
|
||||
_build_excel,
|
||||
_recipient_display_name,
|
||||
)
|
||||
from app.services.contract_payment import parse_service
|
||||
from app.services.contract_payment.parse_service import parse_contract_bytes, parse_contract_text
|
||||
@@ -549,6 +556,81 @@ def test_weekly_alert_summary_counts_pending_and_next_friday(app):
|
||||
assert at_send_time["next_send_at"] == "2026-09-11T10:00:00Z"
|
||||
|
||||
|
||||
def test_weekly_excel_writes_recipient_real_name_not_role(app):
|
||||
"""关键断言:企微 Excel 列名为「收件人」,单元格写系统用户真实姓名,不再写岗位名称。"""
|
||||
with app.app_context():
|
||||
owner = User(
|
||||
username="excel-owner",
|
||||
email="excel-owner@example.com",
|
||||
status="active",
|
||||
real_name="张三",
|
||||
)
|
||||
db.session.add(owner)
|
||||
db.session.flush()
|
||||
contract = ContractPaymentContract(
|
||||
contract_no="HT-EXCEL-1",
|
||||
purchase_order_no="PO-1",
|
||||
supplier_name="苏州供应商",
|
||||
owner_user_id=owner.id,
|
||||
status="active",
|
||||
)
|
||||
db.session.add(contract)
|
||||
db.session.flush()
|
||||
node = ContractPaymentNode(
|
||||
contract_id=contract.id,
|
||||
node_code="shipment",
|
||||
node_type="shipment",
|
||||
status="active",
|
||||
due_date=date(2026, 9, 1),
|
||||
)
|
||||
db.session.add(node)
|
||||
db.session.flush()
|
||||
alert = ContractPaymentAlert(
|
||||
contract_id=contract.id,
|
||||
node_id=node.id,
|
||||
trigger_key="round:1:date:2026-09-01",
|
||||
recipient_user_id=owner.id,
|
||||
alert_round=1,
|
||||
status="pending",
|
||||
)
|
||||
db.session.add(alert)
|
||||
db.session.commit()
|
||||
|
||||
data, summary = _build_excel([(alert, contract, node, owner)])
|
||||
wb = load_workbook(BytesIO(data))
|
||||
headers = [cell.value for cell in wb.active[1]]
|
||||
row = [cell.value for cell in wb.active[2]]
|
||||
|
||||
assert "收件岗位" not in headers
|
||||
assert headers[10] == "收件人"
|
||||
assert row[10] == "张三"
|
||||
assert row[11] == "待发送"
|
||||
assert "pending" not in row
|
||||
assert "采购负责人" not in row
|
||||
assert summary["total"] == 1
|
||||
|
||||
|
||||
def test_recipient_display_name_falls_back_to_username(app):
|
||||
"""关键断言:真实姓名为空时 Excel 收件人回退用户名,而不是岗位标签。"""
|
||||
with app.app_context():
|
||||
named = User(username="buyer", email="named@example.com", status="active", real_name="李采购")
|
||||
unnamed = User(username="buyer02", email="unnamed@example.com", status="active", real_name=" ")
|
||||
|
||||
assert _recipient_display_name(named) == "李采购"
|
||||
assert _recipient_display_name(unnamed) == "buyer02"
|
||||
assert _recipient_display_name(None) == "—"
|
||||
|
||||
|
||||
def test_alert_status_label_uses_chinese():
|
||||
"""关键断言:Excel 状态列把 pending/sent/failed 写成中文,未知码保留原文。"""
|
||||
assert _alert_status_label("pending") == "待发送"
|
||||
assert _alert_status_label("sent") == "已发送"
|
||||
assert _alert_status_label("failed") == "发送失败"
|
||||
assert _alert_status_label("resolved") == "已关闭"
|
||||
assert _alert_status_label("unknown-code") == "unknown-code"
|
||||
assert _alert_status_label(None) == "—"
|
||||
|
||||
|
||||
def test_erp_payment_amount_matches_the_corresponding_stage():
|
||||
"""关键断言:不同金额的 ERP 付款必须分别匹配同金额付款阶段。"""
|
||||
paid_node_ids = resolve_paid_node_ids(
|
||||
|
||||
@@ -3,7 +3,7 @@ backend/tests/test_contract_payment_permissions.py
|
||||
|
||||
用途:验收合同付款预警专项权限 `contract_payment.access` 的角色默认值、显式覆盖与 API 拦截。
|
||||
依赖:User 功能权限、Flask test client、contract_payment 蓝图。
|
||||
逻辑:admin 默认可访问;普通用户默认 403;显式授予后可查询台账;关闭 admin 覆盖后同样 403。
|
||||
逻辑:admin 默认可访问;普通用户默认 403;显式授予后可查询台账,但不能触发同步/扫描/发送;关闭 admin 覆盖后同样 403。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -128,3 +128,23 @@ def test_contract_payment_api_allows_granted_user(client, app):
|
||||
body = list_res.get_json()
|
||||
assert body.get('success') is True
|
||||
assert 'items' in (body.get('data') or {})
|
||||
|
||||
|
||||
def test_contract_payment_jobs_require_admin(client, app):
|
||||
"""已授权的普通用户可查台账,但不能触发同步、扫描或周汇总发送。"""
|
||||
with app.app_context():
|
||||
granted = _create_user(
|
||||
username='cp_api_jobs_denied',
|
||||
feature_permissions={'contract_payment.access': True},
|
||||
)
|
||||
|
||||
headers = _auth_headers(granted)
|
||||
# 关键断言:访问权不等于操作权,手动任务仅管理员。
|
||||
for path in (
|
||||
'/api/contract-payment/sync',
|
||||
'/api/contract-payment/alerts/run',
|
||||
'/api/contract-payment/alerts/weekly-run',
|
||||
):
|
||||
res = client.post(path, headers=headers, json={})
|
||||
assert res.status_code == 403, path
|
||||
assert _json_error(res)['code'] == 'CONTRACT_PAYMENT.ADMIN_REQUIRED'
|
||||
Reference in new issue
Block a user