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

@@ -0,0 +1,136 @@
"""
backend/migrations/versions/vc4e5f6a7b8c_seed_voucher_po_print_schedule.py
用途:写入立账凭证 PO 订单生成周批(周二 05:00),早于检核周批(06:00)。
依赖:Alembic revision vc3d4e5f6a7b;schedule_tasks / users 已存在。
逻辑:有用户则插入 SCH-ACCOUNTING-VOUCHER-PO-PRINT;无用户时跳过,由应用钩子补建。
企微接收人尽量从检核排程复制。downgrade 只删本 schedule_no。
Revision ID: vc4e5f6a7b8c
Revises: vc3d4e5f6a7b
Create Date: 2026-09-14 19:40:00.000000
"""
from __future__ import annotations
import json
from datetime import datetime, timedelta, timezone
import sqlalchemy as sa
from alembic import op
revision = "vc4e5f6a7b8c"
down_revision = "vc3d4e5f6a7b"
branch_labels = None
depends_on = None
_SCHEDULE_NO = "SCH-ACCOUNTING-VOUCHER-PO-PRINT"
_CRON_EXPR = "0 5 * * 2"
_TZ_NAME = "Asia/Shanghai"
_JOB_PARAMS = {
"profile_code": "sales",
"scope_code": "HXJM",
"notify_failure_enabled": True,
}
def _compute_next_run_at_utc_naive() -> datetime:
"""按周二 05:00 Asia/Shanghai 计算下次执行,返回 UTC naive。"""
try:
from croniter import croniter
from zoneinfo import ZoneInfo
tz = ZoneInfo(_TZ_NAME)
base = datetime.now(tz)
next_local = croniter(_CRON_EXPR, base).get_next(datetime)
if next_local.tzinfo is None:
next_local = next_local.replace(tzinfo=tz)
return next_local.astimezone(timezone.utc).replace(tzinfo=None)
except Exception:
tz = timezone(timedelta(hours=8))
now_local = datetime.now(tz)
next_local = now_local.replace(hour=5, minute=0, second=0, microsecond=0)
while next_local <= now_local or next_local.weekday() != 1:
next_local = next_local + timedelta(days=1)
next_local = next_local.replace(hour=5, minute=0, second=0, microsecond=0)
return next_local.astimezone(timezone.utc).replace(tzinfo=None)
def upgrade() -> None:
conn = op.get_bind()
exists = conn.execute(
sa.text(
"SELECT 1 FROM schedule_tasks "
"WHERE schedule_no = :sno OR platform_api_path = 'accounting-voucher-po-print' "
"LIMIT 1"
),
{"sno": _SCHEDULE_NO},
).first()
if exists:
return
owner = conn.execute(
sa.text("SELECT id FROM users WHERE role = 'admin' ORDER BY id ASC LIMIT 1")
).first()
if owner is None:
owner = conn.execute(sa.text("SELECT id FROM users ORDER BY id ASC LIMIT 1")).first()
if owner is None:
return
notify_row = conn.execute(
sa.text(
"SELECT notify_user_ids_json FROM schedule_tasks "
"WHERE schedule_no = 'SCH-ACCOUNTING-VOUCHER-CHECK' "
" OR platform_api_path = 'accounting-voucher-check' "
"LIMIT 1"
)
).first()
notify_ids = notify_row[0] if notify_row and notify_row[0] is not None else []
if not isinstance(notify_ids, list):
notify_ids = []
now = datetime.utcnow()
next_run = _compute_next_run_at_utc_naive()
conn.execute(
sa.text(
"""
INSERT INTO schedule_tasks (
schedule_no, name, schedule_type, owner_user_id,
cron_expr, schedule_timezone, wait_seconds,
platform_api_path, job_params, notify_user_ids_json,
kb_sync_enabled, notify_enabled,
enabled, next_run_at, last_run_at,
created_by, updated_by, created_at, updated_at
) VALUES (
:schedule_no, :name, 'platform_job', :owner_user_id,
:cron_expr, 'Asia/Shanghai', 0,
'accounting-voucher-po-print', CAST(:job_params AS JSONB), CAST(:notify_ids AS JSONB),
false, true,
true, :next_run_at, NULL,
:owner_user_id, :owner_user_id, :now, :now
)
"""
).bindparams(
sa.bindparam("job_params", type_=sa.String()),
sa.bindparam("notify_ids", type_=sa.String()),
),
{
"schedule_no": _SCHEDULE_NO,
"name": "立账凭证 PO 订单生成(周批)",
"owner_user_id": int(owner[0]),
"cron_expr": _CRON_EXPR,
"job_params": json.dumps(_JOB_PARAMS, ensure_ascii=False),
"notify_ids": json.dumps(notify_ids, ensure_ascii=False),
"next_run_at": next_run,
"now": now,
},
)
def downgrade() -> None:
"""仅删除本迁移约定的 schedule_no,避免误删管理员手工创建的同路径排程。"""
conn = op.get_bind()
conn.execute(
sa.text("DELETE FROM schedule_tasks WHERE schedule_no = :sno"),
{"sno": _SCHEDULE_NO},
)