42 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			42 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Python
		
	
	
	
| from apps.inm.models import MIO, MIOItem, MaterialBatch
 | |
| from rest_framework.exceptions import ValidationError
 | |
| 
 | |
| 
 | |
| class InmService:
 | |
|     
 | |
|     @classmethod
 | |
|     def update_inm(cls, instance: MIO):
 | |
|         """
 | |
|         更新库存, 暂不支持反向操作
 | |
|         """
 | |
|         if instance.type in [MIO.MIO_TYPE_PUR_IN, MIO.MIO_TYPE_DO_IN, MIO.MIO_TYPE_OTHER_IN]: # 采购入库, 生产入库, 其他入库
 | |
|             cls.update_mb(instance)
 | |
|             if instance.type == MIO.MIO_TYPE_PUR_IN:  # 需要更新订单
 | |
|                 from apps.pum.services import PumService
 | |
|                 PumService.mio_purin(instance)
 | |
|         elif instance.type in [MIO.MIO_TYPE_DO_OUT, MIO.MIO_TYPE_SALE_OUT, MIO.MIO_TYPE_OTHER_OUT]: # 生产领料 销售出库
 | |
|             cls.update_mb(instance, -1)
 | |
|             if instance.type == MIO.MIO_TYPE_SALE_OUT:
 | |
|                 from apps.sam.services import SamService
 | |
|                 SamService.mio_saleout(instance)
 | |
| 
 | |
|     @classmethod
 | |
|     def update_mb(cls, instance: MIO, in_or_out: int =1):
 | |
|         """
 | |
|         更新物料批次
 | |
|         """
 | |
|         for i in MIOItem.objects.filter(mio=instance):
 | |
|             material = i.material
 | |
|             warehouse = i.warehouse
 | |
|             mb, _ = 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
 | |
|                 mb.save()
 | |
|             elif in_or_out == -1:
 | |
|                 mb.count = mb.count - i.count
 | |
|                 if mb.count < 0:
 | |
|                     raise ValidationError('批次库存不足,操作失败')
 | |
|                 mb.save() |