diff --git a/uncloud/uncloud_pay/migrations/0014_auto_20200303_1027.py b/uncloud/uncloud_pay/migrations/0014_auto_20200303_1027.py new file mode 100644 index 0000000..05759d1 --- /dev/null +++ b/uncloud/uncloud_pay/migrations/0014_auto_20200303_1027.py @@ -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', + ), + ] diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py index 4e5770a..551b96d 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -129,11 +129,15 @@ class BillRecord(): def __init__(self, bill, order_record): self.bill = bill self.order = order_record.order - self.setup_fee = order_record.setup_fee self.recurring_price = order_record.recurring_price self.recurring_period = order_record.recurring_period 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 def recurring_count(self): # Compute billing delta. @@ -178,17 +182,15 @@ class BillRecord(): elif self.recurring_period == RecurringPeriod.PER_SECOND: seconds = ceil(billed_delta / timedelta(seconds=1)) return Decimal(seconds) + elif self.recurring_period == RecurringPeriod.ONE_TIME: + return Decimal(0) else: raise Exception('Unsupported recurring period: {}.'. format(record.recurring_period)) @property def amount(self): - amount = self.recurring_count * self.recurring_price - if self.order.starting_date > self.bill.starting_date: - amount += self.setup_fee - - return amount + return self.recurring_price * self.recurring_count + self.one_time_price ### # Orders. @@ -231,22 +233,22 @@ class Order(models.Model): return OrderRecord.objects.filter(order=self) @property - def setup_fee(self): - return reduce(lambda acc, record: acc + record.setup_fee, self.records, 0) + def one_time_price(self): + return reduce(lambda acc, record: acc + record.one_time_price, self.records, 0) @property def recurring_price(self): 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, - setup_fee=setup_fee, + one_time_price=one_time_price, recurring_price=recurring_price, description=description) class OrderRecord(models.Model): 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, decimal_places=AMOUNT_DECIMALS, validators=[MinValueValidator(0)]) @@ -303,7 +305,7 @@ class Product(models.Model): pass # To be implemented in child. @property - def setup_fee(self): + def one_time_price(self): return 0 @property diff --git a/uncloud/uncloud_pay/serializers.py b/uncloud/uncloud_pay/serializers.py index 051b882..16b725a 100644 --- a/uncloud/uncloud_pay/serializers.py +++ b/uncloud/uncloud_pay/serializers.py @@ -97,7 +97,7 @@ class BillRecordSerializer(serializers.Serializer): recurring_period = serializers.CharField() recurring_price = 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) class BillSerializer(serializers.ModelSerializer): @@ -113,7 +113,7 @@ class BillSerializer(serializers.ModelSerializer): class OrderRecordSerializer(serializers.ModelSerializer): class Meta: model = OrderRecord - fields = ['setup_fee', 'recurring_price', 'description'] + fields = ['one_time_price', 'recurring_price', 'description'] class OrderSerializer(serializers.ModelSerializer): @@ -121,7 +121,7 @@ class OrderSerializer(serializers.ModelSerializer): class Meta: model = Order 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): vms = VMProductSerializer(many=True, read_only=True) diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index 107f23e..d9a5732 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -49,7 +49,7 @@ class VMProductViewSet(ProductViewSet): # 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? - order.add_record(vm.setup_fee, + order.add_record(vm.one_time_price, vm.recurring_price(order.recurring_period), vm.description) return Response(serializer.data) diff --git a/uncloud/ungleich_service/models.py b/uncloud/ungleich_service/models.py index 8f95973..9d6a8ac 100644 --- a/uncloud/ungleich_service/models.py +++ b/uncloud/ungleich_service/models.py @@ -28,5 +28,5 @@ class MatrixServiceProduct(Product): RecurringPeriod.choices)) @property - def setup_fee(self): + def one_time_price(self): return 30 diff --git a/uncloud/ungleich_service/views.py b/uncloud/ungleich_service/views.py index d5191a2..47c15e2 100644 --- a/uncloud/ungleich_service/views.py +++ b/uncloud/ungleich_service/views.py @@ -41,7 +41,7 @@ class MatrixServiceProductViewSet(ProductViewSet): # XXX: Move this to some kind of on_create hook in parent # Product class? order.add_record( - vm.setup_fee, + vm.one_time_price, vm.recurring_price(order.recurring_period), vm.description) @@ -54,7 +54,7 @@ class MatrixServiceProductViewSet(ProductViewSet): # XXX: Move this to some kind of on_create hook in parent # Product class? order.add_record( - service.setup_fee, + service.one_time_price, service.recurring_price(order.recurring_period), service.description)