From f9eee5a52304c6877a1888c6a1fc9de94aaa9a1a Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 12 Jan 2026 10:21:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20handover=5Frevert=20=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/wpm/models.py | 26 +++++++++++++++++++++++++- apps/wpm/services.py | 23 ++++++----------------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/apps/wpm/models.py b/apps/wpm/models.py index 26e37796..a00b2557 100644 --- a/apps/wpm/models.py +++ b/apps/wpm/models.py @@ -14,7 +14,7 @@ from django.db.models import Count from django.db import transaction from django.db.models import Max import re -from django.db.models import Q +from django.db.models import Q, F import django.utils.timezone as timezone from apps.utils.sql import query_all_dict import logging @@ -163,6 +163,30 @@ class WMaterial(CommonBDModel): ), state__in=[WMaterial.WM_OK, WMaterial.WM_REPAIR] ) + + @classmethod + def increase(cls, wm_id: str, user:User, count, count_eweight=None): + updates = {} + if count: + updates['count'] = F('count') + count + if count_eweight: + updates['count_eweight'] = count_eweight + if not updates: + return 0 + updates["update_by"] = user + updates['update_time'] = timezone.now() + return cls.objects.filter(id=wm_id).update(**updates) + + @classmethod + def decrease(cls, wm_id: str, user:User, count): + if not count: + return 0 + updated = cls.objects.filter(id=wm_id, count__gte= count).update( + count=F('count') - count, update_by=user, update_time=timezone.now()) + if updated == 0: + batch = WMaterial.objects.get(id=wm_id).batch + raise ParseError(f'{batch}_库存不足,无法完成扣减') + return updated class Fmlog(CommonADModel): """TN: 父级生产日志 diff --git a/apps/wpm/services.py b/apps/wpm/services.py index a424ef09..59406afa 100644 --- a/apps/wpm/services.py +++ b/apps/wpm/services.py @@ -776,14 +776,7 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime, batch = wm_from.batch batches.append(batch) - updated = ( - WMaterial.objects - .filter(id=wm_from.id, count__gte=xcount) - .update(count=F('count') - xcount) - ) - - if updated == 0: - raise ParseError(f'{wm_from.batch} 车间库存不足!') + WMaterial.decrease(wm_id=wm_from.id, user=user, count=xcount) if need_add: # 开始变动 @@ -907,9 +900,7 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime, else: raise ParseError("不支持该交接类型") - WMaterial.objects.filter(id=wm_to.id).update(count=F('count') + xcount) - if handover.count_eweight: - WMaterial.objects.filter(id=wm_to.id).update(count_eweight=handover.count_eweight) + WMaterial.increase(wm_id=wm_to.id, user=user,count=xcount, count_eweight=handover.count_eweight if handover.count_eweight else None) handover_or_b.wm_to = wm_to handover_or_b.save() if material.tracking == Material.MA_TRACKING_SINGLE: @@ -937,6 +928,7 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime, ana_batch_thread(xbatchs=batches) def handover_revert(handover:Handover, handler:User=None): + handover = Handover.objects.select_for_update().get(id=handover.id) if handover.submit_time is None: raise ParseError('该交接单未提交!') ticket:Ticket = handover.ticket @@ -968,16 +960,13 @@ def handover_revert(handover:Handover, handler:User=None): # 此时是自己交给自己,不需要做任何操作 pass else: - wm.count = wm.count + item.count - wm.save() - wm_to.count = wm_to.count - item.count - if wm_to.count < 0: - raise ParseError('库存不足无法撤回!') - wm_to.save() + WMaterial.increase(wm_id=wm.id, user=handler, count=item.count) + WMaterial.decrease(wm_id=wm_to.id, user=handler, count=item.count) if material.tracking == Material.MA_TRACKING_SINGLE: handoverbws = Handoverbw.objects.filter(handoverb=item) if handoverbws.count() != item.count: raise ParseError("交接与明细数量不一致,操作失败") + wm = WMaterial.objects.get(id=wm.id) for item in handoverbws: wpr:Wpr = item.wpr Wpr.change_or_new(wpr=wpr, wm=wm, old_wm=wpr.wm, old_mb=wpr.mb, add_version=False)