forked from uncloud/uncloud
Wire order records to bills, fix user balance
This commit is contained in:
parent
9e253d497b
commit
9e9018060e
3 changed files with 46 additions and 21 deletions
|
@ -8,12 +8,15 @@ from django.utils import timezone
|
|||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from calendar import monthrange
|
||||
|
||||
def sum_amounts(entries):
|
||||
return reduce(lambda acc, entry: acc + entry.amount, entries, 0)
|
||||
|
||||
def get_balance_for(user):
|
||||
bills = sum_amounts(Bill.objects.filter(owner=user))
|
||||
payments = sum_amounts(Payment.objects.filter(owner=user))
|
||||
bills = reduce(
|
||||
lambda acc, entry: acc + entry.total,
|
||||
Bill.objects.filter(owner=user),
|
||||
0)
|
||||
payments = reduce(
|
||||
lambda acc, entry: acc + entry.amount,
|
||||
Payment.objects.filter(owner=user),
|
||||
0)
|
||||
return payments - bills
|
||||
|
||||
def get_payment_method_for(user):
|
||||
|
|
|
@ -33,26 +33,40 @@ class Bill(models.Model):
|
|||
valid = models.BooleanField(default=True)
|
||||
|
||||
@property
|
||||
def entries(self):
|
||||
# TODO: return list of Bill entries, extract from linked order
|
||||
# for each related order
|
||||
# for each product
|
||||
# build BillEntry
|
||||
return []
|
||||
def records(self):
|
||||
bill_records = []
|
||||
orders = Order.objects.filter(bill=self)
|
||||
for order in orders:
|
||||
for order_record in order.records:
|
||||
bill_record = BillRecord(
|
||||
self,
|
||||
order_record.setup_fee,
|
||||
order_record.recurring_price,
|
||||
order_record.recurring_period,
|
||||
order_record.description)
|
||||
bill_records.append(bill_record)
|
||||
|
||||
return bill_records
|
||||
|
||||
@property
|
||||
def total(self):
|
||||
orders = Order.objects.filter(bill=self)
|
||||
return reduce(lambda acc, order: acc + order.amount, orders, 0)
|
||||
return reduce(lambda acc, record: acc + record.amount(), self.records, 0)
|
||||
|
||||
class BillEntry():
|
||||
start_date = timezone.now()
|
||||
end_date = timezone.now()
|
||||
recurring_period = RecurringPeriod.PER_MONTH
|
||||
recurring_price = 0
|
||||
amount = 0
|
||||
description = ""
|
||||
class BillRecord():
|
||||
def __init__(self, bill, setup_fee, recurring_price, recurring_period, description):
|
||||
self.bill = bill
|
||||
self.setup_fee = setup_fee
|
||||
self.recurring_price = recurring_price
|
||||
self.recurring_period = recurring_period
|
||||
self.description = description
|
||||
|
||||
def amount(self):
|
||||
# TODO: Billing logic here!
|
||||
if self.recurring_period == RecurringPeriod.PER_MONTH:
|
||||
return self.recurring_price # TODO
|
||||
else:
|
||||
raise Exception('Unsupported recurring period: {}.'.
|
||||
format(record.recurring_period))
|
||||
|
||||
# /!\ BIG FAT WARNING /!\ #
|
||||
#
|
||||
|
|
|
@ -7,11 +7,19 @@ from functools import reduce
|
|||
from uncloud_vm.serializers import VMProductSerializer
|
||||
from uncloud_vm.models import VMProduct
|
||||
|
||||
# TODO: remove magic numbers for decimal fields
|
||||
class BillRecordSerializer(serializers.Serializer):
|
||||
description = serializers.CharField()
|
||||
recurring_period = serializers.CharField()
|
||||
recurring_price = serializers.DecimalField(max_digits=10, decimal_places=2)
|
||||
amount = serializers.DecimalField(max_digits=10, decimal_places=2)
|
||||
|
||||
class BillSerializer(serializers.ModelSerializer):
|
||||
records = BillRecordSerializer(many=True, read_only=True)
|
||||
class Meta:
|
||||
model = Bill
|
||||
fields = ['owner', 'total', 'due_date', 'creation_date',
|
||||
'starting_date', 'ending_date']
|
||||
'starting_date', 'ending_date', 'records']
|
||||
|
||||
class PaymentSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
|
|
Loading…
Reference in a new issue