98 lines
3.3 KiB
Python
98 lines
3.3 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)
|
|
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=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.create_by != user:
|
|
raise PermissionDenied('非创建人不可提交')
|
|
if ins.state != MIO.MIO_CREATE:
|
|
raise ParseError('订单非创建中')
|
|
ins.submit_time = timezone.now()
|
|
ins.state = MIO.MIO_SUBMITED
|
|
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) |