HimoolERP/apps/statistic/views.py

139 lines
6 KiB
Python
Raw Normal View History

2021-12-16 17:54:31 +08:00
from extensions.common.schema import *
from extensions.common.base import *
2021-11-04 23:49:56 +08:00
from extensions.permissions import *
from extensions.exceptions import *
from extensions.viewsets import *
2021-12-26 23:00:08 +08:00
from extensions.models import *
from apps.statistic.serializers import *
from apps.statistic.permissions import *
from apps.statistic.filters import *
from apps.statistic.schemas import *
from apps.statistic.models import *
from apps.purchase.models import *
2021-12-26 23:24:52 +08:00
from apps.sales.models import *
2021-11-04 17:35:34 +08:00
2021-11-04 23:49:56 +08:00
2021-12-26 23:00:08 +08:00
class PurchaseReportViewSet(BaseViewSet):
2021-12-26 14:27:13 +08:00
"""采购报表"""
2021-12-26 23:00:08 +08:00
permission_classes = [IsAuthenticated, PurchaseReportPermission]
filterset_class = PurchaseReportFilter
search_fields = ['goods__number', 'goods__name']
queryset = PurchaseGoods.objects.all()
@extend_schema(parameters=[PurchaseReportParameter],
responses={200: PurchaseReportStatisticResponse})
@action(detail=False, methods=['get'])
def statistics(self, request, *args, **kwargs):
2021-12-26 14:27:13 +08:00
"""统计"""
2021-12-26 23:00:08 +08:00
queryset = self.filter_queryset(self.get_queryset())
result = queryset.aggregate(
total_count=Count('purchase_order', distinct=True),
total_quantity=Coalesce(Sum('purchase_quantity'), Value(0.0)),
total_amount=Coalesce(Sum('total_amount'), Value(0, output_field=AmountField()))
)
return Response(data=result, status=status.HTTP_200_OK)
@extend_schema(parameters=[PurchaseReportParameter],
responses={200: PurchaseReportDetialSerializer})
@action(detail=False, methods=['get'])
def detials(self, request, *args, **kwargs):
2021-12-26 14:27:13 +08:00
"""明细"""
2021-12-26 23:00:08 +08:00
queryset = self.filter_queryset(self.get_queryset())
queryset = queryset.select_related('goods', 'goods__category', 'goods__unit', 'purchase_order',
2021-12-26 23:24:52 +08:00
'purchase_order__warehouse', 'purchase_order__supplier',
'purchase_order__creator')
2021-12-26 23:00:08 +08:00
queryset = self.paginate_queryset(queryset)
serializer = PurchaseReportDetialSerializer(instance=queryset, many=True)
return self.get_paginated_response(serializer.data)
@extend_schema(parameters=[PurchaseReportParameter],
responses={200: PurchaseReportGroupByGoodsResponse})
@action(detail=False, methods=['get'])
def group_by_goods(self, request, *args, **kwargs):
2021-12-26 14:27:13 +08:00
"""商品汇总"""
2021-12-26 23:00:08 +08:00
queryset = self.filter_queryset(self.get_queryset())
queryset = queryset.select_related('goods', 'goods__category', 'goods__unit')
queryset = queryset.values('goods').annotate(
goods_number=F('goods__number'), goods_name=F('goods__name'),
goods_barcode=F('goods__barcode'), goods_spec=F('goods__spec'),
category_name=F('goods__category__name'), unit_name=F('goods__unit__name'),
total_purchase_quantity=Coalesce(Sum('purchase_quantity'), Value(0.0)),
total_purchase_amount=Coalesce(Sum('total_amount'), Value(0, output_field=AmountField())),
min_purchase_price=Min('purchase_price'), avg_purchase_price=Avg('purchase_price'),
max_purchase_price=Max('purchase_price')
)
queryset = self.paginate_queryset(queryset)
2021-12-26 14:27:13 +08:00
2021-12-26 23:00:08 +08:00
return self.get_paginated_response(queryset)
2021-11-04 23:49:56 +08:00
2021-12-26 23:00:08 +08:00
2021-12-26 23:24:52 +08:00
class SalesReportViewSet(BaseViewSet):
"""销售报表"""
permission_classes = [IsAuthenticated, SalesReportPermission]
filterset_class = SalesReportFilter
search_fields = ['goods__number', 'goods__name']
queryset = SalesGoods.objects.all()
@extend_schema(parameters=[SalesReportParameter],
responses={200: SalesReportStatisticResponse})
@action(detail=False, methods=['get'])
def statistics(self, request, *args, **kwargs):
"""统计"""
queryset = self.filter_queryset(self.get_queryset())
result = queryset.aggregate(
total_count=Count('sales_order', distinct=True),
total_quantity=Coalesce(Sum('sales_quantity'), Value(0.0)),
total_amount=Coalesce(Sum('total_amount'), Value(0, output_field=AmountField()))
)
return Response(data=result, status=status.HTTP_200_OK)
@extend_schema(parameters=[SalesReportParameter],
responses={200: SalesReportDetialSerializer})
@action(detail=False, methods=['get'])
def detials(self, request, *args, **kwargs):
"""明细"""
queryset = self.filter_queryset(self.get_queryset())
queryset = queryset.select_related('goods', 'goods__category', 'goods__unit', 'sales_order',
'sales_order__warehouse', 'sales_order__supplier',
'sales_order__creator')
queryset = self.paginate_queryset(queryset)
serializer = SalesReportDetialSerializer(instance=queryset, many=True)
return self.get_paginated_response(serializer.data)
@extend_schema(parameters=[SalesReportParameter],
responses={200: SalesReportGroupByGoodsResponse})
@action(detail=False, methods=['get'])
def group_by_goods(self, request, *args, **kwargs):
"""商品汇总"""
queryset = self.filter_queryset(self.get_queryset())
queryset = queryset.select_related('goods', 'goods__category', 'goods__unit')
queryset = queryset.values('goods').annotate(
goods_number=F('goods__number'), goods_name=F('goods__name'),
goods_barcode=F('goods__barcode'), goods_spec=F('goods__spec'),
category_name=F('goods__category__name'), unit_name=F('goods__unit__name'),
total_sales_quantity=Coalesce(Sum('sales_quantity'), Value(0.0)),
total_sales_amount=Coalesce(Sum('total_amount'), Value(0, output_field=AmountField())),
min_sales_price=Min('sales_price'), avg_sales_price=Avg('sales_price'),
max_sales_price=Max('sales_price')
)
queryset = self.paginate_queryset(queryset)
return self.get_paginated_response(queryset)
2021-12-26 23:00:08 +08:00
__all__ = [
2021-12-26 23:24:52 +08:00
'PurchaseReportViewSet', 'SalesReportViewSet',
2021-11-04 23:49:56 +08:00
]