feat(invoice-check): 通过invoice号反查tghx的采购单,生成PO订单
This commit is contained in:
1 parent
1c34ab962f
commit
40cead7430
27 files changed
+792
-74
No files matched your search
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
backend/app/services/voucher_check/schedule_seed.py
|
||||
|
||||
用途:确保 accounting-voucher-check 默认周批 ScheduleTask 存在(周二 06:00 Asia/Shanghai)。
|
||||
依赖:ScheduleTask/User、timezone_util、voucher_check.config.DEFAULT_JOB_PARAMS。
|
||||
逻辑:迁移种子 + create_app / User after_commit 幂等补建;已存在的日批改成周二,并剥离 schema 外的旧 job_params。
|
||||
用途:确保立账凭证检核(周二 06:00)与 PO 订单生成(周二 05:00)默认 ScheduleTask 存在。
|
||||
依赖:ScheduleTask/User、timezone_util、voucher_check.config。
|
||||
逻辑:迁移种子 + create_app / User after_commit 幂等补建;检核排程剥离 schema 外的旧 job_params。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -19,7 +19,7 @@ from sqlalchemy.orm.attributes import flag_modified
|
||||
from app import db
|
||||
from app.models import ScheduleTask, User
|
||||
from app.services.schedule.timezone_util import compute_next_run_at
|
||||
from app.services.voucher_check.config import DEFAULT_JOB_PARAMS
|
||||
from app.services.voucher_check.config import DEFAULT_JOB_PARAMS, DEFAULT_PO_PRINT_JOB_PARAMS
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -28,6 +28,11 @@ DEFAULT_VOUCHER_CHECK_PLATFORM_PATH = "accounting-voucher-check"
|
||||
DEFAULT_VOUCHER_CHECK_CRON = "0 6 * * 2"
|
||||
DEFAULT_VOUCHER_CHECK_NAME = "立账凭证检核(周批)"
|
||||
|
||||
DEFAULT_VOUCHER_PO_PRINT_SCHEDULE_NO = "SCH-ACCOUNTING-VOUCHER-PO-PRINT"
|
||||
DEFAULT_VOUCHER_PO_PRINT_PLATFORM_PATH = "accounting-voucher-po-print"
|
||||
DEFAULT_VOUCHER_PO_PRINT_CRON = "0 5 * * 2"
|
||||
DEFAULT_VOUCHER_PO_PRINT_NAME = "立账凭证 PO 订单生成(周批)"
|
||||
|
||||
_HOOKS_REGISTERED = False
|
||||
_SESSION_INFO_KEY = "_seed_voucher_check_schedule"
|
||||
|
||||
@@ -163,6 +168,113 @@ def ensure_default_voucher_check_schedule(
|
||||
return task
|
||||
|
||||
|
||||
def _pick_schedule_owner(sess: Any) -> Any | None:
|
||||
"""优先管理员,否则任意用户;尚无用户时返回 None。"""
|
||||
owner = (
|
||||
sess.query(User)
|
||||
.filter(User.role == "admin")
|
||||
.order_by(User.id.asc())
|
||||
.first()
|
||||
)
|
||||
if owner is None:
|
||||
owner = sess.query(User).order_by(User.id.asc()).first()
|
||||
return owner
|
||||
|
||||
|
||||
def ensure_default_voucher_po_print_schedule(
|
||||
*,
|
||||
commit: bool = True,
|
||||
session: Any | None = None,
|
||||
) -> ScheduleTask | None:
|
||||
"""
|
||||
幂等确保默认 PO 订单生成周批排程存在(周二 05:00,早于检核 06:00)。
|
||||
核心逻辑:已存在同 schedule_no / platform_api_path 则跳过;新建时尽量复用检核任务的企微接收人。
|
||||
|
||||
Args:
|
||||
commit: 是否立即提交。
|
||||
session: 可选独立 Session。
|
||||
|
||||
Returns:
|
||||
新建或已存在的 ScheduleTask;尚无用户时返回 None。
|
||||
"""
|
||||
sess = session if session is not None else db.session
|
||||
existing = (
|
||||
sess.query(ScheduleTask)
|
||||
.filter(
|
||||
(ScheduleTask.schedule_no == DEFAULT_VOUCHER_PO_PRINT_SCHEDULE_NO)
|
||||
| (ScheduleTask.platform_api_path == DEFAULT_VOUCHER_PO_PRINT_PLATFORM_PATH)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if existing is not None:
|
||||
return existing
|
||||
|
||||
owner = _pick_schedule_owner(sess)
|
||||
if owner is None:
|
||||
_LOGGER.info("skip seed accounting-voucher-po-print schedule: no users yet")
|
||||
return None
|
||||
|
||||
check_task = (
|
||||
sess.query(ScheduleTask)
|
||||
.filter(
|
||||
(ScheduleTask.schedule_no == DEFAULT_VOUCHER_CHECK_SCHEDULE_NO)
|
||||
| (ScheduleTask.platform_api_path == DEFAULT_VOUCHER_CHECK_PLATFORM_PATH)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
notify_ids = []
|
||||
if check_task is not None and isinstance(check_task.notify_user_ids_json, list):
|
||||
notify_ids = list(check_task.notify_user_ids_json)
|
||||
|
||||
now = datetime.utcnow()
|
||||
next_run = compute_next_run_at(
|
||||
DEFAULT_VOUCHER_PO_PRINT_CRON,
|
||||
timezone_name="Asia/Shanghai",
|
||||
) or now
|
||||
|
||||
task = ScheduleTask(
|
||||
schedule_no=DEFAULT_VOUCHER_PO_PRINT_SCHEDULE_NO,
|
||||
name=DEFAULT_VOUCHER_PO_PRINT_NAME,
|
||||
schedule_type="platform_job",
|
||||
owner_user_id=int(owner.id),
|
||||
cron_expr=DEFAULT_VOUCHER_PO_PRINT_CRON,
|
||||
schedule_timezone="Asia/Shanghai",
|
||||
wait_seconds=0,
|
||||
platform_api_path=DEFAULT_VOUCHER_PO_PRINT_PLATFORM_PATH,
|
||||
job_params=dict(DEFAULT_PO_PRINT_JOB_PARAMS),
|
||||
kb_sync_enabled=False,
|
||||
notify_user_ids_json=notify_ids,
|
||||
notify_enabled=True,
|
||||
enabled=True,
|
||||
next_run_at=next_run,
|
||||
created_by=int(owner.id),
|
||||
updated_by=int(owner.id),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
sess.add(task)
|
||||
if commit:
|
||||
try:
|
||||
sess.commit()
|
||||
except Exception:
|
||||
sess.rollback()
|
||||
existing = (
|
||||
sess.query(ScheduleTask)
|
||||
.filter(
|
||||
(ScheduleTask.schedule_no == DEFAULT_VOUCHER_PO_PRINT_SCHEDULE_NO)
|
||||
| (ScheduleTask.platform_api_path == DEFAULT_VOUCHER_PO_PRINT_PLATFORM_PATH)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if existing is not None:
|
||||
return existing
|
||||
raise
|
||||
else:
|
||||
sess.flush()
|
||||
_LOGGER.info("seeded accounting-voucher-po-print schedule id=%s", task.id)
|
||||
return task
|
||||
|
||||
|
||||
def register_voucher_check_schedule_hooks() -> None:
|
||||
"""注册 User after_commit 补建钩子(幂等)。"""
|
||||
global _HOOKS_REGISTERED
|
||||
@@ -181,6 +293,7 @@ def register_voucher_check_schedule_hooks() -> None:
|
||||
factory = sessionmaker(bind=bind)
|
||||
with factory() as new_sess:
|
||||
ensure_default_voucher_check_schedule(commit=True, session=new_sess)
|
||||
ensure_default_voucher_po_print_schedule(commit=True, session=new_sess)
|
||||
except Exception:
|
||||
_LOGGER.exception("voucher-check schedule seed after_commit failed")
|
||||
|
||||
|
||||
Reference in new issue
Block a user