fix(contract-plan): 合同付款预警增加“标记已付”功能
This commit is contained in:
1 parent
5c6ccf9792
commit
ac3fc99883
11 files changed
+231
-19
No files matched your search
@@ -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": "合同付款预警通知事件未启用",
|
||||
|
||||
Reference in new issue
Block a user