feat: 物料批次导入功能
This commit is contained in:
parent
08f9fa106a
commit
c8cc3469e1
|
@ -1,8 +1,10 @@
|
|||
from apps.inm.models import MIO, MIOItem, MaterialBatch, MaterialBatchA, MIOItemA
|
||||
from apps.inm.models import MIO, MIOItem, MaterialBatch, MaterialBatchA, MIOItemA, WareHouse
|
||||
from rest_framework.exceptions import ValidationError, ParseError
|
||||
from django.db.models.aggregates import Sum
|
||||
from django.db.models import F
|
||||
from apps.wpm.services import do_out, do_in
|
||||
from apps.mtm.models import Material
|
||||
from apps.mtm.models import Material, Process
|
||||
from apps.utils.tools import ranstr
|
||||
|
||||
|
||||
class InmService:
|
||||
|
@ -68,3 +70,70 @@ class InmService:
|
|||
material_count = 0
|
||||
Material.objects.filter(id=material.id).update(
|
||||
count=material_count)
|
||||
|
||||
|
||||
def daoru_mb(path: str):
|
||||
"""
|
||||
导入物料批次(如没有物料自动创建)
|
||||
"""
|
||||
objs1 = Material.objects.filter(specification__contains=' ')
|
||||
for i in objs1:
|
||||
i.specification = i.specification.replace(' ', '')
|
||||
i.save()
|
||||
objs2 = Material.objects.filter(specification__contains='×')
|
||||
for i in objs2:
|
||||
i.specification = i.specification.replace('×', '*')
|
||||
i.save()
|
||||
objs3 = (Material.objects.filter(
|
||||
specification__contains='优级') | Material.objects.filter(specification__contains='一级')).exclude(specification__contains='|')
|
||||
for i in objs3:
|
||||
i.specification = i.specification.replace(
|
||||
'优级', '|优级').replace('一级', '|一级')
|
||||
i.save()
|
||||
type_dict = {'主要原料': 30, '半成品': 20, '成品': 10,
|
||||
'辅助材料': 40, '加工工具': 50, '辅助工装': 60, '办公用品': 70}
|
||||
from apps.utils.snowflake import idWorker
|
||||
from openpyxl import load_workbook
|
||||
wb = load_workbook(path)
|
||||
process_l = Process.objects.all()
|
||||
process_d = {p.name: p for p in process_l}
|
||||
warehouse_l = WareHouse.objects.all()
|
||||
warehouse_d = {w.name: w for w in warehouse_l}
|
||||
for sheet in wb.worksheets:
|
||||
i = 3
|
||||
while sheet[f'a{i}'].value:
|
||||
try:
|
||||
type = type_dict[sheet[f'a{i}'].value.replace(' ', '')]
|
||||
name = sheet[f'b{i}'].value.replace(' ', '')
|
||||
specification = sheet[f'c{i}'].value.replace(
|
||||
' ', '') + '|' + sheet[f'd{i}'].value.replace(' ', '')
|
||||
model = sheet[f'e{i}'].value.replace(' ', '')
|
||||
process = process_d[sheet[f'f{i}'].value.replace(' ', '')]
|
||||
batch = sheet[f'g{i}'].value.replace(' ', '')
|
||||
count = int(sheet[f'h{i}'].value)
|
||||
warehouse = warehouse_d[sheet[f'i{i}'].value.replace(' ', '')]
|
||||
except KeyError as e:
|
||||
raise ParseError(f'第{i}行数据有误:{str(e)}')
|
||||
material, _ = Material.objects.get_or_create(
|
||||
type=type, name=name, specification=specification, model=model, process=process, defaults={
|
||||
'type': type,
|
||||
'name': name,
|
||||
'specification': specification,
|
||||
'model': model,
|
||||
'process': process,
|
||||
'number': ranstr(6),
|
||||
'id': idWorker.get_id()
|
||||
})
|
||||
mb, is_created = MaterialBatch.objects.get_or_create(
|
||||
material=material, batch=batch, warehouse=warehouse, defaults={
|
||||
'material': material,
|
||||
'batch': batch,
|
||||
'warehouse': warehouse,
|
||||
'count': count,
|
||||
'id': idWorker.get_id()
|
||||
})
|
||||
# if not is_created:
|
||||
# mb.count += count
|
||||
# mb.save()
|
||||
print(f'第{i}行数据导入成功')
|
||||
i = i + 1
|
||||
|
|
|
@ -3,6 +3,7 @@ from rest_framework.mixins import ListModelMixin, DestroyModelMixin
|
|||
from rest_framework.exceptions import ParseError, PermissionDenied
|
||||
from rest_framework.decorators import action
|
||||
from django.db import transaction
|
||||
from django.conf import settings
|
||||
from rest_framework import serializers
|
||||
from django.utils import timezone
|
||||
from rest_framework.response import Response
|
||||
|
@ -13,7 +14,7 @@ from apps.inm.serializers import (
|
|||
MaterialBatchSerializer, WareHourseSerializer, MIOListSerializer, MIOItemSerializer, MioItemAnaSerializer,
|
||||
MIODoSerializer, MIOSaleSerializer, MIOPurSerializer, MIOOtherSerializer, MIOItemCreateSerializer, MaterialBatchDetailSerializer, MIODetailSerializer, MIOItemTestSerializer, MIOItemPurInTestSerializer)
|
||||
from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet
|
||||
from apps.inm.services import InmService
|
||||
from apps.inm.services import InmService, daoru_mb
|
||||
from apps.utils.mixins import BulkCreateModelMixin, BulkDestroyModelMixin, BulkUpdateModelMixin
|
||||
from apps.utils.permission import has_perm
|
||||
from .filters import MaterialBatchFilter, MioFilter
|
||||
|
@ -52,6 +53,16 @@ class MaterialBatchViewSet(ListModelMixin, CustomGenericViewSet):
|
|||
search_fields = ['material__name', 'material__number',
|
||||
'material__model', 'material__specification', 'batch']
|
||||
|
||||
@action(methods=['post'], detail=False, serializer_class=serializers.Serializer, perms_map={'post': 'materialbatch.daoru'})
|
||||
@transaction.atomic
|
||||
def daoru(self, request, *args, **kwargs):
|
||||
"""导入物料批次
|
||||
|
||||
导入物料
|
||||
"""
|
||||
daoru_mb(settings.BASE_DIR + request.data.get('path', ''))
|
||||
return Response()
|
||||
|
||||
|
||||
class MioDoViewSet(BulkCreateModelMixin, BulkUpdateModelMixin, CustomGenericViewSet):
|
||||
perms_map = {'post': '*', 'put': 'mio.do'}
|
||||
|
|
Loading…
Reference in New Issue