2020-02-27 10:21:38 +00:00
|
|
|
from django.db import models
|
2020-03-04 08:39:18 +00:00
|
|
|
from django.db.models import Q
|
2020-02-27 10:21:38 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-02-27 19:29:50 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-05-08 08:56:03 +00:00
|
|
|
from django.core.validators import MinValueValidator
|
2020-02-29 08:08:30 +00:00
|
|
|
from django.utils import timezone
|
2020-05-01 22:16:29 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist, ValidationError
|
2020-03-04 08:39:18 +00:00
|
|
|
|
2020-03-09 10:30:31 +00:00
|
|
|
import logging
|
2020-03-04 08:39:18 +00:00
|
|
|
from functools import reduce
|
2020-04-18 09:21:11 +00:00
|
|
|
import itertools
|
2020-03-03 07:53:19 +00:00
|
|
|
from math import ceil
|
2020-06-21 14:08:00 +00:00
|
|
|
import datetime
|
2020-03-03 07:53:19 +00:00
|
|
|
from calendar import monthrange
|
2020-03-18 13:53:26 +00:00
|
|
|
from decimal import Decimal
|
|
|
|
|
2020-03-03 15:55:56 +00:00
|
|
|
import uncloud_pay.stripe
|
2020-03-04 08:39:18 +00:00
|
|
|
from uncloud_pay.helpers import beginning_of_month, end_of_month
|
2020-05-08 08:56:03 +00:00
|
|
|
from uncloud_pay import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS, COUNTRIES
|
2020-03-22 19:55:11 +00:00
|
|
|
from uncloud.models import UncloudModel, UncloudStatus
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-03-03 10:15:48 +00:00
|
|
|
from decimal import Decimal
|
2020-03-09 10:30:31 +00:00
|
|
|
import decimal
|
2020-03-04 09:55:12 +00:00
|
|
|
|
|
|
|
# Used to generate bill due dates.
|
2020-06-21 14:08:00 +00:00
|
|
|
BILL_PAYMENT_DELAY=datetime.timedelta(days=10)
|
2020-03-04 09:55:12 +00:00
|
|
|
|
2020-03-09 10:30:31 +00:00
|
|
|
# Initialize logger.
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-06-21 14:08:00 +00:00
|
|
|
def start_of_month(a_day):
|
|
|
|
""" Returns first of the month of a given datetime object"""
|
|
|
|
return a_day.replace(day=1,hour=0,minute=0,second=0, microsecond=0)
|
|
|
|
|
|
|
|
def end_of_month(a_day):
|
|
|
|
""" Returns first of the month of a given datetime object"""
|
|
|
|
|
|
|
|
_, last_day = monthrange(a_day.year, a_day.month)
|
|
|
|
return a_day.replace(day=last_day,hour=23,minute=59,second=59, microsecond=0)
|
|
|
|
|
|
|
|
def start_of_this_month():
|
|
|
|
""" Returns first of this month"""
|
|
|
|
a_day = timezone.now()
|
|
|
|
return a_day.replace(day=1,hour=0,minute=0,second=0, microsecond=0)
|
|
|
|
|
|
|
|
def end_of_this_month():
|
|
|
|
""" Returns first of this month"""
|
|
|
|
a_day = timezone.now()
|
|
|
|
|
|
|
|
_, last_day = monthrange(a_day.year, a_day.month)
|
|
|
|
return a_day.replace(day=last_day,hour=23,minute=59,second=59, microsecond=0)
|
|
|
|
|
|
|
|
def default_payment_delay():
|
|
|
|
return timezone.now() + BILL_PAYMENT_DELAY
|
|
|
|
|
2020-02-27 19:29:50 +00:00
|
|
|
# See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types
|
2020-05-23 21:32:45 +00:00
|
|
|
class RecurringPeriod(models.IntegerChoices):
|
2020-05-24 11:45:03 +00:00
|
|
|
"""
|
|
|
|
We don't support months are years, because the vary in length.
|
|
|
|
This is not only complicated, but also unfair to the user, as the user pays the same
|
|
|
|
amount for different durations.
|
|
|
|
"""
|
2020-05-23 21:32:45 +00:00
|
|
|
PER_365D = 365*24*3600, _('Per 365 days')
|
|
|
|
PER_30D = 30*24*3600, _('Per 30 days')
|
|
|
|
PER_WEEK = 7*24*3600, _('Per Week')
|
|
|
|
PER_DAY = 24*3600, _('Per Day')
|
|
|
|
PER_HOUR = 3600, _('Per Hour')
|
|
|
|
PER_MINUTE = 60, _('Per Minute')
|
|
|
|
PER_SECOND = 1, _('Per Second')
|
|
|
|
ONE_TIME = 0, _('Onetime')
|
2020-05-23 21:08:59 +00:00
|
|
|
|
2020-04-15 13:17:38 +00:00
|
|
|
class CountryField(models.CharField):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs.setdefault('choices', COUNTRIES)
|
|
|
|
kwargs.setdefault('default', 'CH')
|
|
|
|
kwargs.setdefault('max_length', 2)
|
|
|
|
|
2020-05-24 11:45:03 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2020-03-04 08:39:18 +00:00
|
|
|
|
2020-04-15 13:17:38 +00:00
|
|
|
def get_internal_type(self):
|
|
|
|
return "CharField"
|
2020-03-04 08:39:18 +00:00
|
|
|
|
2020-03-18 13:36:40 +00:00
|
|
|
def get_balance_for_user(user):
|
2020-03-04 08:39:18 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
class StripeCustomer(models.Model):
|
|
|
|
owner = models.OneToOneField( get_user_model(),
|
|
|
|
primary_key=True,
|
|
|
|
on_delete=models.CASCADE)
|
|
|
|
stripe_id = models.CharField(max_length=32)
|
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
###
|
|
|
|
# Payments and Payment Methods.
|
|
|
|
|
|
|
|
class Payment(models.Model):
|
|
|
|
owner = models.ForeignKey(get_user_model(),
|
|
|
|
on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
amount = models.DecimalField(
|
|
|
|
default=0.0,
|
|
|
|
max_digits=AMOUNT_MAX_DIGITS,
|
|
|
|
decimal_places=AMOUNT_DECIMALS,
|
|
|
|
validators=[MinValueValidator(0)])
|
|
|
|
|
|
|
|
source = models.CharField(max_length=256,
|
|
|
|
choices = (
|
|
|
|
('wire', 'Wire Transfer'),
|
|
|
|
('stripe', 'Stripe'),
|
|
|
|
('voucher', 'Voucher'),
|
|
|
|
('referral', 'Referral'),
|
|
|
|
('unknown', 'Unknown')
|
|
|
|
),
|
|
|
|
default='unknown')
|
|
|
|
timestamp = models.DateTimeField(editable=False, auto_now_add=True)
|
|
|
|
|
2020-03-10 08:14:52 +00:00
|
|
|
# We override save() in order to active products awaiting payment.
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
# _state.adding is switched to false after super(...) call.
|
|
|
|
being_created = self._state.adding
|
2020-03-04 09:55:12 +00:00
|
|
|
|
2020-03-10 08:14:52 +00:00
|
|
|
unpaid_bills_before_payment = Bill.get_unpaid_for(self.owner)
|
|
|
|
super(Payment, self).save(*args, **kwargs) # Save payment in DB.
|
|
|
|
unpaid_bills_after_payment = Bill.get_unpaid_for(self.owner)
|
2020-03-04 09:55:12 +00:00
|
|
|
|
2020-03-10 08:14:52 +00:00
|
|
|
newly_paid_bills = list(
|
|
|
|
set(unpaid_bills_before_payment) - set(unpaid_bills_after_payment))
|
|
|
|
for bill in newly_paid_bills:
|
|
|
|
bill.activate_products()
|
2020-03-04 09:55:12 +00:00
|
|
|
|
2020-05-23 21:08:59 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
class PaymentMethod(models.Model):
|
|
|
|
owner = models.ForeignKey(get_user_model(),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
editable=False)
|
|
|
|
source = models.CharField(max_length=256,
|
|
|
|
choices = (
|
|
|
|
('stripe', 'Stripe'),
|
|
|
|
('unknown', 'Unknown'),
|
|
|
|
),
|
|
|
|
default='stripe')
|
|
|
|
description = models.TextField()
|
2020-03-05 14:19:25 +00:00
|
|
|
primary = models.BooleanField(default=False, editable=False)
|
2020-03-03 08:13:04 +00:00
|
|
|
|
|
|
|
# Only used for "Stripe" source
|
2020-03-05 09:23:34 +00:00
|
|
|
stripe_payment_method_id = models.CharField(max_length=32, blank=True, null=True)
|
|
|
|
stripe_setup_intent_id = models.CharField(max_length=32, blank=True, null=True)
|
2020-03-03 08:13:04 +00:00
|
|
|
|
2020-03-03 15:55:56 +00:00
|
|
|
@property
|
|
|
|
def stripe_card_last4(self):
|
2020-03-05 09:23:34 +00:00
|
|
|
if self.source == 'stripe' and self.active:
|
|
|
|
payment_method = uncloud_pay.stripe.get_payment_method(
|
|
|
|
self.stripe_payment_method_id)
|
|
|
|
return payment_method.card.last4
|
2020-03-03 15:55:56 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2020-03-05 09:23:34 +00:00
|
|
|
@property
|
|
|
|
def active(self):
|
|
|
|
if self.source == 'stripe' and self.stripe_payment_method_id != None:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2020-03-03 15:55:56 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
def charge(self, amount):
|
2020-03-05 10:03:47 +00:00
|
|
|
if not self.active:
|
|
|
|
raise Exception('This payment method is inactive.')
|
|
|
|
|
2020-03-05 10:13:04 +00:00
|
|
|
if amount < 0: # Make sure we don't charge negative amount by errors...
|
2020-03-05 10:03:47 +00:00
|
|
|
raise Exception('Cannot charge negative amount.')
|
|
|
|
|
|
|
|
if self.source == 'stripe':
|
|
|
|
stripe_customer = StripeCustomer.objects.get(owner=self.owner).stripe_id
|
|
|
|
stripe_payment = uncloud_pay.stripe.charge_customer(
|
|
|
|
amount, stripe_customer, self.stripe_payment_method_id)
|
2020-03-05 10:13:04 +00:00
|
|
|
if 'paid' in stripe_payment and stripe_payment['paid'] == False:
|
|
|
|
raise Exception(stripe_payment['error'])
|
|
|
|
else:
|
|
|
|
payment = Payment.objects.create(
|
|
|
|
owner=self.owner, source=self.source, amount=amount)
|
2020-03-05 10:03:47 +00:00
|
|
|
|
|
|
|
return payment
|
2020-03-03 08:13:04 +00:00
|
|
|
else:
|
2020-03-05 10:03:47 +00:00
|
|
|
raise Exception('This payment method is unsupported/cannot be charged.')
|
2020-03-03 08:13:04 +00:00
|
|
|
|
2020-03-05 14:19:25 +00:00
|
|
|
def set_as_primary_for(self, user):
|
|
|
|
methods = PaymentMethod.objects.filter(owner=user, primary=True)
|
|
|
|
for method in methods:
|
|
|
|
print(method)
|
|
|
|
method.primary = False
|
|
|
|
method.save()
|
|
|
|
|
|
|
|
self.primary = True
|
|
|
|
self.save()
|
2020-03-04 08:39:18 +00:00
|
|
|
|
|
|
|
def get_primary_for(user):
|
|
|
|
methods = PaymentMethod.objects.filter(owner=user)
|
|
|
|
for method in methods:
|
|
|
|
# Do we want to do something with non-primary method?
|
2020-03-05 10:03:47 +00:00
|
|
|
if method.active and method.primary:
|
2020-03-04 08:39:18 +00:00
|
|
|
return method
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
class Meta:
|
2020-03-05 10:03:47 +00:00
|
|
|
# TODO: limit to one primary method per user.
|
|
|
|
# unique_together is no good since it won't allow more than one
|
|
|
|
# non-primary method.
|
2020-03-05 09:23:34 +00:00
|
|
|
pass
|
2020-03-03 08:13:04 +00:00
|
|
|
|
|
|
|
###
|
2020-03-05 09:27:33 +00:00
|
|
|
# Bills.
|
2020-03-03 08:13:04 +00:00
|
|
|
|
2020-04-18 06:30:45 +00:00
|
|
|
class BillingAddress(models.Model):
|
|
|
|
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
|
|
|
|
2020-05-07 13:38:49 +00:00
|
|
|
organization = models.CharField(max_length=100)
|
2020-04-18 06:30:45 +00:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
street = models.CharField(max_length=100)
|
|
|
|
city = models.CharField(max_length=50)
|
|
|
|
postal_code = models.CharField(max_length=50)
|
|
|
|
country = CountryField(blank=True)
|
|
|
|
vat_number = models.CharField(max_length=100, default="", blank=True)
|
2020-06-21 12:35:12 +00:00
|
|
|
active = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(fields=['owner'],
|
2020-06-21 12:45:05 +00:00
|
|
|
condition=Q(active=True),
|
2020-06-21 12:35:12 +00:00
|
|
|
name='one_active_billing_address_per_user')
|
|
|
|
]
|
2020-04-18 06:30:45 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_addresses_for(user):
|
|
|
|
return BillingAddress.objects.filter(owner=user)
|
|
|
|
|
2020-05-02 19:20:14 +00:00
|
|
|
@classmethod
|
|
|
|
def get_preferred_address_for(cls, user):
|
|
|
|
addresses = cls.get_addresses_for(user)
|
2020-04-18 06:30:45 +00:00
|
|
|
if len(addresses) == 0:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
# TODO: allow user to set primary/preferred address
|
|
|
|
return addresses[0]
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-06-21 11:46:54 +00:00
|
|
|
return "{} - {}, {}, {} {}, {}".format(
|
|
|
|
self.owner,
|
|
|
|
self.name, self.street, self.postal_code, self.city,
|
|
|
|
self.country)
|
|
|
|
|
|
|
|
###
|
|
|
|
# VAT
|
2020-04-18 06:30:45 +00:00
|
|
|
|
2020-04-18 09:43:55 +00:00
|
|
|
class VATRate(models.Model):
|
|
|
|
start_date = models.DateField(blank=True, null=True)
|
|
|
|
stop_date = models.DateField(blank=True, null=True)
|
|
|
|
territory_codes = models.TextField(blank=True, default='')
|
|
|
|
currency_code = models.CharField(max_length=10)
|
|
|
|
rate = models.FloatField()
|
|
|
|
rate_type = models.TextField(blank=True, default='')
|
|
|
|
description = models.TextField(blank=True, default='')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_for_country(country_code):
|
|
|
|
vat_rate = None
|
|
|
|
try:
|
|
|
|
vat_rate = VATRate.objects.get(
|
|
|
|
territory_codes=country_code, start_date__isnull=False, stop_date=None
|
|
|
|
)
|
|
|
|
return vat_rate.rate
|
|
|
|
except VATRate.DoesNotExist as dne:
|
|
|
|
logger.debug(str(dne))
|
|
|
|
logger.debug("Did not find VAT rate for %s, returning 0" % country_code)
|
|
|
|
return 0
|
2020-04-18 06:30:45 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
###
|
|
|
|
# Orders.
|
|
|
|
|
2020-02-27 10:21:38 +00:00
|
|
|
class Order(models.Model):
|
2020-05-24 11:45:03 +00:00
|
|
|
"""
|
|
|
|
Order are assumed IMMUTABLE and used as SOURCE OF TRUST for generating
|
|
|
|
bills. Do **NOT** mutate then!
|
|
|
|
"""
|
|
|
|
|
2020-02-27 10:21:38 +00:00
|
|
|
owner = models.ForeignKey(get_user_model(),
|
|
|
|
on_delete=models.CASCADE,
|
2020-06-21 12:35:12 +00:00
|
|
|
editable=True)
|
|
|
|
|
|
|
|
billing_address = models.ForeignKey(BillingAddress,
|
|
|
|
on_delete=models.CASCADE)
|
2020-05-07 10:08:18 +00:00
|
|
|
description = models.TextField()
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-05-02 20:48:05 +00:00
|
|
|
# TODO: enforce ending_date - starting_date to be larger than recurring_period.
|
|
|
|
creation_date = models.DateTimeField(auto_now_add=True)
|
|
|
|
starting_date = models.DateTimeField(default=timezone.now)
|
|
|
|
ending_date = models.DateTimeField(blank=True,
|
|
|
|
null=True)
|
|
|
|
|
2020-05-24 11:45:03 +00:00
|
|
|
recurring_period = models.IntegerField(choices = RecurringPeriod.choices,
|
|
|
|
default = RecurringPeriod.PER_30D)
|
2020-05-02 20:48:05 +00:00
|
|
|
|
|
|
|
one_time_price = models.DecimalField(default=0.0,
|
|
|
|
max_digits=AMOUNT_MAX_DIGITS,
|
|
|
|
decimal_places=AMOUNT_DECIMALS,
|
|
|
|
validators=[MinValueValidator(0)])
|
|
|
|
|
|
|
|
recurring_price = models.DecimalField(default=0.0,
|
|
|
|
max_digits=AMOUNT_MAX_DIGITS,
|
|
|
|
decimal_places=AMOUNT_DECIMALS,
|
|
|
|
validators=[MinValueValidator(0)])
|
|
|
|
|
|
|
|
replaced_by = models.ForeignKey('self',
|
2020-05-08 14:47:32 +00:00
|
|
|
related_name='supersede',
|
2020-05-02 20:48:05 +00:00
|
|
|
on_delete=models.PROTECT,
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
2020-05-08 14:47:32 +00:00
|
|
|
|
|
|
|
depends_on = models.ForeignKey('self',
|
|
|
|
related_name='parent_of',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
|
|
|
|
2020-06-21 12:35:12 +00:00
|
|
|
@property
|
|
|
|
def count_billed(self):
|
|
|
|
"""
|
|
|
|
How many times this order was billed so far.
|
|
|
|
This logic is mainly thought to be for recurring bills, but also works for one time bills
|
|
|
|
"""
|
|
|
|
|
2020-06-21 14:42:55 +00:00
|
|
|
return sum([ br.quantity for br in self.bill_records.all() ])
|
2020-06-21 12:35:12 +00:00
|
|
|
|
|
|
|
|
2020-05-23 19:32:56 +00:00
|
|
|
def active_before(self, ending_date):
|
|
|
|
# Was this order started before the specified ending date?
|
|
|
|
if self.starting_date <= ending_date:
|
|
|
|
if self.ending_date:
|
|
|
|
if self.ending_date > ending_date:
|
2020-05-23 21:08:59 +00:00
|
|
|
pass
|
2020-05-23 19:32:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_recurring(self):
|
|
|
|
return not self.recurring_period == RecurringPeriod.ONE_TIME
|
2020-05-08 07:31:46 +00:00
|
|
|
@property
|
|
|
|
def is_terminated(self):
|
|
|
|
return self.ending_date != None and self.ending_date < timezone.now()
|
|
|
|
|
2020-05-23 19:32:56 +00:00
|
|
|
def is_terminated_at(self, a_date):
|
|
|
|
return self.ending_date != None and self.ending_date < timezone.now()
|
|
|
|
|
2020-05-08 07:31:46 +00:00
|
|
|
def terminate(self):
|
|
|
|
if not self.is_terminated:
|
|
|
|
self.ending_date = timezone.now()
|
|
|
|
self.save()
|
2020-05-02 20:48:05 +00:00
|
|
|
|
|
|
|
# Trigger initial bill generation at order creation.
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.ending_date and self.ending_date < self.starting_date:
|
|
|
|
raise ValidationError("End date cannot be before starting date")
|
|
|
|
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2020-05-08 09:33:59 +00:00
|
|
|
def generate_initial_bill(self):
|
|
|
|
return Bill.generate_for(self.starting_date.year, self.starting_date.month, self.owner)
|
2020-05-02 20:48:05 +00:00
|
|
|
|
2020-05-08 09:33:59 +00:00
|
|
|
# Used by uncloud_pay tests.
|
|
|
|
@property
|
|
|
|
def bills(self):
|
|
|
|
return Bill.objects.filter(order=self)
|
2020-05-02 20:48:05 +00:00
|
|
|
|
2020-04-18 11:51:31 +00:00
|
|
|
@staticmethod
|
|
|
|
def from_product(product, **kwargs):
|
|
|
|
# FIXME: this is only a workaround.
|
|
|
|
billing_address = BillingAddress.get_preferred_address_for(product.owner)
|
|
|
|
if billing_address == None:
|
|
|
|
raise Exception("Owner does not have a billing address!")
|
|
|
|
|
|
|
|
return Order(description=product.description,
|
|
|
|
one_time_price=product.one_time_price,
|
|
|
|
recurring_price=product.recurring_price,
|
|
|
|
billing_address=billing_address,
|
|
|
|
owner=product.owner,
|
|
|
|
**kwargs)
|
|
|
|
|
2020-05-02 20:48:05 +00:00
|
|
|
|
2020-03-02 08:25:03 +00:00
|
|
|
@property
|
|
|
|
def records(self):
|
|
|
|
return OrderRecord.objects.filter(order=self)
|
|
|
|
|
|
|
|
@property
|
2020-03-03 10:27:35 +00:00
|
|
|
def one_time_price(self):
|
|
|
|
return reduce(lambda acc, record: acc + record.one_time_price, self.records, 0)
|
2020-02-29 08:08:30 +00:00
|
|
|
|
2020-03-01 14:47:27 +00:00
|
|
|
@property
|
2020-03-02 08:25:03 +00:00
|
|
|
def recurring_price(self):
|
|
|
|
return reduce(lambda acc, record: acc + record.recurring_price, self.records, 0)
|
|
|
|
|
2020-04-13 09:54:41 +00:00
|
|
|
# Used by uncloud_pay tests.
|
2020-04-17 07:15:52 +00:00
|
|
|
@property
|
|
|
|
def bills(self):
|
|
|
|
return Bill.objects.filter(order=self)
|
|
|
|
|
2020-04-13 09:54:41 +00:00
|
|
|
def add_record(self, one_time_price, recurring_price, description):
|
|
|
|
OrderRecord.objects.create(order=self,
|
|
|
|
one_time_price=one_time_price,
|
|
|
|
recurring_price=recurring_price,
|
|
|
|
description=description)
|
|
|
|
|
2020-05-02 20:03:34 +00:00
|
|
|
def __str__(self):
|
2020-06-21 14:08:00 +00:00
|
|
|
return f"Order {self.owner}-{self.id}"
|
2020-05-02 20:48:05 +00:00
|
|
|
|
2020-06-21 14:08:00 +00:00
|
|
|
# return "{} created at {}, {}->{}, recurring period {}. One time price {}, recurring price {}".format(
|
|
|
|
# self.id, self.creation_date,
|
|
|
|
# self.starting_date, self.ending_date,
|
|
|
|
# self.recurring_period,
|
|
|
|
# self.one_time_price,
|
|
|
|
# self.recurring_price)
|
2020-04-13 09:54:41 +00:00
|
|
|
|
2020-06-21 11:46:54 +00:00
|
|
|
|
2020-06-21 14:08:00 +00:00
|
|
|
class Bill(models.Model):
|
2020-06-21 11:46:54 +00:00
|
|
|
owner = models.ForeignKey(get_user_model(),
|
|
|
|
on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
creation_date = models.DateTimeField(auto_now_add=True)
|
2020-08-01 12:05:50 +00:00
|
|
|
|
2020-06-21 14:08:00 +00:00
|
|
|
# FIXME: this is a race condition, if ending_date is evaluated
|
|
|
|
# in the next month the bill spawns two months!
|
|
|
|
starting_date = models.DateTimeField(default=start_of_this_month)
|
|
|
|
ending_date = models.DateTimeField(default=end_of_this_month)
|
|
|
|
due_date = models.DateField(default=default_payment_delay)
|
2020-06-21 11:46:54 +00:00
|
|
|
|
|
|
|
valid = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
# Mapping to BillRecords
|
|
|
|
# https://stackoverflow.com/questions/4443190/djangos-manytomany-relationship-with-additional-fields
|
|
|
|
bill_records = models.ManyToManyField(Order, through="BillRecord")
|
|
|
|
|
2020-06-21 14:08:00 +00:00
|
|
|
class Meta:
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(fields=['owner',
|
|
|
|
'starting_date',
|
|
|
|
'ending_date' ],
|
|
|
|
name='one_bill_per_month_per_user')
|
|
|
|
]
|
|
|
|
|
2020-06-21 11:46:54 +00:00
|
|
|
# billing address and vat rate is the same for the whole bill
|
|
|
|
# @property
|
|
|
|
# def vat_rate(self):
|
|
|
|
# return Decimal(VATRate.get_for_country(self.bill.billing_address.country))
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-06-21 14:08:00 +00:00
|
|
|
return f"Bill {self.owner}-{self.id}"
|
2020-06-21 11:46:54 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_all_bills(cls):
|
|
|
|
for owner in get_user_model().objects.all():
|
|
|
|
# mintime = time of first order
|
|
|
|
# maxtime = time of last order
|
|
|
|
# iterate month based through it
|
|
|
|
|
|
|
|
cls.assign_orders_to_bill(owner, year, month)
|
|
|
|
pass
|
|
|
|
|
|
|
|
def assign_orders_to_bill(self, owner, year, month):
|
|
|
|
"""
|
|
|
|
Generate a bill for the specific month of a user.
|
|
|
|
|
|
|
|
First handle all one time orders
|
|
|
|
|
|
|
|
FIXME:
|
|
|
|
|
|
|
|
- limit this to active users in the future! (2020-05-23)
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
Find all one time orders that have a starting date that falls into this month
|
|
|
|
recurring_period=RecurringPeriod.ONE_TIME,
|
|
|
|
|
|
|
|
Can we do this even for recurring / all of them
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# FIXME: add something to check whether the order should be billed at all - i.e. a marker that
|
|
|
|
# disables searching -> optimization for later
|
|
|
|
# Create the initial bill record
|
|
|
|
# FIXME: maybe limit not even to starting/ending date, but to empty_bill record -- to be fixed in the future
|
|
|
|
# for order in Order.objects.filter(Q(starting_date__gte=self.starting_date),
|
|
|
|
# Q(starting_date__lte=self.ending_date),
|
|
|
|
|
|
|
|
# FIXME below: only check for active orders
|
|
|
|
|
|
|
|
# Ensure all orders of that owner have at least one bill record
|
|
|
|
for order in Order.objects.filter(owner=owner,
|
|
|
|
bill_records=None):
|
|
|
|
|
|
|
|
bill_record = BillRecord.objects.create(bill=self,
|
2020-06-21 14:42:55 +00:00
|
|
|
quantity=1,
|
2020-06-21 11:46:54 +00:00
|
|
|
starting_date=order.starting_date,
|
|
|
|
ending_date=order.starting_date + timedelta(seconds=order.recurring_period))
|
|
|
|
|
|
|
|
|
|
|
|
# For each recurring order get the usage and bill it
|
|
|
|
for order in Order.objects.filter(~Q(recurring_period=RecurringPeriod.ONE_TIME),
|
|
|
|
Q(starting_date__lt=self.starting_date),
|
|
|
|
owner=owner):
|
|
|
|
|
|
|
|
if order.recurring_period > 0: # avoid div/0 - these are one time payments
|
|
|
|
|
|
|
|
# How much time will have passed by the end of the billing cycle
|
|
|
|
td = self.ending_date - order.starting_date
|
|
|
|
|
|
|
|
# How MANY times it will have been used by then
|
|
|
|
used_times = ceil(td / timedelta(seconds=order.recurring_period))
|
|
|
|
|
|
|
|
billed_times = len(order.bills)
|
|
|
|
|
|
|
|
# How many times it WAS billed -- can also be inferred from the bills that link to it!
|
|
|
|
if used_times > billed_times:
|
|
|
|
billing_times = used_times - billed_times
|
|
|
|
|
|
|
|
# ALSO REGISTER THE TIME PERIOD!
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class BillRecord(models.Model):
|
|
|
|
"""
|
|
|
|
Entry of a bill, dynamically generated from an order.
|
|
|
|
"""
|
|
|
|
|
|
|
|
bill = models.ForeignKey(Bill, on_delete=models.CASCADE)
|
|
|
|
order = models.ForeignKey(Order, on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
# How many times the order has been used in this record
|
2020-06-21 14:42:55 +00:00
|
|
|
quantity = models.IntegerField(default=1)
|
2020-06-21 11:46:54 +00:00
|
|
|
|
|
|
|
# The timeframe the bill record is for can (and probably often will) differ
|
|
|
|
# from the bill time
|
|
|
|
creation_date = models.DateTimeField(auto_now_add=True)
|
|
|
|
starting_date = models.DateTimeField()
|
|
|
|
ending_date = models.DateTimeField()
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-06-21 14:42:55 +00:00
|
|
|
return f"{self.bill}: {self.quantity} x {self.order}"
|
2020-06-21 11:46:54 +00:00
|
|
|
|
2020-04-13 09:54:41 +00:00
|
|
|
|
2020-03-02 07:09:42 +00:00
|
|
|
class OrderRecord(models.Model):
|
2020-03-03 10:34:47 +00:00
|
|
|
"""
|
|
|
|
Order records store billing informations for products: the actual product
|
|
|
|
might be mutated and/or moved to another order but we do not want to loose
|
|
|
|
the details of old orders.
|
|
|
|
|
|
|
|
Used as source of trust to dynamically generate bill entries.
|
|
|
|
"""
|
|
|
|
|
2020-03-02 07:09:42 +00:00
|
|
|
order = models.ForeignKey(Order, on_delete=models.CASCADE)
|
2020-06-21 11:46:54 +00:00
|
|
|
|
2020-03-03 10:27:35 +00:00
|
|
|
one_time_price = models.DecimalField(default=0.0,
|
2020-03-02 07:09:42 +00:00
|
|
|
max_digits=AMOUNT_MAX_DIGITS,
|
|
|
|
decimal_places=AMOUNT_DECIMALS,
|
|
|
|
validators=[MinValueValidator(0)])
|
|
|
|
recurring_price = models.DecimalField(default=0.0,
|
|
|
|
max_digits=AMOUNT_MAX_DIGITS,
|
|
|
|
decimal_places=AMOUNT_DECIMALS,
|
|
|
|
validators=[MinValueValidator(0)])
|
|
|
|
|
|
|
|
description = models.TextField()
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-03-21 10:59:04 +00:00
|
|
|
|
2020-03-02 07:09:42 +00:00
|
|
|
@property
|
|
|
|
def recurring_period(self):
|
|
|
|
return self.order.recurring_period
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-03-03 07:53:19 +00:00
|
|
|
@property
|
|
|
|
def starting_date(self):
|
|
|
|
return self.order.starting_date
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ending_date(self):
|
|
|
|
return self.order.ending_date
|
|
|
|
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
###
|
|
|
|
# Products
|
2020-02-27 19:29:50 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
# Abstract (= no database representation) class used as parent for products
|
|
|
|
# (e.g. uncloud_vm.models.VMProduct).
|
2020-03-21 10:59:04 +00:00
|
|
|
class Product(UncloudModel):
|
2020-02-27 19:29:50 +00:00
|
|
|
owner = models.ForeignKey(get_user_model(),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
editable=False)
|
|
|
|
|
2020-03-10 08:14:52 +00:00
|
|
|
description = "Generic Product"
|
2020-02-27 19:29:50 +00:00
|
|
|
|
2020-03-04 08:39:18 +00:00
|
|
|
status = models.CharField(max_length=32,
|
2020-03-22 19:55:11 +00:00
|
|
|
choices=UncloudStatus.choices,
|
2020-03-10 08:14:52 +00:00
|
|
|
default=UncloudStatus.AWAITING_PAYMENT)
|
2020-02-27 19:29:50 +00:00
|
|
|
|
|
|
|
order = models.ForeignKey(Order,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
editable=False,
|
|
|
|
null=True)
|
|
|
|
|
2020-04-12 09:35:37 +00:00
|
|
|
# Default period for all products
|
2020-05-23 21:32:45 +00:00
|
|
|
default_recurring_period = RecurringPeriod.PER_30D
|
2020-04-12 09:35:37 +00:00
|
|
|
|
2020-03-09 16:25:02 +00:00
|
|
|
# Used to save records.
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
# _state.adding is switched to false after super(...) call.
|
|
|
|
being_created = self._state.adding
|
|
|
|
|
2020-05-01 22:16:29 +00:00
|
|
|
# First time saving - create an order
|
|
|
|
if not self.order:
|
|
|
|
billing_address = BillingAddress.get_preferred_address_for(self.owner)
|
2020-05-02 20:03:34 +00:00
|
|
|
print(billing_address)
|
2020-05-01 22:16:29 +00:00
|
|
|
|
|
|
|
if not billing_address:
|
2020-05-02 19:20:14 +00:00
|
|
|
raise ValidationError("Cannot order without a billing address")
|
2020-05-01 22:16:29 +00:00
|
|
|
|
2020-05-07 18:22:42 +00:00
|
|
|
# FIXME: allow user to choose recurring_period
|
2020-05-02 20:03:34 +00:00
|
|
|
self.order = Order.objects.create(owner=self.owner,
|
2020-05-02 21:44:20 +00:00
|
|
|
billing_address=billing_address,
|
|
|
|
one_time_price=self.one_time_price,
|
2020-05-07 18:22:42 +00:00
|
|
|
recurring_period=self.default_recurring_period,
|
2020-05-02 21:44:20 +00:00
|
|
|
recurring_price=self.recurring_price)
|
2020-05-01 22:16:29 +00:00
|
|
|
|
2020-05-02 20:03:34 +00:00
|
|
|
super().save(*args, **kwargs)
|
2020-03-09 16:25:02 +00:00
|
|
|
|
2020-05-02 20:03:34 +00:00
|
|
|
# # Make sure we only create records on creation.
|
|
|
|
# if being_created:
|
|
|
|
# record = OrderRecord(
|
|
|
|
# one_time_price=self.one_time_price,
|
|
|
|
# recurring_price=self.recurring_price,
|
|
|
|
# description=self.description)
|
|
|
|
# self.order.orderrecord_set.add(record, bulk=False)
|
2020-04-12 09:35:37 +00:00
|
|
|
|
2020-02-27 19:29:50 +00:00
|
|
|
@property
|
2020-04-18 08:43:23 +00:00
|
|
|
def recurring_price(self):
|
2020-02-27 19:29:50 +00:00
|
|
|
pass # To be implemented in child.
|
|
|
|
|
2020-02-28 15:26:45 +00:00
|
|
|
@property
|
2020-03-03 10:27:35 +00:00
|
|
|
def one_time_price(self):
|
2020-02-28 15:26:45 +00:00
|
|
|
return 0
|
|
|
|
|
2020-04-18 08:43:23 +00:00
|
|
|
@property
|
|
|
|
def billing_address(self):
|
|
|
|
return self.order.billing_address
|
|
|
|
|
2020-03-03 09:51:16 +00:00
|
|
|
@staticmethod
|
|
|
|
def allowed_recurring_periods():
|
|
|
|
return RecurringPeriod.choices
|
|
|
|
|
2020-02-27 19:29:50 +00:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2020-04-12 09:35:37 +00:00
|
|
|
|
|
|
|
def discounted_price_by_period(self, requested_period):
|
|
|
|
"""
|
|
|
|
Each product has a standard recurring period for which
|
|
|
|
we define a pricing. I.e. VPN is usually year, VM is usually monthly.
|
|
|
|
|
|
|
|
The user can opt-in to use a different period, which influences the price:
|
|
|
|
The longer a user commits, the higher the discount.
|
|
|
|
|
|
|
|
Products can also be limited in the available periods. For instance
|
|
|
|
a VPN only makes sense to be bought for at least one day.
|
|
|
|
|
|
|
|
Rules are as follows:
|
|
|
|
|
|
|
|
given a standard recurring period of ..., changing to ... modifies price ...
|
|
|
|
|
|
|
|
|
|
|
|
# One month for free if buying / year, compared to a month: about 8.33% discount
|
|
|
|
per_year -> per_month -> /11
|
|
|
|
per_month -> per_year -> *11
|
|
|
|
|
|
|
|
# Month has 30.42 days on average. About 7.9% discount to go monthly
|
|
|
|
per_month -> per_day -> /28
|
|
|
|
per_day -> per_month -> *28
|
|
|
|
|
|
|
|
# Day has 24h, give one for free
|
|
|
|
per_day -> per_hour -> /23
|
|
|
|
per_hour -> per_day -> /23
|
|
|
|
|
|
|
|
|
|
|
|
Examples
|
|
|
|
|
|
|
|
VPN @ 120CHF/y becomes
|
|
|
|
- 10.91 CHF/month (130.91 CHF/year)
|
|
|
|
- 0.39 CHF/day (142.21 CHF/year)
|
|
|
|
|
|
|
|
VM @ 15 CHF/month becomes
|
|
|
|
- 165 CHF/month (13.75 CHF/month)
|
|
|
|
- 0.54 CHF/day (16.30 CHF/month)
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-05-23 21:32:45 +00:00
|
|
|
if self.default_recurring_period == RecurringPeriod.PER_365D:
|
|
|
|
if requested_period == RecurringPeriod.PER_365D:
|
2020-04-12 09:35:37 +00:00
|
|
|
return self.recurring_price
|
2020-05-23 21:32:45 +00:00
|
|
|
if requested_period == RecurringPeriod.PER_30D:
|
2020-04-12 09:35:37 +00:00
|
|
|
return self.recurring_price/11.
|
|
|
|
if requested_period == RecurringPeriod.PER_DAY:
|
|
|
|
return self.recurring_price/11./28.
|
|
|
|
|
2020-05-23 21:32:45 +00:00
|
|
|
elif self.default_recurring_period == RecurringPeriod.PER_30D:
|
|
|
|
if requested_period == RecurringPeriod.PER_365D:
|
2020-04-12 09:35:37 +00:00
|
|
|
return self.recurring_price*11
|
2020-05-23 21:32:45 +00:00
|
|
|
if requested_period == RecurringPeriod.PER_30D:
|
2020-04-12 09:35:37 +00:00
|
|
|
return self.recurring_price
|
|
|
|
if requested_period == RecurringPeriod.PER_DAY:
|
|
|
|
return self.recurring_price/28.
|
|
|
|
|
|
|
|
elif self.default_recurring_period == RecurringPeriod.PER_DAY:
|
2020-05-23 21:32:45 +00:00
|
|
|
if requested_period == RecurringPeriod.PER_365D:
|
2020-04-12 09:35:37 +00:00
|
|
|
return self.recurring_price*11*28
|
2020-05-23 21:32:45 +00:00
|
|
|
if requested_period == RecurringPeriod.PER_30D:
|
2020-04-12 09:35:37 +00:00
|
|
|
return self.recurring_price*28
|
|
|
|
if requested_period == RecurringPeriod.PER_DAY:
|
|
|
|
return self.recurring_price
|
|
|
|
else:
|
|
|
|
# FIXME: use the right type of exception here!
|
|
|
|
raise Exception("Did not implement the discounter for this case")
|
2020-05-24 11:45:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Interesting snippets
|
|
|
|
|
|
|
|
# # Trigger initial bill generation at order creation.
|
|
|
|
# def save(self, *args, **kwargs):
|
|
|
|
# if self.ending_date and self.ending_date < self.starting_date:
|
|
|
|
# raise ValidationError("End date cannot be before starting date")
|
|
|
|
|
|
|
|
# super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
# Bill.generate_for(self.starting_date.year, self.starting_date.month, self.owner)
|