增加入库记录页面,同时修正三组入库的时间问题
This commit is contained in:
@ -40,7 +40,7 @@ class BuyInboundService:
|
||||
return []
|
||||
|
||||
# ============================================================
|
||||
# 2. 新增入库逻辑
|
||||
# 2. 新增入库逻辑 (修改:精确到时间)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def handle_inbound(data):
|
||||
@ -51,14 +51,24 @@ class BuyInboundService:
|
||||
material = MaterialBase.query.get(base_id)
|
||||
if not material: raise ValueError("物料不存在")
|
||||
|
||||
in_date_val = datetime.utcnow().date()
|
||||
# [核心修改] 默认使用当前时间(含时分秒),不再截取 .date()
|
||||
current_time = datetime.now()
|
||||
in_date_val = current_time
|
||||
|
||||
if data.get('in_date'):
|
||||
try:
|
||||
date_str = str(data['in_date'])
|
||||
if len(date_str) > 10: date_str = date_str[:10]
|
||||
in_date_val = datetime.strptime(date_str, '%Y-%m-%d').date()
|
||||
# 如果前端传了时分秒,尝试直接解析
|
||||
if len(date_str) > 10:
|
||||
in_date_val = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
|
||||
else:
|
||||
# 如果只传了日期,手动补上当前时间的时分秒,保证同日入库的排序正确
|
||||
d_temp = datetime.strptime(date_str, '%Y-%m-%d')
|
||||
in_date_val = datetime(d_temp.year, d_temp.month, d_temp.day,
|
||||
current_time.hour, current_time.minute, current_time.second)
|
||||
except:
|
||||
pass
|
||||
# 解析失败则使用当前时间作为兜底
|
||||
in_date_val = current_time
|
||||
|
||||
in_qty = float(data.get('in_quantity') or 0)
|
||||
u_price = float(data.get('unit_price') or 0)
|
||||
@ -80,10 +90,10 @@ class BuyInboundService:
|
||||
global_print_id=next_global_id,
|
||||
sku=generated_sku,
|
||||
barcode=final_barcode,
|
||||
in_date=in_date_val,
|
||||
in_date=in_date_val, # 存入 DateTime 对象
|
||||
serial_number=data.get('serial_number'),
|
||||
batch_number=data.get('batch_number'),
|
||||
status=data.get('status', '在库'), # 默认在库
|
||||
status=data.get('status', '在库'),
|
||||
in_quantity=in_qty,
|
||||
stock_quantity=in_qty,
|
||||
available_quantity=in_qty,
|
||||
@ -185,7 +195,7 @@ class BuyInboundService:
|
||||
return []
|
||||
|
||||
# ============================================================
|
||||
# 6. 获取列表 (核心逻辑修改)
|
||||
# 6. 获取列表 (修改:按时间倒序排序 + 展示只显示日期)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def get_list(page, limit, keyword=None, statuses=None):
|
||||
@ -196,7 +206,7 @@ class BuyInboundService:
|
||||
try:
|
||||
query = db.session.query(StockBuy).outerjoin(MaterialBase, StockBuy.base_id == MaterialBase.id)
|
||||
|
||||
# 1. 关键词搜索 (覆盖所有关键字段)
|
||||
# 1. 关键词搜索
|
||||
if keyword:
|
||||
kw = f'%{keyword}%'
|
||||
query = query.filter(
|
||||
@ -210,24 +220,13 @@ class BuyInboundService:
|
||||
)
|
||||
)
|
||||
|
||||
# 2. 状态筛选与零库存隐藏逻辑
|
||||
# 用户要求:
|
||||
# - 默认显示:'在库', '借库'。
|
||||
# - 零库存规则:库存为0时,不在页面显示(除非筛选了'已出库')。
|
||||
|
||||
# 2. 状态筛选
|
||||
if not statuses:
|
||||
# 默认情况:只查 '在库' 和 '借库'
|
||||
statuses = ['在库', '借库']
|
||||
|
||||
# 构建筛选条件
|
||||
# 如果筛选条件中 包含 '已出库',则允许显示 stock_quantity >= 0 (即显示所有)
|
||||
# 如果筛选条件中 不包含 '已出库',则强制要求 stock_quantity > 0 (隐藏零库存)
|
||||
|
||||
if '已出库' in statuses:
|
||||
# 用户想看已出库的,直接按状态查,不做数量限制
|
||||
query = query.filter(StockBuy.status.in_(statuses))
|
||||
else:
|
||||
# 用户不想看已出库的,按状态查 AND 数量必须 > 0
|
||||
query = query.filter(
|
||||
and_(
|
||||
StockBuy.status.in_(statuses),
|
||||
@ -235,7 +234,8 @@ class BuyInboundService:
|
||||
)
|
||||
)
|
||||
|
||||
pagination = query.order_by(StockBuy.id.desc()).paginate(page=page, per_page=limit, error_out=False)
|
||||
# [核心修改] 按照入库时间倒序排序 (从近到远)
|
||||
pagination = query.order_by(StockBuy.in_date.desc()).paginate(page=page, per_page=limit, error_out=False)
|
||||
current_items = pagination.items
|
||||
|
||||
def parse_img(json_str):
|
||||
@ -247,10 +247,17 @@ class BuyInboundService:
|
||||
|
||||
items = []
|
||||
for item in current_items:
|
||||
# 获取单行数据,不再进行聚合计算
|
||||
qty_stock = float(item.stock_quantity or 0)
|
||||
qty_avail = float(item.available_quantity or 0)
|
||||
|
||||
# [核心修改] 格式化展示日期,去掉时分秒
|
||||
date_display = ''
|
||||
if item.in_date:
|
||||
try:
|
||||
date_display = item.in_date.strftime('%Y-%m-%d')
|
||||
except:
|
||||
date_display = str(item.in_date)[:10]
|
||||
|
||||
d = {
|
||||
'id': item.id,
|
||||
'base_id': item.base_id,
|
||||
@ -261,7 +268,7 @@ class BuyInboundService:
|
||||
'material_type': item.material.material_type if item.material else '',
|
||||
|
||||
'sku': item.sku,
|
||||
'inbound_date': str(item.in_date) if item.in_date else '',
|
||||
'inbound_date': date_display, # 前端展示用的日期字符串
|
||||
'barcode': item.barcode,
|
||||
'serial_number': item.serial_number,
|
||||
'batch_number': item.batch_number,
|
||||
@ -273,8 +280,6 @@ class BuyInboundService:
|
||||
'qty_stock': qty_stock,
|
||||
'qty_available': qty_avail,
|
||||
|
||||
# 解除挂钩:不再返回所有批次的总和,直接返回当前批次的数量
|
||||
# 为了兼容前端字段名,这里直接用当前行数量填充
|
||||
'sum_stock': qty_stock,
|
||||
'sum_available': qty_avail,
|
||||
|
||||
|
||||
Reference in New Issue
Block a user