提交 6281d9d9 authored 作者: 伍姿英's avatar 伍姿英

Merge branch 'release/3.14.3'

......@@ -28,9 +28,64 @@ class PackageDataWizard(models.TransientModel):
required=True
)
def _parse_and_convert_tz(self, val):
# def _parse_and_convert_tz(self, val):
# """
# 将任意时间格式(字符串/日期/时间/Excel数字/时间戳)转为 0时区 (UTC) 时间
# """
# if val is None or val == '':
# return False
#
# dt = False
#
# if isinstance(val, (int, float)):
# if val > 1000000:
# if val > 100000000000:
# val = val / 1000.0
# try:
# dt = datetime.fromtimestamp(val)
# except Exception:
# return False
# else:
# try:
# dt = datetime(1899, 12, 30) + fields.Datetime.context_timestamp(self,
# datetime.now()).tzinfo.utcoffset(
# None) * 0
# from datetime import timedelta
# dt = datetime(1899, 12, 30) + timedelta(days=val)
# except Exception:
# return False
# elif isinstance(val, str):
# try:
# # 优先尝试框架原生的标准解析,效率高
# dt = fields.Datetime.from_string(val)
# except (ValueError, AttributeError):
# try:
# # 原生解析失败时,使用 dateutil 兜底解析 "2026/6/25" 等非标格式
# dt = parser.parse(val)
# except Exception:
# return False
# elif isinstance(val, datetime):
# dt = val
# elif isinstance(val, date):
# dt = datetime.combine(val, datetime.min.time())
#
# if not dt:
# return False
#
# local_tz = pytz.timezone(self.timezone)
#
# if dt.tzinfo is None:
# local_dt = local_tz.localize(dt)
# else:
# local_dt = dt.astimezone(local_tz)
#
# utc_dt = local_dt.astimezone(pytz.UTC).replace(tzinfo=None)
# return utc_dt
def _parse_and_convert_tz(self, val, date_only=False):
"""
将任意时间格式(字符串/日期/时间/Excel数字/时间戳)转为 0时区 (UTC) 时间
将任意时间格式(字符串/日期/时间/Excel数字/时间戳)转为 0时区 (UTC) 时间。
如果 date_only=True,则仅解析并返回本地日期,不进行时区转换。
"""
if val is None or val == '':
return False
......@@ -47,20 +102,16 @@ class PackageDataWizard(models.TransientModel):
return False
else:
try:
dt = datetime(1899, 12, 30) + fields.Datetime.context_timestamp(self,
datetime.now()).tzinfo.utcoffset(
None) * 0
# 简化 Excel 数值日期的计算
from datetime import timedelta
dt = datetime(1899, 12, 30) + timedelta(days=val)
except Exception:
return False
elif isinstance(val, str):
try:
# 优先尝试框架原生的标准解析,效率高
dt = fields.Datetime.from_string(val)
except (ValueError, AttributeError):
try:
# 原生解析失败时,使用 dateutil 兜底解析 "2026/6/25" 等非标格式
dt = parser.parse(val)
except Exception:
return False
......@@ -72,6 +123,10 @@ class PackageDataWizard(models.TransientModel):
if not dt:
return False
# 【新增】:如果是纯日期,直接返回,跳过后面的时区转换
if date_only:
return dt.replace(hour=0, minute=0, second=0, microsecond=0)
local_tz = pytz.timezone(self.timezone)
if dt.tzinfo is None:
......@@ -172,20 +227,23 @@ class PackageDataWizard(models.TransientModel):
tally_utc = self._parse_and_convert_tz(raw_tally)
delivery_utc = self._parse_and_convert_tz(raw_delivery)
pallet_date_utc = self._parse_and_convert_tz(raw_pallet_date)
# pallet_date_utc = self._parse_and_convert_tz(raw_pallet_date)
pallet_date_parsed = self._parse_and_convert_tz(raw_pallet_date, date_only=True)
if tally_utc:
row_data_dict['理货时间'] = tally_utc.strftime('%Y-%m-%d %H:%M:%S')
if delivery_utc:
row_data_dict['尾程交货时间'] = delivery_utc.strftime('%Y-%m-%d %H:%M:%S')
if pallet_date_utc:
row_data_dict['托盘使用日期'] = pallet_date_utc.strftime('%Y-%m-%d %H:%M:%S')
# if pallet_date_utc:
# row_data_dict['托盘使用日期'] = pallet_date_utc.strftime('%Y-%m-%d %H:%M:%S')
if pallet_date_parsed:
# 【修改】:纯日期格式直接写入 年-月-日
row_data_dict['托盘使用日期'] = pallet_date_parsed.strftime('%Y-%m-%d')
# 提取人员与托盘号文本(防呆:去空格判断)
tally_person = row_data_dict.get('理货人')
delivery_person = row_data_dict.get('尾程交货人')
pallet_no = row_data_dict.get('托盘号')
# print(row_data_dict)
tally_person_str = str(tally_person).strip() if tally_person else ''
delivery_person_str = str(delivery_person).strip() if delivery_person else ''
pallet_no_str = str(pallet_no).strip() if pallet_no else ''
......@@ -236,9 +294,13 @@ class PackageDataWizard(models.TransientModel):
# print(raw_pallet_date, pallet_date_utc, now_utc)
if not raw_pallet_date:
row_errors.append("托盘使用日期不能为空")
elif pallet_date_utc and pallet_date_utc > now_utc:
row_errors.append("托盘使用日期不能是未来日期")
# elif pallet_date_utc and pallet_date_utc > now_utc:
# row_errors.append("托盘使用日期不能是未来日期")
elif pallet_date_parsed:
# 【修改】:获取用户当地时区的“今天”日期,比较年月日
local_today = datetime.now(pytz.timezone(self.timezone)).date()
if pallet_date_parsed.date() > local_today:
row_errors.append("托盘使用日期不能是未来日期")
# -----------------------------------------------------
# 原逻辑:理货时间、尾程交货时间不能是未来时间
# -----------------------------------------------------
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论