Coverage for accounting/serializers.py: 85%
52 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-04-12 12:57 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-04-12 12:57 -0600
1from rest_framework import serializers
2from rest_polymorphic.serializers import PolymorphicSerializer
3from invoices.models import Invoice
4from invoices.serializers import InvoiceConfigurationSerializer, InvoiceProductConfigurationSerializer
5from memberships.models import Membership
6from memberships.serializers import BasicMembershipSerializer
7from products.serializers import BasicProductPolymorphicSerializer
8from .models import Movement, ProductCharge
11class MovementSerializer(serializers.ModelSerializer):
12 """
13 Serializer for Charge base model
14 """
16 class Meta:
17 model = Movement
18 fields = ["membership", "date", "amount", "concept", "balance", "type"]
21class MovementsPolymorphicSerializer(PolymorphicSerializer):
22 """
23 Polymorphic serializer for Charge objects and sub objects
24 """
26 model_serializer_mapping = {
27 Movement: MovementSerializer,
28 }
31class InvoiceSerializerForProductCharge(serializers.ModelSerializer):
32 """
33 Serializer for Invoice on ProductChargeSerializer
34 """
36 invoice_configuration = InvoiceConfigurationSerializer()
37 product_configurations = InvoiceProductConfigurationSerializer(many=True)
39 class Meta:
40 model = Invoice
41 read_only_fields = ["random_slug", "pdf", "xml", "sat_uuid", "status"]
42 fields = read_only_fields + ["membership", "invoice_configuration", "product_configurations", "use"]
45class ProductChargeSerializer(serializers.ModelSerializer):
46 """
47 Serializer for ProductCharge
48 """
50 is_invoicable = serializers.SerializerMethodField()
51 invoices = InvoiceSerializerForProductCharge(read_only=True, many=True)
52 product = BasicProductPolymorphicSerializer(read_only=True)
54 class Meta:
55 model = ProductCharge
56 read_only_fields = ["random_slug", "balance"]
57 fields = read_only_fields + [
58 "membership",
59 "product",
60 "date",
61 "amount",
62 "concept",
63 "paid_amount",
64 "invoiced_amount",
65 "is_invoicable",
66 "invoices",
67 "payment_status",
68 ]
70 def get_is_invoicable(self, instance):
71 return instance.paid_amount > 0 and instance.invoiced_amount < instance.paid_amount
73 def to_representation(self, instance):
74 representation = super(ProductChargeSerializer, self).to_representation(instance)
75 membership_slug = representation.get("membership")
76 membership = Membership.objects.get(random_slug=membership_slug)
77 serializer = BasicMembershipSerializer(instance=membership)
78 representation["membership"] = serializer.data
79 return representation
82class DetailProductChargeSerializer(ProductChargeSerializer):
83 """
84 Detail serializer for ProductCharge
85 """
87 uses = serializers.SerializerMethodField()
89 class Meta(ProductChargeSerializer.Meta):
90 fields = ProductChargeSerializer.Meta.fields + ["uses"]
92 def get_uses(self, instance):
93 try:
94 return instance.product.invoice_configuration.uses.values("random_slug", "description", "use_key")
95 except Exception:
96 return None
99class AnnualProductChargeSerializer(serializers.Serializer):
100 """
101 Serializer for annual data on ProductCharge
102 """
104 month = serializers.IntegerField()
105 amount = serializers.DecimalField(max_digits=12, decimal_places=2)
106 paid_amount = serializers.DecimalField(max_digits=12, decimal_places=2)
107 invoiced_amount = serializers.DecimalField(max_digits=12, decimal_places=2)