修复出库时候找不到名称等问题
This commit is contained in:
@ -32,14 +32,14 @@ class MaterialBase(db.Model):
|
|||||||
# 关联关系区域
|
# 关联关系区域
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
# 1. 关联采购库存 (StockBuy)
|
# 1. 关联采购库存 (StockBuy) - 修改 back_populates 为 'base'
|
||||||
stock_buys = db.relationship('StockBuy', back_populates='material', lazy='dynamic')
|
stock_buys = db.relationship('StockBuy', back_populates='base', lazy='dynamic')
|
||||||
|
|
||||||
# 2. 关联半成品库存 (StockSemi)
|
# 2. 关联半成品库存 (StockSemi) - 修改 back_populates 为 'base'
|
||||||
stock_semis = db.relationship('StockSemi', back_populates='material', lazy='dynamic')
|
stock_semis = db.relationship('StockSemi', back_populates='base', lazy='dynamic')
|
||||||
|
|
||||||
# 3. 关联成品库存 (StockProduct)
|
# 3. 关联成品库存 (StockProduct) - 修改 back_populates 为 'base'
|
||||||
stock_products = db.relationship('StockProduct', back_populates='material', lazy='dynamic')
|
stock_products = db.relationship('StockProduct', back_populates='base', lazy='dynamic')
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
|
# inventory-backend/app/models/inbound/buy.py
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
import json
|
import json
|
||||||
|
# 显式导入 MaterialBase 以防 relationship 找不到引用
|
||||||
|
from app.models.base import MaterialBase
|
||||||
|
|
||||||
class StockBuy(db.Model):
|
class StockBuy(db.Model):
|
||||||
"""
|
"""
|
||||||
@ -48,9 +51,8 @@ class StockBuy(db.Model):
|
|||||||
# [新增] 全局打印流水号 (用于跨表连续编号,对应 Sequence: global_print_seq)
|
# [新增] 全局打印流水号 (用于跨表连续编号,对应 Sequence: global_print_seq)
|
||||||
global_print_id = db.Column(db.Integer)
|
global_print_id = db.Column(db.Integer)
|
||||||
|
|
||||||
# 关系定义
|
# 关系定义 [已修改]
|
||||||
# 注意:这里使用字符串 'MaterialBase' 引用,避免了直接 import 导致的潜在循环依赖
|
base = db.relationship('MaterialBase', back_populates='stock_buys')
|
||||||
material = db.relationship('MaterialBase', back_populates='stock_buys')
|
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
# 辅助解析函数:将数据库存储的 JSON 字符串转为 List
|
# 辅助解析函数:将数据库存储的 JSON 字符串转为 List
|
||||||
@ -68,11 +70,12 @@ class StockBuy(db.Model):
|
|||||||
return {
|
return {
|
||||||
'id': self.id,
|
'id': self.id,
|
||||||
'base_id': self.base_id,
|
'base_id': self.base_id,
|
||||||
'material_name': self.material.name if self.material else '',
|
# [已修改] 使用 self.base
|
||||||
'spec_model': self.material.spec_model if self.material else '',
|
'material_name': self.base.name if self.base else '',
|
||||||
'category': self.material.category if self.material else '',
|
'spec_model': self.base.spec_model if self.base else '',
|
||||||
'unit': self.material.unit if self.material else '',
|
'category': self.base.category if self.base else '',
|
||||||
'material_type': self.material.material_type if self.material else '',
|
'unit': self.base.unit if self.base else '',
|
||||||
|
'material_type': self.base.material_type if self.base else '',
|
||||||
|
|
||||||
'sku': self.sku,
|
'sku': self.sku,
|
||||||
'inbound_date': self.in_date.strftime('%Y-%m-%d') if self.in_date else '',
|
'inbound_date': self.in_date.strftime('%Y-%m-%d') if self.in_date else '',
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
# app/models/inbound/product.py
|
# app/models/inbound/product.py
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
import json
|
import json
|
||||||
|
from app.models.base import MaterialBase
|
||||||
|
|
||||||
class StockProduct(db.Model):
|
class StockProduct(db.Model):
|
||||||
"""
|
"""
|
||||||
@ -57,8 +57,8 @@ class StockProduct(db.Model):
|
|||||||
# 全局打印流水号
|
# 全局打印流水号
|
||||||
global_print_id = db.Column(db.Integer)
|
global_print_id = db.Column(db.Integer)
|
||||||
|
|
||||||
# 关系定义
|
# 关系定义 [已修改]
|
||||||
material = db.relationship('MaterialBase', back_populates='stock_products')
|
base = db.relationship('MaterialBase', back_populates='stock_products')
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
raw_val = float(self.raw_material_cost or 0)
|
raw_val = float(self.raw_material_cost or 0)
|
||||||
@ -79,11 +79,12 @@ class StockProduct(db.Model):
|
|||||||
return {
|
return {
|
||||||
'id': self.id,
|
'id': self.id,
|
||||||
'base_id': self.base_id,
|
'base_id': self.base_id,
|
||||||
'material_name': self.material.name if self.material else '',
|
# [已修改] 使用 self.base
|
||||||
'spec_model': self.material.spec_model if self.material else '',
|
'material_name': self.base.name if self.base else '',
|
||||||
'category': self.material.category if self.material else '',
|
'spec_model': self.base.spec_model if self.base else '',
|
||||||
'unit': self.material.unit if self.material else '',
|
'category': self.base.category if self.base else '',
|
||||||
'material_type': self.material.material_type if self.material else '',
|
'unit': self.base.unit if self.base else '',
|
||||||
|
'material_type': self.base.material_type if self.base else '',
|
||||||
|
|
||||||
'sku': self.sku,
|
'sku': self.sku,
|
||||||
'inbound_date': self.production_date.strftime('%Y-%m-%d') if self.production_date else '',
|
'inbound_date': self.production_date.strftime('%Y-%m-%d') if self.production_date else '',
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
# app/models/inbound/semi.py
|
# app/models/inbound/semi.py
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
import json
|
import json
|
||||||
|
from app.models.base import MaterialBase
|
||||||
|
|
||||||
class StockSemi(db.Model):
|
class StockSemi(db.Model):
|
||||||
"""
|
"""
|
||||||
@ -55,8 +55,8 @@ class StockSemi(db.Model):
|
|||||||
# [新增] 全局打印流水号
|
# [新增] 全局打印流水号
|
||||||
global_print_id = db.Column(db.Integer)
|
global_print_id = db.Column(db.Integer)
|
||||||
|
|
||||||
# 关系定义
|
# 关系定义 [已修改]
|
||||||
material = db.relationship('MaterialBase', back_populates='stock_semis')
|
base = db.relationship('MaterialBase', back_populates='stock_semis')
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
raw_val = float(self.raw_material_cost or 0)
|
raw_val = float(self.raw_material_cost or 0)
|
||||||
@ -78,11 +78,12 @@ class StockSemi(db.Model):
|
|||||||
return {
|
return {
|
||||||
'id': self.id,
|
'id': self.id,
|
||||||
'base_id': self.base_id,
|
'base_id': self.base_id,
|
||||||
'material_name': self.material.name if self.material else '',
|
# [已修改] 使用 self.base
|
||||||
'spec_model': self.material.spec_model if self.material else '',
|
'material_name': self.base.name if self.base else '',
|
||||||
'category': self.material.category if self.material else '',
|
'spec_model': self.base.spec_model if self.base else '',
|
||||||
'unit': self.material.unit if self.material else '',
|
'category': self.base.category if self.base else '',
|
||||||
'material_type': self.material.material_type if self.material else '',
|
'unit': self.base.unit if self.base else '',
|
||||||
|
'material_type': self.base.material_type if self.base else '',
|
||||||
|
|
||||||
'sku': self.sku,
|
'sku': self.sku,
|
||||||
'inbound_date': self.production_date.strftime('%Y-%m-%d') if self.production_date else '',
|
'inbound_date': self.production_date.strftime('%Y-%m-%d') if self.production_date else '',
|
||||||
|
|||||||
@ -37,8 +37,8 @@ class BuyInboundService:
|
|||||||
|
|
||||||
exists = query.first()
|
exists = query.first()
|
||||||
if exists:
|
if exists:
|
||||||
# 获取占用该SN的物料名称,提示更友好
|
# [修改] 获取占用该SN的物料名称 (material -> base)
|
||||||
occupied_name = exists.material.name if exists.material else "未知物料"
|
occupied_name = exists.base.name if exists.base else "未知物料"
|
||||||
raise ValueError(f"序列号【{serial_number}】已存在!被物料 [{occupied_name}] 占用,请核查。")
|
raise ValueError(f"序列号【{serial_number}】已存在!被物料 [{occupied_name}] 占用,请核查。")
|
||||||
|
|
||||||
# 2. 批号 (BN) 同物料唯一校验
|
# 2. 批号 (BN) 同物料唯一校验
|
||||||
@ -309,12 +309,12 @@ class BuyInboundService:
|
|||||||
d = {
|
d = {
|
||||||
'id': item.id,
|
'id': item.id,
|
||||||
'base_id': item.base_id,
|
'base_id': item.base_id,
|
||||||
# 确保这里从关联的 MaterialBase 获取规格型号
|
# [核心修改] 确保这里从关联的 .base 获取信息
|
||||||
'material_name': item.material.name if item.material else '',
|
'material_name': item.base.name if item.base else '',
|
||||||
'spec_model': item.material.spec_model if item.material else '',
|
'spec_model': item.base.spec_model if item.base else '',
|
||||||
'category': item.material.category if item.material else '',
|
'category': item.base.category if item.base else '',
|
||||||
'unit': item.material.unit if item.material else '',
|
'unit': item.base.unit if item.base else '',
|
||||||
'material_type': item.material.material_type if item.material else '',
|
'material_type': item.base.material_type if item.base else '',
|
||||||
|
|
||||||
'sku': item.sku,
|
'sku': item.sku,
|
||||||
'inbound_date': date_display,
|
'inbound_date': date_display,
|
||||||
|
|||||||
@ -30,7 +30,8 @@ class ProductInboundService:
|
|||||||
|
|
||||||
exists = query.first()
|
exists = query.first()
|
||||||
if exists:
|
if exists:
|
||||||
occupied_name = exists.material.name if (hasattr(exists, 'material') and exists.material) else "未知物料"
|
# [修改] material -> base
|
||||||
|
occupied_name = exists.base.name if (hasattr(exists, 'base') and exists.base) else "未知物料"
|
||||||
raise ValueError(f"序列号【{serial_number}】已存在!被成品 [{occupied_name}] 占用,请核查。")
|
raise ValueError(f"序列号【{serial_number}】已存在!被成品 [{occupied_name}] 占用,请核查。")
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@ -318,6 +319,7 @@ class ProductInboundService:
|
|||||||
|
|
||||||
items = []
|
items = []
|
||||||
for item in current_items:
|
for item in current_items:
|
||||||
|
# [注意] 因为Model层已经修改了 to_dict 内部的 material -> base,所以这里直接调 to_dict 即可
|
||||||
d = item.to_dict()
|
d = item.to_dict()
|
||||||
|
|
||||||
# 格式化日期
|
# 格式化日期
|
||||||
|
|||||||
@ -32,7 +32,8 @@ class SemiInboundService:
|
|||||||
|
|
||||||
exists = query.first()
|
exists = query.first()
|
||||||
if exists:
|
if exists:
|
||||||
occupied_name = exists.material.name if (hasattr(exists, 'material') and exists.material) else "未知物料"
|
# [修改] material -> base
|
||||||
|
occupied_name = exists.base.name if (hasattr(exists, 'base') and exists.base) else "未知物料"
|
||||||
raise ValueError(f"序列号【{serial_number}】已存在!被半成品 [{occupied_name}] 占用,请核查。")
|
raise ValueError(f"序列号【{serial_number}】已存在!被半成品 [{occupied_name}] 占用,请核查。")
|
||||||
|
|
||||||
# 2. 批号 (BN) 校验 - 同物料下不能重复开单
|
# 2. 批号 (BN) 校验 - 同物料下不能重复开单
|
||||||
@ -420,6 +421,7 @@ class SemiInboundService:
|
|||||||
|
|
||||||
items = []
|
items = []
|
||||||
for item in current_items:
|
for item in current_items:
|
||||||
|
# [注意] 因为Model层已经修改了 to_dict 内部的 material -> base,所以这里直接调 to_dict 即可
|
||||||
d = item.to_dict()
|
d = item.to_dict()
|
||||||
|
|
||||||
# 格式化展示日期
|
# 格式化展示日期
|
||||||
|
|||||||
Reference in New Issue
Block a user