diff --git a/inventory-backend/app/api/v1/outbound.py b/inventory-backend/app/api/v1/outbound.py index ab2c3c0..bd9d3bb 100644 --- a/inventory-backend/app/api/v1/outbound.py +++ b/inventory-backend/app/api/v1/outbound.py @@ -351,6 +351,99 @@ def _allocate_bom_requirements(requirements, company_limit, }), 200 +# ============================================================================== +# 备选库位查询 (GET /api/v1/outbound/alternatives) +# +# 场景:申请单已把货预占在某个库位,但工人到现场发现该库位进不去/找不到, +# 需要改扫同物料的其它批次。改造前系统不告诉他「还有哪些库位有货」, +# 工人只能凭记忆或挨个翻 —— 这个接口就是为「物理覆盖」提供可见性。 +# +# 与 bom-match-stock 查询模式的区别: +# · 该模式按 stock_quantity > 0 过滤,会把「有货但已被别单全部预占」的 +# 库位也列出来,工人跑过去才发现拿不到货; +# · 本接口按 available_quantity > 0 过滤,只给**真正能拿**的库位, +# 并额外标注哪一条是本单锁定的推荐行。 +# ============================================================================== +@outbound_bp.route('/alternatives', methods=['GET']) +@jwt_required() +def get_stock_alternatives(): + """ + 查询某物料的全部可替代库位(available_quantity > 0)。 + + Query: base_id(必填)、source_table / stock_id(可选,用于标注推荐行) + Returns: { items: [{stock_id, source_table, warehouse_location, + available_quantity, is_locked, typeLabel, sku, batch_number}] } + """ + try: + base_id = request.args.get('base_id', type=int) + if not base_id: + return jsonify({'code': 400, 'msg': 'base_id 不能为空'}), 400 + + prefer_table = (request.args.get('source_table') or '').strip() + try: + prefer_stock_id = int(request.args.get('stock_id') or 0) + except (TypeError, ValueError): + prefer_stock_id = 0 + + from app.utils.decorators import get_current_company_filter + from app.models.base import MaterialBase + from app.models.inbound.buy import StockBuy + from app.models.inbound.semi import StockSemi + from app.models.inbound.product import StockProduct + from sqlalchemy.orm import joinedload + + company_limit = get_current_company_filter() + items = [] + + for model, source_table, label in ( + (StockBuy, 'stock_buy', '采购件'), + (StockSemi, 'stock_semi', '半成品'), + (StockProduct, 'stock_product', '成品'), + ): + q = model.query.filter( + model.base_id == base_id, + model.available_quantity > 0, # ★ 只给真正能拿的 + ) + if company_limit is not None: + q = q.filter(model.base.has(MaterialBase.company_name == company_limit)) + try: + rows = q.options(joinedload(model.base)).all() + except Exception as e: + current_app.logger.error( + f"[alternatives] {source_table} 查询失败: {type(e).__name__}: {e}" + ) + continue + + for s in rows: + items.append({ + 'stock_id': s.id, + 'source_table': source_table, + 'typeLabel': label, + 'sku': s.sku or '', + 'batch_number': getattr(s, 'batch_number', '') or getattr(s, 'serial_number', '') or '', + 'warehouse_location': getattr(s, 'warehouse_location', '') or '', + 'available_quantity': float(s.available_quantity or 0), + # ★ 该行是否就是本单锁定的推荐批次 + 'is_locked': (prefer_stock_id and s.id == prefer_stock_id + and source_table == prefer_table), + }) + + # 排序:推荐行置顶,其余按可用量降序(工人优先看到货最多的库位) + items.sort(key=lambda x: (not x['is_locked'], -x['available_quantity'])) + + return jsonify({ + 'code': 200, 'msg': 'success', + 'data': { + 'items': items, + 'total_available': round(sum(i['available_quantity'] for i in items), 4), + } + }), 200 + + except Exception as e: + traceback.print_exc() + return jsonify({'code': 500, 'msg': f'查询备选库位失败: {str(e)}'}), 500 + + # ============================================================================== # BOM 匹配库存接口 (POST /api/v1/outbound/bom-match-stock) # diff --git a/inventory-web/src/api/outbound.ts b/inventory-web/src/api/outbound.ts index d859278..e934cef 100644 --- a/inventory-web/src/api/outbound.ts +++ b/inventory-web/src/api/outbound.ts @@ -170,4 +170,28 @@ export function bomMatchStock(payload: BomRequirement[] | { requirements: BomReq method: 'post', data }) -} \ No newline at end of file +} +/** + * 备选库位查询 —— 为「物理覆盖」提供可见性 + * + * 申请单把货预占在某个库位后,工人现场可能进不去该库位,需要改扫同物料的 + * 其它批次。本接口按 available_quantity > 0 返回**真正能拿**的库位, + * 并标注哪一条是本单锁定的推荐行。 + * + * @param baseId 物料 ID(申请明细里的 base_id) + * @param locked 本单锁定的批次(用于标注「推荐」),可选 + */ +export function getStockAlternatives( + baseId: number, + locked?: { stock_id?: number; source_table?: string } +) { + return request({ + url: '/v1/outbound/alternatives', + method: 'get', + params: { + base_id: baseId, + stock_id: locked?.stock_id, + source_table: locked?.source_table + } + }) +} diff --git a/inventory-web/src/views/outbound/create.vue b/inventory-web/src/views/outbound/create.vue index b863c35..4415c87 100644 --- a/inventory-web/src/views/outbound/create.vue +++ b/inventory-web/src/views/outbound/create.vue @@ -63,7 +63,45 @@ - + + + +