更新计划按工序字段统计

This commit is contained in:
caoqianming 2021-12-27 14:56:55 +08:00
parent c8c9cd4554
commit 15812bf048
3 changed files with 23 additions and 3 deletions

View File

@ -29,7 +29,7 @@ class ProductionPlan(CommonAModel):
count_ok = models.PositiveIntegerField('合格数', default=0)
start_date = models.DateField('计划开工日期')
end_date = models.DateField('计划完工日期')
process_json = models.JSONField('按工序的统计数', default=list, null=True, blank=True)
process_json = models.JSONField('按工序的统计数', default=dict, null=True, blank=True)
is_planed = models.BooleanField('是否已排产', default=False)
class Meta:
verbose_name = '生产计划'

View File

@ -1,10 +1,27 @@
from django.db.models.aggregates import Sum
from apps.pm.models import ProductionPlan, SubProductionPlan
import math
class PmService:
@classmethod
def update_plan_process_json(cls, plan:ProductionPlan):
"""
更新计划统计字段
更新计划按工序统计字段
"""
ret = {}
subplans = SubProductionPlan.objects.filter()
subplans = SubProductionPlan.objects.filter(production_plan=plan, is_deleted=False)
qs = subplans.values('process', 'process__name', 'process_number').annotate(count=Sum('main_count'),
count_real=Sum('main_count_real'), count_ok=Sum('main_count_ok'))
qs_list = list(qs)
for i in qs_list:
ret[i['process_number']] = {
'count':i['count'],
'count_real':i['count_real'],
'count_ok':i['count_ok'],
'rate': round((i['count_ok']/i['count_real'])*100,2) if i['count_real'] > 0 else 0
}
plan.process_json = ret
plan.save()

View File

@ -2,6 +2,7 @@ from django.db.models.signals import post_save
from django.dispatch import receiver
from apps.mtm.models import Material
from apps.pm.models import SubProductionPlan, SubProductionProgress
from apps.pm.services import PmService
@receiver(post_save, sender=SubProductionProgress)
def update_subplan_main(sender, instance, created, **kwargs):
@ -26,6 +27,8 @@ def update_subplan_main(sender, instance, created, **kwargs):
plan.count_real = subplan.main_count_real
plan.count_ok = subplan.main_count_ok
plan.save()
# 更新子计划工序统计字段
PmService.update_plan_process_json(subplan.production_plan)