release: bump version to 1.1.7, update xq0031_xq0032 plugin to 1.0.8
主要变更: 1. 整体版本升级至1.1.7,更新安装包与shell版本号 2. 新增API与后端能力: - 新增停止运行接口,支持强制终止插件进程与浏览器子进程 - 云端会话令牌与下载票据落库,适配多worker部署 - 优化Chrome版本读取逻辑,避免弹窗与编码问题 3. xq0031_xq0032插件升级至1.0.8: - 新增仓库中文名自动筛选功能,简化用户操作 - 优化ERP登录与报表导出流程,修复标签页残留问题 - 新增前端强制停止按钮,支持实时终止运行 - 修复中文编码与乱码问题,兼容Windows非UTF-8环境 4. 修复授权校验逻辑,确保账号与授权匹配 5. 更新文档与部署脚本,修正发版流程说明
This commit is contained in:
@@ -6,8 +6,12 @@
|
||||
组织/仓库编码/日期选项由插件界面配置经 run() 参数注入;交互逻辑与影刀版保持同源。
|
||||
"""
|
||||
import json
|
||||
import re
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
import openpyxl
|
||||
|
||||
from . import config
|
||||
from . import k3_common as kc
|
||||
@@ -54,7 +58,10 @@ def _wait_window_title(page, keyword, timeout=None):
|
||||
|
||||
# ================== 报表操作 ==================
|
||||
def _open_report(page):
|
||||
"""点击全局搜索 → 输入「物料收发汇总表」→ 回车 → 等待过滤界面出现。"""
|
||||
"""点击全局搜索 → 输入「物料收发汇总表」→ 等联想下拉 → 点结果项(与需求32同法,不回车)→ 等待过滤界面出现。
|
||||
|
||||
不能用回车直接开第一个结果:回车会收起联想下拉且可能开成其它报表/列表页,
|
||||
导致后续「点击搜索结果」兜底找不到项。改成在联想下拉里点选「物料收发汇总表」。"""
|
||||
if not kc.click_visible(page, 'css:.kd-fullsearch-searchicon', timeout=10):
|
||||
config.log(f'{TAG} 找不到全局搜索图标')
|
||||
return False
|
||||
@@ -65,9 +72,9 @@ def _open_report(page):
|
||||
return False
|
||||
sbox.clear()
|
||||
sbox.input('物料收发汇总表')
|
||||
time.sleep(0.3)
|
||||
kc.press_enter(page)
|
||||
time.sleep(1.5)
|
||||
time.sleep(0.8) # 只出联想下拉,不回车
|
||||
|
||||
_click_search_result(page)
|
||||
|
||||
# 等待过滤界面(含组织字段 FSTOCKORGID)出现,限定在 .k-window 弹窗内
|
||||
end = time.time() + 40
|
||||
@@ -77,7 +84,7 @@ def _open_report(page):
|
||||
return True
|
||||
time.sleep(0.5)
|
||||
|
||||
# 兜底:点击搜索结果中的「物料收发汇总表」菜单项
|
||||
# 兜底:联想下拉里点选失败时,再扫一遍含关键字的可见菜单项
|
||||
config.log(f'{TAG} 未自动打开,尝试点击搜索结果')
|
||||
els = page.eles('xpath://*[contains(text(), "物料收发汇总表")]')
|
||||
for el in els:
|
||||
@@ -93,6 +100,29 @@ def _open_report(page):
|
||||
return False
|
||||
|
||||
|
||||
def _click_search_result(page):
|
||||
"""在联想下拉(.kd-fullsearch-selectable li)里点选文本为「物料收发汇总表」的结果项。"""
|
||||
end = time.time() + 20
|
||||
while time.time() < end:
|
||||
for li in page.eles('css:.kd-fullsearch-selectable li.kd-fullsearch-item-container',
|
||||
timeout=2):
|
||||
try:
|
||||
if not li.states.is_displayed:
|
||||
continue
|
||||
t = li.ele('css:span[searchitem="text"]', timeout=0.5)
|
||||
txt = ((t.attr('title') or (t.text or '')).strip() if t is not None else '')
|
||||
if '物料收发汇总表' not in txt:
|
||||
continue
|
||||
li.click()
|
||||
time.sleep(2)
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(0.5)
|
||||
config.log(f'{TAG} 联想下拉中未找到「物料收发汇总表」结果项')
|
||||
return False
|
||||
|
||||
|
||||
def _open_filter(page):
|
||||
"""确保过滤界面打开:
|
||||
1. 先关闭「文件下载」窗口(K3 下载完成后残留、挡在最上层)
|
||||
@@ -308,13 +338,13 @@ def _set_date(page, begin, end):
|
||||
|
||||
|
||||
def _click_add_row(page):
|
||||
"""点击仓库过滤面板「新增行」。
|
||||
全局 li.k-link.kdFilterAdd 会同时匹配「主报表过滤」+「仓库过滤」两个面板,
|
||||
原实现总点到主过滤那个(其行数不增加导致误判失败)。因此必须限定在仓库过滤
|
||||
容器(id 含 FBeginStockId_Filter)内定位。物理点击只会选中文字,改用 JS click 触发。"""
|
||||
"""点击过滤窗口「条件」页签过滤网格的「新增行」。
|
||||
新流程不再打开仓库过滤面板,过滤窗口内 li.kdFilterAdd 即为目标
|
||||
(aa.md: <ul class="k-link kdGridFilterBar"><li class="k-link kdFilterAdd">新增行</li>...)。
|
||||
物理点击只会选中文字,改用 JS click 触发。"""
|
||||
for css in (
|
||||
'xpath://li[contains(@class,"kdFilterAdd") and ancestor::div[contains(@id,"FBeginStockId_Filter")]]',
|
||||
'css:[id*="FBeginStockId_Filter-FBATCHFILTERGRID_ENTRYBAR-tbAddRow"]',
|
||||
'css:.k-window li.kdFilterAdd',
|
||||
'xpath://li[contains(@class,"kdFilterAdd") and ancestor::div[contains(@id,"_Filter")]]',
|
||||
):
|
||||
# 直接取单个元素命中即点,避免 eles 遍历整页拖慢
|
||||
el = page.ele(css, timeout=2)
|
||||
@@ -328,6 +358,149 @@ def _click_add_row(page):
|
||||
return False
|
||||
|
||||
|
||||
def _click_filter_tab(page, tab):
|
||||
"""点击过滤窗口顶部页签(快捷/条件/选项...),tab 传页签文本(如 '条件'/'快捷')。
|
||||
K3 页签结构(aa.md):ul.k-tabstrip-items > li[role="tab"] > span.k-link(页签名)。"""
|
||||
target = None
|
||||
for li in page.eles('css:.k-window ul.k-tabstrip-items li[role="tab"]', timeout=2):
|
||||
try:
|
||||
if not li.states.is_displayed:
|
||||
continue
|
||||
link = li.ele('css:.k-link', timeout=0.5)
|
||||
if link is not None and (link.text or '').strip() == tab:
|
||||
target = li
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
if target is None:
|
||||
return False
|
||||
if 'k-state-active' in (target.attr('class') or ''):
|
||||
config.log(f'{TAG} 过滤页签已是: {tab}')
|
||||
return True
|
||||
try:
|
||||
target.click()
|
||||
except Exception:
|
||||
try:
|
||||
target.click(by_js=True)
|
||||
except Exception:
|
||||
return False
|
||||
time.sleep(0.6)
|
||||
config.log(f'{TAG} 已切换过滤页签: {tab}')
|
||||
return True
|
||||
|
||||
|
||||
def _condition_rows(page):
|
||||
"""过滤窗口「条件」页签当前可见的条件行 <tr>(含字段下拉 kd-defaultQuikerRow-partFileds 的行)。"""
|
||||
rows = []
|
||||
for css in (
|
||||
'css:.k-window tr:has(.kd-defaultQuikerRow-partFileds)',
|
||||
'xpath://div[contains(@class,"k-window")]//tr[.//*[contains(@class,"kd-defaultQuikerRow-partFileds")]]',
|
||||
):
|
||||
try:
|
||||
for tr in page.eles(css, timeout=2):
|
||||
try:
|
||||
if tr.states.is_displayed:
|
||||
rows.append(tr)
|
||||
except Exception:
|
||||
pass
|
||||
if rows:
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
return rows
|
||||
|
||||
|
||||
def _select_row_field(page, tr, field):
|
||||
"""把条件行字段下拉设为指定字段(如「仓库编码」),回读 title 验证。
|
||||
行结构(aa.md):<td class="part_fileds_extend"><span class="k-combobox kd-defaultQuikerRow-partFileds">
|
||||
<input role="combobox" title=当前字段>...<span class="k-icon k-i-close">清空</span>。
|
||||
主路径统一用 JS 驱动 kendoComboBox 选中(每行行为完全一致,规避 UI 展开/输入过滤的
|
||||
时序与焦点差异导致的逐字键入/点箭头等不一致):open 触发数据加载 → select 匹配文本
|
||||
→ trigger change → 回读 title 验证;JS 不可用时才兜底走 UI 输入过滤点选。"""
|
||||
box = tr.ele('css:input.kd-defaultQuikerRow-partFileds[role="combobox"]', timeout=2)
|
||||
if box is None:
|
||||
box = tr.ele('css:.kd-defaultQuikerRow-partFileds[role="combobox"]', timeout=2)
|
||||
if box is None:
|
||||
return False
|
||||
try:
|
||||
if (box.attr('title') or '').strip() == field:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _done():
|
||||
try:
|
||||
return (box.attr('title') or '').strip() == field
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
# 方式一(主):JS 统一驱动 kendoComboBox 选中目标字段(run_js 内 this 指向 tr)
|
||||
try:
|
||||
field_lit = json.dumps(field)
|
||||
r = tr.run_js(f"""
|
||||
var $ = window.jQuery;
|
||||
var $inp = $(this).find('input[data-role="combobox"]').first();
|
||||
var k = $inp.data('kendoComboBox');
|
||||
if (!k) {{ return 'nok'; }}
|
||||
try {{ k.open(); }} catch (e) {{}}
|
||||
var pick = function (d) {{
|
||||
if (!d) return false;
|
||||
for (var key in d) {{
|
||||
if (typeof d[key] === 'string' && $.trim(d[key]) === {field_lit}) return true;
|
||||
}}
|
||||
return false;
|
||||
}};
|
||||
var idx = k.select(pick);
|
||||
try {{ k.trigger('change'); }} catch (e) {{}}
|
||||
return idx === -1 ? 'miss' : 'ok';
|
||||
""")
|
||||
time.sleep(0.5)
|
||||
if str(r) == 'ok':
|
||||
return True
|
||||
except Exception as e:
|
||||
config.log(f'{TAG} 字段JS选择异常: {e}')
|
||||
|
||||
# 方式二(兜底):点输入框 → 清空 → 输入字段名过滤 → 从列表点选
|
||||
try:
|
||||
box.click()
|
||||
time.sleep(0.5)
|
||||
try:
|
||||
box.clear()
|
||||
time.sleep(0.3)
|
||||
except Exception:
|
||||
pass
|
||||
box.input(field)
|
||||
time.sleep(1.0)
|
||||
owns = ''
|
||||
try:
|
||||
owns = box.attr('aria-owns') or ''
|
||||
except Exception:
|
||||
pass
|
||||
if owns:
|
||||
try:
|
||||
lb = page.ele('#' + owns, timeout=2)
|
||||
except Exception:
|
||||
lb = None
|
||||
if lb is not None:
|
||||
for it in lb.eles('css:li', timeout=2):
|
||||
try:
|
||||
if it.states.is_displayed and it.text.strip() == field:
|
||||
it.click()
|
||||
time.sleep(0.5)
|
||||
if _done():
|
||||
return True
|
||||
except Exception:
|
||||
continue
|
||||
# 过滤后可能仅一项:回车确认
|
||||
kc.press_enter_in(box, page)
|
||||
time.sleep(0.6)
|
||||
if _done():
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def _row_tr(v):
|
||||
"""上溯到包含该值输入框的条件行 <tr>(一个过滤条件一行,内含逻辑/字段/比较/值)。"""
|
||||
node = v
|
||||
@@ -343,61 +516,162 @@ def _row_tr(v):
|
||||
|
||||
def _ensure_logic_or(page, v):
|
||||
"""把指定值输入框所在行的逻辑下拉制为「或者」(OR)。
|
||||
多个仓库编码之间需为或关系;行内逻辑下拉类名含 _PART_Logic_listbox,选项 并且/或者。"""
|
||||
多个仓库编码之间需为或关系;行内逻辑下拉类名含 _PART_Logic_listbox,选项 并且/或者。
|
||||
K3 现场结论:点右侧箭头展开不可靠(点不出列表);改为点输入框展开后点选,
|
||||
再用 JS 驱动 kendoDropDownList 兜底,选中后回读验证,避免条件行保持「并且」查不出数据。"""
|
||||
tr = _row_tr(v)
|
||||
if tr is None:
|
||||
return False
|
||||
lg = tr.ele('css:span[role="listbox"][aria-owns*="_PART_Logic_listbox"]', timeout=2)
|
||||
if lg is None:
|
||||
return False
|
||||
|
||||
def _done():
|
||||
"""回读当前逻辑文本,是「或者」返回 True。"""
|
||||
try:
|
||||
cur = lg.ele('css:.k-input', timeout=1)
|
||||
return bool(cur and cur.text.strip() == '或者')
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
if _done():
|
||||
return True
|
||||
|
||||
# 方式一:点输入框(非箭头)展开列表,点选「或者」
|
||||
try:
|
||||
cur = lg.ele('css:.k-input', timeout=1)
|
||||
if cur and cur.text.strip() == '或者':
|
||||
return True
|
||||
inp = lg.ele('css:.k-input', timeout=2)
|
||||
if inp is not None:
|
||||
inp.click()
|
||||
time.sleep(0.6)
|
||||
lb = page.ele('#' + lg.attr('aria-owns'), timeout=2)
|
||||
if lb is not None:
|
||||
for it in lb.eles('css:li', timeout=2):
|
||||
try:
|
||||
if it.text.strip() == '或者':
|
||||
it.click()
|
||||
time.sleep(0.4)
|
||||
if _done():
|
||||
return True
|
||||
except Exception:
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 方式二(兜底):JS 直接驱动 kendoDropDownList 选中「或者」(run_js 内 this 指向 lg)
|
||||
try:
|
||||
lg.ele('css:.k-select', timeout=2).click()
|
||||
time.sleep(0.3)
|
||||
lb = page.ele('#' + lg.attr('aria-owns'), timeout=2)
|
||||
if lb is None:
|
||||
return False
|
||||
for it in lb.eles('css:li', timeout=2):
|
||||
try:
|
||||
if it.text.strip() == '或者':
|
||||
it.click()
|
||||
time.sleep(0.2)
|
||||
return True
|
||||
except Exception:
|
||||
continue
|
||||
r = lg.run_js("""
|
||||
var $ = window.jQuery;
|
||||
var $lg = $(this);
|
||||
var $k = $lg.find('input[data-role="dropdownlist"]').first();
|
||||
var k = $k.data('kendoDropDownList');
|
||||
if (!k) { return false; }
|
||||
var hit = false;
|
||||
k.ul.find('li').each(function () {
|
||||
if ($(this).text().trim() === '或者') { k.select($(this)); hit = true; }
|
||||
});
|
||||
return hit;
|
||||
""")
|
||||
time.sleep(0.4)
|
||||
if r:
|
||||
return _done()
|
||||
except Exception as e:
|
||||
config.log(f'{TAG} 设置逻辑为或者失败: {e}')
|
||||
return False
|
||||
return False
|
||||
|
||||
|
||||
def _diag_stock_btn(page):
|
||||
"""仓库搜索按钮找不到时输出诊断信息,定位是过滤窗口没开还是按钮不可见。"""
|
||||
try:
|
||||
wins = [t.text for t in page.eles('css:.k-window-title') if t.states.is_displayed]
|
||||
config.log(f' [诊断] 当前可见窗口标题: {wins}')
|
||||
c = page.ele('css:[id*="FBEGINSTOCKID-EDITOR"]', timeout=1)
|
||||
config.log(f' [诊断] FBEGINSTOCKID-EDITOR 容器存在={bool(c)}'
|
||||
f' 可见={c.states.is_displayed if c else "-"}')
|
||||
btns = page.eles('css:[id*="FBEGINSTOCKID"] .kd-editor-btn')
|
||||
vis = 0
|
||||
for b in btns:
|
||||
def _clear_stock_range(page):
|
||||
"""清空过滤窗口「快捷」页签的开始/结束仓库范围(FBEGINSTOCKID/FENDSTOCKID)默认值。
|
||||
现场验证(旧系统):JS 对象级清空 widget(val/options.value/valData)在提交查询时无效,
|
||||
K3 不从 widget 对象读仓库范围,查询仍被默认仓库(如代管仓 03010127)卡住、拿不到全部仓库;
|
||||
必须模拟用户手动操作——点击输入框 -> Ctrl+A 全选 -> Delete 删除 -> Tab 触发 change,
|
||||
才能真正清掉方案里的仓库范围,查询返回全部仓库(用户手动验证同法有效)。
|
||||
仅当清空后回读内部值仍带 name/number 时返回 False(外层重试);无组件/异常只记日志不阻断。"""
|
||||
ok = True
|
||||
for key in ('FBEGINSTOCKID', 'FENDSTOCKID'):
|
||||
el = page.ele(f'css:.k-window [id$="{key}-EDITOR"]', timeout=2)
|
||||
if el is None:
|
||||
continue
|
||||
try:
|
||||
inp = el.ele('css:input.k-input', timeout=1)
|
||||
if inp is None:
|
||||
inp = el.ele('css:input', timeout=1)
|
||||
if inp is None:
|
||||
config.log(f'{TAG} 仓库范围字段 {key} 无输入框,跳过')
|
||||
continue
|
||||
try:
|
||||
if b.states.is_displayed:
|
||||
vis += 1
|
||||
inp.click()
|
||||
except Exception:
|
||||
pass
|
||||
config.log(f' [诊断] FBEGINSTOCKID 内 .kd-editor-btn 总数={len(btns)} 可见={vis}')
|
||||
org_css = 'css:.k-window [id*="FSTOCKORGID"]'
|
||||
config.log(f' [诊断] 过滤窗口内(.k-window) FSTOCKORGID 可见节点: '
|
||||
f'{kc.ele_visible(page, org_css, timeout=1)}')
|
||||
except Exception as e:
|
||||
config.log(f' [诊断] 异常: {e}')
|
||||
inp.click(by_js=True)
|
||||
time.sleep(0.4)
|
||||
ac = page.actions
|
||||
ac.key_down('ctrl').key_down('a').key_up('a').key_up('ctrl')
|
||||
time.sleep(0.15)
|
||||
ac.key_down('delete').key_up('delete')
|
||||
time.sleep(0.2)
|
||||
ac.key_down('tab').key_up('tab')
|
||||
time.sleep(0.5)
|
||||
r = el.run_js("""
|
||||
var $ = window.jQuery, $el = $(this), inst = $el.data('kendoKDBosF7');
|
||||
if (!inst) { return 'nok'; }
|
||||
var v = inst.val() || {};
|
||||
var name = v.name || '', number = v.number || '';
|
||||
return (name === '' && number === '') ? 'ok' : 'still:' + name + '/' + number;
|
||||
""")
|
||||
if str(r) == 'ok':
|
||||
config.log(f'{TAG} 已清空仓库范围字段 {key}')
|
||||
elif str(r) == 'nok':
|
||||
config.log(f'{TAG} 仓库范围字段 {key} 无 kendoKDBosF7 组件,跳过')
|
||||
else:
|
||||
ok = False
|
||||
config.log(f'{TAG} 清空仓库范围字段 {key} 未生效: {r}')
|
||||
except Exception as e:
|
||||
ok = False
|
||||
config.log(f'{TAG} 清空仓库范围字段 {key} 异常: {e}')
|
||||
return ok
|
||||
|
||||
|
||||
def _delete_stock_rows(page):
|
||||
"""删除过滤窗口「条件」页签里字段为「仓库编码」的条件行(K3 个人方案残留的仓库列表行)。
|
||||
行结构(aa.md):字段下拉 title=仓库编码 + 值 + 行尾 .DeleteButtonIconStyle 删除按钮。
|
||||
删除后行号会变,重新扫描直到没有仓库编码行;非仓库编码行一律跳过不误删。"""
|
||||
ok = True
|
||||
for _ in range(5):
|
||||
rows = _condition_rows(page)
|
||||
if not rows:
|
||||
break
|
||||
hit = False
|
||||
for tr in rows:
|
||||
box = tr.ele('css:input.kd-defaultQuikerRow-partFileds[role="combobox"]', timeout=1)
|
||||
if box is None:
|
||||
box = tr.ele('css:.kd-defaultQuikerRow-partFileds[role="combobox"]', timeout=1)
|
||||
if box is None:
|
||||
continue
|
||||
try:
|
||||
title = (box.attr('title') or '').strip()
|
||||
except Exception:
|
||||
continue
|
||||
if title != '仓库编码':
|
||||
continue
|
||||
delbtn = tr.ele('css:.DeleteButtonIconStyle', timeout=1)
|
||||
if delbtn is None:
|
||||
ok = False
|
||||
continue
|
||||
try:
|
||||
delbtn.click()
|
||||
except Exception:
|
||||
try:
|
||||
delbtn.click(by_js=True)
|
||||
except Exception:
|
||||
ok = False
|
||||
continue
|
||||
time.sleep(0.5)
|
||||
hit = True
|
||||
break
|
||||
if not hit:
|
||||
break
|
||||
if ok:
|
||||
config.log(f'{TAG} 已清空「条件」页签仓库编码条件行')
|
||||
return ok
|
||||
|
||||
|
||||
def _submit_query(page, with_date=None):
|
||||
@@ -409,6 +683,9 @@ def _submit_query(page, with_date=None):
|
||||
if not _set_date(page, *with_date):
|
||||
config.log(f'{TAG} 提交前日期重设失败')
|
||||
return False
|
||||
if not _clear_stock_range(page):
|
||||
config.log(f'{TAG} 提交前清空仓库范围失败')
|
||||
return False
|
||||
if not kc.click_visible(page, 'css:.k-window [id*="_Filter-FBTNOK_c"]:not([id*="FBeginStockId"])'):
|
||||
config.log(f'{TAG} 找不到主过滤界面确定按钮')
|
||||
return False
|
||||
@@ -417,62 +694,67 @@ def _submit_query(page, with_date=None):
|
||||
|
||||
|
||||
def _set_stock_codes(page, codes, begin, end):
|
||||
"""仓库编码:点搜索 → 点过滤 → 逐行新增输入(或关系)→ 确定 → 退出 → 主界面确定触发查询。
|
||||
每行结构:括号 + 逻辑(或) + 字段(基本信息-编码) + 比较(等于) + 值(输入后回车查询)。
|
||||
新增行后填仓库编码(一行一个),并把每行逻辑设为「或者」(OR) 保证多仓库为或关系。
|
||||
提交查询(点主过滤确定)前重设一次日期:仓库面板操作过程可能把过滤条件重置为默认。"""
|
||||
"""【旧流程·备用】仓库编码:切到过滤窗口「条件」页签 → 逐行新增(字段=仓库编码,值=实际编码,逻辑=或者)
|
||||
→ 回到「快捷」页签 → 提交查询(提交前重设日期防覆盖)。
|
||||
新流程(aa.md:仓库编码切换位置,不再打开仓库列表/仓库过滤面板):
|
||||
在过滤窗口「条件」页签直接新增条件行,每行结构(aa.md):括号 + 逻辑(或者) + 字段(仓库编码)
|
||||
+ 比较(等于) + 值(输入后回车查询),逻辑设为「或者」(OR) 保证多仓库为或关系。
|
||||
注意:现 run() 已改为「统一下载全部仓库 → 下载后按中文名筛选 Excel」,
|
||||
本函数及其条件行辅助(_condition_rows/_click_add_row/_select_row_field 等)不再被调用,
|
||||
仅保留供业务需要恢复按编码在 K3 内过滤时使用。"""
|
||||
if not codes:
|
||||
return False
|
||||
# 1. 点仓库编码搜索按钮,打开仓库列表窗口(限定 .k-window 过滤窗口内)
|
||||
if not kc.click_visible(page, 'css:.k-window [id*="FBEGINSTOCKID-EDITOR"] .kd-editor-btn'):
|
||||
config.log(f'{TAG} 找不到仓库编码搜索按钮')
|
||||
_diag_stock_btn(page)
|
||||
# 1. 切到「条件」页签(新增行/字段下拉都在该页签下)
|
||||
if not _click_filter_tab(page, '条件'):
|
||||
config.log(f'{TAG} 找不到过滤窗口「条件」页签')
|
||||
return False
|
||||
time.sleep(1.5)
|
||||
# 2. 点「过滤」打开过滤面板
|
||||
if not kc.click_visible(page, 'css:[id*="FBeginStockId-BILLMENU_TOOLBAR-tbFilter"]'):
|
||||
config.log(f'{TAG} 找不到过滤按钮')
|
||||
return False
|
||||
time.sleep(1)
|
||||
time.sleep(1.0)
|
||||
|
||||
# 3. 逐行新增输入仓库编码,并将每行逻辑设为「或者」
|
||||
val_css = 'css:[id*="FBeginStockId_Filter-FFILTERGRID"] input[placeholder*="请输入关键字"]'
|
||||
# 2. 逐行新增输入仓库编码,字段=仓库编码,逻辑=或者
|
||||
for i, code in enumerate(codes):
|
||||
# 快捷过滤网格初始为空(无默认行),每行都得先「新增行」再填值
|
||||
vs = page.eles(val_css)
|
||||
if len(vs) <= i:
|
||||
rows = _condition_rows(page)
|
||||
if len(rows) <= i:
|
||||
added = False
|
||||
for _ in range(3):
|
||||
if _click_add_row(page):
|
||||
time.sleep(0.8)
|
||||
if len(page.eles(val_css)) > i:
|
||||
time.sleep(1.2)
|
||||
if len(_condition_rows(page)) > i:
|
||||
added = True
|
||||
break
|
||||
if not added:
|
||||
config.log(f'{TAG} 点击新增行失败(第{i + 1}行,当前值输入框{len(vs)}个)')
|
||||
config.log(f'{TAG} 点击新增行失败(第{i + 1}行,当前条件行{len(rows)}个)')
|
||||
return False
|
||||
vs = page.eles(val_css)
|
||||
config.log(f'{TAG} 当前过滤值输入框数: {len(vs)}')
|
||||
v = vs[i] if len(vs) > i else None
|
||||
if not v:
|
||||
config.log(f'{TAG} 找不到第{i + 1}行过滤值输入框')
|
||||
rows = _condition_rows(page)
|
||||
tr = rows[i] if len(rows) > i else None
|
||||
if tr is None:
|
||||
config.log(f'{TAG} 找不到第{i + 1}行条件行')
|
||||
return False
|
||||
# 2.1 字段选「仓库编码」
|
||||
if not _select_row_field(page, tr, '仓库编码'):
|
||||
config.log(f'{TAG} 第{i + 1}行字段选择失败({code})')
|
||||
return False
|
||||
time.sleep(0.6)
|
||||
# 2.2 值输入框填实际编码 + 回车触发查询确认
|
||||
v = tr.ele('css:input[placeholder*="回车查询"]', timeout=2)
|
||||
if v is None:
|
||||
v = tr.ele('css:.kd-popListEdit-warp input.k-input', timeout=2)
|
||||
if v is None:
|
||||
config.log(f'{TAG} 第{i + 1}行找不到值输入框')
|
||||
return False
|
||||
v.clear()
|
||||
time.sleep(0.3)
|
||||
v.input(code)
|
||||
time.sleep(0.3)
|
||||
kc.press_enter_in(v, page) # 回车直接发给输入框,触发查询确认
|
||||
time.sleep(0.8)
|
||||
time.sleep(1.2)
|
||||
_ensure_logic_or(page, v) # 保证该行逻辑为「或者」
|
||||
config.log(f'{TAG} 仓库条件行{i + 1}: {code}(逻辑或)')
|
||||
config.log(f'{TAG} 仓库条件行{i + 1}: {code}(字段=仓库编码,逻辑或)')
|
||||
time.sleep(0.8)
|
||||
|
||||
# 4. 过滤面板内「确定」
|
||||
if not kc.click_visible(page, 'css:[id*="FBeginStockId_Filter-FBTNOK_c"]'):
|
||||
config.log(f'{TAG} 找不到过滤面板确定按钮')
|
||||
# 3. 回到「快捷」页签,设置日期并提交查询(提交前重设日期防覆盖)
|
||||
if not _click_filter_tab(page, '快捷'):
|
||||
config.log(f'{TAG} 找不到过滤窗口「快捷」页签')
|
||||
return False
|
||||
time.sleep(0.8)
|
||||
# 5. 「退出」关闭仓库过滤窗口
|
||||
kc.click_visible(page, 'css:[id*="FBeginStockId-BILLMENU_TOOLBAR-tbClose"]', timeout=8)
|
||||
time.sleep(0.8)
|
||||
# 6. 提交查询(提交前重设日期防仓库面板操作覆盖)
|
||||
return _submit_query(page, (begin, end))
|
||||
|
||||
|
||||
@@ -616,15 +898,67 @@ def _export_all_pages(page):
|
||||
return True
|
||||
|
||||
|
||||
# ================== 下载后按仓库中文名筛选 ==================
|
||||
def _stock_names(codes):
|
||||
"""编码列表 → 仓库中文名列表;无映射的编码记日志并跳过。"""
|
||||
names, missing = [], []
|
||||
for c in codes:
|
||||
n = config.STOCK_NAME_MAP.get(str(c).strip())
|
||||
if n:
|
||||
names.append(n)
|
||||
else:
|
||||
missing.append(c)
|
||||
if missing:
|
||||
config.log(f'{TAG} 以下仓库编码无中文名映射,筛选时将不包含: {missing}')
|
||||
return names
|
||||
|
||||
|
||||
def _filter_excel_by_stock_names(file, names):
|
||||
"""按仓库中文名列(表头第 4 行「仓库」)筛行:仅保留仓库名在 names 中的数据行,覆盖保存。
|
||||
从末行向上删除不匹配行,保留表头与单元格格式;仓库列空白的行不删(防御,避免误删子行)。
|
||||
返回保留的数据行数;表头未找到仓库列或文件异常时记日志返回 None。"""
|
||||
from openpyxl import load_workbook
|
||||
try:
|
||||
wb = load_workbook(str(file))
|
||||
except Exception as e:
|
||||
config.log(f'{TAG} 筛选失败,无法打开 {Path(file).name}: {e}')
|
||||
return None
|
||||
ws = wb[wb.sheetnames[0]]
|
||||
# 表头在第 4 行,定位仓库名称列(实测列名为「仓库名称」)
|
||||
col = None
|
||||
for c in range(1, ws.max_column + 1):
|
||||
h = ws.cell(4, c).value
|
||||
if h is not None and str(h).strip() in ('仓库', '仓库名称'):
|
||||
col = c
|
||||
break
|
||||
if col is None:
|
||||
config.log(f'{TAG} 筛选失败:第 4 行表头未找到仓库名称列')
|
||||
wb.close()
|
||||
return None
|
||||
kept = 0
|
||||
for r in range(ws.max_row, 4, -1): # 从末行向上,倒序删除不影响行号
|
||||
v = ws.cell(r, col).value
|
||||
if v is not None and str(v).strip() not in names:
|
||||
ws.delete_rows(r)
|
||||
else:
|
||||
kept += 1
|
||||
wb.save(str(file))
|
||||
wb.close()
|
||||
config.log(f'{TAG} 已按仓库中文名筛选 {Path(file).name},保留 {kept} 行({", ".join(names)})')
|
||||
return kept
|
||||
|
||||
|
||||
# ================== 单次导出 ==================
|
||||
def _run_once(page, begin, end, codes, save_dir, new_name, first, org):
|
||||
def _run_once(page, begin, end, save_dir, new_name, first, org, set_org=True):
|
||||
if not _open_filter(page):
|
||||
return False
|
||||
if first:
|
||||
# 首档:完整设置 组织 → 仓库(内部提交前重设日期并触发查询)
|
||||
if not _set_org(page, org):
|
||||
# 首档:新版/旧版唯一区别是组织——新版需选组织(set_org=True),旧版用登录默认组织;
|
||||
# 其余完全一致:无需点「保存」固化方案,仓库范围由 _submit_query 提交前以键盘方式
|
||||
# 清空,日期正常输入,编码不做 K3 过滤,下载后按所选编码的中文名筛选 Excel(见 run())。
|
||||
if set_org and not _set_org(page, org):
|
||||
return False
|
||||
if not _set_stock_codes(page, codes, begin, end):
|
||||
if not _submit_query(page, (begin, end)):
|
||||
return False
|
||||
else:
|
||||
# 后续档:组织/仓库等条件 K3 已保留,只需改日期后提交查询
|
||||
@@ -640,8 +974,9 @@ def _run_once(page, begin, end, codes, save_dir, new_name, first, org):
|
||||
|
||||
|
||||
# ================== 入口 ==================
|
||||
def run(page, save_dir, org, codes, date_options, progress=None):
|
||||
def run(page, save_dir, org, codes, date_options, progress=None, set_org=True):
|
||||
"""先全局搜索打开报表,再逐日期选项导出(组织/仓库仅首档完整设置,后续档只改日期)。
|
||||
set_org=False 时跳过组织设置(旧系统使用登录默认组织)。
|
||||
progress(done, total) 为整体进度回调;返回 (saved_files, failed_labels)。"""
|
||||
if not _open_report(page):
|
||||
raise RuntimeError("打开「物料收发汇总表」失败:全局搜索未打开报表过滤界面(详见日志)")
|
||||
@@ -653,14 +988,18 @@ def run(page, save_dir, org, codes, date_options, progress=None):
|
||||
success = False
|
||||
for attempt in range(1, config.RETRY_MAX + 1):
|
||||
config.log(f'{TAG} [{label}] 第 {attempt}/{config.RETRY_MAX} 次: {begin} ~ {end}')
|
||||
if _run_once(page, begin, end, codes, save_dir, new_name,
|
||||
first=(i == 0), org=org):
|
||||
if _run_once(page, begin, end, save_dir, new_name,
|
||||
first=(i == 0), org=org, set_org=set_org):
|
||||
success = True
|
||||
break
|
||||
kc.reset_filter_state(page, tag=TAG, wait_query=True) # 清掉失败留下的弹层/半填条件再重试
|
||||
time.sleep(2)
|
||||
if success:
|
||||
saved.append(str(save_dir / new_name))
|
||||
fpath = save_dir / new_name
|
||||
if codes:
|
||||
# 下载的是全部仓库数据,按所选编码的中文名筛行后覆盖保存
|
||||
_filter_excel_by_stock_names(fpath, _stock_names(codes))
|
||||
saved.append(str(fpath))
|
||||
else:
|
||||
config.log(f'{TAG} [{label}] 重试 {config.RETRY_MAX} 次均失败,继续下一个日期选项')
|
||||
failed.append(label)
|
||||
@@ -671,3 +1010,78 @@ def run(page, save_dir, org, codes, date_options, progress=None):
|
||||
pass
|
||||
time.sleep(1)
|
||||
return saved, failed
|
||||
|
||||
|
||||
def merge_summary(save_dir):
|
||||
"""把本目录下各档「物料收发汇总表_标签.xlsx」合并为一个汇总文件
|
||||
物料收发汇总表_<日期>.xlsx(与原影刀版 merge_summary.py 同源):
|
||||
- 每个报表第 4 行是表头;合并时第 4 行表头第一列新增「时间区间」字段
|
||||
- 数据行第一列填入报表标签(60D / 30D / 自定义日期区间 ...)
|
||||
- 跳过每表最后一行汇总行
|
||||
返回汇总文件路径;无可合并报表返回 None。"""
|
||||
m = re.search(r'(\d{4}-\d{2}-\d{2})', str(save_dir))
|
||||
date_tag = m.group(1) if m else datetime.now().strftime('%Y-%m-%d')
|
||||
out = Path(save_dir) / f'{config.FILE_PREFIX_31}_{date_tag}.xlsx'
|
||||
|
||||
def _label(name):
|
||||
m = re.match(rf'^{config.FILE_PREFIX_31}_(.+)\.xlsx$', name)
|
||||
return m.group(1) if m else None
|
||||
|
||||
files = sorted(p for p in Path(save_dir).glob(f'{config.FILE_PREFIX_31}_*.xlsx')
|
||||
if _label(p.name) and p.name != out.name)
|
||||
if not files:
|
||||
config.log(f'{TAG} 目录下没有可合并的报表: {save_dir}')
|
||||
return None
|
||||
|
||||
header = None
|
||||
all_rows, labels = [], []
|
||||
for f in files:
|
||||
label = _label(f.name)
|
||||
try:
|
||||
wb = openpyxl.load_workbook(f, data_only=True)
|
||||
except Exception as e:
|
||||
config.log(f'{TAG} [跳过] 无法读取的报表: {f.name}({e})')
|
||||
continue
|
||||
ws = wb[wb.sheetnames[0]]
|
||||
h = [ws.cell(4, c).value for c in range(1, ws.max_column + 1)]
|
||||
if header is None:
|
||||
header = h
|
||||
elif h != header:
|
||||
config.log(f'{TAG} [跳过] 表头不一致: {f.name}')
|
||||
continue
|
||||
# 数据从第 5 行起,跳过最后一行汇总行
|
||||
n = 0
|
||||
for r in range(5, ws.max_row):
|
||||
row = [ws.cell(r, c).value for c in range(1, ws.max_column + 1)]
|
||||
if any(v is not None and str(v).strip() != '' for v in row):
|
||||
all_rows.append([label] + row)
|
||||
n += 1
|
||||
labels.append(label)
|
||||
config.log(f'{TAG} [{label}] 数据 {n} 行 <- {f.name}')
|
||||
wb.close()
|
||||
|
||||
if not all_rows:
|
||||
config.log(f'{TAG} 没有可合并的数据')
|
||||
return None
|
||||
if out.exists():
|
||||
out.unlink()
|
||||
|
||||
wb = openpyxl.Workbook()
|
||||
ws = wb.active
|
||||
ws.title = 'Sheet1'
|
||||
# 前 3 行说明,保持「表头在第 4 行」的结构与单报表一致
|
||||
ws.cell(1, 1, '物料收发汇总表(汇总)')
|
||||
ws.cell(2, 1, f'生成时间: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}')
|
||||
ws.cell(3, 1, f'合并报表: {", ".join(labels)}')
|
||||
# 第 4 行表头:第一列插入「时间区间」
|
||||
ws.cell(4, 1, '时间区间')
|
||||
for c, v in enumerate(header, start=2):
|
||||
ws.cell(4, c, v)
|
||||
# 数据(第一列为报表标签)
|
||||
for r, row in enumerate(all_rows, start=5):
|
||||
for c, v in enumerate(row, start=1):
|
||||
ws.cell(r, c, v)
|
||||
wb.save(out)
|
||||
wb.close()
|
||||
config.log(f'{TAG} 已生成汇总文件: {out}(共 {len(all_rows)} 条数据,{len(labels)} 个报表)')
|
||||
return out
|
||||
|
||||
Reference in New Issue
Block a user