fix(contract-plan): 合同付款预警增加“标记已付”功能
This commit is contained in:
1 parent
5c6ccf9792
commit
ac3fc99883
11 files changed
+231
-19
No files matched your search
@@ -5,7 +5,7 @@ backend/app/api/contract_payment.py
|
||||
依赖:JWT 当前用户、统一响应 envelope、ForbiddenError、Agent 授权、contract_payment 服务和通用 Job 队列。
|
||||
逻辑:页面查询合同、节点、本周待发送摘要、四张汇总卡和周汇总批次;失败批次返回可展示原因。同步和预警操作入队,避免请求线程执行外部系统和企微调用;发送后可按 Job 查询是否真正完成。
|
||||
下载和单合同重解析走 read_contract_attachment_bytes:原文还在则直接读,被临时清理删掉则按附件 ID 从 OA/DMS 重新下载。
|
||||
待确认合同的人工复核走 apply_manual_review:比例合计 100% 后改为已解析并进入台账。
|
||||
待确认合同的人工复核走 apply_manual_review:比例合计 100% 后改为已解析并进入台账。未付的预付款、出货款可手动标为已付。
|
||||
合同序列化时对 parse_issues 去重,避免 MinerU 重复抽取把同一原因展示多遍;同时返回 OA 公司及对应 ERP 账套。
|
||||
鉴权:查询接口要求平台 admin 或持有 `agent_code=contract_payment` 的 Agent 授权;
|
||||
同步/扫描/周汇总发送仅 `role=admin`。
|
||||
@@ -32,7 +32,7 @@ from app.models import (
|
||||
FileAsset,
|
||||
Job,
|
||||
)
|
||||
from app.services.contract_payment.alert_service import build_weekly_alert_summary, describe_batch_failure
|
||||
from app.services.contract_payment.alert_service import build_weekly_alert_summary, describe_batch_failure, mark_payment_node_paid
|
||||
from app.services.contract_payment.ledger_focus import (
|
||||
LEDGER_FOCUS_VALUES,
|
||||
apply_ledger_focus,
|
||||
@@ -202,8 +202,9 @@ def _serialize_contract(row: ContractPaymentContract) -> dict:
|
||||
"anchor_type": node.anchor_type,
|
||||
"anchor_date": node.anchor_date.isoformat() if node.anchor_date else None,
|
||||
"due_date": node.due_date.isoformat() if node.due_date else None,
|
||||
# ERP apa_file 付款日期;已付款时展示,未付为空。
|
||||
# 付款日期。paid_source=manual 表示人工标记,其余来自 ERP。
|
||||
"paid_at": (node.evidence_json or {}).get("paid_at"),
|
||||
"paid_source": (node.evidence_json or {}).get("paid_source"),
|
||||
"status": node.status,
|
||||
} for node in nodes],
|
||||
"alerts": [{
|
||||
@@ -635,3 +636,27 @@ def manual_review_contract(contract_id: int):
|
||||
data = request.get_json() or {}
|
||||
row = apply_manual_review(contract_id, data, operator_id=getattr(user, "id", None))
|
||||
return ok({"contract": _serialize_contract(row)}, request_id=getattr(g, "request_id", None))
|
||||
|
||||
|
||||
@contract_payment_bp.route("/contracts/<int:contract_id>/nodes/<int:node_id>/mark-paid", methods=["POST"])
|
||||
def mark_contract_node_paid(contract_id: int, node_id: int):
|
||||
"""
|
||||
把未付的预付款或出货款手动标为已付。
|
||||
|
||||
核心逻辑:与人工复核一样,持有合同付款 Agent 的用户即可提交。只改这一阶段,并关闭尚未发送的预警。
|
||||
|
||||
Args:
|
||||
contract_id: 合同台账主键。
|
||||
node_id: 付款节点主键。
|
||||
|
||||
Returns:
|
||||
标准成功 envelope,含更新后的合同详情。
|
||||
|
||||
Raises:
|
||||
NotFoundError: 合同或节点不存在。
|
||||
ValidationError: 阶段不能标记,或付款日期无效。
|
||||
"""
|
||||
user = _require_contract_payment_access()
|
||||
data = request.get_json() or {}
|
||||
row = mark_payment_node_paid(contract_id, node_id, data, operator_id=getattr(user, "id", None))
|
||||
return ok({"contract": _serialize_contract(row)}, request_id=getattr(g, "request_id", None))
|
||||
@@ -3,7 +3,7 @@ services/contract_payment/alert_service.py
|
||||
|
||||
用途:按付款阶段生成待发送预警,在每周五按收件人汇总发送企业微信和 Excel 明细,并提供工作台待发送摘要。
|
||||
依赖:合同付款模型、用户目录、openpyxl、企业微信通知服务、OA 上级矩阵、排程时区工具。
|
||||
逻辑:每日任务只写入 pending 审计;周汇总任务锁定本周收件人批次,发送一条汇总卡片和一份逐笔 Excel;发送失败时把原因写入批次审计。摘要接口与周五发送使用同一 pending 口径。
|
||||
逻辑:每日任务只写入 pending 审计;周汇总任务锁定本周收件人批次,发送一条汇总卡片和一份逐笔 Excel;发送失败时把原因写入批次审计。摘要接口与周五发送使用同一 pending 口径。预付款、出货款可手动标为已付,并关闭该节点尚未发送的预警。
|
||||
第一轮按台账申请人 `real_name` 唯一匹配系统用户;第二、三轮查 OA HrmResource + Matrixtable_3,用 zz/jfl 作为企微 userid,不向第一轮兜底。
|
||||
"""
|
||||
|
||||
@@ -149,15 +149,70 @@ def generate_alerts(*, today: date | None = None, send: bool = False) -> dict[st
|
||||
return {"created": created, "pending": created, "sent": 0, "skipped": skipped, "resolved": resolved}
|
||||
|
||||
|
||||
def _resolve_pending_alerts(*, node_id: int) -> int:
|
||||
"""将某节点未发送的提醒标记为已按 ERP 付款关闭,保留历史发送记录。"""
|
||||
def _resolve_pending_alerts(*, node_id: int, reason: str = "erp_paid") -> int:
|
||||
"""将某节点未发送的提醒标记为已关闭,保留历史发送记录。"""
|
||||
rows = db.session.query(ContractPaymentAlert).filter_by(node_id=node_id, status="pending").all()
|
||||
for alert in rows:
|
||||
alert.status = "resolved"
|
||||
alert.message_json = {**(alert.message_json or {}), "resolved_reason": "erp_paid"}
|
||||
alert.message_json = {**(alert.message_json or {}), "resolved_reason": reason}
|
||||
return len(rows)
|
||||
|
||||
|
||||
def mark_payment_node_paid(contract_id: int, node_id: int, payload: dict, *, operator_id: int | None = None) -> ContractPaymentContract:
|
||||
"""
|
||||
把一个未付的预付款或出货款手动标为已付。
|
||||
|
||||
核心逻辑:只改这一阶段。写入付款日期和手动来源,并关闭该节点尚未发送的预警;已发送记录保留。
|
||||
验收款、质保款和已经付款的阶段不能标记。
|
||||
|
||||
Args:
|
||||
contract_id: 合同台账主键。
|
||||
node_id: 付款节点主键,必须属于该合同。
|
||||
payload: paid_date 必填,remark 可选。
|
||||
operator_id: 操作人用户 ID,写入证据供审计。
|
||||
|
||||
Returns:
|
||||
更新后的合同。
|
||||
|
||||
Raises:
|
||||
NotFoundError: 合同或节点不存在。
|
||||
ValidationError: 阶段类型、状态或付款日期不合法。
|
||||
"""
|
||||
from app.core.errors import NotFoundError, ValidationError
|
||||
|
||||
contract = db.session.get(ContractPaymentContract, contract_id)
|
||||
if contract is None:
|
||||
raise NotFoundError("合同不存在")
|
||||
node = db.session.get(ContractPaymentNode, node_id)
|
||||
if node is None or node.contract_id != contract.id:
|
||||
raise NotFoundError("付款节点不存在")
|
||||
if node.node_type not in {"prepayment", "shipment"}:
|
||||
raise ValidationError("只有预付款和出货款可以手动标为已付")
|
||||
if node.status == "paid":
|
||||
raise ValidationError("该阶段已是已付")
|
||||
if node.status != "active":
|
||||
raise ValidationError("该阶段当前不能标为已付")
|
||||
raw_date = str(payload.get("paid_date") or "").strip()[:10]
|
||||
try:
|
||||
paid_date = date.fromisoformat(raw_date)
|
||||
except ValueError:
|
||||
raise ValidationError("请填写付款日期") from None
|
||||
remark = str(payload.get("remark") or "").strip()
|
||||
if len(remark) > 500:
|
||||
raise ValidationError("说明不能超过 500 字")
|
||||
node.status = "paid"
|
||||
node.evidence_json = {
|
||||
**(node.evidence_json or {}),
|
||||
"paid_at": paid_date.isoformat(),
|
||||
"paid_source": "manual",
|
||||
"paid_remark": remark or None,
|
||||
"paid_by": operator_id,
|
||||
}
|
||||
_resolve_pending_alerts(node_id=node.id, reason="manual_paid")
|
||||
db.session.commit()
|
||||
return contract
|
||||
|
||||
|
||||
_FAILURE_REASON_LABELS: dict[str, str] = {
|
||||
"notify_disabled": "企业微信通知未启用",
|
||||
"event_disabled": "合同付款预警通知事件未启用",
|
||||
|
||||
@@ -194,3 +194,35 @@ def test_manual_review_keeps_paid_fact_and_acceptance_event(app):
|
||||
assert nodes["warranty"].status == "not_applicable"
|
||||
assert event.event_date == date(2026, 10, 1)
|
||||
assert event.source_event_id == f"manual-review:{contract.id}"
|
||||
|
||||
|
||||
def test_mark_prepayment_paid_closes_only_pending_alerts(app):
|
||||
"""关键断言:手动标为已付只改这一阶段,未发送预警关闭,已发送记录保留。"""
|
||||
from app.models import ContractPaymentAlert
|
||||
from app.services.contract_payment.alert_service import mark_payment_node_paid
|
||||
|
||||
with app.app_context():
|
||||
contract = ContractPaymentContract(contract_no="JC-MARK", status="active", parse_status="parsed", plan_status="active")
|
||||
db.session.add(contract)
|
||||
db.session.flush()
|
||||
prepay = ContractPaymentNode(contract_id=contract.id, node_code="prepayment", node_type="prepayment", amount=Decimal("35400"), status="active")
|
||||
shipment = ContractPaymentNode(contract_id=contract.id, node_code="shipment", node_type="shipment", amount=Decimal("47200"), status="active")
|
||||
db.session.add_all([prepay, shipment])
|
||||
db.session.flush()
|
||||
pending = ContractPaymentAlert(contract_id=contract.id, node_id=prepay.id, alert_round=1, trigger_key="round:1", recipient_user_id=1, status="pending")
|
||||
sent = ContractPaymentAlert(contract_id=contract.id, node_id=prepay.id, alert_round=1, trigger_key="round:1:sent", recipient_user_id=2, status="sent")
|
||||
db.session.add_all([pending, sent])
|
||||
db.session.commit()
|
||||
mark_payment_node_paid(contract.id, prepay.id, {"paid_date": "2026-09-21", "remark": "财务已确认"}, operator_id=9)
|
||||
db.session.refresh(prepay)
|
||||
db.session.refresh(shipment)
|
||||
db.session.refresh(pending)
|
||||
db.session.refresh(sent)
|
||||
|
||||
assert prepay.status == "paid"
|
||||
assert prepay.evidence_json["paid_at"] == "2026-09-21"
|
||||
assert prepay.evidence_json["paid_source"] == "manual"
|
||||
assert shipment.status == "active"
|
||||
assert pending.status == "resolved"
|
||||
assert pending.message_json["resolved_reason"] == "manual_paid"
|
||||
assert sent.status == "sent"
|
||||
Reference in new issue
Block a user