39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
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 [3]: # 采购入库
|
|
# 更新相关表
|
|
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 [1]: # 生产领料
|
|
# 更新相关表
|
|
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()
|
|
|