From 4016c421beae7fa32b375c568f201c6507aaf115 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Wed, 13 Nov 2024 17:22:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20MaterialBatch=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E4=BB=A5=E6=94=AF=E6=8C=81=E8=BF=BD=E8=B8=AA?= =?UTF-8?q?=E5=8E=9F=E6=96=99=E6=89=B9=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inm/migrations/0021_auto_20241113_1716.py | 30 +++++++++++++++++++ apps/inm/models.py | 5 +++- apps/inm/services.py | 19 +++++++++++- apps/wpm/services.py | 12 ++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 apps/inm/migrations/0021_auto_20241113_1716.py diff --git a/apps/inm/migrations/0021_auto_20241113_1716.py b/apps/inm/migrations/0021_auto_20241113_1716.py new file mode 100644 index 00000000..524348f4 --- /dev/null +++ b/apps/inm/migrations/0021_auto_20241113_1716.py @@ -0,0 +1,30 @@ +# Generated by Django 3.2.12 on 2024-11-13 09:16 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('mtm', '0042_auto_20241010_1140'), + ('inm', '0020_materialbatch_supplier'), + ] + + operations = [ + migrations.AddField( + model_name='materialbatch', + name='batch_ofrom', + field=models.TextField(blank=True, null=True, verbose_name='原料批次号'), + ), + migrations.AddField( + model_name='materialbatch', + name='material_ofrom', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='mb_mofrom', to='mtm.material', verbose_name='原料物料'), + ), + migrations.AlterField( + model_name='materialbatch', + name='batch', + field=models.TextField(verbose_name='批次号'), + ), + ] diff --git a/apps/inm/models.py b/apps/inm/models.py index e0ac4ba3..e0753961 100644 --- a/apps/inm/models.py +++ b/apps/inm/models.py @@ -20,7 +20,7 @@ class MaterialBatch(BaseModel): """ 物料批次 """ - batch = models.CharField('批次号', max_length=100) + batch = models.TextField('批次号') material = models.ForeignKey( Material, on_delete=models.CASCADE, verbose_name='物料') warehouse = models.ForeignKey( @@ -30,6 +30,9 @@ class MaterialBatch(BaseModel): expiration_date = models.DateField('有效期', null=True, blank=True) supplier = models.ForeignKey( Supplier, verbose_name='供应商', on_delete=models.SET_NULL, null=True, blank=True) + + batch_ofrom = models.TextField('原料批次号', null=True, blank=True) + material_ofrom = models.ForeignKey(Material, verbose_name='原料物料', on_delete=models.SET_NULL, null=True, blank=True, related_name='mb_mofrom') class Meta: unique_together = ('material', 'batch', 'warehouse') diff --git a/apps/inm/services.py b/apps/inm/services.py index e561f58f..b3f8df9a 100644 --- a/apps/inm/services.py +++ b/apps/inm/services.py @@ -80,18 +80,35 @@ class InmService: out = -1 默认使用count字段做加减 """ + from apps.wpm.models import WMaterial if type is None or belong_dept is None: mio = i.mio type = mio.type belong_dept = mio.belong_dept material = i.material warehouse = i.warehouse + + ddict = {"count": 0, "supplier": i.mio.supplier} + if material.type == Material.MA_TYPE_MAINSO: + ddict["batch_ofrom"] = i.batch + ddict["material_ofrom"] = material mb, is_created = MaterialBatch.objects.get_or_create( material=material, warehouse=warehouse, batch=i.batch, - defaults={"count": 0, "supplier": i.mio.supplier} + defaults=ddict ) + if mb.batch_ofrom is None and material.type in [Material.MA_TYPE_GOOD, Material.MA_TYPE_GOOD]: + wm_qs = WMaterial.objects.filter(material=material, batch=i.batch) + batchs_count = wm_qs.values_list('batch_ofrom', flat=True).distinct().count() + if batchs_count > 1: + raise ParseError("半成品/成品同批次号的原料批次应相同!") + if batchs_count == 1: + wm = wm_qs.first() + mb.batch_ofrom = wm.batch_ofrom + mb.material_ofrom = wm.material_ofrom + mb.save() + if in_or_out == 1: mb.count = mb.count + getattr(i, field) if type == MIO.MIO_TYPE_DO_IN: # 生产入库需要记录production_dept字段 diff --git a/apps/wpm/services.py b/apps/wpm/services.py index 34672ee8..5dfa3879 100644 --- a/apps/wpm/services.py +++ b/apps/wpm/services.py @@ -139,6 +139,18 @@ def do_out(mio: MIO): action_list = [[item.material, item.batch, item.count]] for al in action_list: xmaterial, xbatch, xcount = al + ddict = { + "batch": xbatch, + "material": xmaterial, + "count": xcount, + "create_by": do_user, + "belong_dept": belong_dept, + "mgroup": mgroup, + "state": WMaterial.WM_OK, + } + if xmaterial.type == Material.MA_TYPE_MAINSO: + ddict["batch_ofrom"] = xbatch + ddict["material_ofrom"] = xmaterial # 领到车间库存(或工段) wm, new_create = WMaterial.objects.get_or_create(batch=xbatch, material=xmaterial, belong_dept=belong_dept, mgroup=mgroup,