114 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			114 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Python
		
	
	
	
| from django.shortcuts import render
 | |
| from rest_framework.mixins import ListModelMixin, DestroyModelMixin
 | |
| from rest_framework.exceptions import ParseError, PermissionDenied
 | |
| from rest_framework.decorators import action
 | |
| from django.db import transaction
 | |
| from rest_framework import serializers
 | |
| from django.utils import timezone
 | |
| from rest_framework.response import Response
 | |
| 
 | |
| from apps.inm.models import WareHouse, MaterialBatch, MIO, MIOItem
 | |
| from apps.inm.serializers import (
 | |
|     MaterialBatchSerializer, WareHourseSerializer, MIOSerializer, MIOItemSerializer, MIODoSerializer)
 | |
| from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet
 | |
| from apps.inm.services import InmService
 | |
| from apps.utils.mixins import BulkCreateModelMixin, BulkDestroyModelMixin
 | |
| 
 | |
| 
 | |
| # Create your views here.
 | |
| class WarehouseVIewSet(CustomModelViewSet):
 | |
|     """
 | |
|     list: 仓库信息
 | |
| 
 | |
|     仓库信息
 | |
|     """
 | |
|     queryset = WareHouse.objects.all()
 | |
|     serializer_class = WareHourseSerializer
 | |
|     search_fields = ['name', 'number', 'place']
 | |
|     ordering = ['create_time']
 | |
| 
 | |
|     def perform_destroy(self, instance):
 | |
|         if MaterialBatch.objects.filter(warehouse=instance).exclude(count=0).exists():
 | |
|             raise ParseError('该仓库存在物料')
 | |
|         instance.delete()
 | |
| 
 | |
| 
 | |
| class MaterialBatchViewSet(ListModelMixin, CustomGenericViewSet):
 | |
|     """
 | |
|     list: 物料批次
 | |
| 
 | |
|     物料批次
 | |
|     """
 | |
|     perms_map = {'get': '*'}
 | |
|     queryset = MaterialBatch.objects.all()
 | |
|     serializer_class = MaterialBatchSerializer
 | |
|     select_related_fields = ['warehouse', 'material']
 | |
|     filterset_fields = ['warehouse', 'material']
 | |
| 
 | |
| 
 | |
| class MIOViewSet(ListModelMixin, DestroyModelMixin, CustomGenericViewSet):
 | |
|     """
 | |
|     list: 出入库记录
 | |
| 
 | |
|     出入库记录
 | |
|     """
 | |
|     perms_map = {'get': '*', 'delete': 'mio.delete'}
 | |
|     queryset = MIO.objects.all()
 | |
|     select_related_fields = ['create_by']
 | |
|     serializer_class = MIOSerializer
 | |
| 
 | |
|     def perform_destroy(self, instance):
 | |
|         if instance.state != MIO.MIO_CREATE:
 | |
|             raise ParseError('非创建中不可删除')
 | |
|         return super().perform_destroy(instance)
 | |
| 
 | |
|     @action(methods=['post'], detail=False, perms_map={'post': 'mio.do'}, serializer_class=MIODoSerializer)
 | |
|     @transaction.atomic
 | |
|     def do(self, request, *args, **kwargs):
 | |
|         """创建生产领料/生产入库
 | |
| 
 | |
|         创建生产领料/生产入库
 | |
|         """
 | |
|         sr = MIODoSerializer(data=request.data)
 | |
|         sr.is_valid(raise_exception=True)
 | |
|         sr.save()
 | |
|         return Response()
 | |
| 
 | |
|     @action(methods=['post'], detail=True, perms_map={'post': 'mio.update'}, serializer_class=serializers.Serializer)
 | |
|     @transaction.atomic
 | |
|     def submit(self, request, *args, **kwargs):
 | |
|         """提交
 | |
| 
 | |
|         提交
 | |
|         """
 | |
|         ins = self.get_object()
 | |
|         user = request.user
 | |
|         if ins.state != MIO.MIO_TYPE_DO_OUT and ins.create_by != user:
 | |
|             raise PermissionDenied('非创建人不可提交')
 | |
|         if ins.state != MIO.MIO_CREATE:
 | |
|             raise ParseError('记录状态异常')
 | |
|         ins.submit_time = timezone.now()
 | |
|         ins.state = MIO.MIO_SUBMITED
 | |
|         ins.submit_user = request.user
 | |
|         ins.save()
 | |
|         InmService.update_inm(ins)
 | |
|         return Response()
 | |
| 
 | |
| 
 | |
| class MIOItemViewSet(ListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin, CustomGenericViewSet):
 | |
|     """
 | |
|     list: 出入库明细
 | |
| 
 | |
|     出入库明细
 | |
|     """
 | |
|     queryset = MIOItem.objects.all()
 | |
|     serializer_class = MIOItemSerializer
 | |
|     select_related_fields = ['warehouse', 'mio', 'material']
 | |
|     filterset_fields = ['warehouse', 'mio', 'material']
 | |
|     ordering = ['create_time']
 | |
| 
 | |
|     def perform_destroy(self, instance):
 | |
|         if instance.state != MIO.MIO_CREATE:
 | |
|             raise ParseError('出入库记录非创建中不可删除')
 | |
|         return super().perform_destroy(instance)
 |