GUOZHI OPEN SOURCE · 实战开源脚本、模板与 AI SKILLS

开源代码库与可直接下载的实战工具

不藏私、不做概念 PPT:这里收录了我自己在物流对账、外箱压抛、凯利备货、金融物理赫斯特指数测算和跨境差评挖掘中真正跑过的 6 套开源脚本与模板。点击即可在线阅读源码、一键复制或直接下载到本地运行。

物流与供应链工程 · freight_bill_auditor.py

货代月底账单自动核销与抛重异常标红脚本

帮家里把月底 2.5 天的物流对账压到 15 分钟的核心工具。读取历史发货 CSV,自动按 /6000 或 /5000 复算理论计费重,标红货代多算抛重或单票费用偏离超过 8% 的异常运单。

  • • 零第三方依赖,仅用 Python 标准库 csv / math 即可直接运行
  • • 自动比对理论计费重 vs 货代账单重,揪出隐性多算与抛比偷换
  • • 一键输出异常明细 CSV 与当月各渠道真实单公斤成本汇总
#!/usr/bin/env python3
"""
Guozhi Open Source · Freight Bill Auditor (v1.4)
核销货代月底账单,自动检测抛重多算与单票异常加价(>8%)。

输入 CSV 字段示例:
order_id,channel,divisor,actual_kg,l_cm,w_cm,h_cm,unit_price,billed_total
"""

import csv
import sys
from collections import defaultdict


def audit_freight_bills(input_csv: str, output_csv: str = "audit_flags.csv", threshold: float = 0.08):
    flagged = []
    channel_stats = defaultdict(lambda: {"orders": 0, "actual_kg": 0.0, "paid": 0.0, "bulky": 0})

    with open(input_csv, mode="r", encoding="utf-8-sig") as f:
        reader = csv.DictReader(f)
        for row in reader:
            actual_kg = float(row["actual_kg"])
            l_cm, w_cm, h_cm = float(row["l_cm"]), float(row["w_cm"]), float(row["h_cm"])
            divisor = float(row.get("divisor") or 6000)
            unit_price = float(row["unit_price"])
            billed_total = float(row["billed_total"])

            vol_kg = (l_cm * w_cm * h_cm) / divisor
            expected_billed_kg = max(actual_kg, vol_kg)
            expected_total = expected_billed_kg * unit_price
            diff_ratio = (billed_total - expected_total) / max(expected_total, 1e-6)

            ch = row["channel"]
            channel_stats[ch]["orders"] += 1
            channel_stats[ch]["actual_kg"] += actual_kg
            channel_stats[ch]["paid"] += billed_total
            if vol_kg > actual_kg:
                channel_stats[ch]["bulky"] += 1

            if abs(diff_ratio) > threshold:
                flagged.append({
                    "order_id": row["order_id"],
                    "channel": ch,
                    "actual_kg": round(actual_kg, 2),
                    "vol_kg": round(vol_kg, 2),
                    "expected_total": round(expected_total, 2),
                    "billed_total": round(billed_total, 2),
                    "diff_pct": f"{diff_ratio * 100:+.1f}%",
                    "reason": "抛重触发+账单偏高" if vol_kg > actual_kg and diff_ratio > 0 else "疑似隐性附加费/进位偏差",
                })

    if flagged:
        with open(output_csv, mode="w", newline="", encoding="utf-8-sig") as out:
            writer = csv.DictWriter(out, fieldnames=list(flagged[0].keys()))
            writer.writeheader()
            writer.writerows(flagged)

    print("=== 渠道真实单公斤成本汇总 ===")
    for ch, st in channel_stats.items():
        real_rate = st["paid"] / max(st["actual_kg"], 1e-6)
        bulky_rate = st["bulky"] / max(st["orders"], 1) * 100
        print(f"- {ch}: {st['orders']} 票 | 实重均价 ¥{real_rate:.2f}/kg | 抛重占比 {bulky_rate:.1f}%")

    print(f"\n共标记异常运单: {len(flagged)} 票 -> 已导出至 {output_csv}")


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("用法: python freight_bill_auditor.py your_bills.csv")
    else:
        audit_freight_bills(sys.argv[1])