169 lines
6.4 KiB
Python
169 lines
6.4 KiB
Python
from flask import Blueprint, request, jsonify
|
|
from app.services.inbound.buy_service import BuyInboundService
|
|
import traceback
|
|
|
|
inbound_buy_bp = Blueprint('stock_buy', __name__)
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 0. 基础物料搜索
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/search-base', methods=['GET'])
|
|
def search_base():
|
|
try:
|
|
keyword = request.args.get('keyword', '')
|
|
page = request.args.get('page', 1, type=int)
|
|
# 固定每次加载50条
|
|
limit = 50
|
|
|
|
result = BuyInboundService.search_base_material(keyword, page, limit)
|
|
return jsonify({
|
|
"code": 200,
|
|
"msg": "success",
|
|
"data": result['items'],
|
|
"total": result['total'],
|
|
"has_next": result['has_next']
|
|
})
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 1. 获取列表 (修改:接收 category 和 material_type)
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/list', methods=['GET'])
|
|
def get_list():
|
|
try:
|
|
page = request.args.get('page', 1, type=int)
|
|
limit = request.args.get('pageSize', 15, type=int)
|
|
keyword = request.args.get('keyword', '')
|
|
|
|
# 新增筛选参数
|
|
category = request.args.get('category', '')
|
|
material_type = request.args.get('material_type', '')
|
|
|
|
# 状态参数处理
|
|
statuses_str = request.args.get('statuses', '')
|
|
statuses = statuses_str.split(',') if statuses_str else []
|
|
|
|
result = BuyInboundService.get_list(page, limit, keyword, statuses, category, material_type)
|
|
return jsonify({"code": 200, "msg": "success", "data": result})
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 2. 新增入库
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/submit', methods=['POST'])
|
|
def submit():
|
|
try:
|
|
data = request.get_json()
|
|
if not data:
|
|
return jsonify({"code": 400, "msg": "No data"}), 400
|
|
|
|
new_stock = BuyInboundService.handle_inbound(data)
|
|
|
|
return jsonify({
|
|
"code": 200,
|
|
"msg": "入库成功",
|
|
"data": new_stock.to_dict()
|
|
})
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 3. 更新入库
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/<int:id>', methods=['PUT'])
|
|
def update_buy(id):
|
|
try:
|
|
data = request.get_json()
|
|
BuyInboundService.update_inbound(id, data)
|
|
return jsonify({"code": 200, "msg": "更新成功"})
|
|
except Exception as e:
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 4. 删除
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/<int:id>', methods=['DELETE'])
|
|
def delete_buy(id):
|
|
try:
|
|
BuyInboundService.delete_inbound(id)
|
|
return jsonify({"code": 200, "msg": "删除成功"})
|
|
except Exception as e:
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 5. [新增] 获取筛选下拉选项 (修复404的关键)
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/options', methods=['GET'])
|
|
def get_options():
|
|
try:
|
|
data = BuyInboundService.get_filter_options()
|
|
return jsonify({"code": 200, "msg": "success", "data": data})
|
|
except Exception as e:
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 6. 获取关联的出库历史 (如果有)
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/<int:id>/history', methods=['GET'])
|
|
def get_history(id):
|
|
# 如果没有出库模块,这个接口可能为空,但为保持兼容性保留
|
|
return jsonify({"code": 200, "msg": "success", "data": []})
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 7. 供应商建议
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/suggestions/suppliers', methods=['GET'])
|
|
def get_supplier_suggestions():
|
|
base_id = request.args.get('base_id', type=int)
|
|
if not base_id:
|
|
return jsonify({"code": 400, "msg": "base_id required"}), 400
|
|
data = BuyInboundService.get_history_suppliers(base_id)
|
|
return jsonify({"code": 200, "msg": "success", "data": data})
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 8. 采购人建议 (全局)
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/suggestions/users', methods=['GET'])
|
|
def get_user_suggestions():
|
|
keyword = request.args.get('keyword', '')
|
|
data = BuyInboundService.get_history_purchasers(keyword)
|
|
return jsonify({"code": 200, "msg": "success", "data": data})
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 9. 链接建议
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/suggestions/links', methods=['GET'])
|
|
def get_link_suggestions():
|
|
base_id = request.args.get('base_id', type=int)
|
|
link_type = request.args.get('type', 'original') # original or detail
|
|
if not base_id:
|
|
return jsonify({"code": 400, "msg": "base_id required"}), 400
|
|
data = BuyInboundService.get_history_links(base_id, link_type)
|
|
return jsonify({"code": 200, "msg": "success", "data": data})
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# 10. 库位建议
|
|
# ------------------------------------------------------------------
|
|
@inbound_buy_bp.route('/suggestions/locations', methods=['GET'])
|
|
def get_location_suggestions():
|
|
base_id = request.args.get('base_id', type=int)
|
|
if not base_id:
|
|
return jsonify({"code": 400, "msg": "base_id required"}), 400
|
|
data = BuyInboundService.get_history_locations(base_id)
|
|
return jsonify({"code": 200, "msg": "success", "data": data}) |