2020-02-27 11:38:04 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-02-27 10:59:28 +00:00
|
|
|
from rest_framework import serializers
|
2020-02-27 14:15:12 +00:00
|
|
|
from .models import *
|
2020-03-02 21:26:40 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
###
|
|
|
|
# Payments and Payment Methods.
|
2020-02-27 10:59:28 +00:00
|
|
|
|
|
|
|
class PaymentSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Payment
|
2020-03-05 10:13:04 +00:00
|
|
|
fields = '__all__'
|
2020-02-27 11:38:04 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
class PaymentMethodSerializer(serializers.ModelSerializer):
|
2020-03-03 15:55:56 +00:00
|
|
|
stripe_card_last4 = serializers.IntegerField()
|
2020-03-03 08:13:04 +00:00
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
class Meta:
|
|
|
|
model = PaymentMethod
|
2020-03-05 09:23:34 +00:00
|
|
|
fields = ['uuid', 'source', 'description', 'primary', 'stripe_card_last4', 'active']
|
2020-03-03 17:16:25 +00:00
|
|
|
|
2020-03-05 10:28:43 +00:00
|
|
|
class UpdatePaymentMethodSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = PaymentMethod
|
|
|
|
fields = ['description', 'primary']
|
|
|
|
|
2020-03-03 17:16:25 +00:00
|
|
|
class ChargePaymentMethodSerializer(serializers.Serializer):
|
|
|
|
amount = serializers.DecimalField(max_digits=10, decimal_places=2)
|
2020-03-03 08:13:04 +00:00
|
|
|
|
2020-03-02 21:26:40 +00:00
|
|
|
class CreatePaymentMethodSerializer(serializers.ModelSerializer):
|
2020-03-05 09:23:34 +00:00
|
|
|
please_visit = serializers.CharField(read_only=True)
|
2020-03-02 21:26:40 +00:00
|
|
|
class Meta:
|
|
|
|
model = PaymentMethod
|
2020-03-05 09:23:34 +00:00
|
|
|
fields = ['source', 'description', 'primary', 'please_visit']
|
2020-03-02 21:26:40 +00:00
|
|
|
|
2020-03-06 08:32:25 +00:00
|
|
|
###
|
|
|
|
# Orders & Products.
|
|
|
|
|
|
|
|
class OrderRecordSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = OrderRecord
|
|
|
|
fields = ['one_time_price', 'recurring_price', 'description']
|
|
|
|
|
|
|
|
|
|
|
|
class OrderSerializer(serializers.ModelSerializer):
|
|
|
|
records = OrderRecordSerializer(many=True, read_only=True)
|
|
|
|
class Meta:
|
|
|
|
model = Order
|
|
|
|
fields = ['uuid', 'creation_date', 'starting_date', 'ending_date',
|
|
|
|
'bill', 'recurring_period', 'records', 'recurring_price', 'one_time_price']
|
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
###
|
|
|
|
# Bills
|
|
|
|
|
|
|
|
# TODO: remove magic numbers for decimal fields
|
|
|
|
class BillRecordSerializer(serializers.Serializer):
|
2020-03-06 08:32:25 +00:00
|
|
|
order = serializers.HyperlinkedRelatedField(
|
|
|
|
view_name='order-detail',
|
|
|
|
read_only=True)
|
2020-04-18 08:43:23 +00:00
|
|
|
description = serializers.CharField()
|
|
|
|
one_time_price = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
|
|
|
recurring_price = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
|
|
|
recurring_period = serializers.ChoiceField(choices=RecurringPeriod.choices)
|
|
|
|
recurring_count = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
|
|
|
vat = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
|
|
|
amount = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
2020-03-03 08:13:04 +00:00
|
|
|
|
2020-04-18 06:30:45 +00:00
|
|
|
class BillingAddressSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BillingAddress
|
|
|
|
fields = ['uuid', 'name', 'street', 'city', 'postal_code', 'country', 'vat_number']
|
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
class BillSerializer(serializers.ModelSerializer):
|
2020-04-18 06:30:45 +00:00
|
|
|
billing_address = BillingAddressSerializer(read_only=True)
|
2020-03-03 08:13:04 +00:00
|
|
|
records = BillRecordSerializer(many=True, read_only=True)
|
2020-04-15 14:01:31 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
class Meta:
|
|
|
|
model = Bill
|
2020-03-06 08:39:41 +00:00
|
|
|
fields = ['reference', 'owner', 'total', 'due_date', 'creation_date',
|
2020-04-18 06:30:45 +00:00
|
|
|
'starting_date', 'ending_date', 'records', 'final', 'billing_address']
|
2020-04-15 13:17:38 +00:00
|
|
|
|
|
|
|
# We do not want users to mutate the country / VAT number of an address, as it
|
|
|
|
# will change VAT on existing bills.
|
|
|
|
class UpdateBillingAddressSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BillingAddress
|
|
|
|
fields = ['uuid', 'street', 'city', 'postal_code']
|