Coverage for invoices/views.py: 67%
65 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-05-22 11:02 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-05-22 11:02 -0600
1from rest_framework.decorators import action
2from memberships.mixins import MemberMixin
3from rest_framework import permissions
4from rest_framework.mixins import (
5 CreateModelMixin as Create,
6 ListModelMixin as List,
7 UpdateModelMixin as Update,
8 DestroyModelMixin as Delete,
9 RetrieveModelMixin as Detail,
10)
11from rest_framework.response import Response
12from rest_framework.viewsets import GenericViewSet
13from rest_framework.validators import ValidationError
14from organizations.mixins import OrganizationAdminMixin
15from app.facturapi import FacturapiOrganizationClient
16from .filters import InvoiceFilter
17from .models import Invoice, FiscalRegime, InvoiceUse
18from .serializers import (
19 FiscalRegimeSerializer,
20 InvoiceUseSerializer,
21 ProductInvoiceConfigurationSerializer,
22 MembershipInvoiceConfigurationSerializer,
23 InvoiceSerializer,
24 InvoiceCancellationSerializer,
25 InvoiceExportSerializer,
26)
29class FiscalRegimeViewSet(GenericViewSet, List):
30 """
31 ViewSet for FiscalRegime
32 """
34 serializer_class = FiscalRegimeSerializer
35 permission_classes = [permissions.AllowAny]
36 queryset = FiscalRegime.objects.all()
39class InvoiceUseViewSet(GenericViewSet, List):
40 """
41 ViewSet for InvoiceUse
42 """
44 serializer_class = InvoiceUseSerializer
45 permission_classes = [permissions.AllowAny]
46 queryset = InvoiceUse.objects.all()
49class MembershipInvoiceConfigurationViewSet(MemberMixin, GenericViewSet, Create, List, Update, Delete):
50 """
51 ViewSet for MembershipInvoiceConfiguration
52 """
54 serializer_class = MembershipInvoiceConfigurationSerializer
57class ProductInvoiceConfigurationViewSet(OrganizationAdminMixin, GenericViewSet, Create, List, Detail, Update, Delete):
58 """
59 ViewSet for ProductInvoiceConfiguration
60 """
62 serializer_class = ProductInvoiceConfigurationSerializer
63 filterset_fields = ["for_membership", "for_event", "for_stock", "product_key"]
64 search_fields = ["description"]
67class InvoiceViewSet(OrganizationAdminMixin, GenericViewSet, Create, List, Detail):
68 """
69 ViewSet for Invoice
70 """
72 serializer_class = InvoiceSerializer
73 filterset_class = InvoiceFilter
75 @action(detail=True, methods=["DELETE"])
76 def cancel(self, request, *args, **kwargs):
77 invoice = self.get_object()
79 serializer = InvoiceCancellationSerializer(data=request.data)
80 serializer.is_valid(True)
82 facturapi = FacturapiOrganizationClient(invoice.organization.facturapi_secret_key)
84 try:
85 facturapi_invoice = facturapi.Invoice.cancel(
86 invoice.facturapi_invoice_id, serializer.data.get("cancellation_motive")
87 )
88 invoice_status = facturapi_invoice.get("status")
89 if invoice_status == "canceled":
90 invoice.status = Invoice.Status.CANCELED
91 invoice.cancellation_status = Invoice.CancellationStatus.ACCEPTED
92 invoice.cancellation_motive = serializer.data.get("cancellation_motive")
93 invoice.save()
94 elif invoice_status == "valid":
95 invoice.cancellation_status = Invoice.CancellationStatus.PENDING
96 invoice.save()
97 except Exception as e:
98 raise ValidationError(e)
100 return Response(self.serializer_class(invoice).data)
102 @action(detail=True, methods=["POST"])
103 def send_email(self, request, *args, **kwargs):
104 invoice = self.get_object()
106 try:
107 invoice.send_invoice_email(request.data.get("emails"))
108 except Exception as e:
109 raise ValidationError(e)
111 return Response()
114class MemberInvoiceViewSet(MemberMixin, GenericViewSet, List, Detail):
115 """
116 ViewSet for Member Invoice
117 """
119 serializer_class = InvoiceSerializer
120 filterset_class = InvoiceFilter
123class PublicInvoiceViewSet(GenericViewSet, Create):
124 """
125 Public ViewSet for Invoice without access token
126 """
128 serializer_class = InvoiceSerializer
129 permission_classes = [permissions.AllowAny]
132class InvoiceExportViewSet(OrganizationAdminMixin, GenericViewSet, Create):
133 """
134 ViewSet for InvoiceExport
135 """
137 serializer_class = InvoiceExportSerializer