盘库操作初设计
This commit is contained in:
@ -1,9 +1,15 @@
|
||||
# app/services/inbound/buy_service.py
|
||||
from app.extensions import db
|
||||
# 引用新的模型类 StockBuy
|
||||
from app.models.inbound.buy import StockBuy
|
||||
from app.models.base import MaterialBase
|
||||
from app.models.outbound import TransOutbound
|
||||
from datetime import datetime, timedelta, timezone # [修改] 引入 timezone
|
||||
|
||||
# 尝试导入出库模型,如果不存在则忽略(防止报错影响入库功能)
|
||||
try:
|
||||
from app.models.outbound import TransOutbound
|
||||
except ImportError:
|
||||
TransOutbound = None
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from sqlalchemy import or_, func, text, and_
|
||||
import traceback
|
||||
import json
|
||||
@ -76,11 +82,22 @@ class BuyInboundService:
|
||||
in_qty = float(data.get('in_quantity') or 0)
|
||||
u_price = float(data.get('unit_price') or 0)
|
||||
|
||||
seq_sql = text("SELECT nextval('global_print_seq')")
|
||||
result = db.session.execute(seq_sql)
|
||||
next_global_id = result.scalar()
|
||||
# [核心逻辑] 获取全局打印流水号
|
||||
try:
|
||||
seq_sql = text("SELECT nextval('global_print_seq')")
|
||||
result = db.session.execute(seq_sql)
|
||||
next_global_id = result.scalar()
|
||||
except Exception:
|
||||
# 如果序列不存在,回退处理(或在数据库创建序列)
|
||||
print("Warning: Sequence global_print_seq not found.")
|
||||
next_global_id = None
|
||||
|
||||
# SKU 生成逻辑:如果没有 ID,用临时随机数或空;通常应该依赖 next_global_id
|
||||
if next_global_id:
|
||||
generated_sku = str(next_global_id).zfill(10)
|
||||
else:
|
||||
generated_sku = datetime.now().strftime('%Y%m%d%H%M%S') # 降级方案
|
||||
|
||||
generated_sku = str(next_global_id).zfill(10)
|
||||
final_barcode = data.get('barcode') or generated_sku
|
||||
|
||||
arrival_list = data.get('arrival_photo', [])
|
||||
@ -151,6 +168,7 @@ class BuyInboundService:
|
||||
|
||||
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
|
||||
@ -189,6 +207,8 @@ class BuyInboundService:
|
||||
@staticmethod
|
||||
def get_outbound_history(stock_id):
|
||||
"""获取出库历史"""
|
||||
if not TransOutbound:
|
||||
return []
|
||||
try:
|
||||
records = TransOutbound.query.filter_by(
|
||||
source_table='stock_buy', stock_id=stock_id
|
||||
@ -228,8 +248,10 @@ class BuyInboundService:
|
||||
statuses = ['在库', '借库']
|
||||
|
||||
if '已出库' in statuses:
|
||||
# 如果明确查已出库,可以包含库存为0的
|
||||
query = query.filter(StockBuy.status.in_(statuses))
|
||||
else:
|
||||
# 默认查在库,必须保证库存 > 0
|
||||
query = query.filter(
|
||||
and_(
|
||||
StockBuy.status.in_(statuses),
|
||||
|
||||
Reference in New Issue
Block a user