Fix #1080 -- Deal with gaps in the invoice database (#1086)

This commit is contained in:
Raphael Michel
2018-11-20 10:36:13 +01:00
committed by GitHub
parent 1cba4b1d45
commit c93f804992
2 changed files with 18 additions and 5 deletions

View File

@@ -2,6 +2,8 @@ import string
from decimal import Decimal
from django.db import DatabaseError, models, transaction
from django.db.models import Max
from django.db.models.functions import Cast
from django.utils import timezone
from django.utils.crypto import get_random_string
from django.utils.functional import cached_property
@@ -124,8 +126,12 @@ class Invoice(models.Model):
numeric_invoices = Invoice.objects.filter(
event__organizer=self.event.organizer,
prefix=self.prefix,
).exclude(invoice_no__contains='-')
return self._to_numeric_invoice_number(numeric_invoices.count() + 1)
).exclude(invoice_no__contains='-').annotate(
numeric_number=Cast('invoice_no', models.IntegerField())
).aggregate(
max=Max('numeric_number')
)['max'] or 0
return self._to_numeric_invoice_number(numeric_invoices + 1)
def _get_invoice_number_from_order(self):
return '{order}-{count}'.format(
@@ -183,7 +189,7 @@ class Invoice(models.Model):
class Meta:
unique_together = ('organizer', 'prefix', 'invoice_no')
ordering = ('invoice_no',)
ordering = ('date', 'invoice_no',)
class InvoiceLine(models.Model):