- R-A2 sync-cash-plans: 年度行形态判定(_is_annual_budget_row)跳过 + skipped_annual 披露 - R2 POST /plans 期间形态校验(YYYY-MM / YYYY-00 显式接受) - R3 budget_roll_forward 排除年度行 - R1 cash_classify 归类路径年度行判据 + 披露 - 新增 2 个回归件(14+10 例) 仅含本族 hunk(禁整文件提交): 开发区其余 14 hunk 属他会话在途件,未纳入。
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
"""预算管理 API — 管理会计OS
|
||||
预算值的CRUD、自动分解、版本管理
|
||||
"""
|
||||
import re
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func, or_
|
||||
@@ -96,7 +98,19 @@ def create_budget_plan(
|
||||
if not kpi:
|
||||
raise HTTPException(404, "KPI不存在")
|
||||
|
||||
year, month = period.split("-")
|
||||
# 期间形态校验(年度行同族残余 2026-10-13:YYYY-00 显式接受为年度行,YYYY 提示补全形态)
|
||||
if not isinstance(period, str):
|
||||
raise HTTPException(400, "预算期间格式不正确:请使用 YYYY-MM(月 01–12)或 YYYY-00(年度)")
|
||||
if re.fullmatch(r"\d{4}-(0[1-9]|1[0-2])", period):
|
||||
# 月度形态
|
||||
year, month = period.split("-")[0], int(period.split("-")[1])
|
||||
elif re.fullmatch(r"\d{4}-00", period):
|
||||
# 年度形态(系统内部既有年度行,如 apply-method / 年度分解写入):显式接受
|
||||
year, month = period.split("-")[0], 0
|
||||
elif re.fullmatch(r"\d{4}", period):
|
||||
raise HTTPException(400, f"预算期间缺少月份:年度预算请使用 {period}-00 形态(例:2027-00),月度预算使用 YYYY-MM")
|
||||
else:
|
||||
raise HTTPException(400, "预算期间格式不正确:请使用 YYYY-MM(月 01–12)或 YYYY-00(年度)")
|
||||
version = data.get("version", "v1.0")
|
||||
map_id = data.get("map_id") # 预算归属地图(2026-08-27: 按地图隔离)
|
||||
|
||||
@@ -590,10 +604,14 @@ def budget_roll_forward(
|
||||
.filter(BudgetPlan.status == "active", KPIDefinition.entity_id == entity_id)
|
||||
.all())
|
||||
|
||||
# 按KPI分组
|
||||
# 按KPI分组(年度行排除 2026-10-13:budget_month=0 哨兵不参与月度滚动,仅年度行的 KPI 不删不产新行)
|
||||
from collections import defaultdict
|
||||
kpi_plans = defaultdict(list)
|
||||
annual_rows_ignored = 0
|
||||
for p in plans:
|
||||
if _is_annual_budget_row(p.period, p.budget_month):
|
||||
annual_rows_ignored += 1
|
||||
continue
|
||||
kpi_plans[p.kpi_id].append(p)
|
||||
|
||||
rolled_kpis = []
|
||||
@@ -653,6 +671,7 @@ def budget_roll_forward(
|
||||
"rolled_kpis": rolled_kpis,
|
||||
"current_month": f"{current_year}-{current_month:02d}",
|
||||
"rolling_months": val.get("rolling_months", 12),
|
||||
"annual_rows_ignored": annual_rows_ignored,
|
||||
}
|
||||
|
||||
|
||||
@@ -1631,6 +1650,17 @@ def diff_budget_versions(
|
||||
# 预算↔现金流联动(断点修复#1, 2026-08-27)
|
||||
# ════════════════════════════════════════════════════════════
|
||||
|
||||
def _is_annual_budget_row(period: Optional[str], budget_month: Optional[int]) -> bool:
|
||||
"""年度预算行判定(形态驱动):budget_month == 0(年度哨兵),
|
||||
或 period 命中年度形态 YYYY / YYYY-00。"""
|
||||
if budget_month == 0:
|
||||
return True
|
||||
if period is None:
|
||||
return False
|
||||
p = str(period).strip()
|
||||
return re.fullmatch(r"\d{4}(?:-00)?", p) is not None
|
||||
|
||||
|
||||
@router.post("/sync-cash-plans")
|
||||
def sync_cash_plans(
|
||||
entity_id: int = Depends(get_entity_id),
|
||||
@@ -1683,7 +1713,13 @@ def sync_cash_plans(
|
||||
kpis = {k.id: k for k in db.query(KPIDefinition).filter(KPIDefinition.id.in_(kpi_ids)).all()} if kpi_ids else {}
|
||||
|
||||
created, updated, unclassified_count = 0, 0, 0
|
||||
skipped_annual = 0
|
||||
for b in budgets:
|
||||
# 年度汇总行(budget_month=0 哨兵 / period=YYYY 或 YYYY-00)不联动现金流:
|
||||
# CashPlan 无年度维度, 跳过以免全年额回落成 YYYY-01 污染按月分桶
|
||||
if _is_annual_budget_row(b.period, b.budget_month):
|
||||
skipped_annual += 1
|
||||
continue
|
||||
kpi = kpis.get(b.kpi_id)
|
||||
if not kpi:
|
||||
continue
|
||||
@@ -1736,8 +1772,12 @@ def sync_cash_plans(
|
||||
))
|
||||
created += 1
|
||||
db.commit()
|
||||
message = f"现金流联动完成: 新建{created}条, 更新{updated}条, 待分类{unclassified_count}条"
|
||||
if skipped_annual:
|
||||
message += f", 跳过年度行{skipped_annual}条"
|
||||
return {
|
||||
"message": f"现金流联动完成: 新建{created}条, 更新{updated}条, 待分类{unclassified_count}条",
|
||||
"message": message,
|
||||
"created": created, "updated": updated,
|
||||
"unclassified_count": unclassified_count,
|
||||
"skipped_annual": skipped_annual,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user