From 4cadeac2d4d19e20c65daaab8a09806424d29c17 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Tue, 15 Mar 2022 10:45:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E4=BA=A7=E5=93=81=E7=94=9F?= =?UTF-8?q?=E4=BA=A7=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hb_server/apps/inm/services.py | 6 +++++- hb_server/apps/srm/serializers.py | 3 +++ hb_server/apps/srm/services.py | 25 +++++++++++++++++++++++++ hb_server/apps/srm/urls.py | 3 ++- hb_server/apps/srm/views.py | 16 +++++++++++++++- hb_server/apps/wpm/services.py | 17 +++++++++++++++++ 6 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 hb_server/apps/srm/services.py diff --git a/hb_server/apps/inm/services.py b/hb_server/apps/inm/services.py index 159f148..0d44820 100644 --- a/hb_server/apps/inm/services.py +++ b/hb_server/apps/inm/services.py @@ -6,6 +6,8 @@ from apps.sam.models import SalePack, SaleProduct from django.db.models import Count from django.db.models.aggregates import Sum import logging + +from apps.wpm.services import WpmService logger = logging.getLogger('log') class InmService: @@ -133,8 +135,10 @@ class InmService: # 更新动态产品表情况 from apps.wpm.models import WProduct - WProduct.objects.filter(id__in=ips.values_list('wproduct', flat=True)).update( + wps = WProduct.objects.filter(id__in=ips.values_list('wproduct', flat=True)) + wps.update( act_state=WProduct.WPR_ACT_STATE_SELLED) + WpmService.add_wproducts_flow_log(instances=wps, change_str='selled') # 变更销售记录实际发货数 sale.count_real = ips.count() diff --git a/hb_server/apps/srm/serializers.py b/hb_server/apps/srm/serializers.py index 2a4e11f..283d360 100644 --- a/hb_server/apps/srm/serializers.py +++ b/hb_server/apps/srm/serializers.py @@ -25,6 +25,9 @@ class ProcessYieldSerializer(serializers.Serializer): datetime_start = serializers.DateField(label='开始时间', required=False, allow_null=True) datetime_end = serializers.DateField(label='结束时间', required=False, allow_null=True) +class ProductCountSerializer(serializers.Serializer): + datetime_start = serializers.DateField(label='开始时间', required=False, allow_null=True) + datetime_end = serializers.DateField(label='结束时间', required=False, allow_null=True) class AtWorkCountSerializer(serializers.Serializer): year = serializers.IntegerField(label='年') diff --git a/hb_server/apps/srm/services.py b/hb_server/apps/srm/services.py new file mode 100644 index 0000000..de57324 --- /dev/null +++ b/hb_server/apps/srm/services.py @@ -0,0 +1,25 @@ +from apps.mtm.models import Material +from apps.wpm.models import WProduct, WproductFlow + + +class SrmServices: + """ + 数据统计分析 + """ + @classmethod + def get_wp_product_count(cls, datetime_start, datetime_end): + """ + 根据生产情况统计相关数量 + """ + objs = WproductFlow.objects.filter(is_lastlog=True, material__type=Material.MA_TYPE_GOOD) + if datetime_start: + objs = objs.filter(create_time__gte=datetime_start) + if datetime_end: + objs = WproductFlow.objects.filter(create_time__lte=datetime_end) + count = objs.count() + count_ok = objs.filter(act_state__in=[WProduct.WPR_ACT_STATE_INM, + WProduct.WPR_ACT_STATE_OK, WProduct.WPR_ACT_STATE_SELLED]).count() + count_notok = objs.filter(act_state__in=[WProduct.WPR_ACT_STATE_NOTOK, WProduct.WPR_ACT_STATE_SCRAP]).count() + count_selled = objs.filter(act_state=WProduct.WPR_ACT_STATE_SELLED).count() + count_mtestok = objs.filter(is_mtestok=True).count() + return dict(count=count,count_ok=count_ok, count_notok=count_notok, count_selled=count_selled, count_mtestok=count_mtestok) \ No newline at end of file diff --git a/hb_server/apps/srm/urls.py b/hb_server/apps/srm/urls.py index 2b53153..2437fbe 100644 --- a/hb_server/apps/srm/urls.py +++ b/hb_server/apps/srm/urls.py @@ -3,11 +3,12 @@ from rest_framework import urlpatterns from django.urls import path, include from rest_framework.routers import DefaultRouter -from apps.srm.views import AtWorkCountView, GanttPlan, ProcessYieldView +from apps.srm.views import AtWorkCountView, GanttPlan, ProcessYieldView, ProductCountView router = DefaultRouter() urlpatterns = [ path('gantt/plan/', GanttPlan.as_view()), + path('product/count/', ProductCountView.as_view()), path('process/yield/', ProcessYieldView.as_view()), path('at_work/', AtWorkCountView.as_view()), path('', include(router.urls)), diff --git a/hb_server/apps/srm/views.py b/hb_server/apps/srm/views.py index ead2e2e..28d0da3 100644 --- a/hb_server/apps/srm/views.py +++ b/hb_server/apps/srm/views.py @@ -9,7 +9,8 @@ from rest_framework.response import Response from apps.hrm.models import ClockRecord from apps.mtm.models import Process, Step from apps.pm.models import ProductionPlan, SubProductionPlan -from apps.srm.serializers import AtWorkCountSerializer, PlanGanttSerializer, ProcessYieldSerializer +from apps.srm.serializers import AtWorkCountSerializer, PlanGanttSerializer, ProcessYieldSerializer, ProductCountSerializer +from apps.srm.services import SrmServices from apps.wpm.models import WProduct, WproductFlow from django.db.models import Count, F # Create your views here. @@ -23,6 +24,19 @@ class GanttPlan(ListAPIView): queryset = ProductionPlan.objects.filter(is_deleted=False, is_planed=True).prefetch_related('subplan_plan', 'subplan_plan__process') ordering = ['-id'] +class ProductCountView(CreateAPIView): + """ + 产品数量统计 + """ + perms_map = {'post':'*'} + serializer_class = ProductCountSerializer + def create(self, request, *args, **kwargs): + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + vdata = serializer.validated_data + res = SrmServices.get_wp_product_count(datetime_start= vdata.get('datetime_start', None), datetime_end= vdata.get('datetime_end', None)) + return Response(res) + class ProcessYieldView(CreateAPIView): """ 工序成品率统计 diff --git a/hb_server/apps/wpm/services.py b/hb_server/apps/wpm/services.py index 36db321..74af318 100644 --- a/hb_server/apps/wpm/services.py +++ b/hb_server/apps/wpm/services.py @@ -167,6 +167,23 @@ class WpmService(object): ins.change_str = change_str ins.save() + @classmethod + def add_wproducts_flow_log(cls, instances, change_str=''): + """ + 批量创建产品变动日志 + """ + WproductFlow.objects.filter(wproduct__in=instances).update(is_lastlog=False) + wfw = [] + for i in instances: + ins = WproductFlow() + ins.wproduct = i + for f in WproductFlow.__meta.fields: + if f.name not in ['id', 'wproduct', 'is_lastlog']: + setattr(ins, f.name, getattr(i, f.name, None)) + ins.change_str = change_str + wfw.append(ins) + WproductFlow.objects.bulk_create(wfw) + @classmethod def update_cutting_list_with_operation(cls, op:Operation): """