feat: mlog work_start_time必填
This commit is contained in:
parent
6582a4dae1
commit
1929fd30a6
|
@ -4,6 +4,8 @@ from rest_framework.exceptions import ParseError
|
|||
from apps.utils.models import CommonBDModel
|
||||
from collections import defaultdict, deque
|
||||
from django.db.models import Q
|
||||
from datetime import datetime, timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
class Process(CommonBModel):
|
||||
"""
|
||||
|
@ -177,6 +179,30 @@ class Mgroup(CommonBModel):
|
|||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
def get_shift(self, w_s_time:datetime):
|
||||
# 如果没有时区信息,使用默认时区(东八区)
|
||||
if not timezone.is_aware(w_s_time):
|
||||
w_s_time = timezone.make_aware(w_s_time)
|
||||
else:
|
||||
w_s_time = timezone.localtime(w_s_time)
|
||||
|
||||
shifts = Shift.objects.filter(rule=self.shift_rule).order_by('sort')
|
||||
# 处理跨天班次的情况
|
||||
for shift in shifts:
|
||||
# 如果开始时间小于结束时间,表示班次在同一天内
|
||||
if shift.start_time_o < shift.end_time_o:
|
||||
if shift.start_time_o <= w_s_time.time() < shift.end_time_o:
|
||||
return w_s_time.date(), shift
|
||||
else: # 班次跨天(如夜班从当天晚上到次日凌晨)
|
||||
if w_s_time.time() >= shift.start_time_o or w_s_time.time() < shift.end_time_o:
|
||||
# 如果当前时间在开始时间之后,属于当天
|
||||
if w_s_time.time() >= shift.start_time_o:
|
||||
return w_s_time.date(), shift
|
||||
# 如果当前时间在结束时间之前,属于前一天
|
||||
else:
|
||||
return (w_s_time - timedelta(days=1)).date(), shift
|
||||
return w_s_time.date(), None
|
||||
|
||||
|
||||
class TeamMember(BaseModel):
|
||||
|
|
|
@ -354,11 +354,12 @@ class MlogSerializer(CustomModelSerializer):
|
|||
model = Mlog
|
||||
fields = '__all__'
|
||||
read_only_fields = EXCLUDE_FIELDS + \
|
||||
['submit_time', 'submit_user', 'material_outs']
|
||||
['submit_time', 'submit_user', 'material_outs', "handle_date", "shift"]
|
||||
extra_kwargs = {
|
||||
"batch": {"required": True},
|
||||
"shift": {"required": True},
|
||||
"material_out": {"required": True}
|
||||
"shift": {"required": False},
|
||||
"material_out": {"required": True},
|
||||
"work_start_time": {"required": True}
|
||||
}
|
||||
|
||||
def create(self, validated_data):
|
||||
|
@ -611,12 +612,7 @@ class MlogSerializer(CustomModelSerializer):
|
|||
raise ParseError('合格数量不能小于0')
|
||||
if attrs['count_real'] != attrs['count_ok'] + attrs['count_notok']:
|
||||
raise ParseError('生产数量需等于合格数量+不合格数量')
|
||||
if mtask:
|
||||
if mtask.start_date == mtask.end_date:
|
||||
attrs['handle_date'] = mtask.start_date
|
||||
else:
|
||||
if attrs.get('work_end_time', None):
|
||||
attrs['handle_date'] = localdate(attrs['work_end_time'])
|
||||
|
||||
mtaskb: Mtaskb = attrs.get('mtaskb', None)
|
||||
if mtaskb:
|
||||
mtask = mtaskb.mtask
|
||||
|
@ -629,13 +625,21 @@ class MlogSerializer(CustomModelSerializer):
|
|||
if wm_in and wm_in.material != mtask.material_in:
|
||||
raise ParseError('消耗物料与任务不一致')
|
||||
attrs['material_out'] = material_out
|
||||
if mtask.start_date == mtask.end_date:
|
||||
attrs['handle_date'] = mtask.end_date
|
||||
else:
|
||||
mgroup = attrs['mgroup']
|
||||
material_out = attrs['material_out']
|
||||
if not (mgroup and material_out):
|
||||
raise ParseError('缺少工段或产物!')
|
||||
|
||||
# 时间
|
||||
mgroup:Mgroup = attrs['mgroup']
|
||||
work_start_time:datetime = attrs['work_start_time']
|
||||
handle_date, attrs["shift"] = mgroup.get_shift(work_start_time)
|
||||
if mtask.start_date == mtask.end_date:
|
||||
attrs['handle_date'] = mtask.end_date
|
||||
if attrs['handle_date'] != handle_date:
|
||||
raise ParseError('任务日期与生产日期不一致')
|
||||
|
||||
handle_user = attrs.get('handle_user', None)
|
||||
if handle_user is None and hasattr(self, "request"):
|
||||
handle_user = self.request.user
|
||||
|
@ -685,6 +689,8 @@ class MlogInitSerializer(CustomModelSerializer):
|
|||
# 如果已经确定产出,则自动获取qct
|
||||
if attrs.get("material_out", None):
|
||||
attrs["qct"] = Qct.get(attrs["material_out"], "process", "out")
|
||||
|
||||
attrs["handle_date"], attrs["shift"] = mgroup.get_shift(attrs['work_start_time'])
|
||||
return attrs
|
||||
|
||||
class MlogChangeSerializer(CustomModelSerializer):
|
||||
|
@ -1518,7 +1524,7 @@ class BatchMgroupSerializer(serializers.Serializer):
|
|||
|
||||
class MlogQuickSerializer(serializers.Serializer):
|
||||
work_start_time = serializers.DateTimeField(label="开始时间")
|
||||
handle_date = serializers.DateField(label="操作日期")
|
||||
# handle_date = serializers.DateField(label="操作日期")
|
||||
work_end_time = serializers.DateTimeField(label="结束时间", required=False)
|
||||
team = serializers.CharField(label="班组ID", required=False)
|
||||
equipment = serializers.CharField(label="设备ID", required=False)
|
||||
|
|
|
@ -430,7 +430,6 @@ class MlogViewSet(CustomModelViewSet):
|
|||
vdata = sr.validated_data
|
||||
mloginit_data = {"mgroup": vdata["mgroup"],
|
||||
"work_start_time": vdata["work_start_time"],
|
||||
"handle_date": vdata["handle_date"],
|
||||
"handle_user": vdata["handle_user"],
|
||||
"is_fix": vdata["is_fix"], "create_by": request.user}
|
||||
if "work_end_time" in vdata:
|
||||
|
|
Loading…
Reference in New Issue