diff --git a/inventory-backend/app/api/v1/stock/adjustment.py b/inventory-backend/app/api/v1/stock/adjustment.py
index 6357ecf..8cc99ec 100644
--- a/inventory-backend/app/api/v1/stock/adjustment.py
+++ b/inventory-backend/app/api/v1/stock/adjustment.py
@@ -8,6 +8,7 @@ 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 app.models.inbound.stocktake import StocktakeDraft
from datetime import datetime
import random
import string
@@ -221,3 +222,77 @@ def get_stocks():
'limit': limit
}
})
+
+
+# --------------------------------------------------------
+# 5. 一键引入盘点差异
+# POST /api/v1/stock/adjustment/import-from-stocktake
+# --------------------------------------------------------
+@adjustment_bp.route('/import-from-stocktake', methods=['POST'])
+@jwt_required()
+@permission_required('stock_adjustment:operation')
+def import_from_stocktake():
+ """从盘点差异记录导入为盘盈盘亏单"""
+ identity = get_jwt_identity()
+ operator = identity.get('username', 'system') if isinstance(identity, dict) else str(identity)
+
+ try:
+ # 查询所有有差异的盘点记录
+ drafts = StocktakeDraft.query.filter(StocktakeDraft.diff_qty != 0).all()
+
+ if not drafts:
+ return jsonify({'code': 200, 'msg': '暂无盘点差异记录', 'data': {'count': 0}})
+
+ count = 0
+ for draft in drafts:
+ # 判断盘盈/盘亏
+ diff = float(draft.diff_qty or 0)
+ if diff == 0:
+ continue
+
+ adjust_type = 'profit' if diff > 0 else 'loss'
+ adjust_quantity = abs(diff)
+
+ # 获取物料基础信息
+ base_id = None
+ sku = ''
+ warehouse_location = ''
+
+ # 根据source_table获取对应的库存记录
+ stock_model = get_stock_model(draft.source_table)
+ if stock_model and draft.stock_id:
+ stock = stock_model.query.get(draft.stock_id)
+ if stock:
+ base_id = getattr(stock, 'base_id', None)
+ sku = getattr(stock, 'sku', None) or getattr(stock, 'SKU', '')
+ warehouse_location = getattr(stock, 'warehouse_location', '')
+
+ # 生成调整单号
+ order_no = generate_order_no()
+
+ # 使用备注或默认原因
+ reason = draft.remark if draft.remark else '盘点差异自动生成'
+
+ # 创建调整单
+ adjustment = StockAdjustment(
+ order_no=order_no,
+ base_id=base_id,
+ stock_id=draft.stock_id,
+ source_table=draft.source_table,
+ sku=sku,
+ warehouse_location=warehouse_location,
+ adjust_type=adjust_type,
+ adjust_quantity=adjust_quantity,
+ reason=reason,
+ status='pending',
+ operator=operator
+ )
+ db.session.add(adjustment)
+ count += 1
+
+ db.session.commit()
+ return jsonify({'code': 200, 'msg': '导入成功', 'data': {'count': count}})
+
+ except Exception as e:
+ db.session.rollback()
+ return jsonify({'code': 500, 'msg': f'导入失败: {str(e)}'}), 500
diff --git a/inventory-web/src/views/stock/adjustment/index.vue b/inventory-web/src/views/stock/adjustment/index.vue
index f66b381..c1d65fc 100644
--- a/inventory-web/src/views/stock/adjustment/index.vue
+++ b/inventory-web/src/views/stock/adjustment/index.vue
@@ -13,6 +13,9 @@
查询
+
+ 一键引入盘点差异
+
新增调整单
@@ -132,8 +135,9 @@