From 79d1242d25c5c4e87e0b2030f06408cf4c2d8141 Mon Sep 17 00:00:00 2001 From: wuzilong <704541015@qq.com> Date: Mon, 16 Nov 2026 15:02:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(pl):=20=E5=AE=89=E6=8E=92=E5=89=8D=E8=AF=81?= =?UTF-8?q?=E4=B9=A6=E9=BD=90=E5=A4=87=E6=8C=89=E7=94=9F=E6=95=88=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=86=99=E5=85=A5=E5=BE=85=E5=AE=8C=E5=96=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免版本未打标时被当成 legacy,导致待完善列表漏单;安排打成新规则时回补 pending。 Co-authored-by: Cursor --- .../pl_cert_quota_rule_config_service.py | 20 +++ backend/app/services/pl_quota_waiver.py | 23 ++- backend/tests/test_pl_quota_waiver.py | 170 ++++++++++++++++++ 3 files changed, 206 insertions(+), 7 deletions(-) diff --git a/backend/app/services/pl_cert_quota_rule_config_service.py b/backend/app/services/pl_cert_quota_rule_config_service.py index 6caf2469c..547a1e523 100644 --- a/backend/app/services/pl_cert_quota_rule_config_service.py +++ b/backend/app/services/pl_cert_quota_rule_config_service.py @@ -83,6 +83,13 @@ def stamp_plan_rule_on_arrange( version = resolve_rule_version_for_arrange(db, user.tenant_no, when) plan.cert_quota_arranged_at = when plan.cert_quota_rule_version = version + # 安排前已按旧判定齐备时,打成新规则后回补待完善,避免待完善列表漏单。 + if ( + version == VERSION_CUSTOMER_CATEGORY + and int(getattr(plan, 'quota_cert_waived', 0) or 0) == 1 + and int(getattr(plan, 'quota_cert_pending_fulfill', 0) or 0) != 1 + ): + plan.quota_cert_pending_fulfill = 1 return version @@ -95,6 +102,19 @@ def plan_uses_customer_category(plan) -> bool: return (getattr(plan, 'cert_quota_rule_version', None) or VERSION_LEGACY) == VERSION_CUSTOMER_CATEGORY +def plan_requires_cert_fulfill(db: Session, tenant_no: str, plan) -> bool: + """齐备后是否须进待完善:已打新规则,或尚未安排但按当前时刻将落入新规则。 + + 安排前 `cert_quota_rule_version` 常为空;不能把空当成 legacy,否则会漏写 pending。 + 已安排并打成 legacy 的计划仍按旧规则,不进待完善。 + """ + if plan_uses_customer_category(plan): + return True + if getattr(plan, 'cert_quota_arranged_at', None) is not None: + return False + return resolve_rule_version_for_arrange(db, tenant_no) == VERSION_CUSTOMER_CATEGORY + + def infer_arranged_at(db: Session, plan) -> Optional[datetime]: """历史缺字段时用关联运输单创建时间推断首次安排。""" stamped = getattr(plan, 'cert_quota_arranged_at', None) diff --git a/backend/app/services/pl_quota_waiver.py b/backend/app/services/pl_quota_waiver.py index bee723f0a..33fe6745d 100644 --- a/backend/app/services/pl_quota_waiver.py +++ b/backend/app/services/pl_quota_waiver.py @@ -269,7 +269,17 @@ def mark_ready(db, user, plan_id): plan = _plan(db, user, plan_id, lock=True) if plan.follow_status == "cancelled" or plan.archive_status == "cancelled": raise HTTPException(409, "已作废的计划不能标记证书齐备") + from app.services.pl_cert_quota_rule_config_service import plan_requires_cert_fulfill + if quota_cert_waived(plan): + # 历史:安排前齐备按空版本当成 legacy,漏写 pending;再次调用时回补。 + if ( + plan_requires_cert_fulfill(db, user.tenant_no, plan) + and int(getattr(plan, "quota_cert_pending_fulfill", 0) or 0) != 1 + ): + plan.quota_cert_pending_fulfill = 1 + plan.update_by = user.id + db.flush() labels = _product_names(db, user, [plan.id]) return _row(plan, labels.get(plan.id, "")) if not db.query(PlPlan.id).filter(PlPlan.id == plan.id, _toxic_exists(db, user.tenant_no)).first(): @@ -290,8 +300,7 @@ def mark_ready(db, user, plan_id): plan.quota_cert_waived_by = user.id plan.quota_cert_waived_by_name = name[:100] if name else None plan.quota_cert_waived_time = datetime.now() - from app.services.pl_cert_quota_rule_config_service import plan_uses_customer_category - if plan_uses_customer_category(plan): + if plan_requires_cert_fulfill(db, user.tenant_no, plan): plan.quota_cert_pending_fulfill = 1 change_content = "标记证书齐备:可安排运输;归档前须在证书齐备中完善并关闭" else: @@ -313,15 +322,15 @@ def mark_ready(db, user, plan_id): def list_pending_fulfill_plans(db, user, page=1, page_size=20, keyword=""): - """新规则下已齐备、待完善挂证的计划。""" - from app.services.pl_cert_quota_rule_config_service import VERSION_CUSTOMER_CATEGORY + """新规则下已齐备、待完善挂证的计划。 + 以 pending 为准;安排前版本可能仍为空,不能再要求 cert_quota_rule_version。 + """ query = db.query(PlPlan).filter( PlPlan.tenant_no == user.tenant_no, PlPlan.del_flag == 0, PlPlan.follow_status != "cancelled", PlPlan.archive_status != "cancelled", - PlPlan.cert_quota_rule_version == VERSION_CUSTOMER_CATEGORY, PlPlan.quota_cert_pending_fulfill == 1, PlPlan.quota_cert_waived == 1, ) @@ -345,14 +354,14 @@ def list_pending_fulfill_plans(db, user, page=1, page_size=20, keyword=""): def fulfill_plan_certs(db, user, plan_id, *, operation_id=None): """完善并关闭:按安排同款自动选证并 reserve,成功后出队。""" - from app.services.pl_cert_quota_rule_config_service import plan_uses_customer_category + from app.services.pl_cert_quota_rule_config_service import plan_requires_cert_fulfill from app.services.pl_plan_service import record_plan_change from app.services.pl_quota_business import apply_plan_quota from app.services.pl_quota_planning import _plan from types import SimpleNamespace plan = _plan(db, user, plan_id, lock=True) - if not plan_uses_customer_category(plan): + if not plan_requires_cert_fulfill(db, user.tenant_no, plan): raise HTTPException(409, '仅新规则计划支持完善并关闭') if int(getattr(plan, 'quota_cert_pending_fulfill', 0) or 0) != 1: raise HTTPException(409, '该计划不在证书齐备待完善列表中') diff --git a/backend/tests/test_pl_quota_waiver.py b/backend/tests/test_pl_quota_waiver.py index 472a8b3dc..d55f67cbd 100644 --- a/backend/tests/test_pl_quota_waiver.py +++ b/backend/tests/test_pl_quota_waiver.py @@ -113,6 +113,9 @@ def test_mark_ready_allows_weighed_plan_until_quota_is_posted(monkeypatch): quota_cert_waived_by=None, quota_cert_waived_by_name=None, quota_cert_waived_time=None, + quota_cert_pending_fulfill=0, + cert_quota_rule_version="legacy", + cert_quota_arranged_at=__import__("datetime").datetime(2026, 11, 1, 10, 0, 0), update_by=None, plan_no="PO1", plan_date=None, @@ -173,6 +176,173 @@ def test_mark_ready_allows_weighed_plan_until_quota_is_posted(monkeypatch): raise AssertionError("已入账仍允许证书齐备") +def test_mark_ready_before_arrange_uses_effective_at_for_pending(monkeypatch): + """安排前版本未打标时,仍按生效时间走新规则并进入待完善。""" + from datetime import datetime, timedelta + + from app.models.pl_quota_models import PlPlanCertUsage + from app.services.pl_quota_waiver import mark_ready + + plan = SimpleNamespace( + id="p1", + follow_status="draft", + archive_status="unarchived", + quota_cert_waived=0, + quota_cert_waived_by=None, + quota_cert_waived_by_name=None, + quota_cert_waived_time=None, + quota_cert_pending_fulfill=0, + cert_quota_rule_version=None, + cert_quota_arranged_at=None, + update_by=None, + plan_no="PO1", + plan_date=None, + license_plate=None, + supplier_name=None, + customer_name=None, + ) + user = SimpleNamespace(id="u1", tenant_no="shanxia2.0", name="齐备员", dept_id="") + changes = [] + + class Query: + def __init__(self, rows): + self.rows = rows + + def filter(self, *args, **kwargs): + return self + + def filter_by(self, **kwargs): + return self + + def all(self): + return list(self.rows) + + def first(self): + return self.rows[0] if self.rows else None + + class DB: + def query(self, model): + if model is PlPlanCertUsage: + return Query([]) + return Query([plan]) + + def flush(self): + return None + + monkeypatch.setattr("app.services.pl_quota_planning._plan", lambda *args, **kwargs: plan) + monkeypatch.setattr("app.services.pl_quota_waiver._toxic_exists", lambda *args, **kwargs: True) + monkeypatch.setattr("app.services.pl_quota_waiver._product_names", lambda *args, **kwargs: {}) + monkeypatch.setattr( + "app.services.pl_plan_service.record_plan_change", + lambda *args, **kwargs: changes.append(kwargs), + ) + monkeypatch.setattr( + "app.services.pl_cert_quota_rule_config_service.get_effective_at", + lambda *args, **kwargs: datetime.now() - timedelta(hours=1), + ) + + mark_ready(DB(), user, "p1") + assert plan.quota_cert_waived == 1 + assert plan.quota_cert_pending_fulfill == 1 + assert changes and "完善并关闭" in (changes[-1].get("change_content") or "") + + +def test_mark_ready_stamped_legacy_skips_pending(monkeypatch): + """已按首次安排打成 legacy 的计划,齐备不进待完善。""" + from datetime import datetime + + from app.models.pl_quota_models import PlPlanCertUsage + from app.services.pl_quota_waiver import mark_ready + + plan = SimpleNamespace( + id="p1", + follow_status="arranged", + archive_status="unarchived", + quota_cert_waived=0, + quota_cert_waived_by=None, + quota_cert_waived_by_name=None, + quota_cert_waived_time=None, + quota_cert_pending_fulfill=0, + cert_quota_rule_version="legacy", + cert_quota_arranged_at=datetime(2026, 11, 1, 10, 0, 0), + update_by=None, + plan_no="PO1", + plan_date=None, + license_plate=None, + supplier_name=None, + customer_name=None, + ) + user = SimpleNamespace(id="u1", tenant_no="shanxia2.0", name="齐备员", dept_id="") + changes = [] + + class Query: + def __init__(self, rows): + self.rows = rows + + def filter(self, *args, **kwargs): + return self + + def filter_by(self, **kwargs): + return self + + def all(self): + return list(self.rows) + + def first(self): + return self.rows[0] if self.rows else None + + class DB: + def query(self, model): + if model is PlPlanCertUsage: + return Query([]) + return Query([plan]) + + def flush(self): + return None + + monkeypatch.setattr("app.services.pl_quota_planning._plan", lambda *args, **kwargs: plan) + monkeypatch.setattr("app.services.pl_quota_waiver._toxic_exists", lambda *args, **kwargs: True) + monkeypatch.setattr("app.services.pl_quota_waiver._product_names", lambda *args, **kwargs: {}) + monkeypatch.setattr( + "app.services.pl_plan_service.record_plan_change", + lambda *args, **kwargs: changes.append(kwargs), + ) + monkeypatch.setattr( + "app.services.pl_cert_quota_rule_config_service.get_effective_at", + lambda *args, **kwargs: datetime(2026, 11, 15, 20, 0, 0), + ) + + mark_ready(DB(), user, "p1") + assert plan.quota_cert_waived == 1 + assert plan.quota_cert_pending_fulfill == 0 + assert changes and "不再校验" in (changes[-1].get("change_content") or "") + + +def test_stamp_arrange_backfills_pending_when_already_waived(monkeypatch): + """安排打成新规则时,若已齐备则回补待完善。""" + from datetime import datetime + + from app.services.pl_cert_quota_rule_config_service import ( + VERSION_CUSTOMER_CATEGORY, + stamp_plan_rule_on_arrange, + ) + + plan = SimpleNamespace( + cert_quota_arranged_at=None, + cert_quota_rule_version=None, + quota_cert_waived=1, + quota_cert_pending_fulfill=0, + ) + user = SimpleNamespace(tenant_no="shanxia2.0") + monkeypatch.setattr( + "app.services.pl_cert_quota_rule_config_service.resolve_rule_version_for_arrange", + lambda *args, **kwargs: VERSION_CUSTOMER_CATEGORY, + ) + version = stamp_plan_rule_on_arrange(None, user, plan, arrange_time=datetime(2026, 11, 16, 14, 0, 0)) + assert version == VERSION_CUSTOMER_CATEGORY + assert plan.quota_cert_pending_fulfill == 1 + + def test_waived_arrange_check_keeps_source_errors_and_skips_cert_balance(monkeypatch): from app.services.pl_quota_planning import contract_cert_arrange_block