From 6f8ab25b5c49965e3323b81977a1befe6fa3559b Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 22 Apr 2024 11:14:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=8D=8A?= =?UTF-8?q?=E6=88=90=E5=93=81=E6=A3=80=E9=AA=8C=E6=92=A4=E5=9B=9E=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/inm/services.py | 93 +++++++++++++++++++++----------------------- apps/inm/views.py | 17 +++++++- 2 files changed, 60 insertions(+), 50 deletions(-) diff --git a/apps/inm/services.py b/apps/inm/services.py index f4358d39..43132c05 100644 --- a/apps/inm/services.py +++ b/apps/inm/services.py @@ -5,6 +5,7 @@ from django.db.models import F from apps.wpm.services import do_out, do_in from apps.mtm.models import Material, Process from apps.utils.tools import ranstr +from apps.system.models import Dept class InmService: @@ -61,63 +62,57 @@ class InmService: in = 1 out = -1 """ - belong_dept = instance.belong_dept mioitems = MIOItem.objects.filter(mio=instance) if not mioitems.exists(): raise ParseError("未填写物料明细") + type = instance.type + prodction_dept = instance.belong_dept for i in MIOItem.objects.filter(mio=instance): - material = i.material - warehouse = i.warehouse - mb, is_created = MaterialBatch.objects.get_or_create( - material=material, warehouse=warehouse, batch=i.batch, defaults={"material": material, "warehouse": warehouse, "count": 0, "batch": i.batch} - ) - if in_or_out == 1: - mb.count = mb.count + i.count - # if mb.expiration_date is None: - # mb.expiration_date = i.expiration_date - if instance.type == MIO.MIO_TYPE_DO_IN: # 生产入库需要记录production_dept字段 - if mb.production_dept is None or mb.production_dept == belong_dept: - mb.production_dept = belong_dept - else: - raise ParseError("同种物料不同生产车间应该有不同批次号!") - mb.save() - mias = MIOItemA.objects.filter(mioitem=i) - if mias.exists(): # 组合件入库 - if not is_created: - raise ParseError("该批次组合件已存在") - for mia in mias: - MaterialBatchA.objects.create(mb=mb, material=mia.material, batch=mia.batch) - elif in_or_out == -1: - mb.count = mb.count - i.count - if mb.count < 0: - raise ParseError("批次库存不足,操作失败") - elif mb.count == 0: - mb.delete() - else: - mb.save() - else: - raise ParseError("不支持的操作") - cls.cal_mat_count(material) + cls.update_mb_item(i, in_or_out, type, prodction_dept) @classmethod - def update_mb_after_test(cls, ins: MIOItem): - count_notok = ins.count_notok - batch = ins.batch - material = ins.material - warehouse = ins.warehouse - try: - mb = MaterialBatch.objects.get(material=material, batch=batch, warehouse=warehouse) - count_new = mb.count - count_notok - if count_new < 0: - raise ParseError("库存扣减失败,请确认!") - mb.count = count_new + def update_mb_item(cls, i: MIOItem, in_or_out: int = 1, field:str='count', type: str =None, production_dept: Dept=None): + """ + 更新物料批次(根据明细) + in = 1 + out = -1 + 默认使用count字段做加减 + """ + if type is None or production_dept is None: + mio = i.mio + type = mio.type + production_dept = mio.belong_dept + material = i.material + warehouse = i.warehouse + mb, is_created = MaterialBatch.objects.get_or_create( + material=material, warehouse=warehouse, batch=i.batch, defaults={"material": material, "warehouse": warehouse, "count": 0, "batch": i.batch} + ) + if in_or_out == 1: + mb.count = mb.count + getattr(i, field) + if type == MIO.MIO_TYPE_DO_IN: # 生产入库需要记录production_dept字段 + if mb.production_dept is None or mb.production_dept == production_dept: + mb.production_dept = production_dept + else: + raise ParseError("同种物料不同生产车间应该有不同批次号!") mb.save() - except MaterialBatch.DoesNotExist: - # 库存不存在已被消耗了 - if count_notok != 0: - raise ParseError("库存已全消耗!") + mias = MIOItemA.objects.filter(mioitem=i) + if mias.exists(): # 组合件入库 + if not is_created: + raise ParseError("该批次组合件已存在") + for mia in mias: + MaterialBatchA.objects.create(mb=mb, material=mia.material, batch=mia.batch) + elif in_or_out == -1: + mb.count = mb.count - getattr(i, field) + if mb.count < 0: + raise ParseError("批次库存不足,操作失败") + elif mb.count == 0: + mb.delete() + else: + mb.save() + else: + raise ParseError("不支持的操作") cls.cal_mat_count(material) - + def daoru_mb(path: str): """ diff --git a/apps/inm/views.py b/apps/inm/views.py index e2c58abc..c32d0634 100644 --- a/apps/inm/views.py +++ b/apps/inm/views.py @@ -253,7 +253,22 @@ class MIOItemViewSet(ListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin sr.is_valid(raise_exception=True) sr.save() # 开始变动库存 - InmService.update_mb_after_test(ins) + InmService.update_mb_item(ins, -1, 'count_notok', mio.type, mio.belong_dept) + return Response() + + @action(methods=['post'], detail=True, perms_map={'post': 'mioitem.test'}, serializer_class=serializers.Serializer) + @transaction.atomic + def test_revert(self, request, *args, **kwargs): + """ + 检验撤回 + """ + ins: MIOItem = self.get_object() + mio: MIO = ins.mio + if ins.test_date is None: + raise ParseError('该明细还未检验') + InmService.update_mb_item(ins, 1, 'count_notok', mio.type, mio.belong_dept) + ins.test_date = None + ins.save() return Response() @action(methods=['post'], detail=True, perms_map={'post': 'mioitem.test'}, serializer_class=MIOItemPurInTestSerializer) From 45fda773abd5689adcfa6403e039c5c1145059c0 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 22 Apr 2024 15:23:24 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20mqtt=E6=8F=92=E5=85=A5=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=97=B6=E9=9C=80=E6=A0=A1=E9=AA=8Cenabled?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/enm/services.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/enm/services.py b/apps/enm/services.py index 0a3d9a39..abb15ffe 100644 --- a/apps/enm/services.py +++ b/apps/enm/services.py @@ -276,7 +276,7 @@ def insert_mplogx_from_king_mqtt_chunk(objs: list, otime_obj: datetime, is_offse code = f"K_{n}" cache_key = Mpoint.cache_key(code) mpoint_data = get_mpoint_cache(code) - if mpoint_data is None: + if mpoint_data is None or not mpoint_data['enabled']: continue val_type = mpoint_data["val_type"] # if is_offset: From aecec480028fd5dffb11fb49254f3a1b0590abc7 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 22 Apr 2024 15:30:42 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20mpoint=5Flast=5Ftimex=20=E5=A4=84?= =?UTF-8?q?=E7=90=86=E7=9A=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/enm/services.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/enm/services.py b/apps/enm/services.py index abb15ffe..2d7a9176 100644 --- a/apps/enm/services.py +++ b/apps/enm/services.py @@ -287,7 +287,7 @@ def insert_mplogx_from_king_mqtt_chunk(objs: list, otime_obj: datetime, is_offse # else: # timex = timezone.make_aware(datetime.strptime(timex, '%Y-%m-%d %H:%M:%S.%f')) mpoint_interval = mpoint_data["interval"] - mpoint_last_timex = mpoint_data.get("last_timex", None) + mpoint_last_timex = mpoint_data.get('last_data', {}).get('last_timex', None) mpoint_is_rep_ep_running_state = mpoint_data.get("is_rep_ep_running_state", False) mpoint_ep_base_val1 = mpoint_data.get("ep_base_val1", None) # 控制采集间隔