diff --git a/db.sqlite3 b/db.sqlite3 index cd19cc0..4d3f068 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/markt/__pycache__/admin.cpython-38.pyc b/markt/__pycache__/admin.cpython-38.pyc index 5e1ab1c..81fa099 100644 Binary files a/markt/__pycache__/admin.cpython-38.pyc and b/markt/__pycache__/admin.cpython-38.pyc differ diff --git a/markt/__pycache__/models.cpython-38.pyc b/markt/__pycache__/models.cpython-38.pyc index fe50b7b..a8f3c5d 100644 Binary files a/markt/__pycache__/models.cpython-38.pyc and b/markt/__pycache__/models.cpython-38.pyc differ diff --git a/markt/admin.py b/markt/admin.py index ba45202..8a3cf08 100644 --- a/markt/admin.py +++ b/markt/admin.py @@ -9,7 +9,7 @@ class OrderAdmin(admin.ModelAdmin): @admin.register(Factuur) class FactuurAdmin(admin.ModelAdmin): - list_display = ("Factuur_ID", "Klant_ID", "total" ) + list_display = ("Factuur_ID", "Klant_ID", "Total" ) diff --git a/markt/migrations/0011_auto_20200524_1559.py b/markt/migrations/0011_auto_20200524_1559.py new file mode 100644 index 0000000..fcf1f10 --- /dev/null +++ b/markt/migrations/0011_auto_20200524_1559.py @@ -0,0 +1,21 @@ +# Generated by Django 3.0.6 on 2020-05-24 18:59 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('markt', '0010_auto_20200524_1530'), + ] + + operations = [ + migrations.RemoveField( + model_name='factuur', + name='Subtotal', + ), + migrations.RemoveField( + model_name='factuur', + name='total', + ), + ] diff --git a/markt/migrations/__pycache__/0011_auto_20200524_1559.cpython-38.pyc b/markt/migrations/__pycache__/0011_auto_20200524_1559.cpython-38.pyc new file mode 100644 index 0000000..acb9a3e Binary files /dev/null and b/markt/migrations/__pycache__/0011_auto_20200524_1559.cpython-38.pyc differ diff --git a/markt/models.py b/markt/models.py index f8086ce..1b8fe75 100644 --- a/markt/models.py +++ b/markt/models.py @@ -1,6 +1,7 @@ from django.db import models # Used to generate URLs by reversing the URL patterns from django.urls import reverse +from django.db.models import Sum # Create your models here. @@ -135,7 +136,6 @@ class Order(models.Model): new_amount = self.Voorraad_ID.Voorraad_Amount - self.Amount if new_amount < 0: raise ValueError("Er zijn maar {} in voorraad".format(self.Voorraad_ID.Voorraad_Amount)) - self.Voorraad_ID.Voorraad_Amount -= self.Amount self.Voorraad_ID.save() return super(Order, self).save(*args, **kwargs) @@ -146,8 +146,8 @@ class Factuur(models.Model): Klant_ID = models.ForeignKey("Klant", on_delete=models.PROTECT) Orders = models.ManyToManyField("Order", null=True, blank=True) Korting_percent = models.FloatField() - Subtotal = models.FloatField(null=True, blank=True) - total = models.FloatField(null=True, blank=True) + # Subtotal = models.FloatField(null=True, blank=True) + # total = models.FloatField(null=True, blank=True) def get_absolute_url(self): """Returns the url to access a particular instance of the model.""" @@ -157,12 +157,26 @@ class Factuur(models.Model): """String for representing the Model object.""" return str(self.Factuur_ID) - def save(self, *args, **kwargs): - super(Factuur, self).save(*args, **kwargs) - self.Subtotal = sum([i.Prijs for i in self.Orders.all()]) - self.total = self.Subtotal * (1 - self.Korting_percent/100) + @property + def Subtotal(self): + if self.pk: + return self.Orders.all().aggregate(Sum('Prijs'))['Prijs__sum'] + else: + return 0 - return super(Factuur, self).save(*args, **kwargs) + @property + def Total(self): + if self.pk: + return (100 - self.Korting_percent) * self.Subtotal / 100 + else: + return 0 + + # def save(self, *args, **kwargs): + # super(Factuur, self).save(*args, **kwargs) + # self.Subtotal = sum([i.Prijs for i in self.Orders.all()]) + # self.total = self.Subtotal * (1 - self.Korting_percent/100) + + # return super(Factuur, self).save(*args, **kwargs) class Meta: db_table = "Factuur"