修改半成品与成品出入库相关联逻辑
This commit is contained in:
@ -1,8 +1,9 @@
|
||||
# app/services/inbound/product_service.py
|
||||
from app.extensions import db
|
||||
from app.models.base import MaterialBase
|
||||
from app.models.outbound import TransOutbound
|
||||
from datetime import datetime
|
||||
from sqlalchemy import or_, func, text, and_ # Added and_
|
||||
from sqlalchemy import or_, func, text, and_
|
||||
import traceback
|
||||
import json
|
||||
|
||||
@ -64,7 +65,7 @@ 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', [])
|
||||
@ -135,7 +136,7 @@ class ProductInboundService:
|
||||
for f in fields:
|
||||
if f in data: setattr(stock, f, data[f])
|
||||
|
||||
# [核心修改] 更新 JSON 字段
|
||||
# 更新 JSON 字段
|
||||
if 'product_photo' in data:
|
||||
imgs = data['product_photo']
|
||||
if isinstance(imgs, list): stock.product_photo = json.dumps(imgs)
|
||||
@ -155,10 +156,9 @@ class ProductInboundService:
|
||||
if 'in_quantity' in data:
|
||||
new_qty = float(data['in_quantity'])
|
||||
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
|
||||
stock.in_quantity = new_qty
|
||||
stock.stock_quantity = float(stock.stock_quantity) + diff
|
||||
stock.available_quantity = float(stock.available_quantity) + diff
|
||||
|
||||
if 'production_start_time' in data or 'production_end_time' in data:
|
||||
old_range = stock.production_time_range or " ~ "
|
||||
@ -188,6 +188,23 @@ class ProductInboundService:
|
||||
db.session.rollback()
|
||||
raise e
|
||||
|
||||
# ============================================================
|
||||
# 获取出库流转历史 (与 Buy 逻辑一致,关联 TransOutbound 表)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def get_outbound_history(stock_id):
|
||||
"""获取出库历史"""
|
||||
try:
|
||||
records = TransOutbound.query.filter_by(
|
||||
source_table='stock_product', stock_id=stock_id
|
||||
).order_by(TransOutbound.outbound_time.desc()).all()
|
||||
return [r.to_dict() for r in records]
|
||||
except:
|
||||
return []
|
||||
|
||||
# ============================================================
|
||||
# 获取列表 (包含状态筛选与零库存隐藏逻辑)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def get_list(page, limit, keyword=None, statuses=None):
|
||||
from app.models.inbound.product import StockProduct
|
||||
@ -222,11 +239,8 @@ class ProductInboundService:
|
||||
|
||||
pagination = query.order_by(StockProduct.id.desc()).paginate(page=page, per_page=limit, error_out=False)
|
||||
|
||||
# 3. 数据组装 (移除聚合逻辑,改为单行数据)
|
||||
current_items = pagination.items
|
||||
|
||||
# 移除聚合查询代码...
|
||||
|
||||
def parse_img(json_str):
|
||||
if not json_str: return []
|
||||
try:
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
# app/services/inbound/semi_service.py
|
||||
from app.extensions import db
|
||||
from app.models.base import MaterialBase
|
||||
from app.models.outbound import TransOutbound
|
||||
from datetime import datetime
|
||||
from sqlalchemy import or_, func, text, and_ # Added and_
|
||||
from sqlalchemy import or_, func, text, and_
|
||||
import traceback
|
||||
import json
|
||||
|
||||
@ -130,7 +131,7 @@ class SemiInboundService:
|
||||
batch_number=data.get('batch_number'),
|
||||
barcode=final_barcode,
|
||||
|
||||
status=data.get('status', '在库'), # 默认在库
|
||||
status='在库',
|
||||
quality_status=data.get('quality_status', '合格'),
|
||||
in_quantity=in_qty,
|
||||
stock_quantity=in_qty,
|
||||
@ -150,6 +151,7 @@ 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),
|
||||
|
||||
@ -198,6 +200,7 @@ 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):
|
||||
@ -284,13 +287,29 @@ class SemiInboundService:
|
||||
db.session.rollback()
|
||||
raise e
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# [核心修改] 获取关联出库历史 (跟 Buy 保持一致)
|
||||
# ------------------------------------------------------------------
|
||||
@staticmethod
|
||||
def get_outbound_history(stock_id):
|
||||
"""获取出库历史"""
|
||||
try:
|
||||
records = TransOutbound.query.filter_by(
|
||||
source_table='stock_semi', stock_id=stock_id
|
||||
).order_by(TransOutbound.outbound_time.desc()).all()
|
||||
return [r.to_dict() for r in records]
|
||||
except:
|
||||
return []
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# [核心修改] 列表查询:支持状态筛选、默认隐藏0库存、去除聚合
|
||||
# ------------------------------------------------------------------
|
||||
@staticmethod
|
||||
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(
|
||||
@ -305,7 +324,7 @@ class SemiInboundService:
|
||||
)
|
||||
)
|
||||
|
||||
# 2. 状态筛选与零库存隐藏逻辑
|
||||
# [新增] 状态筛选与零库存隐藏逻辑
|
||||
if not statuses:
|
||||
statuses = ['在库', '借库']
|
||||
|
||||
@ -321,9 +340,9 @@ class SemiInboundService:
|
||||
)
|
||||
|
||||
pagination = query.order_by(StockSemi.id.desc()).paginate(page=page, per_page=limit, error_out=False)
|
||||
|
||||
current_items = pagination.items
|
||||
|
||||
# 3. 数据组装 (移除 aggregation map,使用单行数据)
|
||||
def parse_img(json_str):
|
||||
if not json_str: return []
|
||||
try:
|
||||
@ -339,7 +358,7 @@ class SemiInboundService:
|
||||
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']
|
||||
|
||||
@ -347,7 +366,7 @@ class SemiInboundService:
|
||||
d['arrival_photo'] = parse_img(item.arrival_photo)
|
||||
d['quality_report_link'] = parse_img(item.quality_report_link)
|
||||
|
||||
# 打印相关
|
||||
# 打印ID
|
||||
d['global_print_id'] = item.global_print_id
|
||||
|
||||
items.append(d)
|
||||
|
||||
Reference in New Issue
Block a user