From 2e5771dc6f0ec4c25318a658083e5b4c17e04966 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Thu, 30 Dec 2021 09:32:47 +0800 Subject: [PATCH] add_wproduct_flow_log --- hb_server/apps/pm/services.py | 1 + .../0041_wproductflow_change_str.py | 18 +++ hb_server/apps/wpm/models.py | 2 + hb_server/apps/wpm/services.py | 15 ++ hb_server/apps/wpm/signals.py | 19 +-- hb_server/apps/wpm/views.py | 151 ++++++++++-------- 6 files changed, 127 insertions(+), 79 deletions(-) create mode 100644 hb_server/apps/wpm/migrations/0041_wproductflow_change_str.py diff --git a/hb_server/apps/pm/services.py b/hb_server/apps/pm/services.py index b5eef7e..9e9c2b7 100644 --- a/hb_server/apps/pm/services.py +++ b/hb_server/apps/pm/services.py @@ -15,6 +15,7 @@ class PmService: qs_list = list(qs) for i in qs_list: ret[i['process__number']] = { + 'process':i['process'], 'count':i['count'], 'count_real':i['count_real'], 'count_ok':i['count_ok'], diff --git a/hb_server/apps/wpm/migrations/0041_wproductflow_change_str.py b/hb_server/apps/wpm/migrations/0041_wproductflow_change_str.py new file mode 100644 index 0000000..06b4e04 --- /dev/null +++ b/hb_server/apps/wpm/migrations/0041_wproductflow_change_str.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.9 on 2021-12-30 01:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('wpm', '0040_alter_wproductflow_number'), + ] + + operations = [ + migrations.AddField( + model_name='wproductflow', + name='change_str', + field=models.CharField(default='', max_length=1000, verbose_name='变动描述'), + ), + ] diff --git a/hb_server/apps/wpm/models.py b/hb_server/apps/wpm/models.py index 8638f9c..92caa8a 100644 --- a/hb_server/apps/wpm/models.py +++ b/hb_server/apps/wpm/models.py @@ -148,7 +148,9 @@ class WproductFlow(CommonAModel): on_delete=models.SET_NULL, null=True, blank=True) ticket = models.ForeignKey('wf.ticket', verbose_name='当前工单', on_delete=models.SET_NULL, null=True, blank=True) + is_lastlog = models.BooleanField('是否该子计划下的最后一条日志', default=True) + change_str = models.CharField('变动描述', default='', max_length=1000) class Pick(CommonADModel): diff --git a/hb_server/apps/wpm/services.py b/hb_server/apps/wpm/services.py index 7165d51..a4ef8aa 100644 --- a/hb_server/apps/wpm/services.py +++ b/hb_server/apps/wpm/services.py @@ -131,3 +131,18 @@ class WpmServies(object): if count_mtestok >= plan.count: plan.state = ProductionPlan.PLAN_MTEST_DONE plan.save() + + @classmethod + def add_wproduct_flow_log(cls, instance:WProduct, change_str:str=''): + """ + 创建产品变动日志 + """ + # update_fields = kwargs['update_fields'] + WproductFlow.objects.filter(wproduct=instance, subproduction_plan=instance.subproduction_plan).update(is_lastlog=False) + ins = WproductFlow() + ins.wproduct = instance + for f in WproductFlow._meta.fields: + if f.name not in ['id', 'wproduct', 'is_lastlog']: + setattr(ins, f.name, getattr(instance, f.name, None)) + ins.change_str = change_str + ins.save() diff --git a/hb_server/apps/wpm/signals.py b/hb_server/apps/wpm/signals.py index aa307ea..de92df1 100644 --- a/hb_server/apps/wpm/signals.py +++ b/hb_server/apps/wpm/signals.py @@ -34,7 +34,7 @@ def handleTicket(sender, instance, created, **kwargs): # 工单绑定半成品 wproduct.ticket = instance wproduct.save() - + WpmServies.add_wproduct_flow_log(wproduct, 'ticket_create') # 检验员 if not ticket_data.get('tester', None): ticket_data['tester'] = test_record.create_by.id @@ -69,6 +69,7 @@ def handleTicket(sender, instance, created, **kwargs): wp.ticket = None # 解除当前工单 wp.act_state = WProduct.WPR_ACT_STATE_DOWAIT wp.save() + WpmServies.add_wproduct_flow_log(wp, 'ticket_finish') # 更新子计划合格进度 WpmServies.update_subproduction_progress_main(sp=sp) @@ -81,19 +82,5 @@ def handleTicket(sender, instance, created, **kwargs): wt.save() wp.ticket = None # 解除当前工单 wp.save() - - -@receiver(post_save, sender=WProduct) -def update_wproduct_log(sender, instance, created, **kwargs): - """ - 创建产品变动日志 - """ - # update_fields = kwargs['update_fields'] - WproductFlow.objects.filter(wproduct=instance, subproduction_plan=instance.subproduction_plan).update(is_lastlog=False) - ins = WproductFlow() - ins.wproduct = instance - for f in WproductFlow._meta.fields: - if f.name not in ['id', 'create_time', 'update_time', 'wproduct', 'is_lastlog']: - setattr(ins, f.name, getattr(instance, f.name, None)) - ins.save() + WpmServies.add_wproduct_flow_log(wp, 'ticket_finish') diff --git a/hb_server/apps/wpm/views.py b/hb_server/apps/wpm/views.py index c57c4ee..adbac59 100644 --- a/hb_server/apps/wpm/views.py +++ b/hb_server/apps/wpm/views.py @@ -81,13 +81,23 @@ class WPlanViewSet(ListModelMixin, GenericViewSet): wps.update(step=first_step, act_state=WProduct.WPR_ACT_STATE_TORETEST, is_hidden=False, warehouse=None, subproduction_plan=sp, update_by=request.user, update_time=timezone.now()) - for i in wps: + + for m in i['wproducts']: + m.step = first_step + m.act_state = WProduct.WPR_ACT_STATE_TORETEST + m.is_hidden = False + m.warehouse = None + m.subproduction_plan = sp + m.update_by = request.user + m.update_time = timezone.now() + m.save() + WpmServies.add_wproduct_flow_log(instance=m, change_str='pick_half') pw = PickWproduct() pw.pick =pick - pw.wproduct = i - pw.number = i.number - pw.material = i.material - pw.subproduction_plan = i.suproduction_plan + pw.wproduct = m + pw.number = m.number + pw.material = m.material + pw.subproduction_plan = m.suproduction_plan pw.save() sp.is_picked = True sp.save() @@ -95,63 +105,63 @@ class WPlanViewSet(ListModelMixin, GenericViewSet): return Response() - @action(methods=['post'], detail=True, perms_map={'post':'*'}, serializer_class=WplanPutInSerializer) - @transaction.atomic - def putin(self, request, pk=None): - """ - 半成品入库 - """ - serializer= WplanPutInSerializer(data=request.data) - serializer.is_valid(raise_exception=True) - vdata = serializer.validated_data - subplan = self.get_object() - material = subplan.product - batch = subplan.number - warehouse = vdata['warehouse'] - wproducts = WProduct.objects.filter(subproduction_plan=subplan, - act_state=WProduct.WPR_ACT_STATE_OK, material=material, is_deleted=False) - if wproducts.exists(): - # 创建入库记录 - remark = vdata.get('remark', '') - fifo = FIFO.objects.create(type=FIFO.FIFO_TYPE_DO_IN, - is_audited=True, auditor=request.user, inout_date=timezone.now(), create_by=request.user, remark=remark) - # 创建入库明细 - fifoitem = FIFOItem() - fifoitem.is_tested = True - fifoitem.is_testok = True - fifoitem.warehouse = warehouse - fifoitem.material = material - fifoitem.count = wproducts.count() - fifoitem.batch = batch - fifoitem.fifo = fifo - fifoitem.subproduction_plan = subplan - fifoitem.save() - # 创建入库明细半成品 - ips = [] - for i in wproducts: - ip = {} - ip['fifoitem'] = fifoitem - ip['wproduct'] = i - ip['number'] = i.number - ip['material'] = material - ips.append(FIFOItemProduct(**ip)) - FIFOItemProduct.objects.bulk_create(ips) - # 创建IProduct - ips2 = [] - for i in wproducts: - ip = {} - ip['warehouse'] = warehouse - ip['batch'] = batch - ip['wproduct'] = i - ip['number'] = i.number - ip['material'] = material - ips2.append(IProduct(**ip)) - IProduct.objects.bulk_create(ips2) - # 更新库存并修改半成品进行状态 - update_inm(fifo) - wproducts.update(act_sate=WProduct.WPR_ACT_STATE_INM, warehouse=warehouse, update_by=request.user, update_time=timezone.now()) + # @action(methods=['post'], detail=True, perms_map={'post':'*'}, serializer_class=WplanPutInSerializer) + # @transaction.atomic + # def putin(self, request, pk=None): + # """ + # 半成品入库 + # """ + # serializer= WplanPutInSerializer(data=request.data) + # serializer.is_valid(raise_exception=True) + # vdata = serializer.validated_data + # subplan = self.get_object() + # material = subplan.product + # batch = subplan.number + # warehouse = vdata['warehouse'] + # wproducts = WProduct.objects.filter(subproduction_plan=subplan, + # act_state=WProduct.WPR_ACT_STATE_OK, material=material, is_deleted=False) + # if wproducts.exists(): + # # 创建入库记录 + # remark = vdata.get('remark', '') + # fifo = FIFO.objects.create(type=FIFO.FIFO_TYPE_DO_IN, + # is_audited=True, auditor=request.user, inout_date=timezone.now(), create_by=request.user, remark=remark) + # # 创建入库明细 + # fifoitem = FIFOItem() + # fifoitem.is_tested = True + # fifoitem.is_testok = True + # fifoitem.warehouse = warehouse + # fifoitem.material = material + # fifoitem.count = wproducts.count() + # fifoitem.batch = batch + # fifoitem.fifo = fifo + # fifoitem.subproduction_plan = subplan + # fifoitem.save() + # # 创建入库明细半成品 + # ips = [] + # for i in wproducts: + # ip = {} + # ip['fifoitem'] = fifoitem + # ip['wproduct'] = i + # ip['number'] = i.number + # ip['material'] = material + # ips.append(FIFOItemProduct(**ip)) + # FIFOItemProduct.objects.bulk_create(ips) + # # 创建IProduct + # ips2 = [] + # for i in wproducts: + # ip = {} + # ip['warehouse'] = warehouse + # ip['batch'] = batch + # ip['wproduct'] = i + # ip['number'] = i.number + # ip['material'] = material + # ips2.append(IProduct(**ip)) + # IProduct.objects.bulk_create(ips2) + # # 更新库存并修改半成品进行状态 + # update_inm(fifo) + # wproducts.update(act_sate=WProduct.WPR_ACT_STATE_INM, warehouse=warehouse, update_by=request.user, update_time=timezone.now()) - return Response() + # return Response() class WMaterialViewSet(CreateUpdateModelAMixin, ListModelMixin, GenericViewSet): @@ -232,10 +242,12 @@ class WProductViewSet(ListModelMixin, GenericViewSet): elif wproduct.act_state == WProduct.WPR_ACT_STATE_TOCOMBTEST: savedict['type'] = TestRecord.TEST_COMB tr = TestRecord.objects.create(**savedict) + # 更新wproduct wproduct.test = tr wproduct.update_by = request.user wproduct.update_time = timezone.now() wproduct.save() + WpmServies.add_wproduct_flow_log(wproduct, 'test_init') # 创建检验条目 for i in RecordFormField.objects.filter(form=form, is_deleted=False): tri = TestRecordItem() @@ -298,6 +310,7 @@ class WProductViewSet(ListModelMixin, GenericViewSet): i.update_by = request.user i.update_time = timezone.now() i.save() + WpmServies.add_wproduct_flow_log(i, 'putins') return Response() @action(methods=['post'], detail=True, perms_map={'post':'*'}, serializer_class=WproductPutInSerializer) @@ -345,6 +358,7 @@ class WProductViewSet(ListModelMixin, GenericViewSet): wproduct.act_state=WProduct.WPR_ACT_STATE_INM wproduct.warehouse=warehouse wproduct.save() + WpmServies.add_wproduct_flow_log(wproduct, 'putin') return Response() @action(methods=['post'], detail=True, perms_map={'post':'*'}, serializer_class=ScrapSerializer) @@ -370,6 +384,7 @@ class WProductViewSet(ListModelMixin, GenericViewSet): obj.update_by = request.user obj.update_time = timezone.now() obj.save() + WpmServies.add_wproduct_flow_log(obj, 'scrap') return Response() # @action(methods=['get'], detail=False, perms_map={'get':'*'}) @@ -445,7 +460,11 @@ class OperationViewSet(ListModelMixin, RetrieveModelMixin, CreateModelMixin, Upd if instance.is_submited: raise exceptions.APIException('该操作已提交') # 恢复半成品可操作 - instance.wp_operation.all().update(act_state=WProduct.WPR_ACT_STATE_DOWAIT) + for i in instance.wp_operation.all(): + i.act_state = WProduct.WPR_ACT_STATE_DOWAIT + i.update_by = request.user + i.save() + WpmServies.add_wproduct_flow_log(i, 'operation_delete') self.perform_destroy(instance) return Response(status=status.HTTP_204_NO_CONTENT) @@ -465,9 +484,13 @@ class OperationViewSet(ListModelMixin, RetrieveModelMixin, CreateModelMixin, Upd # 创建操作所用半成品关联记录 if 'wproducts' in vdata: owps = [] - WProduct.objects.filter(pk__in=[x.id for x in vdata['wproducts']]).update(operation=op, act_state=WProduct.WPR_ACT_STATE_DOING) splans = WpmServies.get_subplans_queryset_from_wproducts(vdata['wproducts']) for wpd in vdata['wproducts']: + wpd.operation= op + wpd.act_state = WProduct.WPR_ACT_STATE_DOING + wpd.update_by = request.user + wpd.save() + WpmServies.add_wproduct_flow_log(wpd, 'operation_create') owp = {} owp['operation'] = op owp['wproduct'] = wpd @@ -565,6 +588,7 @@ class OperationViewSet(ListModelMixin, RetrieveModelMixin, CreateModelMixin, Upd wp.operation = None wp.update_by = request.user wp.save() + WpmServies.add_wproduct_flow_log(wp, 'operation_submit') elif step.type == Step.STEP_TYPE_DIV: # 更新物料产出情况 for i in OperationMaterial.objects.filter(operation=op, type=SubprodctionMaterial.SUB_MA_TYPE_OUT): @@ -595,6 +619,7 @@ class OperationViewSet(ListModelMixin, RetrieveModelMixin, CreateModelMixin, Upd WpmServies.update_subproduction_progress_main(sp=oms_w.subproduction_plan) wproduct.create_by = request.user wproduct.save() + WpmServies.add_wproduct_flow_log(wproduct, 'operation_submit') # 隐藏原半成品 wps = WProduct.objects.filter(ow_wproduct__operation = op) wps.update(is_hidden=True, child=wproduct, update_by=request.user, update_time=timezone.now())