diff --git a/apps/wpm/services.py b/apps/wpm/services.py index 93db0bbb..124617b8 100644 --- a/apps/wpm/services.py +++ b/apps/wpm/services.py @@ -226,6 +226,7 @@ def mlog_submit(mlog: Mlog, user: User, now: Union[datetime.datetime, None]): if material_out: # 需要入车间库存 + tracking = material_out.tracking into_wm_mgroup = material_out.process.into_wm_mgroup if material_out.process else False need_store_notok = material_out.process.store_notok if material_out.process else False m_outs = Mlogb.objects.filter(mlog=mlog, material_out__isnull=False) @@ -235,7 +236,7 @@ def mlog_submit(mlog: Mlog, user: User, now: Union[datetime.datetime, None]): m_outs_list = [(mo.material_out, mo.batch if mo.batch else mlog.batch, mo.count_ok, mlog.count_real_eweight, None, mo) for mo in m_outs.all()] if need_store_notok: for item in m_outs: - if Mlogbw.objects.filter(mlogb=item).exists(): + if tracking == Material.MA_TRACKING_SINGLE: # 获取所有主要的不合格项 bw_qs = Mlogbw.objects.filter(mlogb=item) defectIds= FtestDefect.objects.filter(ftest__mlogbw_ftest__in=bw_qs, is_main=True, has=True).values_list("defect__id", flat=True).distinct() @@ -253,7 +254,6 @@ def mlog_submit(mlog: Mlog, user: User, now: Union[datetime.datetime, None]): if xcount > 0: defect = defects_map[defect_id] m_outs_list.append((item.material_out, item.batch, xcount, 0, defect, item)) - else: for f in Mlogb._meta.fields: if 'count_n_' in f.name and getattr(item, f.name) > 0: @@ -352,22 +352,45 @@ def mlog_revert(mlog: Mlog, user: User, now: Union[datetime.datetime, None]): (mo.material_out, mo.batch if mo.batch else mlog.batch, mo.count_ok, mlog.count_real_eweight, None, mo) for mo in m_outs.all()] if stored_notok: - for item in m_outs: - for f in Mlogb._meta.fields: - if 'count_n_' in f.name and getattr(item, f.name) > 0: - notok_sign = f.name.replace('count_n_', '') - m_outs_list.append((item.material_out, item.batch if item.batch else mlog.batch, - getattr(item, f.name), mlog.count_real_eweight, notok_sign, item)) + for item in m_outs: + if material_out.tracking == Material.MA_TRACKING_SINGLE: + # 获取所有主要的不合格项 + bw_qs = Mlogbw.objects.filter(mlogb=item) + defectIds= FtestDefect.objects.filter(ftest__mlogbw_ftest__in=bw_qs, is_main=True, has=True).values_list("defect__id", flat=True).distinct() + defects_map = {d.id: d for d in Defect.objects.filter(id__in=defectIds)} + # 过滤并统计相关数据 + filtered_bw_qs = bw_qs.filter( + ftest__defects_ftest__defect__id__in=defects_map.keys(), + ftest__defects_ftest__has=True, + ftest__defects_ftest__is_main=True + ).values('ftest__defects_ftest__defect__id').annotate(xcount=Count('id')) + # 整理结果 + for defect_data in filtered_bw_qs: + defect_id = defect_data['ftest__defects_ftest__defect__id'] + xcount = defect_data['xcount'] + if xcount > 0: + defect = defects_map[defect_id] + m_outs_list.append((item.material_out, item.batch, xcount, 0, defect, item)) + else: + for f in Mlogb._meta.fields: + if 'count_n_' in f.name and getattr(item, f.name) > 0: + notok_sign = f.name.replace('count_n_', '') + m_outs_list.append((item.material_out, item.batch if item.batch else mlog.batch, + getattr(item, f.name), mlog.count_real_eweight, notok_sign, item)) else: m_outs_list = [(material_out, mlog.batch, mlog.count_ok, mlog.count_real_eweight, None, mlog)] # 一次填写的暂时不处理不合格品 for mo in m_outs_list: - mo_ma, mo_batch, mo_count, _, notok_sign, mlog_or_b = mo + mo_ma, mo_batch, mo_count, _, notok_sign_or_defect, mlog_or_b = mo if mo_count == 0: continue - wm_state = WMaterial.WM_OK if notok_sign is None else WMaterial.WM_NOTOK - lookup = {'batch': mo_batch, 'material': mo_ma, 'mgroup': None, 'notok_sign': notok_sign, 'state': wm_state} + wm_state = WMaterial.WM_OK if notok_sign_or_defect is None else WMaterial.WM_NOTOK + lookup = {'batch': mo_batch, 'material': mo_ma, 'mgroup': None, 'notok_sign': None, 'defect': None, 'state': wm_state} + if isinstance(notok_sign_or_defect, Defect): + lookup['defect'] = notok_sign_or_defect + else: + lookup['notok_sign'] = notok_sign_or_defect if stored_mgroup: lookup['mgroup'] = mgroup else: