86 lines
3.5 KiB
Python
86 lines
3.5 KiB
Python
from rest_framework.views import APIView
|
|
from apps.mtm.models import InputMaterial
|
|
from apps.system.mixins import CreateUpdateModelAMixin
|
|
from apps.pm.serializers import ProductionPlanCreateFromOrderSerializer, ProductionPlanSerializer, ResourceCalListSerializer, ResourceCalSerializer
|
|
from rest_framework.mixins import CreateModelMixin, ListModelMixin
|
|
from apps.pm.models import ProductionPlan
|
|
from rest_framework.viewsets import GenericViewSet, ModelViewSet
|
|
from django.shortcuts import render
|
|
from apps.sam.models import Order
|
|
from rest_framework.exceptions import APIException
|
|
from rest_framework.response import Response
|
|
from rest_framework.decorators import action
|
|
# Create your views here.
|
|
|
|
def updateOrderPlanedCount(order):
|
|
"""
|
|
更新订单已排数量
|
|
"""
|
|
planed_count = 0
|
|
plans = ProductionPlan.objects.filter(order=order)
|
|
for i in plans:
|
|
planed_count = planed_count + i.count
|
|
order.planed_count = planed_count
|
|
order.save()
|
|
|
|
class ProductionPlanViewSet(CreateUpdateModelAMixin, ListModelMixin, CreateModelMixin, GenericViewSet):
|
|
"""
|
|
生产计划
|
|
"""
|
|
perms_map = {'*': '*'}
|
|
queryset = ProductionPlan.objects.select_related('order', 'order__contract', 'product')
|
|
serializer_class = ProductionPlanSerializer
|
|
search_fields = ['number']
|
|
filterset_fields = []
|
|
ordering_fields = ['id']
|
|
ordering = ['-id']
|
|
|
|
def get_serializer_class(self):
|
|
if self.action in ['create']:
|
|
return ProductionPlanCreateFromOrderSerializer
|
|
return ProductionPlanSerializer
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
data = request.data
|
|
serializer = self.get_serializer(data=data)
|
|
serializer.is_valid(raise_exception=True)
|
|
if data.get('order', None):
|
|
order = Order.objects.get(pk=data['order'])
|
|
if order.planed_count >= data['count'] or data['count'] > 0:
|
|
pass
|
|
else:
|
|
raise APIException('排产数量错误')
|
|
instance = serializer.save(create_by=request.user, product=order.product)
|
|
updateOrderPlanedCount(instance.order)
|
|
return Response()
|
|
|
|
class ResourceViewSet(GenericViewSet):
|
|
|
|
perms_map = {'*': '*'}
|
|
|
|
@action(methods=['post'], detail=False, perms_map={'get':'*'}, serializer_class=ResourceCalListSerializer)
|
|
def cal(self, request, pk=None):
|
|
"""
|
|
物料消耗计算
|
|
"""
|
|
rdata = request.data
|
|
serializer = self.get_serializer(data=rdata)
|
|
serializer.is_valid(raise_exception=True)
|
|
res_d_list = []
|
|
res = []
|
|
for i in rdata:
|
|
materials = InputMaterial.objects.filter(subproduction__product__id=i['id'],
|
|
subproduction__is_deleted=False, is_deleted=False, material__type__in=[3,4]).order_by('material__number')\
|
|
.values('material__id', 'material__name', 'material__number', 'material__type', 'count', 'material__count')
|
|
l_m = list(materials)
|
|
for m in l_m:
|
|
if m['material__id'] in res_d_list:
|
|
index = res_d_list.index(m['material__id'])
|
|
res[index]['count'] = res[index]['count'] + m['count']*i['count']
|
|
else:
|
|
res_d_list.append(m['material__id'])
|
|
res.append({'id':m['material__id'], 'name':m['material__name'],
|
|
'type':m['material__type'], 'number':m['material__number'],
|
|
'count':m['count']*i['count'], 'inv_count':m['material__count']})
|
|
return Response(res)
|