sale 创建操作错误
This commit is contained in:
parent
0f65b1ff8a
commit
c451a53500
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 3.2.9 on 2021-12-06 06:15
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('sam', '0007_saleproduct_remark'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='saleproduct',
|
||||
name='number',
|
||||
field=models.CharField(max_length=50, verbose_name='物品编号'),
|
||||
),
|
||||
]
|
|
@ -70,9 +70,6 @@ class Order(CommonAModel):
|
|||
verbose_name = '订单信息'
|
||||
verbose_name_plural = verbose_name
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class Sale(CommonADModel):
|
||||
"""
|
||||
|
@ -90,7 +87,7 @@ class SaleProduct(BaseModel):
|
|||
具体产品
|
||||
"""
|
||||
sale = models.ForeignKey(Sale, verbose_name='关联销售记录', on_delete=models.CASCADE)
|
||||
number = models.CharField('物品编号', unique=True, max_length=50)
|
||||
number = models.CharField('物品编号', max_length=50)
|
||||
iproduct = models.ForeignKey('inm.iproduct', verbose_name='关联库存产品', on_delete=models.CASCADE, related_name='sale_iproduct')
|
||||
is_mtested = models.BooleanField('是否军检', default=False)
|
||||
is_mtestok = models.BooleanField('是否军检合格', default=True)
|
||||
|
|
|
@ -73,20 +73,6 @@ class SaleCreateSerializer(serializers.ModelSerializer):
|
|||
attrs['product'] = order.product
|
||||
return super().validate(attrs)
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, validated_data):
|
||||
iproducts = validated_data.pop('iproducts')
|
||||
validated_data['count'] = len(iproducts)
|
||||
sale = Sale.objects.create(**validated_data)
|
||||
i_l = []
|
||||
for i in iproducts:
|
||||
i_d ={}
|
||||
i_d['sale'] = sale
|
||||
i_d['number'] = i.number
|
||||
i_d['iproduct'] = i
|
||||
i_l.append(SaleProduct(**i_d))
|
||||
SaleProduct.objects.bulk_create(i_l)
|
||||
return sale
|
||||
|
||||
class SaleListSerializer(serializers.ModelSerializer):
|
||||
customer_ = CustomerSimpleSerializer(source='customer', read_only=True)
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from django.db import transaction
|
||||
from rest_framework import exceptions, serializers
|
||||
from rest_framework.mixins import CreateModelMixin, DestroyModelMixin, ListModelMixin, RetrieveModelMixin
|
||||
from apps.inm.models import FIFO
|
||||
from apps.sam.serializers import ContractCreateUpdateSerializer, ContractSerializer, CustomerCreateUpdateSerializer, CustomerSerializer, OrderCreateUpdateSerializer, OrderSerializer, SaleCreateSerializer, SaleListSerializer, SaleProductCreateSerializer, SaleProductListSerializer, SaleProductMtestSerializer
|
||||
from apps.sam.models import Contract, Customer, Order, Sale, SaleProduct
|
||||
from rest_framework.viewsets import GenericViewSet, ModelViewSet
|
||||
|
@ -8,6 +10,7 @@ from django.shortcuts import render
|
|||
from rest_framework.decorators import action
|
||||
from django.db.models import F
|
||||
from rest_framework.response import Response
|
||||
from django.utils import timezone
|
||||
# Create your views here.
|
||||
class CustomerViewSet(CreateUpdateCustomMixin, ModelViewSet):
|
||||
"""
|
||||
|
@ -91,7 +94,27 @@ class SaleViewSet(CreateUpdateCustomMixin, ListModelMixin, RetrieveModelMixin, C
|
|||
return SaleListSerializer
|
||||
return super().get_serializer_class()
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
data = request.data
|
||||
serializer = SaleCreateSerializer(data=data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
vdata = serializer.validated_data
|
||||
with transaction.atomic():
|
||||
iproducts = vdata.pop('iproducts')
|
||||
vdata['count'] = len(iproducts)
|
||||
sale = Sale.objects.create(**vdata)
|
||||
i_l = []
|
||||
for i in iproducts:
|
||||
i_d ={}
|
||||
i_d['sale'] = sale
|
||||
i_d['number'] = i.number
|
||||
i_d['iproduct'] = i
|
||||
i_l.append(SaleProduct(**i_d))
|
||||
SaleProduct.objects.bulk_create(i_l)
|
||||
return Response()
|
||||
|
||||
@action(methods=['post'], detail=True, perms_map={'post':'*'}, serializer_class=serializers.Serializer)
|
||||
@transaction.atomic
|
||||
def audit(self, request, pk=None):
|
||||
"""
|
||||
审核
|
||||
|
@ -100,6 +123,19 @@ class SaleViewSet(CreateUpdateCustomMixin, ListModelMixin, RetrieveModelMixin, C
|
|||
if obj.is_audited:
|
||||
raise exceptions.APIException('已审核通过')
|
||||
# 创建出库记录
|
||||
fifo = FIFO()
|
||||
fifo.type = FIFO.FIFO_TYPE_SALE_OUT
|
||||
fifo.is_audited = True
|
||||
fifo.auditor = request.user
|
||||
fifo.inout_date = timezone.now()
|
||||
fifo.save()
|
||||
# 出库条目
|
||||
spds = SaleProduct.objects.filter(sale=obj)
|
||||
for i in spds:
|
||||
if i.is_mtested and i.is_mtestok:
|
||||
pass
|
||||
else:
|
||||
raise exceptions.APIException('存在未军检产品')
|
||||
# 更新库存
|
||||
return Response()
|
||||
|
||||
|
|
Loading…
Reference in New Issue