From 186d45a8e4f33138ac065e9bb1c325567d2e5bdc Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Sun, 13 Oct 2024 08:28:19 +0200 Subject: [PATCH] fix --- alkatorapi/models.py | 15 +++++++++++++++ alkatorapi/views.py | 12 +----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/alkatorapi/models.py b/alkatorapi/models.py index 8093cec..9128cd4 100644 --- a/alkatorapi/models.py +++ b/alkatorapi/models.py @@ -96,6 +96,21 @@ class Invoice(models.Model): trans_id = models.CharField(null=True, blank=True, max_length=120) address = models.CharField(max_length=255, null=True, blank=True) + @classmethod + def next_invoice_id(cls): + invoice_date = datetime.today() + invoice_id = invoice_date.year * 1000000 + invoice_date.month * 10000 + invoice_date.day * 100 + + try: + latest_invoice = cls.objects.latest("invoice_id") + if latest_invoice.invoice_id is None or latest_invoice.invoice_id < invoice_id: + invoice_id = invoice_id + 1 + else: + invoice_id = latest_invoice.invoice_id + 1 + except Invoice.DoesNotExist: + invoice_id = invoice_id + 1 + return invoice_id + def calculate_total_price(self): total_price = 0 for item in InvoiceProduct.objects.filter(invoice=self): diff --git a/alkatorapi/views.py b/alkatorapi/views.py index a85f5be..54bd0e5 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -102,17 +102,7 @@ def register_racer(request): except: return HttpResponse('{"reason":"Špatný formát datu narození!"}', status=400, content_type='application/json') - invoice_date = datetime.today() - invoice_id = invoice_date.year * 1000000 + invoice_date.month * 10000 + invoice_date.day * 100 - - try: - latest_racer = Racer.objects.latest("invoice_id") - if latest_racer.invoice_id is None or latest_racer.invoice_id < invoice_id: - invoice_id = invoice_id + 1 - else: - invoice_id = latest_racer.invoice_id + 1 - except Racer.DoesNotExist: - invoice_id = invoice_id + 1 + invoice_id = Invoice.next_invoice_id() profile = request.user.profile user = request.user