from apps.qm.models import TestRecord from apps.system.models import CommonAModel, Organization, User from django.db import models from django.contrib.auth.models import AbstractUser from django.db.models.base import Model import django.utils.timezone as timezone from django.db.models.query import QuerySet from utils.model import SoftModel, BaseModel from apps.mtm.models import Material, Process, RecordForm, RecordFormField, SubProduction, SubprodctionMaterial from apps.sam.models import Order class ProductionPlan(CommonAModel): """ 生产计划 """ PLAN_STATE_PLANING = 10 PLAN_STATE_ASSGINED = 20 PLAN_STATE_ACCEPTED = 30 PLAN_STATE_WORKING = 40 PLAN_STATE_DONE = 50 PLAN_MTEST_DONE = 60 PLAN_STATE_PAUSE = 70 PLAN_STATE_STOP = 80 state_choices=( (PLAN_STATE_PLANING, '制定中'), (PLAN_STATE_ASSGINED, '已下达'), (PLAN_STATE_ACCEPTED, '已接收'), (PLAN_STATE_WORKING, '生产中'), (PLAN_STATE_DONE, '生产完成'), (PLAN_MTEST_DONE, '军检完成'), (PLAN_STATE_PAUSE, '暂停'), (PLAN_STATE_STOP, '终止') ) number = models.CharField('编号', max_length=50, unique=True) order = models.ForeignKey(Order, verbose_name='关联订单', null=True, blank=True, on_delete=models.SET_NULL, related_name='plan_order') state = models.PositiveIntegerField('状态', choices=state_choices, default=PLAN_STATE_PLANING) product = models.ForeignKey(Material, verbose_name='生产产品', on_delete=models.CASCADE) count = models.PositiveIntegerField('生产数量', default=1) count_real = models.PositiveIntegerField('实际产出数', default=0) count_ok = models.PositiveIntegerField('合格数', default=0) count_notok = models.PositiveIntegerField('不合格数量', default=0) count_mtestok = models.PositiveIntegerField('军检合格数量', default=0) start_date = models.DateField('计划开工日期') end_date = models.DateField('计划完工日期') process_json = models.JSONField('按工序的统计数', default=dict, null=True, blank=True) is_planed = models.BooleanField('是否已排产', default=False) old_state = models.PositiveIntegerField('原状态', choices=state_choices, null=True, blank=True) class Meta: verbose_name = '生产计划' verbose_name_plural = verbose_name def __str__(self): return self.number class SubProductionPlan(CommonAModel): """ 子生产计划 """ SUBPLAN_STATE_PLANING = 10 SUBPLAN_STATE_ASSGINED = 20 SUBPLAN_STATE_ACCEPTED = 30 SUBPLAN_STATE_WORKING = 40 SUBPLAN_STATE_DONE = 50 state_choices=( (SUBPLAN_STATE_PLANING, '制定中'), (SUBPLAN_STATE_ASSGINED, '已下达'), (SUBPLAN_STATE_ACCEPTED, '已接收'), (SUBPLAN_STATE_WORKING, '生产中'), (SUBPLAN_STATE_DONE, '已完成'), ) number = models.CharField('子计划编号', max_length=50, unique=True, null=True, blank=True) production_plan = models.ForeignKey(ProductionPlan, verbose_name='关联主生产计划', on_delete=models.CASCADE, related_name='subplan_plan') subproduction = models.ForeignKey(SubProduction, verbose_name='关联生产分解', on_delete=models.CASCADE, related_name='subplan_subprod') start_date = models.DateField('计划开工日期') end_date = models.DateField('计划完工日期') workshop = models.ForeignKey(Organization, verbose_name='生产车间', on_delete=models.CASCADE) process = models.ForeignKey(Process, verbose_name='关联大工序', on_delete=models.CASCADE) product = models.ForeignKey(Material, verbose_name='主要产品', on_delete=models.CASCADE, null=True, blank=True) count = models.PositiveIntegerField('应产出数', default=0) count_real = models.PositiveIntegerField('实际产出数', default=0) count_ok = models.PositiveIntegerField('合格数', default=0) count_notok = models.PositiveIntegerField('不合格数量', default=0) steps = models.JSONField('工艺步骤', default=list) state = models.IntegerField('状态', default=SUBPLAN_STATE_PLANING) start_date_real = models.DateField('实际开工日期', null=True, blank=True) end_date_real = models.DateField('实际完工日期', null=True, blank=True) is_picked = models.BooleanField('是否已领料', default=False) first_test = models.ForeignKey('qm.testrecord', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='首件检验') leader_1 = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="工序负责人", null=True, blank=True, related_name='first_leader_1') leader_2 = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="技术负责人", null=True, blank=True, related_name='first_leader_2') leader_3 = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="总检", null=True, blank=True, related_name='first_leader_3') first_sign_time = models.DateTimeField('首件签字时间', null=True, blank=True) remark = models.CharField('备注', max_length=100, null=True, blank=True) class Meta: verbose_name = '子生产计划' verbose_name_plural = verbose_name # @property # def first_test(self): # return self.test_subplan.filter(type=TestRecord.TEST_FIRST, is_deleted=False).first() class SubProductionProgress(BaseModel): """ 子计划生产进度统计表/物料消耗 """ subproduction_plan = models.ForeignKey(SubProductionPlan, verbose_name='关联子生产计划', on_delete=models.CASCADE, related_name='progress_subplan') material = models.ForeignKey(Material, verbose_name='关联物料', on_delete=models.CASCADE) is_main = models.BooleanField('是否主产出', default=False) type = models.IntegerField('物料应用类型', default=SubprodctionMaterial.type_choices) count = models.PositiveIntegerField('应出入数') count_pick = models.PositiveIntegerField('实际领用数', default=0) count_real = models.PositiveIntegerField('实际消耗/产出数', default=0) count_ok = models.PositiveIntegerField('合格数量', default=0) count_notok = models.PositiveIntegerField('不合格数量', default=0)