from apps.cm.models import LableMat from rest_framework.decorators import action from apps.cm.serializers import TidSerializer, LabelMatSerializer from apps.inm.models import MaterialBatch, MIOItem from apps.wpm.models import WMaterial from rest_framework.exceptions import ParseError, NotFound from rest_framework.response import Response from apps.utils.viewsets import CustomGenericViewSet, RetrieveModelMixin, CustomListModelMixin # Create your views here. class LableMatViewSet(CustomListModelMixin, RetrieveModelMixin, CustomGenericViewSet): perms_map = {"post": "*", "get": "*"} serializer_class = LabelMatSerializer queryset = LableMat.objects.all() select_related_fields = ["material", "material_origin", "supplier"] @action(methods=['post'], detail=False, serializer_class=TidSerializer) def get_from_mb(self, request, pk=None): """ 从仓库明细获取物料标签 从仓库明细获取物料标签 """ tid = request.data.get('tid') try: mb = MaterialBatch.objects.get(id=tid) except MaterialBatch.DoesNotExist: raise NotFound('仓库明细不存在') obj, _ = LableMat.objects.get_or_create( state=10, material=mb.material, batch=mb.batch, defaults={ "supplier": mb.supplier } ) rdata = LabelMatSerializer(obj).data rdata["code_label"] = f"mat:{obj.id}" return Response(rdata) @action(methods=['post'], detail=False, serializer_class=TidSerializer) def get_from_wm(self, request, pk=None): """ 从车间库存明细获取物料标签 从车间库存明细获取物料标签 """ tid = request.data.get('tid') try: wm = WMaterial.objects.get(id=tid) except WMaterial.DoesNotExist: raise NotFound('车间库存不存在') obj, _ = LableMat.objects.get_or_create( state=wm.state, material=wm.material, batch=wm.batch, notok_sign=wm.notok_sign, material_origin=wm.material_origin) rdata = LabelMatSerializer(obj).data rdata["code_label"] = f"mat:{obj.id}" return Response(rdata) @action(methods=['post'], detail=False, serializer_class=TidSerializer) def get_from_mioitem(self, request, pk=None): """ 从出入库明细获取物料标签 从出入库明细获取物料标签 """ tid = request.data.get('tid') try: mioitem: MIOItem = MIOItem.objects.get(id=tid) except MIOItem.DoesNotExist: raise NotFound('出入库明细不存在') obj, _ = LableMat.objects.get_or_create( state=10, material=mioitem.material, batch=mioitem.batch, defaults={ "supplier": mioitem.mio.supplier }) rdata = LabelMatSerializer(obj).data rdata["code_label"] = f"mat:{obj.id}" return rdata