增加事务操作
This commit is contained in:
parent
537fda2e86
commit
5d5a1e7604
|
@ -4,6 +4,7 @@ from apps.inm.models import FIFO, FIFOItem, IProduct, MaterialBatch, WareHouse,I
|
|||
|
||||
from apps.system.serializers import UserSimpleSerializer
|
||||
from apps.mtm.serializers import MaterialSimpleSerializer
|
||||
from django.db import transaction
|
||||
|
||||
class WareHouseSerializer(serializers. ModelSerializer):
|
||||
create_by_=UserSimpleSerializer('create_by', read_only=True)
|
||||
|
@ -91,28 +92,29 @@ class FIFOInPurSerializer(serializers.ModelSerializer):
|
|||
pass
|
||||
|
||||
# 创建采购入库
|
||||
validated_data['type'] = FIFO.FIFO_TYPE_PUR_IN
|
||||
obj = FIFO(**validated_data)
|
||||
obj.save()
|
||||
for i in details:
|
||||
if 'details' in i:
|
||||
p_details = i.pop('details')
|
||||
if len(p_details) != i['count']:
|
||||
raise serializers.ValidationError('数目对不上')
|
||||
with transaction.atomic():
|
||||
validated_data['type'] = FIFO.FIFO_TYPE_PUR_IN
|
||||
obj = FIFO(**validated_data)
|
||||
obj.save()
|
||||
for i in details:
|
||||
if 'details' in i:
|
||||
p_details = i.pop('details')
|
||||
if len(p_details) != i['count']:
|
||||
raise serializers.ValidationError('数目对不上')
|
||||
else:
|
||||
i['fifo'] = obj
|
||||
fifod = FIFOItem.objects.create(**i)
|
||||
p_list = []
|
||||
for x in p_details:
|
||||
x['state'] = 1
|
||||
x['material'] = i['material']
|
||||
x['warehouse'] = validated_data['warehouse']
|
||||
x['batch'] = i['batch']
|
||||
x['fifos'] = [fifod.id]
|
||||
p_list.append(IProduct(**x))
|
||||
IProduct.objects.bulk_create(p_list)
|
||||
else:
|
||||
i['fifo'] = obj
|
||||
fifod = FIFOItem.objects.create(**i)
|
||||
p_list = []
|
||||
for x in p_details:
|
||||
x['state'] = 1
|
||||
x['material'] = i['material']
|
||||
x['warehouse'] = validated_data['warehouse']
|
||||
x['batch'] = i['batch']
|
||||
x['fifos'] = [fifod.id]
|
||||
p_list.append(IProduct(**x))
|
||||
IProduct.objects.bulk_create(p_list)
|
||||
else:
|
||||
i['fifo'] = obj
|
||||
FIFOItem.objects.create(**i)
|
||||
FIFOItem.objects.create(**i)
|
||||
return obj
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ from apps.inm.signals import update_inm
|
|||
from apps.system.mixins import CreateUpdateModelAMixin, OptimizationMixin
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from django.db import transaction
|
||||
|
||||
# Create your views here.
|
||||
class WarehouseViewSet(CreateUpdateModelAMixin, ModelViewSet):
|
||||
|
@ -118,8 +119,9 @@ class FIFOViewSet(ListModelMixin, GenericViewSet):
|
|||
raise APIException('未检验通过, 不可审核')
|
||||
if obj.is_audited:
|
||||
raise APIException('该入库记录已审核通过')
|
||||
obj.is_audited = True
|
||||
obj.save()
|
||||
update_inm(obj) # 更新库存
|
||||
with transaction.atomic():
|
||||
obj.is_audited = True
|
||||
obj.save()
|
||||
update_inm(obj) # 更新库存
|
||||
return Response()
|
||||
|
|
@ -6,6 +6,7 @@ from apps.system.mixins import CreateUpdateModelAMixin
|
|||
from rest_framework.exceptions import APIException
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from django.db import transaction
|
||||
# Create your views here.
|
||||
class StandardViewSet(CreateUpdateModelAMixin, ModelViewSet):
|
||||
"""
|
||||
|
@ -66,26 +67,26 @@ class TestRecordViewSet(ModelViewSet):
|
|||
record_data = vdata.pop('record_data')
|
||||
if 'is_testok' not in vdata:
|
||||
raise APIException('未填写检测结论')
|
||||
|
||||
obj = serializer.save(create_by = self.request.user)
|
||||
tris = []
|
||||
for m in record_data: # 保存记录详情
|
||||
form_field = m['form_field']
|
||||
m['field_name'] = form_field.field_name
|
||||
m['field_key'] = form_field.field_key
|
||||
m['field_type'] = form_field.field_type
|
||||
m['field_value'] = m['field_value']
|
||||
m['sort'] = form_field.sort
|
||||
m['need_judge'] = form_field.need_judge
|
||||
m['is_testok'] = m['is_testok'] if 'is_testok' in m else True
|
||||
m['test_record'] = obj
|
||||
tris.append(TestRecordItem(**m))
|
||||
TestRecordItem.objects.bulk_create(tris)
|
||||
with transaction.atomic():
|
||||
obj = serializer.save(create_by = self.request.user)
|
||||
tris = []
|
||||
for m in record_data: # 保存记录详情
|
||||
form_field = m['form_field']
|
||||
m['field_name'] = form_field.field_name
|
||||
m['field_key'] = form_field.field_key
|
||||
m['field_type'] = form_field.field_type
|
||||
m['field_value'] = m['field_value']
|
||||
m['sort'] = form_field.sort
|
||||
m['need_judge'] = form_field.need_judge
|
||||
m['is_testok'] = m['is_testok'] if 'is_testok' in m else True
|
||||
m['test_record'] = obj
|
||||
tris.append(TestRecordItem(**m))
|
||||
TestRecordItem.objects.bulk_create(tris)
|
||||
|
||||
# 如果检测合格
|
||||
if obj.fifo_item:
|
||||
obj.fifo_item.is_testok = True if obj.is_testok else False
|
||||
obj.fifo_item.is_tested = True
|
||||
obj.fifo_item.save()
|
||||
# 如果检测合格
|
||||
if obj.fifo_item:
|
||||
obj.fifo_item.is_testok = True if obj.is_testok else False
|
||||
obj.fifo_item.is_tested = True
|
||||
obj.fifo_item.save()
|
||||
|
||||
return Response(status=status.HTTP_201_CREATED)
|
|
@ -10,6 +10,7 @@ from django.utils import timezone
|
|||
from django.utils.translation import gettext_lazy as _
|
||||
from apps.system.serializers import UserSimpleSerializer
|
||||
from apps.wpm.models import Operation, WMaterial, WProduct, OperationRecord, OperationRecordItem
|
||||
from django.db import transaction
|
||||
|
||||
class PickDetailSerializer(serializers.Serializer):
|
||||
material = serializers.PrimaryKeyRelatedField(queryset=Material.objects.all(), label="物料ID")
|
||||
|
@ -36,40 +37,41 @@ class PickSerializer(serializers.Serializer):
|
|||
except:
|
||||
raise serializers.ValidationError('物料不存在')
|
||||
# 创建出库记录
|
||||
operator = self.context['request'].user
|
||||
validated_data['create_by'] = operator
|
||||
validated_data['operator'] = operator
|
||||
validated_data['type'] = FIFO.FIFO_TYPE_DO_OUT
|
||||
validated_data['inout_date'] = timezone.now()
|
||||
fifo = FIFO.objects.create(**validated_data)
|
||||
for i in picks:
|
||||
# 更新出库详情
|
||||
i['fifo'] = fifo
|
||||
i['count'] = i.pop('pick_count')
|
||||
i['is_testok'] = True # 默认检测合格
|
||||
FIFOItem.objects.create(**i)
|
||||
# 更新车间物料
|
||||
wm, _ = WMaterial.objects.get_or_create(material=i['material'], batch=i['batch'], \
|
||||
subproduction_plan=sp,defaults={
|
||||
'material':i['material'],
|
||||
'batch':i['batch'],
|
||||
'subproduction_plan':sp,
|
||||
'count':0
|
||||
})
|
||||
wm.count = wm.count + i['count']
|
||||
wm.save()
|
||||
# 更新子计划物料情况
|
||||
spp = SubProductionProgress.objects.get(material=i['material'], subproduction_plan=sp, type=1)
|
||||
spp.count_pick = spp.count_pick + i['count']
|
||||
spp.save()
|
||||
sp.is_picked=True
|
||||
sp.state = 3 #生产中
|
||||
sp.state_date_real = timezone.now() #实际开工日期
|
||||
sp.save()
|
||||
# 更新库存
|
||||
fifo.is_audited = True
|
||||
fifo.save()
|
||||
update_inm(fifo)
|
||||
with transaction.atomic():
|
||||
operator = self.context['request'].user
|
||||
validated_data['create_by'] = operator
|
||||
validated_data['operator'] = operator
|
||||
validated_data['type'] = FIFO.FIFO_TYPE_DO_OUT
|
||||
validated_data['inout_date'] = timezone.now()
|
||||
fifo = FIFO.objects.create(**validated_data)
|
||||
for i in picks:
|
||||
# 更新出库详情
|
||||
i['fifo'] = fifo
|
||||
i['count'] = i.pop('pick_count')
|
||||
i['is_testok'] = True # 默认检测合格
|
||||
FIFOItem.objects.create(**i)
|
||||
# 更新车间物料
|
||||
wm, _ = WMaterial.objects.get_or_create(material=i['material'], batch=i['batch'], \
|
||||
subproduction_plan=sp,defaults={
|
||||
'material':i['material'],
|
||||
'batch':i['batch'],
|
||||
'subproduction_plan':sp,
|
||||
'count':0
|
||||
})
|
||||
wm.count = wm.count + i['count']
|
||||
wm.save()
|
||||
# 更新子计划物料情况
|
||||
spp = SubProductionProgress.objects.get(material=i['material'], subproduction_plan=sp, type=1)
|
||||
spp.count_pick = spp.count_pick + i['count']
|
||||
spp.save()
|
||||
sp.is_picked=True
|
||||
sp.state = 3 #生产中
|
||||
sp.state_date_real = timezone.now() #实际开工日期
|
||||
sp.save()
|
||||
# 更新库存
|
||||
fifo.is_audited = True
|
||||
fifo.save()
|
||||
update_inm(fifo)
|
||||
return fifo
|
||||
|
||||
class WMaterialListSerializer(serializers.ModelSerializer):
|
||||
|
|
|
@ -16,6 +16,7 @@ from apps.wpm.models import WMaterial, WProduct, Operation, OperationMaterial, O
|
|||
|
||||
from apps.wpm.serializers import OperationDetailSerializer, OperationListSerializer, PickSerializer, OperationInitSerializer, OperationSubmitSerializer, WMaterialListSerializer, WProductListSerializer
|
||||
from rest_framework.response import Response
|
||||
from django.db import transaction
|
||||
# Create your views here.
|
||||
class WPlanViewSet(ListModelMixin, GenericViewSet):
|
||||
"""
|
||||
|
@ -131,6 +132,8 @@ class DoFormInit(CreateAPIView, GenericAPIView):
|
|||
class DoFormSubmit(CreateAPIView, GenericAPIView):
|
||||
perms_map={'*':'*'}
|
||||
serializer_class = OperationSubmitSerializer
|
||||
|
||||
@transaction.atomic
|
||||
def post(self, request, format=None):
|
||||
"""
|
||||
提交操作表单
|
||||
|
|
Loading…
Reference in New Issue