75 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Python
		
	
	
	
| from rest_framework.exceptions import ValidationError
 | |
| from apps.inm.models import FIFOItemProduct, IProduct, Inventory, MaterialBatch, FIFO, FIFOItem
 | |
| 
 | |
| class InmService:
 | |
|     @classmethod
 | |
|     def update_inm(cls, instance:FIFO, type:int=1):
 | |
|         """
 | |
|         更新库存(正反)
 | |
|         """
 | |
|         if instance.type in [FIFO.FIFO_TYPE_PUR_IN, FIFO.FIFO_TYPE_DO_IN]: # 采购入库, 生产入库
 | |
|             # 更新相关表
 | |
|             for i in FIFOItem.objects.filter(fifo=instance):
 | |
|                 material = i.material
 | |
|                 warehouse = i.warehouse
 | |
|                 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()
 | |
|         
 | |
|                 # 创建IProduct
 | |
|                 ips2 = []
 | |
|                 for m in FIFOItemProduct.objects.filter(fifoitem=i):
 | |
|                     ip = {}
 | |
|                     ip['warehouse'] = warehouse
 | |
|                     ip['batch'] = i.batch
 | |
|                     wp = m.wproduct
 | |
|                     ip['wproduct'] = wp
 | |
|                     ip['number'] = m.number
 | |
|                     ip['material'] = m.material
 | |
|                     ips2.append(IProduct(**ip))
 | |
|                 IProduct.objects.bulk_create(ips2)
 | |
| 
 | |
|                 # 如果是采购入库更新采购订单表
 | |
|                 if instance.type == FIFO.FIFO_TYPE_PUR_IN:
 | |
|                     pu_order_item = i.pu_order_item
 | |
|                     delivered_count = pu_order_item.delivered_count + i.count
 | |
|                     if delivered_count > pu_order_item.count:
 | |
|                         raise ValidationError('超出采购订单所需量')
 | |
|                     pu_order_item.delivered_count = delivered_count
 | |
|                     pu_order_item.save()
 | |
| 
 | |
|         elif instance.type in [FIFO.FIFO_TYPE_DO_OUT, FIFO.FIFO_TYPE_SALE_OUT]: # 生产领料 销售出库
 | |
|             # 更新相关表
 | |
|             for i in FIFOItem.objects.filter(fifo=instance):
 | |
|                 material = i.material
 | |
|                 warehouse = i.warehouse
 | |
|                 o1 = Inventory.objects.get(material=material, warehouse=warehouse)
 | |
|                 temp_count = o1.count - i.count
 | |
|                 if temp_count < 0:
 | |
|                     raise ValidationError('库存不足,操作失败')
 | |
|                 o1.count = temp_count
 | |
|                 o1.save()
 | |
|                 o2 = MaterialBatch.objects.get(material=material, warehouse=warehouse, batch=i.batch)
 | |
|                 temp_count = o2.count - i.count
 | |
|                 if temp_count < 0:
 | |
|                     raise ValidationError('库存不足,操作失败')
 | |
|                 o2.count = temp_count
 | |
|                 o2.save()
 | |
|                 temp_count = material.count - i.count
 | |
|                 if temp_count < 0:
 | |
|                     raise ValidationError('库存不足,操作失败')
 | |
|                 material.count = temp_count
 | |
|                 material.save()
 | |
| 
 | |
|                 # 删除IProduct
 | |
|                 if instance.type == FIFO.FIFO_TYPE_DO_OUT:
 | |
|                     # 生产领料的情况直接从IProduct中删除
 | |
|                     numbers = FIFOItemProduct.objects.filter(fifoitem=i).values_list('number', flat=True)
 | |
|                     IProduct.objects.filter(number__in=numbers).delete() |