Commit 3dfa6639 by zhouhaibao

feat(data-process): 新增轻改财务成本分项统计功能

- 在 light_reform_inshop_processor.py 中引入 fetch_light_reform_finance_stats 函数
- 在 light_reform_park_processor.py 中集成财务成本分项查询功能
- 新增 10 个财务成本字段包括产品成本、内部结算成本、原厂/副厂成本等
- 重构 report_utils.py 中的财务统计数据查询逻辑
- 优化 SQL 查询性能,使用单次聚合查询替代多次数据库访问
- 实现店中店和园区店共用财务成本统计,支持按订单类型和品类切分
1 parent 97f07f16
...@@ -25,7 +25,7 @@ from report_utils import ( ...@@ -25,7 +25,7 @@ from report_utils import (
parse_inshop_delivery_touch, parse_commerce_policy_inshop, parse_inshop_delivery_touch, parse_commerce_policy_inshop,
parse_labor_cost, is_luxury_brand, parse_labor_cost, is_luxury_brand,
parse_budget_achieve_excel, extract_budget_ratio, prorate_by_period, parse_budget_achieve_excel, extract_budget_ratio, prorate_by_period,
fetch_park_daily_report, fetch_light_reform_goods_stats, fuzzy_match_store, fetch_park_daily_report, fetch_light_reform_goods_stats, fetch_light_reform_finance_stats, fuzzy_match_store,
get_with_fallback, safe_div, safe_div_pct, safe_div_pct_decimal, get_with_fallback, safe_div, safe_div_pct, safe_div_pct_decimal,
) )
from models import ReportLightReformInshop, db from models import ReportLightReformInshop, db
...@@ -158,14 +158,15 @@ class LightReformInshopProcessor: ...@@ -158,14 +158,15 @@ class LightReformInshopProcessor:
daily_report_map = fetch_park_daily_report(period_start, period_end, scene_type=1) daily_report_map = fetch_park_daily_report(period_start, period_end, scene_type=1)
# 2c. 品类成交数 + 产值(店中店+园区店共用 SQL,按 dj_order_type + AITO/HIMA 关键字聚合) # 2c. 品类成交数 + 产值(店中店+园区店共用 SQL,按 dj_order_type + AITO/HIMA 关键字聚合)
goods_stats_map = fetch_light_reform_goods_stats(period_start, period_end) goods_stats_map = fetch_light_reform_goods_stats(period_start, period_end)
# 2d. 财务成本分项(店中店+园区店共用 SQL,按 dj_order_type + 品类 + 原厂/副厂切分)
finance_map = fetch_light_reform_finance_stats(period_start, period_end, scene_type=1)
# 2b. 模糊匹配到门店 → 组装 sql_data dict(get_with_fallback 双优先用) # 2b. 模糊匹配到门店 → 组装 sql_data dict(get_with_fallback 双优先用)
# 店中店:delivery_cnt→交车;equity→交付触客;after_sale→售后触客;
# 售后首保/事故回厂数 → 无 SQL,默认 0
biz_sql_data = {} biz_sql_data = {}
for rec in base_records: for rec in base_records:
sn = rec.get('store_name', '') sn = rec.get('store_name', '')
dr = fuzzy_match_store(sn, daily_report_map) or {} dr = fuzzy_match_store(sn, daily_report_map) or {}
gs = fuzzy_match_store(sn, goods_stats_map) or {} gs = fuzzy_match_store(sn, goods_stats_map) or {}
fs = fuzzy_match_store(sn, finance_map) or {}
biz_sql_data[sn] = { biz_sql_data[sn] = {
# —— 日报类(来自 fetch_park_daily_report)—— # —— 日报类(来自 fetch_park_daily_report)——
'delivery_cnt': dr.get('delivery_cnt', 0), 'delivery_cnt': dr.get('delivery_cnt', 0),
...@@ -175,7 +176,7 @@ class LightReformInshopProcessor: ...@@ -175,7 +176,7 @@ class LightReformInshopProcessor:
'aftersales_shigu_cnt': 0, # 无 SQL 'aftersales_shigu_cnt': 0, # 无 SQL
'sales_order_cnt': 0, # 无 SQL(销售大定数) 'sales_order_cnt': 0, # 无 SQL(销售大定数)
'sales_touch_cnt': 0, # 无 SQL(销售触客数) 'sales_touch_cnt': 0, # 无 SQL(销售触客数)
# —— 品类成交数 + 产值(来自 fetch_inshop_goods_stats)—— # —— 品类成交数 + 产值(来自 fetch_light_reform_goods_stats)——
'total_deal_cnt': gs.get('total_deal_cnt', 0), 'total_deal_cnt': gs.get('total_deal_cnt', 0),
'window_film_oe_cnt': gs.get('window_film_oe_cnt', 0), 'window_film_oe_cnt': gs.get('window_film_oe_cnt', 0),
'window_film_nonoe_cnt': gs.get('window_film_nonoe_cnt', 0), 'window_film_nonoe_cnt': gs.get('window_film_nonoe_cnt', 0),
...@@ -188,6 +189,17 @@ class LightReformInshopProcessor: ...@@ -188,6 +189,17 @@ class LightReformInshopProcessor:
'internal_settle_value': gs.get('internal_settle_value', Decimal('0')), 'internal_settle_value': gs.get('internal_settle_value', Decimal('0')),
'oe_output_value': gs.get('oe_output_value', Decimal('0')), 'oe_output_value': gs.get('oe_output_value', Decimal('0')),
'nonoe_output_value': gs.get('nonoe_output_value', Decimal('0')), 'nonoe_output_value': gs.get('nonoe_output_value', Decimal('0')),
# —— 财务成本分项(来自 fetch_light_reform_finance_stats)——
'product_cost_finance': fs.get('product_cost_finance', Decimal('0')),
'internal_settle_cost': fs.get('internal_settle_cost', Decimal('0')),
'oe_product_cost': fs.get('oe_product_cost', Decimal('0')),
'nonoe_product_cost': fs.get('nonoe_product_cost', Decimal('0')),
'window_film_cost': fs.get('window_film_cost', Decimal('0')),
'car_wrap_cost': fs.get('car_wrap_cost', Decimal('0')),
'premium_cost': fs.get('premium_cost', Decimal('0')),
'store_share': fs.get('store_share', Decimal('0')),
'construction_income': fs.get('construction_income', Decimal('0')),
'market_fee': fs.get('market_fee', Decimal('0')),
} }
# ========== Step3: 上月预算明细(已在 Step1 从 Excel 解析) ========== # ========== Step3: 上月预算明细(已在 Step1 从 Excel 解析) ==========
......
...@@ -32,7 +32,7 @@ from report_utils import ( ...@@ -32,7 +32,7 @@ from report_utils import (
parse_labor_cost, is_luxury_brand, parse_labor_cost, is_luxury_brand,
parse_budget_achieve_excel, extract_budget_ratio, prorate_by_period, parse_budget_achieve_excel, extract_budget_ratio, prorate_by_period,
fetch_indicator_data, fetch_park_daily_report, fetch_light_reform_goods_stats, fetch_indicator_data, fetch_park_daily_report, fetch_light_reform_goods_stats,
fetch_est_delivery_count, fuzzy_match_store, fetch_light_reform_finance_stats, fetch_est_delivery_count, fuzzy_match_store,
get_with_fallback, safe_div, safe_div_pct, safe_div_pct_decimal, get_with_fallback, safe_div, safe_div_pct, safe_div_pct_decimal,
) )
from models import ReportLightReformPark, db from models import ReportLightReformPark, db
...@@ -166,13 +166,15 @@ class LightReformParkProcessor: ...@@ -166,13 +166,15 @@ class LightReformParkProcessor:
goods_stats_map = fetch_light_reform_goods_stats(period_start, period_end) goods_stats_map = fetch_light_reform_goods_stats(period_start, period_end)
# 3c. 预估交车数(静态配置表,无日期参数,traffic_party_name 即门店名) # 3c. 预估交车数(静态配置表,无日期参数,traffic_party_name 即门店名)
est_delivery_map = fetch_est_delivery_count(base_scene_code='100101') est_delivery_map = fetch_est_delivery_count(base_scene_code='100101')
# 3d. 模糊匹配到门店 → 组装 sql_data dict # 3d. 财务成本分项(店中店+园区店共用 SQL,park 侧仅消费 4 项)
# 园区店:delivery_cnt → 交车;equity + after_sale → 到店数;无效到店无 SQL → 0 finance_map = fetch_light_reform_finance_stats(period_start, period_end, scene_type=1)
# 3e. 模糊匹配到门店 → 组装 sql_data dict
biz_sql_data = {} biz_sql_data = {}
for rec in base_records: for rec in base_records:
sn = rec.get('store_name', '') sn = rec.get('store_name', '')
dr = fuzzy_match_store(sn, daily_report_map) or {} dr = fuzzy_match_store(sn, daily_report_map) or {}
gs = fuzzy_match_store(sn, goods_stats_map) or {} gs = fuzzy_match_store(sn, goods_stats_map) or {}
fs = fuzzy_match_store(sn, finance_map) or {}
eq = dr.get('equity_to_shop_count', 0) eq = dr.get('equity_to_shop_count', 0)
af = dr.get('after_sale_equity_to_shop_count', 0) af = dr.get('after_sale_equity_to_shop_count', 0)
biz_sql_data[sn] = { biz_sql_data[sn] = {
...@@ -188,10 +190,15 @@ class LightReformParkProcessor: ...@@ -188,10 +190,15 @@ class LightReformParkProcessor:
# —— 产值(来自 fetch_light_reform_goods_stats)—— # —— 产值(来自 fetch_light_reform_goods_stats)——
'store_sales_output': gs.get('store_output_value', Decimal('0')), 'store_sales_output': gs.get('store_output_value', Decimal('0')),
'internal_settle_value': gs.get('internal_settle_value', Decimal('0')), 'internal_settle_value': gs.get('internal_settle_value', Decimal('0')),
# —— 预估交车数(来自 fetch_est_delivery_count,traffic_party_name 即门店名)—— # —— 预估交车数(来自 fetch_est_delivery_count)——
'est_delivery_cnt': fuzzy_match_store(sn, est_delivery_map) or 0, 'est_delivery_cnt': fuzzy_match_store(sn, est_delivery_map) or 0,
# —— 财务分项(park 仅消费产品成本/内部结算成本/流量费/市场费)——
'product_cost_finance': fs.get('product_cost_finance', Decimal('0')),
'internal_settle_cost': fs.get('internal_settle_cost', Decimal('0')),
'traffic_fee_finance': fs.get('store_share', Decimal('0')), # 流量费 = gopa.shop_traffic_money (dj_order_type!=6)
'market_fee_weighted': fs.get('market_fee', Decimal('0')), # 市场费加权测算 = fee 四项费用累计
} }
# 3e. 其他聚合 SQL(成本/市场费/流量费等)后续接入,当前保持默认 0 # 3f. 其他字段(service_fee / construction_cost 等) park 从 Excel 或公式取,保持 0
# ========== Step4: 上月预算明细(已在 Step1 从 Excel 解析) ========== # ========== Step4: 上月预算明细(已在 Step1 从 Excel 解析) ==========
prev_budget_map = budget_achieve_map prev_budget_map = budget_achieve_map
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!