2026-01-27 15:50:23 +08:00
|
|
|
|
from app.extensions import db
|
2026-01-28 17:44:39 +08:00
|
|
|
|
from app.models.inbound.buy import StockBuy
|
2026-01-30 11:50:35 +08:00
|
|
|
|
from app.models.base import MaterialBase
|
2026-02-06 10:16:37 +08:00
|
|
|
|
|
2026-02-06 17:11:47 +08:00
|
|
|
|
# 尝试导入出库模型,如果不存在则忽略
|
2026-02-06 10:16:37 +08:00
|
|
|
|
try:
|
|
|
|
|
|
from app.models.outbound import TransOutbound
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
|
TransOutbound = None
|
|
|
|
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta, timezone
|
2026-02-05 11:37:06 +08:00
|
|
|
|
from sqlalchemy import or_, func, text, and_
|
2026-01-27 15:50:23 +08:00
|
|
|
|
import traceback
|
2026-02-03 11:55:33 +08:00
|
|
|
|
import json
|
2026-01-27 15:50:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BuyInboundService:
|
2026-02-05 11:08:29 +08:00
|
|
|
|
|
2026-02-06 17:11:47 +08:00
|
|
|
|
# ============================================================
|
|
|
|
|
|
# 0. 辅助:唯一性校验 (核心修复)
|
|
|
|
|
|
# ============================================================
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _check_unique(base_id, serial_number, batch_number, exclude_id=None):
|
|
|
|
|
|
"""
|
|
|
|
|
|
校验序列号和批号的唯一性逻辑
|
|
|
|
|
|
:param base_id: 当前物料的基础ID
|
|
|
|
|
|
:param serial_number: 序列号
|
|
|
|
|
|
:param batch_number: 批号
|
|
|
|
|
|
:param exclude_id: 排除的ID (用于编辑模式)
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. 序列号 (SN) 全局唯一校验
|
|
|
|
|
|
# 解释: 不同规格的物料通常也不应该有相同的SN,防止扫码混淆
|
|
|
|
|
|
if serial_number:
|
|
|
|
|
|
query = StockBuy.query.filter(StockBuy.serial_number == serial_number)
|
|
|
|
|
|
if exclude_id:
|
|
|
|
|
|
query = query.filter(StockBuy.id != exclude_id)
|
|
|
|
|
|
|
|
|
|
|
|
exists = query.first()
|
|
|
|
|
|
if exists:
|
2026-02-10 11:13:07 +08:00
|
|
|
|
# [修改] 获取占用该SN的物料名称 (material -> base)
|
|
|
|
|
|
occupied_name = exists.base.name if exists.base else "未知物料"
|
2026-02-06 17:11:47 +08:00
|
|
|
|
raise ValueError(f"序列号【{serial_number}】已存在!被物料 [{occupied_name}] 占用,请核查。")
|
|
|
|
|
|
|
|
|
|
|
|
# 2. 批号 (BN) 同物料唯一校验
|
|
|
|
|
|
# 解释: 不同规格的物料可以有相同的批号(如都有 001 批次),但同一个物料不能重复建单
|
|
|
|
|
|
if batch_number and base_id:
|
|
|
|
|
|
query = StockBuy.query.filter(
|
|
|
|
|
|
StockBuy.base_id == base_id,
|
|
|
|
|
|
StockBuy.batch_number == batch_number
|
|
|
|
|
|
)
|
|
|
|
|
|
if exclude_id:
|
|
|
|
|
|
query = query.filter(StockBuy.id != exclude_id)
|
|
|
|
|
|
|
|
|
|
|
|
if query.first():
|
|
|
|
|
|
raise ValueError(f"该物料已存在批号【{batch_number}】,请勿重复录入,可直接在该批次下追加库存。")
|
|
|
|
|
|
|
2026-02-05 11:08:29 +08:00
|
|
|
|
# ============================================================
|
2026-02-05 11:37:06 +08:00
|
|
|
|
# 1. 基础物料搜索
|
2026-02-05 11:08:29 +08:00
|
|
|
|
# ============================================================
|
2026-01-27 15:50:23 +08:00
|
|
|
|
@staticmethod
|
2026-01-28 11:22:08 +08:00
|
|
|
|
def search_base_material(keyword):
|
2026-01-27 15:50:23 +08:00
|
|
|
|
try:
|
2026-02-10 13:50:26 +08:00
|
|
|
|
# [核心修改] 只查询已启用的物料,防止选择已禁用的历史物料
|
2026-01-30 11:50:35 +08:00
|
|
|
|
query = MaterialBase.query.filter(MaterialBase.is_enabled == True)
|
2026-02-10 13:50:26 +08:00
|
|
|
|
|
2026-01-30 11:50:35 +08:00
|
|
|
|
if keyword:
|
|
|
|
|
|
query = query.filter(
|
|
|
|
|
|
or_(
|
|
|
|
|
|
MaterialBase.name.ilike(f'%{keyword}%'),
|
2026-02-06 17:11:47 +08:00
|
|
|
|
MaterialBase.spec_model.ilike(f'%{keyword}%'),
|
|
|
|
|
|
MaterialBase.pinyin.ilike(f'%{keyword}%') # 假设有拼音搜索
|
2026-01-30 11:50:35 +08:00
|
|
|
|
)
|
2026-01-28 11:22:08 +08:00
|
|
|
|
)
|
2026-01-30 11:50:35 +08:00
|
|
|
|
query = query.order_by(MaterialBase.id.desc()).limit(20)
|
2026-01-28 11:22:08 +08:00
|
|
|
|
results = []
|
|
|
|
|
|
for item in query.all():
|
|
|
|
|
|
results.append({
|
2026-02-06 17:11:47 +08:00
|
|
|
|
'id': item.id,
|
|
|
|
|
|
'name': item.name,
|
|
|
|
|
|
'spec': item.spec_model, # 确保这里字段对应正确
|
|
|
|
|
|
'category': item.category,
|
|
|
|
|
|
'unit': item.unit,
|
|
|
|
|
|
'type': item.material_type,
|
|
|
|
|
|
'status': '启用'
|
2026-01-28 11:22:08 +08:00
|
|
|
|
})
|
|
|
|
|
|
return results
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
return []
|
2026-01-27 15:50:23 +08:00
|
|
|
|
|
2026-02-05 11:08:29 +08:00
|
|
|
|
# ============================================================
|
2026-02-06 17:11:47 +08:00
|
|
|
|
# 2. 新增入库逻辑
|
2026-02-05 11:08:29 +08:00
|
|
|
|
# ============================================================
|
2026-01-28 11:22:08 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def handle_inbound(data):
|
|
|
|
|
|
try:
|
|
|
|
|
|
base_id = data.get('base_id')
|
2026-02-06 17:11:47 +08:00
|
|
|
|
if not base_id:
|
|
|
|
|
|
raise ValueError("必须选择基础物料")
|
|
|
|
|
|
|
2026-01-28 11:22:08 +08:00
|
|
|
|
material = MaterialBase.query.get(base_id)
|
2026-02-06 17:11:47 +08:00
|
|
|
|
if not material:
|
|
|
|
|
|
raise ValueError("所选物料不存在")
|
|
|
|
|
|
|
2026-02-10 13:50:26 +08:00
|
|
|
|
# [核心修改] 后端二次校验:如果物料已停用,禁止入库
|
|
|
|
|
|
if not material.is_enabled:
|
|
|
|
|
|
raise ValueError(f"物料【{material.name}】已停用,无法办理新入库。")
|
|
|
|
|
|
|
2026-02-06 17:11:47 +08:00
|
|
|
|
# --- [修复点] 执行唯一性校验 ---
|
|
|
|
|
|
BuyInboundService._check_unique(
|
|
|
|
|
|
base_id=base_id,
|
|
|
|
|
|
serial_number=data.get('serial_number'),
|
|
|
|
|
|
batch_number=data.get('batch_number')
|
|
|
|
|
|
)
|
2026-01-27 15:50:23 +08:00
|
|
|
|
|
2026-02-06 17:11:47 +08:00
|
|
|
|
# 时间处理 (强制北京时间)
|
2026-02-05 14:36:36 +08:00
|
|
|
|
beijing_tz = timezone(timedelta(hours=8))
|
|
|
|
|
|
current_time = datetime.now(beijing_tz).replace(tzinfo=None)
|
2026-02-05 14:30:11 +08:00
|
|
|
|
in_date_val = current_time
|
|
|
|
|
|
|
2026-01-27 15:50:23 +08:00
|
|
|
|
if data.get('in_date'):
|
|
|
|
|
|
try:
|
2026-01-30 11:50:35 +08:00
|
|
|
|
date_str = str(data['in_date'])
|
2026-02-05 14:30:11 +08:00
|
|
|
|
if len(date_str) > 10:
|
|
|
|
|
|
in_date_val = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
|
|
|
|
|
|
else:
|
|
|
|
|
|
d_temp = datetime.strptime(date_str, '%Y-%m-%d')
|
|
|
|
|
|
in_date_val = datetime(d_temp.year, d_temp.month, d_temp.day,
|
|
|
|
|
|
current_time.hour, current_time.minute, current_time.second)
|
2026-02-05 11:37:06 +08:00
|
|
|
|
except:
|
2026-02-05 14:30:11 +08:00
|
|
|
|
in_date_val = current_time
|
2026-01-27 15:50:23 +08:00
|
|
|
|
|
2026-01-28 11:22:08 +08:00
|
|
|
|
in_qty = float(data.get('in_quantity') or 0)
|
|
|
|
|
|
u_price = float(data.get('unit_price') or 0)
|
2026-01-27 16:43:44 +08:00
|
|
|
|
|
2026-02-06 17:11:47 +08:00
|
|
|
|
# 获取全局打印ID
|
2026-02-06 10:16:37 +08:00
|
|
|
|
try:
|
|
|
|
|
|
seq_sql = text("SELECT nextval('global_print_seq')")
|
|
|
|
|
|
result = db.session.execute(seq_sql)
|
|
|
|
|
|
next_global_id = result.scalar()
|
2026-02-06 17:11:47 +08:00
|
|
|
|
except:
|
2026-02-06 10:16:37 +08:00
|
|
|
|
next_global_id = None
|
|
|
|
|
|
|
2026-02-06 17:11:47 +08:00
|
|
|
|
# SKU 生成
|
2026-02-06 10:16:37 +08:00
|
|
|
|
if next_global_id:
|
|
|
|
|
|
generated_sku = str(next_global_id).zfill(10)
|
|
|
|
|
|
else:
|
2026-02-06 17:11:47 +08:00
|
|
|
|
generated_sku = datetime.now().strftime('%Y%m%d%H%M%S')
|
2026-02-02 15:06:20 +08:00
|
|
|
|
|
2026-02-05 11:37:06 +08:00
|
|
|
|
final_barcode = data.get('barcode') or generated_sku
|
2026-02-02 15:06:20 +08:00
|
|
|
|
|
2026-02-03 11:55:33 +08:00
|
|
|
|
arrival_list = data.get('arrival_photo', [])
|
|
|
|
|
|
report_list = data.get('inspection_report', [])
|
|
|
|
|
|
|
2026-01-27 15:50:23 +08:00
|
|
|
|
new_stock = StockBuy(
|
|
|
|
|
|
base_id=material.id,
|
2026-02-02 15:06:20 +08:00
|
|
|
|
global_print_id=next_global_id,
|
2026-02-05 11:37:06 +08:00
|
|
|
|
sku=generated_sku,
|
|
|
|
|
|
barcode=final_barcode,
|
2026-02-06 17:11:47 +08:00
|
|
|
|
in_date=in_date_val,
|
2026-01-27 15:50:23 +08:00
|
|
|
|
serial_number=data.get('serial_number'),
|
|
|
|
|
|
batch_number=data.get('batch_number'),
|
2026-02-05 14:30:11 +08:00
|
|
|
|
status=data.get('status', '在库'),
|
2026-01-27 16:43:44 +08:00
|
|
|
|
in_quantity=in_qty,
|
2026-02-06 17:11:47 +08:00
|
|
|
|
stock_quantity=in_qty, # 初始库存等于入库数
|
2026-01-27 16:43:44 +08:00
|
|
|
|
available_quantity=in_qty,
|
2026-01-28 11:22:08 +08:00
|
|
|
|
inspection_status=data.get('inspection_status', '未检'),
|
|
|
|
|
|
warehouse_location=data.get('warehouse_location'),
|
2026-01-27 16:43:44 +08:00
|
|
|
|
unit_price=u_price,
|
|
|
|
|
|
total_price=in_qty * u_price,
|
2026-01-27 15:50:23 +08:00
|
|
|
|
currency=data.get('currency', 'CNY'),
|
|
|
|
|
|
exchange_rate=data.get('exchange_rate', 1.0),
|
|
|
|
|
|
supplier_name=data.get('supplier_name'),
|
2026-01-28 11:22:08 +08:00
|
|
|
|
buyer_name=data.get('purchaser'),
|
|
|
|
|
|
buyer_email=data.get('purchaser_email'),
|
|
|
|
|
|
original_link=data.get('source_link'),
|
2026-01-27 15:50:23 +08:00
|
|
|
|
detail_link=data.get('detail_link'),
|
2026-02-03 11:55:33 +08:00
|
|
|
|
arrival_photo=json.dumps(arrival_list),
|
|
|
|
|
|
inspection_report=json.dumps(report_list)
|
2026-01-27 15:50:23 +08:00
|
|
|
|
)
|
|
|
|
|
|
db.session.add(new_stock)
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
return new_stock
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
db.session.rollback()
|
|
|
|
|
|
raise e
|
|
|
|
|
|
|
2026-02-05 11:08:29 +08:00
|
|
|
|
# ============================================================
|
|
|
|
|
|
# 3. 更新入库逻辑
|
|
|
|
|
|
# ============================================================
|
2026-01-27 15:50:23 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def update_inbound(stock_id, data):
|
|
|
|
|
|
try:
|
|
|
|
|
|
stock = StockBuy.query.get(stock_id)
|
2026-02-06 17:11:47 +08:00
|
|
|
|
if not stock:
|
|
|
|
|
|
raise ValueError("记录不存在")
|
|
|
|
|
|
|
|
|
|
|
|
# --- [修复点] 编辑时也要校验唯一性 (排除自身ID) ---
|
|
|
|
|
|
# 如果修改了物料(base_id),或者修改了SN/BN,都需要校验
|
|
|
|
|
|
new_base_id = data.get('base_id', stock.base_id)
|
|
|
|
|
|
new_sn = data.get('serial_number', stock.serial_number)
|
|
|
|
|
|
new_bn = data.get('batch_number', stock.batch_number)
|
|
|
|
|
|
|
|
|
|
|
|
BuyInboundService._check_unique(
|
|
|
|
|
|
base_id=new_base_id,
|
|
|
|
|
|
serial_number=new_sn,
|
|
|
|
|
|
batch_number=new_bn,
|
|
|
|
|
|
exclude_id=stock_id
|
|
|
|
|
|
)
|
2026-01-27 15:50:23 +08:00
|
|
|
|
|
2026-02-06 17:11:47 +08:00
|
|
|
|
# 更新字段
|
2026-01-28 11:22:08 +08:00
|
|
|
|
field_mapping = {
|
2026-02-06 17:11:47 +08:00
|
|
|
|
'sku': 'sku', 'barcode': 'barcode', 'base_id': 'base_id',
|
2026-01-28 11:22:08 +08:00
|
|
|
|
'warehouse_location': 'warehouse_location',
|
2026-02-05 11:37:06 +08:00
|
|
|
|
'serial_number': 'serial_number', 'batch_number': 'batch_number',
|
|
|
|
|
|
'status': 'status', 'inspection_status': 'inspection_status',
|
|
|
|
|
|
'supplier_name': 'supplier_name', 'detail_link': 'detail_link',
|
|
|
|
|
|
'currency': 'currency', 'exchange_rate': 'exchange_rate',
|
|
|
|
|
|
'purchaser': 'buyer_name', 'purchaser_email': 'buyer_email',
|
2026-02-03 11:55:33 +08:00
|
|
|
|
'source_link': 'original_link'
|
2026-01-28 11:22:08 +08:00
|
|
|
|
}
|
2026-02-05 11:37:06 +08:00
|
|
|
|
for k, v in field_mapping.items():
|
|
|
|
|
|
if k in data: setattr(stock, v, data[k])
|
2026-01-28 11:22:08 +08:00
|
|
|
|
|
2026-02-05 11:37:06 +08:00
|
|
|
|
if 'arrival_photo' in data and isinstance(data['arrival_photo'], list):
|
|
|
|
|
|
stock.arrival_photo = json.dumps(data['arrival_photo'])
|
|
|
|
|
|
if 'inspection_report' in data and isinstance(data['inspection_report'], list):
|
|
|
|
|
|
stock.inspection_report = json.dumps(data['inspection_report'])
|
2026-01-27 15:50:23 +08:00
|
|
|
|
|
2026-02-06 17:11:47 +08:00
|
|
|
|
# 库存数量变更逻辑
|
2026-01-28 11:22:08 +08:00
|
|
|
|
if 'in_quantity' in data:
|
|
|
|
|
|
new_qty = float(data['in_quantity'])
|
2026-02-05 11:37:06 +08:00
|
|
|
|
diff = new_qty - float(stock.in_quantity)
|
|
|
|
|
|
if diff != 0:
|
2026-01-27 15:50:23 +08:00
|
|
|
|
stock.in_quantity = new_qty
|
|
|
|
|
|
stock.stock_quantity = float(stock.stock_quantity) + diff
|
|
|
|
|
|
stock.available_quantity = float(stock.available_quantity) + diff
|
|
|
|
|
|
|
2026-01-28 11:22:08 +08:00
|
|
|
|
if 'unit_price' in data:
|
2026-02-05 11:37:06 +08:00
|
|
|
|
stock.unit_price = float(data['unit_price'])
|
2026-01-27 15:50:23 +08:00
|
|
|
|
|
2026-02-05 11:37:06 +08:00
|
|
|
|
stock.total_price = float(stock.in_quantity) * float(stock.unit_price)
|
2026-01-27 15:50:23 +08:00
|
|
|
|
db.session.commit()
|
|
|
|
|
|
return stock
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
db.session.rollback()
|
|
|
|
|
|
raise e
|
|
|
|
|
|
|
2026-02-05 11:08:29 +08:00
|
|
|
|
# ============================================================
|
|
|
|
|
|
# 4. 删除逻辑
|
|
|
|
|
|
# ============================================================
|
2026-01-27 15:50:23 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def delete_inbound(stock_id):
|
|
|
|
|
|
try:
|
|
|
|
|
|
stock = StockBuy.query.get(stock_id)
|
2026-02-05 11:37:06 +08:00
|
|
|
|
if not stock: raise ValueError("记录不存在")
|
2026-01-27 15:50:23 +08:00
|
|
|
|
db.session.delete(stock)
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
return True
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
db.session.rollback()
|
|
|
|
|
|
raise e
|
|
|
|
|
|
|
2026-02-05 11:08:29 +08:00
|
|
|
|
# ============================================================
|
2026-02-06 17:11:47 +08:00
|
|
|
|
# 5. 获取列表
|
2026-02-05 11:08:29 +08:00
|
|
|
|
# ============================================================
|
|
|
|
|
|
@staticmethod
|
2026-02-05 11:37:06 +08:00
|
|
|
|
def get_list(page, limit, keyword=None, statuses=None):
|
2026-02-05 11:08:29 +08:00
|
|
|
|
try:
|
2026-01-28 11:22:08 +08:00
|
|
|
|
query = db.session.query(StockBuy).outerjoin(MaterialBase, StockBuy.base_id == MaterialBase.id)
|
|
|
|
|
|
|
|
|
|
|
|
if keyword:
|
2026-02-05 11:37:06 +08:00
|
|
|
|
kw = f'%{keyword}%'
|
2026-01-28 11:22:08 +08:00
|
|
|
|
query = query.filter(
|
|
|
|
|
|
or_(
|
2026-02-05 11:37:06 +08:00
|
|
|
|
MaterialBase.name.ilike(kw),
|
|
|
|
|
|
MaterialBase.spec_model.ilike(kw),
|
|
|
|
|
|
StockBuy.batch_number.ilike(kw),
|
|
|
|
|
|
StockBuy.serial_number.ilike(kw),
|
|
|
|
|
|
StockBuy.sku.ilike(kw),
|
|
|
|
|
|
StockBuy.supplier_name.ilike(kw)
|
2026-01-28 11:22:08 +08:00
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-02-05 11:37:06 +08:00
|
|
|
|
if not statuses:
|
|
|
|
|
|
statuses = ['在库', '借库']
|
|
|
|
|
|
|
|
|
|
|
|
if '已出库' in statuses:
|
|
|
|
|
|
query = query.filter(StockBuy.status.in_(statuses))
|
|
|
|
|
|
else:
|
2026-02-06 17:11:47 +08:00
|
|
|
|
query = query.filter(and_(StockBuy.status.in_(statuses), StockBuy.stock_quantity > 0))
|
2026-01-28 11:22:08 +08:00
|
|
|
|
|
2026-02-05 14:30:11 +08:00
|
|
|
|
pagination = query.order_by(StockBuy.in_date.desc()).paginate(page=page, per_page=limit, error_out=False)
|
2026-01-28 11:22:08 +08:00
|
|
|
|
current_items = pagination.items
|
2026-02-05 11:37:06 +08:00
|
|
|
|
|
|
|
|
|
|
def parse_img(json_str):
|
|
|
|
|
|
if not json_str: return []
|
2026-02-03 11:55:33 +08:00
|
|
|
|
try:
|
2026-02-05 11:37:06 +08:00
|
|
|
|
return json.loads(json_str) if json_str.startswith('[') else [json_str]
|
2026-02-03 11:55:33 +08:00
|
|
|
|
except:
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
2026-01-28 11:22:08 +08:00
|
|
|
|
items = []
|
|
|
|
|
|
for item in current_items:
|
2026-02-05 11:37:06 +08:00
|
|
|
|
qty_stock = float(item.stock_quantity or 0)
|
2026-02-05 11:08:29 +08:00
|
|
|
|
qty_avail = float(item.available_quantity or 0)
|
|
|
|
|
|
|
2026-02-05 14:30:11 +08:00
|
|
|
|
date_display = ''
|
|
|
|
|
|
if item.in_date:
|
|
|
|
|
|
try:
|
|
|
|
|
|
date_display = item.in_date.strftime('%Y-%m-%d')
|
|
|
|
|
|
except:
|
|
|
|
|
|
date_display = str(item.in_date)[:10]
|
|
|
|
|
|
|
2026-01-28 11:22:08 +08:00
|
|
|
|
d = {
|
|
|
|
|
|
'id': item.id,
|
|
|
|
|
|
'base_id': item.base_id,
|
2026-02-10 11:13:07 +08:00
|
|
|
|
# [核心修改] 确保这里从关联的 .base 获取信息
|
|
|
|
|
|
'material_name': item.base.name if item.base else '',
|
|
|
|
|
|
'spec_model': item.base.spec_model if item.base else '',
|
|
|
|
|
|
'category': item.base.category if item.base else '',
|
|
|
|
|
|
'unit': item.base.unit if item.base else '',
|
|
|
|
|
|
'material_type': item.base.material_type if item.base else '',
|
2026-01-28 11:22:08 +08:00
|
|
|
|
|
|
|
|
|
|
'sku': item.sku,
|
2026-02-06 17:11:47 +08:00
|
|
|
|
'inbound_date': date_display,
|
2026-01-28 11:22:08 +08:00
|
|
|
|
'barcode': item.barcode,
|
|
|
|
|
|
'serial_number': item.serial_number,
|
|
|
|
|
|
'batch_number': item.batch_number,
|
2026-02-05 11:37:06 +08:00
|
|
|
|
'status': item.status,
|
2026-01-28 11:22:08 +08:00
|
|
|
|
'inspection_status': item.inspection_status,
|
2026-02-05 11:37:06 +08:00
|
|
|
|
'qty_inbound': float(item.in_quantity or 0),
|
|
|
|
|
|
'qty_stock': qty_stock,
|
2026-02-05 11:08:29 +08:00
|
|
|
|
'qty_available': qty_avail,
|
2026-01-28 11:22:08 +08:00
|
|
|
|
'warehouse_loc': item.warehouse_location,
|
|
|
|
|
|
'unit_price': float(item.unit_price or 0),
|
|
|
|
|
|
'total_price': float(item.total_price or 0),
|
|
|
|
|
|
'currency': item.currency,
|
|
|
|
|
|
'exchange_rate': float(item.exchange_rate or 1),
|
|
|
|
|
|
'supplier_name': item.supplier_name,
|
|
|
|
|
|
'purchaser': item.buyer_name,
|
|
|
|
|
|
'purchaser_email': item.buyer_email,
|
|
|
|
|
|
'source_link': item.original_link,
|
|
|
|
|
|
'detail_link': item.detail_link,
|
2026-02-05 11:37:06 +08:00
|
|
|
|
'arrival_photo': parse_img(item.arrival_photo),
|
|
|
|
|
|
'inspection_report': parse_img(item.inspection_report),
|
2026-02-06 17:11:47 +08:00
|
|
|
|
'global_print_id': item.global_print_id
|
2026-01-28 11:22:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
items.append(d)
|
|
|
|
|
|
|
2026-01-27 15:50:23 +08:00
|
|
|
|
return {"total": pagination.total, "items": items}
|
|
|
|
|
|
except Exception as e:
|
2026-01-28 11:22:08 +08:00
|
|
|
|
traceback.print_exc()
|
2026-01-27 15:50:23 +08:00
|
|
|
|
return {"total": 0, "items": []}
|