feat: 利润走势

This commit is contained in:
Czw996 2021-12-28 01:25:57 +08:00
parent 706f9e83e6
commit 4424e1c3a4
7 changed files with 69 additions and 4 deletions

View file

@ -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',
]

View file

@ -17,7 +17,12 @@ class SalesTrendPermission(ModelPermission):
code = 'sales_trend'
class ProfitTrendPermission(ModelPermission):
code = 'profit_trend'
__all__ = [
'PurchaseReportPermission', 'SalesReportPermission',
'SalesHotGoodsPermission', 'SalesTrendPermission',
'ProfitTrendPermission',
]

View file

@ -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',
]

View file

@ -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

View file

@ -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',
]

View file

@ -0,0 +1,10 @@
# 收支统计
## 功能
- 利润走势
[/api/profit_trends/]{start_date, end_date}
## 其他接口

View file

@ -8,6 +8,7 @@
- 库存报表
- 批次报表
- 收支统计
- 利润走势
- 基础数据
- 客户分类
- 客户