增加入库记录页面,同时修正三组入库的时间问题
This commit is contained in:
@ -32,6 +32,9 @@ class ProductInboundService:
|
||||
traceback.print_exc()
|
||||
return []
|
||||
|
||||
# ============================================================
|
||||
# 2. 新增入库逻辑 (修改:精确到时间)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def handle_inbound(data):
|
||||
from app.models.inbound.product import StockProduct
|
||||
@ -42,14 +45,21 @@ class ProductInboundService:
|
||||
material = MaterialBase.query.get(base_id)
|
||||
if not material: raise ValueError("物料不存在")
|
||||
|
||||
in_date_val = datetime.utcnow().date()
|
||||
# [核心修改] 处理 production_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)
|
||||
|
||||
@ -65,7 +75,6 @@ class ProductInboundService:
|
||||
generated_sku = str(next_global_id).zfill(10)
|
||||
final_barcode = data.get('barcode') or generated_sku
|
||||
|
||||
# 处理三个图片/链接列表
|
||||
photo_list = data.get('product_photo', [])
|
||||
quality_list = data.get('quality_report_link', [])
|
||||
inspection_list = data.get('inspection_report_link', [])
|
||||
@ -78,7 +87,7 @@ class ProductInboundService:
|
||||
base_id=material.id,
|
||||
global_print_id=next_global_id,
|
||||
sku=generated_sku,
|
||||
production_date=in_date_val,
|
||||
production_date=in_date_val, # 存入 DateTime
|
||||
barcode=final_barcode,
|
||||
serial_number=data.get('serial_number'),
|
||||
|
||||
@ -100,7 +109,6 @@ class ProductInboundService:
|
||||
|
||||
quality_status=data.get('quality_status', '合格'),
|
||||
|
||||
# 存为 JSON
|
||||
product_photo=json.dumps(photo_list),
|
||||
quality_report_link=json.dumps(quality_list),
|
||||
inspection_report_link=json.dumps(inspection_list),
|
||||
@ -136,7 +144,6 @@ class ProductInboundService:
|
||||
for f in fields:
|
||||
if f in data: setattr(stock, f, data[f])
|
||||
|
||||
# 更新 JSON 字段
|
||||
if 'product_photo' in data:
|
||||
imgs = data['product_photo']
|
||||
if isinstance(imgs, list): stock.product_photo = json.dumps(imgs)
|
||||
@ -188,9 +195,6 @@ class ProductInboundService:
|
||||
db.session.rollback()
|
||||
raise e
|
||||
|
||||
# ============================================================
|
||||
# 获取出库流转历史 (与 Buy 逻辑一致,关联 TransOutbound 表)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def get_outbound_history(stock_id):
|
||||
"""获取出库历史"""
|
||||
@ -203,7 +207,7 @@ class ProductInboundService:
|
||||
return []
|
||||
|
||||
# ============================================================
|
||||
# 获取列表 (包含状态筛选与零库存隐藏逻辑)
|
||||
# 获取列表 (修改:按时间倒序排序 + 展示只显示日期)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def get_list(page, limit, keyword=None, statuses=None):
|
||||
@ -211,7 +215,6 @@ class ProductInboundService:
|
||||
try:
|
||||
query = db.session.query(StockProduct).outerjoin(MaterialBase, StockProduct.base_id == MaterialBase.id)
|
||||
|
||||
# 1. 关键词搜索
|
||||
if keyword:
|
||||
query = query.filter(or_(
|
||||
MaterialBase.name.ilike(f'%{keyword}%'),
|
||||
@ -222,11 +225,9 @@ class ProductInboundService:
|
||||
StockProduct.sku.ilike(f'%{keyword}%')
|
||||
))
|
||||
|
||||
# 2. 状态筛选与零库存隐藏逻辑
|
||||
if not statuses:
|
||||
statuses = ['在库', '借库']
|
||||
|
||||
# 如果筛选包含'已出库',则显示所有数量;否则隐藏 stock_quantity <= 0 的记录
|
||||
if '已出库' in statuses:
|
||||
query = query.filter(StockProduct.status.in_(statuses))
|
||||
else:
|
||||
@ -237,7 +238,9 @@ class ProductInboundService:
|
||||
)
|
||||
)
|
||||
|
||||
pagination = query.order_by(StockProduct.id.desc()).paginate(page=page, per_page=limit, error_out=False)
|
||||
# [核心修改] 按照 production_date (入库日期) 倒序排序
|
||||
pagination = query.order_by(StockProduct.production_date.desc()).paginate(page=page, per_page=limit,
|
||||
error_out=False)
|
||||
|
||||
current_items = pagination.items
|
||||
|
||||
@ -252,20 +255,23 @@ class ProductInboundService:
|
||||
for item in current_items:
|
||||
d = item.to_dict()
|
||||
|
||||
# 直接使用当前行的库存,不再聚合
|
||||
# [核心修改] 格式化日期
|
||||
date_display = ''
|
||||
if item.production_date:
|
||||
try:
|
||||
date_display = item.production_date.strftime('%Y-%m-%d')
|
||||
except:
|
||||
date_display = str(item.production_date)[:10]
|
||||
d['inbound_date'] = date_display
|
||||
|
||||
d['qty_stock'] = float(item.stock_quantity or 0)
|
||||
d['qty_available'] = float(item.available_quantity or 0)
|
||||
|
||||
# 兼容前端字段 key
|
||||
d['sum_stock'] = d['qty_stock']
|
||||
d['sum_available'] = d['qty_available']
|
||||
|
||||
# 图片/链接解析
|
||||
d['product_photo'] = parse_img(item.product_photo)
|
||||
d['quality_report_link'] = parse_img(item.quality_report_link)
|
||||
d['inspection_report_link'] = parse_img(item.inspection_report_link)
|
||||
|
||||
# 打印ID
|
||||
d['global_print_id'] = item.global_print_id
|
||||
|
||||
items.append(d)
|
||||
|
||||
Reference in New Issue
Block a user