Fix RecurMixin

This commit is contained in:
Kumi 2020-06-06 15:21:47 +02:00
parent 4dc0b86378
commit 50ccd9f0a5
2 changed files with 8 additions and 4 deletions

View file

@ -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

View file

@ -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):