Include BillRecords in the admin

This commit is contained in:
Nico Schottelius 2020-06-21 16:08:00 +02:00
commit 8a17ee6de5
4 changed files with 128 additions and 21 deletions

View file

@ -10,7 +10,7 @@ import logging
from functools import reduce
import itertools
from math import ceil
from datetime import timedelta
import datetime
from calendar import monthrange
from decimal import Decimal
@ -23,11 +23,36 @@ from decimal import Decimal
import decimal
# Used to generate bill due dates.
BILL_PAYMENT_DELAY=timedelta(days=10)
BILL_PAYMENT_DELAY=datetime.timedelta(days=10)
# Initialize logger.
logger = logging.getLogger(__name__)
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
# See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types
class RecurringPeriod(models.IntegerChoices):
"""
@ -390,34 +415,41 @@ class Order(models.Model):
description=description)
def __str__(self):
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)
return f"Order {self.owner}-{self.id}"
# 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)
class Bill(models.Model):
""" FIXME:
Bill needs to be unique in the triple (owner, year, month)
"""
owner = models.ForeignKey(get_user_model(),
on_delete=models.CASCADE)
creation_date = models.DateTimeField(auto_now_add=True)
starting_date = models.DateTimeField()
ending_date = models.DateTimeField()
due_date = models.DateField()
# 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)
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")
class Meta:
constraints = [
models.UniqueConstraint(fields=['owner',
'starting_date',
'ending_date' ],
name='one_bill_per_month_per_user')
]
# billing address and vat rate is the same for the whole bill
# @property
# def vat_rate(self):
@ -425,7 +457,7 @@ class Bill(models.Model):
def __str__(self):
return f"uc-{self.id}"
return f"Bill {self.owner}-{self.id}"
@classmethod
def create_all_bills(cls):
@ -516,7 +548,7 @@ class BillRecord(models.Model):
ending_date = models.DateTimeField()
def __str__(self):
return f"{order.owner}"
return f"{self.bill}: {self.usage_count} x {self.order}"
class OrderRecord(models.Model):