feat: 增加is_count_utask字段以确认同步utask

This commit is contained in:
caoqianming 2023-10-26 18:20:04 +08:00
parent 5ba3b4e07a
commit cbc43fdb65
7 changed files with 64 additions and 17 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2023-10-26 10:18
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mtm', '0018_material_need_route'),
]
operations = [
migrations.AddField(
model_name='route',
name='is_count_utask',
field=models.BooleanField(default=False, verbose_name='是否主任务统计'),
),
]

View File

@ -1,7 +1,7 @@
from django.db import models from django.db import models
from apps.system.models import CommonAModel, Dictionary, CommonBModel, CommonADModel, File, BaseModel from apps.system.models import CommonAModel, Dictionary, CommonBModel, CommonADModel, File, BaseModel
from django.db.models import Subquery, OuterRef from django.db.models import Subquery, OuterRef
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError, ParseError
class Process(CommonBModel): class Process(CommonBModel):
@ -154,6 +154,7 @@ class Route(CommonADModel):
Material, verbose_name='主要输入物料', on_delete=models.CASCADE, related_name='route_material_in', null=True, blank=True) Material, verbose_name='主要输入物料', on_delete=models.CASCADE, related_name='route_material_in', null=True, blank=True)
material_out = models.ForeignKey( material_out = models.ForeignKey(
Material, verbose_name='主要输出物料', on_delete=models.CASCADE, related_name='route_material_out', null=True, blank=True) Material, verbose_name='主要输出物料', on_delete=models.CASCADE, related_name='route_material_out', null=True, blank=True)
is_count_utask = models.BooleanField('是否主任务统计', default=False)
out_rate = models.FloatField('出材率', default=100, null=True, blank=True) out_rate = models.FloatField('出材率', default=100, null=True, blank=True)
@staticmethod @staticmethod
@ -166,7 +167,9 @@ class Route(CommonADModel):
rq = Route.objects.filter( rq = Route.objects.filter(
**kwargs).order_by('sort', 'process__sort', 'create_time') **kwargs).order_by('sort', 'process__sort', 'create_time')
if rq.first().material_in is None or rq.last().material_out is None or rq.last().material_out != rq.last().material: if rq.first().material_in is None or rq.last().material_out is None or rq.last().material_out != rq.last().material:
raise ValidationError('首步缺少输入/最后一步缺少输出') raise ParseError('首步缺少输入/最后一步缺少输出')
if not rq.filter(is_count_utask=True).exists():
raise ParseError('未指定统计步骤')
if autotask: if autotask:
kwargs['is_autotask'] = True kwargs['is_autotask'] = True
return rq return rq

View File

@ -4,6 +4,7 @@ from apps.utils.constants import EXCLUDE_FIELDS
from rest_framework import serializers from rest_framework import serializers
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
from apps.system.models import Dept from apps.system.models import Dept
from django.db import transaction
class ShiftSerializer(CustomModelSerializer): class ShiftSerializer(CustomModelSerializer):
@ -113,19 +114,24 @@ class RouteSerializer(CustomModelSerializer):
instance.save() instance.save()
def create(self, validated_data): def create(self, validated_data):
instance = super().create(validated_data) process = validated_data['process']
if 'material_out' in validated_data and validated_data['material_out']: if Route.objects.filter(material=validated_data['material'], process=process).exists():
pass raise ValidationError('已选择该工序')
else: with transaction.atomic():
self.gen_material_out(instance) instance = super().create(validated_data)
return instance if 'material_out' in validated_data and validated_data['material_out']:
pass
else:
self.gen_material_out(instance)
return instance
def update(self, instance, validated_data): def update(self, instance, validated_data):
validated_data.pop('material', None) validated_data.pop('material', None)
validated_data.pop('process', None) validated_data.pop('process', None)
instance = super().update(instance, validated_data) with transaction.atomic():
if 'material_out' in validated_data and validated_data['material_out']: instance = super().update(instance, validated_data)
pass if 'material_out' in validated_data and validated_data['material_out']:
else: pass
self.gen_material_out(instance) else:
return instance self.gen_material_out(instance)
return instance

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2023-10-26 10:18
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('pm', '0012_auto_20231026_1624'),
]
operations = [
migrations.AddField(
model_name='mtask',
name='is_count_utask',
field=models.BooleanField(default=False, verbose_name='是否主任务统计'),
),
]

View File

@ -63,6 +63,7 @@ class Mtask(CommonADModel):
Material, verbose_name='领用物', on_delete=models.CASCADE, related_name='mtask_material_in', null=True, blank=True) Material, verbose_name='领用物', on_delete=models.CASCADE, related_name='mtask_material_in', null=True, blank=True)
material_out = models.ForeignKey( material_out = models.ForeignKey(
Material, verbose_name='产物', on_delete=models.CASCADE, related_name='mtask_material_out') Material, verbose_name='产物', on_delete=models.CASCADE, related_name='mtask_material_out')
is_count_utask = models.BooleanField('是否主任务统计', default=False)
count = models.PositiveIntegerField('任务数', default=1) count = models.PositiveIntegerField('任务数', default=1)
count_real = models.PositiveIntegerField('实际生产数', default=0) count_real = models.PositiveIntegerField('实际生产数', default=0)
count_ok = models.PositiveIntegerField('合格数', default=0) count_ok = models.PositiveIntegerField('合格数', default=0)

View File

@ -126,7 +126,8 @@ class PmService:
'end_date': task_date, 'end_date': task_date,
'utask': utask, 'utask': utask,
'create_by': user, 'create_by': user,
'update_by': user 'update_by': user,
'is_count_utask': val.is_count_utask
}) })
@classmethod @classmethod

View File

@ -157,8 +157,8 @@ def update_mtask(mtask: Mtask):
mtask.count_notok = res['sum_count_notok'] if res['sum_count_notok'] else 0 mtask.count_notok = res['sum_count_notok'] if res['sum_count_notok'] else 0
mtask.save() mtask.save()
utask = mtask.utask utask = mtask.utask
if utask and mtask.material_out == utask.material: if utask and mtask.is_count_utask:
res2 = Mtask.objects.filter(utask=utask, material_out=utask.material).aggregate(sum_count_real=Sum( res2 = Mtask.objects.filter(utask=utask, mgroup=mtask.mgroup).aggregate(sum_count_real=Sum(
'count_real'), sum_count_ok=Sum('count_ok'), sum_count_notok=Sum('count_notok')) 'count_real'), sum_count_ok=Sum('count_ok'), sum_count_notok=Sum('count_notok'))
utask.count_real = res2['sum_count_real'] if res2['sum_count_real'] else 0 utask.count_real = res2['sum_count_real'] if res2['sum_count_real'] else 0
utask.count_ok = res2['sum_count_ok'] if res2['sum_count_ok'] else 0 utask.count_ok = res2['sum_count_ok'] if res2['sum_count_ok'] else 0