forked from erp-dev/erp
beta
This commit is contained in:
19
mes/migrations/0004_production_assignment_device_nullable.py
Normal file
19
mes/migrations/0004_production_assignment_device_nullable.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# Generated by Django 5.2.8 on 2026-07-01 06:55
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('mes', '0003_productionassignment_production_quantity_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='productionassignment',
|
||||
name='device',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='production_assignments', to='mes.device', verbose_name='设备'),
|
||||
),
|
||||
]
|
||||
@@ -128,6 +128,8 @@ class ProductionAssignment(ModelBase):
|
||||
Device,
|
||||
on_delete=models.PROTECT,
|
||||
related_name='production_assignments',
|
||||
null=True,
|
||||
blank=True,
|
||||
verbose_name='设备',
|
||||
)
|
||||
content_type = models.ForeignKey(
|
||||
|
||||
@@ -244,14 +244,15 @@ def get_production_assignment_for_merchant(*, merchant, assignment_id: int) -> P
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def create_production_assignment(*, merchant, device: Device, content_type: ContentType, object_id: int,
|
||||
def create_production_assignment(*, merchant, device: Device | None = None,
|
||||
content_type: ContentType, object_id: int,
|
||||
assigner, production_quantity: int, assignee=None, created_by=None, operator=None,
|
||||
status: int = ProductionAssignmentStatusEnum.DRAFT, extra=None) -> ProductionAssignment:
|
||||
_assert_employee_belongs_to_merchant(employee=assigner, merchant=merchant, role='指派者')
|
||||
if assignee is not None:
|
||||
_assert_employee_belongs_to_merchant(employee=assignee, merchant=merchant, role='被指派人')
|
||||
_assert_employee_belongs_to_merchant(employee=operator, merchant=merchant, role='操作人')
|
||||
if device.merchant_id != merchant.id:
|
||||
if device is not None and device.merchant_id != merchant.id:
|
||||
raise ValueError('设备不属于当前商户')
|
||||
_validate_content_object_merchant(content_type=content_type, object_id=object_id, merchant=merchant)
|
||||
assignment = ProductionAssignment(
|
||||
@@ -277,7 +278,7 @@ def update_production_assignment(*, assignment: ProductionAssignment, operator,
|
||||
_assert_employee_belongs_to_merchant(employee=operator, merchant=assignment.merchant, role='操作人')
|
||||
assignment.operator = operator
|
||||
if device is not UNSET:
|
||||
if device.merchant_id != assignment.merchant_id:
|
||||
if device is not None and device.merchant_id != assignment.merchant_id:
|
||||
raise ValueError('设备不属于当前商户')
|
||||
assignment.device = device
|
||||
if assignee is not UNSET:
|
||||
|
||||
79
mes/tests.py
79
mes/tests.py
@@ -366,3 +366,82 @@ class MesServiceTestCase(TestCase):
|
||||
operator=self.operator,
|
||||
status=ProductionAssignmentStatusEnum.CANCELLED,
|
||||
)
|
||||
|
||||
def test_create_production_assignment_without_device(self):
|
||||
customer = Customer.objects.create(
|
||||
merchant=self.merchant,
|
||||
name='无设备客户',
|
||||
mobile='13800138002',
|
||||
)
|
||||
printing_order = PrintingOrder.objects.create(
|
||||
merchant=self.merchant,
|
||||
customer=customer,
|
||||
fabric='无设备面料',
|
||||
width='150cm',
|
||||
created_by=self.user,
|
||||
)
|
||||
content_type = ContentType.objects.get_for_model(PrintingOrder)
|
||||
|
||||
assignment = services.create_production_assignment(
|
||||
merchant=self.merchant,
|
||||
device=None,
|
||||
content_type=content_type,
|
||||
object_id=printing_order.id,
|
||||
assigner=self.operator,
|
||||
production_quantity=100,
|
||||
created_by=self.user,
|
||||
operator=self.operator,
|
||||
)
|
||||
|
||||
self.assertIsNone(assignment.device)
|
||||
self.assertIsNone(assignment.device_id)
|
||||
self.assertEqual(assignment.status, ProductionAssignmentStatusEnum.DRAFT)
|
||||
self.assertEqual(assignment.production_quantity, 100)
|
||||
|
||||
def test_update_production_assignment_clear_device(self):
|
||||
customer = Customer.objects.create(
|
||||
merchant=self.merchant,
|
||||
name='清空设备客户',
|
||||
mobile='13800138003',
|
||||
)
|
||||
printing_order = PrintingOrder.objects.create(
|
||||
merchant=self.merchant,
|
||||
customer=customer,
|
||||
fabric='清空设备面料',
|
||||
width='150cm',
|
||||
created_by=self.user,
|
||||
)
|
||||
category = services.create_device_category(
|
||||
merchant=self.merchant,
|
||||
name='打印机',
|
||||
created_by=self.user,
|
||||
operator=self.operator,
|
||||
)
|
||||
device = services.create_device(
|
||||
merchant=self.merchant,
|
||||
category=category,
|
||||
name='设备A',
|
||||
created_by=self.user,
|
||||
operator=self.operator,
|
||||
peak_capacity=120,
|
||||
)
|
||||
content_type = ContentType.objects.get_for_model(PrintingOrder)
|
||||
assignment = services.create_production_assignment(
|
||||
merchant=self.merchant,
|
||||
device=device,
|
||||
content_type=content_type,
|
||||
object_id=printing_order.id,
|
||||
assigner=self.operator,
|
||||
production_quantity=200,
|
||||
created_by=self.user,
|
||||
operator=self.operator,
|
||||
)
|
||||
self.assertIsNotNone(assignment.device)
|
||||
|
||||
updated = services.update_production_assignment(
|
||||
assignment=assignment,
|
||||
operator=self.operator,
|
||||
device=None,
|
||||
)
|
||||
self.assertIsNone(updated.device)
|
||||
self.assertIsNone(updated.device_id)
|
||||
|
||||
Reference in New Issue
Block a user