半成品入库与出库相关联
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
from app.extensions import db
|
||||
from app.models.base import MaterialBase
|
||||
from datetime import datetime
|
||||
from sqlalchemy import or_, func, text
|
||||
from sqlalchemy import or_, func, text, and_ # Added and_
|
||||
import traceback
|
||||
import json
|
||||
|
||||
@ -130,7 +130,7 @@ class SemiInboundService:
|
||||
batch_number=data.get('batch_number'),
|
||||
barcode=final_barcode,
|
||||
|
||||
status='在库',
|
||||
status=data.get('status', '在库'), # 默认在库
|
||||
quality_status=data.get('quality_status', '合格'),
|
||||
in_quantity=in_qty,
|
||||
stock_quantity=in_qty,
|
||||
@ -150,7 +150,6 @@ class SemiInboundService:
|
||||
manual_cost=manual_cost,
|
||||
total_price=total_value,
|
||||
|
||||
# [核心修改] 将列表转为 JSON 字符串存储
|
||||
arrival_photo=json.dumps(arrival_list),
|
||||
quality_report_link=json.dumps(quality_report_list),
|
||||
|
||||
@ -199,7 +198,6 @@ class SemiInboundService:
|
||||
if frontend_key in data:
|
||||
setattr(stock, db_attr, data[frontend_key])
|
||||
|
||||
# [核心修改] 图片字段更新 (List -> JSON String)
|
||||
if 'arrival_photo' in data:
|
||||
imgs = data['arrival_photo']
|
||||
if isinstance(imgs, list):
|
||||
@ -244,9 +242,8 @@ class SemiInboundService:
|
||||
|
||||
if 'in_quantity' in data:
|
||||
new_qty = float(data['in_quantity'])
|
||||
old_qty = float(stock.in_quantity)
|
||||
if new_qty != old_qty:
|
||||
diff = new_qty - old_qty
|
||||
diff = new_qty - float(stock.in_quantity)
|
||||
if diff != 0:
|
||||
stock.in_quantity = new_qty
|
||||
stock.stock_quantity = float(stock.stock_quantity) + diff
|
||||
stock.available_quantity = float(stock.available_quantity) + diff
|
||||
@ -288,49 +285,71 @@ class SemiInboundService:
|
||||
raise e
|
||||
|
||||
@staticmethod
|
||||
def get_list(page, limit, keyword=None):
|
||||
def get_list(page, limit, keyword=None, statuses=None):
|
||||
from app.models.inbound.semi import StockSemi
|
||||
try:
|
||||
query = db.session.query(StockSemi).outerjoin(MaterialBase, StockSemi.base_id == MaterialBase.id)
|
||||
|
||||
# 1. 关键词搜索
|
||||
if keyword:
|
||||
kw = f'%{keyword}%'
|
||||
query = query.filter(
|
||||
or_(
|
||||
MaterialBase.name.ilike(f'%{keyword}%'),
|
||||
MaterialBase.spec_model.ilike(f'%{keyword}%'),
|
||||
StockSemi.batch_number.ilike(f'%{keyword}%'),
|
||||
StockSemi.serial_number.ilike(f'%{keyword}%'),
|
||||
StockSemi.sku.ilike(f'%{keyword}%'),
|
||||
StockSemi.work_order_code.ilike(f'%{keyword}%'),
|
||||
StockSemi.bom_code.ilike(f'%{keyword}%')
|
||||
MaterialBase.name.ilike(kw),
|
||||
MaterialBase.spec_model.ilike(kw),
|
||||
StockSemi.batch_number.ilike(kw),
|
||||
StockSemi.serial_number.ilike(kw),
|
||||
StockSemi.sku.ilike(kw),
|
||||
StockSemi.work_order_code.ilike(kw),
|
||||
StockSemi.bom_code.ilike(kw)
|
||||
)
|
||||
)
|
||||
|
||||
# 2. 状态筛选与零库存隐藏逻辑
|
||||
if not statuses:
|
||||
statuses = ['在库', '借库']
|
||||
|
||||
# 如果筛选包含'已出库',则显示所有数量;否则隐藏 stock_quantity <= 0 的记录
|
||||
if '已出库' in statuses:
|
||||
query = query.filter(StockSemi.status.in_(statuses))
|
||||
else:
|
||||
query = query.filter(
|
||||
and_(
|
||||
StockSemi.status.in_(statuses),
|
||||
StockSemi.stock_quantity > 0
|
||||
)
|
||||
)
|
||||
|
||||
pagination = query.order_by(StockSemi.id.desc()).paginate(page=page, per_page=limit, error_out=False)
|
||||
|
||||
current_items = pagination.items
|
||||
base_ids = list(set([item.base_id for item in current_items if item.base_id]))
|
||||
|
||||
stock_map = {}
|
||||
if base_ids:
|
||||
aggregates = db.session.query(
|
||||
StockSemi.base_id,
|
||||
func.sum(StockSemi.stock_quantity).label('total_stock'),
|
||||
func.sum(StockSemi.available_quantity).label('total_avail')
|
||||
).filter(StockSemi.base_id.in_(base_ids)).group_by(StockSemi.base_id).all()
|
||||
|
||||
for agg in aggregates:
|
||||
stock_map[agg.base_id] = {
|
||||
'total_stock': float(agg.total_stock or 0),
|
||||
'total_avail': float(agg.total_avail or 0)
|
||||
}
|
||||
# 3. 数据组装 (移除 aggregation map,使用单行数据)
|
||||
def parse_img(json_str):
|
||||
if not json_str: return []
|
||||
try:
|
||||
return json.loads(json_str) if json_str.startswith('[') else [json_str]
|
||||
except:
|
||||
return []
|
||||
|
||||
items = []
|
||||
for item in current_items:
|
||||
stats = stock_map.get(item.base_id, {'total_stock': 0, 'total_avail': 0})
|
||||
d = item.to_dict()
|
||||
d['sum_stock'] = stats['total_stock']
|
||||
d['sum_available'] = stats['total_avail']
|
||||
|
||||
# 直接使用当前行的库存,不再聚合
|
||||
d['qty_stock'] = float(item.stock_quantity or 0)
|
||||
d['qty_available'] = float(item.available_quantity or 0)
|
||||
|
||||
# 兼容前端字段
|
||||
d['sum_stock'] = d['qty_stock']
|
||||
d['sum_available'] = d['qty_available']
|
||||
|
||||
# 图片解析
|
||||
d['arrival_photo'] = parse_img(item.arrival_photo)
|
||||
d['quality_report_link'] = parse_img(item.quality_report_link)
|
||||
|
||||
# 打印相关
|
||||
d['global_print_id'] = item.global_print_id
|
||||
|
||||
items.append(d)
|
||||
|
||||
return {"total": pagination.total, "items": items}
|
||||
|
||||
Reference in New Issue
Block a user