#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试发货单上传解析功能 """ import pandas as pd import numpy as np def test_parse_shipment(): """测试解析发货单""" file_path = '/home/hyx/work/生产管理系统/发货单-20251121.xls' # 读取Excel文件 df = pd.read_excel(file_path, header=None) print("=== 解析发货单头部信息 ===") # 提取头部信息 shipment_date = None transport_method = None # 解析发货日期(第1行,索引2) if len(df) > 1 and len(df.columns) > 2: shipment_date_raw = df.iloc[1, 2] if pd.notna(shipment_date_raw): if isinstance(shipment_date_raw, pd.Timestamp): shipment_date = shipment_date_raw.strftime('%Y-%m-%d') else: shipment_date = str(shipment_date_raw) print(f"发货日期: {shipment_date}") # 解析供货方式(第2行,索引2) if len(df) > 2 and len(df.columns) > 2: transport_method_raw = df.iloc[2, 2] if pd.notna(transport_method_raw): transport_method = str(transport_method_raw) print(f"供货方式(运输单号): {transport_method}") # 找到表格数据的起始行(序号、采购单号、物料编码...) header_row = None for i in range(len(df)): if df.iloc[i, 0] == '序号': header_row = i break if header_row is None: print("错误:无法识别发货单格式,未找到表格头部") return print(f"\n表格起始行: {header_row}") # 从表格起始行读取数据 data_df = pd.read_excel(file_path, header=header_row) print(f"表格列名: {data_df.columns.tolist()}") # 过滤掉合计行和备注行(只保留序号为数字的行) valid_data = data_df[data_df['序号'].apply(lambda x: isinstance(x, (int, float)) and not pd.isna(x))] print(f"\n有效数据行数: {len(valid_data)}") # 模拟客户订单数据 customer_orders = { 'CGDD002878': { 'order_date': '2025-11-15', 'material': 'AP-DZ006 灯条基站', 'unit_price': 150.5 }, 'CGDD003082': { 'order_date': '2025-11-18', 'material': '飞机盒', 'unit_price': 5.0 } } print("\n=== 解析数据行 ===") last_contract_no = None for idx, row in valid_data.iterrows(): print(f"\n序号: {int(row['序号'])}") # 提取数据 contract_no = row.get('采购单号') if pd.isna(contract_no): if last_contract_no: contract_no = last_contract_no print(f" 采购单号: {contract_no} (继承上一行)") else: print(f" 采购单号: 空 (错误)") continue else: contract_no = str(contract_no).strip() last_contract_no = contract_no print(f" 采购单号(合同编号): {contract_no}") material_code = row.get('物料编码') if pd.isna(material_code): print(f" 物料编码: 空 (错误)") continue material_code = str(material_code).strip().replace('\n', ' ') print(f" 物料编码(物料名称): {material_code}") spec_model = row.get('规格型号') if pd.isna(spec_model): spec_model = '' else: spec_model = str(spec_model).strip() print(f" 规格型号: {spec_model}") quantity = row.get('实送数量') if pd.isna(quantity): print(f" 实送数量: 空 (错误)") continue quantity = int(float(quantity)) print(f" 实送数量(数量): {quantity}") unit = row.get('单位') if pd.isna(unit): unit = 'pcs' else: unit = str(unit).strip() print(f" 单位: {unit}") # 从备注中提取运输单号(如果有) remark = row.get('备注') transport_no = transport_method or '' if pd.notna(remark): remark_str = str(remark).strip() if remark_str: transport_no = remark_str print(f" 运输单号: {transport_no}") # 从客户订单中查找单价和下单时间 unit_price = 0 order_date = shipment_date or '' if contract_no in customer_orders: order_info = customer_orders[contract_no] # 匹配物料名称 if material_code in order_info['material']: unit_price = order_info['unit_price'] order_date = order_info['order_date'] print(f" 含税单价: {unit_price} (从客户订单查找)") print(f" 下单时间: {order_date} (从客户订单查找)") else: print(f" 含税单价: {unit_price} (未找到匹配的客户订单)") print(f" 下单时间: {order_date} (使用发货日期)") else: print(f" 含税单价: {unit_price} (未找到对应的采购单号)") print(f" 下单时间: {order_date} (使用发货日期)") # 计算含税金额 total_amount = quantity * unit_price print(f" 含税金额: {total_amount}") print(f" 交货日期: {shipment_date}") print(f" 出货日期: {shipment_date}") if __name__ == '__main__': test_parse_shipment()