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,235 @@
|
||||
"""
|
||||
backend/migrations/versions/a0b1c2d3e4f5_add_voucher_check_tables.py
|
||||
|
||||
用途:新增立账凭证检核状态/明细表,并种子日批 ScheduleTask(06:00)。
|
||||
依赖:Alembic revision e5f7a9b1c3d4;schedule_runs / users 已存在。
|
||||
逻辑:建表 + 幂等插入 SCH-ACCOUNTING-VOUCHER-CHECK;无用户时跳过种子由应用钩子补建。
|
||||
|
||||
Revision ID: a0b1c2d3e4f5
|
||||
Revises: e5f7a9b1c3d4
|
||||
Create Date: 2026-08-05 10:50:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "a0b1c2d3e4f5"
|
||||
down_revision = "e5f7a9b1c3d4"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
_SCHEDULE_NO = "SCH-ACCOUNTING-VOUCHER-CHECK"
|
||||
_CRON_EXPR = "0 6 * * *"
|
||||
_TZ_NAME = "Asia/Shanghai"
|
||||
|
||||
_DEFAULT_JOB_PARAMS = {
|
||||
"scope_code": "HXJM",
|
||||
"directory_template": "{root}/{YYYY}/{MM}/{invoice_no}",
|
||||
"required_files": [
|
||||
{
|
||||
"code": "price_check_sheet",
|
||||
"name": "核价单",
|
||||
"required": True,
|
||||
"accepted_filenames": ["核价单.pdf"],
|
||||
},
|
||||
{
|
||||
"code": "purchase_order",
|
||||
"name": "采购单",
|
||||
"required": True,
|
||||
"accepted_filenames": ["采购单.pdf"],
|
||||
},
|
||||
],
|
||||
"filename_case_sensitive": True,
|
||||
"notify_missing_enabled": True,
|
||||
"notify_failure_enabled": True,
|
||||
"same_day_dedup_enabled": True,
|
||||
}
|
||||
|
||||
|
||||
def _compute_next_run_at_utc_naive() -> datetime:
|
||||
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=6, minute=0, second=0, microsecond=0)
|
||||
if next_local <= now_local:
|
||||
next_local = next_local + timedelta(days=1)
|
||||
return next_local.astimezone(timezone.utc).replace(tzinfo=None)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"voucher_check_invoice_states",
|
||||
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
|
||||
sa.Column("scope_code", sa.String(length=32), nullable=False),
|
||||
sa.Column("period_month", sa.Date(), nullable=False),
|
||||
sa.Column("invoice_no", sa.String(length=128), nullable=False),
|
||||
sa.Column("invoice_date", sa.Date(), nullable=True),
|
||||
sa.Column("status", sa.String(length=24), nullable=False),
|
||||
sa.Column("folder_path", sa.String(length=1024), nullable=True),
|
||||
sa.Column("folder_exists", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
||||
sa.Column("material_fingerprint", sa.String(length=64), nullable=True),
|
||||
sa.Column("matched_files_json", sa.JSON(), nullable=True),
|
||||
sa.Column("missing_materials_json", sa.JSON(), nullable=True),
|
||||
sa.Column("last_actual_run_id", sa.BigInteger(), sa.ForeignKey("schedule_runs.id", ondelete="SET NULL"), nullable=True),
|
||||
sa.Column("last_checked_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("last_seen_erp_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("last_missing_notified_on", sa.Date(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.UniqueConstraint(
|
||||
"scope_code",
|
||||
"period_month",
|
||||
"invoice_no",
|
||||
name="uq_voucher_check_invoice_states_scope_period_invoice",
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_voucher_check_invoice_states_period_status",
|
||||
"voucher_check_invoice_states",
|
||||
["period_month", "status"],
|
||||
)
|
||||
op.create_index(
|
||||
"ix_voucher_check_invoice_states_invoice_no",
|
||||
"voucher_check_invoice_states",
|
||||
["invoice_no"],
|
||||
)
|
||||
op.create_index(
|
||||
"ix_voucher_check_invoice_states_notified_on",
|
||||
"voucher_check_invoice_states",
|
||||
["last_missing_notified_on"],
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"voucher_check_run_items",
|
||||
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
|
||||
sa.Column(
|
||||
"schedule_run_id",
|
||||
sa.BigInteger(),
|
||||
sa.ForeignKey("schedule_runs.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
"invoice_state_id",
|
||||
sa.BigInteger(),
|
||||
sa.ForeignKey("voucher_check_invoice_states.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column("scope_code", sa.String(length=32), nullable=False),
|
||||
sa.Column("period_month", sa.Date(), nullable=False),
|
||||
sa.Column("invoice_no", sa.String(length=128), nullable=False),
|
||||
sa.Column("invoice_date", sa.Date(), nullable=True),
|
||||
sa.Column("result", sa.String(length=24), nullable=False),
|
||||
sa.Column("folder_path", sa.String(length=1024), nullable=True),
|
||||
sa.Column("folder_exists", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
||||
sa.Column("material_fingerprint", sa.String(length=64), nullable=True),
|
||||
sa.Column("matched_files_json", sa.JSON(), nullable=True),
|
||||
sa.Column("missing_materials_json", sa.JSON(), nullable=True),
|
||||
sa.Column("error_code", sa.String(length=64), nullable=True),
|
||||
sa.Column("error_message", sa.Text(), nullable=True),
|
||||
sa.Column("checked_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.UniqueConstraint(
|
||||
"schedule_run_id",
|
||||
"invoice_no",
|
||||
name="uq_voucher_check_run_items_run_invoice",
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_voucher_check_run_items_schedule_run_id",
|
||||
"voucher_check_run_items",
|
||||
["schedule_run_id"],
|
||||
)
|
||||
op.create_index(
|
||||
"ix_voucher_check_run_items_run_result",
|
||||
"voucher_check_run_items",
|
||||
["schedule_run_id", "result"],
|
||||
)
|
||||
op.create_index(
|
||||
"ix_voucher_check_run_items_period_result",
|
||||
"voucher_check_run_items",
|
||||
["period_month", "result"],
|
||||
)
|
||||
|
||||
conn = op.get_bind()
|
||||
exists = conn.execute(
|
||||
sa.text(
|
||||
"SELECT 1 FROM schedule_tasks "
|
||||
"WHERE schedule_no = :sno OR platform_api_path = 'accounting-voucher-check' "
|
||||
"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
|
||||
|
||||
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,
|
||||
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-check', CAST(:job_params 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())),
|
||||
{
|
||||
"schedule_no": _SCHEDULE_NO,
|
||||
"name": "立账凭证检核(日批)",
|
||||
"owner_user_id": int(owner[0]),
|
||||
"cron_expr": _CRON_EXPR,
|
||||
"job_params": json.dumps(_DEFAULT_JOB_PARAMS, ensure_ascii=False),
|
||||
"next_run_at": next_run,
|
||||
"now": now,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
conn = op.get_bind()
|
||||
conn.execute(
|
||||
sa.text("DELETE FROM schedule_tasks WHERE schedule_no = :sno"),
|
||||
{"sno": _SCHEDULE_NO},
|
||||
)
|
||||
op.drop_index("ix_voucher_check_run_items_period_result", table_name="voucher_check_run_items")
|
||||
op.drop_index("ix_voucher_check_run_items_run_result", table_name="voucher_check_run_items")
|
||||
op.drop_index("ix_voucher_check_run_items_schedule_run_id", table_name="voucher_check_run_items")
|
||||
op.drop_table("voucher_check_run_items")
|
||||
op.drop_index("ix_voucher_check_invoice_states_notified_on", table_name="voucher_check_invoice_states")
|
||||
op.drop_index("ix_voucher_check_invoice_states_invoice_no", table_name="voucher_check_invoice_states")
|
||||
op.drop_index("ix_voucher_check_invoice_states_period_status", table_name="voucher_check_invoice_states")
|
||||
op.drop_table("voucher_check_invoice_states")
|
||||
Reference in new issue
Block a user