from django.db.models.signals import post_save from django.dispatch import receiver from apps.inm.models import Inventory, MaterialBatch, FIFO, FIFOItem def update_inm(instance:FIFO, type:int=1): """ 更新库存(正反) """ warehouse = instance.warehouse if instance.type in [FIFO.FIFO_TYPE_PUR_IN]: # 采购入库 # 更新相关表 for i in FIFOItem.objects.filter(fifo=instance): material = i.material o1, _ = Inventory.objects.get_or_create(material=material, warehouse=warehouse, \ defaults={'material':material, 'warehouse':warehouse, 'count':0}) o1.count = o1.count + i.count o1.save() o2, _ = MaterialBatch.objects.get_or_create(material=material, warehouse=warehouse, batch=i.batch,\ defaults={'material':material, 'warehouse':warehouse, 'count':0, 'batch':i.batch}) o2.count = o2.count + i.count o2.save() material.count = material.count + i.count material.save() elif instance.type in [FIFO.FIFO_TYPE_DO_OUT]: # 生产领料 # 更新相关表 for i in FIFOItem.objects.filter(fifo=instance): material = i.material o1 = Inventory.objects.get(material=material, warehouse=warehouse) o1.count = o1.count - i.count o1.save() o2 = MaterialBatch.objects.get(material=material, warehouse=warehouse, batch=i.batch) o2.count = o2.count - i.count o2.save() material.count = material.count - i.count material.save()