Commit 77c29fc6 by zhouhaibao

fix(renovation): 修复装修摊分悬空金额及合并单元格继承问题

- 移除解析器中合并单元格向上继承逻辑,避免错误归属门店
- 调整跳过条件为门店名、园区店、合同金额全空才跳过
- 新增池内全非嘀加跳过行的悬空金额累计机制
- 将悬空金额并入 unmatched_amount 确保对账完整
- 补充悬空金额并入未匹配的日志记录
1 parent d0dfc667
...@@ -430,8 +430,11 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV ...@@ -430,8 +430,11 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV
# - G 池(按系统园区店名称/park_store 匹配):用 mgv_ratio_park(按业务类型+园区店) # - G 池(按系统园区店名称/park_store 匹配):用 mgv_ratio_park(按业务类型+园区店)
# 作为该行的摊分系数,更直接体现「同业务类型+同园区店」的 GMV 占比。 # 作为该行的摊分系数,更直接体现「同业务类型+同园区店」的 GMV 占比。
result = {} result = {}
# 用于 _allocate 内部把"全非嘀加被跳过"的悬空金额转进 unmatched_amount
_unmatched_skipped = Decimal('0')
def _allocate(pool, row_map, field='gmv'): def _allocate(pool, row_map, field='gmv'):
nonlocal _unmatched_skipped
for key, total in pool.items(): for key, total in pool.items():
if total == 0: if total == 0:
continue continue
...@@ -445,8 +448,9 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV ...@@ -445,8 +448,9 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV
logger.info(f"[装修摊销] key={key}: 仅 1 行且非嘀加 → 兜底全部分摊给该行") logger.info(f"[装修摊销] key={key}: 仅 1 行且非嘀加 → 兜底全部分摊给该行")
dijia_rows = rows dijia_rows = rows
else: else:
# 多行全非嘀加 → 不分摊(费用=0),跳过(差值进 unmatched_amount) # 多行全非嘀加 → 不分摊(费用=0),跳过 → 悬空金额进未匹配
logger.info(f"[装修摊销] key={key}: {len(rows)} 行全非嘀加 → 跳过(进未匹配)") logger.info(f"[装修摊销] key={key}: {len(rows)} 行全非嘀加 → 跳过(进未匹配)")
_unmatched_skipped += Decimal(str(total))
continue continue
if len(dijia_rows) < len(rows): if len(dijia_rows) < len(rows):
logger.info(f"[装修摊销] key={key}: {len(rows)} 行中有 {len(dijia_rows)} 行" logger.info(f"[装修摊销] key={key}: {len(rows)} 行中有 {len(dijia_rows)} 行"
...@@ -487,6 +491,10 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV ...@@ -487,6 +491,10 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV
_allocate(f_pool, f_rows, field='gmv') _allocate(f_pool, f_rows, field='gmv')
_allocate(g_pool, g_rows, field='mgv_ratio_park') _allocate(g_pool, g_rows, field='mgv_ratio_park')
# 把池内"全非嘀加被跳过"的悬空金额并入未匹配
if _unmatched_skipped:
unmatched_amount += _unmatched_skipped
logger.info(f"[装修摊销] 池内全非嘀加跳过的悬空金额={float(_unmatched_skipped):.2f} → 并入未匹配")
# 全局对账:各池分摊合计对齐到「匹配明细金额总和(分取整)」,消除跨池四舍五入残差 # 全局对账:各池分摊合计对齐到「匹配明细金额总和(分取整)」,消除跨池四舍五入残差
reconcile_total(result, round(sum(f_pool.values()) + sum(g_pool.values()), 2)) reconcile_total(result, round(sum(f_pool.values()) + sum(g_pool.values()), 2))
# 记录未匹配金额到返回 dict 的特殊 key # 记录未匹配金额到返回 dict 的特殊 key
......
...@@ -85,10 +85,6 @@ class RenovationDetailParser: ...@@ -85,10 +85,6 @@ class RenovationDetailParser:
try: try:
ws = wb[wb.sheetnames[0]] ws = wb[wb.sheetnames[0]]
details = [] details = []
# Excel 合并单元格:openpyxl 只有合并区域左上角有值,后续行返回 None。
# 这里缓存上一行的值,续行向上继承,避免合并区域内有合同金额的行被跳过。
last_store_name = ''
last_park_store = ''
for row in ws.iter_rows(min_row=DATA_ROW_START, max_col=MAX_COL, values_only=True): for row in ws.iter_rows(min_row=DATA_ROW_START, max_col=MAX_COL, values_only=True):
def cell(col): def cell(col):
idx = col - 1 idx = col - 1
...@@ -99,22 +95,15 @@ class RenovationDetailParser: ...@@ -99,22 +95,15 @@ class RenovationDetailParser:
contract_amount = _to_num(cell(COL_CONTRACT)) contract_amount = _to_num(cell(COL_CONTRACT))
accept_date = cell(COL_ACCEPT_DATE) accept_date = cell(COL_ACCEPT_DATE)
# 合并单元格续行:门店名称/园区店为空 → 继承上一行 # Excel 合并单元格:openpyxl 只有合并区域左上角有值,后续行返回 None。
if not store_name and last_store_name: # 修复前跳过条件:if not store_name and not park_store → 把合并续行直接 skip
store_name = last_store_name # 修复后:只有门店名、园区店、合同金额全空才跳过(空行/合计行)。
if not park_store and last_park_store: # 合并续行(门店名=空、园区店=空但合同金额有值)会带着 store_name=''、
park_store = last_park_store # park_store='' 进入 compute_renovation_amounts → fn='', gn='' → 直接进 unmatched。
# 注意:**不做向上继承**——合并续行的门店名本来就是空的,不能归属到上一个门店名下。
# 跳过条件:门店名称/园区店都空 **且** 合同金额也为 0 → 空行/合计行
if not store_name and not park_store and contract_amount == 0: if not store_name and not park_store and contract_amount == 0:
continue continue
# 缓存(空值不覆盖)
if store_name:
last_store_name = store_name
if park_store:
last_park_store = park_store
details.append({ details.append({
'store_name': store_name, 'store_name': store_name,
'park_store_name': park_store, 'park_store_name': park_store,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!