Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
OrganizationUUIDField,
PlanUUIDField,
)
from metering_billing.serializers.model_serializers import DraftInvoiceSerializer
from metering_billing.serializers.request_serializers import DraftInvoiceRequestSerializer
from metering_billing.utils import (
calculate_end_date,
convert_to_datetime,
Expand Down Expand Up @@ -302,6 +304,52 @@ def archive(self, request, customer_id=None):
CustomerDeleteResponseSerializer().validate(return_data)
return Response(return_data, status=status.HTTP_200_OK)

@extend_schema(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to get rid of the other method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean the old draft_invoice?

request=DraftInvoiceRequestSerializer,
parameters=[DraftInvoiceRequestSerializer],
responses={
200: inline_serializer(
name="DraftInvoiceResponse",
fields={"invoice": DraftInvoiceSerializer}
)
},
)
@action(detail=True, methods=["get"], permission_classes=[IsAuthenticated | HasUserAPIKey])
def draft_invoice(self, request):
organization = request.organization
serializer = DraftInvoiceRequestSerializer(
data=request.query_params,
context={"organization": organization}
)
serializer.is_valid(raise_exception=True)
customer = serializer.validated_data.get("customer")
sub_records = SubscriptionRecord.objects.active().filter(
organization=organization,
customer=customer
)
response = {"invoice": None}
if sub_records is None or len(sub_records) == 0:
response = {"invoice": []}
else:
sub_records = sub_records.select_related("billing_plan").prefetch_related(
"billing_plan__plan_components",
"billing_plan__plan_components__billable_metric",
"billing_plan__plan_components__tiers",
"billing_plan__pricing_unit",
)
invoices = generate_invoice(
sub_records,
draft=True,
charge_next_plan=serializer.validated_data.get(
"include_next_period", True
),
)
serializer = DraftInvoiceSerializer(invoices, many=True).data
for invoice in invoices:
invoice.delete()
response = {"invoices": serializer or []}
return Response(response, status=status.HTTP_200_OK)

def perform_create(self, serializer):
try:
return serializer.save(organization=self.request.organization)
Expand Down
Loading