def costs(self, request, uuid): offering = self.get_object() customers = structure_models.Customer.objects.all() active_customers = structure_filters.AccountingStartDateFilter( ).filter_queryset(request, customers, self) if self.request.query_params: serializer = core_serializers.DateRangeFilterSerializer( data=self.request.query_params) serializer.is_valid(raise_exception=True) start_year, start_month = serializer.validated_data['start'] end_year, end_month = serializer.validated_data['end'] end = datetime.date(year=end_year, month=end_month, day=1) start = datetime.date(year=start_year, month=start_month, day=1) else: today = datetime.date.today() end = datetime.date(year=today.year, month=today.month, day=1) start = datetime.date(year=today.year - 1, month=today.month, day=1) costs = utils.get_offering_costs(offering, active_customers, start, end) page = self.paginate_queryset(costs) return self.get_paginated_response(page)
def customers(self, request, uuid): offering = self.get_object() customers = structure_models.Customer.objects.all() active_customers = structure_filters.AccountingStartDateFilter( ).filter_queryset(request, customers, self) customer_queryset = utils.get_offering_customers( offering, active_customers) serializer_class = structure_serializers.CustomerSerializer serializer = serializer_class(instance=customer_queryset, many=True, context=self.get_serializer_context()) page = self.paginate_queryset(serializer.data) return self.get_paginated_response(page)
def get(self, request, format=None): if not self.request.user.is_staff and not request.user.is_support: raise exceptions.PermissionDenied() customers = structure_models.Customer.objects.all() customers = structure_filters.AccountingStartDateFilter().filter_queryset(request, customers, self) name = request.query_params.get('name', '') if name: customers = customers.filter(name__icontains=name) year, month = invoice_utils.parse_period(request.query_params) invoices = invoices_models.Invoice.objects.filter(customer__in=customers) invoices = invoices.filter(year=year, month=month) total = sum(invoice.total for invoice in invoices) return response.Response({'total': total}, status=status.HTTP_200_OK)
def get_active_customers(request, view): customers = structure_models.Customer.objects.all() return structure_filters.AccountingStartDateFilter().filter_queryset( request, customers, view )
def growth(self, request): if not self.request.user.is_staff and not request.user.is_support: raise exceptions.PermissionDenied() customers = structure_models.Customer.objects.all() customers = structure_filters.AccountingStartDateFilter().filter_queryset( request, customers, self ) customers_count = 4 if 'customers_count' in request.query_params: try: customers_count = int(request.query_params['customers_count']) except ValueError: raise exceptions.ValidationError('customers_count is not a number') if customers_count > 20: raise exceptions.ValidationError( 'customers_count should not be greater than 20' ) is_accounting_mode = request.query_params.get('accounting_mode') == 'accounting' today = datetime.date.today() current_month = today - relativedelta(months=12) majors = list( models.Invoice.objects.filter( customer__in=customers, created__gte=current_month ) .values('customer_id') .annotate(total=Sum('current_cost')) .order_by('-total') .values_list('customer_id', flat=True)[:customers_count] ) minors = customers.exclude(id__in=majors) customer_periods = {} total_periods = {} other_periods = {} for i in range(13): invoices = models.Invoice.objects.filter( year=current_month.year, month=current_month.month, ) key = f'{current_month.year}-{current_month.month}' row = customer_periods[key] = {} subtotal = 0 for invoice in invoices.filter(customer_id__in=majors): value = is_accounting_mode and invoice.price or invoice.total subtotal += value row[invoice.customer.uuid.hex] = value other_periods[key] = sum( is_accounting_mode and invoice.price or invoice.total for invoice in invoices.filter(customer_id__in=minors) ) total_periods[key] = subtotal + other_periods[key] current_month += relativedelta(months=1) result = { 'periods': total_periods.keys(), 'total_periods': total_periods.values(), 'other_periods': other_periods.values(), 'customer_periods': [ { 'name': customer.name, 'periods': [ customer_periods[period].get(customer.uuid.hex, 0) for period in total_periods.keys() ], } for customer in structure_models.Customer.objects.filter(id__in=majors) ], } return Response(result, status=status.HTTP_200_OK)