fix: 优化物料批次导入
This commit is contained in:
parent
92bf0ac29e
commit
f7d5375a5f
|
@ -8,7 +8,6 @@ from apps.utils.tools import ranstr
|
||||||
|
|
||||||
|
|
||||||
class InmService:
|
class InmService:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update_inm(cls, instance: MIO, is_reverse: bool = False):
|
def update_inm(cls, instance: MIO, is_reverse: bool = False):
|
||||||
"""
|
"""
|
||||||
|
@ -21,6 +20,7 @@ class InmService:
|
||||||
cls.update_mb(instance, in_or_out)
|
cls.update_mb(instance, in_or_out)
|
||||||
if instance.type == MIO.MIO_TYPE_PUR_IN: # 需要更新订单
|
if instance.type == MIO.MIO_TYPE_PUR_IN: # 需要更新订单
|
||||||
from apps.pum.services import PumService
|
from apps.pum.services import PumService
|
||||||
|
|
||||||
PumService.mio_purin(instance, is_reverse)
|
PumService.mio_purin(instance, is_reverse)
|
||||||
elif instance.type == MIO.MIO_TYPE_DO_IN:
|
elif instance.type == MIO.MIO_TYPE_DO_IN:
|
||||||
if is_reverse:
|
if is_reverse:
|
||||||
|
@ -34,26 +34,26 @@ class InmService:
|
||||||
cls.update_mb(instance, in_or_out)
|
cls.update_mb(instance, in_or_out)
|
||||||
if instance.type == MIO.MIO_TYPE_SALE_OUT:
|
if instance.type == MIO.MIO_TYPE_SALE_OUT:
|
||||||
from apps.sam.services import SamService
|
from apps.sam.services import SamService
|
||||||
|
|
||||||
SamService.mio_saleout(instance, is_reverse)
|
SamService.mio_saleout(instance, is_reverse)
|
||||||
elif instance.type == MIO.MIO_TYPE_DO_OUT:
|
elif instance.type == MIO.MIO_TYPE_DO_OUT:
|
||||||
if is_reverse:
|
if is_reverse:
|
||||||
do_in(instance)
|
do_in(instance)
|
||||||
else:
|
else:
|
||||||
do_out(instance)
|
do_out(instance)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def cal_mat_count(cls, material: Material):
|
def cal_mat_count(cls, material: Material):
|
||||||
material_count = MaterialBatch.objects.filter(
|
material_count = MaterialBatch.objects.filter(material=material).aggregate(total=Sum("count"))["total"]
|
||||||
material=material).aggregate(total=Sum('count'))['total']
|
|
||||||
from apps.wpm.models import WMaterial
|
from apps.wpm.models import WMaterial
|
||||||
wm_count = WMaterial.objects.filter(material=material).aggregate(total=Sum('count'))['total']
|
|
||||||
|
wm_count = WMaterial.objects.filter(material=material).aggregate(total=Sum("count"))["total"]
|
||||||
if material_count is None:
|
if material_count is None:
|
||||||
material_count = 0
|
material_count = 0
|
||||||
if wm_count is None:
|
if wm_count is None:
|
||||||
wm_count = 0
|
wm_count = 0
|
||||||
Material.objects.filter(id=material.id).update(
|
Material.objects.filter(id=material.id).update(count=material_count + wm_count)
|
||||||
count=material_count+wm_count)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update_mb(cls, instance: MIO, in_or_out: int = 1):
|
def update_mb(cls, instance: MIO, in_or_out: int = 1):
|
||||||
"""
|
"""
|
||||||
|
@ -64,39 +64,39 @@ class InmService:
|
||||||
belong_dept = instance.belong_dept
|
belong_dept = instance.belong_dept
|
||||||
mioitems = MIOItem.objects.filter(mio=instance)
|
mioitems = MIOItem.objects.filter(mio=instance)
|
||||||
if not mioitems.exists():
|
if not mioitems.exists():
|
||||||
raise ParseError('未填写物料明细')
|
raise ParseError("未填写物料明细")
|
||||||
for i in MIOItem.objects.filter(mio=instance):
|
for i in MIOItem.objects.filter(mio=instance):
|
||||||
material = i.material
|
material = i.material
|
||||||
warehouse = i.warehouse
|
warehouse = i.warehouse
|
||||||
mb, is_created = MaterialBatch.objects.get_or_create(material=material, warehouse=warehouse, batch=i.batch,
|
mb, is_created = MaterialBatch.objects.get_or_create(
|
||||||
defaults={'material': material, 'warehouse': warehouse, 'count': 0, 'batch': i.batch})
|
material=material, warehouse=warehouse, batch=i.batch, defaults={"material": material, "warehouse": warehouse, "count": 0, "batch": i.batch}
|
||||||
|
)
|
||||||
if in_or_out == 1:
|
if in_or_out == 1:
|
||||||
mb.count = mb.count + i.count
|
mb.count = mb.count + i.count
|
||||||
# if mb.expiration_date is None:
|
# if mb.expiration_date is None:
|
||||||
# mb.expiration_date = i.expiration_date
|
# mb.expiration_date = i.expiration_date
|
||||||
if instance.type == MIO.MIO_TYPE_DO_IN: # 生产入库需要记录production_dept字段
|
if instance.type == MIO.MIO_TYPE_DO_IN: # 生产入库需要记录production_dept字段
|
||||||
if mb.production_dept is None or mb.production_dept == belong_dept:
|
if mb.production_dept is None or mb.production_dept == belong_dept:
|
||||||
mb.production_dept = belong_dept
|
mb.production_dept = belong_dept
|
||||||
else:
|
else:
|
||||||
raise ParseError('同种物料不同生产车间应该有不同批次号!')
|
raise ParseError("同种物料不同生产车间应该有不同批次号!")
|
||||||
mb.save()
|
mb.save()
|
||||||
mias = MIOItemA.objects.filter(mioitem=i)
|
mias = MIOItemA.objects.filter(mioitem=i)
|
||||||
if mias.exists(): # 组合件入库
|
if mias.exists(): # 组合件入库
|
||||||
if not is_created:
|
if not is_created:
|
||||||
raise ParseError('该批次组合件已存在')
|
raise ParseError("该批次组合件已存在")
|
||||||
for mia in mias:
|
for mia in mias:
|
||||||
MaterialBatchA.objects.create(
|
MaterialBatchA.objects.create(mb=mb, material=mia.material, batch=mia.batch)
|
||||||
mb=mb, material=mia.material, batch=mia.batch)
|
|
||||||
elif in_or_out == -1:
|
elif in_or_out == -1:
|
||||||
mb.count = mb.count - i.count
|
mb.count = mb.count - i.count
|
||||||
if mb.count < 0:
|
if mb.count < 0:
|
||||||
raise ParseError('批次库存不足,操作失败')
|
raise ParseError("批次库存不足,操作失败")
|
||||||
elif mb.count == 0:
|
elif mb.count == 0:
|
||||||
mb.delete()
|
mb.delete()
|
||||||
else:
|
else:
|
||||||
mb.save()
|
mb.save()
|
||||||
else:
|
else:
|
||||||
raise ParseError('不支持的操作')
|
raise ParseError("不支持的操作")
|
||||||
cls.cal_mat_count(material)
|
cls.cal_mat_count(material)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -106,41 +106,42 @@ class InmService:
|
||||||
material = ins.material
|
material = ins.material
|
||||||
warehouse = ins.warehouse
|
warehouse = ins.warehouse
|
||||||
try:
|
try:
|
||||||
mb = MaterialBatch.objects.get(
|
mb = MaterialBatch.objects.get(material=material, batch=batch, warehouse=warehouse)
|
||||||
material=material, batch=batch, warehouse=warehouse)
|
|
||||||
count_new = mb.count - count_notok
|
count_new = mb.count - count_notok
|
||||||
if count_new < 0:
|
if count_new < 0:
|
||||||
raise ParseError('库存扣减失败,请确认!')
|
raise ParseError("库存扣减失败,请确认!")
|
||||||
mb.count = count_new
|
mb.count = count_new
|
||||||
mb.save()
|
mb.save()
|
||||||
except MaterialBatch.DoesNotExist:
|
except MaterialBatch.DoesNotExist:
|
||||||
# 库存不存在已被消耗了
|
# 库存不存在已被消耗了
|
||||||
if count_notok != 0:
|
if count_notok != 0:
|
||||||
raise ParseError('库存已全消耗!')
|
raise ParseError("库存已全消耗!")
|
||||||
cls.cal_mat_count(material)
|
cls.cal_mat_count(material)
|
||||||
|
|
||||||
|
|
||||||
def daoru_mb(path: str):
|
def daoru_mb(path: str):
|
||||||
"""
|
"""
|
||||||
导入物料批次(如没有物料自动创建)
|
导入物料批次(如没有物料自动创建)
|
||||||
"""
|
"""
|
||||||
objs1 = Material.objects.filter(specification__contains=' ')
|
# 注释的是初次导入时做的数据矫正
|
||||||
for i in objs1:
|
# objs1 = Material.objects.filter(specification__contains=' ')
|
||||||
i.specification = i.specification.replace(' ', '')
|
# for i in objs1:
|
||||||
i.save()
|
# i.specification = i.specification.replace(' ', '')
|
||||||
objs2 = Material.objects.filter(specification__contains='×')
|
# i.save()
|
||||||
for i in objs2:
|
# objs2 = Material.objects.filter(specification__contains='×')
|
||||||
i.specification = i.specification.replace('×', '*')
|
# for i in objs2:
|
||||||
i.save()
|
# i.specification = i.specification.replace('×', '*')
|
||||||
objs3 = (Material.objects.filter(
|
# i.save()
|
||||||
specification__contains='优级') | Material.objects.filter(specification__contains='一级')).exclude(specification__contains='|')
|
# objs3 = (Material.objects.filter(
|
||||||
for i in objs3:
|
# specification__contains='优级') | Material.objects.filter(specification__contains='一级')).exclude(specification__contains='|')
|
||||||
i.specification = i.specification.replace(
|
# for i in objs3:
|
||||||
'优级', '|优级').replace('一级', '|一级')
|
# i.specification = i.specification.replace(
|
||||||
i.save()
|
# '优级', '|优级').replace('一级', '|一级')
|
||||||
type_dict = {'主要原料': 30, '半成品': 20, '成品': 10,
|
# i.save()
|
||||||
'辅助材料': 40, '加工工具': 50, '辅助工装': 60, '办公用品': 70}
|
type_dict = {"主要原料": 30, "半成品": 20, "成品": 10, "辅助材料": 40, "加工工具": 50, "辅助工装": 60, "办公用品": 70}
|
||||||
from apps.utils.snowflake import idWorker
|
from apps.utils.snowflake import idWorker
|
||||||
from openpyxl import load_workbook
|
from openpyxl import load_workbook
|
||||||
|
|
||||||
wb = load_workbook(path)
|
wb = load_workbook(path)
|
||||||
process_l = Process.objects.all()
|
process_l = Process.objects.all()
|
||||||
process_d = {p.name: p for p in process_l}
|
process_d = {p.name: p for p in process_l}
|
||||||
|
@ -148,39 +149,34 @@ def daoru_mb(path: str):
|
||||||
warehouse_d = {w.name: w for w in warehouse_l}
|
warehouse_d = {w.name: w for w in warehouse_l}
|
||||||
for sheet in wb.worksheets:
|
for sheet in wb.worksheets:
|
||||||
i = 3
|
i = 3
|
||||||
while sheet[f'a{i}'].value:
|
while sheet[f"a{i}"].value:
|
||||||
try:
|
try:
|
||||||
type = type_dict[sheet[f'a{i}'].value.replace(' ', '')]
|
type = type_dict[sheet[f"a{i}"].value.replace(" ", "")]
|
||||||
name = sheet[f'b{i}'].value.replace(' ', '')
|
name = sheet[f"b{i}"].value.replace(" ", "")
|
||||||
specification = sheet[f'c{i}'].value.replace(
|
specification = sheet[f"c{i}"].value.replace(" ", "")
|
||||||
' ', '') + '|' + sheet[f'd{i}'].value.replace(' ', '')
|
if sheet[f"d{i}"].value and sheet[f"d{i}"].value.replace(" ", ""):
|
||||||
model = sheet[f'e{i}'].value.replace(' ', '')
|
specification = specification + "|" + sheet[f"d{i}"].value.replace(" ", "")
|
||||||
process = process_d[sheet[f'f{i}'].value.replace(' ', '')]
|
model = sheet[f"e{i}"].value.replace(" ", "")
|
||||||
batch = sheet[f'g{i}'].value.replace(' ', '')
|
process = process_d[sheet[f"f{i}"].value.replace(" ", "")]
|
||||||
count = int(sheet[f'h{i}'].value)
|
batch = sheet[f"g{i}"].value.replace(" ", "")
|
||||||
warehouse = warehouse_d[sheet[f'i{i}'].value.replace(' ', '')]
|
count = int(sheet[f"h{i}"].value)
|
||||||
|
warehouse = warehouse_d[sheet[f"i{i}"].value.replace(" ", "")]
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
raise ParseError(f'第{i}行数据有误:{str(e)}')
|
raise ParseError(f"第{i}行数据有误:{str(e)}")
|
||||||
material, _ = Material.objects.get_or_create(
|
material, _ = Material.objects.get_or_create(
|
||||||
type=type, name=name, specification=specification, model=model, process=process, defaults={
|
type=type,
|
||||||
'type': type,
|
name=name,
|
||||||
'name': name,
|
specification=specification,
|
||||||
'specification': specification,
|
model=model,
|
||||||
'model': model,
|
process=process,
|
||||||
'process': process,
|
defaults={"type": type, "name": name, "specification": specification, "model": model, "process": process, "number": ranstr(6), "id": idWorker.get_id()},
|
||||||
'number': ranstr(6),
|
)
|
||||||
'id': idWorker.get_id()
|
|
||||||
})
|
|
||||||
mb, is_created = MaterialBatch.objects.get_or_create(
|
mb, is_created = MaterialBatch.objects.get_or_create(
|
||||||
material=material, batch=batch, warehouse=warehouse, defaults={
|
material=material, batch=batch, warehouse=warehouse, defaults={"material": material, "batch": batch, "warehouse": warehouse, "count": count, "id": idWorker.get_id()}
|
||||||
'material': material,
|
)
|
||||||
'batch': batch,
|
InmService.cal_mat_count(material)
|
||||||
'warehouse': warehouse,
|
|
||||||
'count': count,
|
|
||||||
'id': idWorker.get_id()
|
|
||||||
})
|
|
||||||
# if not is_created:
|
# if not is_created:
|
||||||
# mb.count += count
|
# mb.count += count
|
||||||
# mb.save()
|
# mb.save()
|
||||||
print(f'第{i}行数据导入成功')
|
print(f"第{i}行数据导入成功")
|
||||||
i = i + 1
|
i = i + 1
|
||||||
|
|
Loading…
Reference in New Issue