feat(agent): 立账凭证检核
(cherry picked from commit f9e2fb5efc1ec7bf4f6e52e08dd3591b4e77edd9)
This commit is contained in:
1 parent
9bcbf9d211
commit
f615e56bb9
70 files changed
+4968
-108
No files matched your search
@@ -0,0 +1,126 @@
|
||||
"""
|
||||
backend/app/services/voucher_check/persistence.py
|
||||
|
||||
用途:立账凭证检核单票状态与批次明细的事务写入。
|
||||
依赖:models.voucher_check、ScheduleRun、SQLAlchemy session。
|
||||
逻辑:runner 阶段 B 调用;skipped 只更新 last_seen_erp_at;批次级失败不进入本模块。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime
|
||||
from typing import Any
|
||||
|
||||
from app import db
|
||||
from app.models.voucher_check import VoucherCheckInvoiceState, VoucherCheckRunItem
|
||||
|
||||
|
||||
def persist_batch_results(
|
||||
*,
|
||||
schedule_run_id: int,
|
||||
scope_code: str,
|
||||
period_month: date,
|
||||
items: list[dict[str, Any]],
|
||||
now: datetime | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
单事务写入批次明细并 upsert 最新状态。
|
||||
核心逻辑:先写 run_items;非 skipped/technical_error 再 upsert states;
|
||||
skipped 仅刷新 last_seen_erp_at。
|
||||
|
||||
Args:
|
||||
schedule_run_id: ScheduleRun.id
|
||||
scope_code: 账套
|
||||
period_month: 当月首日
|
||||
items: 内存中的结果行(含 result / fingerprint / missing 等)
|
||||
now: 可选时钟
|
||||
"""
|
||||
ts = now or datetime.utcnow()
|
||||
for item in items:
|
||||
invoice_no = str(item["invoice_no"]).strip()
|
||||
result = str(item["result"])
|
||||
run_item = VoucherCheckRunItem(
|
||||
schedule_run_id=schedule_run_id,
|
||||
scope_code=scope_code,
|
||||
period_month=period_month,
|
||||
invoice_no=invoice_no,
|
||||
invoice_date=item.get("invoice_date"),
|
||||
result=result,
|
||||
folder_path=item.get("folder_path"),
|
||||
folder_exists=bool(item.get("folder_exists")),
|
||||
material_fingerprint=item.get("material_fingerprint"),
|
||||
matched_files_json=item.get("matched_files") or {},
|
||||
missing_materials_json=item.get("missing_materials") or [],
|
||||
error_code=item.get("error_code"),
|
||||
error_message=item.get("error_message"),
|
||||
checked_at=item.get("checked_at"),
|
||||
created_at=ts,
|
||||
)
|
||||
db.session.add(run_item)
|
||||
|
||||
state = (
|
||||
db.session.query(VoucherCheckInvoiceState)
|
||||
.filter_by(
|
||||
scope_code=scope_code,
|
||||
period_month=period_month,
|
||||
invoice_no=invoice_no,
|
||||
)
|
||||
.one_or_none()
|
||||
)
|
||||
if state is None:
|
||||
state = VoucherCheckInvoiceState(
|
||||
scope_code=scope_code,
|
||||
period_month=period_month,
|
||||
invoice_no=invoice_no,
|
||||
created_at=ts,
|
||||
)
|
||||
db.session.add(state)
|
||||
|
||||
state.last_seen_erp_at = ts
|
||||
state.invoice_date = item.get("invoice_date") or state.invoice_date
|
||||
state.updated_at = ts
|
||||
|
||||
if result == "skipped":
|
||||
# 跳过:不改写齐全结论与实际扫描时间
|
||||
continue
|
||||
if result == "technical_error":
|
||||
continue
|
||||
|
||||
state.status = "complete" if result == "complete" else "missing"
|
||||
state.folder_path = item.get("folder_path")
|
||||
state.folder_exists = bool(item.get("folder_exists"))
|
||||
state.material_fingerprint = item.get("material_fingerprint")
|
||||
state.matched_files_json = item.get("matched_files") or {}
|
||||
state.missing_materials_json = item.get("missing_materials") or []
|
||||
state.last_actual_run_id = schedule_run_id
|
||||
state.last_checked_at = item.get("checked_at") or ts
|
||||
|
||||
db.session.flush()
|
||||
run_item.invoice_state_id = state.id
|
||||
|
||||
db.session.flush()
|
||||
|
||||
|
||||
def mark_missing_notified(
|
||||
*,
|
||||
scope_code: str,
|
||||
period_month: date,
|
||||
invoice_nos: list[str],
|
||||
notified_on: date,
|
||||
) -> None:
|
||||
"""发送缺资料通知成功后更新同票同日节流字段。"""
|
||||
if not invoice_nos:
|
||||
return
|
||||
states = (
|
||||
db.session.query(VoucherCheckInvoiceState)
|
||||
.filter(
|
||||
VoucherCheckInvoiceState.scope_code == scope_code,
|
||||
VoucherCheckInvoiceState.period_month == period_month,
|
||||
VoucherCheckInvoiceState.invoice_no.in_(invoice_nos),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for state in states:
|
||||
state.last_missing_notified_on = notified_on
|
||||
state.updated_at = datetime.utcnow()
|
||||
db.session.flush()
|
||||
Reference in new issue
Block a user