Commit 9abd5db8 by zhouhaibao

refactor(data-process): 重构数据处理模块以支持业务类型过滤和预算比例提取

- 在 parse_budget_achieve_excel 函数中添加 biz_type 参数以支持业务类型过滤
- 新增 extract_budget_ratio 函数用于安全提取预算比例数据
- 新增 prorate_by_period 函数用于按周期摊销月度固定费用
- 为多个解析函数添加 biz_type 参数和业务类型过滤功能
- 统一多个 Excel 解析函数的聚合逻辑,支持同门店同业务类型多行数据处理
- 重构轻改店中店和园区店处理器中的预算数据处理逻辑
- 替换原有的 fetch_prev_month_budget_detail 调用为 Excel 解析方式
- 优化人力成本表解析逻辑,支持动态业务类型过滤
1 parent 45b68eba
...@@ -24,7 +24,7 @@ from report_utils import ( ...@@ -24,7 +24,7 @@ from report_utils import (
SUBTASK_PROCESSING, SUBTASK_COMPLETED, SUBTASK_FAILED, SUBTASK_PROCESSING, SUBTASK_COMPLETED, SUBTASK_FAILED,
download_all_files, download_all_files,
parse_saas_base, parse_region_map, parse_delivery_table, parse_saas_base, parse_region_map, parse_delivery_table,
parse_budget_achieve_excel, parse_budget_achieve_excel, extract_budget_ratio,
fetch_indicator_data, fetch_indicator_data,
safe_div, safe_div_pct, safe_div_pct_decimal, safe_div, safe_div_pct, safe_div_pct_decimal,
) )
...@@ -146,7 +146,7 @@ class WarrantyProcessor: ...@@ -146,7 +146,7 @@ class WarrantyProcessor:
budget_path = file_map.get('预算达成数据(商务)') or file_map.get('预算达成数据(商务)') budget_path = file_map.get('预算达成数据(商务)') or file_map.get('预算达成数据(商务)')
prev_labor_ratio_map = {} prev_labor_ratio_map = {}
if budget_path: if budget_path:
prev_labor_ratio_map = parse_budget_achieve_excel(budget_path) prev_labor_ratio_map = parse_budget_achieve_excel(budget_path, biz_type='2.3')
finally: finally:
cleanup_tmp_dir(tmp_dir) cleanup_tmp_dir(tmp_dir)
...@@ -198,7 +198,7 @@ class WarrantyProcessor: ...@@ -198,7 +198,7 @@ class WarrantyProcessor:
gl_share = store_output_value - product_cost - store_share - guarantee_amount gl_share = store_output_value - product_cost - store_share - guarantee_amount
market_fee_ratio = safe_div_pct_decimal(market_fee, store_output_value) market_fee_ratio = safe_div_pct_decimal(market_fee, store_output_value)
marketing_fee_ratio = prev_labor_ratio_map.get(store_name, Decimal('0')) marketing_fee_ratio = extract_budget_ratio(prev_labor_ratio_map, store_name, 'labor_ratio')
marketing_fee = (store_output_value * marketing_fee_ratio).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) marketing_fee = (store_output_value * marketing_fee_ratio).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
service_fee_ratio = safe_div_pct_decimal(service_fee, store_output_value) service_fee_ratio = safe_div_pct_decimal(service_fee, store_output_value)
......
...@@ -24,7 +24,7 @@ from report_utils import ( ...@@ -24,7 +24,7 @@ from report_utils import (
SUBTASK_PROCESSING, SUBTASK_COMPLETED, SUBTASK_FAILED, SUBTASK_PROCESSING, SUBTASK_COMPLETED, SUBTASK_FAILED,
download_all_files, download_all_files,
parse_saas_base, parse_region_map, parse_delivery_table, parse_saas_base, parse_region_map, parse_delivery_table,
parse_budget_achieve_excel, parse_budget_achieve_excel, extract_budget_ratio,
fetch_indicator_data, fetch_indicator_data,
safe_div, safe_div_pct, safe_div_pct_decimal, safe_div, safe_div_pct, safe_div_pct_decimal,
) )
...@@ -148,7 +148,7 @@ class EquityProcessor: ...@@ -148,7 +148,7 @@ class EquityProcessor:
budget_path = file_map.get('预算达成数据(商务)') or file_map.get('预算达成数据(商务)') budget_path = file_map.get('预算达成数据(商务)') or file_map.get('预算达成数据(商务)')
prev_labor_ratio_map = {} prev_labor_ratio_map = {}
if budget_path: if budget_path:
prev_labor_ratio_map = parse_budget_achieve_excel(budget_path) prev_labor_ratio_map = parse_budget_achieve_excel(budget_path, biz_type='2.1')
finally: finally:
cleanup_tmp_dir(tmp_dir) cleanup_tmp_dir(tmp_dir)
...@@ -193,7 +193,7 @@ class EquityProcessor: ...@@ -193,7 +193,7 @@ class EquityProcessor:
gl_share = store_output_value - product_cost - store_share - guarantee_amount gl_share = store_output_value - product_cost - store_share - guarantee_amount
market_fee_ratio = safe_div_pct_decimal(market_fee, store_output_value) market_fee_ratio = safe_div_pct_decimal(market_fee, store_output_value)
marketing_fee_ratio = prev_labor_ratio_map.get(store_name, Decimal('0')) marketing_fee_ratio = extract_budget_ratio(prev_labor_ratio_map, store_name, 'labor_ratio')
marketing_fee = (store_output_value * marketing_fee_ratio).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) marketing_fee = (store_output_value * marketing_fee_ratio).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
service_fee_ratio = safe_div_pct_decimal(service_fee, store_output_value) service_fee_ratio = safe_div_pct_decimal(service_fee, store_output_value)
......
...@@ -22,7 +22,7 @@ from report_utils import ( ...@@ -22,7 +22,7 @@ from report_utils import (
parse_saas_base_sheet2, parse_region_map, parse_saas_base_sheet2, parse_region_map,
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,
fetch_prev_month_budget_detail, parse_budget_achieve_excel, extract_budget_ratio, prorate_by_period,
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
...@@ -127,32 +127,35 @@ class LightReformInshopProcessor: ...@@ -127,32 +127,35 @@ class LightReformInshopProcessor:
delivery_touch_map = {} delivery_touch_map = {}
dt_path = file_map.get('店中店交车触客表') dt_path = file_map.get('店中店交车触客表')
if dt_path: if dt_path:
delivery_touch_map = parse_inshop_delivery_touch(dt_path) delivery_touch_map = parse_inshop_delivery_touch(dt_path, biz_type='1.1')
# 商务政策表(店中店处理版) # 商务政策表(店中店处理版)
commerce_map = {} commerce_map = {}
cp_path = file_map.get('商务政策表(店中店)') or file_map.get('商务政策表(店中店)') cp_path = file_map.get('商务政策表(店中店)') or file_map.get('商务政策表(店中店)')
if cp_path: if cp_path:
commerce_map = parse_commerce_policy_inshop(cp_path) commerce_map = parse_commerce_policy_inshop(cp_path, biz_type='1.1')
# 人力成本表 # 人力成本表
labor_map = {} labor_map = {}
lc_path = file_map.get('人力成本表') lc_path = file_map.get('人力成本表')
if lc_path: if lc_path:
labor_map = parse_labor_cost(lc_path) labor_map = parse_labor_cost(lc_path, biz_type='1.1')
# 预算达成数据(商务)→ 装修分摊/房租水电/门店耗材/服务费占比
budget_achieve_map = {}
budget_path = file_map.get('预算达成数据(商务)') or file_map.get('预算达成数据(商务)')
if budget_path:
budget_achieve_map = parse_budget_achieve_excel(budget_path, biz_type='1.1')
finally: finally:
cleanup_tmp_dir(tmp_dir) cleanup_tmp_dir(tmp_dir)
# ========== Step2: 业务系统 SQL(TODO 占位) ========== # ========== Step2: 业务系统 SQL(TODO 占位) ==========
# 轻改店中店完全不调指标系统,所有 SQL 来源字段统一走 biz_sql_data
# 包含:成交数/产值、品类成交数、各类产值/成本、门店分成、市场费、施工收入等
biz_sql_data = {} biz_sql_data = {}
logger.warning("[轻改店中店] Step2 业务系统 SQL 暂未实现,相关字段默认 0") logger.warning("[轻改店中店] Step2 业务系统 SQL 暂未实现")
# ========== Step4: 上月预算明细 ========== # ========== Step3: 上月预算明细(已在 Step1 从 Excel 解析) ==========
store_names = list({r.get('store_name', '') for r in base_records if r.get('store_name')}) prev_budget_map = budget_achieve_map
prev_budget_map = fetch_prev_month_budget_detail(task, store_names)
# ========== Step5: 逐行计算 ========== # ========== Step5: 逐行计算 ==========
rows_to_insert = [] rows_to_insert = []
...@@ -226,12 +229,19 @@ class LightReformInshopProcessor: ...@@ -226,12 +229,19 @@ class LightReformInshopProcessor:
construction_cost_post = Decimal(str(cp.get('construction_cost_post', 0))) construction_cost_post = Decimal(str(cp.get('construction_cost_post', 0)))
frontline_ratio_cp = Decimal(str(cp.get('frontline_incentive_ratio', 0))) frontline_ratio_cp = Decimal(str(cp.get('frontline_incentive_ratio', 0)))
# —— 上月预算明细 —— # —— 预算达成 Excel(按周期天数摊销月度固定费用) ——
pb = prev_budget_map.get(sn, {}) # 装修分摊 / 房租水电 / 门店耗材:按"周期天数/月份总天数"摊销
renovation_amortization = pb.get('renovation_amortization', Decimal('0')) renovation_amortization = prorate_by_period(
rent_utilities = pb.get('rent_utilities', Decimal('0')) extract_budget_ratio(prev_budget_map, sn, 'renovation_amortization'),
store_consumables = pb.get('store_consumables', Decimal('0')) period_start, period_end)
sp_ratio = pb.get('service_provider_share_ratio', Decimal('0')) rent_utilities = prorate_by_period(
extract_budget_ratio(prev_budget_map, sn, 'rent_utilities'),
period_start, period_end)
store_consumables = prorate_by_period(
extract_budget_ratio(prev_budget_map, sn, 'store_consumables'),
period_start, period_end)
# 服务费占比:直接取(比例值不摊销)
sp_ratio = extract_budget_ratio(prev_budget_map, sn, 'service_provider_share_ratio')
# —— 人力成本 —— # —— 人力成本 ——
headcount = labor_map.get(sn, 0) headcount = labor_map.get(sn, 0)
......
...@@ -25,7 +25,8 @@ from report_utils import ( ...@@ -25,7 +25,8 @@ from report_utils import (
parse_saas_base_sheet2_exclude, parse_region_map, parse_saas_base_sheet2_exclude, parse_region_map,
parse_park_delivery_touch, parse_commerce_policy_park, parse_park_delivery_touch, parse_commerce_policy_park,
parse_labor_cost, is_luxury_brand, parse_labor_cost, is_luxury_brand,
fetch_prev_month_budget_detail, fetch_indicator_data, parse_budget_achieve_excel, extract_budget_ratio, prorate_by_period,
fetch_indicator_data,
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
...@@ -124,36 +125,40 @@ class LightReformParkProcessor: ...@@ -124,36 +125,40 @@ class LightReformParkProcessor:
delivery_touch_map = {} delivery_touch_map = {}
dt_path = file_map.get('园区店交车触客表') dt_path = file_map.get('园区店交车触客表')
if dt_path: if dt_path:
delivery_touch_map = parse_park_delivery_touch(dt_path) delivery_touch_map = parse_park_delivery_touch(dt_path, biz_type='1.1')
# 商务政策表(园区店处理版) # 商务政策表(园区店处理版)
commerce_map = {} commerce_map = {}
cp_path = file_map.get('商务政策表(园区店)') or file_map.get('商务政策表(园区店)') cp_path = file_map.get('商务政策表(园区店)') or file_map.get('商务政策表(园区店)')
if cp_path: if cp_path:
commerce_map = parse_commerce_policy_park(cp_path) commerce_map = parse_commerce_policy_park(cp_path, biz_type='1.1')
# 人力成本表 # 人力成本表
labor_map = {} labor_map = {}
lc_path = file_map.get('人力成本表') lc_path = file_map.get('人力成本表')
if lc_path: if lc_path:
labor_map = parse_labor_cost(lc_path) labor_map = parse_labor_cost(lc_path, biz_type='1.1')
# 预算达成数据(商务)→ 装修分摊/房租水电/门店耗材/服务费占比
budget_achieve_map = {}
budget_path = file_map.get('预算达成数据(商务)') or file_map.get('预算达成数据(商务)')
if budget_path:
budget_achieve_map = parse_budget_achieve_excel(budget_path, biz_type='1.1')
finally: finally:
cleanup_tmp_dir(tmp_dir) cleanup_tmp_dir(tmp_dir)
# ========== Step2: 指标系统 biz=2 轻改 ========== # ========== Step2: 指标系统 biz=2 轻改 ==========
park_codes = ['A075'] # A075=成交数(园区店唯一指标) park_codes = ['A075'] # A075=成交数
indicator_data = fetch_indicator_data( indicator_data = fetch_indicator_data(
start_str, end_str, INDICATOR_BIZ_MAP.get('light_reform', 2), park_codes) start_str, end_str, INDICATOR_BIZ_MAP.get('light_reform', 2), park_codes)
# ========== Step3: 业务系统 SQL(TODO 占位) ========== # ========== Step3: 业务系统 SQL(TODO 占位) ==========
# 窗膜/车衣/精品成交数、产值、产品成本、流量费、市场费、预估流量等
biz_sql_data = {} biz_sql_data = {}
logger.warning("[轻改园区店] Step3 业务系统 SQL 暂未实现,相关字段默认 0") logger.warning("[轻改园区店] Step3 业务系统 SQL 暂未实现")
# ========== Step4: 上月预算明细 ========== # ========== Step4: 上月预算明细(已在 Step1 从 Excel 解析) ==========
store_names = list({r.get('store_name', '') for r in base_records if r.get('store_name')}) prev_budget_map = budget_achieve_map
prev_budget_map = fetch_prev_month_budget_detail(task, store_names)
# ========== Step5: 合作门店数量(按 park_name 聚合) ========== # ========== Step5: 合作门店数量(按 park_name 聚合) ==========
from collections import Counter from collections import Counter
...@@ -213,12 +218,19 @@ class LightReformParkProcessor: ...@@ -213,12 +218,19 @@ class LightReformParkProcessor:
est_delivery_cnt = rec.get('est_delivery_cnt', 0) est_delivery_cnt = rec.get('est_delivery_cnt', 0)
est_output_value_raw = rec.get('est_output_value', Decimal('0')) est_output_value_raw = rec.get('est_output_value', Decimal('0'))
# —— 上月预算明细 —— # —— 预算达成 Excel(按周期天数摊销月度固定费用) ——
pb = prev_budget_map.get(sn, {}) renovation_amortization = prorate_by_period(
renovation_amortization = pb.get('renovation_amortization', Decimal('0')) extract_budget_ratio(prev_budget_map, sn, 'renovation_amortization'),
rent_utilities = pb.get('rent_utilities', Decimal('0')) period_start, period_end)
store_consumables = pb.get('store_consumables', Decimal('0')) rent_utilities = prorate_by_period(
sp_ratio = pb.get('service_provider_share_ratio', Decimal('0')) extract_budget_ratio(prev_budget_map, sn, 'rent_utilities'),
period_start, period_end)
store_consumables = prorate_by_period(
extract_budget_ratio(prev_budget_map, sn, 'store_consumables'),
period_start, period_end)
# 服务费占比:直营店强制 0,服务商直接取
sp_ratio = Decimal('0') if operate_type == '直营' else extract_budget_ratio(
prev_budget_map, sn, 'service_provider_share_ratio')
# ======= 公式 ======= # ======= 公式 =======
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!