feat: MaterialBatch 添加字段以支持追踪原料批次

This commit is contained in:
caoqianming 2024-11-13 17:22:25 +08:00
parent 7d9582fe34
commit 4016c421be
4 changed files with 64 additions and 2 deletions

View File

@ -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='批次号'),
),
]

View File

@ -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')

View File

@ -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字段

View File

@ -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,