from apps.mtm.models import Material from apps.pm.models import ProductionPlan from apps.sam.models import Order from apps.wpm.models import WProduct, WproductFlow from django.db.models import F from apps.wpm.serializers import WProductDetailSerializer class SrmServices: """ 数据统计分析 """ @classmethod def get_product_count(cls, datetime_start=None, datetime_end=None, tag=1, dept=None): """ 根据生产情况统计相关数量 """ if tag == 1: objs = WproductFlow.objects.filter(is_lastlog=True, material__type=Material.MA_TYPE_GOOD) else: objs = WproductFlow.objects.filter(is_lastlog=True) if datetime_start: objs = objs.filter(create_time__gte=datetime_start) if datetime_end: objs = objs.filter(create_time__lte=datetime_end) if dept: objs = objs.filter(subproduction_plan__workshop=dept) 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_notok = ( objs.filter(act_state__in=[WProduct.WPR_ACT_STATE_NOTOK, WProduct.WPR_ACT_STATE_SCRAP]).exclude(step__process__id=1) | objs.filter(act_state__in=[WProduct.WPR_ACT_STATE_NOTOK, WProduct.WPR_ACT_STATE_SCRAP], step__process__id=1).exclude(number=None) ).count() count_selled = objs.filter(act_state=WProduct.WPR_ACT_STATE_SELLED).count() count_mtestok = objs.filter(is_mtestok=True).count() count_mtestnotok = objs.filter(is_mtestok=False).count() count_doing = objs.filter(act_state__in=[ WProduct.WPR_ACT_STATE_TOTEST, WProduct.WPR_ACT_STATE_TOCOMBTEST, WProduct.WPR_ACT_STATE_TOFINALTEST, WProduct.WPR_ACT_STATE_TORETEST, WProduct.WPR_ACT_STATE_DOWAIT, WProduct.WPR_ACT_STATE_DOING ], subproduction_plan__product=F('material')).count() return dict(count=count,count_ok=count_ok, count_notok=count_notok, count_selled=count_selled, count_mtestok=count_mtestok, count_mtestnotok=count_mtestnotok, count_doing=count_doing) @classmethod def get_plan_count(cls, datetime_start=None, datetime_end=None): """ 任务数量 """ objs = ProductionPlan.objects.all() if datetime_start: objs = objs.filter(end_date__gte=datetime_start) if datetime_end: objs = objs.filter(end_date__lte=datetime_end) count = objs.count() count_use = objs.exclude(state__in=[ProductionPlan.PLAN_STATE_PAUSE, ProductionPlan.PLAN_STATE_STOP]).count() count_completed = objs.filter(state__in=[ProductionPlan.PLAN_STATE_DONE, ProductionPlan.PLAN_MTEST_DONE]).count() return dict(count=count, count_use=count_use, count_completed=count_completed) @classmethod def get_order_count(cls, datetime_start=None, datetime_end=None): """ 订单数量 """ objs = Order.objects.all() if datetime_start: objs = objs.filter(delivery_date__gte=datetime_start) if datetime_end: objs = objs.filter(delivery_date__lte=datetime_end) count = objs.count() count_delivered = objs.filter(delivered_count__gte=F('count')).count() return dict(count=count, count_delivered=count_delivered)