Consistently use one_time_price instead of setup_fee

This commit is contained in:
fnux 2020-03-03 11:27:35 +01:00
parent a40da40169
commit 11e22f5001
6 changed files with 39 additions and 19 deletions

View file

@ -0,0 +1,18 @@
# Generated by Django 3.0.3 on 2020-03-03 10:27
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0013_paymentmethod_stripe_card_id'),
]
operations = [
migrations.RenameField(
model_name='orderrecord',
old_name='setup_fee',
new_name='one_time_price',
),
]

View file

@ -129,11 +129,15 @@ class BillRecord():
def __init__(self, bill, order_record): def __init__(self, bill, order_record):
self.bill = bill self.bill = bill
self.order = order_record.order self.order = order_record.order
self.setup_fee = order_record.setup_fee
self.recurring_price = order_record.recurring_price self.recurring_price = order_record.recurring_price
self.recurring_period = order_record.recurring_period self.recurring_period = order_record.recurring_period
self.description = order_record.description self.description = order_record.description
if self.order.starting_date > self.bill.starting_date:
self.one_time_price = one_time_price
else:
self.one_time_price = 0
@property @property
def recurring_count(self): def recurring_count(self):
# Compute billing delta. # Compute billing delta.
@ -178,17 +182,15 @@ class BillRecord():
elif self.recurring_period == RecurringPeriod.PER_SECOND: elif self.recurring_period == RecurringPeriod.PER_SECOND:
seconds = ceil(billed_delta / timedelta(seconds=1)) seconds = ceil(billed_delta / timedelta(seconds=1))
return Decimal(seconds) return Decimal(seconds)
elif self.recurring_period == RecurringPeriod.ONE_TIME:
return Decimal(0)
else: else:
raise Exception('Unsupported recurring period: {}.'. raise Exception('Unsupported recurring period: {}.'.
format(record.recurring_period)) format(record.recurring_period))
@property @property
def amount(self): def amount(self):
amount = self.recurring_count * self.recurring_price return self.recurring_price * self.recurring_count + self.one_time_price
if self.order.starting_date > self.bill.starting_date:
amount += self.setup_fee
return amount
### ###
# Orders. # Orders.
@ -231,22 +233,22 @@ class Order(models.Model):
return OrderRecord.objects.filter(order=self) return OrderRecord.objects.filter(order=self)
@property @property
def setup_fee(self): def one_time_price(self):
return reduce(lambda acc, record: acc + record.setup_fee, self.records, 0) return reduce(lambda acc, record: acc + record.one_time_price, self.records, 0)
@property @property
def recurring_price(self): def recurring_price(self):
return reduce(lambda acc, record: acc + record.recurring_price, self.records, 0) return reduce(lambda acc, record: acc + record.recurring_price, self.records, 0)
def add_record(self, setup_fee, recurring_price, description): def add_record(self, one_time_price, recurring_price, description):
OrderRecord.objects.create(order=self, OrderRecord.objects.create(order=self,
setup_fee=setup_fee, one_time_price=one_time_price,
recurring_price=recurring_price, recurring_price=recurring_price,
description=description) description=description)
class OrderRecord(models.Model): class OrderRecord(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE) order = models.ForeignKey(Order, on_delete=models.CASCADE)
setup_fee = models.DecimalField(default=0.0, one_time_price = models.DecimalField(default=0.0,
max_digits=AMOUNT_MAX_DIGITS, max_digits=AMOUNT_MAX_DIGITS,
decimal_places=AMOUNT_DECIMALS, decimal_places=AMOUNT_DECIMALS,
validators=[MinValueValidator(0)]) validators=[MinValueValidator(0)])
@ -303,7 +305,7 @@ class Product(models.Model):
pass # To be implemented in child. pass # To be implemented in child.
@property @property
def setup_fee(self): def one_time_price(self):
return 0 return 0
@property @property

View file

@ -97,7 +97,7 @@ class BillRecordSerializer(serializers.Serializer):
recurring_period = serializers.CharField() recurring_period = serializers.CharField()
recurring_price = serializers.DecimalField(max_digits=10, decimal_places=2) recurring_price = serializers.DecimalField(max_digits=10, decimal_places=2)
recurring_count = serializers.DecimalField(max_digits=10, decimal_places=2) recurring_count = serializers.DecimalField(max_digits=10, decimal_places=2)
setup_fee = serializers.DecimalField(max_digits=10, decimal_places=2) one_time_price = serializers.DecimalField(max_digits=10, decimal_places=2)
amount = serializers.DecimalField(max_digits=10, decimal_places=2) amount = serializers.DecimalField(max_digits=10, decimal_places=2)
class BillSerializer(serializers.ModelSerializer): class BillSerializer(serializers.ModelSerializer):
@ -113,7 +113,7 @@ class BillSerializer(serializers.ModelSerializer):
class OrderRecordSerializer(serializers.ModelSerializer): class OrderRecordSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = OrderRecord model = OrderRecord
fields = ['setup_fee', 'recurring_price', 'description'] fields = ['one_time_price', 'recurring_price', 'description']
class OrderSerializer(serializers.ModelSerializer): class OrderSerializer(serializers.ModelSerializer):
@ -121,7 +121,7 @@ class OrderSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Order model = Order
fields = ['uuid', 'creation_date', 'starting_date', 'ending_date', fields = ['uuid', 'creation_date', 'starting_date', 'ending_date',
'bill', 'recurring_period', 'records', 'recurring_price', 'setup_fee'] 'bill', 'recurring_period', 'records', 'recurring_price', 'one_time_price']
class ProductSerializer(serializers.Serializer): class ProductSerializer(serializers.Serializer):
vms = VMProductSerializer(many=True, read_only=True) vms = VMProductSerializer(many=True, read_only=True)

View file

@ -49,7 +49,7 @@ class VMProductViewSet(ProductViewSet):
# Add Product record to order (VM is mutable, allows to keep history in order). # Add Product record to order (VM is mutable, allows to keep history in order).
# XXX: Move this to some kind of on_create hook in parent Product class? # XXX: Move this to some kind of on_create hook in parent Product class?
order.add_record(vm.setup_fee, order.add_record(vm.one_time_price,
vm.recurring_price(order.recurring_period), vm.description) vm.recurring_price(order.recurring_period), vm.description)
return Response(serializer.data) return Response(serializer.data)

View file

@ -28,5 +28,5 @@ class MatrixServiceProduct(Product):
RecurringPeriod.choices)) RecurringPeriod.choices))
@property @property
def setup_fee(self): def one_time_price(self):
return 30 return 30

View file

@ -41,7 +41,7 @@ class MatrixServiceProductViewSet(ProductViewSet):
# XXX: Move this to some kind of on_create hook in parent # XXX: Move this to some kind of on_create hook in parent
# Product class? # Product class?
order.add_record( order.add_record(
vm.setup_fee, vm.one_time_price,
vm.recurring_price(order.recurring_period), vm.recurring_price(order.recurring_period),
vm.description) vm.description)
@ -54,7 +54,7 @@ class MatrixServiceProductViewSet(ProductViewSet):
# XXX: Move this to some kind of on_create hook in parent # XXX: Move this to some kind of on_create hook in parent
# Product class? # Product class?
order.add_record( order.add_record(
service.setup_fee, service.one_time_price,
service.recurring_price(order.recurring_period), service.recurring_price(order.recurring_period),
service.description) service.description)