修复条码处理和数量计算问题:修复条码格式化函数,确保在数量为空时能正确计算

This commit is contained in:
侯欢 2025-05-30 12:08:06 +08:00
parent 5cf3eeed0f
commit c9afe413f5
2 changed files with 69 additions and 5 deletions

View File

@ -376,6 +376,30 @@ class ExcelProcessor:
# 应用单位转换规则 # 应用单位转换规则
product = self.unit_converter.process_unit_conversion(product) product = self.unit_converter.process_unit_conversion(product)
# 如果数量为0但单价和金额都存在计算数量 = 金额/单价
if (product['quantity'] == 0 or product['quantity'] is None) and product['price'] > 0 and product['amount']:
try:
# 确保金额是数字
if isinstance(product['amount'], str):
# 移除货币符号和非数字字符,保留数字、小数点和逗号
amount_str = re.sub(r'[^\d\.,]', '', product['amount'].strip())
# 替换逗号为小数点(如果逗号作为小数分隔符)
if ',' in amount_str and '.' not in amount_str:
amount_str = amount_str.replace(',', '.')
# 处理既有逗号又有小数点的情况(通常逗号是千位分隔符)
elif ',' in amount_str and '.' in amount_str:
amount_str = amount_str.replace(',', '')
amount = float(amount_str)
else:
amount = float(product['amount'])
# 计算数量
if amount > 0:
quantity = amount / product['price']
logger.info(f"数量为空或为0通过金额({amount})和单价({product['price']})计算得出数量: {quantity}")
product['quantity'] = quantity
except Exception as e:
logger.warning(f"通过金额和单价计算数量失败: {e}")
products.append(product) products.append(product)
except Exception as e: except Exception as e:
logger.error(f"提取第{idx+1}行商品信息时出错: {e}", exc_info=True) logger.error(f"提取第{idx+1}行商品信息时出错: {e}", exc_info=True)
@ -421,6 +445,31 @@ class ExcelProcessor:
# 获取数量和单价 # 获取数量和单价
quantity = product.get('quantity', 0) quantity = product.get('quantity', 0)
price = product.get('price', 0) price = product.get('price', 0)
amount = product.get('amount', 0)
# 如果数量为0但单价和金额都存在计算数量 = 金额/单价
if (quantity == 0 or quantity is None) and price > 0 and amount:
try:
# 确保金额是数字
if isinstance(amount, str):
# 移除货币符号和非数字字符,保留数字、小数点和逗号
amount_str = re.sub(r'[^\d\.,]', '', amount.strip())
# 替换逗号为小数点(如果逗号作为小数分隔符)
if ',' in amount_str and '.' not in amount_str:
amount_str = amount_str.replace(',', '.')
# 处理既有逗号又有小数点的情况(通常逗号是千位分隔符)
elif ',' in amount_str and '.' in amount_str:
amount_str = amount_str.replace(',', '')
amount = float(amount_str)
else:
amount = float(amount)
# 计算数量
if amount > 0:
quantity = amount / price
logger.info(f"数量为空或为0通过金额({amount})和单价({price})计算得出数量: {quantity}")
product['quantity'] = quantity
except Exception as e:
logger.warning(f"通过金额和单价计算数量失败: {e}")
# 判断是否为赠品价格为0 # 判断是否为赠品价格为0
is_gift = price == 0 is_gift = price == 0

View File

@ -202,12 +202,27 @@ def format_barcode(barcode: Any) -> str:
Returns: Returns:
格式化后的条码字符串 格式化后的条码字符串
""" """
if isinstance(barcode, (int, float)) or is_scientific_notation(str(barcode)): if barcode is None:
return ""
# 先转为字符串
barcode_str = str(barcode).strip()
# 判断是否为科学计数法
if is_scientific_notation(barcode_str):
try: try:
# 转换为整数并格式化为字符串 # 科学计数法转为普通数字字符串
return f"{int(float(barcode))}" barcode_str = f"{float(barcode_str):.0f}"
except (ValueError, TypeError): except (ValueError, TypeError):
pass pass
# 如果不是数字或转换失败,返回原始字符串 # 移除可能的小数部分(如"123456.0"变为"123456"
return str(barcode) if '.' in barcode_str:
barcode_str = re.sub(r'\.0+$', '', barcode_str)
# 确保是纯数字字符串
if not barcode_str.isdigit():
# 只保留数字字符
barcode_str = re.sub(r'\D', '', barcode_str)
return barcode_str