ERP/test_price_matching.py

149 lines
6.0 KiB
Python
Raw Normal View History

2026-02-03 05:27:32 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试对账单单价匹配功能 - 详细版本
"""
import sqlite3
import sys
import os
def test_price_matching():
"""测试价格匹配逻辑"""
# 连接数据库
conn = sqlite3.connect('server/data.db')
c = conn.cursor()
print("=== 对账单单价匹配测试 ===\n")
# 获取所有客户订单
print("1. 获取所有客户订单数据...")
c.execute('SELECT order_no, material, unit_price FROM customer_orders')
customer_orders = c.fetchall()
# 构建订单单价字典
order_prices = {}
for order in customer_orders:
order_no = order[0]
if order_no not in order_prices:
order_prices[order_no] = {}
# 处理物料名称(支持换行符分割的多个物料)
materials = str(order[1]).split('\n')
for material in materials:
material = material.strip()
if material:
order_prices[order_no][material] = order[2]
print(f" 共找到 {len(order_prices)} 个不同的合同号")
# 显示每个合同的物料列表
print("\n2. 客户订单中的合同号及物料:")
for order_no, materials in sorted(order_prices.items()):
print(f"\n 合同号: {order_no}")
for material, price in sorted(materials.items()):
print(f" - 物料: {material[:50]}... , 单价: {price}")
# 获取所有对账单
print("\n\n3. 检查对账单匹配情况...")
c.execute('SELECT id, contract_no, material_name, unit_price FROM reconciliations ORDER BY contract_no')
reconciliations = c.fetchall()
# 统计匹配情况
matched = 0
not_matched = []
for recon in reconciliations:
recon_id, contract_no, material_name, current_price = recon
new_price = None
match_type = ""
# 精确匹配合同号
if contract_no in order_prices:
# 尝试精确匹配物料名
if material_name in order_prices[contract_no]:
new_price = order_prices[contract_no][material_name]
match_type = "精确匹配"
else:
# 尝试部分匹配(物料名包含关系)
for order_material, price in order_prices[contract_no].items():
if material_name in order_material or order_material in material_name:
new_price = price
match_type = f"部分匹配(订单物料: {order_material[:30]}...)"
break
# 如果精确匹配没找到,尝试模糊匹配合同号
if new_price is None:
for order_no, materials in order_prices.items():
if contract_no in order_no or order_no in contract_no:
if material_name in materials:
new_price = materials[material_name]
match_type = f"模糊匹配合同号({order_no})"
break
else:
# 尝试部分匹配物料名
for order_material, price in materials.items():
if material_name in order_material or order_material in material_name:
new_price = price
match_type = f"模糊匹配(订单: {order_no}, 物料: {order_material[:30]}...)"
break
if new_price is not None:
break
if new_price is not None:
matched += 1
print(f" ✓ ID={recon_id}, 合同={contract_no}, 匹配类型: {match_type}")
else:
not_matched.append((recon_id, contract_no, material_name))
print(f"\n\n4. 匹配结果统计:")
print(f" 总对账单记录数: {len(reconciliations)}")
print(f" 成功匹配: {matched}")
print(f" 未匹配: {len(not_matched)}")
if not_matched:
print(f"\n5. 未匹配的记录前10条")
for recon_id, contract_no, material_name in not_matched[:10]:
print(f" ✗ ID={recon_id}, 合同号={contract_no}, 物料={material_name[:50]}...")
# 检查是否有相似的合同号
similar_orders = [o for o in order_prices.keys() if contract_no[:10] in o or o[:10] in contract_no]
if similar_orders:
print(f" → 相似合同号: {', '.join(similar_orders)}")
# 测试特定匹配案例
print(f"\n6. 测试特定匹配案例:")
test_cases = [
("CGDD002878", "AP-DZ006灯条基站"),
("CGDD002878", "WD1MK0SMD0551"),
("CGDD001850", "AP-DZ009灯条基站"),
]
for contract_no, material_name in test_cases:
print(f"\n 测试: 合同号={contract_no}, 物料={material_name}")
if contract_no in order_prices:
print(f" 找到合同号,包含 {len(order_prices[contract_no])} 个物料")
for mat, price in order_prices[contract_no].items():
if material_name in mat or mat in material_name:
print(f" ✓ 匹配成功: {mat[:50]}... -> {price}")
break
else:
print(f" ✗ 未找到匹配的物料")
else:
print(f" ✗ 未找到合同号")
# 查找相似合同号
similar = [o for o in order_prices.keys() if contract_no in o or o in contract_no]
if similar:
print(f" → 相似合同号: {similar}")
conn.close()
print("\n=== 优化建议 ===")
print("1. 确保对账单的合同号与客户订单的订单号完全一致")
print("2. 物料名称应该标准化,避免使用简写或别名")
print("3. 可以在上传发货单时自动匹配客户订单单价")
print("4. 对于无法匹配的记录,可以手动设置默认单价")
if __name__ == '__main__':
test_price_matching()