协同群改为智价通BOT接管对话,报价单按需生成;补客户名称、计费重与陆运报价条件

为什么改:应用建的协同群只能代答,销售在群里发文件、@人、问报价都对不上人;
本次把群对话交给智价通BOT,应用只负责拉群和解散群,并把几处销售已反馈的口径补齐。

群与身份:
- 新增 group_bot 短协调(Redis 带环境前缀):记录 BOT 已接管群、当前关联工单、
  @BOT 指令窗口与窗口外先到的附件,只做协调不当六态,六态仍在主账。
- 群里没有 @BOT 的文字不回;非 @ 文字断开当前单关联,之后必须重新 @BOT 带工单号。
- 先发文件再 @BOT 也认:窗口外附件先缓冲,等 @BOT 认领后补办。
- 会话存档只补附件、不重复办理 BOT 已办的同一句文字,避免同一条消息回两遍。
- 入站 from.userid 是加密 open_userid 时按官方接口换明文(带 Redis 缓存),
  否则与主账 sales_wecom_id 永远比不上,航线本人会被判成「不是本人」。
- 桥侧只认本群 req_id,不再借用别的群 req_id,避免测服把话发进别的群。

报价单:
- 建群后只发字段摘要和成交卡,Excel/PDF 等群里 @BOT 说「生成报价单」再出;
  多段逐段出,没价出空白表;出表后补推成交跟进不涨 deal_version、不重置等待计时。
- 陆运转人工先选业务类型和报价场景再建群,按选定模板名直取,不再靠关键词猜。
- 空运舱位只回一条时直接出该条报价,不再发选择卡。

字段与金额:
- 客户名称改为海/空/陆询价非必填,有值写回工单列,后台列表、报价单与 factsJson 一致。
- 空运新增计费重投影:体积重=CBM×166.7,计费重=max(毛重,体积重);
  TMS 无价、航线手工按 KG 报价时按计费重×单价算整票金额,群摘要与报价单同公式。
- 陆运报价附加条件统一到 land_quote_condition:未含税/含0税/未含燃油/含燃油费,
  燃油只乘主运费,不乘提货送货报关等杂费;跨境整车无备注列时追加到底部备注事项。
- 海运拼柜不适用箱型箱量:不进必填、不进补问、不出站,改回整柜可再用。
- 海运提货地址/派送地址改为非必填,写了才出站(placeOfReceipt/deliveryAddress),
  禁止从港口或贸易条款反推地址;TMS 查不到价时把 TMS 原话透给销售。
- 中港集拼与跨境集拼同口径:按体积+毛重,不采车型数量;修正被国内拼车别名抢单。
- 报关费区只收报关类费目;口岸海关监管费+接驳费按车型列回填固定行。

主账与后台:
- 看板处理量按协同群处理人统计;下线「平均报价销售额」;处理人搜索先把姓名换回企微 id。
- 「转人工」筛选同时兼容旧六态与新 collab_mode=handoff,新版单不再漏。
- 报价单客户名称、提货/派送地址、空运计费重、拼柜空箱型按新口径填表。
- 新增协同成员查询支持与 AIR_NEED_WO v2 迁移脚本。

文档:04 补海运/空运/陆运必填与地址口径,10 补地址出站规则,12 补测服独立应用 1000015 的部署与切换记录。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
linmianxun
2026-11-23 14:18:22 +08:00
co-authored by Cursor
parent b3637f3fc4
commit d9b2029c47
102 changed files with 11216 additions and 870 deletions
+265 -24
View File
@@ -16,6 +16,7 @@ from typing import Any, Optional
logger = logging.getLogger(__name__)
_CLOSED = frozenset({"已成交", "未成交", "已关闭", "转人工"})
_QUOTED = frozenset({"已报价", "协商中"})
@@ -29,8 +30,13 @@ _TO_VERBS = (
"涨到|升到|加到|减到|调到|增至|变为|变成|改为|改成|调整为|调整成"
)
_PERCENT_TO = re.compile(rf"({_TO_VERBS})\s*(\d+(?:\.\d+)?)\s*%")
# 金额前常带币种(「提高到USD 1330.00」),不能因为中间夹了 USD 就抽不到。
_CURRENCY_PREFIX = r"(?:CNY|RMB|USD|EUR|HKD|JPY|GBP|人民币|美元|港币|欧元|日元)"
# (?!\d) 防止「增加到110%」回退成「加到11」;(?!\s*%) 把带百分号的留给 _PERCENT_TO。
_TARGET = re.compile(rf"({_TO_VERBS})\s*(\d+(?:\.\d+)?)(?!\d)(?!\s*%)")
_TARGET = re.compile(
rf"({_TO_VERBS})\s*(?:{_CURRENCY_PREFIX}\s*)?(\d+(?:\.\d+)?)(?!\d)(?!\s*%)",
re.I,
)
_DAO_PERCENT = re.compile(rf"(?:{_TO_VERBS})\s*(\d+(?:\.\d+)?)\s*%")
_SPLIT = re.compile(r"[,,;;。!!??\n]|和(?=\S)")
_FEE_TOKEN = re.compile(r"([\u4e00-\u9fffA-Za-z0-9]{1,16}费)")
@@ -191,6 +197,148 @@ def _store_bucket(quote: dict[str, Any], key: str, items: list[dict[str, Any]])
quote[alt] = json.dumps(items, ensure_ascii=False)
def _ccy_code(raw: Any) -> str:
"""币种归一:RMB 统一成 CNY;空串表示沿用表头。"""
code = str(raw or "").strip().upper()
return "CNY" if code == "RMB" else code
def _sea_bucket_snapshot(quote: dict[str, Any] | None) -> dict[str, dict[str, Any]]:
"""
调价前先记住每段小计与明细币种。
为什么必须先记:一旦改了明细行,就再也算不出 TMS 当时用的小计口径
(尤其美元段小计是人民币,必须用原小计/原明细和反推,不能编汇率)。
"""
src = dict(quote or {})
header = _ccy_code(src.get("currency") or src.get("currencyCode"))
out: dict[str, dict[str, Any]] = {}
for key, _label in _SEA_BUCKETS:
sub_text = str(src.get("fee_total_" + key) or "").strip()
sub_amt, sub_prefix = parse_money(sub_text)
amounts: list[float] = []
ccys: set[str] = set()
for item in _bucket_items(src, key):
amount, prefix = parse_money(
item.get("amount") or item.get("value") or item.get("salePrice")
)
if amount is None:
continue
amounts.append(amount)
ccys.add(_ccy_code(item.get("currency") or prefix or header))
out[key] = {
"subtotal": sub_amt,
"sub_ccy": _ccy_code(sub_prefix or header),
"items_total": sum(amounts),
"line_ccys": ccys,
}
return out
def _set_subtotal_row(
rows: list[dict[str, Any]], bucket: str, new_value: float
) -> None:
"""把算好的小计写回这一行,_write_adjusted_buckets 再落 fee_total_*。"""
sub_key = "fee_total_" + bucket
text = format_money(new_value)
for row in rows:
if str(row.get("_subtotal_key") or "") == sub_key:
row["amount"] = text
return
def recompute_sea_bucket_totals(
base: dict[str, Any] | None,
new_rows: list[dict[str, Any]],
) -> dict[str, Any]:
"""
产品按行改价后,按 TMS 原小计口径重算海运三段小计。
返回:
subtotals {bucket: 小数计文本}
delta 合计应增加的人民币差额(只含能落到表头币种的小计)
bucket_rows 已算进小计的新明细下标,调用方算合计时不能再把这行加一次
currency_hint 有外币差额没法落合计时为 True
为什么不直接加明细:美元段的小计是人民币,TMS 原小计 ÷ 原明细和
就是 TMS 自己用的口径;用比例放大差额,不另编汇率。
"""
src = dict(base or {})
snap = _sea_bucket_snapshot(src)
rows = [dict(x) for x in (new_rows or []) if isinstance(x, dict)]
header = _ccy_code(src.get("currency") or src.get("currencyCode"))
used: set[int] = set()
subtotals: dict[str, str] = {}
bucket_rows: set[int] = set()
delta = 0.0
currency_hint = False
for key, _label in _SEA_BUCKETS:
items = _bucket_items(src, key)
if not items:
continue
state = snap.get(key) or {}
old_sub = state.get("subtotal")
old_rows = items
# 先按名称把新明细配回本段原行,和 stamp_prices_for_sheet 用同一套顺序。
matched: list[tuple[int, float, float]] = []
for item in old_rows:
amount, prefix = parse_money(
item.get("amount") or item.get("value") or item.get("salePrice")
)
idx = _match_fee_index(_item_fee_name(item), rows, used)
if amount is None or idx < 0:
continue
new_amount, _ = parse_money(
rows[idx].get("amount") or rows[idx].get("value") or rows[idx].get("salePrice")
)
if new_amount is None:
continue
used.add(idx)
matched.append((idx, amount, new_amount))
if not matched:
continue
native_delta = sum(new - old for _idx, old, new in matched)
if old_sub is None:
# 没有 TMS 小计:同币种差额直接给合计,外币才提示。
line_ccys = {c for c in (state.get("line_ccys") or set()) if c}
if not header or not line_ccys or line_ccys == {header}:
delta += native_delta
bucket_rows.update(idx for idx, _old, _new in matched)
else:
currency_hint = True
continue
sub_ccy = _ccy_code(state.get("sub_ccy") or header)
if sub_ccy and header and sub_ccy != header:
currency_hint = True
continue
line_ccys = {c for c in (state.get("line_ccys") or set()) if c}
if not line_ccys or line_ccys == {sub_ccy}:
delta_sub = native_delta
elif len(line_ccys) == 1:
base_sum = float(state.get("items_total") or 0)
if base_sum > 1e-9:
delta_sub = native_delta * (old_sub / base_sum)
else:
currency_hint = True
continue
else:
if len(matched) == 1 and abs(matched[0][1]) > 1e-9:
_idx, old_line, new_line = matched[0]
delta_sub = old_sub * (new_line / old_line - 1)
else:
currency_hint = True
continue
subtotals[key] = format_money(old_sub + delta_sub)
bucket_rows.update(idx for idx, _old, _new in matched)
delta += delta_sub
return {
"subtotals": subtotals,
"delta": delta,
"bucket_rows": bucket_rows,
"currency_hint": currency_hint,
}
def adjustable_rows(quote: dict[str, Any] | None) -> list[dict[str, Any]]:
"""
调价能对上的行:扁平明细,加上海运分段里的费目和小计。
@@ -873,6 +1021,20 @@ def _write_price(item: dict[str, Any], plain: str) -> None:
item["costPrice"] = plain
def _sync_line_currency(item: dict[str, Any], fee: dict[str, Any]) -> None:
"""
产品手工报价写了币种时,报价单读的那份副本必须同步行币种。
为何:模板「币别」列直接取 podCostItems / 舱位费 / 路段行的 currency。
旧逻辑只把金额盖进副本、不盖币种,结果 TMS 原币种(CNY)把产品写的 USD
吃掉,报价单成了「文件费 CNY 60.00」(2026-11-22 真机)。
空币种一律不动:没写币种时沿用原行(通常已从 TMS/表头继承),禁止在这里猜。
"""
ccy = str(fee.get("currency") or fee.get("currencyCode") or "").strip()
if ccy:
item["currency"] = ccy
def _match_fee_index(name: str, fees: list[dict[str, Any]], used: set[int]) -> int:
return next(
(
@@ -964,6 +1126,7 @@ def _stamp_fee_items(quote: dict[str, Any], fees: list[dict[str, Any]]) -> None:
continue
used.add(idx)
_write_price(item, _plain_amount(str(fees[idx].get("amount") or "")))
_sync_line_currency(item, fees[idx])
if not item.get("name"):
item["name"] = _fee_title(fees[idx])
if not item.get("feeName"):
@@ -992,10 +1155,13 @@ def _stamp_one_route(row: dict[str, Any], fees: list[dict[str, Any]]) -> None:
"""一段线路:运输费写金额,提货/送货写到对应列。"""
if len(fees) == 1:
_write_price(row, _plain_amount(str(fees[0].get("amount") or "")))
_sync_line_currency(row, fees[0])
return
transport = [fee for fee in fees if any(word in _fee_title(fee) for word in ("运输", "运费", "陆运"))]
if len(transport) == 1:
_write_price(row, _plain_amount(str(transport[0].get("amount") or "")))
# 路段行只有一个币种格:主运输费点明币种时,路段整段跟它走。
_sync_line_currency(row, transport[0])
for fee in fees:
title = _fee_title(fee)
plain = _plain_amount(str(fee.get("amount") or ""))
@@ -1015,9 +1181,12 @@ def _stamp_route_list(rows: list[Any], fees: list[dict[str, Any]]) -> None:
return
if len(legs) == len(fees):
for leg, fee in zip(legs, fees):
ccy_before = str(leg.get("currency") or "")
_write_price(leg, _plain_amount(str(fee.get("amount") or "")))
_sync_line_currency(leg, fee)
return
if len(legs) == 1:
ccy_before = str(legs[0].get("currency") or "")
_stamp_one_route(legs[0], fees)
@@ -1059,7 +1228,10 @@ def _stamp_buckets(quote: dict[str, Any], fees: list[dict[str, Any]]) -> None:
plain = _plain_amount(str(fees[idx].get("amount") or ""))
if not plain:
continue
ccy_before = str(item.get("currency") or "")
fee_ccy = str(fees[idx].get("currency") or "")
_write_price(item, plain)
_sync_line_currency(item, fees[idx])
changed = True
if changed:
_store_bucket(quote, key, items)
@@ -1100,7 +1272,9 @@ def _stamp_air_bucket_list(
continue
used.add(idx)
plain = _plain_amount(str(fees[idx].get("amount") or ""))
ccy_before = str(charge.get("currency") or "")
_write_price(charge, plain)
_sync_line_currency(charge, fees[idx])
# 单价/数量若调价行有值也盖上,模板按单价×数量重算时才对
fee = fees[idx]
up = _plain_amount(str(fee.get("unit_price") or fee.get("unitPrice") or ""))
@@ -1397,16 +1571,24 @@ def apply_adjust(
applied: list[dict[str, Any]] = []
unmatched: list[str] = []
ambiguous: list[str] = []
currency_hint = bool(multi)
# 只有真正没法落到合计的外币差额才提示「不做汇率」;海运分段有 TMS 小计可反推,不算换汇。
currency_hint = False
total_amt, _ = parse_money(src.get("total"))
new_total = total_amt
total_touched = False
total_new_value: Optional[float] = None
single_same_ccy = _single_line_same_currency(
[row for row in rows if not row.get("_subtotal_key") and not row.get("_is_total")],
total_ccy or header,
) is not None
subtotal_only: dict[str, float] = {}
touched_buckets: set[str] = set()
single_line_new: Optional[float] = None
# 调价前的小计/币种快照:改完明细后再算小计,必须靠它反推 TMS 当时的口径。
bucket_snapshot = _sea_bucket_snapshot(src)
bucket_line_delta: dict[str, float] = {}
bucket_changed_lines: dict[str, list[tuple[float, float]]] = {}
explicit_subtotal: dict[str, float] = {}
line_delta_total = 0.0
target_ccy = total_ccy or header
for item in extract.items:
spoken = (item.fee_name or "").strip()
@@ -1477,34 +1659,93 @@ def apply_adjust(
any_applied = True
bucket = str(row.get("_bucket") or "")
if row.get("_is_total"):
subtotal_only["__total__"] = new_amt
total_touched = True
total_new_value = new_amt
continue
if row.get("_subtotal_key"):
# 只改小计数字写回 fee_total_*;明细是否另调看有没有点名费目
subtotal_only[bucket] = new_amt - old_amt
# 点名小计:小计数字以销售说的为准,明细是否另调看有没有点名费目。
explicit_subtotal[bucket] = new_amt
continue
if single_same_ccy:
single_line_new = new_amt
if bucket:
touched_buckets.add(bucket)
if (not multi) and new_total is not None:
if single_same_ccy:
new_total = new_amt
else:
new_total = new_total + (new_amt - old_amt)
bucket_line_delta[bucket] = bucket_line_delta.get(bucket, 0.0) + (new_amt - old_amt)
bucket_changed_lines.setdefault(bucket, []).append((old_amt, new_amt))
continue
# 非分段行:同币种差额可直接进合计;外币没有可依据的小计,交给提示。
line_ccy_code = _ccy_code(line_ccy or prefix)
if not target_ccy or not line_ccy_code or line_ccy_code == target_ccy:
line_delta_total += new_amt - old_amt
else:
currency_hint = True
if not any_applied:
unmatched.append(spoken)
if (not multi) and new_total is not None:
if "__total__" in subtotal_only:
# 合计是整单改数;小计差额不再叠进合计,避免「合计×1.1 + 小计差额」重复
new_total = subtotal_only.pop("__total__")
# 先按段重算小计:TMS 美元段小计是人民币,用「原小计 ÷ 原明细和」反推 TMS 自己的口径,
# 不另编汇率;同币种段就是简单差额。
subtotal_delta_total = 0.0
for bucket, native_delta in bucket_line_delta.items():
if bucket in explicit_subtotal:
# 小计由销售点名,差额在下面统一算,避免明细差额再叠一次。
continue
snap = bucket_snapshot.get(bucket) or {}
old_sub = snap.get("subtotal")
sub_ccy = _ccy_code(snap.get("sub_ccy") or header)
if old_sub is None:
# 没有 TMS 小计:同币种可进合计,外币只能提示。
line_ccys = {c for c in (snap.get("line_ccys") or set()) if c}
if not target_ccy or not line_ccys or line_ccys == {target_ccy}:
subtotal_delta_total += native_delta
else:
currency_hint = True
continue
if sub_ccy and target_ccy and sub_ccy != target_ccy:
currency_hint = True
continue
line_ccys = {c for c in (snap.get("line_ccys") or set()) if c}
if not line_ccys or line_ccys == {sub_ccy}:
delta_sub = native_delta
elif len(line_ccys) == 1:
base = float(snap.get("items_total") or 0)
if base > 1e-9:
delta_sub = native_delta * (old_sub / base)
else:
currency_hint = True
continue
else:
for bucket, delta in subtotal_only.items():
if bucket not in touched_buckets:
new_total = new_total + delta
elif total_touched and "__total__" in subtotal_only:
# 多币种:点名「费用合计」仍改合计数字,不做汇率换算
new_total = subtotal_only["__total__"]
# 同一段混了多币种:只有单行调整时按该行比例缩放小计;多行无法拆分。
changed = bucket_changed_lines.get(bucket) or []
if len(changed) == 1 and abs(changed[0][0]) > 1e-9:
old_line, new_line = changed[0]
delta_sub = old_sub * (new_line / old_line - 1)
else:
currency_hint = True
continue
new_sub = old_sub + delta_sub
_set_subtotal_row(rows, bucket, new_sub)
subtotal_delta_total += delta_sub
# 点名改小计、但没点名明细的段:小计差额直接进合计。
for bucket, new_sub in explicit_subtotal.items():
snap = bucket_snapshot.get(bucket) or {}
old_sub = snap.get("subtotal")
if old_sub is None:
continue
sub_ccy = _ccy_code(snap.get("sub_ccy") or header)
if sub_ccy and target_ccy and sub_ccy != target_ccy:
currency_hint = True
continue
subtotal_delta_total += new_sub - old_sub
if total_touched and total_new_value is not None:
# 销售点名改合计:合计数字以他说的为准,小计仍按明细更新。
new_total = total_new_value
elif single_same_ccy and single_line_new is not None:
# 明细只有一项且与合计同币种:合计跟着这一项走。
new_total = single_line_new
elif new_total is not None:
new_total = new_total + subtotal_delta_total + line_delta_total
out = dict(src)
visible = []
@@ -1516,7 +1757,7 @@ def apply_adjust(
out["fee_rows"] = visible
out["fee_lines"] = visible
_write_adjusted_buckets(out, rows)
if applied and new_total is not None and new_total != total_amt and ((not multi) or total_touched):
if applied and new_total is not None and new_total != total_amt:
out["total"] = format_money(new_total, sample=str(src.get("total") or ""), currency=total_ccy)
if not applied:
code = "ambiguous" if ambiguous else "none_matched"