fix: 修复月份选择器问题,确保12月正确显示
- 修复跨年月份计算逻辑(1月时正确计算为去年12月) - 改进_get_month_list()方法,生成正确的近12个月列表 - 增加Combobox宽度以完整显示月份值如'2025-12' - 优化手动剔除次月多统计的船对话框
This commit is contained in:
@@ -104,7 +104,7 @@ class DailyLogsDatabase(DatabaseBase):
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS manual_adjustments (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
date TEXT NOT NULL, -- 调整适用的日期
|
||||
date TEXT NOT NULL, -- 调整适用的日期(目标日期)
|
||||
ship_name TEXT NOT NULL, -- 船名
|
||||
teu INTEGER NOT NULL, -- TEU数量
|
||||
twenty_feet INTEGER DEFAULT 0, -- 20尺箱量
|
||||
@@ -115,6 +115,23 @@ class DailyLogsDatabase(DatabaseBase):
|
||||
)
|
||||
''')
|
||||
|
||||
# 检查是否需要添加新字段
|
||||
cursor.execute("PRAGMA table_info(manual_adjustments)")
|
||||
columns = [col[1] for col in cursor.fetchall()]
|
||||
|
||||
# 添加缺失的字段
|
||||
if 'source_date' not in columns:
|
||||
cursor.execute('ALTER TABLE manual_adjustments ADD COLUMN source_date TEXT')
|
||||
logger.info("已添加 source_date 字段到 manual_adjustments 表")
|
||||
|
||||
if 'reason' not in columns:
|
||||
cursor.execute('ALTER TABLE manual_adjustments ADD COLUMN reason TEXT')
|
||||
logger.info("已添加 reason 字段到 manual_adjustments 表")
|
||||
|
||||
if 'status' not in columns:
|
||||
cursor.execute('ALTER TABLE manual_adjustments ADD COLUMN status TEXT DEFAULT "pending"')
|
||||
logger.info("已添加 status 字段到 manual_adjustments 表")
|
||||
|
||||
# 创建索引
|
||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_manual_date ON manual_adjustments(date)')
|
||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_manual_type ON manual_adjustments(adjustment_type)')
|
||||
@@ -409,18 +426,23 @@ class DailyLogsDatabase(DatabaseBase):
|
||||
|
||||
def insert_manual_adjustment(self, date: str, ship_name: str, teu: int,
|
||||
twenty_feet: int = 0, forty_feet: int = 0,
|
||||
adjustment_type: str = 'add', note: str = '') -> bool:
|
||||
adjustment_type: str = 'add', note: str = '',
|
||||
source_date: str = None, reason: str = '',
|
||||
status: str = 'pending') -> bool:
|
||||
"""
|
||||
插入手动调整数据
|
||||
|
||||
参数:
|
||||
date: 日期字符串
|
||||
date: 日期字符串(目标日期)
|
||||
ship_name: 船名
|
||||
teu: TEU数量
|
||||
twenty_feet: 20尺箱量
|
||||
forty_feet: 40尺箱量
|
||||
adjustment_type: 调整类型 'add' 或 'exclude'
|
||||
note: 备注
|
||||
source_date: 源日期(上月底日期,可选)
|
||||
reason: 调整原因
|
||||
status: 调整状态:'pending', 'processed'
|
||||
|
||||
返回:
|
||||
是否成功
|
||||
@@ -428,10 +450,12 @@ class DailyLogsDatabase(DatabaseBase):
|
||||
try:
|
||||
query = '''
|
||||
INSERT INTO manual_adjustments
|
||||
(date, ship_name, teu, twenty_feet, forty_feet, adjustment_type, note, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
(date, source_date, ship_name, teu, twenty_feet, forty_feet,
|
||||
adjustment_type, note, reason, status, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
'''
|
||||
params = (date, ship_name, teu, twenty_feet, forty_feet, adjustment_type, note)
|
||||
params = (date, source_date, ship_name, teu, twenty_feet, forty_feet,
|
||||
adjustment_type, note, reason, status)
|
||||
self.execute_update(query, params)
|
||||
logger.info(f"插入手动调整数据: {date} {ship_name} {teu}TEU ({adjustment_type})")
|
||||
return True
|
||||
@@ -473,6 +497,143 @@ class DailyLogsDatabase(DatabaseBase):
|
||||
'''
|
||||
return self.execute_query(query, (date, adjustment_type))
|
||||
|
||||
def get_cross_month_adjustments(self, source_date: str = None, target_date: str = None,
|
||||
status: str = None) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
获取跨月调整数据
|
||||
|
||||
参数:
|
||||
source_date: 源日期(上月底日期)
|
||||
target_date: 目标日期(次月日期)
|
||||
status: 调整状态
|
||||
|
||||
返回:
|
||||
跨月调整数据列表
|
||||
"""
|
||||
try:
|
||||
conditions = []
|
||||
params = []
|
||||
|
||||
if source_date:
|
||||
conditions.append("source_date = ?")
|
||||
params.append(source_date)
|
||||
|
||||
if target_date:
|
||||
conditions.append("date = ?")
|
||||
params.append(target_date)
|
||||
|
||||
if status:
|
||||
conditions.append("status = ?")
|
||||
params.append(status)
|
||||
|
||||
where_clause = " AND ".join(conditions) if conditions else "1=1"
|
||||
query = f'''
|
||||
SELECT * FROM manual_adjustments
|
||||
WHERE {where_clause} ORDER BY created_at DESC
|
||||
'''
|
||||
|
||||
return self.execute_query(query, tuple(params))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取跨月调整数据失败: {e}")
|
||||
return []
|
||||
|
||||
def get_pending_cross_month_adjustments(self) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
获取待处理的跨月调整数据
|
||||
|
||||
返回:
|
||||
待处理的跨月调整数据列表
|
||||
"""
|
||||
return self.get_cross_month_adjustments(status='pending')
|
||||
|
||||
def update_adjustment_status(self, adjustment_id: int, status: str) -> bool:
|
||||
"""
|
||||
更新调整状态
|
||||
|
||||
参数:
|
||||
adjustment_id: 调整记录ID
|
||||
status: 新状态
|
||||
|
||||
返回:
|
||||
是否成功
|
||||
"""
|
||||
try:
|
||||
query = 'UPDATE manual_adjustments SET status = ? WHERE id = ?'
|
||||
result = self.execute_update(query, (status, adjustment_id))
|
||||
if result > 0:
|
||||
logger.info(f"更新调整状态: ID={adjustment_id} -> {status}")
|
||||
return True
|
||||
else:
|
||||
logger.warning(f"未找到调整记录: ID={adjustment_id}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"更新调整状态失败: {e}")
|
||||
return False
|
||||
|
||||
def insert_cross_month_exclusion(self, source_date: str, target_date: str,
|
||||
ship_name: str, teu: int,
|
||||
twenty_feet: int = 0, forty_feet: int = 0,
|
||||
reason: str = '') -> bool:
|
||||
"""
|
||||
插入跨月剔除调整(手动剔除次月多统计的船)
|
||||
|
||||
参数:
|
||||
source_date: 源日期(上月底日期)
|
||||
target_date: 目标日期(次月日期)
|
||||
ship_name: 船名
|
||||
teu: TEU数量
|
||||
twenty_feet: 20尺箱量
|
||||
forty_feet: 40尺箱量
|
||||
reason: 调整原因
|
||||
|
||||
返回:
|
||||
是否成功
|
||||
"""
|
||||
try:
|
||||
# 1. 插入剔除记录(从月底最后一天扣除)
|
||||
exclude_success = self.insert_manual_adjustment(
|
||||
date=source_date,
|
||||
source_date=source_date,
|
||||
ship_name=ship_name,
|
||||
teu=teu,
|
||||
twenty_feet=twenty_feet,
|
||||
forty_feet=forty_feet,
|
||||
adjustment_type='exclude',
|
||||
note=f"手动剔除次月多统计的船,目标日期: {target_date}",
|
||||
reason=reason,
|
||||
status='pending'
|
||||
)
|
||||
|
||||
if not exclude_success:
|
||||
return False
|
||||
|
||||
# 2. 自动将相同数据添加到次月1号
|
||||
add_success = self.insert_manual_adjustment(
|
||||
date=target_date,
|
||||
source_date=source_date,
|
||||
ship_name=ship_name,
|
||||
teu=teu,
|
||||
twenty_feet=twenty_feet,
|
||||
forty_feet=forty_feet,
|
||||
adjustment_type='add',
|
||||
note=f"从{source_date}转移的数据: {reason}",
|
||||
reason=reason,
|
||||
status='pending'
|
||||
)
|
||||
|
||||
if add_success:
|
||||
logger.info(f"插入跨月剔除调整: {source_date} -> {target_date} {ship_name} {teu}TEU")
|
||||
return True
|
||||
else:
|
||||
logger.error(f"插入跨月剔除调整失败: 添加数据到次月1号失败")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"插入跨月剔除调整失败: {e}")
|
||||
return False
|
||||
|
||||
def delete_manual_adjustment(self, adjustment_id: int) -> bool:
|
||||
"""
|
||||
删除指定ID的手动调整数据
|
||||
|
||||
Reference in New Issue
Block a user