forked from uncloud/uncloud
Add recurring_count to bills
This commit is contained in:
parent
2eaaad49db
commit
a40da40169
2 changed files with 12 additions and 7 deletions
|
@ -8,6 +8,7 @@ from math import ceil
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from calendar import monthrange
|
from calendar import monthrange
|
||||||
|
|
||||||
|
from decimal import Decimal
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
# Define DecimalField properties, used to represent amounts of money.
|
# Define DecimalField properties, used to represent amounts of money.
|
||||||
|
@ -113,7 +114,7 @@ class Bill(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def total(self):
|
def total(self):
|
||||||
return reduce(lambda acc, record: acc + record.amount(), self.records, 0)
|
return reduce(lambda acc, record: acc + record.amount, self.records, 0)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def final(self):
|
def final(self):
|
||||||
|
@ -133,7 +134,8 @@ class BillRecord():
|
||||||
self.recurring_period = order_record.recurring_period
|
self.recurring_period = order_record.recurring_period
|
||||||
self.description = order_record.description
|
self.description = order_record.description
|
||||||
|
|
||||||
def amount(self):
|
@property
|
||||||
|
def recurring_count(self):
|
||||||
# Compute billing delta.
|
# Compute billing delta.
|
||||||
billed_until = self.bill.ending_date
|
billed_until = self.bill.ending_date
|
||||||
if self.order.ending_date != None and self.order.ending_date < self.order.ending_date:
|
if self.order.ending_date != None and self.order.ending_date < self.order.ending_date:
|
||||||
|
@ -166,21 +168,23 @@ class BillRecord():
|
||||||
(_, days_in_month) = monthrange(
|
(_, days_in_month) = monthrange(
|
||||||
self.bill.starting_date.year,
|
self.bill.starting_date.year,
|
||||||
self.bill.starting_date.month)
|
self.bill.starting_date.month)
|
||||||
adjusted_recurring_price = self.recurring_price / days_in_month
|
return Decimal(days / days_in_month)
|
||||||
amount = adjusted_recurring_price * days
|
|
||||||
elif self.recurring_period == RecurringPeriod.PER_DAY:
|
elif self.recurring_period == RecurringPeriod.PER_DAY:
|
||||||
days = ceil(billed_delta / timedelta(days=1))
|
days = ceil(billed_delta / timedelta(days=1))
|
||||||
amount = self.recurring_price * days
|
return Decimal(days)
|
||||||
elif self.recurring_period == RecurringPeriod.PER_HOUR:
|
elif self.recurring_period == RecurringPeriod.PER_HOUR:
|
||||||
hours = ceil(billed_delta / timedelta(hours=1))
|
hours = ceil(billed_delta / timedelta(hours=1))
|
||||||
amount = self.recurring_price * hours
|
return Decimal(hours)
|
||||||
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))
|
||||||
amount = self.recurring_price * seconds
|
return Decimal(seconds)
|
||||||
else:
|
else:
|
||||||
raise Exception('Unsupported recurring period: {}.'.
|
raise Exception('Unsupported recurring period: {}.'.
|
||||||
format(record.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:
|
if self.order.starting_date > self.bill.starting_date:
|
||||||
amount += self.setup_fee
|
amount += self.setup_fee
|
||||||
|
|
||||||
|
|
|
@ -96,6 +96,7 @@ class BillRecordSerializer(serializers.Serializer):
|
||||||
description = serializers.CharField()
|
description = serializers.CharField()
|
||||||
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)
|
||||||
setup_fee = serializers.DecimalField(max_digits=10, decimal_places=2)
|
setup_fee = 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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue