fix: sort warehouse tree by name, fix tree batch delete cascade, and implement safe history location autofill
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
# inventory-backend/app/services/inbound/buy_service.py
|
||||
from app.extensions import db
|
||||
from app.models.inbound.buy import StockBuy
|
||||
from app.models.inbound.product import StockProduct
|
||||
from app.models.base import MaterialBase
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from sqlalchemy import or_, func, text, and_
|
||||
@ -525,3 +526,42 @@ class BuyInboundService:
|
||||
def get_history_locations(base_id):
|
||||
return [r[0] for r in
|
||||
db.session.query(StockBuy.warehouse_location).filter(StockBuy.base_id == base_id).distinct().all()]
|
||||
|
||||
@staticmethod
|
||||
def get_last_location_by_base_id(base_id):
|
||||
"""
|
||||
获取指定物料最近一次入库的库位(跨表查询)
|
||||
查询顺序:采购入库 -> 成品入库 -> 半成品入库,返回最新入库的库位
|
||||
"""
|
||||
from app.models.inbound.semi import StockSemi
|
||||
|
||||
# 1. 查询采购入库最新记录
|
||||
last_buy = StockBuy.query.filter(
|
||||
StockBuy.base_id == base_id
|
||||
).order_by(StockBuy.in_date.desc()).first()
|
||||
|
||||
# 2. 查询成品入库最新记录
|
||||
last_product = StockProduct.query.filter(
|
||||
StockProduct.base_id == base_id
|
||||
).order_by(StockProduct.in_date.desc()).first()
|
||||
|
||||
# 3. 查询半成品入库最新记录
|
||||
last_semi = StockSemi.query.filter(
|
||||
StockSemi.base_id == base_id
|
||||
).order_by(StockSemi.in_date.desc()).first()
|
||||
|
||||
# 比较三个表中的最新入库时间,返回最新的库位
|
||||
candidates = []
|
||||
if last_buy and last_buy.warehouse_location:
|
||||
candidates.append((last_buy.in_date, last_buy.warehouse_location))
|
||||
if last_product and last_product.warehouse_location:
|
||||
candidates.append((last_product.in_date, last_product.warehouse_location))
|
||||
if last_semi and last_semi.warehouse_location:
|
||||
candidates.append((last_semi.in_date, last_semi.warehouse_location))
|
||||
|
||||
if not candidates:
|
||||
return ""
|
||||
|
||||
# 按时间倒序排序,返回最新的库位
|
||||
candidates.sort(key=lambda x: x[0] if x[0] else datetime.min, reverse=True)
|
||||
return candidates[0][1] if candidates[0][1] else ""
|
||||
|
||||
Reference in New Issue
Block a user