works better now

This commit is contained in:
MassiveAtoms 2020-05-24 16:05:50 -03:00
commit 6acc614785
7 changed files with 44 additions and 9 deletions

Binary file not shown.

View File

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

View File

@ -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',
),
]

View File

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