From 4424e1c3a4cdc2a163dd93c179626786794242d4 Mon Sep 17 00:00:00 2001 From: Czw996 <459749926@qq.com> Date: Tue, 28 Dec 2021 01:25:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=A9=E6=B6=A6=E8=B5=B0=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/statistic/filters.py | 11 +++++++++- apps/statistic/permissions.py | 5 +++++ apps/statistic/schemas.py | 16 ++++++++++++++- apps/statistic/urls.py | 3 ++- apps/statistic/views.py | 27 ++++++++++++++++++++++++- documents/项目文档/报表统计/利润走势.md | 10 +++++++++ documents/项目文档/菜单.md | 1 + 7 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 documents/项目文档/报表统计/利润走势.md diff --git a/apps/statistic/filters.py b/apps/statistic/filters.py index 36302ce..5103ea5 100644 --- a/apps/statistic/filters.py +++ b/apps/statistic/filters.py @@ -44,7 +44,16 @@ class SalesTrendFilter(FilterSet): fields = ['start_date', 'end_date'] +class ProfitTrendFilter(FilterSet): + start_date = DateFilter(field_name='sales_order__create_time', required=True, lookup_expr='gte', label='开始日期') + end_date = DateFilter(field_name='sales_order__create_time', required=True, lookup_expr='lt', label='结束日期') + + class Meta: + model = SalesGoods + fields = ['start_date', 'end_date'] + + __all__ = [ 'PurchaseReportFilter', 'SalesReportFilter', - 'SalesHotGoodsFilter', 'SalesTrendFilter', + 'SalesHotGoodsFilter', 'SalesTrendFilter', 'ProfitTrendFilter', ] diff --git a/apps/statistic/permissions.py b/apps/statistic/permissions.py index 0930aa5..bd4a8e2 100644 --- a/apps/statistic/permissions.py +++ b/apps/statistic/permissions.py @@ -17,7 +17,12 @@ class SalesTrendPermission(ModelPermission): code = 'sales_trend' +class ProfitTrendPermission(ModelPermission): + code = 'profit_trend' + + __all__ = [ 'PurchaseReportPermission', 'SalesReportPermission', 'SalesHotGoodsPermission', 'SalesTrendPermission', + 'ProfitTrendPermission', ] diff --git a/apps/statistic/schemas.py b/apps/statistic/schemas.py index 4df8eef..e363324 100644 --- a/apps/statistic/schemas.py +++ b/apps/statistic/schemas.py @@ -82,11 +82,25 @@ class SalesTrendResponse(Serializer): warehouse_number = CharField(label='商品编号') warehouse_name = CharField(label='商品名称') total_sales_amount = AmountField(label='销售总金额') - date = DateField(label='商品ID') + date = DateField(label='日期') + + +class ProfitTrendParameter(Serializer): + start_date = DateField(required=True, label='开始日期') + end_date = DateField(required=True, label='结束日期') + + +class ProfitTrendResponse(Serializer): + warehouse = IntegerField(label='仓库ID') + warehouse_number = CharField(label='商品编号') + warehouse_name = CharField(label='商品名称') + total_profit_amount = AmountField(label='销售总利润') + date = DateField(label='日期') __all__ = [ 'PurchaseReportParameter', 'PurchaseReportStatisticResponse', 'PurchaseReportGroupByGoodsResponse', 'SalesReportParameter', 'SalesReportStatisticResponse', 'SalesReportGroupByGoodsResponse', 'SalesHotGoodsParameter', 'SalesHotGoodsResponse', 'SalesTrendParameter', 'SalesTrendResponse', + 'ProfitTrendParameter', 'ProfitTrendResponse', ] diff --git a/apps/statistic/urls.py b/apps/statistic/urls.py index 36dff08..30b699e 100644 --- a/apps/statistic/urls.py +++ b/apps/statistic/urls.py @@ -6,5 +6,6 @@ router = BaseRouter() router.register('purchase_reports', PurchaseReportViewSet, 'purchase_report') router.register('sales_reports', SalesReportViewSet, 'sales_report') router.register('sales_hot_goods', SalesHotGoodsViewSet, 'sales_hot_goods') -router.register('sales_trends', SalesTrendViewSet, 'sales_trends') +router.register('sales_trends', SalesTrendViewSet, 'sales_trend') +router.register('profit_trends', ProfitTrendViewSet, 'profit_trend') urlpatterns = router.urls diff --git a/apps/statistic/views.py b/apps/statistic/views.py index e266374..69d896f 100644 --- a/apps/statistic/views.py +++ b/apps/statistic/views.py @@ -176,7 +176,32 @@ class SalesTrendViewSet(BaseViewSet, ListModelMixin): return Response(data=queryset, status=status.HTTP_200_OK) +class ProfitTrendViewSet(BaseViewSet, ListModelMixin): + """利润走势""" + + permission_classes = [IsAuthenticated, ProfitTrendPermission] + pagination_class = None + filterset_class = ProfitTrendFilter + queryset = SalesGoods.objects.all() + + @extend_schema(parameters=[ProfitTrendParameter], responses={200: ProfitTrendResponse}) + def list(self, request, *args, **kwargs): + queryset = self.filter_queryset(self.get_queryset()) + queryset = queryset.select_related('sales_order__warehouse') + queryset = queryset.extra(select={'date': connection.ops.date_trunc_sql('day', 'create_time')}) + queryset = queryset.values('sales_order__warehouse', 'date').annotate( + warehouse=F('sales_order__warehouse'), + warehouse_number=F('sales_order__warehouse__number'), + warehouse_name=F('sales_order__warehouse__name'), + total_profit_amount=Coalesce(Sum( + F('total_amount') - F('sales_quantity') * F('goods__purchase_price'), + output_field=AmountField()), Value(0, output_field=AmountField())), + ).values('warehouse', 'warehouse_number', 'warehouse_name', 'total_profit_amount', 'date') + + return Response(data=queryset, status=status.HTTP_200_OK) + + __all__ = [ 'PurchaseReportViewSet', 'SalesReportViewSet', - 'SalesHotGoodsViewSet', 'SalesTrendViewSet', + 'SalesHotGoodsViewSet', 'SalesTrendViewSet', 'ProfitTrendViewSet', ] diff --git a/documents/项目文档/报表统计/利润走势.md b/documents/项目文档/报表统计/利润走势.md new file mode 100644 index 0000000..fd4a47a --- /dev/null +++ b/documents/项目文档/报表统计/利润走势.md @@ -0,0 +1,10 @@ +# 收支统计 + + +## 功能 + +- 利润走势 +[/api/profit_trends/]{start_date, end_date} + + +## 其他接口 diff --git a/documents/项目文档/菜单.md b/documents/项目文档/菜单.md index 4a51b27..4b0c1e1 100644 --- a/documents/项目文档/菜单.md +++ b/documents/项目文档/菜单.md @@ -8,6 +8,7 @@ - 库存报表 - 批次报表 - 收支统计 + - 利润走势 - 基础数据 - 客户分类 - 客户