From c9afe413f59477381b445a7cbaf0375bf976745c Mon Sep 17 00:00:00 2001 From: houhuan Date: Fri, 30 May 2025 12:08:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9D=A1=E7=A0=81=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=92=8C=E6=95=B0=E9=87=8F=E8=AE=A1=E7=AE=97=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9A=E4=BF=AE=E5=A4=8D=E6=9D=A1=E7=A0=81=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E5=87=BD=E6=95=B0=EF=BC=8C=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E5=9C=A8=E6=95=B0=E9=87=8F=E4=B8=BA=E7=A9=BA=E6=97=B6=E8=83=BD?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/excel/processor.py | 49 ++++++++++++++++++++++++++++++++++ app/core/utils/string_utils.py | 25 +++++++++++++---- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/app/core/excel/processor.py b/app/core/excel/processor.py index 2971fd6..dd7fc84 100644 --- a/app/core/excel/processor.py +++ b/app/core/excel/processor.py @@ -376,6 +376,30 @@ class ExcelProcessor: # 应用单位转换规则 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) except Exception as e: logger.error(f"提取第{idx+1}行商品信息时出错: {e}", exc_info=True) @@ -421,6 +445,31 @@ class ExcelProcessor: # 获取数量和单价 quantity = product.get('quantity', 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) is_gift = price == 0 diff --git a/app/core/utils/string_utils.py b/app/core/utils/string_utils.py index 95fc5e9..b8d5a3e 100644 --- a/app/core/utils/string_utils.py +++ b/app/core/utils/string_utils.py @@ -202,12 +202,27 @@ def format_barcode(barcode: Any) -> str: 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: - # 转换为整数并格式化为字符串 - return f"{int(float(barcode))}" + # 科学计数法转为普通数字字符串 + barcode_str = f"{float(barcode_str):.0f}" except (ValueError, TypeError): pass - # 如果不是数字或转换失败,返回原始字符串 - return str(barcode) \ No newline at end of file + # 移除可能的小数部分(如"123456.0"变为"123456") + 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 \ No newline at end of file