Wire order records to bills, fix user balance

This commit is contained in:
fnux 2020-03-02 10:46:04 +01:00
parent 9e253d497b
commit 9e9018060e
3 changed files with 46 additions and 21 deletions

View file

@ -8,12 +8,15 @@ from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from calendar import monthrange from calendar import monthrange
def sum_amounts(entries):
return reduce(lambda acc, entry: acc + entry.amount, entries, 0)
def get_balance_for(user): def get_balance_for(user):
bills = sum_amounts(Bill.objects.filter(owner=user)) bills = reduce(
payments = sum_amounts(Payment.objects.filter(owner=user)) 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 return payments - bills
def get_payment_method_for(user): def get_payment_method_for(user):

View file

@ -33,26 +33,40 @@ class Bill(models.Model):
valid = models.BooleanField(default=True) valid = models.BooleanField(default=True)
@property @property
def entries(self): def records(self):
# TODO: return list of Bill entries, extract from linked order bill_records = []
# for each related order orders = Order.objects.filter(bill=self)
# for each product for order in orders:
# build BillEntry for order_record in order.records:
return [] 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 @property
def total(self): def total(self):
orders = Order.objects.filter(bill=self) return reduce(lambda acc, record: acc + record.amount(), self.records, 0)
return reduce(lambda acc, order: acc + order.amount, orders, 0)
class BillEntry(): class BillRecord():
start_date = timezone.now() def __init__(self, bill, setup_fee, recurring_price, recurring_period, description):
end_date = timezone.now() self.bill = bill
recurring_period = RecurringPeriod.PER_MONTH self.setup_fee = setup_fee
recurring_price = 0 self.recurring_price = recurring_price
amount = 0 self.recurring_period = recurring_period
description = "" 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 /!\ # # /!\ BIG FAT WARNING /!\ #
# #

View file

@ -7,11 +7,19 @@ from functools import reduce
from uncloud_vm.serializers import VMProductSerializer from uncloud_vm.serializers import VMProductSerializer
from uncloud_vm.models import VMProduct 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): class BillSerializer(serializers.ModelSerializer):
records = BillRecordSerializer(many=True, read_only=True)
class Meta: class Meta:
model = Bill model = Bill
fields = ['owner', 'total', 'due_date', 'creation_date', fields = ['owner', 'total', 'due_date', 'creation_date',
'starting_date', 'ending_date'] 'starting_date', 'ending_date', 'records']
class PaymentSerializer(serializers.ModelSerializer): class PaymentSerializer(serializers.ModelSerializer):
class Meta: class Meta: