feat: 出入库操作支持提交后可撤销功能
This commit is contained in:
parent
33e661b1dc
commit
eef99ed624
|
@ -10,29 +10,43 @@ from apps.utils.tools import ranstr
|
||||||
class InmService:
|
class InmService:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update_inm(cls, instance: MIO):
|
def update_inm(cls, instance: MIO, is_reverse: bool = False):
|
||||||
"""
|
"""
|
||||||
更新库存, 暂不支持反向操作
|
更新库存, 支持反向操作
|
||||||
"""
|
"""
|
||||||
if instance.type in [MIO.MIO_TYPE_PUR_IN, MIO.MIO_TYPE_DO_IN, MIO.MIO_TYPE_OTHER_IN]: # 采购入库, 生产入库, 其他入库
|
if instance.type in [MIO.MIO_TYPE_PUR_IN, MIO.MIO_TYPE_DO_IN, MIO.MIO_TYPE_OTHER_IN]: # 采购入库, 生产入库, 其他入库
|
||||||
cls.update_mb(instance)
|
in_or_out = 1
|
||||||
|
if is_reverse:
|
||||||
|
in_or_out = -1
|
||||||
|
cls.update_mb(instance, in_or_out)
|
||||||
if instance.type == MIO.MIO_TYPE_PUR_IN: # 需要更新订单
|
if instance.type == MIO.MIO_TYPE_PUR_IN: # 需要更新订单
|
||||||
from apps.pum.services import PumService
|
from apps.pum.services import PumService
|
||||||
PumService.mio_purin(instance)
|
PumService.mio_purin(instance, is_reverse)
|
||||||
elif instance.type == MIO.MIO_TYPE_DO_IN:
|
elif instance.type == MIO.MIO_TYPE_DO_IN:
|
||||||
|
if is_reverse:
|
||||||
|
do_out(instance)
|
||||||
|
else:
|
||||||
do_in(instance)
|
do_in(instance)
|
||||||
elif instance.type in [MIO.MIO_TYPE_DO_OUT, MIO.MIO_TYPE_SALE_OUT, MIO.MIO_TYPE_OTHER_OUT]: # 生产领料 销售出库
|
elif instance.type in [MIO.MIO_TYPE_DO_OUT, MIO.MIO_TYPE_SALE_OUT, MIO.MIO_TYPE_OTHER_OUT]: # 生产领料 销售出库
|
||||||
cls.update_mb(instance, -1)
|
in_or_out = -1
|
||||||
|
if is_reverse:
|
||||||
|
in_or_out = 1
|
||||||
|
cls.update_mb(instance, 1)
|
||||||
if instance.type == MIO.MIO_TYPE_SALE_OUT:
|
if instance.type == MIO.MIO_TYPE_SALE_OUT:
|
||||||
from apps.sam.services import SamService
|
from apps.sam.services import SamService
|
||||||
SamService.mio_saleout(instance)
|
SamService.mio_saleout(instance, is_reverse)
|
||||||
elif instance.type == MIO.MIO_TYPE_DO_OUT:
|
elif instance.type == MIO.MIO_TYPE_DO_OUT:
|
||||||
|
if is_reverse:
|
||||||
|
do_in(instance)
|
||||||
|
else:
|
||||||
do_out(instance)
|
do_out(instance)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update_mb(cls, instance: MIO, in_or_out: int = 1):
|
def update_mb(cls, instance: MIO, in_or_out: int = 1):
|
||||||
"""
|
"""
|
||||||
更新物料批次
|
更新物料批次
|
||||||
|
in = 1
|
||||||
|
out = -1
|
||||||
"""
|
"""
|
||||||
mioitems = MIOItem.objects.filter(mio=instance)
|
mioitems = MIOItem.objects.filter(mio=instance)
|
||||||
if not mioitems.exists():
|
if not mioitems.exists():
|
||||||
|
|
|
@ -186,6 +186,25 @@ class MIOViewSet(CustomModelViewSet):
|
||||||
InmService.update_inm(ins)
|
InmService.update_inm(ins)
|
||||||
return Response(MIOListSerializer(instance=ins).data)
|
return Response(MIOListSerializer(instance=ins).data)
|
||||||
|
|
||||||
|
@action(methods=['post'], detail=True, perms_map={'post': 'mio.submit'}, serializer_class=serializers.Serializer)
|
||||||
|
@transaction.atomic
|
||||||
|
def revert(self, request, *args, **kwargs):
|
||||||
|
"""撤回
|
||||||
|
|
||||||
|
撤回
|
||||||
|
"""
|
||||||
|
ins = self.get_object()
|
||||||
|
user = self.request.user
|
||||||
|
if ins.state != MIO.MIO_SUBMITED:
|
||||||
|
raise ParseError('记录状态异常')
|
||||||
|
if ins.submit_user != user:
|
||||||
|
raise ParseError('非提交人不可撤回')
|
||||||
|
ins.submit_time = None
|
||||||
|
ins.state = MIO.MIO_CREATE
|
||||||
|
ins.save()
|
||||||
|
InmService.update_inm(ins, is_reverse=True)
|
||||||
|
return Response()
|
||||||
|
|
||||||
|
|
||||||
class MIOItemViewSet(ListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin, CustomGenericViewSet):
|
class MIOItemViewSet(ListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin, CustomGenericViewSet):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -19,17 +19,24 @@ class PumService:
|
||||||
puplan.state = state
|
puplan.state = state
|
||||||
puplan.save()
|
puplan.save()
|
||||||
|
|
||||||
def mio_purin(mio: MIO):
|
def mio_purin(mio: MIO, is_reverse: bool = False):
|
||||||
"""
|
"""
|
||||||
采购入库成功后的操作
|
采购入库成功后的操作
|
||||||
"""
|
"""
|
||||||
pu_order = mio.pu_order
|
pu_order = mio.pu_order
|
||||||
|
if pu_order is None:
|
||||||
|
return
|
||||||
for i in MIOItem.objects.filter(mio=mio):
|
for i in MIOItem.objects.filter(mio=mio):
|
||||||
pu_orderitem = PuOrderItem.objects.get(
|
pu_orderitem = PuOrderItem.objects.get(
|
||||||
material=i.material, pu_order=pu_order)
|
material=i.material, pu_order=pu_order)
|
||||||
|
if is_reverse:
|
||||||
|
delivered_count = pu_orderitem.delivered_count - i.count
|
||||||
|
else:
|
||||||
delivered_count = pu_orderitem.delivered_count + i.count
|
delivered_count = pu_orderitem.delivered_count + i.count
|
||||||
if delivered_count > pu_orderitem.count:
|
if delivered_count > pu_orderitem.count:
|
||||||
raise ValidationError(f'{i.material.name}-超出采购订单所需数量')
|
raise ValidationError(f'{i.material.name}-超出采购订单所需数量')
|
||||||
|
elif delivered_count < 0:
|
||||||
|
raise ValidationError(f'{i.material.name}-数量小于0')
|
||||||
pu_orderitem.delivered_count = delivered_count
|
pu_orderitem.delivered_count = delivered_count
|
||||||
pu_orderitem.save()
|
pu_orderitem.save()
|
||||||
pu_order_state = PuOrder.PUORDER_SHIP
|
pu_order_state = PuOrder.PUORDER_SHIP
|
||||||
|
|
|
@ -6,23 +6,32 @@ from apps.inm.models import MIO, MIOItem
|
||||||
|
|
||||||
class SamService:
|
class SamService:
|
||||||
|
|
||||||
def mio_saleout(mio: MIO):
|
def mio_saleout(mio: MIO, is_reverse: bool = False):
|
||||||
"""
|
"""
|
||||||
销售出库成功后的操作
|
销售出库成功后的操作
|
||||||
"""
|
"""
|
||||||
order = mio.order
|
order = mio.order
|
||||||
|
if order is None:
|
||||||
|
return
|
||||||
for i in MIOItem.objects.filter(mio=mio):
|
for i in MIOItem.objects.filter(mio=mio):
|
||||||
orderitem = OrderItem.objects.get(order=order, material=i.material)
|
orderitem = OrderItem.objects.get(order=order, material=i.material)
|
||||||
|
if is_reverse:
|
||||||
|
delivered_count = orderitem.delivered_count - i.count
|
||||||
|
else:
|
||||||
delivered_count = orderitem.delivered_count + i.count
|
delivered_count = orderitem.delivered_count + i.count
|
||||||
if delivered_count > orderitem.count:
|
if delivered_count > orderitem.count:
|
||||||
raise ValidationError((f'{i.material.name}-超出订单所需数量'))
|
raise ValidationError((f'{i.material.name}-超出订单所需数量'))
|
||||||
|
elif delivered_count < 0:
|
||||||
|
raise ValidationError((f'{i.material.name}-数量小于0'))
|
||||||
orderitem.delivered_count = delivered_count
|
orderitem.delivered_count = delivered_count
|
||||||
orderitem.save()
|
orderitem.save()
|
||||||
# 更新order的状态
|
# 更新order的状态
|
||||||
qs = OrderItem.objects.filter(
|
qs = OrderItem.objects.filter(
|
||||||
order=order, count__lte=F('delivered_count'))
|
order=order, count__lte=F('delivered_count'))
|
||||||
|
order_state = Order.ORDER_DOING
|
||||||
if qs.exists():
|
if qs.exists():
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
order.state = Order.ORDER_DELIVERED
|
order_state = Order.ORDER_DELIVERED
|
||||||
|
order.state = order_state
|
||||||
order.save()
|
order.save()
|
||||||
|
|
|
@ -84,11 +84,23 @@ def do_out(mio: MIO):
|
||||||
do_user = mio.do_user
|
do_user = mio.do_user
|
||||||
mioitems = MIOItem.objects.filter(mio=mio)
|
mioitems = MIOItem.objects.filter(mio=mio)
|
||||||
for item in mioitems:
|
for item in mioitems:
|
||||||
wm, new_create = WMaterial.objects.get_or_create(batch=item.batch, material=item.material,
|
action_list = []
|
||||||
|
mias = MIOItemA.objects.filter(mioitem=item)
|
||||||
|
if mias.exists(): # 组合件入库
|
||||||
|
mias_list = list(mias.values_list('material', 'batch', 'rate'))
|
||||||
|
for i in range(len(mias_list)):
|
||||||
|
material, batch, rate = mias_list[i]
|
||||||
|
new_count = rate * item.count # 假设 item.count 存在
|
||||||
|
action_list.append([material, batch, new_count])
|
||||||
|
else:
|
||||||
|
action_list = [[item.material, item.batch, item.count]]
|
||||||
|
for al in action_list:
|
||||||
|
xmaterial, xbatch, xcount = al
|
||||||
|
wm, new_create = WMaterial.objects.get_or_create(batch=xbatch, material=xmaterial,
|
||||||
belong_dept=belong_dept, defaults={
|
belong_dept=belong_dept, defaults={
|
||||||
"batch": item.batch,
|
"batch": xbatch,
|
||||||
"material": item.material,
|
"material": xmaterial,
|
||||||
"count": item.count,
|
"count": xcount,
|
||||||
"create_by": do_user,
|
"create_by": do_user,
|
||||||
"belong_dept": belong_dept
|
"belong_dept": belong_dept
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue