Commit 41f63cbd by 周立明

负数不跳过

1 parent 988ba57b
# 2026-08-14 工作日志
## 6月 惠州鸿粤宏鑫 装修摊销差异排查
- 用户反馈 2026-06(Task #16)流量方「惠州鸿粤宏鑫汽车销售服务有限公司」装修摊销(code=4801.53) 与其手算不一致,怀疑一笔负数金额。
- 排查:装修费用明细 #206 中该门店共 4 条命中 F 池(store_name=惠州鸿粤宏鑫汽车销售服务有限公司):
- [26] +148500.0 /36 = 4125.00(验收 2025-11,在窗)
- [27] **-7505.2 /36 = -208.48(验收 2025-11)→ 负数**
- [28] +13693.0 /36 = 380.36(验收 2026-01,在窗)
- [29] +10662.0 /36 = 296.17(验收 2025-11,在窗)
- 代码行为:`compute_renovation_amounts` 第 405-407 行 `amt=(contract_amount)/36; if amt<=0: continue` —— **负数明细被整条跳过(既不减也不加)**,故 F 池只算 3 笔正数 = 4801.53,reconcile 后 id=50022 表内=4801.53。
- 差异根因确为负数明细:若手算把负数当作冲减(减去 208.48),得 4593.05;与 code 差 **208.48 = 7505.2/36**。验证:复算(含负但实际仍跳过负)=(仅正数)= 均 4801.53,与表内一致。
- 结论:差异 100% 来自负数明细的处理口径不同——代码"丢弃负数",用户手算"减去负数"。属业务口径待确认,未改代码。
- 修复方向(待用户拍板):若负数应作冲减,需改 `if amt<=0: continue` 为 `if amt==0: continue`(保留负贡献),并注意 `_allocate` 内 `if total<=0: continue` 对净负池会跳过分配——惠州鸿粤宏鑫 净池仍为正(4593.05)无碍,但通用场景需一并处理。
## 负数冲减修复已实施(用户拍板"当作冲减减去")
- 改动 `clean_budget_achievement.py`:
1. `compute_renovation_amounts` 第 405-407 行 `if amt <= 0: continue` → `if amt == 0: continue`(负数保留为负贡献,参与按 GMV 占比分摊)。
2. `_allocate` 第 426 行 `if total <= 0: continue` → `if total == 0: continue`(净负池也能分配成负摊销)。
3. docstring 补注"合同金额为负时作为冲减(负摊销)参与计算"。
- `distribute_by_weight` / `reconcile_total` 本就能处理负 total,无需改。
- 验证:修复代码复算 `id=50022`(旧id) → 4593.05(=4801.53−208.48),与用户手算一致。
- **坑**:首次重跑 Task #16 后库内仍是 4801.53 —— 因运行中加载了编辑前的旧 `.pyc` 缓存。清 `__pycache__` 后重跑,库内 `id=53242` 现为 **4593.05**,6月全表装修摊销为负的行数=0。结论:改 .py 后务必 `rm -rf __pycache__` 或重启进程,长运行 app.py/调度进程不热重载。
- 全局影响:该修复对所有月份/任务统一生效(任何负数合同金额今后均作冲减)。2026-06 仅惠州鸿粤宏鑫 一处受影响:4801.53→4593.05。
## 门店资产耗材 负数冲减修复(用户补充"门店资产耗材负数金额也需要当冲减扣掉")
- 此前仅装修摊销 `compute_renovation_amounts` 改了负数冲减;`compute_consumables_amounts`(门店资产耗材)仍保留旧逻辑 `if amt<=0: continue` / `_alloc` 内 `if total<=0: continue`,负数被整条丢弃。
- 实证:合成测试 G 池「正 360 + 负 −90」——修复前门店耗材得 360.00(负数丢失),装修摊销得 270.00(已正确);修复后门店耗材也得 **270.00**(负数作冲减)。
- 改动 `clean_budget_achievement.py` `compute_consumables_amounts`:
1. 第 687 行 `if amt <= 0: continue` → `if amt == 0: continue`(负数保留为负贡献,并入 F/G 池,按 GMV 占比摊成负摊消)。
2. `_alloc` 第 724 行 `if total <= 0: continue` → `if total == 0: continue`(净负池分配成负摊消)。
3. docstring 顶部补注"负数金额按冲减处理"。
- 验证:`py_compile` SYNTAX OK;合成测试门店耗材 G 池净 270.00 ✓。
- 数据影响:2026-06 门店耗材明细 1314 行中**负数 0 行**,故重跑 Task #16 门窗耗材值不变;该修复对将来含负数耗材金额的月份/任务统一生效。`distribute_by_weight`/`reconcile_total` 本就支持负 total,无需改。
- 仍提醒:改 .py 后需 `rm -rf __pycache__` 或重启 app.py 进程,否则长运行进程仍用旧字节码。
...@@ -343,6 +343,7 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV ...@@ -343,6 +343,7 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV
规则(用户提供,逐条明细定模式、避免重复计提): 规则(用户提供,逐条明细定模式、避免重复计提):
1. 仅 business_type='1.1' 的结果行参与(biz_rows 已按此过滤)。 1. 仅 business_type='1.1' 的结果行参与(biz_rows 已按此过滤)。
2. 基数 = 明细表「合同金额」(L) ÷ 摊销总月数(n_months); 2. 基数 = 明细表「合同金额」(L) ÷ 摊销总月数(n_months);
合同金额为负时作为冲减(负摊销)参与计算,不整条丢弃;
仅取明细中存在「实际验收日期」(T) 的记录,且 stat_month 落在 仅取明细中存在「实际验收日期」(T) 的记录,且 stat_month 落在
[验收次月, 验收+摊销总月数] 区间内才计提 [验收次月, 验收+摊销总月数] 区间内才计提
(用户明确规则:当前月份须 ∈ [验收+1月, 验收+36月])。 (用户明确规则:当前月份须 ∈ [验收+1月, 验收+36月])。
...@@ -403,7 +404,7 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV ...@@ -403,7 +404,7 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV
if not in_window(d['accept_date']): if not in_window(d['accept_date']):
continue continue
amt = (d['contract_amount'] or 0.0) / n_months amt = (d['contract_amount'] or 0.0) / n_months
if amt <= 0: if amt == 0:
continue continue
# 明细名先归一为标准名(别名归并),再 strip D+ 与预算行口径一致 # 明细名先归一为标准名(别名归并),再 strip D+ 与预算行口径一致
fn = strip_if_dplus(to_standard(d['store_name'], '4s')) fn = strip_if_dplus(to_standard(d['store_name'], '4s'))
...@@ -423,7 +424,7 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV ...@@ -423,7 +424,7 @@ def compute_renovation_amounts(detail_rows, biz_rows, stat_month, n_months=RENOV
def _allocate(pool, row_map, field='gmv'): def _allocate(pool, row_map, field='gmv'):
for key, total in pool.items(): for key, total in pool.items():
if total <= 0: if total == 0:
continue continue
rows = row_map[key] rows = row_map[key]
if field == 'mgv_ratio_park': if field == 'mgv_ratio_park':
...@@ -635,6 +636,9 @@ def query_all_biz_rows(conn, task_id, stat_month): ...@@ -635,6 +636,9 @@ def query_all_biz_rows(conn, task_id, stat_month):
def compute_consumables_amounts(detail_rows, biz_rows, stat_month): def compute_consumables_amounts(detail_rows, biz_rows, stat_month):
"""按规则计算每行预算达成数据的「门店资产耗材」金额。 """按规则计算每行预算达成数据的「门店资产耗材」金额。
负数金额按冲减处理:明细金额为负时保留为负贡献,并入对应 F/G 池,
最终按 GMV 占比摊成各行的负摊消(与装修摊销口径一致)。
逻辑与装修摊销高度相似,差异: 逻辑与装修摊销高度相似,差异:
- 金额直接作为分摊基数,不做除法(无摊销总月数); - 金额直接作为分摊基数,不做除法(无摊销总月数);
- 不限制业务类型:按明细自身的业务类型(归一化后)分别匹配对应业务的预算行; - 不限制业务类型:按明细自身的业务类型(归一化后)分别匹配对应业务的预算行;
...@@ -683,8 +687,8 @@ def compute_consumables_amounts(detail_rows, biz_rows, stat_month): ...@@ -683,8 +687,8 @@ def compute_consumables_amounts(detail_rows, biz_rows, stat_month):
if d.get('month') != month_label: if d.get('month') != month_label:
continue continue
amt = float(d['amount'] or 0) amt = float(d['amount'] or 0)
if amt <= 0: if amt == 0:
continue continue # 负数金额保留为负贡献,按园区店/流量方 GMV 占比作冲减扣减
bt = normalize_bt(d['business_type']) bt = normalize_bt(d['business_type'])
fmap = bt_f.get(bt, {}) fmap = bt_f.get(bt, {})
gmap = bt_g.get(bt, {}) gmap = bt_g.get(bt, {})
...@@ -720,8 +724,8 @@ def compute_consumables_amounts(detail_rows, biz_rows, stat_month): ...@@ -720,8 +724,8 @@ def compute_consumables_amounts(detail_rows, biz_rows, stat_month):
def _alloc(pool, row_map, field='gmv'): def _alloc(pool, row_map, field='gmv'):
for key, total in pool.items(): for key, total in pool.items():
if total <= 0: if total == 0:
continue continue # 净额为 0 才跳过;净负池同样分配成负摊消(冲减)
rows = row_map[key] rows = row_map[key]
if field == 'mgv_ratio_park': if field == 'mgv_ratio_park':
ratios = [float(r['gmv_ratio_park']) if r.get('gmv_ratio_park') is not None else None ratios = [float(r['gmv_ratio_park']) if r.get('gmv_ratio_park') is not None else None
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!