feat: 增加半成品检验撤回功能
This commit is contained in:
parent
509fa02939
commit
6f8ab25b5c
|
@ -5,6 +5,7 @@ from django.db.models import F
|
||||||
from apps.wpm.services import do_out, do_in
|
from apps.wpm.services import do_out, do_in
|
||||||
from apps.mtm.models import Material, Process
|
from apps.mtm.models import Material, Process
|
||||||
from apps.utils.tools import ranstr
|
from apps.utils.tools import ranstr
|
||||||
|
from apps.system.models import Dept
|
||||||
|
|
||||||
|
|
||||||
class InmService:
|
class InmService:
|
||||||
|
@ -61,63 +62,57 @@ class InmService:
|
||||||
in = 1
|
in = 1
|
||||||
out = -1
|
out = -1
|
||||||
"""
|
"""
|
||||||
belong_dept = instance.belong_dept
|
|
||||||
mioitems = MIOItem.objects.filter(mio=instance)
|
mioitems = MIOItem.objects.filter(mio=instance)
|
||||||
if not mioitems.exists():
|
if not mioitems.exists():
|
||||||
raise ParseError("未填写物料明细")
|
raise ParseError("未填写物料明细")
|
||||||
|
type = instance.type
|
||||||
|
prodction_dept = instance.belong_dept
|
||||||
for i in MIOItem.objects.filter(mio=instance):
|
for i in MIOItem.objects.filter(mio=instance):
|
||||||
material = i.material
|
cls.update_mb_item(i, in_or_out, type, prodction_dept)
|
||||||
warehouse = i.warehouse
|
|
||||||
mb, is_created = MaterialBatch.objects.get_or_create(
|
|
||||||
material=material, warehouse=warehouse, batch=i.batch, defaults={"material": material, "warehouse": warehouse, "count": 0, "batch": i.batch}
|
|
||||||
)
|
|
||||||
if in_or_out == 1:
|
|
||||||
mb.count = mb.count + i.count
|
|
||||||
# if mb.expiration_date is None:
|
|
||||||
# mb.expiration_date = i.expiration_date
|
|
||||||
if instance.type == MIO.MIO_TYPE_DO_IN: # 生产入库需要记录production_dept字段
|
|
||||||
if mb.production_dept is None or mb.production_dept == belong_dept:
|
|
||||||
mb.production_dept = belong_dept
|
|
||||||
else:
|
|
||||||
raise ParseError("同种物料不同生产车间应该有不同批次号!")
|
|
||||||
mb.save()
|
|
||||||
mias = MIOItemA.objects.filter(mioitem=i)
|
|
||||||
if mias.exists(): # 组合件入库
|
|
||||||
if not is_created:
|
|
||||||
raise ParseError("该批次组合件已存在")
|
|
||||||
for mia in mias:
|
|
||||||
MaterialBatchA.objects.create(mb=mb, material=mia.material, batch=mia.batch)
|
|
||||||
elif in_or_out == -1:
|
|
||||||
mb.count = mb.count - i.count
|
|
||||||
if mb.count < 0:
|
|
||||||
raise ParseError("批次库存不足,操作失败")
|
|
||||||
elif mb.count == 0:
|
|
||||||
mb.delete()
|
|
||||||
else:
|
|
||||||
mb.save()
|
|
||||||
else:
|
|
||||||
raise ParseError("不支持的操作")
|
|
||||||
cls.cal_mat_count(material)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update_mb_after_test(cls, ins: MIOItem):
|
def update_mb_item(cls, i: MIOItem, in_or_out: int = 1, field:str='count', type: str =None, production_dept: Dept=None):
|
||||||
count_notok = ins.count_notok
|
"""
|
||||||
batch = ins.batch
|
更新物料批次(根据明细)
|
||||||
material = ins.material
|
in = 1
|
||||||
warehouse = ins.warehouse
|
out = -1
|
||||||
try:
|
默认使用count字段做加减
|
||||||
mb = MaterialBatch.objects.get(material=material, batch=batch, warehouse=warehouse)
|
"""
|
||||||
count_new = mb.count - count_notok
|
if type is None or production_dept is None:
|
||||||
if count_new < 0:
|
mio = i.mio
|
||||||
raise ParseError("库存扣减失败,请确认!")
|
type = mio.type
|
||||||
mb.count = count_new
|
production_dept = mio.belong_dept
|
||||||
|
material = i.material
|
||||||
|
warehouse = i.warehouse
|
||||||
|
mb, is_created = MaterialBatch.objects.get_or_create(
|
||||||
|
material=material, warehouse=warehouse, batch=i.batch, defaults={"material": material, "warehouse": warehouse, "count": 0, "batch": i.batch}
|
||||||
|
)
|
||||||
|
if in_or_out == 1:
|
||||||
|
mb.count = mb.count + getattr(i, field)
|
||||||
|
if type == MIO.MIO_TYPE_DO_IN: # 生产入库需要记录production_dept字段
|
||||||
|
if mb.production_dept is None or mb.production_dept == production_dept:
|
||||||
|
mb.production_dept = production_dept
|
||||||
|
else:
|
||||||
|
raise ParseError("同种物料不同生产车间应该有不同批次号!")
|
||||||
mb.save()
|
mb.save()
|
||||||
except MaterialBatch.DoesNotExist:
|
mias = MIOItemA.objects.filter(mioitem=i)
|
||||||
# 库存不存在已被消耗了
|
if mias.exists(): # 组合件入库
|
||||||
if count_notok != 0:
|
if not is_created:
|
||||||
raise ParseError("库存已全消耗!")
|
raise ParseError("该批次组合件已存在")
|
||||||
|
for mia in mias:
|
||||||
|
MaterialBatchA.objects.create(mb=mb, material=mia.material, batch=mia.batch)
|
||||||
|
elif in_or_out == -1:
|
||||||
|
mb.count = mb.count - getattr(i, field)
|
||||||
|
if mb.count < 0:
|
||||||
|
raise ParseError("批次库存不足,操作失败")
|
||||||
|
elif mb.count == 0:
|
||||||
|
mb.delete()
|
||||||
|
else:
|
||||||
|
mb.save()
|
||||||
|
else:
|
||||||
|
raise ParseError("不支持的操作")
|
||||||
cls.cal_mat_count(material)
|
cls.cal_mat_count(material)
|
||||||
|
|
||||||
|
|
||||||
def daoru_mb(path: str):
|
def daoru_mb(path: str):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -253,7 +253,22 @@ class MIOItemViewSet(ListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin
|
||||||
sr.is_valid(raise_exception=True)
|
sr.is_valid(raise_exception=True)
|
||||||
sr.save()
|
sr.save()
|
||||||
# 开始变动库存
|
# 开始变动库存
|
||||||
InmService.update_mb_after_test(ins)
|
InmService.update_mb_item(ins, -1, 'count_notok', mio.type, mio.belong_dept)
|
||||||
|
return Response()
|
||||||
|
|
||||||
|
@action(methods=['post'], detail=True, perms_map={'post': 'mioitem.test'}, serializer_class=serializers.Serializer)
|
||||||
|
@transaction.atomic
|
||||||
|
def test_revert(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
检验撤回
|
||||||
|
"""
|
||||||
|
ins: MIOItem = self.get_object()
|
||||||
|
mio: MIO = ins.mio
|
||||||
|
if ins.test_date is None:
|
||||||
|
raise ParseError('该明细还未检验')
|
||||||
|
InmService.update_mb_item(ins, 1, 'count_notok', mio.type, mio.belong_dept)
|
||||||
|
ins.test_date = None
|
||||||
|
ins.save()
|
||||||
return Response()
|
return Response()
|
||||||
|
|
||||||
@action(methods=['post'], detail=True, perms_map={'post': 'mioitem.test'}, serializer_class=MIOItemPurInTestSerializer)
|
@action(methods=['post'], detail=True, perms_map={'post': 'mioitem.test'}, serializer_class=MIOItemPurInTestSerializer)
|
||||||
|
|
Loading…
Reference in New Issue