2021-09-01 19:29:24 +07:00
|
|
|
from djmoney.contrib.django_rest_framework import MoneyField
|
2022-11-21 22:12:27 +07:00
|
|
|
from djmoney.settings import DECIMAL_PLACES
|
2021-08-31 11:11:51 +07:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2022-11-21 22:06:57 +07:00
|
|
|
from api import custom_validator
|
2023-02-10 07:05:06 +00:00
|
|
|
from core.models import Invoice, BillingProject, Notification, Balance, BalanceTransaction
|
2021-10-29 13:00:33 +07:00
|
|
|
from core.component import component
|
2021-08-31 11:11:51 +07:00
|
|
|
|
|
|
|
|
2021-10-29 13:00:33 +07:00
|
|
|
class InvoiceComponentSerializer(serializers.ModelSerializer):
|
2021-09-01 19:29:24 +07:00
|
|
|
adjusted_end_date = serializers.DateTimeField()
|
2023-09-26 22:17:13 +07:00
|
|
|
price_charged = MoneyField(max_digits=256, decimal_places=DECIMAL_PLACES)
|
2021-09-01 19:29:24 +07:00
|
|
|
price_charged_currency = serializers.CharField(source="price_charged.currency")
|
|
|
|
|
2021-08-31 11:11:51 +07:00
|
|
|
|
2021-10-29 13:00:33 +07:00
|
|
|
def generate_invoice_component_serializer(model):
|
|
|
|
"""
|
|
|
|
Generate Invoice Component Serializer for particular model
|
|
|
|
:param model: The invoice component model
|
|
|
|
:return: serializer for particular model
|
|
|
|
"""
|
|
|
|
name = type(model).__name__
|
|
|
|
meta_params = {
|
|
|
|
"model": model,
|
|
|
|
"fields": "__all__"
|
|
|
|
}
|
|
|
|
meta_class = type("Meta", (object,), meta_params)
|
|
|
|
serializer_class = type(f"{name}Serializer", (InvoiceComponentSerializer,), {"Meta": meta_class})
|
2021-08-31 11:11:51 +07:00
|
|
|
|
2021-10-29 13:00:33 +07:00
|
|
|
return serializer_class
|
2021-08-31 11:11:51 +07:00
|
|
|
|
|
|
|
|
|
|
|
class InvoiceSerializer(serializers.ModelSerializer):
|
2023-09-26 22:17:13 +07:00
|
|
|
subtotal = MoneyField(max_digits=256, decimal_places=DECIMAL_PLACES)
|
2021-09-01 19:29:24 +07:00
|
|
|
subtotal_currency = serializers.CharField(source="subtotal.currency")
|
2023-09-26 22:17:13 +07:00
|
|
|
total = MoneyField(max_digits=256, decimal_places=DECIMAL_PLACES)
|
2022-04-01 16:38:59 +07:00
|
|
|
total_currency = serializers.CharField(source="total.currency", required=False)
|
2021-08-31 11:11:51 +07:00
|
|
|
|
2021-10-29 13:00:33 +07:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
for field, model in component.INVOICE_COMPONENT_MODEL.items():
|
|
|
|
self.fields[field] = generate_invoice_component_serializer(model)(many=True)
|
|
|
|
|
2021-08-31 11:11:51 +07:00
|
|
|
class Meta:
|
|
|
|
model = Invoice
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleInvoiceSerializer(serializers.ModelSerializer):
|
2023-09-26 22:17:13 +07:00
|
|
|
subtotal = MoneyField(max_digits=256, decimal_places=DECIMAL_PLACES)
|
2022-02-14 16:11:54 +07:00
|
|
|
subtotal_currency = serializers.CharField(source="subtotal.currency")
|
2023-09-26 22:17:13 +07:00
|
|
|
total = MoneyField(max_digits=256, decimal_places=DECIMAL_PLACES)
|
2022-04-01 16:38:59 +07:00
|
|
|
total_currency = serializers.CharField(source="total.currency", required=False)
|
2022-02-14 16:11:54 +07:00
|
|
|
|
2021-08-31 11:11:51 +07:00
|
|
|
class Meta:
|
|
|
|
model = Invoice
|
2022-04-01 16:38:59 +07:00
|
|
|
fields = ['id', 'start_date', 'end_date', 'state', 'tax', 'subtotal', 'subtotal_currency', 'total',
|
|
|
|
'total_currency']
|
2022-06-22 07:34:41 +07:00
|
|
|
|
|
|
|
|
|
|
|
class BillingProjectSerializer(serializers.ModelSerializer):
|
|
|
|
tenant_id = serializers.CharField(required=False, read_only=True)
|
2022-11-21 22:06:57 +07:00
|
|
|
email_notification = serializers.CharField(required=False, validators=[custom_validator.email_list])
|
2022-06-22 07:34:41 +07:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = BillingProject
|
|
|
|
fields = ['tenant_id', 'email_notification']
|
2022-07-08 07:43:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NotificationSerializer(serializers.ModelSerializer):
|
|
|
|
project = BillingProjectSerializer()
|
2022-07-20 16:17:26 +07:00
|
|
|
recipient = serializers.CharField()
|
2022-07-08 07:43:26 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Notification
|
2022-11-21 22:06:57 +07:00
|
|
|
fields = ['id', 'project', 'title', 'short_description', 'content', 'sent_status', 'is_read', 'created_at',
|
|
|
|
'recipient']
|
2023-02-10 07:05:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BalanceSerializer(serializers.ModelSerializer):
|
|
|
|
project = BillingProjectSerializer()
|
2023-09-26 22:17:13 +07:00
|
|
|
amount = MoneyField(max_digits=256, decimal_places=DECIMAL_PLACES)
|
2023-02-10 07:05:06 +00:00
|
|
|
amount_currency = serializers.CharField(source="amount.currency")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Balance
|
|
|
|
fields = ['id', 'project', 'amount', 'amount_currency']
|
|
|
|
|
|
|
|
|
|
|
|
class BalanceTransactionSerializer(serializers.ModelSerializer):
|
2023-09-26 22:17:13 +07:00
|
|
|
amount = MoneyField(max_digits=256, decimal_places=DECIMAL_PLACES)
|
2023-02-10 07:05:06 +00:00
|
|
|
amount_currency = serializers.CharField(source="amount.currency")
|
|
|
|
action = serializers.CharField(required=False)
|
|
|
|
description = serializers.CharField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = BalanceTransaction
|
|
|
|
fields = ['id', 'amount', 'amount_currency', 'action', 'description', 'created_at']
|
|
|
|
|
|
|
|
|