feat: 修改mlog_submit和mlog_revert以支持日志多个工艺步骤
This commit is contained in:
parent
1d840e7c9d
commit
4b2bcc6d29
|
@ -190,57 +190,59 @@ def mlog_submit(mlog: Mlog, user: User, now: Union[datetime.datetime, None]):
|
||||||
exclude_batchst_ids.append(target.id)
|
exclude_batchst_ids.append(target.id)
|
||||||
BatchLog.g_create(source=source, target=target, mlog=mlog)
|
BatchLog.g_create(source=source, target=target, mlog=mlog)
|
||||||
|
|
||||||
if material_in or is_fix: # 需要进行车间库存管理
|
# 消耗物料处理
|
||||||
m_ins_list = []
|
m_ins_list = []
|
||||||
m_ins = Mlogb.objects.filter(mlog=mlog, material_in__isnull=False)
|
m_ins = Mlogb.objects.filter(mlog=mlog, material_in__isnull=False)
|
||||||
if m_ins.exists():
|
if m_ins.exists():
|
||||||
m_ins = m_ins.filter(need_inout=True)
|
m_ins = m_ins.filter(need_inout=True)
|
||||||
m_ins_list = [(mi.material_in, mi.batch if mi.batch else mi.batch, mi.count_use, None, mi) for mi in m_ins.all()]
|
m_ins_list = [(mi.material_in, mi.batch if mi.batch else mi.batch, mi.count_use, None, mi) for mi in m_ins.all()]
|
||||||
for item in m_ins:
|
for item in m_ins:
|
||||||
mbd_qs = MlogbDefect.get_defect_qs_from_mlogb(item)
|
mbd_qs = MlogbDefect.get_defect_qs_from_mlogb(item)
|
||||||
for itemx in mbd_qs:
|
for itemx in mbd_qs:
|
||||||
if itemx.defect:
|
if itemx.defect:
|
||||||
m_ins_bl_list.append((item.material_in, item.batch, itemx.count, itemx.defect, item))
|
m_ins_bl_list.append((item.material_in, item.batch, itemx.count, itemx.defect, item))
|
||||||
|
else:
|
||||||
|
m_ins_list = [(material_in, mlog.batch, mlog.count_use, None, mlog)]
|
||||||
|
for mi in m_ins_list:
|
||||||
|
mi_ma, mi_batch, mi_count, defect, mlog_or_b = mi
|
||||||
|
if mi_ma is None:
|
||||||
|
continue
|
||||||
|
if mi_count <= 0:
|
||||||
|
raise ParseError('存在非正数!')
|
||||||
|
# 需要判断领用数是否合理
|
||||||
|
# 优先使用工段库存
|
||||||
|
if isinstance(mlog_or_b, Mlogb) and mlog_or_b.wm_in:
|
||||||
|
wm_qs = WMaterial.objects.filter(id=mlog_or_b.wm_in.id)
|
||||||
else:
|
else:
|
||||||
m_ins_list = [(material_in, mlog.batch, mlog.count_use, None, mlog)]
|
wm_qs = WMaterial.objects.filter(batch=mi_batch, material=mi_ma, mgroup=mgroup, state=WMaterial.WM_OK)
|
||||||
for mi in m_ins_list:
|
if not wm_qs.exists():
|
||||||
mi_ma, mi_batch, mi_count, defect, mlog_or_b = mi
|
wm_qs = WMaterial.objects.filter(batch=mi_batch, material=mi_ma,
|
||||||
if mi_count <= 0:
|
belong_dept=belong_dept, mgroup=None, state=WMaterial.WM_OK)
|
||||||
raise ParseError('存在非正数!')
|
count_x = wm_qs.count()
|
||||||
# 需要判断领用数是否合理
|
if count_x == 1:
|
||||||
# 优先使用工段库存
|
wm = wm_qs.first()
|
||||||
if isinstance(mlog_or_b, Mlogb) and mlog_or_b.wm_in:
|
elif count_x == 0:
|
||||||
wm_qs = WMaterial.objects.filter(id=mlog_or_b.wm_in.id)
|
raise ParseError(
|
||||||
else:
|
f'{str(mi_ma)}-{mi_batch}-批次库存不存在!')
|
||||||
wm_qs = WMaterial.objects.filter(batch=mi_batch, material=mi_ma, mgroup=mgroup, state=WMaterial.WM_OK)
|
else:
|
||||||
if not wm_qs.exists():
|
raise ParseError(
|
||||||
wm_qs = WMaterial.objects.filter(batch=mi_batch, material=mi_ma,
|
f'{str(mi_ma)}-{mi_batch}-存在多个相同批次!')
|
||||||
belong_dept=belong_dept, mgroup=None, state=WMaterial.WM_OK)
|
|
||||||
count_x = wm_qs.count()
|
if mi_count > wm.count:
|
||||||
if count_x == 1:
|
raise ParseError(
|
||||||
wm = wm_qs.first()
|
f'{str(mi_ma)}-{mi_batch}-该批次车间库存不足!')
|
||||||
elif count_x == 0:
|
else:
|
||||||
raise ParseError(
|
wm.count = wm.count - mi_count
|
||||||
f'{str(mi_ma)}-{mi_batch}-批次库存不存在!')
|
wm.update_by = user
|
||||||
else:
|
wm.save()
|
||||||
raise ParseError(
|
if mi_ma.tracking == Material.MA_TRACKING_SINGLE:
|
||||||
f'{str(mi_ma)}-{mi_batch}-存在多个相同批次!')
|
mlogbws = Mlogbw.objects.filter(mlogb=mlog_or_b)
|
||||||
|
if mlogbws.count() != mi_count:
|
||||||
if mi_count > wm.count:
|
raise ParseError("日志与明细数量不一致,操作失败")
|
||||||
raise ParseError(
|
for item in mlogbws:
|
||||||
f'{str(mi_ma)}-{mi_batch}-该批次车间库存不足!')
|
if item.ftest:
|
||||||
else:
|
raise ParseError("不支持消耗物料的检验")
|
||||||
wm.count = wm.count - mi_count
|
Wpr.change_or_new(wpr=item.wpr, old_wm=wm)
|
||||||
wm.update_by = user
|
|
||||||
wm.save()
|
|
||||||
if mi_ma.tracking == Material.MA_TRACKING_SINGLE:
|
|
||||||
mlogbws = Mlogbw.objects.filter(mlogb=mlog_or_b)
|
|
||||||
if mlogbws.count() != mi_count:
|
|
||||||
raise ParseError("日志与明细数量不一致,操作失败")
|
|
||||||
for item in mlogbws:
|
|
||||||
if item.ftest:
|
|
||||||
raise ParseError("不支持消耗物料的检验")
|
|
||||||
Wpr.change_or_new(wpr=item.wpr, old_wm=wm)
|
|
||||||
|
|
||||||
# 针对加工前不良的暂时额外处理
|
# 针对加工前不良的暂时额外处理
|
||||||
if need_store_notok:
|
if need_store_notok:
|
||||||
|
@ -261,114 +263,115 @@ def mlog_submit(mlog: Mlog, user: User, now: Union[datetime.datetime, None]):
|
||||||
raise ParseError("加工前不良的物料暂不支持单件追踪")
|
raise ParseError("加工前不良的物料暂不支持单件追踪")
|
||||||
|
|
||||||
|
|
||||||
if material_out or is_fix: # 需要入车间库存
|
mlogb_out_qs = Mlogb.objects.filter(mlog=mlog, material_out__isnull=False)
|
||||||
mlogb_out_qs = Mlogb.objects.filter(mlog=mlog, material_out__isnull=False)
|
stored_notok = need_store_notok
|
||||||
stored_notok = need_store_notok
|
stored_mgroup = need_store_notok
|
||||||
stored_mgroup = need_store_notok
|
if mlogb_out_qs.exists():
|
||||||
if mlogb_out_qs.exists():
|
mlogb_out_qs = mlogb_out_qs.filter(need_inout=True)
|
||||||
mlogb_out_qs = mlogb_out_qs.filter(need_inout=True)
|
m_outs_list = [(mo.material_out, mo.batch if mo.batch else mlog.batch, mo.count_ok_full if mo.count_ok_full is not None else mo.count_ok, mlog.count_real_eweight, None, mo) for mo in mlogb_out_qs.all()]
|
||||||
m_outs_list = [(mo.material_out, mo.batch if mo.batch else mlog.batch, mo.count_ok_full if mo.count_ok_full is not None else mo.count_ok, mlog.count_real_eweight, None, mo) for mo in mlogb_out_qs.all()]
|
if need_store_notok:
|
||||||
if need_store_notok:
|
for item in mlogb_out_qs:
|
||||||
for item in mlogb_out_qs:
|
mbd_qs = MlogbDefect.get_defect_qs_from_mlogb(item)
|
||||||
mbd_qs = MlogbDefect.get_defect_qs_from_mlogb(item)
|
if item.qct is not None or mbd_qs.exists():
|
||||||
if item.qct is not None or mbd_qs.exists():
|
# if item.material_out.tracking == Material.MA_TRACKING_SINGLE:
|
||||||
# if item.material_out.tracking == Material.MA_TRACKING_SINGLE:
|
# Mlogbw.cal_count_notok(item)
|
||||||
# Mlogbw.cal_count_notok(item)
|
for itemx in mbd_qs:
|
||||||
for itemx in mbd_qs:
|
m_outs_list.append((item.material_out, item.batch, itemx.count, 0, itemx.defect, item))
|
||||||
m_outs_list.append((item.material_out, item.batch, itemx.count, 0, itemx.defect, item))
|
# # 获取所有主要的不合格项/先暂时保留
|
||||||
# # 获取所有主要的不合格项/先暂时保留
|
# bw_qs = Mlogbw.objects.filter(mlogb=item)
|
||||||
# bw_qs = Mlogbw.objects.filter(mlogb=item)
|
# defectIds= Ftest.objects.filter(mlogbw_ftest__in=bw_qs).exclude(defect_main=None).values_list("defect_main__id", flat=True).distinct()
|
||||||
# defectIds= Ftest.objects.filter(mlogbw_ftest__in=bw_qs).exclude(defect_main=None).values_list("defect_main__id", flat=True).distinct()
|
# defects_map = {d.id: d for d in Defect.objects.filter(id__in=defectIds)}
|
||||||
# defects_map = {d.id: d for d in Defect.objects.filter(id__in=defectIds)}
|
# # 过滤并统计相关数据
|
||||||
# # 过滤并统计相关数据
|
# filtered_bw_qs = bw_qs.filter(
|
||||||
# filtered_bw_qs = bw_qs.filter(
|
# ftest__defect_main__id__in=defects_map.keys()
|
||||||
# ftest__defect_main__id__in=defects_map.keys()
|
# ).values('ftest__defect_main__id').annotate(xcount=Count('id'))
|
||||||
# ).values('ftest__defect_main__id').annotate(xcount=Count('id'))
|
# # 整理结果
|
||||||
# # 整理结果
|
# for defect_data in filtered_bw_qs:
|
||||||
# for defect_data in filtered_bw_qs:
|
# defect_id = defect_data['ftest__defect_main__id']
|
||||||
# defect_id = defect_data['ftest__defect_main__id']
|
# xcount = defect_data['xcount']
|
||||||
# xcount = defect_data['xcount']
|
# if xcount > 0:
|
||||||
# if xcount > 0:
|
# defect = defects_map[defect_id]
|
||||||
# defect = defects_map[defect_id]
|
# m_outs_list.append((item.material_out, item.batch, xcount, 0, defect, item))
|
||||||
# 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))
|
|
||||||
stored_notok = True
|
|
||||||
# 这里有一个漏洞,在产出物为兄弟件的情况下,不合格品的数量是记录在mlog上的,
|
|
||||||
# 而不是mlogb上,以上的额外处理就没有效果了, 不过光子不记录不合格品
|
|
||||||
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, mo_count_eweight, notok_sign_or_defect, mlog_or_b = mo
|
|
||||||
if mo_count < 0:
|
|
||||||
raise ParseError('存在负数!')
|
|
||||||
elif mo_count == 0:
|
|
||||||
continue
|
|
||||||
if is_fix:
|
|
||||||
wm_state = WMaterial.WM_REPAIRED
|
|
||||||
if mo_ma.id in can_matoutIds:
|
|
||||||
wm_state = WMaterial.WM_OK if notok_sign_or_defect is None or (
|
|
||||||
isinstance(notok_sign_or_defect, Defect) and notok_sign_or_defect.okcate in [Defect.DEFECT_OK, Defect.DEFECT_OK_B]
|
|
||||||
) else WMaterial.WM_NOTOK
|
|
||||||
else:
|
|
||||||
wm_state = WMaterial.WM_OK if notok_sign_or_defect is None or (
|
|
||||||
isinstance(notok_sign_or_defect, Defect) and notok_sign_or_defect.okcate in [Defect.DEFECT_OK, Defect.DEFECT_OK_B]
|
|
||||||
) 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
|
|
||||||
elif notok_sign_or_defect is not None:
|
|
||||||
lookup['notok_sign'] = notok_sign_or_defect
|
|
||||||
if into_wm_mgroup:
|
|
||||||
lookup['mgroup'] = mgroup
|
|
||||||
stored_mgroup = True
|
|
||||||
else:
|
|
||||||
lookup['belong_dept'] = belong_dept
|
|
||||||
|
|
||||||
wm, is_create2 = WMaterial.objects.get_or_create(**lookup, defaults={**lookup, "belong_dept": belong_dept})
|
|
||||||
wm.count = wm.count + mo_count
|
|
||||||
wm.count_eweight = mo_count_eweight
|
|
||||||
wm.update_by = user
|
|
||||||
if supplier is not None:
|
|
||||||
wm.supplier = supplier
|
|
||||||
if is_create2:
|
|
||||||
wm.create_by = user
|
|
||||||
if isinstance(mlog_or_b, Mlog) and mlog_or_b.wm_in:
|
|
||||||
wm.batch_ofrom = mlog_or_b.wm_in.batch_ofrom
|
|
||||||
wm.material_ofrom = mlog_or_b.wm_in.material_ofrom
|
|
||||||
elif isinstance(mlog_or_b, Mlogb):
|
|
||||||
wm.batch_ofrom = mlog_or_b.batch_ofrom
|
|
||||||
wm.material_ofrom = mlog_or_b.material_ofrom
|
|
||||||
if isinstance(mlog_or_b, Mlogb):
|
|
||||||
if mlog_or_b.number_from and wm.number_from is not None and wm.number_from != mlog_or_b.number_from:
|
|
||||||
raise ParseError(f'{wm.batch}-该批号现有库存来源于个号{wm.number_from}')
|
|
||||||
wm.number_from = mlog_or_b.number_from
|
|
||||||
wm.save()
|
|
||||||
if mo_ma.tracking == Material.MA_TRACKING_SINGLE:
|
|
||||||
if notok_sign_or_defect:
|
|
||||||
mlogbws = Mlogbw.objects.filter(mlogb=mlog_or_b, ftest__is_ok=False, ftest__defect_main=notok_sign_or_defect)
|
|
||||||
else:
|
else:
|
||||||
mlogbws = Mlogbw.objects.filter(Q(ftest=None) | Q(ftest__is_ok=True), mlogb=mlog_or_b)
|
for f in Mlogb._meta.fields:
|
||||||
mlogbws_count = mlogbws.count()
|
if 'count_n_' in f.name and getattr(item, f.name) > 0:
|
||||||
if mlogbws_count != mo_count:
|
notok_sign = f.name.replace('count_n_', '')
|
||||||
raise ParseError("日志与明细数量不一致,操作失败")
|
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 mlogbws:
|
stored_notok = True
|
||||||
if item.wpr:
|
# 这里有一个漏洞,在产出物为兄弟件的情况下,不合格品的数量是记录在mlog上的,
|
||||||
Wpr.change_or_new(wpr=item.wpr, wm=wm, ftest=item.ftest)
|
# 而不是mlogb上,以上的额外处理就没有效果了, 不过光子不记录不合格品
|
||||||
else:
|
else:
|
||||||
wpr_from = None
|
m_outs_list = [(material_out, mlog.batch, mlog.count_ok, mlog.count_real_eweight, None, mlog)]
|
||||||
if item.mlogbw_from:
|
# 一次填写的暂时不处理不合格品
|
||||||
wpr_from = item.mlogbw_from.wpr
|
for mo in m_outs_list:
|
||||||
wpr = Wpr.change_or_new(number=item.number,
|
mo_ma, mo_batch, mo_count, mo_count_eweight, notok_sign_or_defect, mlog_or_b = mo
|
||||||
wm=wm, ftest=item.ftest,
|
if mo_ma is None:
|
||||||
wpr_from=wpr_from)
|
continue
|
||||||
item.wpr = wpr
|
if mo_count < 0:
|
||||||
item.save()
|
raise ParseError('存在负数!')
|
||||||
|
elif mo_count == 0:
|
||||||
|
continue
|
||||||
|
if is_fix:
|
||||||
|
wm_state = WMaterial.WM_REPAIRED
|
||||||
|
if mo_ma.id in can_matoutIds:
|
||||||
|
wm_state = WMaterial.WM_OK if notok_sign_or_defect is None or (
|
||||||
|
isinstance(notok_sign_or_defect, Defect) and notok_sign_or_defect.okcate in [Defect.DEFECT_OK, Defect.DEFECT_OK_B]
|
||||||
|
) else WMaterial.WM_NOTOK
|
||||||
|
else:
|
||||||
|
wm_state = WMaterial.WM_OK if notok_sign_or_defect is None or (
|
||||||
|
isinstance(notok_sign_or_defect, Defect) and notok_sign_or_defect.okcate in [Defect.DEFECT_OK, Defect.DEFECT_OK_B]
|
||||||
|
) 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
|
||||||
|
elif notok_sign_or_defect is not None:
|
||||||
|
lookup['notok_sign'] = notok_sign_or_defect
|
||||||
|
if into_wm_mgroup:
|
||||||
|
lookup['mgroup'] = mgroup
|
||||||
|
stored_mgroup = True
|
||||||
|
else:
|
||||||
|
lookup['belong_dept'] = belong_dept
|
||||||
|
|
||||||
|
wm, is_create2 = WMaterial.objects.get_or_create(**lookup, defaults={**lookup, "belong_dept": belong_dept})
|
||||||
|
wm.count = wm.count + mo_count
|
||||||
|
wm.count_eweight = mo_count_eweight
|
||||||
|
wm.update_by = user
|
||||||
|
if supplier is not None:
|
||||||
|
wm.supplier = supplier
|
||||||
|
if is_create2:
|
||||||
|
wm.create_by = user
|
||||||
|
if isinstance(mlog_or_b, Mlog) and mlog_or_b.wm_in:
|
||||||
|
wm.batch_ofrom = mlog_or_b.wm_in.batch_ofrom
|
||||||
|
wm.material_ofrom = mlog_or_b.wm_in.material_ofrom
|
||||||
|
elif isinstance(mlog_or_b, Mlogb):
|
||||||
|
wm.batch_ofrom = mlog_or_b.batch_ofrom
|
||||||
|
wm.material_ofrom = mlog_or_b.material_ofrom
|
||||||
|
if isinstance(mlog_or_b, Mlogb):
|
||||||
|
if mlog_or_b.number_from and wm.number_from is not None and wm.number_from != mlog_or_b.number_from:
|
||||||
|
raise ParseError(f'{wm.batch}-该批号现有库存来源于个号{wm.number_from}')
|
||||||
|
wm.number_from = mlog_or_b.number_from
|
||||||
|
wm.save()
|
||||||
|
if mo_ma.tracking == Material.MA_TRACKING_SINGLE:
|
||||||
|
if notok_sign_or_defect:
|
||||||
|
mlogbws = Mlogbw.objects.filter(mlogb=mlog_or_b, ftest__is_ok=False, ftest__defect_main=notok_sign_or_defect)
|
||||||
|
else:
|
||||||
|
mlogbws = Mlogbw.objects.filter(Q(ftest=None) | Q(ftest__is_ok=True), mlogb=mlog_or_b)
|
||||||
|
mlogbws_count = mlogbws.count()
|
||||||
|
if mlogbws_count != mo_count:
|
||||||
|
raise ParseError("日志与明细数量不一致,操作失败")
|
||||||
|
for item in mlogbws:
|
||||||
|
if item.wpr:
|
||||||
|
Wpr.change_or_new(wpr=item.wpr, wm=wm, ftest=item.ftest)
|
||||||
|
else:
|
||||||
|
wpr_from = None
|
||||||
|
if item.mlogbw_from:
|
||||||
|
wpr_from = item.mlogbw_from.wpr
|
||||||
|
wpr = Wpr.change_or_new(number=item.number,
|
||||||
|
wm=wm, ftest=item.ftest,
|
||||||
|
wpr_from=wpr_from)
|
||||||
|
item.wpr = wpr
|
||||||
|
item.save()
|
||||||
|
|
||||||
mlog.submit_time = now
|
mlog.submit_time = now
|
||||||
mlog.submit_user = user
|
mlog.submit_user = user
|
||||||
|
@ -411,169 +414,171 @@ def mlog_revert(mlog: Mlog, user: User, now: Union[datetime.datetime, None]):
|
||||||
if is_fix:
|
if is_fix:
|
||||||
can_matoutIds = process.get_canout_mat_ids()
|
can_matoutIds = process.get_canout_mat_ids()
|
||||||
# 先回退产物
|
# 先回退产物
|
||||||
if material_out or is_fix: # 产物退回
|
# 有多个产物的情况
|
||||||
# 有多个产物的情况
|
# 需要考虑不合格品退回的情况
|
||||||
# 需要考虑不合格品退回的情况
|
mlogb_out_qs = Mlogb.objects.filter(mlog=mlog, material_out__isnull=False)
|
||||||
mlogb_out_qs = Mlogb.objects.filter(mlog=mlog, material_out__isnull=False)
|
if mlogb_out_qs.exists():
|
||||||
if mlogb_out_qs.exists():
|
mlogb_out_qs = mlogb_out_qs.filter(need_inout=True)
|
||||||
mlogb_out_qs = mlogb_out_qs.filter(need_inout=True)
|
m_outs_list = [
|
||||||
m_outs_list = [
|
(mo.material_out, mo.batch if mo.batch else mlog.batch, mo.count_ok_full if mo.count_ok_full is not None else mo.count_ok, mlog.count_real_eweight, None, mo)
|
||||||
(mo.material_out, mo.batch if mo.batch else mlog.batch, mo.count_ok_full if mo.count_ok_full is not None else mo.count_ok, mlog.count_real_eweight, None, mo)
|
for mo in mlogb_out_qs.all()]
|
||||||
for mo in mlogb_out_qs.all()]
|
if stored_notok:
|
||||||
if stored_notok:
|
for item in mlogb_out_qs:
|
||||||
for item in mlogb_out_qs:
|
mbd_qs = MlogbDefect.get_defect_qs_from_mlogb(item)
|
||||||
mbd_qs = MlogbDefect.get_defect_qs_from_mlogb(item)
|
if item.qct is not None or mbd_qs.exists():
|
||||||
if item.qct is not None or mbd_qs.exists():
|
|
||||||
# if item.material_out.tracking == Material.MA_TRACKING_SINGLE:
|
|
||||||
# Mlogbw.cal_count_notok(item)
|
|
||||||
for itemx in mbd_qs:
|
|
||||||
m_outs_list.append((item.material_out, item.batch, itemx.count, 0, itemx.defect, item))
|
|
||||||
# if item.material_out.tracking == Material.MA_TRACKING_SINGLE:
|
# if item.material_out.tracking == Material.MA_TRACKING_SINGLE:
|
||||||
# # 获取所有主要的不合格项
|
# Mlogbw.cal_count_notok(item)
|
||||||
# bw_qs = Mlogbw.objects.filter(mlogb=item)
|
for itemx in mbd_qs:
|
||||||
# defectIds= Ftest.objects.filter(mlogbw_ftest__in=bw_qs).exclude(defect_main=None).values_list("defect_main__id", flat=True).distinct()
|
m_outs_list.append((item.material_out, item.batch, itemx.count, 0, itemx.defect, item))
|
||||||
# defects_map = {d.id: d for d in Defect.objects.filter(id__in=defectIds)}
|
# if item.material_out.tracking == Material.MA_TRACKING_SINGLE:
|
||||||
# # 过滤并统计相关数据
|
# # 获取所有主要的不合格项
|
||||||
# filtered_bw_qs = bw_qs.filter(
|
# bw_qs = Mlogbw.objects.filter(mlogb=item)
|
||||||
# ftest__defect_main__id__in=defects_map.keys()
|
# defectIds= Ftest.objects.filter(mlogbw_ftest__in=bw_qs).exclude(defect_main=None).values_list("defect_main__id", flat=True).distinct()
|
||||||
# ).values('ftest__defect_main__id').annotate(xcount=Count('id'))
|
# defects_map = {d.id: d for d in Defect.objects.filter(id__in=defectIds)}
|
||||||
# # 整理结果
|
# # 过滤并统计相关数据
|
||||||
# for defect_data in filtered_bw_qs:
|
# filtered_bw_qs = bw_qs.filter(
|
||||||
# defect_id = defect_data['ftest__defect_main__id']
|
# ftest__defect_main__id__in=defects_map.keys()
|
||||||
# xcount = defect_data['xcount']
|
# ).values('ftest__defect_main__id').annotate(xcount=Count('id'))
|
||||||
# if xcount > 0:
|
# # 整理结果
|
||||||
# defect = defects_map[defect_id]
|
# for defect_data in filtered_bw_qs:
|
||||||
# m_outs_list.append((item.material_out, item.batch, xcount, 0, defect, item))
|
# defect_id = defect_data['ftest__defect_main__id']
|
||||||
else:
|
# xcount = defect_data['xcount']
|
||||||
for f in Mlogb._meta.fields:
|
# if xcount > 0:
|
||||||
if 'count_n_' in f.name and getattr(item, f.name) > 0:
|
# defect = defects_map[defect_id]
|
||||||
notok_sign = f.name.replace('count_n_', '')
|
# m_outs_list.append((item.material_out, item.batch, xcount, 0, defect, item))
|
||||||
m_outs_list.append((item.material_out, item.batch if item.batch else mlog.batch,
|
else:
|
||||||
getattr(item, f.name), mlog.count_real_eweight, notok_sign, item))
|
for f in Mlogb._meta.fields:
|
||||||
else:
|
if 'count_n_' in f.name and getattr(item, f.name) > 0:
|
||||||
m_outs_list = [(material_out, mlog.batch, mlog.count_ok, mlog.count_real_eweight, None, mlog)]
|
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:
|
for mo in m_outs_list:
|
||||||
mo_ma, mo_batch, mo_count, _, notok_sign_or_defect, mlog_or_b = mo
|
mo_ma, mo_batch, mo_count, _, notok_sign_or_defect, mlog_or_b = mo
|
||||||
if mo_count < 0:
|
if mo_ma is None:
|
||||||
raise ParseError('存在负数!')
|
continue
|
||||||
elif mo_count == 0:
|
if mo_count < 0:
|
||||||
continue
|
raise ParseError('存在负数!')
|
||||||
if is_fix:
|
elif mo_count == 0:
|
||||||
wm_state = WMaterial.WM_REPAIRED
|
continue
|
||||||
if mo_ma.id in can_matoutIds:
|
if is_fix:
|
||||||
wm_state = WMaterial.WM_OK if notok_sign_or_defect is None or (
|
wm_state = WMaterial.WM_REPAIRED
|
||||||
isinstance(notok_sign_or_defect, Defect) and notok_sign_or_defect.okcate in [Defect.DEFECT_OK, Defect.DEFECT_OK_B]
|
if mo_ma.id in can_matoutIds:
|
||||||
) else WMaterial.WM_NOTOK
|
|
||||||
else:
|
|
||||||
wm_state = WMaterial.WM_OK if notok_sign_or_defect is None or (
|
wm_state = WMaterial.WM_OK if notok_sign_or_defect is None or (
|
||||||
isinstance(notok_sign_or_defect, Defect) and notok_sign_or_defect.okcate in [Defect.DEFECT_OK, Defect.DEFECT_OK_B]
|
isinstance(notok_sign_or_defect, Defect) and notok_sign_or_defect.okcate in [Defect.DEFECT_OK, Defect.DEFECT_OK_B]
|
||||||
) else WMaterial.WM_NOTOK
|
) else WMaterial.WM_NOTOK
|
||||||
lookup = {'batch': mo_batch, 'material': mo_ma, 'mgroup': None, 'notok_sign': None, 'defect': None, 'state': wm_state}
|
else:
|
||||||
if isinstance(notok_sign_or_defect, Defect):
|
wm_state = WMaterial.WM_OK if notok_sign_or_defect is None or (
|
||||||
lookup['defect'] = notok_sign_or_defect
|
isinstance(notok_sign_or_defect, Defect) and notok_sign_or_defect.okcate in [Defect.DEFECT_OK, Defect.DEFECT_OK_B]
|
||||||
else:
|
) else WMaterial.WM_NOTOK
|
||||||
lookup['notok_sign'] = notok_sign_or_defect
|
lookup = {'batch': mo_batch, 'material': mo_ma, 'mgroup': None, 'notok_sign': None, 'defect': None, 'state': wm_state}
|
||||||
if stored_mgroup:
|
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:
|
||||||
|
lookup['belong_dept'] = belong_dept
|
||||||
|
wm_qs = WMaterial.objects.filter(**lookup)
|
||||||
|
count_x = wm_qs.count()
|
||||||
|
if count_x == 1:
|
||||||
|
wm = wm_qs.first()
|
||||||
|
elif count_x == 0:
|
||||||
|
raise ParseError(
|
||||||
|
f'{str(mo_ma)}-{mo_batch}-批次库存不存在!')
|
||||||
|
else:
|
||||||
|
raise ParseError(
|
||||||
|
f'{str(mo_ma)}-{mo_batch}-存在多个相同批次!')
|
||||||
|
wm.count = wm.count - mo_count
|
||||||
|
if wm.count < 0:
|
||||||
|
raise ParseError(f'{wm.batch} 车间库存不足, 产物无法回退')
|
||||||
|
elif wm.count >= 0:
|
||||||
|
if isinstance(mlog_or_b, Mlogb):
|
||||||
|
if mlog_or_b.number_from == wm.number_from:
|
||||||
|
wm.number_from = None
|
||||||
|
wm.update_by = user
|
||||||
|
wm.save()
|
||||||
|
if mo_ma.tracking == Material.MA_TRACKING_SINGLE:
|
||||||
|
if notok_sign_or_defect:
|
||||||
|
mlogbws = Mlogbw.objects.filter(mlogb=mlog_or_b, ftest__is_ok=False, ftest__defect_main=notok_sign_or_defect)
|
||||||
|
else:
|
||||||
|
mlogbws = Mlogbw.objects.filter(Q(ftest=None) | Q(ftest__is_ok=True), mlogb=mlog_or_b)
|
||||||
|
if mlogbws.count() != mo_count:
|
||||||
|
raise ParseError("日志与明细数量不一致,操作失败")
|
||||||
|
for item in mlogbws:
|
||||||
|
Wpr.change_or_new(wpr=item.wpr, old_wm=wm)
|
||||||
|
|
||||||
|
# 再生成消耗
|
||||||
|
m_ins_list = []
|
||||||
|
m_ins_bl_list = []
|
||||||
|
into_wm_mgroup = process.into_wm_mgroup
|
||||||
|
m_ins = Mlogb.objects.filter(mlog=mlog, material_in__isnull=False)
|
||||||
|
if m_ins.exists():
|
||||||
|
m_ins = m_ins.filter(need_inout=True)
|
||||||
|
for mi in m_ins.all():
|
||||||
|
m_ins_list.append((mi.material_in, mi.batch, mi.count_use, None, mi))
|
||||||
|
for item in m_ins:
|
||||||
|
mbd_qs = MlogbDefect.get_defect_qs_from_mlogb(item)
|
||||||
|
for itemx in mbd_qs:
|
||||||
|
if itemx.defect:
|
||||||
|
m_ins_bl_list.append((item.material_in, item.batch, itemx.count, itemx.defect, item))
|
||||||
|
else:
|
||||||
|
m_ins_list = [(material_in, mlog.batch, mlog.count_use, mlog.wm_in, mlog)]
|
||||||
|
for mi in m_ins_list:
|
||||||
|
mi_ma, mi_batch, mi_count, defect_or, mlog_or_b = mi
|
||||||
|
if mi_ma is None:
|
||||||
|
continue
|
||||||
|
if mi_count <= 0:
|
||||||
|
raise ParseError('存在非正数!')
|
||||||
|
if isinstance(mlog_or_b, Mlogb) and mlog_or_b.wm_in:
|
||||||
|
wm = WMaterial.objects.get(id=mlog_or_b.wm_in.id)
|
||||||
|
else:
|
||||||
|
# 针对光子的情况,实际上必须需要wm_in
|
||||||
|
lookup = {'batch': mi_batch, 'material': mi_ma, 'mgroup': None, 'state': WMaterial.WM_OK}
|
||||||
|
if into_wm_mgroup:
|
||||||
|
# 退回到本工段
|
||||||
lookup['mgroup'] = mgroup
|
lookup['mgroup'] = mgroup
|
||||||
else:
|
else:
|
||||||
lookup['belong_dept'] = belong_dept
|
lookup['belong_dept'] = belong_dept
|
||||||
wm_qs = WMaterial.objects.filter(**lookup)
|
|
||||||
count_x = wm_qs.count()
|
|
||||||
if count_x == 1:
|
|
||||||
wm = wm_qs.first()
|
|
||||||
elif count_x == 0:
|
|
||||||
raise ParseError(
|
|
||||||
f'{str(mo_ma)}-{mo_batch}-批次库存不存在!')
|
|
||||||
else:
|
|
||||||
raise ParseError(
|
|
||||||
f'{str(mo_ma)}-{mo_batch}-存在多个相同批次!')
|
|
||||||
wm.count = wm.count - mo_count
|
|
||||||
if wm.count < 0:
|
|
||||||
raise ParseError(f'{wm.batch} 车间库存不足, 产物无法回退')
|
|
||||||
elif wm.count >= 0:
|
|
||||||
if isinstance(mlog_or_b, Mlogb):
|
|
||||||
if mlog_or_b.number_from == wm.number_from:
|
|
||||||
wm.number_from = None
|
|
||||||
wm.update_by = user
|
|
||||||
wm.save()
|
|
||||||
if mo_ma.tracking == Material.MA_TRACKING_SINGLE:
|
|
||||||
if notok_sign_or_defect:
|
|
||||||
mlogbws = Mlogbw.objects.filter(mlogb=mlog_or_b, ftest__is_ok=False, ftest__defect_main=notok_sign_or_defect)
|
|
||||||
else:
|
|
||||||
mlogbws = Mlogbw.objects.filter(Q(ftest=None) | Q(ftest__is_ok=True), mlogb=mlog_or_b)
|
|
||||||
if mlogbws.count() != mo_count:
|
|
||||||
raise ParseError("日志与明细数量不一致,操作失败")
|
|
||||||
for item in mlogbws:
|
|
||||||
Wpr.change_or_new(wpr=item.wpr, old_wm=wm)
|
|
||||||
|
|
||||||
# 再生成消耗
|
wm, _ = WMaterial.objects.get_or_create(**lookup, defaults={**lookup, "belong_dept": belong_dept})
|
||||||
if material_in or is_fix: # 领用数退回
|
wm.count = wm.count + mi_count
|
||||||
m_ins_list = []
|
wm.update_by = user
|
||||||
m_ins_bl_list = []
|
wm.save()
|
||||||
into_wm_mgroup = process.into_wm_mgroup
|
if mi_ma.tracking == Material.MA_TRACKING_SINGLE:
|
||||||
m_ins = Mlogb.objects.filter(mlog=mlog, material_in__isnull=False)
|
mlogbws = Mlogbw.objects.filter(mlogb=mlog_or_b)
|
||||||
if m_ins.exists():
|
if mlogbws.count() != mi_count:
|
||||||
m_ins = m_ins.filter(need_inout=True)
|
raise ParseError("日志与明细数量不一致,操作失败")
|
||||||
for mi in m_ins.all():
|
for item in mlogbws:
|
||||||
m_ins_list.append((mi.material_in, mi.batch, mi.count_use, None, mi))
|
# if item.wpr:
|
||||||
for item in m_ins:
|
Wpr.change_or_new(wpr=item.wpr, wm=wm)
|
||||||
mbd_qs = MlogbDefect.get_defect_qs_from_mlogb(item)
|
# else:
|
||||||
for itemx in mbd_qs:
|
# wpr = Wpr.change_or_new(number=item.number, wm=wm)
|
||||||
if itemx.defect:
|
# item.wpr = wpr
|
||||||
m_ins_bl_list.append((item.material_in, item.batch, itemx.count, itemx.defect, item))
|
# item.save()
|
||||||
else:
|
|
||||||
m_ins_list = [(material_in, mlog.batch, mlog.count_use, mlog.wm_in, mlog)]
|
# 针对加工前不良的暂时额外处理
|
||||||
for mi in m_ins_list:
|
if stored_notok:
|
||||||
mi_ma, mi_batch, mi_count, defect_or, mlog_or_b = mi
|
for item in m_ins_bl_list:
|
||||||
if mi_count <= 0:
|
material, batch, count, defect, mi_ = item
|
||||||
|
if count <= 0:
|
||||||
raise ParseError('存在非正数!')
|
raise ParseError('存在非正数!')
|
||||||
if isinstance(mlog_or_b, Mlogb) and mlog_or_b.wm_in:
|
lookup = {'batch': batch, 'material': material, 'mgroup': mgroup, 'defect': defect, 'state': WMaterial.WM_NOTOK}
|
||||||
wm = WMaterial.objects.get(id=mlog_or_b.wm_in.id)
|
wm, is_create = WMaterial.objects.get_or_create(**lookup, defaults={**lookup, "belong_dept": belong_dept})
|
||||||
|
wm.count = wm.count - count
|
||||||
|
if wm.count < 0:
|
||||||
|
raise ParseError('加工前不良数量大于库存量')
|
||||||
|
if is_create:
|
||||||
|
wm.create_by = user
|
||||||
else:
|
else:
|
||||||
# 针对光子的情况,实际上必须需要wm_in
|
wm.update_by = user
|
||||||
lookup = {'batch': mi_batch, 'material': mi_ma, 'mgroup': None, 'state': WMaterial.WM_OK}
|
|
||||||
if into_wm_mgroup:
|
|
||||||
# 退回到本工段
|
|
||||||
lookup['mgroup'] = mgroup
|
|
||||||
else:
|
|
||||||
lookup['belong_dept'] = belong_dept
|
|
||||||
|
|
||||||
wm, _ = WMaterial.objects.get_or_create(**lookup, defaults={**lookup, "belong_dept": belong_dept})
|
|
||||||
wm.count = wm.count + mi_count
|
|
||||||
wm.update_by = user
|
|
||||||
wm.save()
|
wm.save()
|
||||||
if mi_ma.tracking == Material.MA_TRACKING_SINGLE:
|
if material.tracking == Material.MA_TRACKING_SINGLE:
|
||||||
mlogbws = Mlogbw.objects.filter(mlogb=mlog_or_b)
|
raise ParseError("加工前不良的物料暂不支持单件回退")
|
||||||
if mlogbws.count() != mi_count:
|
|
||||||
raise ParseError("日志与明细数量不一致,操作失败")
|
|
||||||
for item in mlogbws:
|
|
||||||
# if item.wpr:
|
|
||||||
Wpr.change_or_new(wpr=item.wpr, wm=wm)
|
|
||||||
# else:
|
|
||||||
# wpr = Wpr.change_or_new(number=item.number, wm=wm)
|
|
||||||
# item.wpr = wpr
|
|
||||||
# item.save()
|
|
||||||
|
|
||||||
# 针对加工前不良的暂时额外处理
|
|
||||||
if stored_notok:
|
|
||||||
for item in m_ins_bl_list:
|
|
||||||
material, batch, count, defect, mi_ = item
|
|
||||||
if count <= 0:
|
|
||||||
raise ParseError('存在非正数!')
|
|
||||||
lookup = {'batch': batch, 'material': material, 'mgroup': mgroup, 'defect': defect, 'state': WMaterial.WM_NOTOK}
|
|
||||||
wm, is_create = WMaterial.objects.get_or_create(**lookup, defaults={**lookup, "belong_dept": belong_dept})
|
|
||||||
wm.count = wm.count - count
|
|
||||||
if wm.count < 0:
|
|
||||||
raise ParseError('加工前不良数量大于库存量')
|
|
||||||
if is_create:
|
|
||||||
wm.create_by = user
|
|
||||||
else:
|
|
||||||
wm.update_by = user
|
|
||||||
wm.save()
|
|
||||||
if material.tracking == Material.MA_TRACKING_SINGLE:
|
|
||||||
raise ParseError("加工前不良的物料暂不支持单件回退")
|
|
||||||
|
|
||||||
mlog.submit_time = None
|
mlog.submit_time = None
|
||||||
mlog.submit_user = None
|
mlog.submit_user = None
|
||||||
|
|
Loading…
Reference in New Issue