forked from uncloud/uncloud
WIP revamped bill logic
This commit is contained in:
parent
af1265003e
commit
e319d1d151
7 changed files with 152 additions and 69 deletions
|
|
@ -2,6 +2,7 @@ from django.db import models
|
|||
from django.contrib.auth import get_user_model
|
||||
from django.core.validators import MinValueValidator
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils import timezone
|
||||
|
||||
import uuid
|
||||
|
||||
|
|
@ -31,16 +32,49 @@ class Bill(models.Model):
|
|||
valid = models.BooleanField(default=True)
|
||||
|
||||
@property
|
||||
def amount(self):
|
||||
orders = Order.objects.filter(bill=self)
|
||||
amount = 0
|
||||
for order in orders:
|
||||
amount += order.recurring_price
|
||||
|
||||
return amount
|
||||
def entries(self):
|
||||
# TODO: return list of Bill entries, extract from linked order
|
||||
# for each related order
|
||||
# for each product
|
||||
# build BillEntry
|
||||
return []
|
||||
|
||||
@property
|
||||
def total(self):
|
||||
#return helpers.sum_amounts(self.entries)
|
||||
pass
|
||||
|
||||
class BillEntry():
|
||||
start_date = timezone.now()
|
||||
end_date = timezone.now()
|
||||
recurring_period = RecurringPeriod.PER_MONTH
|
||||
recurring_price = 0
|
||||
amount = 0
|
||||
description = ""
|
||||
|
||||
|
||||
# /!\ BIG FAT WARNING /!\ #
|
||||
#
|
||||
# Order are assumed IMMUTABLE and used as SOURCE OF TRUST for generating
|
||||
# bills. Do **NOT** mutate then!
|
||||
#
|
||||
# Why? We need to store the state somewhere since product are mutable (e.g.
|
||||
# adding RAM to VM, changing price of 1GB of RAM, ...). An alternative could
|
||||
# have been to only store the state in bills but would have been more
|
||||
# confusing: the order is a 'contract' with the customer, were both parts
|
||||
# agree on deal => That's what we want to keep archived.
|
||||
#
|
||||
# SOON:
|
||||
#
|
||||
# We'll need to add some kind of OrderEntry table (each order might have
|
||||
# multiple entries) storing: recurring_price, recurring_period, setup_fee, description
|
||||
#
|
||||
# FOR NOW:
|
||||
#
|
||||
# We dynamically get pricing from linked product, as they are not updated in
|
||||
# this stage of development.
|
||||
#
|
||||
# /!\ BIG FAT WARNING /!\ #
|
||||
class Order(models.Model):
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
|
|
@ -56,22 +90,11 @@ class Order(models.Model):
|
|||
editable=False,
|
||||
blank=True)
|
||||
|
||||
|
||||
recurring_price = models.DecimalField(
|
||||
max_digits=AMOUNT_MAX_DIGITS,
|
||||
decimal_places=AMOUNT_DECIMALS,
|
||||
validators=[MinValueValidator(0)],
|
||||
editable=False)
|
||||
one_time_price = models.DecimalField(
|
||||
max_digits=AMOUNT_MAX_DIGITS,
|
||||
decimal_places=AMOUNT_DECIMALS,
|
||||
validators=[MinValueValidator(0)],
|
||||
editable=False)
|
||||
|
||||
recurring_period = models.CharField(max_length=32,
|
||||
choices = RecurringPeriod.choices,
|
||||
default = RecurringPeriod.PER_MONTH)
|
||||
|
||||
|
||||
# def amount(self):
|
||||
# amount = recurring_price
|
||||
# if recurring and first_month:
|
||||
|
|
@ -133,9 +156,6 @@ class Payment(models.Model):
|
|||
default='unknown')
|
||||
timestamp = models.DateTimeField(editable=False, auto_now_add=True)
|
||||
|
||||
|
||||
|
||||
|
||||
class Product(models.Model):
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue