This commit is contained in:
Nico Schottelius 2022-01-01 23:35:22 +01:00
parent 5b170ed521
commit e726795495
3 changed files with 24 additions and 17 deletions

View file

@ -19,6 +19,7 @@
</title> </title>
{% block css_extra %} {% endblock css_extra %} {% block css_extra %} {% endblock css_extra %}
{% block head_extra %} {% endblock head_extra %}
{# Load CSS and JavaScript #} {# Load CSS and JavaScript #}
{% bootstrap_css %} {% bootstrap_css %}

View file

@ -136,11 +136,11 @@ class Payment(models.Model):
@classmethod @classmethod
def withdraw(cls, owner, amount, currency='CHF', notes=''): def withdraw(cls, owner, amount, currency='CHF', notes=''):
return cls.objects.create(owner=owner, type="withdraw", amount=amount, return cls.objects.create(owner=owner, type="withdraw", amount=amount,
currency=currency, notes=notes) currency=currency, notes=notes)
# See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types # See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types
class RecurringPeriodDefaultChoices(models.IntegerChoices): class RecurringPeriodDefaultChoices(models.IntegerChoices):
""" """
@ -240,7 +240,7 @@ class BillingAddress(UncloudAddress):
self.owner, self.owner,
self.full_name, self.street, self.postal_code, self.city, self.full_name, self.street, self.postal_code, self.city,
self.country) self.country)
@staticmethod @staticmethod
def get_address_for(user): def get_address_for(user):
return BillingAddress.objects.get(owner=user) return BillingAddress.objects.get(owner=user)
@ -654,6 +654,7 @@ class Order(models.Model):
billing_address = models.ForeignKey(BillingAddress, billing_address = models.ForeignKey(BillingAddress,
on_delete=models.CASCADE) on_delete=models.CASCADE)
# Let's forget about this one
customer = models.ForeignKey(StripeCustomer, on_delete=models.CASCADE, null=True) customer = models.ForeignKey(StripeCustomer, on_delete=models.CASCADE, null=True)
product = models.ForeignKey(Product, blank=False, null=False, on_delete=models.CASCADE) product = models.ForeignKey(Product, blank=False, null=False, on_delete=models.CASCADE)
@ -720,7 +721,7 @@ class Order(models.Model):
if self.recurring_period.duration_seconds > 0: if self.recurring_period.duration_seconds > 0:
delta = until_when - self.starting_date delta = until_when - self.starting_date
num_times = ceil(delta.total_seconds() / self.recurring_period.duration_seconds) num_times = ceil(delta.total_seconds() / self.recurring_period.duration_seconds)
next_date = self.starting_date + datetime.timedelta(seconds=num_times * self.recurring_period.duration_seconds) next_date = self.starting_date + datetime.timedelta(seconds=num_times * self.recurring_period.duration_seconds)
@ -755,7 +756,7 @@ class Order(models.Model):
""" """
return sum([ br.quantity for br in self.bill_records.all() ]) return sum([ br.quantity for br in self.bill_records.all() ])
def cancel(self): def cancel(self):
self.ending_date = timezone.now() self.ending_date = timezone.now()
self.should_be_billed = False self.should_be_billed = False
@ -798,7 +799,7 @@ class Order(models.Model):
@property @property
def is_one_time(self): def is_one_time(self):
return not self.is_recurring return not self.is_recurring
@property @property
def description(self): def description(self):
desc = self.product.description + "( " desc = self.product.description + "( "
@ -843,7 +844,7 @@ class Order(models.Model):
return new_order return new_order
def create_bill_record(self, bill): def create_bill_record(self, bill):
br = None br = None
if self.recurring_price != 0: if self.recurring_price != 0:
@ -922,7 +923,7 @@ class Order(models.Model):
if 'memory' in config: if 'memory' in config:
recurring_price += self.pricing_plan.ram_unit_price * int(config['memory']) recurring_price += self.pricing_plan.ram_unit_price * int(config['memory'])
if 'storage' in config: if 'storage' in config:
#TODO Fix the ssd static value #TODO Fix the ssd static value
recurring_price += (10 * self.pricing_plan.storage_ssd_unit_price) + (self.pricing_plan.storage_hd_unit_price * int(config['storage'])) recurring_price += (10 * self.pricing_plan.storage_ssd_unit_price) + (self.pricing_plan.storage_hd_unit_price * int(config['storage']))
vat_rate = VATRate.get_vat_rate(self.billing_address) vat_rate = VATRate.get_vat_rate(self.billing_address)
@ -933,7 +934,7 @@ class Order(models.Model):
) )
return price_after_discount_with_vat return price_after_discount_with_vat
except Exception as e: except Exception as e:
logger.error("An error occurred while parsing the config obj", e) logger.error("An error occurred while parsing the config obj", e)
return 0 return 0
def check_parameters(self): def check_parameters(self):
@ -987,7 +988,7 @@ class Bill(models.Model):
on_delete=models.CASCADE, on_delete=models.CASCADE,
editable=True, editable=True,
null=False) null=False)
currency = models.CharField(max_length=32, choices=Currency.choices, default=Currency.CHF) currency = models.CharField(max_length=32, choices=Currency.choices, default=Currency.CHF)
# FIXME: editable=True -> is in the admin, but also editable in DRF # FIXME: editable=True -> is in the admin, but also editable in DRF
@ -1067,10 +1068,10 @@ class Bill(models.Model):
owner = billing_address.owner owner = billing_address.owner
all_orders = Order.objects.filter(Q(owner__id=owner.id), Q(should_be_billed=True), all_orders = Order.objects.filter(Q(owner__id=owner.id), Q(should_be_billed=True),
Q(billing_address__id=billing_address.id) Q(billing_address__id=billing_address.id)
).order_by('id') ).order_by('id')
if len(all_orders) > 0: if len(all_orders) > 0:
bill = cls.get_or_create_bill(billing_address, ending_date=ending_date) bill = cls.get_or_create_bill(billing_address, ending_date=ending_date)
for order in all_orders: for order in all_orders:
@ -1084,7 +1085,7 @@ class Bill(models.Model):
def create_next_bill_for_order(cls, order, ending_date=None): def create_next_bill_for_order(cls, order, ending_date=None):
""" """
Create the next bill for a specific order of a user Create the next bill for a specific order of a user
""" """
bill = cls.get_or_create_bill(order.billing_address, ending_date=ending_date) bill = cls.get_or_create_bill(order.billing_address, ending_date=ending_date)
order.create_bill_record(bill) order.create_bill_record(bill)
return bill return bill
@ -1122,7 +1123,7 @@ class Bill(models.Model):
if not ending_date: if not ending_date:
ending_date = end_of_month(starting_date) ending_date = end_of_month(starting_date)
if not bill: if not bill:
bill = cls.objects.create( bill = cls.objects.create(
owner=billing_address.owner, owner=billing_address.owner,
@ -1132,7 +1133,7 @@ class Bill(models.Model):
return bill return bill
def __str__(self): def __str__(self):
return f"{self.owner}-{self.id}" return f"{self.owner}-{self.id}"
@ -1182,7 +1183,7 @@ class BillRecord(models.Model):
return self.order.recurring_price return self.order.recurring_price
else: else:
return self.order.one_time_price return self.order.one_time_price
@property @property
def subtotal(self): def subtotal(self):
billing_address_ins = self.order.billing_address billing_address_ins = self.order.billing_address

View file

@ -1,7 +1,11 @@
{% extends 'uncloud/base.html' %} {% extends 'uncloud/base.html' %}
{% block head_extra %}
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://js.stripe.com/v3/"></script> <script src="https://js.stripe.com/v3/"></script>
{% endblock %} {% endblock %}
{% block content %}
<div class="container"> <div class="container">
{% csrf_token %} {% csrf_token %}
<div id="content"> <div id="content">
@ -38,7 +42,7 @@
var setupForm = document.getElementById('setup-form'); var setupForm = document.getElementById('setup-form');
var clientSecret = setupForm.dataset.secret; var clientSecret = setupForm.dataset.secret;
var stripe = Stripe('{{ stripe_pk }}'); var stripe = Stripe('{{ stripe_pk }}');
var cardButton = document.getElementById('card-button'); var cardButton = document.getElementById('card-button');
var messageContainer = document.getElementById('message'); var messageContainer = document.getElementById('message');
var backmessage = document.getElementById('goback'); var backmessage = document.getElementById('goback');
@ -73,3 +77,4 @@
}); });
}); });
</script> </script>
{% endblock %}