盘库操作初设计
This commit is contained in:
98
inventory-backend/app/api/v1/inbound/stock.py
Normal file
98
inventory-backend/app/api/v1/inbound/stock.py
Normal file
@ -0,0 +1,98 @@
|
||||
from flask import Blueprint, jsonify, request
|
||||
# ★ [核心修复] 导入正确的模型类名 StockBuy (替换原来的 InboundBuy)
|
||||
from app.models.inbound.buy import StockBuy
|
||||
|
||||
# 尝试导入半成品和成品模型 (根据你的命名习惯修正为 StockSemi/StockProduct)
|
||||
# 使用 try-except 防止如果其他文件还没改名导致再次报错
|
||||
try:
|
||||
from app.models.inbound.semi import StockSemi
|
||||
except ImportError:
|
||||
StockSemi = None
|
||||
|
||||
try:
|
||||
from app.models.inbound.product import StockProduct
|
||||
except ImportError:
|
||||
StockProduct = None
|
||||
|
||||
from app.services.print.network_print_service import NetworkPrintService
|
||||
|
||||
bp = Blueprint('stock_ops', __name__)
|
||||
|
||||
|
||||
@bp.route('/all', methods=['GET'])
|
||||
def get_all_stock():
|
||||
"""
|
||||
获取所有在库物品(采购件+半成品+成品)
|
||||
用于:盘点初始化、出库选单列表
|
||||
"""
|
||||
try:
|
||||
# 1. 获取采购件
|
||||
# ★ [核心修复] 使用 StockBuy 查询,并将状态条件改为 '在库' (匹配你的 Model 定义)
|
||||
materials = []
|
||||
if StockBuy:
|
||||
materials = StockBuy.query.filter(StockBuy.status == '在库').all()
|
||||
|
||||
# 2. 获取半成品
|
||||
semis = []
|
||||
if StockSemi:
|
||||
try:
|
||||
# 假设半成品也使用 '在库' 状态
|
||||
semis = StockSemi.query.filter(StockSemi.status == '在库').all()
|
||||
except Exception:
|
||||
semis = []
|
||||
|
||||
# 3. 获取成品
|
||||
products = []
|
||||
if StockProduct:
|
||||
try:
|
||||
products = StockProduct.query.filter(StockProduct.status == '在库').all()
|
||||
except Exception:
|
||||
products = []
|
||||
|
||||
return jsonify({
|
||||
"materials": [item.to_dict() for item in materials],
|
||||
"semis": [item.to_dict() for item in semis],
|
||||
"products": [item.to_dict() for item in products]
|
||||
}), 200
|
||||
except Exception as e:
|
||||
print(f"Error in get_all_stock: {e}") # 输出错误日志以便调试
|
||||
return jsonify({"message": f"查询库存失败: {str(e)}"}), 500
|
||||
|
||||
|
||||
@bp.route('/print/selection', methods=['POST'])
|
||||
def print_selection():
|
||||
"""打印出库选单"""
|
||||
try:
|
||||
data = request.json
|
||||
items = data.get('items', [])
|
||||
|
||||
if not items:
|
||||
return jsonify({"message": "未选择任何物品"}), 400
|
||||
|
||||
printer = NetworkPrintService() # 默认连接 192.168.9.205
|
||||
success, msg = printer.print_outbound_selection(items)
|
||||
|
||||
if success:
|
||||
return jsonify({"message": "打印指令已发送"}), 200
|
||||
else:
|
||||
return jsonify({"message": msg}), 500
|
||||
except Exception as e:
|
||||
return jsonify({"message": f"打印服务错误: {str(e)}"}), 500
|
||||
|
||||
|
||||
@bp.route('/print/stocktake', methods=['POST'])
|
||||
def print_stocktake():
|
||||
"""打印盘点报告"""
|
||||
try:
|
||||
data = request.json
|
||||
# data 结构: { total, scanned, missing, missing_items: [] }
|
||||
|
||||
printer = NetworkPrintService()
|
||||
success, msg = printer.print_stocktake_report(data)
|
||||
|
||||
if success:
|
||||
return jsonify({"message": "盘点报告已发送"}), 200
|
||||
else:
|
||||
return jsonify({"message": msg}), 500
|
||||
except Exception as e:
|
||||
return jsonify({"message": f"打印服务错误: {str(e)}"}), 500
|
||||
Reference in New Issue
Block a user