feat: 重新梳理计算物料总数量
This commit is contained in:
parent
ffc9f0ee4d
commit
7381a65c24
|
@ -6,8 +6,29 @@ 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
|
||||
from apps.utils.thread import MyThread
|
||||
|
||||
|
||||
def cal_material_count(materialId_list: list = []):
|
||||
"""
|
||||
计算物料总数量
|
||||
"""
|
||||
from apps.inm.models import MaterialBatch
|
||||
from apps.wpm.models import WMaterial
|
||||
if materialId_list:
|
||||
objs = Material.objects.filter(id__in=set(materialId_list))
|
||||
else:
|
||||
objs = Material.objects.all()
|
||||
for material in objs:
|
||||
mb_count = MaterialBatch.objects.filter(material=material).aggregate(total=Sum("count"))["total"]
|
||||
wm_count = WMaterial.objects.filter(material=material).aggregate(total=Sum("count"))["total"]
|
||||
if mb_count is None:
|
||||
mb_count = 0
|
||||
if wm_count is None:
|
||||
wm_count = 0
|
||||
Material.objects.filter(id=material.id).update(
|
||||
count_wm=wm_count,
|
||||
count_mb=mb_count,
|
||||
count=mb_count + wm_count)
|
||||
class InmService:
|
||||
@classmethod
|
||||
def update_inm(cls, instance: MIO, is_reverse: bool = False):
|
||||
|
@ -42,19 +63,13 @@ class InmService:
|
|||
do_in(instance)
|
||||
else:
|
||||
do_out(instance)
|
||||
else:
|
||||
raise ParseError('不支持该出入库操作')
|
||||
|
||||
# 统计物料数量
|
||||
m_ids = MIOItem.objects.filter(mio=instance).values_list('material_id', flat=True)
|
||||
MyThread(target=cal_material_count, args=(m_ids, ), daemon=True, log_err=True).start()
|
||||
|
||||
@classmethod
|
||||
def cal_mat_count(cls, material: Material):
|
||||
mb_count = MaterialBatch.objects.filter(material=material).aggregate(total=Sum("count"))["total"]
|
||||
from apps.wpm.models import WMaterial
|
||||
wm_count = WMaterial.objects.filter(material=material).aggregate(total=Sum("count"))["total"]
|
||||
if mb_count is None:
|
||||
mb_count = 0
|
||||
if wm_count is None:
|
||||
wm_count = 0
|
||||
Material.objects.filter(id=material.id).update(
|
||||
count_mb=mb_count,
|
||||
count=mb_count + wm_count)
|
||||
|
||||
@classmethod
|
||||
def update_mb(cls, instance: MIO, in_or_out: int = 1):
|
||||
|
@ -68,7 +83,7 @@ class InmService:
|
|||
raise ParseError("未填写物料明细")
|
||||
type = instance.type
|
||||
belong_dept = instance.belong_dept
|
||||
for i in MIOItem.objects.filter(mio=instance):
|
||||
for i in mioitems:
|
||||
cls.update_mb_item(i, in_or_out, 'count', type, belong_dept)
|
||||
|
||||
@classmethod
|
||||
|
@ -112,7 +127,6 @@ class InmService:
|
|||
mb.save()
|
||||
else:
|
||||
raise ParseError("不支持的操作")
|
||||
cls.cal_mat_count(material)
|
||||
|
||||
|
||||
def daoru_mb(path: str):
|
||||
|
|
|
@ -2,16 +2,3 @@ from __future__ import absolute_import, unicode_literals
|
|||
from apps.utils.tasks import CustomTask
|
||||
from celery import shared_task
|
||||
from apps.mtm.models import Material
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
def correct_material_count(materialId: str = ''):
|
||||
"""
|
||||
矫正现有物料总数量
|
||||
"""
|
||||
from apps.inm.services import InmService
|
||||
if materialId:
|
||||
objs = Material.objects.filter(id=materialId)
|
||||
else:
|
||||
objs = Material.objects.all()
|
||||
for m in objs:
|
||||
InmService.cal_mat_count(m)
|
|
@ -0,0 +1,38 @@
|
|||
# Generated by Django 3.2.12 on 2024-07-04 02:03
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('mtm', '0032_auto_20240702_1409'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='material',
|
||||
name='count_wm',
|
||||
field=models.DecimalField(decimal_places=3, default=0, max_digits=14, verbose_name='车间库存'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='material',
|
||||
name='count',
|
||||
field=models.DecimalField(decimal_places=3, default=0, max_digits=14, verbose_name='总库存'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='material',
|
||||
name='count_mb',
|
||||
field=models.DecimalField(decimal_places=3, default=0, max_digits=14, verbose_name='仓库库存'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='material',
|
||||
name='count_safe',
|
||||
field=models.DecimalField(blank=True, decimal_places=3, max_digits=14, null=True, verbose_name='安全库存数'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='material',
|
||||
name='week_esitimate_consume',
|
||||
field=models.DecimalField(blank=True, decimal_places=3, max_digits=14, null=True, verbose_name='周消耗预估'),
|
||||
),
|
||||
]
|
|
@ -57,11 +57,11 @@ class Material(CommonAModel):
|
|||
testitems = models.JSONField('检测项目', default=list, blank=True)
|
||||
sort = models.PositiveSmallIntegerField('排序', default=1)
|
||||
unit = models.CharField('基准计量单位', default='个', max_length=10)
|
||||
count = models.PositiveIntegerField('总库存', default=0)
|
||||
count_mb = models.PositiveIntegerField('仓库总数', default=0)
|
||||
count_safe = models.PositiveIntegerField('安全库存总数', null=True, blank=True)
|
||||
week_esitimate_consume = models.PositiveIntegerField(
|
||||
'周消耗预估', null=True, blank=True)
|
||||
count = models.DecimalField('总库存', max_digits=14, decimal_places=3, default=0)
|
||||
count_mb = models.DecimalField('仓库库存', max_digits=14, decimal_places=3, default=0)
|
||||
count_wm = models.DecimalField('车间库存', max_digits=14, decimal_places=3, default=0)
|
||||
count_safe = models.DecimalField('安全库存数', max_digits=14, decimal_places=3, null=True, blank=True)
|
||||
week_esitimate_consume = models.DecimalField('周消耗预估', max_digits=14, decimal_places=3, null=True, blank=True)
|
||||
process = models.ForeignKey(
|
||||
Process, verbose_name='所用工序', on_delete=models.CASCADE, null=True, blank=True)
|
||||
parent = models.ForeignKey(
|
||||
|
|
Loading…
Reference in New Issue