forked from erp-dev/erp
fix: added merchant_id in models of shipment module
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user