feat(xq0085): 团购核对支持对比表与门店归属(v1.1.0)
This commit is contained in:
@@ -12,7 +12,6 @@
|
||||
|
||||
与美团侧一致:openpyxl 读值、zip+XML 写值(保留透视/公式),共享盘全程只读。
|
||||
"""
|
||||
import glob
|
||||
import os
|
||||
import re
|
||||
from datetime import date, datetime, timedelta
|
||||
@@ -22,7 +21,7 @@ from openpyxl.utils import column_index_from_string as col
|
||||
from openpyxl.utils import get_column_letter as letter
|
||||
|
||||
from .. import config
|
||||
from . import com_refresh, share_copy
|
||||
from . import base_info_sync, com_refresh, share_copy
|
||||
|
||||
|
||||
def _as_date(value):
|
||||
@@ -43,9 +42,17 @@ def _as_date(value):
|
||||
def _to_number(value):
|
||||
if value is None or value == '':
|
||||
return 0.0
|
||||
if isinstance(value, bool):
|
||||
return 0.0
|
||||
if isinstance(value, (int, float)):
|
||||
return float(value)
|
||||
return float(str(value).replace(',', '').replace('¥', '').replace('%', '').strip() or 0)
|
||||
text = str(value).replace(',', '').replace('¥', '').replace('%', '').strip()
|
||||
if not text:
|
||||
return 0.0
|
||||
try:
|
||||
return float(text)
|
||||
except ValueError:
|
||||
return 0.0 # 透视/报表里混着表头文本,按 0 算
|
||||
|
||||
|
||||
def _col(s):
|
||||
@@ -57,6 +64,9 @@ def _read_export_detail(path):
|
||||
wb = openpyxl.load_workbook(path, read_only=True, data_only=True)
|
||||
try:
|
||||
ws = wb[wb.sheetnames[0]]
|
||||
# 平台导出的 dimension 可能写成 1×1(实测 11 月版本),read_only 下会读到 0 行
|
||||
if (ws.max_row or 1) <= 1 or (ws.max_column or 1) <= 1:
|
||||
ws.reset_dimensions()
|
||||
hi = _col(config.DY_DETAIL_PASTE_LAST_COL)
|
||||
rows = [list(r) for r in ws.iter_rows(min_row=config.DY_HEADER_ROW + 1,
|
||||
min_col=1, max_col=hi, values_only=True)]
|
||||
@@ -67,24 +77,28 @@ def _read_export_detail(path):
|
||||
wb.close()
|
||||
|
||||
|
||||
def _detail_file(work_dir):
|
||||
files = sorted(glob.glob(os.path.join(work_dir, config.DOUYIN_DETAIL_GLOB)))
|
||||
return files[-1] if files else ''
|
||||
|
||||
|
||||
def _local_table(day):
|
||||
return os.path.join(config.WORK_DIR, config.DAILY_WORK_DIR,
|
||||
return os.path.join(config.WORK_DIR,
|
||||
share_copy.table_name(config.DOUYIN_SHARE_KEYWORD, day,
|
||||
config.SHARE_TABLE_EXT))
|
||||
|
||||
|
||||
def _match_day(value, target):
|
||||
"""BS/BQ 这类辅助列存的是「日号数字」,不是日期;两种都要能对上营业日。"""
|
||||
if isinstance(value, bool):
|
||||
return False
|
||||
if isinstance(value, (int, float)):
|
||||
return int(value) == target.day
|
||||
return _as_date(value) == target
|
||||
|
||||
|
||||
def _sum_filtered(ws, time_col, status_col, income_col, target):
|
||||
"""按 时间=营业日 & 状态=已核销 过滤,累计预计收入(转数字)。"""
|
||||
t, s, i = _col(time_col), _col(status_col), _col(income_col)
|
||||
total = 0.0
|
||||
count = 0
|
||||
for r in range(config.DY_DETAIL_DATA_START, int(ws.max_row or 0) + 1):
|
||||
if _as_date(ws.cell(r, t).value) != target:
|
||||
if not _match_day(ws.cell(r, t).value, target):
|
||||
continue
|
||||
if str(ws.cell(r, s).value or '').strip() != config.DY_STATUS_VALUE:
|
||||
continue
|
||||
@@ -116,48 +130,16 @@ def _write_detail_values(table_path, rows):
|
||||
[('核销明细', rows, 1, config.DY_DETAIL_DATA_START)])
|
||||
|
||||
|
||||
def _base_info_check(workbook, dry_run=False):
|
||||
"""【报表】O/P 门店/项目与【基础信息】比对,新增项写回本地副本。"""
|
||||
if config.REPORT_SHEET not in workbook.sheetnames or \
|
||||
config.DY_BASE_SHEET not in workbook.sheetnames:
|
||||
return {'ok': False, 'message': '缺少【报表】或【基础信息】',
|
||||
'new_shops': [], 'new_items': []}
|
||||
report = workbook[config.REPORT_SHEET]
|
||||
base = workbook[config.DY_BASE_SHEET]
|
||||
shop_col = _col(config.DY_REPORT_SHOP_COL)
|
||||
item_col = _col(config.DY_REPORT_ITEM_COL)
|
||||
pairs = []
|
||||
for r in range(config.REPORT_DATA_START, int(report.max_row or 0) + 1):
|
||||
shop = str(report.cell(r, shop_col).value or '').strip()
|
||||
item = str(report.cell(r, item_col).value or '').strip()
|
||||
if shop and shop != '0' and (shop, item) not in pairs:
|
||||
pairs.append((shop, item))
|
||||
known = set()
|
||||
for r in range(config.DY_BASE_INFO_ROW, int(base.max_row or 0) + 1):
|
||||
v = str(base.cell(r, 1).value or '').strip()
|
||||
if v and v != '0':
|
||||
known.add(v)
|
||||
new_shops = [s for s, _ in pairs if s and s not in known]
|
||||
new_items = []
|
||||
for _, item in pairs:
|
||||
if '【' in item and '】' in item:
|
||||
name = item.split('【', 1)[1].split('】', 1)[0].strip()
|
||||
if name and name not in known:
|
||||
new_items.append(name)
|
||||
changed = 0
|
||||
if not dry_run:
|
||||
r = max(int(base.max_row or 0) + 1, config.DY_BASE_INFO_ROW)
|
||||
for name in dict.fromkeys(new_shops + new_items):
|
||||
if name in known:
|
||||
continue
|
||||
base.cell(r, 1, name)
|
||||
known.add(name)
|
||||
r += 1
|
||||
changed += 1
|
||||
return {'ok': True, 'new_shops': new_shops, 'new_items': new_items,
|
||||
'written': changed,
|
||||
'message': f'基础信息检查:新增门店 {len(new_shops)} / 项目 {len(new_items)}'
|
||||
+ ('' if dry_run else f',写入 {changed} 行')}
|
||||
def _base_info_layout():
|
||||
return base_info_sync.layout(base_info_sync.LAYOUT_DOUYIN)
|
||||
|
||||
|
||||
def _base_info_scan(workbook):
|
||||
"""【报表】A/B 与【基础信息】A/E 比对(与美团同一套逻辑),只读不写。
|
||||
|
||||
``workbook`` 必须是 data_only 打开的,否则报表读到的是 VLOOKUP 公式文本。
|
||||
"""
|
||||
return base_info_sync.scan(workbook, _base_info_layout())
|
||||
|
||||
|
||||
def run(day=None, dry_run=False, use_com=None):
|
||||
@@ -171,7 +153,8 @@ def run(day=None, dry_run=False, use_com=None):
|
||||
result['message'] = f'未找到本地抖音核对表副本:{table}(请先从共享盘复制)'
|
||||
return result
|
||||
|
||||
export = _detail_file(config.WORK_DIR)
|
||||
# 核销明细导出优先找抖音分支目录,其次过程表
|
||||
export = config.latest_file(config.DOUYIN_DETAIL_GLOB)
|
||||
if not export:
|
||||
result['message'] = f'未找到导出的核销明细({config.DOUYIN_DETAIL_GLOB})'
|
||||
return result
|
||||
@@ -209,7 +192,7 @@ def run(day=None, dry_run=False, use_com=None):
|
||||
pivot_total=pivot_total, report_total=report_total,
|
||||
convert_total=convert_total, consistent=consistent)
|
||||
|
||||
base_info = _base_info_check(wb, dry_run=True)
|
||||
base_info = _base_info_scan(wb)
|
||||
result['base_info'] = base_info
|
||||
|
||||
if dry_run:
|
||||
@@ -220,16 +203,11 @@ def run(day=None, dry_run=False, use_com=None):
|
||||
return result
|
||||
|
||||
_write_detail_values(table, rows)
|
||||
# 基础信息写入需要可保存的 workbook;zip+XML 不影响,单独用 openpyxl 追加
|
||||
# 基础信息写入需要可保存的 workbook;zip+XML 不影响,单独用 openpyxl 追加。
|
||||
# 直接复用扫描结果,不再重读报表(重读会拿到公式文本,曾把 A 列写坏)。
|
||||
if base_info.get('ok') and (base_info['new_shops'] or base_info['new_items']):
|
||||
wb2 = openpyxl.load_workbook(table)
|
||||
try:
|
||||
written = _base_info_check(wb2, dry_run=False)
|
||||
if written.get('written'):
|
||||
wb2.save(table)
|
||||
result['base_info'] = written
|
||||
finally:
|
||||
wb2.close()
|
||||
changed = base_info_sync.write_changes(table, base_info, _base_info_layout())
|
||||
base_info['written'] = changed
|
||||
|
||||
# 保存后真正刷新透视缓存 + 公式,再从缓存读四口径
|
||||
do_com = config.USE_COM_REFRESH if use_com is None else bool(use_com)
|
||||
|
||||
Reference in New Issue
Block a user