1
0
forked from erp-dev/erp

fix: added merchant_id in models of shipment module

This commit is contained in:
2026-01-14 19:43:29 +08:00
parent 8846446ecf
commit 9a367caab7
11 changed files with 558 additions and 10 deletions

View File

@@ -0,0 +1,90 @@
from django.db import migrations, models
import django.db.models.deletion
def backfill_shipment_salesitem_merchant(apps, schema_editor):
"""
为历史数据补齐 merchant本项目当前声称未上线但迁移要可重复执行且安全
- Shipment.merchant: 从 customer.merchant 推导
- SalesItem.merchant:
- 优先从 shipment.merchant 推导
- 否则从 created_by.employee.merchant 推导(如果存在)
"""
Shipment = apps.get_model('shipment', 'Shipment')
SalesItem = apps.get_model('shipment', 'SalesItem')
# Shipment: merchant = customer.merchant
for s in Shipment.objects.filter(merchant__isnull=True).select_related('customer'):
if s.customer_id:
s.merchant_id = s.customer.merchant_id
s.save(update_fields=['merchant'])
# SalesItem: merchant = shipment.merchant else created_by.employee.merchant
# 注意:迁移态下 employee 关系可能不存在,需 try/except
for item in SalesItem.objects.filter(merchant__isnull=True).select_related('shipment', 'shipment__merchant', 'created_by'):
if item.shipment_id and getattr(item.shipment, 'merchant_id', None):
item.merchant_id = item.shipment.merchant_id
item.save(update_fields=['merchant'])
continue
user = getattr(item, 'created_by', None)
emp = getattr(user, 'employee', None) if user else None
merchant_id = getattr(emp, 'merchant_id', None) if emp else None
if merchant_id:
item.merchant_id = merchant_id
item.save(update_fields=['merchant'])
class Migration(migrations.Migration):
dependencies = [
('basic_info', '0013_alter_customer_options'),
('shipment', '0005_fix_shipment_external_finished_product_relation'),
]
operations = [
migrations.AddField(
model_name='shipment',
name='merchant',
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name='shipments',
to='basic_info.merchant',
verbose_name='所属商户',
),
),
migrations.AddField(
model_name='salesitem',
name='merchant',
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name='sales_items',
to='basic_info.merchant',
verbose_name='所属商户',
),
),
migrations.RunPython(backfill_shipment_salesitem_merchant, migrations.RunPython.noop),
migrations.AlterField(
model_name='shipment',
name='merchant',
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name='shipments',
to='basic_info.merchant',
verbose_name='所属商户',
),
),
migrations.AlterField(
model_name='salesitem',
name='merchant',
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name='sales_items',
to='basic_info.merchant',
verbose_name='所属商户',
),
),
]

View File

@@ -103,6 +103,13 @@ class Shipment(ModelBase):
help_text='用于兼容外部遗留系统的订单号(可空)',
)
merchant = models.ForeignKey(
basic_models.Merchant,
on_delete=models.PROTECT,
related_name='shipments',
verbose_name='所属商户',
)
customer = models.ForeignKey(
basic_models.Customer,
on_delete=models.PROTECT,
@@ -167,6 +174,13 @@ class SalesItem(ModelBase):
verbose_name='出货单',
help_text='为空表示待分配'
)
merchant = models.ForeignKey(
basic_models.Merchant,
on_delete=models.PROTECT,
related_name='sales_items',
verbose_name='所属商户',
)
name = models.CharField(
max_length=200,

View File

@@ -8,7 +8,7 @@ from typing import List
from django.db import transaction
from django.db.models import QuerySet
from shipment.models import SalesItem, Shipment
from shipment.models import ExternalFinishedProduct, SalesItem, Shipment
def get_sales_items_by_printing_order(
@@ -73,6 +73,15 @@ def create_shipment(
customer = Customer.objects.get(id=customer_id)
except Customer.DoesNotExist:
raise ValueError(f'客户 {customer_id} 不存在')
# merchant 隔离:必须能解析出当前用户 merchant
emp = getattr(created_by, 'employee', None)
merchant = getattr(emp, 'merchant', None) if emp else None
if not merchant:
# superuser 也必须绑定 merchant避免产生无法隔离的数据
raise ValueError('用户未关联商户,无法创建出货单')
if customer.merchant_id != merchant.id:
raise ValueError('无权限为该客户创建出货单')
# 验证销售品
if sales_item_ids:
@@ -92,6 +101,7 @@ def create_shipment(
# 创建出货单
shipment = Shipment.objects.create(
merchant=merchant,
customer=customer,
shipment_date=shipment_date,
remark=remark,
@@ -100,6 +110,69 @@ def create_shipment(
# 关联销售品
if sales_item_ids:
SalesItem.objects.filter(id__in=sales_item_ids).update(shipment=shipment)
updated = SalesItem.objects.filter(id__in=sales_item_ids, merchant=merchant).update(shipment=shipment)
if updated != len(sales_item_ids):
raise ValueError('存在不属于当前商户的销售品,无法关联到出货单')
return shipment
@transaction.atomic
def create_external_shipment(
customer_id: int,
shipment_date,
external_id: str,
external_finished_products: List[dict],
created_by,
remark: str = '',
) -> Shipment:
"""
创建出货单external 版),并批量写入外部成品表并关联到出货单。
特点:
- 不绑定任何 SalesItem
- external_id 必填
- external_finished_products 必填(至少 1 条)
"""
from basic_info.models import Customer
# 验证客户存在
try:
customer = Customer.objects.get(id=customer_id)
except Customer.DoesNotExist:
raise ValueError(f'客户 {customer_id} 不存在')
# merchant 隔离
emp = getattr(created_by, 'employee', None)
merchant = getattr(emp, 'merchant', None) if emp else None
if not merchant:
raise ValueError('用户未关联商户,无法创建出货单')
if customer.merchant_id != merchant.id:
raise ValueError('无权限为该客户创建出货单')
external_id = (external_id or '').strip()
if not external_id:
raise ValueError('external_id 不能为空')
if not external_finished_products:
raise ValueError('external_finished_products 不能为空')
shipment = Shipment.objects.create(
merchant=merchant,
customer=customer,
shipment_date=shipment_date,
remark=remark,
created_by=created_by,
external_id=external_id,
)
objs = []
for item in external_finished_products:
objs.append(ExternalFinishedProduct(
shipment=shipment,
style_name=item.get('style_name', ''),
num_of_rolls=item.get('num_of_rolls', 0),
created_by=created_by,
))
ExternalFinishedProduct.objects.bulk_create(objs)
return shipment