feat(invoice-check): 通过invoice号反查tghx的采购单,生成PO订单

This commit is contained in:
sophie committed 2026-09-15 16:15:08 +08:00
1 parent 1c34ab962f
commit 40cead7430
27 files changed
+792 -74

No files matched your search

@@ -38,6 +38,7 @@ def test_platform_jobs_registered():
assert "shipping-inventory-rpa" in keys
assert "faq-aggregate-hot" in keys
assert "fixed-assets-snapshot" in keys
assert "accounting-voucher-po-print" in keys
assert get_platform_job("shipping-inventory-rpa") is not None
@@ -217,6 +218,7 @@ def test_get_platform_jobs_api(client, admin_jwt_headers):
assert by_key["shipping-inventory-rpa"]["label_key"] == "admin.schedule.form.platformJobShippingInventory"
assert by_key["faq-aggregate-hot"]["label_key"] == "admin.schedule.form.platformJobFaqHotAggregate"
assert by_key["fixed-assets-snapshot"]["label_key"] == "admin.schedule.form.platformJobFixedAssetsSnapshot"
assert by_key["accounting-voucher-po-print"]["label_key"] == "admin.schedule.form.platformJobAccountingVoucherPoPrint"
defs = client.get("/api/admin/platform-tasks/definitions", headers=admin_jwt_headers)
assert defs.status_code == 200
@@ -0,0 +1,184 @@
"""
backend/tests/voucher_check/test_po_print_schedule.py
用途:PO 订单生成周批早于检核,且不走 erp_po_print.fetch。
依赖:pytest、app fixture;不连 Oracle / 文档库。
逻辑:断言 cron 05:00、种子幂等、取数后上传、无 PO 跳过、检核 fetch 仍关闭。
"""
from __future__ import annotations
from datetime import date
from types import SimpleNamespace
import pytest
from app.core.errors import ServiceUnavailableError
from app.models import ScheduleRun, ScheduleTask
from app.services.voucher_check.adapters.erp_po_print import ErpPoPrintAdapter
from app.services.voucher_check.erp_po_repository import map_po_document
from app.services.voucher_check.po_print_runner import run_po_print_batch
from app.services.voucher_check.schedule_seed import (
DEFAULT_VOUCHER_CHECK_CRON,
DEFAULT_VOUCHER_PO_PRINT_CRON,
DEFAULT_VOUCHER_PO_PRINT_PLATFORM_PATH,
DEFAULT_VOUCHER_PO_PRINT_SCHEDULE_NO,
ensure_default_voucher_check_schedule,
ensure_default_voucher_po_print_schedule,
)
def test_po_print_cron_is_earlier_than_check():
"""周二 05:00 早于检核 06:00。"""
assert DEFAULT_VOUCHER_PO_PRINT_CRON == "0 5 * * 2"
assert DEFAULT_VOUCHER_CHECK_CRON == "0 6 * * 2"
def test_ensure_po_print_schedule_is_idempotent(app):
with app.app_context():
from app import db
from app.models import User
if db.session.query(User).first() is None:
user = User(
username="po-print-seed-admin",
email="po-print-seed-admin@example.com",
role="admin",
status="active",
)
user.set_password("test")
db.session.add(user)
db.session.commit()
check = ensure_default_voucher_check_schedule(commit=True)
first = ensure_default_voucher_po_print_schedule(commit=True)
second = ensure_default_voucher_po_print_schedule(commit=True)
assert check is not None
assert first is not None
assert first.id == second.id
assert first.schedule_no == DEFAULT_VOUCHER_PO_PRINT_SCHEDULE_NO
assert first.platform_api_path == DEFAULT_VOUCHER_PO_PRINT_PLATFORM_PATH
assert first.cron_expr == DEFAULT_VOUCHER_PO_PRINT_CRON
assert check.cron_expr == DEFAULT_VOUCHER_CHECK_CRON
assert first.platform_api_path != check.platform_api_path
def test_po_print_batch_uploads_and_skips_no_po(app, monkeypatch):
"""有 PO 的发票上传;没有 TGHX 采购单的发票记 skipped_no_po;不调用 fetch()。"""
uploaded: list[str] = []
def fake_fetch_month(config):
return (
date(2026, 8, 1),
[
SimpleNamespace(invoice_no="EWZZ26090002"),
SimpleNamespace(invoice_no="EWZZ26090099"),
],
)
monkeypatch.setattr(
"app.services.voucher_check.po_print_runner.fetch_month_invoices",
fake_fetch_month,
)
monkeypatch.setattr(
"app.services.voucher_check.po_print_runner.SalesDmsClient.ensure_configured",
lambda: None,
)
monkeypatch.setattr(
"app.services.voucher_check.po_print_runner.load_engine_config",
lambda params=None: SimpleNamespace(notify_failure_enabled=False),
)
monkeypatch.setattr(
ErpPoPrintAdapter,
"fetch_documents",
lambda self, invoices, config=None: [map_po_document("EWZZ26090002", [{"po_no": "5220-1", "pmn02": 1}])],
)
def fake_upload(self, document, period_month):
uploaded.append(document.po_no)
return SimpleNamespace(
skipped_same_name=False,
file=SimpleNamespace(filename=f"{document.invoice_no}-{document.po_no}.pdf"),
)
monkeypatch.setattr(ErpPoPrintAdapter, "upload_given", fake_upload)
monkeypatch.setattr("app.services.voucher_check.po_print_runner.send_failure_alert", lambda **kwargs: None)
with app.app_context():
task = ScheduleTask(
schedule_no="SCH-TEST-PO",
name="po print",
schedule_type="platform_job",
platform_api_path="accounting-voucher-po-print",
owner_user_id=1,
cron_expr="0 5 * * 2",
job_params={"profile_code": "sales", "scope_code": "HXJM", "notify_failure_enabled": False},
)
run = ScheduleRun(run_no="RUN-PO-1", schedule_task_id=1, status="running")
total, success, failed, skipped, details, errors, logs = run_po_print_batch(task, run)
assert total == 2
assert success == 1
assert failed == 0
assert skipped == 1
assert uploaded == ["5220-1"]
assert any(item.get("status") == "skipped_no_po" for item in details)
assert ErpPoPrintAdapter.wired is False
with pytest.raises(ServiceUnavailableError) as exc:
ErpPoPrintAdapter().fetch(period_month=date(2026, 8, 1), subjects=[])
assert exc.value.code == "VOUCHER_CHECK.ADAPTER_NOT_WIRED"
def test_po_print_batch_counts_upload_failure(app, monkeypatch):
monkeypatch.setattr(
"app.services.voucher_check.po_print_runner.fetch_month_invoices",
lambda config: (date(2026, 8, 1), [SimpleNamespace(invoice_no="EWZZ26090002")]),
)
monkeypatch.setattr(
"app.services.voucher_check.po_print_runner.SalesDmsClient.ensure_configured",
lambda: None,
)
monkeypatch.setattr(
"app.services.voucher_check.po_print_runner.load_engine_config",
lambda params=None: SimpleNamespace(notify_failure_enabled=False),
)
monkeypatch.setattr(
ErpPoPrintAdapter,
"fetch_documents",
lambda self, invoices, config=None: [map_po_document("EWZZ26090002", [{"po_no": "PO-FAIL", "pmn02": 1}])],
)
monkeypatch.setattr(
ErpPoPrintAdapter,
"upload_given",
lambda self, document, period_month: (_ for _ in ()).throw(RuntimeError("dms down")),
)
alerts: list[str] = []
monkeypatch.setattr(
"app.services.voucher_check.po_print_runner.send_failure_alert",
lambda **kwargs: alerts.append(kwargs.get("stage") or ""),
)
with app.app_context():
task = ScheduleTask(
schedule_no="SCH-TEST-PO2",
name="po print",
schedule_type="platform_job",
platform_api_path="accounting-voucher-po-print",
owner_user_id=1,
cron_expr="0 5 * * 2",
job_params={},
)
run = ScheduleRun(run_no="RUN-PO-2", schedule_task_id=1, status="running")
total, success, failed, skipped, details, errors, logs = run_po_print_batch(task, run)
assert total == 1
assert success == 0
assert failed == 1
assert "upload" in alerts
assert errors
def test_platform_job_registered():
from app.services.schedule.job_registry import get_platform_job
job = get_platform_job("accounting-voucher-po-print")
assert job is not None
assert job.label_key == "admin.schedule.form.platformJobAccountingVoucherPoPrint"
assert job.handler is not None