From 50ccd9f0a54ae4bb350ae305a91cb893effaf2d1 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sat, 6 Jun 2020 15:21:47 +0200 Subject: [PATCH] Fix RecurMixin --- core/mixins/billable.py | 11 ++++++++--- core/models/billable.py | 1 - 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/core/mixins/billable.py b/core/mixins/billable.py index b465fd7..6770024 100644 --- a/core/mixins/billable.py +++ b/core/mixins/billable.py @@ -1,5 +1,7 @@ from django.db.models import PositiveIntegerField, DateField, IntegerChoices, Model +from dateutil.relativedelta import relativedelta + class ActionChoices(IntegerChoices): WAIT = 0, "Do not invoice for now" NEXT = 1, "Add to client's next invoice" @@ -25,11 +27,15 @@ class RecurMixin(Model): def next_invoicing(self): from core.models.invoices import InvoiceItem, Invoice - if not self.recur_action == ActionChoices.DATE: + if not (self.recur_action == ActionChoices.DATE) or self.recur_cycle: return False try: invoiceitems = InvoiceItem.objects.filter(billable=self) + + if invoiceitems and not self.recur_cycle: + return False + invoice = Invoice.objects.filter(invoiceitem_set__contains=invoiceitems).latest("created") delta = relativedelta(days=self.recur_count if self.recur_cycle == CycleChoices.DAYS else 0, weeks=self.recur_count if self.recur_cycle == CycleChoices.WEEKS else 0, @@ -37,6 +43,5 @@ class RecurMixin(Model): years=self.recur_count if self.recur_cycle == CycleChoices.YEARS else 0) return invoice.created + delta - - except: + except InvoiceItem.DoesNotExist: return self.date \ No newline at end of file diff --git a/core/models/billable.py b/core/models/billable.py index 5b3c9a0..ebc6cb8 100644 --- a/core/models/billable.py +++ b/core/models/billable.py @@ -8,7 +8,6 @@ from core.fields.numbers import CostField from core.models.local import Currency from core.mixins.billable import RecurMixin -from dateutil.relativedelta import relativedelta from polymorphic.models import PolymorphicModel class BaseBillable(PolymorphicModel):