feat: ftestwork增加关联仓库库存字段

This commit is contained in:
caoqianming 2024-08-19 11:35:36 +08:00
parent c00f59f8d9
commit 9ce56d3f34
4 changed files with 53 additions and 22 deletions

View File

@ -0,0 +1,20 @@
# Generated by Django 3.2.12 on 2024-08-19 03:25
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('inm', '0001_initial'),
('qm', '0020_auto_20240814_1756'),
]
operations = [
migrations.AddField(
model_name='ftestwork',
name='mb',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='inm.materialbatch', verbose_name='关联仓库'),
),
]

View File

@ -108,6 +108,7 @@ class FtestWork(CommonBDModel):
type = models.CharField('检验类型', max_length=20, choices=FTEST_TYPE_CHOICES, default='prod') type = models.CharField('检验类型', max_length=20, choices=FTEST_TYPE_CHOICES, default='prod')
type2 = models.PositiveSmallIntegerField('检验类型2', choices=((10, '抽检'), (20, '全检')), default=10) type2 = models.PositiveSmallIntegerField('检验类型2', choices=((10, '抽检'), (20, '全检')), default=10)
wm = models.ForeignKey(WMaterial, verbose_name='关联车间库存', on_delete=models.SET_NULL, null=True, blank=True) wm = models.ForeignKey(WMaterial, verbose_name='关联车间库存', on_delete=models.SET_NULL, null=True, blank=True)
mb = models.ForeignKey('inm.materialbatch', verbose_name='关联仓库', on_delete=models.SET_NULL, null=True, blank=True)
test_date = models.DateField('检验日期') test_date = models.DateField('检验日期')
material = models.ForeignKey( material = models.ForeignKey(
Material, verbose_name='产品', on_delete=models.CASCADE) Material, verbose_name='产品', on_delete=models.CASCADE)

View File

@ -6,6 +6,7 @@ from rest_framework.exceptions import ValidationError
from apps.system.models import Dept, Dictionary from apps.system.models import Dept, Dictionary
from apps.wpm.models import SfLog, WMaterial from apps.wpm.models import SfLog, WMaterial
from django.db import transaction from django.db import transaction
from apps.inm.serializers import MaterialBatchDetailSerializer
class TestItemSerializer(CustomModelSerializer): class TestItemSerializer(CustomModelSerializer):
@ -62,33 +63,39 @@ class QuaStatUpdateSerializer(CustomModelSerializer):
class FtestWorkCreateUpdateSerializer(CustomModelSerializer): class FtestWorkCreateUpdateSerializer(CustomModelSerializer):
class Meta: class Meta:
model = FtestWork model = FtestWork
fields = ['id', 'wm', 'type', 'type2', 'test_date', 'count', 'count_sampling', 'count_ok', 'count_notok', 'count_notok_json', 'test_user'] fields = ['id', 'wm', 'mb', 'type', 'type2', 'test_date', 'count', 'count_sampling', 'count_ok', 'count_notok', 'count_notok_json', 'test_user']
extra_kwargs = {'test_user': {'required': True}, 'type': {'required': True}} extra_kwargs = {'test_user': {'required': True}, 'type': {'required': True}}
def validate(self, attrs): def validate(self, attrs):
type2 = attrs.get('type2', 20) type2 = attrs.get('type2', 20)
if type2 == 20: # 如果是全检 if type2 == 20: # 如果是全检
attrs['count_sampling'] = attrs['count'] attrs['count_sampling'] = attrs['count']
if 'wm' not in attrs: if 'wm' in attrs and attrs['wm']:
raise ValidationError('请选择车间库存') attrs['mb'] = None
wm:WMaterial = attrs['wm'] wm:WMaterial = attrs['wm']
if wm.state not in [WMaterial.WM_OK, WMaterial.WM_TEST]: if wm.state not in [WMaterial.WM_OK, WMaterial.WM_TEST]:
raise ValidationError('不支持对该物料检验') raise ValidationError('不支持对该物料检验')
attrs['material'] = wm.material attrs['material'] = wm.material
attrs['batch'] = wm.batch attrs['batch'] = wm.batch
count_notok_json = attrs.get('count_notok_json', None) count_notok_json = attrs.get('count_notok_json', None)
if count_notok_json is None: if count_notok_json is None:
raise ValidationError('不合格项不能为空') raise ValidationError('不合格项不能为空')
count_notok = 0 count_notok = 0
for k, v in count_notok_json.items(): for k, v in count_notok_json.items():
k_2 = k.replace('count_n_', '') k_2 = k.replace('count_n_', '')
if k_2 not in NotOkOption.values: if k_2 not in NotOkOption.values:
raise ValidationError(f'不支持的不合格项{k_2}') raise ValidationError(f'不支持的不合格项{k_2}')
if isinstance(v, int) and v >= 0: if isinstance(v, int) and v >= 0:
count_notok = count_notok + v count_notok = count_notok + v
else: else:
raise ValidationError(f'不合格项{k_2}必须为非负整数') raise ValidationError(f'不合格项{k_2}必须为非负整数')
attrs['count_notok'] = count_notok attrs['count_notok'] = count_notok
elif 'mb' in attrs and attrs['mb']:
attrs['wm'] = None
attrs['material'] = attrs['mb'].material
attrs['batch'] = attrs['mb'].batch
else:
raise ValidationError('请选择车间/仓库库存')
return attrs return attrs
@ -96,6 +103,7 @@ class FtestWorkSerializer(CustomModelSerializer):
material_name = serializers.StringRelatedField( material_name = serializers.StringRelatedField(
source='material', read_only=True) source='material', read_only=True)
material_cate = serializers.CharField(source='material.cate', read_only=True) material_cate = serializers.CharField(source='material.cate', read_only=True)
mb_ = MaterialBatchDetailSerializer(source='mb', read_only=True)
class Meta: class Meta:
model = FtestWork model = FtestWork

View File

@ -125,7 +125,7 @@ class FtestWorkViewSet(CustomModelViewSet):
serializer_class = FtestWorkSerializer serializer_class = FtestWorkSerializer
create_serializer_class = FtestWorkCreateUpdateSerializer create_serializer_class = FtestWorkCreateUpdateSerializer
update_serializer_class = FtestWorkCreateUpdateSerializer update_serializer_class = FtestWorkCreateUpdateSerializer
select_related_fields = ['material'] select_related_fields = ['material', 'mb', 'mb__material']
filterset_class = FtestWorkFilter filterset_class = FtestWorkFilter
def update(self, request, *args, **kwargs): def update(self, request, *args, **kwargs):
@ -148,6 +148,8 @@ class FtestWorkViewSet(CustomModelViewSet):
提交检验工作 提交检验工作
""" """
ins:FtestWork = self.get_object() ins:FtestWork = self.get_object()
if ins.wm is None:
raise ParseError('该检验工作未关联车间库存')
if ins.submit_time is None: if ins.submit_time is None:
ftestwork_submit(ins, request.user) ftestwork_submit(ins, request.user)
else: else: