diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index 0b7a132..afa3e96 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -31,14 +31,17 @@ logger = logging.getLogger(__name__) # See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types class RecurringPeriod(models.TextChoices): - ONE_TIME = 'ONCE', _('Onetime') - PER_YEAR = 'YEAR', _('Per Year') - PER_MONTH = 'MONTH', _('Per Month') - PER_WEEK = 'WEEK', _('Per Week') - PER_DAY = 'DAY', _('Per Day') - PER_HOUR = 'HOUR', _('Per Hour') - PER_MINUTE = 'MINUTE', _('Per Minute') - PER_SECOND = 'SECOND', _('Per Second') + PER_YEAR = 'YEAR', _('Per Year') # this is broken - we can make it 365 days + PER_MONTH = 'MONTH', _('Per Month') # this is broken - varying times + + ONE_TIME = 'ONCE', _('Onetime') # this is ok + PER_365D = '365D', _('Per 365 days') # this is ok + PER_30D = '30D', _('Per 30 days') # this is ok + PER_WEEK = 'WEEK', _('Per Week') # this is ok + PER_DAY = 'DAY', _('Per Day') # this is ok + PER_HOUR = 'HOUR', _('Per Hour') # this is ok + PER_MINUTE = 'MINUTE', _('Per Minute') # this is ok + PER_SECOND = 'SECOND', _('Per Second') # this is ok class CountryField(models.CharField): def __init__(self, *args, **kwargs): @@ -245,6 +248,10 @@ class VATRate(models.Model): return 0 class BillNico(models.Model): + """ FIXME: + Bill needs to be unique in the triple (owner, year, month) + """ + uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) @@ -256,6 +263,54 @@ class BillNico(models.Model): valid = models.BooleanField(default=True) + @staticmethod + def create_all_bills(): + for owner in get_user_model().objects.all(): + # mintime = time of first order + # maxtime = time of last order + # iterate month based through it + 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 + """ + + """ + 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 + + """ + for order in Order.objects.filter(owner=owner, + Q(starting_date__gte=self.starting_date), + Q(starting_date__lte=self.ending_date)): + order.bill.add(this_bill) + + + """ + Find all recurring orders that did not start in this time frame, but need + to be billed in this time frame. + + This is: + - order starting time before our starting time + - order start time + (x * (the_period)) is inside our time frame, x must be integer + test cases: + + 365days: + time_since_last_billed = self.starting_or_ending_date - order.last_bill_date + periods = + [ we could in theory add this as a property to the order: next + """ + for order in Order.objects.filter(owner=owner, + ~Q(recurring_period=RecurringPeriod.ONE_TIME), + Q(starting_date__gte=starting_date), + Q(starting_date__lte=ending_date)): + + + class Bill(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) owner = models.ForeignKey(get_user_model(), @@ -488,7 +543,7 @@ class BillRecord(): def __init__(self, bill, order): self.bill = bill self.order = order - self.recurring_price = order.recurring_price +yep self.recurring_price = order.recurring_price self.recurring_period = order.recurring_period self.description = order.description